예제 #1
0
 def test_unregister(self, instr_set):
     i_set = InstructionSet()
     i_set.register(instr_set["int_add"])
     i_set.register(instr_set["int_sub"])
     i_set.unregister("int_add")
     assert len(i_set) == 1
     assert list(i_set.values())[0].name == "int_sub"
예제 #2
0
 def test_register_by_name(self):
     i_set = InstructionSet()
     i_set.register_by_name(".*_mult")
     print(i_set)
     assert len(i_set) == 2
     assert set([i.name
                 for i in i_set.values()]) == {"int_mult", "float_mult"}
예제 #3
0
    def __init__(self,
                 n_inputs: int,
                 instruction_set: Union[InstructionSet, str],
                 literals: Sequence[Any],
                 erc_generators: Sequence[Callable],
                 distribution: DiscreteProbDistrib = "proportional"):
        self.n_inputs = n_inputs
        self.erc_generators = erc_generators

        self.instruction_set = instruction_set
        if self.instruction_set == "core":
            self.instruction_set = InstructionSet(register_core=True)
        self.type_library = self.instruction_set.type_library
        self.literals = [
            lit if isinstance(lit, Literal) else infer_literal(
                lit, self.type_library) for lit in literals
        ]

        if distribution == "proportional":
            self.distribution = (DiscreteProbDistrib().add(
                GeneTypes.INPUT, self.n_inputs).add(
                    GeneTypes.INSTRUCTION, len(self.instruction_set)).add(
                        GeneTypes.CLOSE,
                        sum([
                            i.code_blocks
                            for i in self.instruction_set.values()
                        ])).add(GeneTypes.LITERAL,
                                len(literals)).add(GeneTypes.ERC,
                                                   len(erc_generators)))
        else:
            self.distribution = distribution
예제 #4
0
def run_ga_on_odd_test(parallelism):
    X = np.arange(-10, 10).reshape(-1, 1)
    y = [[bool(x[0] % 2)] for x in X]

    instruction_set = (InstructionSet().register_core_by_stack(
        {"int"}, exclude_stacks={"str", "exec", "code"}))

    spawner = GeneSpawner(n_inputs=1,
                          instruction_set=instruction_set,
                          literals=[],
                          erc_generators=[
                              partial(random.randint, 0, 10),
                          ])

    est = PushEstimator(spawner=spawner,
                        population_size=30,
                        max_generations=3,
                        simplification_steps=10,
                        parallelism=parallelism)
    est.fit(X, y)

    assert isinstance(est.solution, Individual)
    assert len(est.solution.program.code) > 0

    path = "tmp.push"
    solution = est.solution.copy(deep=True)
    est.save(path)
    est.load(path)
    assert solution == est.solution
    os.remove(path)
예제 #5
0
def simple_test_spawner():
    instruction_set = InstructionSet().register_core_by_stack({"int"}, exclude_stacks={"str", "exec", "code"})
    spawner = GeneSpawner(
        n_inputs=1,
        instruction_set=instruction_set,
        literals=[],
        erc_generators=[
            partial(random.randint, 0, 10),
        ]
    )
    return spawner
예제 #6
0
    def __init__(self,
                 instruction_set: Union[InstructionSet, str] = "core",
                 reset_on_run: bool = True):
        self.reset_on_run = reset_on_run
        # If no instruction set given, create one and register all instructions.
        if instruction_set == "core":
            self.instruction_set = InstructionSet(register_core=True)
        else:
            self.instruction_set = instruction_set

        self.type_library = self.instruction_set.type_library

        # Initialize the PushState and status
        self.state: PushState = None
        self.status: PushInterpreterStatus = None
        self._validate()
예제 #7
0
    def __init__(self,
                 instruction_set: Union[InstructionSet, str] = "core",
                 config: PushInterpreterConfig = None):
        # If no instruction set given, create one and register all instructions.
        if instruction_set == "core":
            self.instruction_set = InstructionSet(register_all=True)
        else:
            self.instruction_set = instruction_set
        self._supported_types = self.instruction_set.supported_types()

        if config is None:
            self.config = PushInterpreterConfig()
        else:
            self.config = config

        # Initialize the PushState and status
        self.reset()
