Exemplo n.º 1
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)
Exemplo n.º 2
0
def test_estimator_with_pandas(simple_test_spawner):
    df = pd.DataFrame({
        "x1": [-2, -1, 0, 1, 2],
        "x2": [-1, 2, -3, 4, -5],
        "y":  [2, -2, 0, 4, -10]
    })

    est = PushEstimator(
        spawner=simple_test_spawner,
        population_size=10,
        max_generations=3,
        simplification_steps=3,
        parallelism=False
    )
    est.fit(df[["x1", "x2"]], df[["y"]])

    assert isinstance(est.solution, Individual)
    assert len(est.solution.program.code) > 0
Exemplo n.º 3
0
def run_ga_on_odd_test(spawner, parallelism):
    X = np.arange(-10, 10).reshape(-1, 1)
    y = [[bool(x[0] % 2)] for x in X]

    est = PushEstimator(
        spawner=spawner,
        population_size=10,
        max_generations=3,
        simplification_steps=3,
        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)
Exemplo n.º 4
0
def test_estimator_with_custom_types(point_cls, point_instr_set):
    X = np.arange(-1.0, 1.0, 0.05).reshape(-1, 4)
    y = [[point_distance(point_cls(x[0], x[1]), point_cls(x[2], x[3]))]
         for x in X]

    spawner = GeneSpawner(n_inputs=1,
                          instruction_set=point_instr_set,
                          literals=[],
                          erc_generators=[])

    est = PushEstimator(
        spawner=spawner,
        population_size=30,
        max_generations=3,
        simplification_steps=2,
        interpreter=PushInterpreter(point_instr_set),
    )
    est.fit(X, y)

    assert isinstance(est.solution, Individual)
    assert len(est.solution.program.code) > 0
Exemplo n.º 5
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
Exemplo n.º 6
0
    "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
    )

    est.fit(X=X, y=y)
    print("Best program found:")
    print(est.solution.program.pretty_str())
    print("Errors:")
    print(est.score(X, y))
Exemplo n.º 7
0
    .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(
    spawner=spawner,
    population_size=300,
    max_generations=20,
    simplification_steps=500,
    interpreter=PushInterpreter(instruction_set),
    verbose=2
)


if __name__ == "__main__":
    est.fit(X, y)
    print(est.solution.program)
    print(est.predict(X))
    print(est.score(X, y))
Exemplo n.º 8
0
spawner = GeneSpawner(n_inputs=1,
                      instruction_set="core",
                      literals=[],
                      erc_generators=[lambda: random.randint(0, 10)])
"""
We now have everything we need to configure a run of PushGP.
We will create a `PushEstimator` and parameterize it however we want. 
Let's be sure to pass an instance of our spawner.
Like earlier, we  simplified the estimator by only passing it what is necessary,
    but there are several other parameters that can be filled and changed.
"""

if __name__ == "__main__":
    est = PushEstimator(spawner=spawner,
                        simplification_steps=2000,
                        parallelism=False,
                        verbose=2)
    """
    Now we begin our PushGP run!
    We will call the estimator's `.fit` method on our training data. 
    This function evolves and finds the best program that solves the given problem (tripling the numbers in 'X').
    This  function also prints a lot of information as it evolves and runs. You can ignore most of this for now. 
    After it prints "End Run," it will state whether or not a solution was found.      
    """

    est.fit(X=X, y=y)
    print()
    """
    Next we print the push program using '.solution.program.pretty_str().'
    This will print out a String of push commands that make up the program. 
    If no solution was found, it will print the best program it found instead.
Exemplo n.º 9
0
import numpy as np
import random
from pyshgp.gp.estimators import PushEstimator
from pyshgp.gp.genome import GeneSpawner


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


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


if __name__ == "__main__":
    est = PushEstimator(
        spawner=spawner,
        population_size=300,
        max_generations=100,
        verbose=2
    )

    est.fit(X, y)
    print(est.solution.program)
    print(est.predict(X))
    print(est.score(X, y))
Exemplo n.º 10
0
         [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()
    est.fit(X=X_train, y=y_train)
    end = time.time()
    print("========================================")
    print("post-evolution stats")
    print("========================================")
    print("Runtime: ", end - start)
    print("Test Error: ", np.sum(est.score(X_test, y_test)))
Exemplo n.º 11
0

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)

    start = time.time()
    est.fit(X=X_train, y=y_train)
    end = time.time()
    print("train_error: ", est.solution.total_error)
    print("test_error: ", np.sum(est.score(X_test, y_test)))
    print("runtime: ", end - start)
    print("final_generation: ", est.search.generation)
    print("best_genome: ", est.solution.genome)
Exemplo n.º 12
0
# 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)

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format="%(asctime)s %(levelname)s %(message)s",
                        stream=sys.stdout)
    start = time.time()
    est.fit(X=X_train, y=y_train)
    end = time.time()
    print("train_error: ", est.search.best_seen.total_error)
    print("test_error: ", np.sum(est.score(X_test, y_test)))
    print("runtime: ", end - start)
    print("final_generation: ", est.search.generation)
    print("best_genome_json: ", est.search.best_seen.genome.jsonify())
Exemplo n.º 13
0

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)

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO,
                        format="%(asctime)s %(levelname)s %(message)s",
                        stream=sys.stdout)
    est.fit(X=X, y=y)
    print(est._result.program)
    print(est.predict(X))
    print(est.score(X, y))
Exemplo n.º 14
0
)


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


est = PushEstimator(
    search="SA",
    spawner=spawner,
    max_generations=500,
    initial_genome_size=(10, 50),
    verbose=2
)

if __name__ == "__main__":
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s %(levelname)s %(message)s",
        stream=sys.stdout
    )
    est.fit(X=X, y=y)
    print(est._result.program)
    print(est.predict(X))
    print(est.score(X, y))
Exemplo n.º 15
0
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(spawner=spawner,
                    population_size=200,
                    max_generations=30,
                    initial_genome_size=(10, 50),
                    verbose=2)

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format="%(asctime)s %(levelname)s %(message)s",
                        stream=sys.stdout)
    est.fit(X=X, y=y)
    print(est._result.program)
    print(est.predict(X))
    print(est.score(X, y))
Exemplo n.º 16
0
                      literals=["Fizz", "Buzz"],
                      erc_generators=[
                          lambda: random.randint(0, 10),
                      ])
"""
We now have everything we need to configure a run of PushGP.

We will create a `PushEstimator` and parameterize it however we want. Let's be sure to 
pass an instance of our custom parent selector.
"""

est = PushEstimator(spawner=spawner,
                    selector=WackySelector(),
                    variation_strategy="umad",
                    last_str_from_stdout=True,
                    population_size=300,
                    max_generations=100,
                    initial_genome_size=(10, 50),
                    simplification_steps=2000,
                    parallelism=False,
                    verbose=2)
"""
Before we start our PushGP, let's customize what will be printed to stdout each generation.

By default, `pyshgp` will print a lot of statistics each generation of evolution. Perhaps we
want to show less.

The `TapManager` provided by `pyshgp` will run a side effect (like printing or logging) before
and after a certain method is called. 

Let's define a custom `Tap` that will print before each generation. We specify the tap is for 
`step` method of all `SearchAlgorithm` objects using the function id `pyshgp.gp.search.SearchAlgorithm.step`