CGP Population structure

Population

class cgp.Population(n_parents: int, seed: int, genome_params: Union[dict, List[dict]], individual_init: Optional[Callable[[cgp.individual.IndividualBase], cgp.individual.IndividualBase]] = None)[source]

A population of individuals.

property champion

Return parent with the highest fitness.

fitness_parents() → List[Union[None, float]][source]

Return fitness for all parents of the population.

List[float]

List of fitness values for all parents.

reorder_genome() → None[source]

Reorders the genome for all parents in the population

None

Individual

class cgp.individual.IndividualSingleGenome(genome: cgp.genome.Genome)[source]

An individual representing a particular computational graph.

class cgp.individual.IndividualMultiGenome(genome: List[cgp.genome.Genome])[source]

An individual with multiple genomes each representing a particular computational graph.

Cartesian Graph

class cgp.CartesianGraph(genome: Genome)[source]

Class representing a particular Cartesian graph defined by a Genome.

determine_active_regions() → List[int][source]

Determine the active regions in the computational graph.

List[int]

List of ids of the active nodes.

pretty_str() → str[source]

Print a pretty representation of the Cartesian graph.

print_active_nodes() → str[source]

Print a representation of all active nodes in the graph.

to_func() → Callable[[List[float]], List[float]][source]

Compile the function(s) represented by the graph.

Generates a definition of the function in Python code and executes the function definition to create a Callable.

Callable

Callable executing the function(s) represented by the graph.

to_numpy() → Callable[[numpy.ndarray], numpy.ndarray][source]

Compile the function(s) represented by the graph to NumPy expression(s).

Generates a definition of the function in Python code and executes the function definition to create a Callable accepting NumPy arrays.

Callable

Callable executing the function(s) represented by the graph.

to_sympy(simplify: Optional[bool] = True) → List[sympy.core.expr.Expr][source]

Compile the function(s) represented by the graph to a SymPy expression.

Generates one SymPy expression for each output node.

simplifyboolean, optional

Whether to simplify the expression using SymPy’s simplify() method. Defaults to True.

List[sympy.core.expr.Expr]

List of SymPy expressions.

to_torch() → torch.nn.modules.module.Module[source]

Compile the function(s) represented by the graph to a Torch class.

Generates a definition of the Torch class in Python code and executes it to create an instance of the class.

torch.nn.Module

Instance of the PyTorch class.

Genome

class cgp.Genome(n_inputs: int, n_outputs: int, n_columns: int, n_rows: int, primitives: Tuple[Type[cgp.node.Node], …], levels_back: Optional[int] = None)[source]

Genome class for individuals.

clone() → cgp.genome.Genome[source]

Clone the genome.

Genome

determine_permissible_values() → List[numpy.ndarray][source]

Determine permissible values for every gene.

None

permissible_values

List[numpy.ndarray]: List of permissible values for every gene

mutate(mutation_rate: float, rng: numpy.random.mtrand.RandomState)[source]

Mutate the genome.

mutation_ratefloat

Probability of a gene to be mutated, between 0 (excluded) and 1 (included).

rngnumpy.random.RandomState

Random number generator instance to use for mutating.

bool

True if only inactive regions of the genome were mutated, False otherwise.

randomize(rng: numpy.random.mtrand.RandomState) → None[source]

Randomize the genome.

rngnumpy.RandomState

Random number generator instance to use for randomizing.

None

reorder(rng: numpy.random.mtrand.RandomState) → None[source]

Reorder the genome

Shuffle node ordering of internal (hidden) nodes in genome without changing the phenotype. (Goldman 2015, DOI: 10.1109/TEVC.2014.2324539)

During reordering, inactive genes, e.g., address genes of nodes with arity zero, are not taken into account and can hence have invalid values after reordering. These invalid values are replaced by random values for the respective gene after reordering.

rngnumpy.RandomState

Random number generator instance.

None

update_parameters_from_torch_class(torch_cls: torch.nn.modules.module.Module) → bool[source]

Update values stored in Parameter nodes of graph from parameters of a given Torch instance. Can be used to import new values from a Torch class after they have been altered, e.g., by local search.

torch_clstorch.nn.module

Instance of a torch class.

bool

Whether any parameter was updated