"""Handles genomes (individuals in the population)."""
from __future__ import division, print_function
from itertools import count
from pprint import saferepr
from random import choice, random, shuffle
import sys
import warnings
from neat.activations import ActivationFunctionSet
from neat.aggregations import AggregationFunctionSet
from neat.config import ConfigParameter, write_pretty_params
from neat.genes import DefaultConnectionGene, DefaultNodeGene
from neat.graphs import creates_cycle
from neat.multiparameter import MultiParameterSet
from neat.six_util import iteritems, iterkeys
[docs]class DefaultGenomeConfig(object):
"""Sets up and holds configuration information for the DefaultGenome class."""
allowed_connectivity = ['unconnected', 'fs_neat_nohidden', 'fs_neat', 'fs_neat_hidden',
'full_nodirect', 'full', 'full_direct',
'partial_nodirect', 'partial', 'partial_direct']
def __init__(self, params):
# Enables functions with evolved parameters
self.multiparameterset = MultiParameterSet('activation', 'aggregation')
# Create full set of available activation functions.
self.activation_defs = ActivationFunctionSet(self.multiparameterset)
# ditto for aggregation functions - name difference for backward compatibility
self.aggregation_function_defs = AggregationFunctionSet(self.multiparameterset)
self.aggregation_defs = self.aggregation_function_defs
self._params = [ConfigParameter('num_inputs', int),
ConfigParameter('num_outputs', int),
ConfigParameter('num_hidden', int),
ConfigParameter('feed_forward', bool),
ConfigParameter('compatibility_disjoint_coefficient', float),
ConfigParameter('compatibility_weight_coefficient', float),
ConfigParameter('conn_add_prob', float),
ConfigParameter('conn_delete_prob', float),
ConfigParameter('node_add_prob', float),
ConfigParameter('node_delete_prob', float),
ConfigParameter('single_structural_mutation', bool, False),
ConfigParameter('structural_mutation_surer',
str, 'default', default_ok=True),
ConfigParameter('initial_connection', str, 'unconnected'),
ConfigParameter('fitter_enabled_transfer', bool, False),
ConfigParameter('identical_mutations_keys', int, 0)]
# Gather configuration data from the gene classes.
self.node_gene_type = params['node_gene_type']
self._params += self.node_gene_type.get_config_params()
self.connection_gene_type = params['connection_gene_type']
self._params += self.connection_gene_type.get_config_params()
# Use the configuration data to interpret the supplied parameters.
for p in self._params:
setattr(self, p.name, p.interpret(params))
# By convention, input pins have negative keys, and the output
# pins have keys 0,1,...
self.input_keys = [-i - 1 for i in range(self.num_inputs)]
self.output_keys = [i for i in range(self.num_outputs)]
if self.identical_mutations_keys:
desired_min = max(5,(max(self.num_outputs,self.num_inputs)-self.num_hidden))
if self.identical_mutations_keys < desired_min:
warnings.warn(
"identical_mutations_keys {0:n} too low - raising to {1:n}".format(
self.identical_mutations_keys, desired_min))
self.identical_mutations_keys = desired_min
self.conn_to_node_dict = {}
self.node_to_conn_dict = {}
else:
self.conn_to_node_dict = None
self.node_to_conn_dict = None
self.connection_fraction = None
# Verify that initial connection type is valid.
# pylint: disable=access-member-before-definition
if 'partial' in self.initial_connection:
c, p = self.initial_connection.split()
self.initial_connection = c
self.connection_fraction = float(p)
if not (0 <= self.connection_fraction <= 1):
raise RuntimeError(
"'partial' connection value must be between 0.0 and 1.0, inclusive.")
assert self.initial_connection in self.allowed_connectivity
# Verify structural_mutation_surer is valid.
# pylint: disable=access-member-before-definition
if self.structural_mutation_surer.lower() in ['1','yes','true','on']:
self.structural_mutation_surer = 'true'
elif self.structural_mutation_surer.lower() in ['0','no','false','off']:
self.structural_mutation_surer = 'false'
elif self.structural_mutation_surer.lower() == 'default':
self.structural_mutation_surer = 'default'
else:
raise RuntimeError(
"Invalid structural_mutation_surer {!r}".format(
self.structural_mutation_surer))
self.node_indexer = None
[docs] def add_activation(self, name, func, **kwargs):
self.activation_defs.add(name, func, **kwargs)
[docs] def add_aggregation(self, name, func, **kwargs):
self.aggregation_function_defs.add(name, func, **kwargs)
def get_activation_MPF(self, name):
return self.multiparameterset.get_MPF(name, 'activation')
def get_aggregation_MPF(self, name):
return self.multiparameterset.get_MPF(name, 'aggregation')
def get_activation_Evolved_MPF(self, name):
return self.multiparameterset.get_Evolved_MPF(name, 'activation')
def get_aggregation_Evolved_MPF(self, name):
return self.multiparameterset.get_Evolved_MPF(name, 'aggregation')
[docs] def save(self, f):
if 'partial' in self.initial_connection:
if not (0 <= self.connection_fraction <= 1):
raise RuntimeError(
"'partial' connection value must be between 0.0 and 1.0, inclusive.")
f.write('initial_connection = {0} {1}\n'.format(self.initial_connection,
self.connection_fraction))
else:
f.write('initial_connection = {0}\n'.format(self.initial_connection))
assert self.initial_connection in self.allowed_connectivity
write_pretty_params(f, self, [p for p in self._params
if 'initial_connection' not in p.name])
def remove_from_node_conn_dict(self, node_id): # DOCUMENT!
if (self.node_to_conn_dict is not None) and (node_id in self.node_to_conn_dict):
conn = self.node_to_conn_dict[node_id]
del self.node_to_conn_dict[node_id]
del self.conn_to_node_dict[conn]
[docs] def get_new_node_key(self, node_dict, conn=None): # UPDATE DOCUMENTATION
if self.node_indexer is None:
self.node_indexer = count(max(list(iterkeys(node_dict))) + 1)
if self.identical_mutations_keys and (conn is not None):
if self.conn_to_node_dict is None:
desired_min = max(5,(max(self.num_outputs,self.num_inputs)-self.num_hidden))
if self.identical_mutations_keys < desired_min:
warnings.warn(
"identical_mutations_keys {0:n} too low - raising to {1:n}".format(
self.identical_mutations_keys, desired_min))
self.identical_mutations_keys = desired_min
self.conn_to_node_dict = {}
self.node_to_conn_dict = {}
if conn in self.conn_to_node_dict:
new_id = self.conn_to_node_dict[conn]
else:
new_id = next(self.node_indexer)
self.conn_to_node_dict[conn] = new_id
self.node_to_conn_dict[new_id] = conn
assert new_id not in node_dict
while len(self.node_to_conn_dict) > self.identical_mutations_keys:
# if using randomized IDs, use OrderedDict for node_to_conn_dict
oldest = min(iterkeys(self.node_to_conn_dict))
if (oldest not in node_dict) or (len(self.node_to_conn_dict)
> (2*self.identical_mutations_keys)):
self.remove_from_node_conn_dict(oldest)
else:
break
else:
new_id = next(self.node_indexer)
assert new_id not in node_dict
return new_id
[docs] def get_compatibility_info(self):
to_return_dict = {}
to_return_dict['disjoint_coefficient'] = self.compatibility_disjoint_coefficient
to_return_dict['weight_coefficient'] = self.compatibility_weight_coefficient
return to_return_dict
[docs] def check_structural_mutation_surer(self):
if self.structural_mutation_surer == 'true':
return True
elif self.structural_mutation_surer == 'false':
return False
elif self.structural_mutation_surer == 'default':
return self.single_structural_mutation
else:
error_string = "Invalid structural_mutation_surer {!r}".format(
self.structural_mutation_surer)
raise RuntimeError(error_string)
[docs]class DefaultGenome(object):
"""
A genome for generalized neural networks.
Terminology
pin: Point at which the network is conceptually connected to the external world;
pins are either input or output.
node: Analog of a physical neuron.
connection: Connection between a pin/node output and a node's input, or between a node's
output and a pin/node input.
key: Identifier for an object, unique within the set of similar objects.
Design assumptions and conventions.
1. Each output pin is connected only to the output of its own unique
neuron by an implicit connection with weight one. This connection
is permanently enabled.
2. The output pin's key is always the same as the key for its
associated neuron.
3. Output neurons can be modified but not deleted.
4. The input values are applied to the input pins unmodified.
"""
[docs] @classmethod
def parse_config(cls, param_dict):
param_dict['node_gene_type'] = DefaultNodeGene
param_dict['connection_gene_type'] = DefaultConnectionGene
return DefaultGenomeConfig(param_dict)
[docs] @classmethod
def write_config(cls, f, config):
config.save(f)
def __init__(self, key):
# Unique identifier for a genome instance.
self.key = key
# (gene_key, gene) pairs for gene sets.
self.connections = {}
self.nodes = {}
# Fitness results.
self.fitness = None
# Should FeedForwardNetwork NOT be used for this genome?
# Direct cycle determination would be better given connection removal and disabling,
# but this is faster and simpler.
self.maybe_recurrent = False
[docs] def mutate(self, config):
""" Mutates this genome. """
if config.single_structural_mutation:
div = max(1,(config.node_add_prob + config.node_delete_prob +
config.conn_add_prob + config.conn_delete_prob))
r = random()
if r < (config.node_add_prob/div):
self.mutate_add_node(config)
elif r < ((config.node_add_prob + config.node_delete_prob)/div):
self.mutate_delete_node(config)
elif r < ((config.node_add_prob + config.node_delete_prob +
config.conn_add_prob)/div):
self.mutate_add_connection(config)
elif r < ((config.node_add_prob + config.node_delete_prob +
config.conn_add_prob + config.conn_delete_prob)/div):
self.mutate_delete_connection(config)
else:
if random() < config.node_add_prob:
self.mutate_add_node(config)
if random() < config.node_delete_prob:
self.mutate_delete_node(config)
if random() < config.conn_add_prob:
self.mutate_add_connection(config)
if random() < config.conn_delete_prob:
self.mutate_delete_connection(config)
# Mutate connection genes.
for cg in self.connections.values():
cg.mutate(config)
# Mutate node genes (bias, response, etc.).
for ng in self.nodes.values():
ng.mutate(config)
[docs] def mutate_add_node(self, config):
if not self.connections:
if config.check_structural_mutation_surer():
self.mutate_add_connection(config)
return
# Choose a random connection to split
conn_to_split = choice(list(self.connections.values()))
new_node_id = config.get_new_node_key(node_dict=self.nodes, conn=conn_to_split)
ng = self.create_node(config, new_node_id)
self.nodes[new_node_id] = ng
# Disable this connection and create two new connections joining its nodes via
# the given node. The new node+connections have roughly the same behavior as
# the original connection (depending on the activation function of the new node).
conn_to_split.enabled = False
i, o = conn_to_split.key
self.add_connection(config, i, new_node_id, 1.0, True)
self.add_connection(config, new_node_id, o, conn_to_split.weight, True)
[docs] def add_connection(self, config, input_key, output_key, weight, enabled):
# TODO: Add further validation of this connection addition?
assert isinstance(input_key, int)
assert isinstance(output_key, int)
assert output_key >= 0
assert isinstance(enabled, bool)
key = (input_key, output_key)
connection = config.connection_gene_type(key)
connection.init_attributes(config)
connection.weight = weight
connection.enabled = enabled
self.connections[key] = connection
[docs] def mutate_add_connection(self, config):
"""
Attempt to add a new connection, the only restriction being that the output
node cannot be one of the network input pins.
"""
possible_outputs = list(iterkeys(self.nodes))
out_node = choice(possible_outputs)
possible_inputs = possible_outputs + config.input_keys
in_node = choice(possible_inputs)
# Don't duplicate connections.
key = (in_node, out_node)
if key in self.connections:
if config.check_structural_mutation_surer():
self.connections[key].enabled = True
return
# Don't allow connections between two output nodes
if in_node in config.output_keys and out_node in config.output_keys:
return
# No need to check for connections between input nodes:
# they cannot be the output end of a connection (see above).
# For feed-forward networks, avoid creating cycles.
if config.feed_forward:
if creates_cycle(list(iterkeys(self.connections)), key):
return
elif not self.maybe_recurrent:
if creates_cycle(list(iterkeys(self.connections)), key):
self.maybe_recurrent = True
cg = self.create_connection(config, in_node, out_node)
self.connections[cg.key] = cg
config.remove_from_node_conn_dict(in_node)
config.remove_from_node_conn_dict(out_node)
[docs] def mutate_delete_node(self, config):
# Do nothing if there are no non-output nodes.
available_nodes = [k for k in iterkeys(self.nodes) if k not in config.output_keys]
if not available_nodes:
return -1
del_key = choice(available_nodes)
connections_to_delete = set()
for k in iterkeys(self.connections):
if del_key in k:
connections_to_delete.add(k)
if k[0]==del_key:
config.remove_from_node_conn_dict(k[1])
else:
config.remove_from_node_conn_dict(k[0])
for key in connections_to_delete:
del self.connections[key]
del self.nodes[del_key]
return del_key
[docs] def mutate_delete_connection(self, config=None):
if self.connections:
key = choice(list(self.connections.keys()))
if config is not None:
config.remove_from_node_conn_dict(key[0])
config.remove_from_node_conn_dict(key[1])
del self.connections[key]
[docs] def distance(self, other, config):
"""
Returns the genetic distance between this genome and the other. This distance value
is used to compute genome compatibility for speciation.
"""
# Compute node gene distance component.
node_distance = 0.0
if self.nodes or other.nodes:
disjoint_nodes = 0
for k2 in iterkeys(other.nodes):
if k2 not in self.nodes:
disjoint_nodes += 1
for k1, n1 in iteritems(self.nodes):
n2 = other.nodes.get(k1)
if n2 is None:
disjoint_nodes += 1
else:
# Homologous genes compute their own distance value.
node_distance += n1.distance(n2, config)
max_nodes = max(len(self.nodes), len(other.nodes))
node_distance = (node_distance +
(config.compatibility_disjoint_coefficient *
disjoint_nodes)) / max_nodes
# Compute connection gene differences.
connection_distance = 0.0
if self.connections or other.connections:
disjoint_connections = 0
for k2 in iterkeys(other.connections):
if k2 not in self.connections:
disjoint_connections += 1
for k1, c1 in iteritems(self.connections):
c2 = other.connections.get(k1)
if c2 is None:
disjoint_connections += 1
else:
# Homologous genes compute their own distance value.
connection_distance += c1.distance(c2, config)
max_conn = max(len(self.connections), len(other.connections))
connection_distance = (connection_distance +
(config.compatibility_disjoint_coefficient *
disjoint_connections)) / max_conn
distance = node_distance + connection_distance
return distance
[docs] def size(self):
"""
Returns genome 'complexity', taken to be
(number of nodes, number of enabled connections)
"""
num_enabled_connections = sum([1 for cg in self.connections.values() if cg.enabled])
return len(self.nodes), num_enabled_connections
[docs] def __str__(self):
s = "Key: {0}\nFitness: {1}\nNodes:".format(self.key, self.fitness)
for k, ng in iteritems(self.nodes):
s += "\n\t{0} {1!s}".format(k, ng)
s += "\nConnections:"
connections = list(self.connections.values())
connections.sort()
for c in connections:
s += "\n\t" + str(c)
return s
[docs] @staticmethod
def create_node(config, node_id):
node = config.node_gene_type(node_id)
node.init_attributes(config)
return node
[docs] @staticmethod
def create_connection(config, input_id, output_id):
connection = config.connection_gene_type((input_id, output_id))
connection.init_attributes(config)
return connection
[docs] def connect_fs_neat_nohidden(self, config):
"""
Randomly connect one input to all output nodes
(FS-NEAT without connections to hidden, if any).
Originally connect_fs_neat.
"""
input_id = choice(config.input_keys)
for output_id in config.output_keys:
connection = self.create_connection(config, input_id, output_id)
self.connections[connection.key] = connection
[docs] def connect_fs_neat_hidden(self, config):
"""
Randomly connect one input to all hidden and output nodes
(FS-NEAT with connections to hidden, if any).
"""
input_id = choice(config.input_keys)
others = [i for i in iterkeys(self.nodes) if i not in config.input_keys]
for output_id in others:
connection = self.create_connection(config, input_id, output_id)
self.connections[connection.key] = connection
[docs] def compute_full_connections(self, config, direct):
"""
Compute connections for a fully-connected feed-forward genome--each
input connected to all hidden nodes
(and output nodes if ``direct`` is set or there are no hidden nodes),
each hidden node connected to all output nodes.
(Recurrent genomes will also include node self-connections.)
"""
hidden = [i for i in iterkeys(self.nodes) if i not in config.output_keys]
output = [i for i in iterkeys(self.nodes) if i in config.output_keys]
connections = []
if hidden:
for input_id in config.input_keys:
for h in hidden:
connections.append((input_id, h))
for h in hidden:
for output_id in output:
connections.append((h, output_id))
if direct or (not hidden):
for input_id in config.input_keys:
for output_id in output:
connections.append((input_id, output_id))
# For recurrent genomes, include node self-connections.
if not config.feed_forward:
for i in iterkeys(self.nodes):
connections.append((i, i))
return connections
[docs] def connect_full_nodirect(self, config):
"""
Create a fully-connected genome
(except without direct input-output unless no hidden nodes).
"""
for input_id, output_id in self.compute_full_connections(config, False):
connection = self.create_connection(config, input_id, output_id)
if input_id == output_id:
self.maybe_recurrent = True
self.connections[connection.key] = connection
[docs] def connect_full_direct(self, config):
"""Create a fully-connected genome, including direct input-output connections."""
for input_id, output_id in self.compute_full_connections(config, True):
connection = self.create_connection(config, input_id, output_id)
if input_id == output_id:
self.maybe_recurrent = True
self.connections[connection.key] = connection
[docs] def connect_partial_nodirect(self, config):
"""
Create a partially-connected genome,
with (unless no hidden nodes) no direct input-output connections.
"""
assert 0 <= config.connection_fraction <= 1
all_connections = self.compute_full_connections(config, False)
shuffle(all_connections)
num_to_add = int(round(len(all_connections) * config.connection_fraction))
for input_id, output_id in all_connections[:num_to_add]:
connection = self.create_connection(config, input_id, output_id)
if input_id == output_id:
self.maybe_recurrent = True
self.connections[connection.key] = connection
[docs] def connect_partial_direct(self, config):
"""
Create a partially-connected genome,
including (possibly) direct input-output connections.
"""
assert 0 <= config.connection_fraction <= 1
all_connections = self.compute_full_connections(config, True)
shuffle(all_connections)
num_to_add = int(round(len(all_connections) * config.connection_fraction))
for input_id, output_id in all_connections[:num_to_add]:
connection = self.create_connection(config, input_id, output_id)
if input_id == output_id:
self.maybe_recurrent = True
self.connections[connection.key] = connection