Esempio n. 1
0
def _code_map(state: PushState) -> PushState:
    if state["exec"].is_empty() or state["code"].is_empty():
        return Token.revert
    e = state["exec"].pop()
    c = state["code"].pop()
    if not isinstance(c, CodeBlock):
        c = CodeBlock([c])
    l1 = [CodeBlock([InstructionMeta(name="code_from_exec", code_blocks=1), item, e]) for item in c]
    l2 = [InstructionMeta(name="code_combine", code_blocks=0) for _ in c[1:]]
    contents = l1 + [InstructionMeta(name="code_wrap", code_blocks=0)] + l2
    state["exec"].push(CodeBlock(contents))
    return state
Esempio n. 2
0
def _code_do_count(state: PushState) -> Union[Token, PushState]:
    if state["code"].is_empty() or state["int"].is_empty():
        return Token.revert
    if state["int"].top() < 1:
        return Token.revert
    code = state["code"].pop()
    count = state["int"].pop()
    state["exec"].push(CodeBlock([
        Literal(value=0, push_type=PushInt),
        Literal(value=count - 1, push_type=PushInt),
        InstructionMeta(name="code_from_exec", code_blocks=1),
        code,
        InstructionMeta(name="code_do_range", code_blocks=0)
    ]))
    return state
Esempio n. 3
0
def _code_do_then_pop(state: PushState) -> Union[Token, PushState]:
    if state["code"].is_empty():
        return Token.revert
    c = state["code"].top()
    state["exec"].push(InstructionMeta(name="code_pop", code_blocks=0))
    state["exec"].push(c)
    return state
Esempio n. 4
0
def _exec_do_while(state: PushState) -> PushState:
    if state["exec"].is_empty():
        return Token.revert
    code = state["exec"].top()
    state["exec"].push(InstructionMeta(name="exec_while", code_blocks=1))
    state["exec"].push(code)
    return state
Esempio n. 5
0
def _deserialize_atoms(lst, instr_set: InstructionSet):
    type_lib = instr_set.type_library
    atoms = []
    for atom_spec in lst:
        atom = None
        if isinstance(atom_spec, list):
            atom = CodeBlock(_deserialize_atoms(atom_spec, instr_set))
        else:
            atom_type = atom_spec["a"]
            if atom_type == "close":
                atom = Closer()
            elif atom_type == "lit":
                push_type = type_lib[atom_spec["t"]]
                value = push_type.coerce(atom_spec["v"])
                atom = Literal(value=value, push_type=push_type)
            elif atom_type == "input":
                atom = Input(input_index=atom_spec["i"])
            elif atom_type == "instr":
                instr = instr_set[atom_spec["n"]]
                atom = InstructionMeta(name=instr.name,
                                       code_blocks=instr.code_blocks)
            else:
                raise ValueError("bad atom spec {s}".format(s=atom_spec))
        atoms.append(atom)
    return atoms
Esempio n. 6
0
def _exec_do_times(state: PushState) -> Union[Token, PushState]:
    if state["exec"].is_empty() or state["int"].is_empty():
        return Token.revert
    if state["int"].top() < 1:
        return Token.revert
    code = state["exec"].pop()
    times = state["int"].pop()
    state["exec"].push(CodeBlock([
        Literal(value=0, push_type=PushInt),
        Literal(value=times - 1, push_type=PushInt),
        InstructionMeta(name="exec_do_range", code_blocks=1),
        CodeBlock([
            InstructionMeta(name="int_pop", code_blocks=0),
            code,
        ])
    ]))
    return state
Esempio n. 7
0
    def random_instruction(self) -> InstructionMeta:
        """Return a random Instruction from the InstructionSet.

        Returns
        -------
        pyshgp.push.atoms.InstructionMeta
            A randomly selected Literal.

        """
        i = np.random.choice(list(self.instruction_set.values()))
        return InstructionMeta(name=i.name, code_blocks=i.code_blocks)
Esempio n. 8
0
def _exec_while(state: PushState) -> Union[Token, PushState]:
    if state["exec"].is_empty():
        return Token.revert
    if state["bool"].is_empty():
        state["exec"].pop()
        return state
    code = state["exec"].top()
    if state["bool"].pop():
        state["exec"].push(InstructionMeta(name="exec_while", code_blocks=1))
        state["exec"].push(code)
    else:
        state["exec"].pop()
    return state
Esempio n. 9
0
def _code_do_range(state: PushState) -> Union[Token, PushState]:
    if state["code"].is_empty() or len(state["int"]) < 2:
        return Token.revert
    to_do = state["code"].pop()
    destintaiton_ndx = state["int"].pop()
    current_ndx = state["int"].pop()

    increment = 0
    if current_ndx < destintaiton_ndx:
        increment = 1
    elif current_ndx > destintaiton_ndx:
        increment = -1

    if not increment == 0:
        state["exec"].push(CodeBlock([
            Literal(value=current_ndx + increment, push_type=PushInt),
            Literal(value=destintaiton_ndx, push_type=PushInt),
            InstructionMeta(name="code_from_exec", code_blocks=1),
            to_do,
            InstructionMeta(name="code_do_range", code_blocks=0)
        ]))
    state["int"].push(current_ndx)
    state["exec"].push(to_do)
    return state
Esempio n. 10
0
def iterate(state: PushState, *, vec_type: PushType,
            el_type: PushType) -> Union[Token, PushState]:
    vec_type_name = vec_type.name
    el_type_name = el_type.name
    if state[vec_type_name].is_empty() or state["exec"].is_empty():
        return Token.revert
    vec = state[vec_type_name].pop()
    if len(vec) == 0:
        state["exec"].pop()
        return state
    elif len(vec) == 1:
        state[el_type_name].push(vec[0])
        return state
    else:
        top_exec = state["exec"].top()
        state["exec"].push(
            InstructionMeta(name=vec_type_name + "_iterate", code_blocks=1))
        state["exec"].push(
            Literal(value=vec_type.coerce(vec[1:]), push_type=vec_type))
        state["exec"].push(top_exec)
        state[el_type_name].push(vec[0])
        return state
import math
import sys

from pyshgp.push.atoms import Literal, CodeBlock, InstructionMeta
from pyshgp.push.types import Char, PushInt, PushStr, BoolVector, IntVector, FloatVector, CharVector, StrVector, \
    PushIntVector

# Shorthand
L = lambda v, t: Literal(value=v, push_type=t)
IM = lambda nm, cb: InstructionMeta(name=nm, code_blocks=cb)
CB = lambda *args: CodeBlock(list(args))

SPECS = [
    # COMMON
    {
        "instr": "bool_pop",
        "in": {"bool": [True, False]},
        "ex": {"bool": [True]}
    },
    {
        "instr": "int_dup",
        "in": {"int": [101]},
        "ex": {"int": [101, 101]}
    },
    {
        "instr": "float_dup_times",
        "in": {"int": [3], "float": [1.23]},
        "ex": {"int": [], "float": [1.23, 1.23, 1.23]}
    },
    {
        "instr": "bool_dup_times",
Esempio n. 12
0
 def meta(self) -> InstructionMeta:
     """Create an ``InstructionMeta`` from the instruction object."""
     return InstructionMeta(name=self.name, code_blocks=self.code_blocks)