예제 #8
0
    def test_register_core_by_stack_with_exclude(self, core_type_lib):
        foo = common.instructions(core_type_lib)
        print([i for i in foo
               if i.name == "exec_dup_times"][0].required_stacks())

        i_set = InstructionSet(register_core=False)
        i_set.register_core_by_stack({"int"},
                                     exclude_stacks={"str", "exec", "code"})
        for i in i_set.values():
            if len(i.required_stacks()) > 0:
                print(i.name, i.required_stacks())
                assert i.name not in {
                    "exec_pop", "exec_dup", "exec_dup_times", "exec_swap",
                    "exec_rot", "exec_flush", "exec_stack_depth", "exec_yank",
                    "exec_yank_dup", "exec_shove", "exec_shove_dup"
                }
                assert "int" in i.required_stacks()
                assert "exec" not in i.required_stacks()
예제 #9
0
def test_ga_on_odd():
    X = np.arange(-10, 10).reshape(-1, 1)
    y = [[bool(x[0] % 2)] for x in X]

    instruction_set = (InstructionSet().register_by_type(
        ["int"], exclude=["str", "exec",
                          "code"]).register_n_inputs(X.shape[1]))

    spawner = GeneSpawner(instruction_set=instruction_set,
                          literals=[],
                          erc_generators=[
                              lambda: random.randint(0, 10),
                          ])

    est = PushEstimator(spawner=spawner,
                        population_size=20,
                        max_generations=10,
                        simplification_steps=100)
    est.fit(X, y)

    assert isinstance(est._result.program, CodeBlock)
    assert len(est._result.program) > 0
예제 #10
0
    ["^_^ " * 5],
]
y_train_edge = [target_function(x[0]) for x in X_train_edge]

X_train_synthetic = [[synthetic_input()] for _ in range(70)]
y_train_synthetic = [target_function(x[0]) for x in X_train_synthetic]

X_train = X_train_edge + X_train_synthetic
y_train = y_train_edge + y_train_synthetic

X_test = [[synthetic_input()] for _ in range(100)]
y_test = [target_function(x[0]) for x in X_test]

# Spawner

instruction_set = (InstructionSet().register_by_type(
    ["int", "bool", "string", "char", "exec", "stdout"]).register_n_inputs(1))

spawner = GeneSpawner(
    instruction_set=instruction_set,
    literals=[Char(" "), Char("\n")],
    erc_generators=[lambda: Char(choice(_possible_chars)), synthetic_input],
)

# Estimator

est = PushEstimator(search="GA",
                    population_size=1000,
                    max_generations=100,
                    spawner=spawner,
                    last_str_from_stdout=True,
                    verbose=2)
예제 #11
0
from pyshgp.push.atoms import CodeBlock
from pyshgp.push.interpreter import PushInterpreter
from pyshgp.push.instruction_set import InstructionSet
from pyshgp.gp.genome import Genome


# Not using fixture in order to spot unintentional updates during execution.
i_set = InstructionSet(register_all=True).register_n_inputs(10)


def load_genome(name: str) -> Genome:
    with open("tests/resources/genomes/" + name + ".json") as f:
        return Genome.from_json_str(f.read(), i_set)


def load_program(name: str) -> CodeBlock:
    with open("tests/resources/programs/" + name + ".json") as f:
        return CodeBlock.from_json_str(f.read(), i_set)


def test_genome_relu_1(interpreter: PushInterpreter):
    name = "relu_via_max"
    genome = load_genome(name)
    prog = load_program(name)
    assert genome.to_code_block() == prog


def test_genome_relu_2(interpreter: PushInterpreter):
    name = "relu_via_if"
    genome = load_genome(name)
    prog = load_program(name)
예제 #12
0
 def test_register_core(self, all_core_instructions):
     i_set = InstructionSet().register_core()
     assert set(i_set.values()) == all_core_instructions
