예제 #1
0
def instructions(type_library: PushTypeLibrary):
    """Return all core printing instructions."""
    i = []

    for push_type in type_library.keys():
        i.append(SimpleInstruction(
            "print_{t}".format(t=push_type),
            _wrap,
            input_stacks=[push_type],
            output_stacks=["stdout"],
            code_blocks=0,
            docstring="Prints the top {t}.".format(t=push_type)
        )),
        i.append(SimpleInstruction(
            "println_{t}".format(t=push_type),
            _wrap_and_newline,
            input_stacks=[push_type],
            output_stacks=["stdout"],
            code_blocks=0,
            docstring="Prints the top {t}.".format(t=push_type)
        ))
    return i
예제 #2
0
def instructions():
    """Return all core printing instructions."""
    i = []

    for push_type in PUSH_TYPES:
        i.append(
            SimpleInstruction(
                "print_{t}".format(t=push_type.name),
                lambda x: [str(x)],
                input_types=[push_type.name],
                output_types=["stdout"],
                code_blocks=0,
                docstring="Prints the top {t}.".format(t=push_type.name)))
    return i
예제 #3
0
def point_distance(p1, p2):
    """Return the distance between two points."""
    delta_x = p2.x - p1.x
    delta_y = p2.y - p1.y
    return sqrt(pow(delta_x, 2.0) + pow(delta_y, 2.0)),


# Another function used to define of one of our custom instructions.
def point_from_floats(f1, f2):
    """Return a tuple containint a Point made from two floats."""
    return Point(f1, f2),


# Our custom instructions objects that manipulate points.
point_distance_insrt = SimpleInstruction(
    "point_dist", point_distance,
    ["point", "point"], ["float"], 0
)
point_from_floats_instr = SimpleInstruction(
    "point_from_floats", point_from_floats,
    ["float", "float"], ["point"], 0
)


# Training data
X = [[Point(row[0], row[1]), Point(row[2], row[3])] for row in np.random.rand(20, 4)]
y = [[point_distance(x[0], x[1])] for x in X]


