コード例 #1
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_core_by_stack({"int"}, exclude_stacks={"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=40,
        max_generations=10,
        simplification_steps=2)
    est.fit(X, y)

    assert isinstance(est._result.program, CodeBlock)
    assert len(est._result.program) > 0
コード例 #2
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)
コード例 #3
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
コード例 #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(
        instruction_set=point_instr_set,
        literals=[],
        erc_generators=[]
    )

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

    assert isinstance(est._result.program, CodeBlock)
    assert len(est._result.program) > 0
コード例 #5
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
コード例 #6
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
コード例 #7
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)
コード例 #8
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)
コード例 #9
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))
コード例 #10
0
    and the spawner will pull floats when spawning genes and genomes.

"""

spawner = GeneSpawner(n_inputs=2,
                      instruction_set=instruction_set,
                      literals=[2.0],
                      erc_generators=[])
"""
We have everything we need to configure a run of PushGP.
We will create a "PushEstimator" and parameterize it however we want. 
We pass an instance of our spawner and define a custom interpreter 
"""

est = PushEstimator(spawner=spawner,
                    population_size=300,
                    max_generations=20,
                    initial_genome_size=(5, 55),
                    simplification_steps=500,
                    interpreter=PushInterpreter(instruction_set),
                    verbose=2)
"""
Now we begin our PyshGP run with custom instructions and types.
If we do find a solution, it should happen relatively quickly. 
Otherwise, the run may have gotten "stuck" or not find a solution during the evolutionary process. 
    We have set max_generations=20 to account for this. Try running it again if this happens.
"""

if __name__ == "__main__":
    est.fit(X, y)
コード例 #11
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))
コード例 #12
0
ファイル: triple.py プロジェクト: vishalbelsare/pyshgp
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.
コード例 #13
0
ファイル: iris.py プロジェクト: vishalbelsare/pyshgp
    [6.3, 2.9, 5.6, 1.8, 2],
    [6.5, 3.0, 5.8, 2.2, 2],
    [7.6, 3.0, 6.6, 2.1, 2],
    [4.9, 2.5, 4.5, 1.7, 2],
    [7.3, 2.9, 6.3, 1.8, 2],
    [6.7, 2.5, 5.8, 1.8, 2],
    [7.2, 3.6, 6.1, 2.5, 2],
],
                    columns=[
                        "sepal_length", "sepal_width", "petal_length",
                        "petal_width", "label"
                    ])

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

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

    x = data[["sepal_length", "sepal_width", "petal_length", "petal_width"]]
    y = data[["label"]]

    est.fit(x, y)
コード例 #14
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)))
コード例 #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))
コード例 #16
0
ファイル: odd.py プロジェクト: fagan2888/pyshgp
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))
コード例 #17
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))
コード例 #18
0
ファイル: point_distance.py プロジェクト: erp12/Pysh
)

print(instruction_set.keys())

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


# Our estimator with a custom interpreter defined.
est = PushEstimator(
    spawner=spawner,
    population_size=500,
    max_generations=20,
    simplification_steps=1000,
    interpreter=PushInterpreter(instruction_set),
    verbose=2
)


if __name__ == "__main__":
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s %(levelname)s %(message)s",
        stream=sys.stdout
    )
    est.fit(X, y)
    print(est._result.program)
    print(est.predict(X))
    print(est.score(X, y))
コード例 #19
0
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)

    est.fit(X=X, y=y)
コード例 #20
0
ファイル: string_demo.py プロジェクト: vishalbelsare/pyshgp
    "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))
コード例 #21
0
ファイル: demo.py プロジェクト: erp12/pyshgp-demo
                      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`