예제 #13
0

def target_function(s):
    return s[:-2] + s[:-2]


X = np.array([
    "abcde", "", "E", "Hi", "Tom", "leprechaun", "zoomzoomzoom",
    "qwertyuiopasd", "GallopTrotCanter", "Quinona", "_abc"
]).reshape(-1, 1)
y = np.array([[target_function(s[0])] for s in X])


instruction_set = (
    InstructionSet()
    .register_by_type(["str", "int"])
    .register_n_inputs(X.shape[1])
)


spawner = GeneSpawner(
    instruction_set=instruction_set,
    literals=[],
    erc_generators=[
        lambda: random.randint(0, 10),
    ]
)


est = PushEstimator(
    search="SA",
예제 #14
0
from pyshgp.gp.estimators import PushEstimator
from pyshgp.gp.genome import GeneSpawner
from pyshgp.gp.selection import Lexicase
from pyshgp.push.instruction_set import InstructionSet


def target_function(x: float) -> (float, float):
    """Generate a training data point."""
    return max(0.0, x), max(0.1 * x, x)


X = np.arange(-1.0, 1.0, 0.05).reshape([-1, 1])
y = np.array([target_function(x[0]) for x in X])

spawner = GeneSpawner(n_inputs=1,
                      instruction_set=InstructionSet().register_core_by_stack(
                          {"float", "bool"}),
                      literals=[0.1, 0.0],
                      erc_generators=[
                          lambda: random.randint(0, 10),
                      ])

ep_lex_sel = Lexicase(epsilon=True)

if __name__ == "__main__":
    est = PushEstimator(population_size=300,
                        max_generations=50,
                        simplification_steps=500,
                        spawner=spawner,
                        selector=ep_lex_sel,
                        verbose=2)
예제 #15
0
 def test_register_core_by_stack(self):
     i_set = InstructionSet()
     i_set.register_core_by_stack({"int"})
     for i in i_set.values():
         if len(i.required_stacks()) > 0:
             assert "int" in i.required_stacks()
예제 #16
0
X_train_synthetic = [mirror_vectors() for _ in range(10)] + \
                    [equal_vectors() for _ in range(10)] + \
                    [random_vectors() for _ in range(10)]

X_train = X_train_edge + X_train_synthetic
y_train = [[target_function(x[0], x[1])] for x in X_train]


X_test = [mirror_vectors() for _ in range(100)] + \
         [equal_vectors() for _ in range(100)] + \
         [random_vectors() for _ in range(100)]
y_test = [[target_function(x[0], x[1])] for x in X_test]

spawner = GeneSpawner(
    n_inputs=2,
    instruction_set=InstructionSet().register_core_by_stack(
        {"int", "bool", "vector_int", "exec"}),
    literals=[" ", "\n"],
    erc_generators=[
        lambda: random.random() < 0.5,
    ],
)

if __name__ == "__main__":
    est = PushEstimator(search="GA",
                        population_size=500,
                        max_generations=150,
                        spawner=spawner,
                        simplification_steps=100,
                        verbose=2)

    start = time.time()
예제 #17
0

def target_function(s):
    """Generate a training data point."""
    return s[:-2] + s[:-2]


X = np.array([
    "abcde", "", "E", "Hi", "Tom", "leprechaun", "zoomzoomzoom",
    "qwertyuiopasd", "GallopTrotCanter", "Quinona", "_abc"
]).reshape(-1, 1)
y = np.array([[target_function(s[0])] for s in X])

spawner = GeneSpawner(
    n_inputs=1,
    instruction_set=InstructionSet().register_core_by_stack({"str", "int"}),
    literals=[],
    erc_generators=[
        lambda: random.randint(0, 10),
    ]
)

if __name__ == "__main__":
    est = PushEstimator(
        spawner=spawner,
        population_size=300,
        max_generations=30,
        initial_genome_size=(10, 50),
        simplification_steps=500,
        parallelism=False,
        verbose=1
예제 #18
0
import sys

from pyshgp.gp.estimators import PushEstimator
from pyshgp.gp.genome import GeneSpawner
from pyshgp.gp.selection import Lexicase
from pyshgp.push.instruction_set import InstructionSet


def target_function(x: float) -> (float, float):
    return (max(0.0, x), max(0.1 * x, x))


X = np.arange(-1.0, 1.0, 0.15).reshape([-1, 1])
y = np.array([target_function(x[0]) for x in X])

instruction_set = (InstructionSet().register_by_type(
    ["float", "bool"]).register_n_inputs(X.shape[1]))

spawner = GeneSpawner(instruction_set=instruction_set,
                      literals=[0.1, 0.0],
                      erc_generators=[
                          lambda: random.randint(0, 10),
                      ])

ep_lex_sel = Lexicase(epsilon=True)

est = PushEstimator(population_size=500,
                    max_generations=50,
                    spawner=spawner,
                    selector=ep_lex_sel,
                    verbose=2)
def check_unary_fn_translation(program_name: str):
    interpreter = PushInterpreter(InstructionSet(register_core=True))
    check_translation(program_name, interpreter)
예제 #20
0
파일: conftest.py 프로젝트: nayabur/pyshgp
def simple_gene_spawner(instr_set):
    i_set = InstructionSet()
    i_set.register_list([instr_set["int_add"], instr_set["int_sub"], instr_set["exec_if"]])
    l_set = [5, 1.2, True]
    return GeneSpawner(1, i_set, l_set, [random.random])
예제 #21
0
파일: conftest.py 프로젝트: nayabur/pyshgp
def instr_set():
    return InstructionSet(register_core=True)
예제 #22
0
파일: conftest.py 프로젝트: nayabur/pyshgp
def point_instr_set(point_type_library, point_instructions):
    return (
        InstructionSet(type_library=point_type_library, register_core=True)
        .register_list(point_instructions)
    )
예제 #23
0
        return Rectangle(float(value[0]), float(value[1]))


type_library = (PushTypeLibrary(
    register_core=False).register(PushFloat).register(RectangleType()))
"""
Next we define out instruction set using the type library and the two instructions we created.
Our two custom instructions as well as the input instructions are  defined.
The instruction set will register all core instructions that can be supported 
    using only exec, code, float, and rectangle types because the only core PushType we registered was "PushInt"
For example, the instruction int_from_float will NOT be registered because
    our type library does not define a type that would support the "int" stack.
"""

instruction_set = (InstructionSet(
    type_library=type_library,
    register_core=True).register(rectangle_areas_instruction).register(
        rectangle_from_floats_instruction))

print("Stacks: ", instruction_set.required_stacks())
print("Types: ", type_library.supported_stacks())
print("Instruction Set: ", instruction_set.keys())
print()
"""
Next we have to declare our "GeneSpawner."
We pass to it our instruction_set.
n_inputs=2 because we would like the genome to possibly include 2 input instructions.
literals=[2.0] because it will detect that 2.0 is a float, 
    and the spawner will pull floats when spawning genes and genomes.

"""
예제 #24
0
파일: odd.py 프로젝트: bmetevier/pyshgp
"""The goal of the Odd problem is to evolve a program that will produce a True if
the input integer is odd, and a False if its even.
"""
import logging
import numpy as np
import random
import sys
from pyshgp.gp.estimators import PushEstimator
from pyshgp.gp.genome import GeneSpawner
from pyshgp.push.instruction_set import InstructionSet

X = np.arange(-10, 10).reshape(-1, 1)
y = [[bool(x % 2)] for x in X]

instruction_set = (InstructionSet(register_all=True).register_n_inputs(
    X.shape[1]))

spawner = GeneSpawner(instruction_set=instruction_set,
                      literals=[],
                      erc_generators=[lambda: random.randint(0, 10)])

est = PushEstimator(spawner=spawner,
                    population_size=500,
                    max_generations=200,
                    verbose=2)

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format="%(asctime)s %(levelname)s %(message)s",
                        stream=sys.stdout)
    est.fit(X, y)
예제 #25
0
y_train = y_train_edge + y_train_synthetic

X_test = [[synthetic_input()] for _ in range(100)]
y_test = [target_function(x[0]) for x in X_test]

# Spawner


def random_char():
    """Return a random character."""
    return Char(choice(_possible_chars))


spawner = GeneSpawner(
    n_inputs=1,
    instruction_set=InstructionSet().register_core_by_stack(
        {"int", "bool", "string", "char", "exec", "stdout"}),
    literals=[" ", "\n"],
    erc_generators=[
        random_char,
    ],
)

if __name__ == "__main__":
    est = PushEstimator(search="GA",
                        population_size=500,
                        max_generations=150,
                        spawner=spawner,
                        simplification_steps=100,
                        last_str_from_stdout=True,
                        parallelism=True,
                        verbose=2)
예제 #26
0
 def test_register(self, instr_set):
     i_set = InstructionSet()
     i_set.register(instr_set["int_add"])
     assert len(i_set) == 1
예제 #27
0
    PushTypeLibrary(register_core=False)
    .register(PushFloat)
    .create_and_register("point", (Point, ), coercion_func=to_point)
)


