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[Optional[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]][source]

Create a Python callable implementing the function described by this graph.

The returned callable expects as many arguments as the number of inputs defined in the genome. The function returns a tuple with length equal to the number of outputs defined in the genome. For convenience, if only a single output is defined the function will not return a tuple but only its first element.

Callable

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

Create a NumPy-array-compatible Python callable implementing the function described by this graph.

The returned callable expects as many arguments as the number of inputs defined in the genome. Every argument needs to be a NumPy array of equal length. The function returns a tuple with length equal to the number of outputs defined in the genome. Each element will have the same length as the input arrays. For convenience, if only a single output is defined the function will not return a tuple but only its first element.

Callable

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

Create SymPy expression(s) representing the function(s) described by this graph.

Returns a list of SymPy expressions, one for each output node. For convenience, if only one output node is defined, it directly returns its expression.

simplifyboolean, optional

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

List[sympy.core.expr.Expr] or sympy.core.expr.Expr

List of SymPy expressions or single expression.

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

Create a Torch nn.Module instance implementing the function defined by this graph.

The generated instance will have a forward method accepting Torch tensor of dimension (<batch size>, n_inputs) and returning a tensor of dimension (<batch_size>, n_outputs).

torch.nn.Module

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