pyneat.genome module

This module defines the genome encoding used by NEAT.

class pyneat.genome.ConnectionGene(key, node_in, node_out, weight, expressed)[source]

Bases: object

Defines a connection gene used in the genome encoding.

key

The innovation key for this gene.

Type:int
node_in

The key of the node this connection from.

Type:int
node_out

The key of the node this connection is to.

Type:int
weight

The connection weight.

Type:float
expressed

True if the connection is expressed (enabled) in the phenotype, False otherwise.

Type:bool
copy()[source]

Create a copy of the connection gene.

Returns:A copy of itself.
Return type:ConnectionGene
class pyneat.genome.Genome(key, config, innovation_store)[source]

Bases: object

Defines a genome used to encode a neural network.

key

A unique identifier for the genome.

Type:int
config

The genome configuration settings.

Type:GenomeConfig
fitness

The fitness of the genome.

Type:float
nodes

A dictionary of node key (int), node gene pairs.

Type:dict
connections

A dictionary of connection gene key, connection gene pairs.

Type:dict
inputs

The node keys of input nodes.

Type::list:`int`
outputs

The node keys of output nodes.

Type::list:`int`
biases

The node keys of bias nodes.

Type::list:`int`
innovation_store

The global innovation store used for tracking new structural mutations.

Type:InnovationStore
add_bias_node(num)[source]

Add a new bias node.

Parameters:num (int) – A number that can uniquely identify bias nodes in the innovation store.
add_connection(node_in, node_out, weight, expressed=True)[source]

Add a connection between two nodes.

Parameters:
  • node_in (int) – The key of the node that leads into the new connection.
  • node_out (int) – The key of the node that the the new connection leads into.
  • weight (float) – The weight of the connection. Must be a value between [0, 1].
  • expressed (bool) – True if the connection should be expressed in the phenotype, False otherwise.
add_node(node_in, node_out, node_type)[source]

Add a new node positioned between two other nodes. Input and output nodes are positioned between non-existent nodes.

Parameters:
  • node_in (int) – The key of the node that precedes this new node.
  • node_out (int) – The key of the node that succeeds this new node.
  • node_type (NodeType) – The type of node to be added.
Returns:

The key of the new node

Return type:

int

configure_crossover(parent1, parent2, average)[source]

Performs crossover between two genomes.

If the two genomes have equal fitness then the joint and excess genes are inherited from the smaller genome.

Parameters:
  • parent1 (Genome) – The first parent.
  • parent2 (Genome) – The second parent.
  • average (bool) – Whether or not to average the weights of mutual connections or choose at random from one of the parents.
configure_new()[source]

Configure a new genome based on the given configuration.

The initial inputs and outputs for input and output nodes are specified as negatives so that matching innovation keys are generated for corresponding input and output nodes between genomes. Inputs nodes use odd negative numbers, and output nodes use even negative numbers.

copy()[source]

Create a copy of the genome.

Note: Copies share the same config and innovation store.

Returns:
A copy of itself, but with the same config and innovation
store.
Return type:Genome
distance(other)[source]

Computes the compatibility (genetic) distance between two genomes.

This is used for deciding how to speciate the population. Distance is a function of the number of disjoint and excess genes, as well as the weight/bias differences of matching genes.

Update (13.04.20): Distance is measured using the original compatibility distance measure defined by Stanley & Miikkulainen (2002).

Parameters:other (Genome) – The other genome to compare itself to.
Returns:The genetic distance between itself and the other genome.
Return type:float
mutate()[source]

Mutate the genome.

Mutates the genome according to the mutation parameter values specified in the genome configuration.

As per the original implementation of NEAT:

  • If any structural mutations are performed, weight and bias mutations will not be performed.
  • If an add node mutation is performed, an add connection mutation will not also be performed.
mutate_add_connection()[source]

Performs an ‘add connection’ structural mutation.

A single connection with a random weight is added between two previously unconnected nodes.