# An instruction set which will register all core instructions that can be supported
# using only exec, code, stdout, float, and point types.
#
# For example, the instruction int_from_float will NOT be registered because
# our type library does not define a type that would support the "int" stack.
#
# Our two custom instructions as well as the input instructions are also defined.
instruction_set = (
    InstructionSet(type_library=type_library, register_core=True)
    .register(point_distance_insrt)
    .register(point_from_floats_instr)
)

print(instruction_set.keys())

spawner = GeneSpawner(
    n_inputs=2,
    instruction_set=instruction_set,
    literals=[2.0],
    erc_generators=[]
)


# Our estimator with a custom interpreter defined.
est = PushEstimator(
예제 #28
0
 def test_register_list(self, instr_set):
     i_set = InstructionSet()
     i_set.register_list([instr_set["int_add"], instr_set["int_sub"]])
     assert len(i_set) == 2
예제 #29
0
import sys

from pyshgp.gp.selection import Lexicase
from pyshgp.gp.estimators import PushEstimator
from pyshgp.gp.genome import GeneSpawner
from pyshgp.push.instruction_set import InstructionSet


def target_function(a, b):
    return (2 * a * b) + (b * b)


X = np.arange(50).reshape(-1, 2)
y = np.array([[target_function(x[0], x[1])] for x in X])

instruction_set = (InstructionSet().register_by_type(
    ["int"], exclude=["str", "exec", "code"]).register_n_inputs(X.shape[1]))

spawner = GeneSpawner(instruction_set=instruction_set,
                      literals=[],
                      erc_generators=[
                          lambda: random.randint(0, 10),
                      ])

ep_lex_sel = Lexicase(epsilon=True)

est = PushEstimator(population_size=200,
                    max_generations=50,
                    spawner=spawner,
                    selector=ep_lex_sel,
                    verbose=2)
예제 #30
0
from pyshgp.gp.selection import Lexicase
from pyshgp.gp.estimators import PushEstimator
from pyshgp.gp.genome import GeneSpawner
from pyshgp.push.instruction_set import InstructionSet


def target_function(a, b):
    """Generate a training data point."""
    return (2 * a * b) + (b * b)


X = np.arange(50).reshape(-1, 2)
y = np.array([[target_function(x[0], x[1])] for x in X])

instruction_set = (InstructionSet().register_core_by_stack(
    {"int"}, exclude_stacks={"str", "exec", "code"}))

spawner = GeneSpawner(n_inputs=2,
                      instruction_set=instruction_set,
                      literals=[],
                      erc_generators=[
                          lambda: random.randint(0, 10),
                      ])

ep_lex_sel = Lexicase(epsilon=True)

if __name__ == '__main__':
    est = PushEstimator(population_size=300,
                        max_generations=50,
                        simplification_steps=500,
                        spawner=spawner,