diff --git a/.travis.yml b/.travis.yml index 17e49d86..d0ea52fa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,6 +25,6 @@ script: - pytest --cov - black --check . - flake8 --config=.flake8 . - - mypy gp + - mypy cgp after_success: - coveralls diff --git a/README.md b/README.md index 02387a06..e62e7d65 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -python-gp -========= +HAL-CGP +======= [![Python3.6](https://img.shields.io/badge/python-3.6-red.svg)](https://www.python.org/downloads/release/python-369/) [![Python3.7](https://img.shields.io/badge/python-3.7-red.svg)](https://www.python.org/) [![Python3.8](https://img.shields.io/badge/python-3.8-red.svg)](https://www.python.org/) [![GPL license](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/old-licenses/gpl-3.0.html) -[![Build Status](https://api.travis-ci.org/Happy-Algorithms-League/python-gp.svg?branch=master)](https://travis-ci.org/Happy-Algorithms-League/python-gp) +[![Build Status](https://api.travis-ci.org/Happy-Algorithms-League/hal-cgp.svg?branch=master)](https://travis-ci.org/Happy-Algorithms-League/hal-cgp) [![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/) Cartesian Genetic Programming (CGP) in Python. @@ -46,7 +46,7 @@ genome_params = { "n_columns": 10, "n_rows": 2, "levels_back": 5, - "primitives": [gp.Add, gp.Sub, gp.Mul, gp.Div, gp.ConstantFloat], + "primitives": [cgp.Add, cgp.Sub, cgp.Mul, cgp.Div, cgp.ConstantFloat], } ea_params = {"n_offsprings": 10, "n_breeding": 10, "tournament_size": 2, "n_processes": 2} @@ -55,8 +55,8 @@ evolve_params = {"max_generations": 1000, "min_fitness": 0.0} ``` 3. Initialize a population and an evolutionary algorithm instance: ```python -pop = gp.Population(**population_params, genome_params=genome_params) -ea = gp.ea.MuPlusLambda(**ea_params) +pop = cgp.Population(**population_params, genome_params=genome_params) +ea = cgp.ea.MuPlusLambda(**ea_params) ``` 4. Define a callback function to record information about the progress of the evolution: ```python @@ -67,7 +67,7 @@ def recording_callback(pop): ``` 5. Use the `evolve` function that ties everything together and executes the evolution: ```python -gp.evolve(pop, obj, ea, **evolve_params, print_progress=True, callback=recording_callback) +cgp.evolve(pop, obj, ea, **evolve_params, print_progress=True, callback=recording_callback) ``` diff --git a/gp/__init__.py b/cgp/__init__.py similarity index 100% rename from gp/__init__.py rename to cgp/__init__.py diff --git a/gp/cartesian_graph.py b/cgp/cartesian_graph.py similarity index 100% rename from gp/cartesian_graph.py rename to cgp/cartesian_graph.py diff --git a/gp/ea/__init__.py b/cgp/ea/__init__.py similarity index 100% rename from gp/ea/__init__.py rename to cgp/ea/__init__.py diff --git a/gp/ea/mu_plus_lambda.py b/cgp/ea/mu_plus_lambda.py similarity index 96% rename from gp/ea/mu_plus_lambda.py rename to cgp/ea/mu_plus_lambda.py index afe2dd51..ddab5664 100644 --- a/gp/ea/mu_plus_lambda.py +++ b/cgp/ea/mu_plus_lambda.py @@ -65,11 +65,11 @@ def initialize_fitness_parents( Parameters ---------- - pop : gp.Population + pop : Population Population instance. objective : Callable[[gp.Individual], gp.Individual] An objective function used for the evolution. Needs to take an - invidual (gp.Individual) as input parameter and return + individual (Individual) as input parameter and return a modified individual (with updated fitness). """ # TODO can we avoid this function? how should a population be @@ -81,16 +81,16 @@ def step(self, pop: Population, objective: Callable[[Individual], Individual]) - Parameters ---------- - pop : gp.Population + pop : Population Population instance. objective : Callable[[gp.Individual], gp.Individual] An objective function used for the evolution. Needs to take an - invidual (gp.Individual) as input parameter and return + individual (Individual) as input parameter and return a modified individual (with updated fitness). Returns ---------- - pop : gp.Population + Population Modified population with new parents. """ offsprings = self._create_new_offspring_generation(pop) diff --git a/gp/genome.py b/cgp/genome.py similarity index 99% rename from gp/genome.py rename to cgp/genome.py index 8dda19f9..7e14b4a0 100644 --- a/gp/genome.py +++ b/cgp/genome.py @@ -423,7 +423,7 @@ def clone(self) -> "Genome": Returns ------- - gp.Genome + Genome """ new = Genome( diff --git a/gp/hl_api.py b/cgp/hl_api.py similarity index 96% rename from gp/hl_api.py rename to cgp/hl_api.py index 36ed6281..faecd8d0 100644 --- a/gp/hl_api.py +++ b/cgp/hl_api.py @@ -21,11 +21,11 @@ def evolve( Parameters ---------- - pop : gp.Population + pop : Population A population class that will be evolved. objective : Callable An objective function used for the evolution. Needs to take an - invidual (gp.Individual) as input parameter and return + individual (Individual) as input parameter and return a modified individual (with updated fitness). ea : EA algorithm instance The evolution algorithm. Needs to be a class instance with an diff --git a/gp/individual.py b/cgp/individual.py similarity index 97% rename from gp/individual.py rename to cgp/individual.py index 903ef1a0..1331fce8 100644 --- a/gp/individual.py +++ b/cgp/individual.py @@ -23,7 +23,7 @@ def __init__(self, fitness, genome): fitness : float Fitness of the individual. genome: Genome instance - Genome of the invididual. + Genome of the individual. """ self.fitness = fitness self.genome = genome @@ -40,7 +40,7 @@ def clone(self): Returns ------- - gp.Individual + Individual """ new_individual = Individual(self.fitness, self.genome.clone()) @@ -55,14 +55,14 @@ def crossover(self, other_parent, rng): Parameters ---------- - other_parent : gp.Individual + other_parent : Individual Other individual to perform crossover with. rng : numpy.RandomState Random number generator instance to use for crossover. Returns ------- - gp.Individual + Individual """ raise NotImplementedError("crossover currently not supported") @@ -181,7 +181,7 @@ def update_parameters_from_torch_class(self, torch_cls): class IndividualMultiGenome(Individual): """An individual with multiple genomes. - Derived from gp.Individual. + Derived from Individual. """ def clone(self): diff --git a/gp/local_search/__init__.py b/cgp/local_search/__init__.py similarity index 100% rename from gp/local_search/__init__.py rename to cgp/local_search/__init__.py diff --git a/gp/local_search/gradient_based.py b/cgp/local_search/gradient_based.py similarity index 100% rename from gp/local_search/gradient_based.py rename to cgp/local_search/gradient_based.py diff --git a/gp/node.py b/cgp/node.py similarity index 99% rename from gp/node.py rename to cgp/node.py index 9df36d86..e3ddc284 100644 --- a/gp/node.py +++ b/cgp/node.py @@ -11,7 +11,7 @@ def register(cls: Type["Node"]) -> None: Parameters ---------- - cls : Type[gp.Node] + cls : Type[Node] Primitive to be registered. Returns diff --git a/gp/node_factories.py b/cgp/node_factories.py similarity index 100% rename from gp/node_factories.py rename to cgp/node_factories.py diff --git a/gp/population.py b/cgp/population.py similarity index 97% rename from gp/population.py rename to cgp/population.py index ac1b11c9..195bea89 100755 --- a/gp/population.py +++ b/cgp/population.py @@ -95,14 +95,14 @@ def crossover(self, breeding_pool: List[Individual], n_offsprings: int) -> List[ Parameters ---------- - breeding_pool : List[gp.Individual] + breeding_pool : List[Individual] List of individuals from which the offspring are created. n_offsprings : int Number of offspring to be created. Returns ---------- - List[gp.Individual] + List[Individual] List of offspring individuals. """ # in principle crossover would rely on a procedure like the @@ -130,12 +130,12 @@ def mutate(self, offsprings: List[Individual]) -> List[Individual]: Parameters ---------- - offsprings : List[gp.Individual] + offsprings : List[Individual] List of offspring individuals to be mutated. Returns ---------- - List[gp.Individual] + List[Individual] List of mutated offspring individuals. """ diff --git a/gp/primitives.py b/cgp/primitives.py similarity index 100% rename from gp/primitives.py rename to cgp/primitives.py diff --git a/gp/utils.py b/cgp/utils.py similarity index 100% rename from gp/utils.py rename to cgp/utils.py diff --git a/examples/example_caching.py b/examples/example_caching.py index b8e21086..2fdcc6c3 100644 --- a/examples/example_caching.py +++ b/examples/example_caching.py @@ -1,7 +1,7 @@ import numpy as np import time -import gp +import cgp """ Example demonstrating the use of the caching decorator. @@ -16,7 +16,7 @@ def f_target(x): return x ** 2 + x + 1.0 -@gp.utils.disk_cache("example_caching_cache.pkl") +@cgp.utils.disk_cache("example_caching_cache.pkl") def inner_objective(expr): """The caching decorator uses the function parameters to identify identical function calls. Here, as many different genotypes @@ -62,15 +62,15 @@ def evolution(): "n_columns": 10, "n_rows": 2, "levels_back": 2, - "primitives": [gp.Add, gp.Sub, gp.Mul, gp.ConstantFloat], + "primitives": [cgp.Add, cgp.Sub, cgp.Mul, cgp.ConstantFloat], }, "evolve_params": {"max_generations": 100, "min_fitness": -1e-12}, } - pop = gp.Population(**params["population_params"], genome_params=params["genome_params"]) - ea = gp.ea.MuPlusLambda(**params["ea_params"]) + pop = cgp.Population(**params["population_params"], genome_params=params["genome_params"]) + ea = cgp.ea.MuPlusLambda(**params["ea_params"]) - gp.evolve(pop, objective, ea, **params["evolve_params"], print_progress=True) + cgp.evolve(pop, objective, ea, **params["evolve_params"], print_progress=True) return pop.champion diff --git a/examples/example_differential_evo_regression.py b/examples/example_differential_evo_regression.py index a39a9b2e..50a338df 100644 --- a/examples/example_differential_evo_regression.py +++ b/examples/example_differential_evo_regression.py @@ -4,7 +4,7 @@ import scipy.constants import torch -import gp +import cgp """Example demonstrating the use of Cartesian Genetic Programming for @@ -67,7 +67,7 @@ def evolution(): "n_columns": 20, "n_rows": 1, "levels_back": None, - "primitives": [gp.Add, gp.Sub, gp.Mul, gp.Parameter], + "primitives": [cgp.Add, cgp.Sub, cgp.Mul, cgp.Parameter], } ea_params = {"n_offsprings": 4, "n_breeding": 4, "tournament_size": 1, "n_processes": 1} @@ -78,19 +78,19 @@ def evolution(): # average out for clipped values local_search_params = {"lr": 1e-3, "gradient_steps": 9} - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) # define the function for local search; parameters such as the # learning rate and number of gradient steps are fixed via the use # of `partial`; the local_search function should only receive a # population of individuals as input local_search = functools.partial( - gp.local_search.gradient_based, + cgp.local_search.gradient_based, objective=functools.partial(inner_objective, seed=population_params["seed"]), **local_search_params, ) - ea = gp.ea.MuPlusLambda(**ea_params, local_search=local_search) + ea = cgp.ea.MuPlusLambda(**ea_params, local_search=local_search) history = {} history["champion"] = [] @@ -102,7 +102,7 @@ def recording_callback(pop): obj = functools.partial(objective, seed=population_params["seed"]) - gp.evolve( + cgp.evolve( pop, obj, ea, **evolve_params, print_progress=True, callback=recording_callback, ) diff --git a/examples/example_evo_regression.py b/examples/example_evo_regression.py index 8c130a23..f4c840fb 100644 --- a/examples/example_evo_regression.py +++ b/examples/example_evo_regression.py @@ -4,7 +4,7 @@ import scipy.constants import warnings -import gp +import cgp """Example demonstrating the use of Cartesian Genetic Programming for @@ -24,14 +24,14 @@ def objective(individual, target_function, seed): Parameters ---------- - individual : gp.Individual + individual : Individual Individual of the Cartesian Genetic Programming Framework. target_function : Callable Target function. Returns ------- - gp.Individual + Individual Modified individual with updated fitness value. """ if individual.fitness is not None: @@ -87,7 +87,7 @@ def evolution(f_target): "n_columns": 10, "n_rows": 2, "levels_back": 5, - "primitives": [gp.Add, gp.Sub, gp.Mul, gp.Div, gp.ConstantFloat], + "primitives": [cgp.Add, cgp.Sub, cgp.Mul, cgp.Div, cgp.ConstantFloat], } ea_params = {"n_offsprings": 10, "n_breeding": 10, "tournament_size": 2, "n_processes": 2} @@ -95,10 +95,10 @@ def evolution(f_target): evolve_params = {"max_generations": 1000, "min_fitness": 0.0} # create population that will be evolved - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) # create instance of evolutionary algorithm - ea = gp.ea.MuPlusLambda(**ea_params) + ea = cgp.ea.MuPlusLambda(**ea_params) # define callback for recording of fitness over generations history = {} @@ -112,7 +112,7 @@ def recording_callback(pop): obj = functools.partial(objective, target_function=f_target, seed=population_params["seed"]) # Perform the evolution - gp.evolve( + cgp.evolve( pop, obj, ea, **evolve_params, print_progress=True, callback=recording_callback, ) return history, pop.champion diff --git a/setup.py b/setup.py index f76cdfef..220eaefa 100644 --- a/setup.py +++ b/setup.py @@ -23,18 +23,18 @@ def read_extra_requirements(): setup( - name="python-gp", + name="hal-cgp", version="0.1", author="Jakob Jordan, Maximilian Schmidt", author_email="jakobjordan@posteo.de", - description=("Cartesian Genetic Programming in Python."), + description=("Cartesian Genetic Programming in pure Python."), license="GPLv3", keywords="genetic programming", - url="https://github.com/jakobj/python-gp", + url="https://github.com/Happy-Algorithms-League/hal-cgp", python_requires=">=3.6, <4", install_requires=read_requirements(), extras_require=read_extra_requirements(), - packages=["gp", "gp.ea", "gp.local_search"], + packages=["cgp", "cgp.ea", "cgp.local_search"], long_description=open("README.md").read(), long_description_content_type="text/markdown", classifiers=[ @@ -45,6 +45,7 @@ def read_extra_requirements(): "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", - "Topic :: Utilities", + "Topic :: Scientific/Engineering", + "Typing :: Typed", ], ) diff --git a/test/conftest.py b/test/conftest.py index 04a432a3..72753c74 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,6 +1,7 @@ -import gp from pytest import fixture +import cgp + @fixture def rng_seed(): @@ -15,7 +16,7 @@ def genome_params(): "n_columns": 3, "n_rows": 3, "levels_back": 2, - "primitives": (gp.Add, gp.Sub, gp.ConstantFloat), + "primitives": (cgp.Add, cgp.Sub, cgp.ConstantFloat), } @@ -36,7 +37,7 @@ def mutation_rate(): @fixture def population_simple_fitness(population_params, genome_params): - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) for i, parent in enumerate(pop.parents): parent.fitness = i diff --git a/test/test_cartesian_graph.py b/test/test_cartesian_graph.py index b38e07f9..0d86d498 100644 --- a/test/test_cartesian_graph.py +++ b/test/test_cartesian_graph.py @@ -2,14 +2,14 @@ import numpy as np import pytest -import gp -from gp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE +import cgp +from cgp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE def test_direct_input_output(): params = {"n_inputs": 1, "n_outputs": 1, "n_columns": 3, "n_rows": 3, "levels_back": 2} - primitives = (gp.Add, gp.Sub) - genome = gp.Genome( + primitives = (cgp.Add, cgp.Sub) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -20,7 +20,7 @@ def test_direct_input_output(): genome.randomize(np.random) genome[-2:] = [0, ID_NON_CODING_GENE] # set inputs for output node to input node - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) x = [2.14159] y = graph(x) @@ -29,8 +29,8 @@ def test_direct_input_output(): def test_to_func_simple(): - primitives = (gp.Add,) - genome = gp.Genome(2, 1, 1, 1, 1, primitives) + primitives = (cgp.Add,) + genome = cgp.Genome(2, 1, 1, 1, 1, primitives) genome.dna = [ ID_INPUT_NODE, @@ -46,7 +46,7 @@ def test_to_func_simple(): 2, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) f = graph.to_func() x = [5.0, 2.0] @@ -54,8 +54,8 @@ def test_to_func_simple(): assert x[0] + x[1] == pytest.approx(y[0]) - primitives = (gp.Sub,) - genome = gp.Genome(2, 1, 1, 1, 1, primitives) + primitives = (cgp.Sub,) + genome = cgp.Genome(2, 1, 1, 1, 1, primitives) genome.dna = [ ID_INPUT_NODE, @@ -71,7 +71,7 @@ def test_to_func_simple(): 2, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) f = graph.to_func() x = [5.0, 2.0] @@ -81,8 +81,8 @@ def test_to_func_simple(): def test_compile_two_columns(): - primitives = (gp.Add, gp.Sub) - genome = gp.Genome(2, 1, 2, 1, 1, primitives) + primitives = (cgp.Add, cgp.Sub) + genome = cgp.Genome(2, 1, 2, 1, 1, primitives) genome.dna = [ ID_INPUT_NODE, @@ -101,7 +101,7 @@ def test_compile_two_columns(): 3, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) f = graph.to_func() x = [5.0, 2.0] @@ -111,8 +111,8 @@ def test_compile_two_columns(): def test_compile_two_columns_two_rows(): - primitives = (gp.Add, gp.Sub) - genome = gp.Genome(2, 2, 2, 2, 1, primitives) + primitives = (cgp.Add, cgp.Sub) + genome = cgp.Genome(2, 2, 2, 2, 1, primitives) genome.dna = [ ID_INPUT_NODE, @@ -140,7 +140,7 @@ def test_compile_two_columns_two_rows(): 5, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) f = graph.to_func() x = [5.0, 2.0] @@ -153,8 +153,8 @@ def test_compile_two_columns_two_rows(): def test_compile_addsubmul(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 2, "n_rows": 2, "levels_back": 1} - primitives = (gp.Add, gp.Sub, gp.Mul) - genome = gp.Genome( + primitives = (cgp.Add, cgp.Sub, cgp.Mul) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -185,7 +185,7 @@ def test_compile_addsubmul(): 4, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) f = graph.to_func() x = [5.0, 2.0] @@ -195,8 +195,8 @@ def test_compile_addsubmul(): def test_to_numpy(): - primitives = (gp.Add, gp.Mul, gp.ConstantFloat) - genome = gp.Genome(1, 1, 2, 2, 1, primitives) + primitives = (cgp.Add, cgp.Mul, cgp.ConstantFloat) + genome = cgp.Genome(1, 1, 2, 2, 1, primitives) # f(x) = x ** 2 + 1. genome.dna = [ ID_INPUT_NODE, @@ -218,7 +218,7 @@ def test_to_numpy(): 3, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) f = graph.to_numpy() x = np.random.normal(size=(100, 1)) @@ -229,8 +229,8 @@ def test_to_numpy(): batch_sizes = [1, 10] -primitives = (gp.Mul, gp.ConstantFloat) -genomes = [gp.Genome(1, 1, 2, 2, 1, primitives) for i in range(2)] +primitives = (cgp.Mul, cgp.ConstantFloat) +genomes = [cgp.Genome(1, 1, 2, 2, 1, primitives) for i in range(2)] # Function: f(x) = 1*x genomes[0].dna = [ ID_INPUT_NODE, @@ -274,7 +274,7 @@ def test_to_numpy(): ID_NON_CODING_GENE, ] -genomes += [gp.Genome(1, 2, 2, 2, 1, primitives) for i in range(2)] +genomes += [cgp.Genome(1, 2, 2, 2, 1, primitives) for i in range(2)] # Function: f(x) = (1*x, 1*1) genomes[2].dna = [ ID_INPUT_NODE, @@ -328,7 +328,7 @@ def test_to_numpy(): @pytest.mark.parametrize("genome, batch_size", itertools.product(genomes, batch_sizes)) def test_compile_numpy_output_shape(genome, batch_size): - c = gp.CartesianGraph(genome).to_numpy() + c = cgp.CartesianGraph(genome).to_numpy() x = np.random.normal(size=(batch_size, 1)) y = c(x) assert y.shape == (batch_size, genome._n_outputs) @@ -338,7 +338,7 @@ def test_compile_numpy_output_shape(genome, batch_size): def test_compile_torch_output_shape(genome, batch_size): torch = pytest.importorskip("torch") - c = gp.CartesianGraph(genome).to_torch() + c = cgp.CartesianGraph(genome).to_torch() x = torch.Tensor(batch_size, 1).normal_() y = c(x) assert y.shape == (batch_size, genome._n_outputs) @@ -347,8 +347,8 @@ def test_compile_torch_output_shape(genome, batch_size): def test_to_sympy(): sympy = pytest.importorskip("sympy") - primitives = (gp.Add, gp.ConstantFloat) - genome = gp.Genome(1, 1, 2, 2, 1, primitives) + primitives = (cgp.Add, cgp.ConstantFloat) + genome = cgp.Genome(1, 1, 2, 2, 1, primitives) genome.dna = [ ID_INPUT_NODE, @@ -370,7 +370,7 @@ def test_to_sympy(): 3, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) x_0_target, y_0_target = sympy.symbols("x_0_target y_0_target") y_0_target = x_0_target + 1.0 @@ -386,8 +386,8 @@ def test_to_sympy(): def test_catch_invalid_sympy_expr(): pytest.importorskip("sympy") - primitives = (gp.Sub, gp.Div) - genome = gp.Genome(1, 1, 2, 1, 1, primitives) + primitives = (cgp.Sub, cgp.Div) + genome = cgp.Genome(1, 1, 2, 1, 1, primitives) # x[0] / (x[0] - x[0]) genome.dna = [ @@ -404,7 +404,7 @@ def test_catch_invalid_sympy_expr(): 2, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) with pytest.raises(Exception): graph.to_sympy(simplify=True) @@ -413,8 +413,8 @@ def test_catch_invalid_sympy_expr(): def test_allow_powers_of_x_0(): pytest.importorskip("sympy") - primitives = (gp.Sub, gp.Mul) - genome = gp.Genome(1, 1, 2, 1, 1, primitives) + primitives = (cgp.Sub, cgp.Mul) + genome = cgp.Genome(1, 1, 2, 1, 1, primitives) # x[0] ** 2 genome.dna = [ @@ -431,16 +431,16 @@ def test_allow_powers_of_x_0(): 2, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) graph.to_sympy(simplify=True) def test_input_dim_python(rng_seed): rng = np.random.RandomState(rng_seed) - genome = gp.Genome(2, 1, 1, 1, 1, (gp.ConstantFloat,)) + genome = cgp.Genome(2, 1, 1, 1, 1, (cgp.ConstantFloat,)) genome.randomize(rng) - f = gp.CartesianGraph(genome).to_func() + f = cgp.CartesianGraph(genome).to_func() # fail for too short input with pytest.raises(ValueError): @@ -457,9 +457,9 @@ def test_input_dim_python(rng_seed): def test_input_dim_numpy(rng_seed): rng = np.random.RandomState(rng_seed) - genome = gp.Genome(2, 1, 1, 1, 1, (gp.ConstantFloat,)) + genome = cgp.Genome(2, 1, 1, 1, 1, (cgp.ConstantFloat,)) genome.randomize(rng) - f = gp.CartesianGraph(genome).to_numpy() + f = cgp.CartesianGraph(genome).to_numpy() # fail for missing batch dimension with pytest.raises(ValueError): @@ -482,9 +482,9 @@ def test_input_dim_torch(rng_seed): rng = np.random.RandomState(rng_seed) - genome = gp.Genome(2, 1, 1, 1, 1, (gp.ConstantFloat,)) + genome = cgp.Genome(2, 1, 1, 1, 1, (cgp.ConstantFloat,)) genome.randomize(rng) - f = gp.CartesianGraph(genome).to_torch() + f = cgp.CartesianGraph(genome).to_torch() # fail for missing batch dimension with pytest.raises(ValueError): @@ -503,8 +503,8 @@ def test_input_dim_torch(rng_seed): def test_pretty_str(): - primitives = (gp.Sub, gp.Mul) - genome = gp.Genome(1, 1, 2, 1, 1, primitives) + primitives = (cgp.Sub, cgp.Mul) + genome = cgp.Genome(1, 1, 2, 1, 1, primitives) # x[0] ** 2 genome.dna = [ @@ -521,7 +521,7 @@ def test_pretty_str(): 2, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) pretty_str = graph.pretty_str() @@ -534,10 +534,10 @@ def test_pretty_str(): def test_pretty_str_with_unequal_inputs_rows_outputs(): - primitives = (gp.Add,) + primitives = (cgp.Add,) # less rows than inputs/outputs - genome = gp.Genome(1, 1, 1, 2, 1, primitives) + genome = cgp.Genome(1, 1, 1, 2, 1, primitives) # f(x) = x[0] + x[0] genome.dna = [ ID_INPUT_NODE, @@ -553,7 +553,7 @@ def test_pretty_str_with_unequal_inputs_rows_outputs(): 1, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) expected_pretty_str = """ 00 * InputNode \t01 * Add (00,00) \t03 * OutputNode (01) \t @@ -562,7 +562,7 @@ def test_pretty_str_with_unequal_inputs_rows_outputs(): assert graph.pretty_str() == expected_pretty_str # more rows than inputs/outputs - genome = gp.Genome(3, 3, 1, 2, 1, primitives) + genome = cgp.Genome(3, 3, 1, 2, 1, primitives) # f(x) = [x[0] + x[1], x[0] + x[1], x[1] + x[2]] genome.dna = [ ID_INPUT_NODE, @@ -590,7 +590,7 @@ def test_pretty_str_with_unequal_inputs_rows_outputs(): 4, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) expected_pretty_str = """ 00 * InputNode \t03 * Add (00,01) \t05 * OutputNode (03) \t diff --git a/test/test_ea_mu_plus_lambda.py b/test/test_ea_mu_plus_lambda.py index c2a8eecd..806f2d95 100644 --- a/test/test_ea_mu_plus_lambda.py +++ b/test/test_ea_mu_plus_lambda.py @@ -2,7 +2,7 @@ import numpy as np import pytest -import gp +import cgp def test_objective_with_label(population_params, genome_params): @@ -15,9 +15,9 @@ def objective_with_label(individual, label): individual.fitness = -1 return individual - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) - ea = gp.ea.MuPlusLambda(1, 2, 1) + ea = cgp.ea.MuPlusLambda(1, 2, 1) ea.initialize_fitness_parents(pop, objective_without_label) ea.step(pop, objective_without_label) @@ -36,8 +36,8 @@ def objective(individual): individual.fitness = np.random.rand() return individual - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) - ea = gp.ea.MuPlusLambda(10, 10, 1) + ea = cgp.ea.MuPlusLambda(10, 10, 1) ea.initialize_fitness_parents(pop, objective) ea.step(pop, objective) diff --git a/test/test_genome.py b/test/test_genome.py index 9462547a..15d1e34e 100644 --- a/test/test_genome.py +++ b/test/test_genome.py @@ -1,15 +1,15 @@ import numpy as np import pytest -import gp -from gp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE +import cgp +from cgp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE def test_check_dna_consistency(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 1, "n_rows": 1, "levels_back": 1} - primitives = (gp.Add,) - genome = gp.Genome( + primitives = (cgp.Add,) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -173,8 +173,8 @@ def test_check_dna_consistency(): def test_permissible_inputs(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 4, "n_rows": 3, "levels_back": 2} - primitives = (gp.Add,) - genome = gp.Genome( + primitives = (cgp.Add,) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -210,8 +210,8 @@ def test_permissible_inputs(): def test_region_iterators(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 1, "n_rows": 1, "levels_back": 1} - primitives = (gp.Add,) - genome = gp.Genome( + primitives = (cgp.Add,) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -247,11 +247,11 @@ def test_region_iterators(): def test_check_levels_back_consistency(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 4, "n_rows": 3, "levels_back": None} - primitives = (gp.Add,) + primitives = (cgp.Add,) params["levels_back"] = 0 with pytest.raises(ValueError): - gp.Genome( + cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -262,7 +262,7 @@ def test_check_levels_back_consistency(): params["levels_back"] = params["n_columns"] + 1 with pytest.raises(ValueError): - gp.Genome( + cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -272,7 +272,7 @@ def test_check_levels_back_consistency(): ) params["levels_back"] = params["n_columns"] - 1 - gp.Genome( + cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -283,8 +283,8 @@ def test_check_levels_back_consistency(): def test_catch_invalid_allele_in_inactive_region(): - primitives = (gp.ConstantFloat,) - genome = gp.Genome(1, 1, 1, 1, 1, primitives) + primitives = (cgp.ConstantFloat,) + genome = cgp.Genome(1, 1, 1, 1, 1, primitives) # should raise error: ConstantFloat node has no inputs, but silent # input gene should still specify valid input @@ -300,8 +300,8 @@ def objective(ind): ind.fitness = ind.idx return ind - pop = gp.Population(**population_params, genome_params=genome_params) - ea = gp.ea.MuPlusLambda(**ea_params) + pop = cgp.Population(**population_params, genome_params=genome_params) + ea = cgp.ea.MuPlusLambda(**ea_params) pop._generate_random_parent_population() @@ -319,7 +319,7 @@ def objective(ind): def test_is_gene_in_input_region(rng_seed): - genome = gp.Genome(2, 1, 2, 1, None, (gp.Add,)) + genome = cgp.Genome(2, 1, 2, 1, None, (cgp.Add,)) rng = np.random.RandomState(rng_seed) genome.randomize(rng) @@ -328,7 +328,7 @@ def test_is_gene_in_input_region(rng_seed): def test_is_gene_in_hidden_region(rng_seed): - genome = gp.Genome(2, 1, 2, 1, None, (gp.Add,)) + genome = cgp.Genome(2, 1, 2, 1, None, (cgp.Add,)) rng = np.random.RandomState(rng_seed) genome.randomize(rng) @@ -339,7 +339,7 @@ def test_is_gene_in_hidden_region(rng_seed): def test_is_gene_in_output_region(rng_seed): - genome = gp.Genome(2, 1, 2, 1, None, (gp.Add,)) + genome = cgp.Genome(2, 1, 2, 1, None, (cgp.Add,)) rng = np.random.RandomState(rng_seed) genome.randomize(rng) @@ -349,7 +349,7 @@ def test_is_gene_in_output_region(rng_seed): def test_mutate_hidden_region(rng_seed): rng = np.random.RandomState(rng_seed) - genome = gp.Genome(1, 1, 3, 1, None, (gp.Add, gp.ConstantFloat)) + genome = cgp.Genome(1, 1, 3, 1, None, (cgp.Add, cgp.ConstantFloat)) dna = [ ID_INPUT_NODE, ID_NON_CODING_GENE, @@ -368,7 +368,7 @@ def test_mutate_hidden_region(rng_seed): ID_NON_CODING_GENE, ] genome.dna = list(dna) - active_regions = gp.CartesianGraph(genome).determine_active_regions() + active_regions = cgp.CartesianGraph(genome).determine_active_regions() # mutating any gene in inactive region returns True genome.dna = list(dna) @@ -398,7 +398,7 @@ def test_mutate_hidden_region(rng_seed): def test_mutate_output_region(rng_seed): rng = np.random.RandomState(rng_seed) - genome = gp.Genome(1, 1, 2, 1, None, (gp.Add,)) + genome = cgp.Genome(1, 1, 2, 1, None, (cgp.Add,)) genome.dna = [ ID_INPUT_NODE, ID_NON_CODING_GENE, diff --git a/test/test_hl_api.py b/test/test_hl_api.py index 01edd8b0..80f98b64 100644 --- a/test/test_hl_api.py +++ b/test/test_hl_api.py @@ -3,7 +3,7 @@ import pytest import time -import gp +import cgp def _objective_test_population(individual, rng_seed): @@ -37,9 +37,9 @@ def _test_population(population_params, genome_params, ea_params): np.random.seed(population_params["seed"]) - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) - ea = gp.ea.MuPlusLambda(**ea_params) + ea = cgp.ea.MuPlusLambda(**ea_params) history = {} history["max_fitness_per_generation"] = [] @@ -48,7 +48,7 @@ def recording_callback(pop): history["max_fitness_per_generation"].append(pop.champion.fitness) obj = functools.partial(_objective_test_population, rng_seed=population_params["seed"]) - gp.evolve(pop, obj, ea, **evolve_params, callback=recording_callback) + cgp.evolve(pop, obj, ea, **evolve_params, callback=recording_callback) assert pop.champion.fitness >= evolve_params["min_fitness"] @@ -85,8 +85,8 @@ def f0(x): def f1(x): return (x[0] * x[1]) - x[1] - y0 = gp.CartesianGraph(individual.genome[0]).to_func() - y1 = gp.CartesianGraph(individual.genome[1]).to_func() + y0 = cgp.CartesianGraph(individual.genome[0]).to_func() + y1 = cgp.CartesianGraph(individual.genome[1]).to_func() loss = 0 for _ in range(100): @@ -110,7 +110,7 @@ def f1(x): "n_columns": 4, "n_rows": 2, "levels_back": 2, - "primitives": (gp.Add, gp.Mul), + "primitives": (cgp.Add, cgp.Mul), }, { "n_inputs": 2, @@ -118,7 +118,7 @@ def f1(x): "n_columns": 2, "n_rows": 2, "levels_back": 2, - "primitives": (gp.Sub, gp.Mul), + "primitives": (cgp.Sub, cgp.Mul), }, ] @@ -126,11 +126,11 @@ def f1(x): np.random.seed(population_params["seed"]) - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) - ea = gp.ea.MuPlusLambda(**ea_params) + ea = cgp.ea.MuPlusLambda(**ea_params) - gp.evolve(pop, _objective, ea, **evolve_params) + cgp.evolve(pop, _objective, ea, **evolve_params) assert abs(pop.champion.fitness) == pytest.approx(0.0) @@ -168,12 +168,12 @@ def test_speedup_parallel_evolve(population_params, genome_params, ea_params): # Serial execution for n_processes in [1, 2, 4]: - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) - ea = gp.ea.MuPlusLambda(**ea_params, n_processes=n_processes) + ea = cgp.ea.MuPlusLambda(**ea_params, n_processes=n_processes) t0 = time.time() - gp.evolve(pop, _objective_speedup_parallel_evolve, ea, **evolve_params) + cgp.evolve(pop, _objective_speedup_parallel_evolve, ea, **evolve_params) T = time.time() - t0 if n_processes == 1: diff --git a/test/test_individual.py b/test/test_individual.py index 15c57e05..d1cb7596 100644 --- a/test/test_individual.py +++ b/test/test_individual.py @@ -3,15 +3,15 @@ import pytest import torch -import gp -from gp.individual import Individual -from gp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE +import cgp +from cgp.individual import Individual +from cgp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE def test_pickle_individual(): - primitives = (gp.Add,) - genome = gp.Genome(1, 1, 1, 1, 1, primitives) + primitives = (cgp.Add,) + genome = cgp.Genome(1, 1, 1, 1, 1, primitives) individual = Individual(None, genome) with open("individual.pkl", "wb") as f: @@ -20,8 +20,8 @@ def test_pickle_individual(): def test_individual_with_parameter_python(): - primitives = (gp.Add, gp.Parameter) - genome = gp.Genome(1, 1, 2, 1, 2, primitives) + primitives = (cgp.Add, cgp.Parameter) + genome = cgp.Genome(1, 1, 2, 1, 2, primitives) # f(x) = x + c genome.dna = [ ID_INPUT_NODE, @@ -58,8 +58,8 @@ def test_individual_with_parameter_python(): def test_individual_with_parameter_torch(): - primitives = (gp.Add, gp.Parameter) - genome = gp.Genome(1, 1, 2, 1, 2, primitives) + primitives = (cgp.Add, cgp.Parameter) + genome = cgp.Genome(1, 1, 2, 1, 2, primitives) # f(x) = x + c genome.dna = [ ID_INPUT_NODE, @@ -98,8 +98,8 @@ def test_individual_with_parameter_torch(): def test_individual_with_parameter_sympy(): - primitives = (gp.Add, gp.Parameter) - genome = gp.Genome(1, 1, 2, 1, 2, primitives) + primitives = (cgp.Add, cgp.Parameter) + genome = cgp.Genome(1, 1, 2, 1, 2, primitives) # f(x) = x + c genome.dna = [ ID_INPUT_NODE, @@ -135,8 +135,8 @@ def test_individual_with_parameter_sympy(): def test_to_and_from_torch_plus_backprop(): - primitives = (gp.Mul, gp.Parameter) - genome = gp.Genome(1, 1, 2, 2, 1, primitives) + primitives = (cgp.Mul, cgp.Parameter) + genome = cgp.Genome(1, 1, 2, 2, 1, primitives) # f(x) = c * x genome.dna = [ ID_INPUT_NODE, diff --git a/test/test_local_search.py b/test/test_local_search.py index 5ccfa955..9ebf1700 100644 --- a/test/test_local_search.py +++ b/test/test_local_search.py @@ -1,17 +1,17 @@ import pytest -import gp -from gp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE +import cgp +from cgp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE def test_gradient_based_step_towards_maximum(): torch = pytest.importorskip("torch") - primitives = (gp.Parameter,) - genome = gp.Genome(1, 1, 1, 1, 1, primitives) + primitives = (cgp.Parameter,) + genome = cgp.Genome(1, 1, 1, 1, 1, primitives) # f(x) = c genome.dna = [ID_INPUT_NODE, ID_NON_CODING_GENE, 0, 0, ID_OUTPUT_NODE, 1] - ind = gp.individual.Individual(None, genome) + ind = cgp.individual.Individual(None, genome) def objective(f): x_dummy = torch.zeros((1, 1), dtype=torch.double) # not used @@ -20,15 +20,15 @@ def objective(f): # test increase parameter value if too small ind.parameter_names_to_values[""] = 0.9 - gp.local_search.gradient_based(ind, objective, 0.05, 1) + cgp.local_search.gradient_based(ind, objective, 0.05, 1) assert ind.parameter_names_to_values[""] == pytest.approx(0.91) # test decrease parameter value if too large ind.parameter_names_to_values[""] = 1.1 - gp.local_search.gradient_based(ind, objective, 0.05, 1) + cgp.local_search.gradient_based(ind, objective, 0.05, 1) assert ind.parameter_names_to_values[""] == pytest.approx(1.09) # test no change of parameter value if at optimum ind.parameter_names_to_values[""] = 1.0 - gp.local_search.gradient_based(ind, objective, 0.05, 1) + cgp.local_search.gradient_based(ind, objective, 0.05, 1) assert ind.parameter_names_to_values[""] == pytest.approx(1.0) diff --git a/test/test_node.py b/test/test_node.py index 30b48271..9783a8da 100644 --- a/test/test_node.py +++ b/test/test_node.py @@ -1,14 +1,14 @@ import pytest -import gp -from gp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE +import cgp +from cgp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE def test_add(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 1, "n_rows": 1, "levels_back": 1} - primitives = (gp.Add,) - genome = gp.Genome( + primitives = (cgp.Add,) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -30,7 +30,7 @@ def test_add(): 2, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) x = [5.0, 1.5] y = graph(x) @@ -41,8 +41,8 @@ def test_add(): def test_sub(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 1, "n_rows": 1, "levels_back": 1} - primitives = (gp.Sub,) - genome = gp.Genome( + primitives = (cgp.Sub,) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -64,7 +64,7 @@ def test_sub(): 2, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) x = [5.0, 1.5] y = graph(x) @@ -75,8 +75,8 @@ def test_sub(): def test_mul(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 1, "n_rows": 1, "levels_back": 1} - primitives = (gp.Mul,) - genome = gp.Genome( + primitives = (cgp.Mul,) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -98,7 +98,7 @@ def test_mul(): 2, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) x = [5.0, 1.5] y = graph(x) @@ -109,8 +109,8 @@ def test_mul(): def test_div(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 1, "n_rows": 1, "levels_back": 1} - primitives = (gp.Div,) - genome = gp.Genome( + primitives = (cgp.Div,) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -132,7 +132,7 @@ def test_div(): 2, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) x = [5.0, 1.5] y = graph(x) @@ -143,8 +143,8 @@ def test_div(): def test_pow(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 1, "n_rows": 1, "levels_back": 1} - primitives = (gp.Pow,) - genome = gp.Genome( + primitives = (cgp.Pow,) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -166,7 +166,7 @@ def test_pow(): 2, ID_NON_CODING_GENE, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) x = [5.0, 1.5] y = graph(x) @@ -177,8 +177,8 @@ def test_pow(): def test_constant_float(): params = {"n_inputs": 2, "n_outputs": 1, "n_columns": 1, "n_rows": 1, "levels_back": 1} - primitives = (gp.ConstantFloat,) - genome = gp.Genome( + primitives = (cgp.ConstantFloat,) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -196,7 +196,7 @@ def test_constant_float(): ID_OUTPUT_NODE, 2, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) x = [None, None] y = graph(x) @@ -214,11 +214,11 @@ def test_inputs_are_cut_to_match_arity(): idx = 0 inputs = [1, 2, 3, 4] - node = gp.ConstantFloat(idx, inputs) + node = cgp.ConstantFloat(idx, inputs) assert node.inputs == [] - node = gp.node.OutputNode(idx, inputs) + node = cgp.node.OutputNode(idx, inputs) assert node.inputs == inputs[:1] - node = gp.Add(idx, inputs) + node = cgp.Add(idx, inputs) assert node.inputs == inputs[:2] diff --git a/test/test_node_factories.py b/test/test_node_factories.py index 8e2f13c2..e8514d59 100644 --- a/test/test_node_factories.py +++ b/test/test_node_factories.py @@ -1,7 +1,7 @@ import pytest -import gp -from gp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE +import cgp +from cgp.genome import ID_INPUT_NODE, ID_OUTPUT_NODE, ID_NON_CODING_GENE def test_constant_float(): @@ -9,8 +9,8 @@ def test_constant_float(): val = 1.678 - primitives = (gp.node_factories.ConstantFloatFactory(val),) - genome = gp.Genome( + primitives = (cgp.node_factories.ConstantFloatFactory(val),) + genome = cgp.Genome( params["n_inputs"], params["n_outputs"], params["n_columns"], @@ -28,7 +28,7 @@ def test_constant_float(): ID_OUTPUT_NODE, 2, ] - graph = gp.CartesianGraph(genome) + graph = cgp.CartesianGraph(genome) x = [None, None] y = graph(x) @@ -37,8 +37,8 @@ def test_constant_float(): # make sure different classes are created for multiple calls to the class # factory - prim_0 = gp.node_factories.ConstantFloatFactory(val)(0, [None]) - prim_1 = gp.node_factories.ConstantFloatFactory(val)(0, [None]) + prim_0 = cgp.node_factories.ConstantFloatFactory(val)(0, [None]) + prim_1 = cgp.node_factories.ConstantFloatFactory(val)(0, [None]) assert prim_0 is not prim_1 diff --git a/test/test_population.py b/test/test_population.py index f82f63d0..cb12895d 100644 --- a/test/test_population.py +++ b/test/test_population.py @@ -1,22 +1,23 @@ import copy -import gp import pytest import numpy as np +import cgp + def test_assert_mutation_rate(rng_seed, genome_params, mutation_rate): with pytest.raises(ValueError): - gp.Population(5, -0.1, rng_seed, genome_params) + cgp.Population(5, -0.1, rng_seed, genome_params) with pytest.raises(ValueError): - gp.Population(5, 1.1, rng_seed, genome_params) + cgp.Population(5, 1.1, rng_seed, genome_params) # assert that no error is thrown for a suitable mutation rate - gp.Population(5, mutation_rate, rng_seed, genome_params) + cgp.Population(5, mutation_rate, rng_seed, genome_params) def test_init_random_parent_population(population_params, genome_params): - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) assert len(pop.parents) == population_params["n_parents"] @@ -57,7 +58,7 @@ def test_crossover_two_offspring(population_simple_fitness): def test_mutate(population_params, genome_params): population_params["mutation_rate"] = 0.999999 - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) offspring = pop.parents offspring_original = copy.deepcopy(offspring) @@ -68,7 +69,7 @@ def test_mutate(population_params, genome_params): def test_fitness_parents(population_params, genome_params): - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) fitness_values = np.random.rand(population_params["n_parents"]) for fitness, parent in zip(fitness_values, pop.parents): parent.fitness = fitness @@ -80,7 +81,7 @@ def test_pop_uses_own_rng(population_params, genome_params, rng_seed): """Test independence of Population on global numpy rng. """ - pop = gp.Population(**population_params, genome_params=genome_params) + pop = cgp.Population(**population_params, genome_params=genome_params) np.random.seed(rng_seed) diff --git a/test/test_primitives.py b/test/test_primitives.py index 434167ae..69c2a514 100644 --- a/test/test_primitives.py +++ b/test/test_primitives.py @@ -1,18 +1,18 @@ import dataclasses import pytest -import gp -from gp.primitives import Primitives +import cgp +from cgp.primitives import Primitives def test_init_with_list_raises(): with pytest.raises(TypeError): - Primitives([gp.Add]) + Primitives([cgp.Add]) def test_init_with_instance_raises(): with pytest.raises(TypeError): - Primitives((gp.Add(0, []),)) + Primitives((cgp.Add(0, []),)) def test_init_with_wrong_class_raises(): @@ -21,19 +21,19 @@ def test_init_with_wrong_class_raises(): def test_immutable_primitives(): - primitives = Primitives((gp.Add, gp.Sub)) + primitives = Primitives((cgp.Add, cgp.Sub)) with pytest.raises(TypeError): - primitives[0] = gp.Add + primitives[0] = cgp.Add with pytest.raises(TypeError): - primitives._primitives[0] = gp.Add + primitives._primitives[0] = cgp.Add with pytest.raises(dataclasses.FrozenInstanceError): - primitives.primitives = (gp.Add,) + primitives.primitives = (cgp.Add,) def test_max_arity(): - plain_primitives = (gp.Add, gp.Sub, gp.ConstantFloat) + plain_primitives = (cgp.Add, cgp.Sub, cgp.ConstantFloat) primitives = Primitives(plain_primitives) arity = 0 @@ -53,7 +53,7 @@ def test_check_for_correct_class(): def test_function_indices_remain_fixed_in_list_conversion(): - plain_primitives = (gp.Add, gp.Sub, gp.Mul, gp.Div) + plain_primitives = (cgp.Add, cgp.Sub, cgp.Mul, cgp.Div) primitives = Primitives(plain_primitives) for k in range(10): diff --git a/test/test_utils.py b/test/test_utils.py index 2403bea5..ec221591 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -5,16 +5,16 @@ import tempfile import time -import gp +import cgp -@gp.utils.disk_cache(tempfile.mkstemp()[1]) +@cgp.utils.disk_cache(tempfile.mkstemp()[1]) def _cache_decorator_objective_single_process(s, sleep_time): time.sleep(sleep_time) # simulate long execution time return s -@gp.utils.disk_cache(tempfile.mkstemp()[1]) +@cgp.utils.disk_cache(tempfile.mkstemp()[1]) def _cache_decorator_objective_two_processes(s, sleep_time): time.sleep(sleep_time) # simulate long execution time return s @@ -62,7 +62,7 @@ def test_cache_decorator_consistency(): cache_fn = tempfile.mkstemp()[1] x = 2 - @gp.utils.disk_cache(cache_fn) + @cgp.utils.disk_cache(cache_fn) def objective_f(x): return x @@ -73,13 +73,13 @@ def objective_f(x): # filename should raise an error with pytest.raises(RuntimeError): - @gp.utils.disk_cache(cache_fn) + @cgp.utils.disk_cache(cache_fn) def objective_g(x): return x ** 2 # decorating a different function with identical output using the # same filename should NOT raise an error - @gp.utils.disk_cache(cache_fn) + @cgp.utils.disk_cache(cache_fn) def objective_h(x): return x @@ -91,8 +91,8 @@ def objective_history_recording(individual): def test_history_recording(population_params, genome_params, ea_params): - pop = gp.Population(**population_params, genome_params=genome_params) - ea = gp.ea.MuPlusLambda(**ea_params) + pop = cgp.Population(**population_params, genome_params=genome_params) + ea = cgp.ea.MuPlusLambda(**ea_params) evolve_params = { "max_generations": 2, @@ -111,7 +111,7 @@ def recording_callback(pop): history["fitness_champion"][pop.generation] = pop.champion.fitness history["champion"].append(pop.champion) - gp.evolve( + cgp.evolve( pop, objective_history_recording, ea, **evolve_params, callback=recording_callback, ) @@ -123,14 +123,14 @@ def recording_callback(pop): def test_primitives_from_class_names(): primitives_str = ["Add", "Sub", "Mul"] - primitives = gp.utils.primitives_from_class_names(primitives_str) - assert issubclass(primitives[0], gp.Add) - assert issubclass(primitives[1], gp.Sub) - assert issubclass(primitives[2], gp.Mul) + primitives = cgp.utils.primitives_from_class_names(primitives_str) + assert issubclass(primitives[0], cgp.Add) + assert issubclass(primitives[1], cgp.Sub) + assert issubclass(primitives[2], cgp.Mul) # make sure custom classes are registered as well - class MyCustomNodeClass(gp.node.Node): + class MyCustomNodeClass(cgp.node.Node): pass - primitives = gp.utils.primitives_from_class_names(["MyCustomNodeClass"]) + primitives = cgp.utils.primitives_from_class_names(["MyCustomNodeClass"]) assert issubclass(primitives[0], MyCustomNodeClass)