Returns:True if a connection was added, False otherwise.
Return type:bool
mutate_add_node()[source]

Performs an ‘add node’ structural mutation.

An existing connection is split and the new node is placed where the old connection used to be. The old connection is disabled and two new connection genes are added. The new connection leading into the new node receives a weight of 1.0 and the connection leading out of the new node receives the old connection weight.

Connections from bias nodes and non-expressed nodes are not split.

Returns:True is a node was added, False otherwise.
Return type:bool
mutate_weights()[source]

Mutates (perturbs) or replaces each connection weight in the genome.

Each weight is either replaced (with some probability, specified in the genome config) or perturbed.

Replaced weights and perturbations are drawn from a uniform distribution with range [-weight_perturb_power, weight_perturb_power).

classmethod parse_config(param_dict)[source]

Takes a dictionary of configuration items, returns an object that will later be passed to the write_config method.

Parameters:param_dict (dict) – A dictionary of configuration parameter values.
Returns:The genome configuration.
Return type:GenomeConfig
size()[source]

Returns a measure of genome complexity.

Returns:
A measure of the complexity of the genome given by
(number of nodes, number of enabled connections)
Return type:tuple
classmethod write_config(filename, config)[source]

Takes a file-like object and the configuration object created by parse_config. This method should write the configuration item definitions to the given file.

Parameters:
  • filename (str) – The name of the file to write the genome configuration to.
  • config (GenomeConfig) – The genome configuration to save.
class pyneat.genome.GenomeConfig(params)[source]

Bases: object

Sets up and holds configuration information for the Genome class.

Config Parameters:

num_inputs (int): The number of inputs each network should have.

num_outputs (int): The number of outputs each network should have.

num_biases (int): The number of bias nodes the network should have.

initial_conn_prob (float): The initial connection probability of each potential connection between inputs and outputs. 0.0 = no connections, i.e. all inputs are disconnected from the outputs. 1.0 = fully connected, i.e. all inputs are connected to all outputs.

activation_func (str): The name of the activation function to be used by hidden and output nodes. Must be present in the set of possible activation functions.

compatibility_disjoint_coefficient (float): The disjoint and excess coefficient to be used when calculating genome distance.

compatibility_weight_coefficient (float): The weight and bias coefficient to be used when calculation genome distance.

normalise_gene_dist (bool): Whether or not normalise the gene dist (for genetic distance calculations) for large genomes.

feed_forward (bool): False if recurrent connections are allowed, True otherwise.

conn_add_prob (float): The probability of adding a new connection when performing mutations.

node_add_prob (float): The probability of adding a new node when performing mutations.

weight_mutate_prob (float): The probability of mutating the connection weights of a genome when performing mutations.

weight_replace_prob (float): The probability of replacing, instead of perturbing, a connection weight when performing weight mutations.

weight_init_power (float): Sets the range of possible values for weight replacements and new weight initialisations.

weight_perturb_power (float): Sets the range of possible values for weight perturbations.

weight_min_value (float): Sets the minimum allowed value for connection weights.

weight_max_value (float): Sets the maximum allowed value for connection weights.

gene_disable_prob (float): The probability of disabling a gene in the child that is disabled in either of the parents when performing crossover.

save(filename)[source]

Save the genome configuration.

Parameters:filename (str) – The filename to write the configuration to.
class pyneat.genome.NodeGene(key, type, activation)[source]

Bases: object

Defines a node gene used in the genome encoding.

key

The innovation key (also the node key) for this gene.

Type:int
type

The type of the node (either input, output or hidden).

Type:NodeType
activation

The node activation function. Note that input and bias nodes should not have an activation function (i.e. it is the identity function).

Type:function
copy()[source]

Create a copy of the node gene.

Returns:A copy of itself.
Return type:NodeGene
class pyneat.genome.NodeType[source]

Bases: enum.Enum

Define the types for nodes in the network.

BIAS = 3
HIDDEN = 1
INPUT = 0
OUTPUT = 2