Genome¶
Main Object:¶
- class abrain.Genome(key=None)[source]¶
Genome class encoding a CPPN either for ES-HyperNEAT or for direct use.
A simple collection of Node/Link. Can only be created via
random()init,crossover()orcopy()(with the help of the nestedDatastructure)- class Data(seed: int, cdata: dict, labels: str | Collection[str] | None, genome_ids: bool = True, innovations: bool = True, key=None)[source]¶
Long-lived data structure encapsulating the various elements needed throughout evolution
- id_manager: Innovations | IDManager¶
Either an innovations database (allowing crossover but slightly more expensive) or a monotonic index-based allocator (for mutations only)
- gid_manager: GIDManager | None¶
(Optional) Manager for genome identifiers and lineage
- static create_for_generic_cppn(inputs: int, outputs: int | Collection[str], labels: str | Collection[str] | None = None, seed: int | None = None, with_input_bias: bool = True, with_innovations: bool = True, with_lineage: bool = True)[source]¶
Create a structure handling CPPNs for generic use
- Parameters:
inputs – Number of inputs for the CPPN
outputs – Number of outputs for the CPPN
labels – (Optional) list of i/o labels for pretty-printing
with_input_bias – Whether to use an input bias
seed – Seed for the random number generator
with_innovations – Whether to use NEAT historical markings
with_lineage – Whether to store lineage information
- static create_for_eshn_cppn(dimension: int, with_input_bias: bool | None = True, with_input_length: bool | None = True, with_leo: bool | None = True, with_output_bias: bool | None = True, seed: int | None = None, with_innovations: bool = True, with_lineage: bool = True)[source]¶
Create a structure handling CPPNs for use with ES-HyperNEAT
- Parameters:
dimension – The substrate dimension (2- or 3-D)
with_input_bias – Whether to use an input bias
with_input_length – Whether to directly provide the distance between points
with_leo – Whether to use a Level of Expression Output
with_output_bias – Whether to use the CPPN to generate per-neuron biases
seed – Seed for the random number generator
with_innovations – Whether to use NEAT historical markings
with_lineage – Whether to store lineage information
- classmethod random(data: Data) Genome[source]¶
Create a random CPPN with boolean initialization
- Parameters:
data – Evolution-wide shared genomic data
- Returns:
A random CPPN genome
- mutate(data: Data, n=1) None[source]¶
Mutate (in-place) this genome :param data: Evolution-wide shared genomic data :param n: Number of mutations to perform
- mutated(data: Data, n=1) Genome[source]¶
Return a mutated (copied) version of this genome :param data: Evolution-wide shared genomic data :param n: Number of mutations to perform
- static crossover(lhs: Genome, rhs: Genome, data: Data, bias: int | str = 'length') Genome[source]¶
Create a new genome from two parents using historical markings
- Bias specifies which parent to take disjoint and excess genes from:
0: lhs
1: rhs
“length”: from the smallest (or random if both have the same size)
- Parameters:
lhs – A parent
rhs – Another parent
data – Evolution-wide shared genomic data
bias – Which parent to take disjoint and excess genes (see description)
- Returns:
The child
- static distance(lhs: Genome, rhs: Genome, weights=None) float[source]¶
Compute the genetic distance between lhs and rhs
- Parameters:
lhs – The first genome
rhs – The first genome
weights – How to weight the different terms:
Node excess
Node disjoint
Node matched
Link excess
Link disjoint
Link matched
- Returns:
the weighted genetic distance
- update_lineage(data: Data, parents: List[Genome])[source]¶
Update lineage fields
- Parameters:
data – Evolution-wide shared genomic data
parents – list (potentially empty) of this genome’s parents
- static from_json(data: Dict[Any, Any]) Genome[source]¶
Recreate a Genome from a string json representation
- static from_dot(path: str, rng: Random) Genome[source]¶
Produce a Genome by parsing a simplified graph description
Warning
Unimplemented
- Parameters:
path – the file to load
rng – random pick unspecified functions
- Returns:
The user-specified CPPN
- to_dot(data: Data, path: str, ext: str = 'pdf', title: str | None = None, debug: str | Iterable | None = None) str[source]¶
Produce a graphical representation of this genome
Note
Missing functions images in nodes and/or lots of warning message are likely caused by misplaced image files. In doubt perform a full reinstall
- Parameters:
data – Genome’s shared data (for labels)
path – The path to write to
ext – The rendering format to use
title – Optional title for the graph (e.g. for generational info)
debug –
Print more information on the graph. Special values (either as a single string or a list):
’depth’ will display every nodes’ depth
’links’ will display every links’ id
’keepdot’ will keep the intermediate dot file
’all’ all of the above
- Raises:
OSError – if the dot program is not available (not installed, on the path and executable)
Helper types:¶
- class abrain._cpp.genotype.Innovations¶
C++ database for innovation markings
- NOT_FOUND = -1¶
- copy(self: abrain._cpp.genotype.Innovations) abrain._cpp.genotype.Innovations¶
Return a perfect (deep)copy of this innovations database
- empty(self: abrain._cpp.genotype.Innovations) bool¶
Whether any historical markings have been generated yet
- static from_json(j: dict) abrain._cpp.genotype.Innovations¶
Convert from the json-compliant Python dictionary j
- get_link_id(self: abrain._cpp.genotype.Innovations, src: int, dst: int) int¶
Attempt to generate a new link innovation marking for provided key
- Parameters:
src – ID of the link’s source
dst – ID of the link’s destination
- Returns:
The newly created, or existing corresponding link innovation marking
- get_node_id(self: abrain._cpp.genotype.Innovations, src: int, dst: int) int¶
Attempt to generate a new node innovation marking for provided key
- Parameters:
src – ID of the link’s source
dst – ID of the link’s destination
- Returns:
The newly created, or existing corresponding node innovation marking
- initialize(self: abrain._cpp.genotype.Innovations, nextNodeID: int) None¶
Reset the database.
Next node id is set to the provided value and the next link id is set to 0. All known nodes and links mappings are cleared.
- Parameters:
nextNodeID – The next node id to give
- link_id(self: abrain._cpp.genotype.Innovations, src: int, dst: int) int¶
Retrieve link innovation marking for provided key
- Parameters:
src – ID of the link’s source
dst – ID of the link’s destination
- Returns:
The corresponding link innovation marking or
Innovations.NOT_FOUNDif not found
- new_link_id(self: abrain._cpp.genotype.Innovations, src: int, dst: int) int¶
Force generation of a new link innovation marking for provided key
- Parameters:
src – ID of the link’s source
dst – ID of the link’s destination
- Returns:
The newly created link innovation marking
- new_node_id(self: abrain._cpp.genotype.Innovations, src: int, dst: int) int¶
Force generation of a new node innovation marking for provided key
- Parameters:
src – ID of the link’s source
dst – ID of the link’s destination
- Returns:
The newly created node innovation marking
- next_link_id(self: abrain._cpp.genotype.Innovations) int¶
Historical marking of the next new link
- next_node_id(self: abrain._cpp.genotype.Innovations) int¶
Historical marking of the next new node
- node_id(self: abrain._cpp.genotype.Innovations, src: int, dst: int) int¶
Retrieve node innovation marking for provided key
- The link refers to the link that was broken up to create this new
node
- Parameters:
src – ID of the link’s source
dst – ID of the link’s destination
- Returns:
The corresponding node innovation marking or
Innovations.NOT_FOUNDif not found
- size(self: abrain._cpp.genotype.Innovations) tuple¶
The number of node and link historical markings currently registered
- to_json(self: abrain._cpp.genotype.Innovations) dict¶
Convert to a json-compliant Python dictionary
- class abrain.core.genome.IDManager[source]¶
Very simple monotonic ID generator for nodes and links
- initialize(next_node_id: int)[source]¶
Reset the manager to next_node_id for the nodes and 0 for the links
- get_node_id(*_)[source]¶
Request a new node id.
Same signature as
get_node_id()but does not use the arguments and, instead, provides monotonously increasing ids
- get_link_id(*_)[source]¶
Request a new link id.
Same signature as
get_link_id()but does not use the arguments and, instead, provides monotonously increasing ids
- link_id = None¶
- node_id = None¶
- new_node_id(*_)¶
Request a new node id.
Same signature as
get_node_id()but does not use the arguments and, instead, provides monotonously increasing ids
- new_link_id(*_)¶
Request a new link id.
Same signature as
get_link_id()but does not use the arguments and, instead, provides monotonously increasing ids
Underlying types:¶
- class abrain._cpp.genotype.CPPNData¶
C++ supporting type for genomic data
- class Link¶
From-to relationship between two computational node
- dst¶
ID of the destination node
- id¶
Historical marking
- src¶
ID of the source node
- weight¶
Connection weight
- class Links¶
Collection of Links
- append(self: abrain._cpp.genotype.CPPNData.Links, x: abrain._cpp.genotype.CPPNData.Link) None¶
Add an item to the end of the list
- clear(self: abrain._cpp.genotype.CPPNData.Links) None¶
Clear the contents
- extend(\*args, **kwargs)¶
Overloaded function.
extend(self: abrain._cpp.genotype.CPPNData.Links, L: abrain._cpp.genotype.CPPNData.Links) -> None
Extend the list by appending all the items in the given list
extend(self: abrain._cpp.genotype.CPPNData.Links, L: Iterable) -> None
Extend the list by appending all the items in the given list
- insert(self: abrain._cpp.genotype.CPPNData.Links, i: int, x: abrain._cpp.genotype.CPPNData.Link) None¶
Insert an item at a given position.
- pop(\*args, **kwargs)¶
Overloaded function.
pop(self: abrain._cpp.genotype.CPPNData.Links) -> abrain._cpp.genotype.CPPNData.Link
Remove and return the last item
pop(self: abrain._cpp.genotype.CPPNData.Links, i: int) -> abrain._cpp.genotype.CPPNData.Link
Remove and return the item at index
i
- class Nodes¶
Collection of Nodes
- append(self: abrain._cpp.genotype.CPPNData.Nodes, x: abrain._cpp.genotype.CPPNData.Node) None¶
Add an item to the end of the list
- clear(self: abrain._cpp.genotype.CPPNData.Nodes) None¶
Clear the contents
- extend(\*args, **kwargs)¶
Overloaded function.
extend(self: abrain._cpp.genotype.CPPNData.Nodes, L: abrain._cpp.genotype.CPPNData.Nodes) -> None
Extend the list by appending all the items in the given list
extend(self: abrain._cpp.genotype.CPPNData.Nodes, L: Iterable) -> None
Extend the list by appending all the items in the given list
- insert(self: abrain._cpp.genotype.CPPNData.Nodes, i: int, x: abrain._cpp.genotype.CPPNData.Node) None¶
Insert an item at a given position.
- pop(\*args, **kwargs)¶
Overloaded function.
pop(self: abrain._cpp.genotype.CPPNData.Nodes) -> abrain._cpp.genotype.CPPNData.Node
Remove and return the last item
pop(self: abrain._cpp.genotype.CPPNData.Nodes, i: int) -> abrain._cpp.genotype.CPPNData.Node
Remove and return the item at index
i
- static from_json(j: dict) abrain._cpp.genotype.CPPNData¶
Convert from the json-compliant Python dictionary j
- to_json(self: abrain._cpp.genotype.CPPNData) dict¶
Convert to a json-compliant Python dictionary
- bias¶
Whether to use an input bias
- inputs¶
Number of inputs
- labels¶
(optional) label for the inputs/outputs
- links¶
The collection of inter-node relationships
- nodes¶
The collection of computing nodes
- outputs¶
Number of outputs
Contains the classes and functions related to abrain’s genotype