# Custom type library that specifies we will be synthesizing programs that
# manipulate "floats" (built-in to pyshgp) and "points" (custom for this problem)
type_library = (
예제 #4
0
파일: text.py 프로젝트: bmetevier/pyshgp
def instructions():
    """Return all core text instructions."""
    i = []

    for push_type in ["str", "char"]:

        i.append(
            SimpleInstruction(
                "{t}_concat".format(t=push_type),
                _concat,
                input_types=[push_type, push_type],
                output_types=["str"],
                code_blocks=0,
                docstring=
                "Concatenates the top two {t}s and pushes the resulting string."
                .format(t=push_type)))

        i.append(
            SimpleInstruction(
                "str_insert_{t}".format(t=push_type),
                _insert,
                input_types=["str", push_type, "int"],
                output_types=["str"],
                code_blocks=0,
                docstring="""Inserts {t} into the top str at index `n` and pushes
            the resulting string. The value for `n` is taken from the int stack."""
                .format(t=push_type)))

        # Getting Characters

        i.append(
            SimpleInstruction(
                "{t}_from_first_char".format(t=push_type),
                _first_char,
                input_types=["str"],
                output_types=[push_type],
                code_blocks=0,
                docstring=
                "Pushes a {t} of the first character of the top string.".
                format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_from_last_char".format(t=push_type),
                _last_char,
                input_types=["str"],
                output_types=[push_type],
                code_blocks=0,
                docstring=
                "Pushes a {t} of the last character of the top string.".format(
                    t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_from_nth_char".format(t=push_type),
                _nth_char,
                input_types=["str", "int"],
                output_types=[push_type],
                code_blocks=0,
                docstring=
                "Pushes a {t} of the nth character of the top string. The top integer denotes nth position."
                .format(t=push_type)))

        # Checking string contents

        i.append(
            SimpleInstruction(
                "str_contains_{t}".format(t=push_type),
                _contains,
                input_types=["str", push_type],
                output_types=["bool"],
                code_blocks=0,
                docstring=
                "Pushes true if the next {t} is in the top string. Pushes false otherwise."
                .format(t=push_type)))

        i.append(
            SimpleInstruction(
                "str_index_of_{t}".format(t=push_type),
                _p_index,
                input_types=["str", push_type],
                output_types=["int"],
                code_blocks=0,
                docstring=
                "Pushes the index of the next {t} in the top string. If not found, pushes -1."
                .format(t=push_type)))

        # Splitting

        # @TODO: srt_split_on_space

        i.append(
            ProducesManyOfTypeInstruction(
                "str_split_on_{t}".format(t=push_type),
                _split_on,
                input_types=["str", push_type],
                output_type="str",
                code_blocks=0,
                docstring=
                "Pushes multiple strs produced by splitting the top str on the top {t}."
                .format(t=push_type)))

        # Replacements

        i.append(
            SimpleInstruction(
                "str_replace_first_{t}".format(t=push_type),
                _replace_n,
                input_types=["str", push_type, push_type],
                output_types=["str"],
                code_blocks=0,
                docstring=
                """Pushes the str produced by replaceing the first occurrence of the
            top {t} with the second {t}.""".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "str_replace_n_{t}".format(t=push_type),
                _replace_n,
                input_types=["str", push_type, push_type, "int"],
                output_types=["str"],
                code_blocks=0,
                docstring=
                """Pushes the str produced by replaceing the first `n` occurrences of the
            top {t} with the second {t}. The value for `n` is the top int.""".
                format(t=push_type)))

        i.append(
            SimpleInstruction(
                "str_replace_all_{t}".format(t=push_type),
                _replace_all,
                input_types=["str", push_type, push_type],
                output_types=["str"],
                code_blocks=0,
                docstring=
                """Pushes the str produced by replaceing all occurrences of the
            top {t} with the second {t}.""".format(t=push_type)))

        # Removals

        i.append(
            SimpleInstruction(
                "str_remove_first_{t}".format(t=push_type),
                _remove_n,
                input_types=["str", push_type],
                output_types=["str"],
                code_blocks=0,
                docstring=
                "Pushes the str produced by removing the first occurrence of the top {t}."
                .format(t=push_type)))

        i.append(
            SimpleInstruction(
                "str_remove_n_{t}".format(t=push_type),
                _remove_n,
                input_types=["str", push_type, "int"],
                output_types=["str"],
                code_blocks=0,
                docstring=
                """Pushes the str produced by remvoing the first `n` occurrences of the
            top {t}. The value for `n` is the top int.""".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "str_remove_all_{t}".format(t=push_type),
                _remove_all,
                input_types=["str", push_type],
                output_types=["str"],
                code_blocks=0,
                docstring=
                "Pushes the str produced by removing all occurrences of the top {t}."
                .format(t=push_type)))

        # Misc

        i.append(
            SimpleInstruction(
                "str_occurrences_of_{t}".format(t=push_type),
                _occurrences_of,
                input_types=["str", push_type],
                output_types=["int"],
                code_blocks=0,
                docstring=
                "Pushes the number of times the top {t} occurs in the top str to the int stack."
                .format(t=push_type)))

    i.append(
        SimpleInstruction(
            "str_reverse",
            _reverse,
            input_types=["str"],
            output_types=["str"],
            code_blocks=0,
            docstring="""Takes the top string and pushes it reversed."""))

    i.append(
        SimpleInstruction(
            "str_head",
            _head,
            input_types=["str", "int"],
            output_types=["str"],
            code_blocks=0,
            docstring=
            """Pushes a string of the first `n` characters from the top string. The value
        for `n` is the top int mod the length of the string."""))

    i.append(
        SimpleInstruction(
            "str_tail",
            _tail,
            input_types=["str", "int"],
            output_types=["str"],
            code_blocks=0,
            docstring=
            """Pushes a string of the last `n` characters from the top string. The value
        for `n` is the top int mod the length of the string."""))

    i.append(
        SimpleInstruction(
            "str_append_char",
            _concat,
            input_types=["str", "char"],
            output_types=["str"],
            code_blocks=0,
            docstring=
            "Appends the top char to the top string pushes the resulting string."
        ))

    i.append(
        SimpleInstruction(
            "str_rest",
            _rest,
            input_types=["str"],
            output_types=["str"],
            code_blocks=0,
            docstring="Pushes the top str without its first character."))

    i.append(
        SimpleInstruction(
            "str_but_last",
            _but_last,
            input_types=["str"],
            output_types=["str"],
            code_blocks=0,
            docstring="Pushes the top str without its last character."))

    i.append(
        SimpleInstruction(
            "str_drop",
            _drop,
            input_types=["str", "int"],
            output_types=["str"],
            code_blocks=0,
            docstring=
            """Pushes the top str without its first `n` character. The value for `n`
        is the top int mod the length of the string."""))

    i.append(
        SimpleInstruction(
            "str_but_last_n",
            _but_last_n,
            input_types=["str", "int"],
            output_types=["str"],
            code_blocks=0,
            docstring=
            """Pushes the top str without its last `n` character. The value for `n`
        is the top int mod the length of the string."""))

    i.append(
        SimpleInstruction(
            "str_length",
            _len,
            input_types=["str"],
            output_types=["int"],
            code_blocks=0,
            docstring="Pushes the length of the top str to the int stack."))

    i.append(
        SimpleInstruction("str_make_empty",
                          _make_empty,
                          input_types=[],
                          output_types=["str"],
                          code_blocks=0,
                          docstring="Pushes an empty string."))

    i.append(
        SimpleInstruction(
            "str_is_empty_string",
            _is_empty,
            input_types=["str"],
            output_types=["bool"],
            code_blocks=0,
            docstring=
            "Pushes True if top string is empty. Pushes False otherwise."))

    i.append(
        SimpleInstruction(
            "str_remove_nth",
            _remove_nth,
            input_types=["str", "int"],
            output_types=["str"],
            code_blocks=0,
            docstring="Pushes the top str with the nth character removed."))

    i.append(
        SimpleInstruction(
            "str_set_nth",
            _set_nth,
            input_types=["str", "char", "int"],
            output_types=["str"],
            code_blocks=0,
            docstring=
            "Pushes the top str with the nth character set to the top character."
        ))

    i.append(
        SimpleInstruction(
            "str_strip_whitespace",
            _strip_whitespace,
            input_types=["str"],
            output_types=["str"],
            code_blocks=0,
            docstring=
            "Pushes the top str with trailing and leading whitespace stripped."
        ))

    # @TODO: Instructions for trim_left and trim_right
    # @TODO: Instructions for pad_left and pad_right

    #  CHARACTER INSTRUCTIONS

    i.append(
        SimpleInstruction(
            "char_is_whitespace",
            _is_whitespace,
            input_types=["char"],
            output_types=["bool"],
            code_blocks=0,
            docstring=
            "Pushes True if the top Char is whitespace. Pushes False otherwise."
        ))

    i.append(
        SimpleInstruction(
            "char_is_letter",
            _is_letter,
            input_types=["char"],
            output_types=["bool"],
            code_blocks=0,
            docstring=
            "Pushes True if the top Char is a letter. Pushes False otherwise.")
    )

    i.append(
        SimpleInstruction(
            "char_is_digit",
            _is_digit,
            input_types=["char"],
            output_types=["bool"],
            code_blocks=0,
            docstring=
            "Pushes True if the top Char is a numeric digit. Pushes False otherwise."
        ))

    #  TYPE CONVERTING

    for push_type in ["bool", "int", "float", "char"]:
        i.append(
            SimpleInstruction(
                "str_from_{t}".format(t=push_type),
                _str_from_thing,
                input_types=[push_type],
                output_types=["str"],
                code_blocks=0,
                docstring="Pushes the top {t} converted into a str.".format(
                    t=push_type)))

    i.append(
        SimpleInstruction(
            "char_from_bool",
            _char_from_bool,
            input_types=["bool"],
            output_types=["char"],
            code_blocks=0,
            docstring=
            """Pushes the char \"T\" if the top bool is True. If the top
        bool is False, pushes the char \"F\"."""))

    i.append(
        SimpleInstruction(
            "char_from_ascii_int",
            _char_from_ascii,
            input_types=["int"],
            output_types=["char"],
            code_blocks=0,
            docstring=
            "Pushes the top int converted into a Character by using the int mod 128 as an ascii value."
        ))

    i.append(
        SimpleInstruction(
            "_char_from_float",
            _char_from_float,
            input_types=["float"],
            output_types=["char"],
            code_blocks=0,
            docstring=
            """Pushes the top float converted into a Character by flooring
        the float to an int, taking the int mod 128, and using it as an ascii value."""
        ))

    i.append(
        ProducesManyOfTypeInstruction(
            "chars_from_str",
            _all_chars,
            input_types=["str"],
            output_type="char",
            code_blocks=0,
            docstring=
            """Pushes each character of the top str to the char stack in reverse order."""
        ))

    return i
예제 #5
0
def instructions(type_library: PushTypeLibrary):
    """Return all core code SimpleInstructions."""
    i = []

    i.append(SimpleInstruction(
        "noop",
        _revert,
        input_stacks=[],
        output_stacks=[],
        code_blocks=0,
        docstring="A noop SimpleInstruction which does nothing."
    ))

    i.append(SimpleInstruction(
        "noop_open",
        _revert,
        input_stacks=[],
        output_stacks=[],
        code_blocks=1,
        docstring="A noop SimpleInstruction which does nothing. Opens a code block."
    ))

    i.append(SimpleInstruction(
        "code_is_code_block",
        _is_code_block,
        input_stacks=["code"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring="Push True if top item on code stack is a CodeBlock. False otherwise."
    ))

    i.append(SimpleInstruction(
        "code_is_singular",
        _is_singular,
        input_stacks=["code"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring="Push True if top item on code stack is a not CodeBlock. False otherwise."
    ))

    i.append(SimpleInstruction(
        "code_length",
        _code_length,
        input_stacks=["code"],
        output_stacks=["int"],
        code_blocks=0,
        docstring="If the top code item is a CodeBlock, pushes its length, otherwise pushes 1."
    ))

    i.append(SimpleInstruction(
        "code_first",
        _code_first,
        input_stacks=["code"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="If the top code item is a CodeBlock, pushes its first element."
    ))

    i.append(SimpleInstruction(
        "code_last",
        _code_first,
        input_stacks=["code"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="If the top code item is a CodeBlock, pushes its last element."
    ))

    i.append(SimpleInstruction(
        "code_rest",
        _code_rest,
        input_stacks=["code"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="If the top code item is a CodeBlock, pushes it to the code stack without its first element."
    ))

    i.append(SimpleInstruction(
        "code_but_last",
        _code_but_last,
        input_stacks=["code"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="If the top code item is a CodeBlock, pushes it to the code stack without its last element."
    ))

    i.append(SimpleInstruction(
        "code_wrap",
        _wrap_code_block,
        input_stacks=["code"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="Wraps the top item on the code stack in a CodeBlock."
    ))

    i.append(SimpleInstruction(
        "code_list",
        _wrap_code_block,
        input_stacks=["code", "code"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="Wraps the top two items on the code stack in a CodeBlock."
    ))

    i.append(SimpleInstruction(
        "code_combine",
        _code_combine,
        input_stacks=["code", "code"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="""Combines the top two items on the code stack in a CodeBlock.
        If one items is a CodeBlock, the other item is appended to it. If both
        items are CodeBlocks, they are concatenated together."""
    ))

    i.append(SimpleInstruction(
        "code_do",
        _wrap_tuple,
        input_stacks=["code"],
        output_stacks=["exec"],
        code_blocks=0,
        docstring="Moves the top element of the code stack to the exec stack for execution."
    ))

    i.append(SimpleInstruction(
        "code_do_dup",
        _dup,
        input_stacks=["code"],
        output_stacks=["exec", "code"],
        code_blocks=0,
        docstring="Copies the top element of the code stack to the exec stack for execution."
    ))

    i.append(StateToStateInstruction(
        "code_do_then_pop",
        _code_do_then_pop,
        stacks_used=["exec", "code"],
        code_blocks=0,
        docstring="""Pushes a `code_pop` InstructionMeta and the top item of the
        code stack to the exec stack. Result is the top code item executing before
        it is removed from the code stack."""
    ))

    i.append(StateToStateInstruction(
        "code_do_range",
        _code_do_range,
        stacks_used=["exec", "code", "int"],
        code_blocks=0,
        docstring="""Evaluates the top item on the code stack for each step along
        the range `i` to `j`. Both `i` and `j` are taken from the int stack."""
    ))

    i.append(StateToStateInstruction(
        "exec_do_range",
        _exec_do_range,
        stacks_used=["exec", "int"],
        code_blocks=1,
        docstring="""Evaluates the top item on the exec stack for each step along
        the range `i` to `j`. Both `i` and `j` are taken from the int stack.
        Differs from code_do_range only in the source of the code and the
        recursive call."""
    ))

    i.append(StateToStateInstruction(
        "code_do_count",
        _code_do_count,
        stacks_used=["exec", "code", "int"],
        code_blocks=0,
        docstring="""Evaluates the top item on the code stack `n` times, where
        `n` comes from the `n` comes from the top of the int stack."""
    ))

    i.append(StateToStateInstruction(
        "exec_do_count",
        _exec_do_count,
        stacks_used=["exec", "int"],
        code_blocks=1,
        docstring="""Evaluates the top item on the exec stack `n` times, where
        `n` comes from the `n` comes from the top of the int stack. Differs from
        code.do*count only in the source of the code and the recursive call."""
    ))

    i.append(StateToStateInstruction(
        "code_do_times",
        _code_do_times,
        stacks_used=["exec", "code", "int"],
        code_blocks=0,
        docstring="""Evaluates the top item on the code stack `n` times, where
        `n` comes from the `n` comes from the top of the int stack."""
    ))

    i.append(StateToStateInstruction(
        "exec_do_times",
        _exec_do_times,
        stacks_used=["exec", "code", "int"],
        code_blocks=1,
        docstring="""Evaluates the top item on the code stack `n` times, where
        `n` comes from the `n` comes from the top of the int stack."""
    ))

    i.append(StateToStateInstruction(
        "exec_while",
        _exec_while,
        stacks_used=["exec", "bool"],
        code_blocks=1,
        docstring="""Evaluates the top item on the exec stack repeated until the top
        bool is no longer True."""
    ))

    i.append(StateToStateInstruction(
        "exec_do_while",
        _exec_do_while,
        stacks_used=["exec", "bool"],
        code_blocks=1,
        docstring="""Evaluates the top item on the exec stack repeated until the top
        bool is no longer True."""
    ))

    i.append(StateToStateInstruction(
        "code_map",
        _code_map,
        stacks_used=["exec", "code"],
        code_blocks=0,
        docstring="""Evaluates the top item on the exec stack for each element of the top
        CodeBlock on the code stack. If the top code item is not a CodeBlock, it is wrapped
        into one."""
    ))

    i.append(SimpleInstruction(
        "code_if",
        _if,
        input_stacks=["bool", "code", "code"],
        output_stacks=["exec"],
        code_blocks=0,
        docstring="""If the top boolean is true, execute the top element of the code
        stack and skip the second. Otherwise, skip the top element of the
        code stack and execute the second."""
    ))

    i.append(SimpleInstruction(
        "exec_if",
        _if,
        input_stacks=["bool", "exec", "exec"],
        output_stacks=["exec"],
        code_blocks=2,
        docstring="""If the top boolean is true, execute the top element of the exec
        stack and skip the second. Otherwise, skip the top element of the
        exec stack and execute the second."""
    ))

    i.append(StateToStateInstruction(
        "code_when",
        _code_when,
        stacks_used=["exec", "code", "bool"],
        code_blocks=0,
        docstring="""Evalutates the top code item if the top bool is True.
        Otherwise the top code is popped."""
    ))

    i.append(StateToStateInstruction(
        "exec_when",
        _exec_when,
        stacks_used=["exec", "bool"],
        code_blocks=1,
        docstring="""Pops the next item on the exec stack without evaluating it
        if the top bool is False. Otherwise, has no effect."""
    ))

    i.append(SimpleInstruction(
        "code_member",
        _code_member,
        input_stacks=["code", "code"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring="""Pushes True if the second code item is a found within the top code item.
        If the top code item is not a CodeBlock, it is wrapped."""
    ))

    i.append(SimpleInstruction(
        "code_nth",
        _code_nth,
        input_stacks=["code", "int"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="""Pushes nth item of the top element on the code stack. If
        the top item is not a CodeBlock it is wrapped in a CodeBlock."""
    ))

    i.append(SimpleInstruction(
        "make_empty_code_block",
        _make_empty_code_block,
        input_stacks=[],
        output_stacks=["code"],
        code_blocks=0,
        docstring="""Pushes an empty CodeBlock to the code stack."""
    ))

    i.append(SimpleInstruction(
        "is_empty_code_block",
        _is_empty_code_block,
        input_stacks=["code"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring="""Pushes true if top code item is an empty CodeBlock. Pushes
        false otherwise."""
    ))

    i.append(SimpleInstruction(
        "code_size",
        _code_size,
        input_stacks=["code"],
        output_stacks=["int"],
        code_blocks=0,
        docstring="""Pushes the total size of the top item on the code stack. If
        the top item is a CodeBlock, this includes the size of all the CodeBlock's
        elements recusively."""
    ))

    i.append(SimpleInstruction(
        "code_extract",
        _code_extract,
        input_stacks=["code", "int"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="""Traverses the top code item depth first and returns the nth
        item based on the top int."""
    ))

    i.append(SimpleInstruction(
        "code_insert",
        _code_insert,
        input_stacks=["code", "code", "int"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="""Traverses the top code item depth first and inserts the
        second code item at position `n`. The value of `n` is the top int."""
    ))

    # code_subst
    # code_contains
    # code_container

    i.append(SimpleInstruction(
        "code_first_position",
        _code_first_position,
        input_stacks=["code", "code"],
        output_stacks=["int"],
        code_blocks=0,
        docstring="""Pushes the first position of the second code item within
        the top code item. If not found, pushes -1. If the top code item is not
        a CodeBlock, this instruction returns 0 if the top two code elements are
        equal and -1 otherwise."""
    ))

    i.append(SimpleInstruction(
        "code_reverse",
        _code_reverse,
        input_stacks=["code"],
        output_stacks=["code"],
        code_blocks=0,
        docstring="""Pushes the top code item reversed. No effect if top code
        item is not a CodeBlock."""
    ))

    return i
예제 #6
0
def instructions():
    """Return all core numeric instructions."""
    i = []

    for push_type in ["int", "float"]:
        i.append(SimpleInstruction(
            "{t}_add".format(t=push_type),
            _add,
            input_types=[push_type, push_type],
            output_types=[push_type],
            code_blocks=0,
            docstring="Adds the top two {t}s and pushes the result.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_sub".format(t=push_type),
            _sub,
            input_types=[push_type, push_type],
            output_types=[push_type],
            code_blocks=0,
            docstring="Subtracts the top two {t}s and pushes the result.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_mult".format(t=push_type),
            _mult,
            input_types=[push_type, push_type],
            output_types=[push_type],
            code_blocks=0,
            docstring="Multiplies the top two {t}s and pushes the result.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_div".format(t=push_type),
            _p_div,
            input_types=[push_type, push_type],
            output_types=[push_type],
            code_blocks=0,
            docstring="Divides the top two {t}s and pushes the result.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_mod".format(t=push_type),
            _p_mod,
            input_types=[push_type, push_type],
            output_types=[push_type],
            code_blocks=0,
            docstring="Computes the modulous of the top two {t}s and pushes the result.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_min".format(t=push_type),
            lambda a, b: [min(b, a)],
            input_types=[push_type, push_type],
            output_types=[push_type],
            code_blocks=0,
            docstring="Pushes the minimum of two {t}.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_max".format(t=push_type),
            lambda a, b: [max(b, a)],
            input_types=[push_type, push_type],
            output_types=[push_type],
            code_blocks=0,
            docstring="Pushes the maximum of two {t}.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_inc".format(t=push_type),
            _inc,
            input_types=[push_type],
            output_types=[push_type],
            code_blocks=0,
            docstring="Increments the top {t} by 1.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_dec".format(t=push_type),
            _dec,
            input_types=[push_type],
            output_types=[push_type],
            code_blocks=0,
            docstring="Decrements the top {t} by 1.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_lt".format(t=push_type),
            lambda a, b: [b < a],
            input_types=[push_type, push_type],
            output_types=["bool"],
            code_blocks=0,
            docstring="Pushes true if the top {t} is less than the second. Pushes false otherwise.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_lte".format(t=push_type),
            lambda a, b: [b <= a],
            input_types=[push_type, push_type],
            output_types=["bool"],
            code_blocks=0,
            docstring="Pushes true if the top {t} is less than, or equal to, the second. Pushes false otherwise.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_gt".format(t=push_type),
            lambda a, b: [b > a],
            input_types=[push_type, push_type],
            output_types=["bool"],
            code_blocks=0,
            docstring="Pushes true if the top {t} is greater than the second.. Pushes false otherwise.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_gte".format(t=push_type),
            lambda a, b: [b >= a],
            input_types=[push_type, push_type],
            output_types=["bool"],
            code_blocks=0,
            docstring="Pushes true if the top {t} is greater than, or equal to, the second. Pushes false otherwise.".format(t=push_type)
        ))

    # Trig functions

    i.append(SimpleInstruction(
        "float_sin",
        lambda x: [math.sin(x)],
        input_types=["float"],
        output_types=["float"],
        code_blocks=0,
        docstring="Pushes the sin of the top float."
    ))

    i.append(SimpleInstruction(
        "float_cos",
        lambda x: [math.cos(x)],
        input_types=["float"],
        output_types=["float"],
        code_blocks=0,
        docstring="Pushes the cos of the top float."
    ))

    i.append(SimpleInstruction(
        "float_tan",
        lambda x: [math.tan(x)],
        input_types=["float"],
        output_types=["float"],
        code_blocks=0,
        docstring="Pushes the tan of the top float."
    ))

    # Type converting

    i.append(SimpleInstruction(
        "int_from_bool",
        lambda b: [int(b)],
        input_types=["bool"],
        output_types=["int"],
        code_blocks=0,
        docstring="Pushes 1 in the top boolean is true. Pushes 0 if the top boolean is false."
    ))

    i.append(SimpleInstruction(
        "float_from_bool",
        lambda b: [float(b)],
        input_types=["bool"],
        output_types=["float"],
        code_blocks=0,
        docstring="Pushes 1.0 in the top boolean is true. Pushes 0.0 if the top boolean is false."
    ))

    i.append(SimpleInstruction(
        "int_from_float",
        lambda f: [int(f)],
        input_types=["float"],
        output_types=["int"],
        code_blocks=0,
        docstring="Casts the top float to an integer and pushes the result."
    ))

    i.append(SimpleInstruction(
        "float_from_int",
        lambda i: [float(i)],
        input_types=["int"],
        output_types=["float"],
        code_blocks=0,
        docstring="Casts the top integer to a float and pushes the result."
    ))

    return i
예제 #7
0
파일: conftest.py 프로젝트: nayabur/pyshgp
def point_instructions():
    return [
        SimpleInstruction("point_dist", point_distance, ["point", "point"], ["float"], 0),
        SimpleInstruction("point_from_floats", point_from_floats, ["float", "float"], ["point"], 0),
    ]
예제 #8
0
def instructions():
    """Return all core numeric instructions."""
    i = []

    i.append(SimpleInstruction(
        "bool_and",
        _and,
        input_stacks=["bool", "bool"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring="Pushes the result of and-ing the top two booleans."
    ))

    i.append(SimpleInstruction(
        "bool_or",
        _or,
        input_stacks=["bool", "bool"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring="Pushes the result of or-ing the top two booleans."
    ))

    i.append(SimpleInstruction(
        "bool_not",
        _not,
        input_stacks=["bool"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring="Pushes the inverse of the boolean."
    ))

    i.append(SimpleInstruction(
        "bool_xor",
        _xor,
        input_stacks=["bool", "bool"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring="Pushes the result of xor-ing the top two booleans."
    ))

    i.append(SimpleInstruction(
        "bool_invert_first_then_and",
        _invert_first_then_and,
        input_stacks=["bool", "bool"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring=""""Pushes the result of and-ing the top two booleans after inverting the
        top boolean."""
    ))

    i.append(SimpleInstruction(
        "bool_second_first_then_and",
        _invert_second_then_and,
        input_stacks=["bool", "bool"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring=""""Pushes the result of and-ing the top two booleans after inverting the
        second boolean."""
    ))

    i.append(SimpleInstruction(
        "bool_from_int",
        _bool_from_int,
        input_stacks=["int"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring="If the top int is 0, pushes False. Pushes True for any other int value."
    ))

    i.append(SimpleInstruction(
        "bool_from_float",
        _bool_from_float,
        input_stacks=["float"],
        output_stacks=["bool"],
        code_blocks=0,
        docstring="If the top float is 0.0, pushes False. Pushes True for any other float value."
    ))

    return i
예제 #9
0
    return Rectangle(
        i1, i2),  # comma at the end packages return value into a tuple


"""
Next, we define our custom instructions that manipulate rectangles by defining parameters in SimpleInstruction.
We want rectangle_areas_instruction to find difference in area between two rectangles, and push it to the float stack.
We want rectangle_from_floats_instruction to take two floats and push a rectangle.
"""

rectangle_areas_instruction = SimpleInstruction(
    name="rectangle_area_diff",  # unique name for the instruction
    f=area_difference,  # function whose signature matches input_stacks and output_stacks
    input_stacks=[
        "rectangle", "rectangle"
    ],  # list of PushType names to use when popping arguments from PushState
    output_stacks=[
        "float"
    ],  # list of PushType names to use when pushing function results to the PushState
    code_blocks=
    0  # number of CodeBlocks to open following the instruction in a Genome
)

rectangle_from_floats_instruction = SimpleInstruction(
    name="rectangle_from_floats",
    f=rectangle_from_floats,
    input_stacks=["float", "float"],
    output_stacks=["rectangle"],
    code_blocks=0)
"""
Now we must define our problem by creating two data sets of example input-output pairs.
One will be used for training, and the other will be used to test our program on unseen data.
예제 #10
0
파일: common.py 프로젝트: bmetevier/pyshgp
def instructions():
    """Return all core numeric instructions."""
    i = []

    for push_type in ["bool", "int", "float", "str", "char", "code", "exec"]:
        i.append(
            SimpleInstruction(
                "{t}_pop".format(t=push_type),
                lambda x: [],
                input_types=[push_type],
                output_types=[],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring="Pops the top {t}.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_dup".format(t=push_type),
                lambda x: [x, x],
                input_types=[push_type],
                output_types=[push_type, push_type],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring="Duplicates the top {t}.".format(t=push_type)))

        i.append(
            ProducesManyOfTypeInstruction(
                "{t}_dup_times".format(t=push_type),
                _dup_times,
                input_types=["int", push_type],
                output_type=push_type,
                code_blocks=(1 if push_type == "exec" else 0),
                docstring=
                "Duplicates the top {t} `n` times where `n` is from the int stack."
                .format(t=push_type)))

        # Disabled due to performance issues.
        # i.append(StateToStateInstruction(
        #     "{t}_dup_top_n".format(t=push_type),
        #     _dup_top_n_factory(push_type),
        #     types_used=[push_type, "int"],
        #     code_blocks=0,
        #     docstring="Duplicates the top n items on the {t} stack.".format(t=push_type)
        # ))

        i.append(
            SimpleInstruction(
                "{t}_swap".format(t=push_type),
                lambda a, b: [a, b],
                input_types=[push_type, push_type],
                output_types=[push_type, push_type],
                code_blocks=(2 if push_type == "exec" else 0),
                docstring="Swaps the top two {t}s.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_rot".format(t=push_type),
                lambda a, b, c: [b, a, c],
                input_types=[push_type] * 3,
                output_types=[push_type] * 3,
                code_blocks=(3 if push_type == "exec" else 0),
                docstring="Rotates the top three {t}s.".format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_flush".format(t=push_type),
                _flusher(push_type),
                types_used=[push_type],
                code_blocks=0,
                docstring="Empties the {t} stack.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_eq".format(t=push_type),
                lambda a, b: [a == b],
                input_types=[push_type, push_type],
                output_types=["bool"],
                code_blocks=0,
                docstring=
                "Pushes True if the top two {t} are equal. Otherwise pushes False."
                .format(t=push_type)))

        i.append(
            TakesStateInstruction(
                "{t}_stack_depth".format(t=push_type),
                _stack_depther(push_type),
                output_types=["int"],
                other_types=[push_type],
                code_blocks=0,
                docstring="Pushes the size of the {t} stack to the int stack.".
                format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_yank".format(t=push_type),
                _yanker(push_type),
                types_used=[push_type, "int"],
                code_blocks=0,
                docstring=
                "Yanks a {t} from deep in the stack based on an index from the int stack and puts it on top."
                .format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_yank_dup".format(t=push_type),
                _yank_duper(push_type),
                types_used=[push_type, "int"],
                code_blocks=0,
                docstring=
                "Yanks a copy of a {t} deep in the stack based on an index from the int stack and puts it on top."
                .format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_shove".format(t=push_type),
                _shover(push_type),
                types_used=[push_type, "int"],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring=
                "Shoves the top {t} deep in the stack based on an index from the int stack."
                .format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_shove_dup".format(t=push_type),
                _shove_duper(push_type),
                types_used=[push_type, "int"],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring=
                "Shoves a copy of the top {t} deep in the stack based on an index from the int stack."
                .format(t=push_type)))

        i.append(
            TakesStateInstruction(
                "{t}_is_empty".format(t=push_type),
                _is_emptyer(push_type),
                output_types=["bool"],
                other_types=[push_type],
                code_blocks=0,
                docstring=
                "Pushes True if the {t} stack is empty. Pushes False otherwise."
                .format(t=push_type)))

    return i
예제 #11
0
def instructions(type_library: PushTypeLibrary):
    """Return all core numeric instructions."""
    i: List[Instruction] = []

    for push_type in set(CORE_VECTOR_PUSH_TYPES).intersection(
            set(type_library.values())):
        vec_type_name = push_type.name
        el_type_name = vec_type_name.replace("vector_", "")

        i.append(
            SimpleInstruction(
                vec_type_name + "_concat",
                concat,
                input_stacks=[vec_type_name, vec_type_name],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring="Concatenates the top two {vt}.".format(
                    vt=vec_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_conj",
                conj,
                input_stacks=[vec_type_name, el_type_name],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring="Appends the top {et} to the top {vt}.".format(
                    vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_take",
                take,
                input_stacks=[vec_type_name, "int"],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring=
                "Creates a new {vt} from the first N elements of the top {vt}. N is top int."
                .format(vt=vec_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_subvec",
                subvec,
                input_stacks=[vec_type_name, "int", "int"],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring=
                "Creates a new {vt} from a slice of the top {vt}. Start and end indices are the top two ints."
                .format(vt=vec_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_first",
                first,
                input_stacks=[vec_type_name],
                output_stacks=[el_type_name],
                code_blocks=0,
                docstring=
                "Takes the first element of the top {vt} and pushes it to the {et} stack."
                .format(vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_last",
                last,
                input_stacks=[vec_type_name],
                output_stacks=[el_type_name],
                code_blocks=0,
                docstring=
                "Takes the last element of the top {vt} and pushes it to the {et} stack."
                .format(vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_nth",
                nth,
                input_stacks=[vec_type_name, "int"],
                output_stacks=[el_type_name],
                code_blocks=0,
                docstring=
                "Takes the nth element of the top {vt} and pushes it to the {et} stack. N is the top int."
                .format(vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_rest",
                rest,
                input_stacks=[vec_type_name],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring="Drops the first element of the top {vt}.".format(
                    vt=vec_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_but_last",
                but_last,
                input_stacks=[vec_type_name],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring="Drops the last element of the top {vt}.".format(
                    vt=vec_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_length",
                length,
                input_stacks=[vec_type_name],
                output_stacks=["int"],
                code_blocks=0,
                docstring="Pushes the length of the top {vt} to the int stack."
                .format(vt=vec_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_reverse",
                reverse,
                input_stacks=[vec_type_name],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring="Reverses the top {vt}.".format(vt=vec_type_name)))

        i.append(
            ProducesManyOfTypeInstruction(
                vec_type_name + "_push_all",
                push_all,
                input_stacks=[vec_type_name],
                output_stack=el_type_name,
                code_blocks=0,
                docstring="Pushes all elements of the top {vt} to the {et}.".
                format(vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_empty_vector",
                empty_vector,
                input_stacks=[vec_type_name],
                output_stacks=["bool"],
                code_blocks=0,
                docstring=
                "Pushes True to the bool stack if the top {vt} is empty. Pushes False otherwise."
                .format(vt=vec_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_contains",
                contains,
                input_stacks=[vec_type_name, el_type_name],
                output_stacks=["bool"],
                code_blocks=0,
                docstring=
                "Pushes True to the bool stack if the top {et} is found in the top {vt}. Pushes False otherwise."
                .format(vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_index_of",
                index_of,
                input_stacks=[vec_type_name, el_type_name],
                output_stacks=["int"],
                code_blocks=0,
                docstring=
                "Pushes the index top {et} is top {vt} to int stack. Pushes -1 if not found."
                .format(vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_occurrences_of",
                occurrences_of,
                input_stacks=[vec_type_name, el_type_name],
                output_stacks=["int"],
                code_blocks=0,
                docstring=
                "Pushes the number of time the top {et} is found in the top {vt} to int stack."
                .format(vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_set_nth",
                set_nth,
                input_stacks=[vec_type_name, "int", el_type_name],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring=
                "Sets the nth element of the top {vt} to be the top {et}. N is the top int."
                .format(vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_replace",
                replace,
                input_stacks=[vec_type_name, el_type_name, el_type_name],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring=
                "Replaces all instances of the top {et} from the top {vt}.".
                format(vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_replace_first",
                replace_first,
                input_stacks=[vec_type_name, el_type_name, el_type_name],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring=
                "Replaces the first instance of the top {et} from the top {vt}."
                .format(vt=vec_type_name, et=el_type_name)))

        i.append(
            SimpleInstruction(
                vec_type_name + "_remove",
                remove,
                input_stacks=[vec_type_name, el_type_name],
                output_stacks=[vec_type_name],
                code_blocks=0,
                docstring=
                "Removes all instances of the top {et} from the top {vt}.".
                format(vt=vec_type_name, et=el_type_name)))

        i.append(
            StateToStateInstruction(
                vec_type_name + "_iterate",
                partial(iterate,
                        vec_type=type_library[vec_type_name],
                        el_type=type_library[el_type_name]),
                stacks_used=[vec_type_name, el_type_name, "exec"],
                code_blocks=1,
                docstring=
                "Iterates over the top {vt} using the code on top of the exec stack."
                .format(vt=vec_type_name)))

    return i
예제 #12
0
def instructions(type_library: PushTypeLibrary):
    """Return all core numeric instructions."""
    i = []

    for push_type in type_library.keys():
        i.append(
            SimpleInstruction(
                "{t}_pop".format(t=push_type),
                _noop,
                input_stacks=[push_type],
                output_stacks=[],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring="Pops the top {t}.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_dup".format(t=push_type),
                _dup,
                input_stacks=[push_type],
                output_stacks=[push_type, push_type],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring="Duplicates the top {t}.".format(t=push_type)))

        i.append(
            ProducesManyOfTypeInstruction(
                "{t}_dup_times".format(t=push_type),
                _dup_times,
                input_stacks=["int", push_type],
                output_stack=push_type,
                code_blocks=(1 if push_type == "exec" else 0),
                docstring=
                "Duplicates the top {t} `n` times where `n` is from the int stack."
                .format(t=push_type)))

        # Disabled due to performance issues.
        # i.append(StateToStateInstruction(
        #     "{t}_dup_top_n".format(t=push_type),
        #     _dup_top_n_factory(push_type),
        #     stacks_used=[push_type, "int"],
        #     code_blocks=0,
        #     docstring="Duplicates the top n items on the {t} stack.".format(t=push_type)
        # ))

        i.append(
            SimpleInstruction(
                "{t}_swap".format(t=push_type),
                _swap,
                input_stacks=[push_type, push_type],
                output_stacks=[push_type, push_type],
                code_blocks=(2 if push_type == "exec" else 0),
                docstring="Swaps the top two {t}s.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_rot".format(t=push_type),
                _rot,
                input_stacks=[push_type] * 3,
                output_stacks=[push_type] * 3,
                code_blocks=(3 if push_type == "exec" else 0),
                docstring="Rotates the top three {t}s.".format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_flush".format(t=push_type),
                partial(_flush, type_name=push_type),
                stacks_used=[push_type],
                code_blocks=0,
                docstring="Empties the {t} stack.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_eq".format(t=push_type),
                _eq,
                input_stacks=[push_type, push_type],
                output_stacks=["bool"],
                code_blocks=0,
                docstring=
                "Pushes True if the top two {t} are equal. Otherwise pushes False."
                .format(t=push_type)))

        i.append(
            TakesStateInstruction(
                "{t}_stack_depth".format(t=push_type),
                partial(_stack_depth, type_name=push_type),
                output_stacks=["int"],
                other_stacks=[push_type],
                code_blocks=0,
                docstring="Pushes the size of the {t} stack to the int stack.".
                format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_yank".format(t=push_type),
                partial(_yank, type_name=push_type),
                stacks_used=[push_type, "int"],
                code_blocks=0,
                docstring=
                "Yanks a {t} from deep in the stack based on an index from the int stack and puts it on top."
                .format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_yank_dup".format(t=push_type),
                partial(_yank_dup, type_name=push_type),
                stacks_used=[push_type, "int"],
                code_blocks=0,
                docstring=
                "Yanks a copy of a {t} deep in the stack based on an index from the int stack and puts it on top."
                .format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_shove".format(t=push_type),
                partial(_shove, type_name=push_type),
                stacks_used=[push_type, "int"],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring=
                "Shoves the top {t} deep in the stack based on an index from the int stack."
                .format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_shove_dup".format(t=push_type),
                partial(_shove_dup, type_name=push_type),
                stacks_used=[push_type, "int"],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring=
                "Shoves a copy of the top {t} deep in the stack based on an index from the int stack."
                .format(t=push_type)))

        i.append(
            TakesStateInstruction(
                "{t}_is_empty".format(t=push_type),
                partial(_is_empty, type_name=push_type),
                output_stacks=["bool"],
                other_stacks=[push_type],
                code_blocks=0,
                docstring=
                "Pushes True if the {t} stack is empty. Pushes False otherwise."
                .format(t=push_type)))

    for push_type_name, push_type in type_library.items():
        if push_type_name == "code":
            continue
        i.append(
            SimpleInstruction(
                "code_from_{t}".format(t=push_type_name),
                partial(_make_code, push_type=push_type),
                input_stacks=[push_type_name],
                output_stacks=["code"],
                code_blocks=(1 if push_type_name == "exec" else 0),
                docstring="Moves the top {t} to the code stack.".format(
                    t=push_type_name)))

    return i
예제 #13
0
def instructions(type_library: PushTypeLibrary):
    """Return all core numeric instructions."""
    i = []

    for push_type in ["int", "float"]:
        i.append(
            SimpleInstruction(
                "{t}_add".format(t=push_type),
                _add,
                input_stacks=[push_type, push_type],
                output_stacks=[push_type],
                code_blocks=0,
                docstring="Adds the top two {t}s and pushes the result.".
                format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_sub".format(t=push_type),
                _sub,
                input_stacks=[push_type, push_type],
                output_stacks=[push_type],
                code_blocks=0,
                docstring="Subtracts the top two {t}s and pushes the result.".
                format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_mult".format(t=push_type),
                _mult,
                input_stacks=[push_type, push_type],
                output_stacks=[push_type],
                code_blocks=0,
                docstring="Multiplies the top two {t}s and pushes the result.".
                format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_div".format(t=push_type),
                _p_div,
                input_stacks=[push_type, push_type],
                output_stacks=[push_type],
                code_blocks=0,
                docstring="Divides the top two {t}s and pushes the result.".
                format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_mod".format(t=push_type),
                _p_mod,
                input_stacks=[push_type, push_type],
                output_stacks=[push_type],
                code_blocks=0,
                docstring=
                "Computes the modulus of the top two {t}s and pushes the result."
                .format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_min".format(t=push_type),
                _min,
                input_stacks=[push_type, push_type],
                output_stacks=[push_type],
                code_blocks=0,
                docstring="Pushes the minimum of two {t}.".format(
                    t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_max".format(t=push_type),
                _max,
                input_stacks=[push_type, push_type],
                output_stacks=[push_type],
                code_blocks=0,
                docstring="Pushes the maximum of two {t}.".format(
                    t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_inc".format(t=push_type),
                _inc,
                input_stacks=[push_type],
                output_stacks=[push_type],
                code_blocks=0,
                docstring="Increments the top {t} by 1.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_dec".format(t=push_type),
                _dec,
                input_stacks=[push_type],
                output_stacks=[push_type],
                code_blocks=0,
                docstring="Decrements the top {t} by 1.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_lt".format(t=push_type),
                _lt,
                input_stacks=[push_type, push_type],
                output_stacks=["bool"],
                code_blocks=0,
                docstring=
                "Pushes true if the top {t} is less than the second. Pushes false otherwise."
                .format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_lte".format(t=push_type),
                _lte,
                input_stacks=[push_type, push_type],
                output_stacks=["bool"],
                code_blocks=0,
                docstring=
                "Pushes true if the top {t} is less than, or equal to, the second. Pushes false otherwise."
                .format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_gt".format(t=push_type),
                _gt,
                input_stacks=[push_type, push_type],
                output_stacks=["bool"],
                code_blocks=0,
                docstring=
                "Pushes true if the top {t} is greater than the second.. Pushes false otherwise."
                .format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_gte".format(t=push_type),
                _gte,
                input_stacks=[push_type, push_type],
                output_stacks=["bool"],
                code_blocks=0,
                docstring=
                "Pushes true if the top {t} is greater than, or equal to, the second. Pushes false otherwise."
                .format(t=push_type)))

    # Trig functions

    i.append(
        SimpleInstruction("float_sin",
                          _sin,
                          input_stacks=["float"],
                          output_stacks=["float"],
                          code_blocks=0,
                          docstring="Pushes the sin of the top float."))

    i.append(
        SimpleInstruction("float_cos",
                          _cos,
                          input_stacks=["float"],
                          output_stacks=["float"],
                          code_blocks=0,
                          docstring="Pushes the cos of the top float."))

    i.append(
        SimpleInstruction("float_tan",
                          _tan,
                          input_stacks=["float"],
                          output_stacks=["float"],
                          code_blocks=0,
                          docstring="Pushes the tan of the top float."))

    # Type converting

    i.append(
        SimpleInstruction(
            "int_from_bool",
            _to_int,
            input_stacks=["bool"],
            output_stacks=["int"],
            code_blocks=0,
            docstring=
            "Pushes 1 in the top boolean is true. Pushes 0 if the top boolean is false."
        ))

    i.append(
        SimpleInstruction(
            "float_from_bool",
            _to_float,
            input_stacks=["bool"],
            output_stacks=["float"],
            code_blocks=0,
            docstring=
            "Pushes 1.0 in the top boolean is true. Pushes 0.0 if the top boolean is false."
        ))

    i.append(
        SimpleInstruction(
            "int_from_float",
            _to_int,
            input_stacks=["float"],
            output_stacks=["int"],
            code_blocks=0,
            docstring="Casts the top float to an integer and pushes the result."
        ))

    i.append(
        SimpleInstruction(
            "float_from_int",
            _to_float,
            input_stacks=["int"],
            output_stacks=["float"],
            code_blocks=0,
            docstring="Casts the top integer to a float and pushes the result."
        ))

    return i