def generate_genome_given_graph(graph, connection_weights): genome = Genome(key='foo') unique_node_keys = [] input_nodes = [] for connection in graph: for node_key in connection: if node_key not in unique_node_keys: unique_node_keys.append(node_key) if node_key < 0: input_nodes.append(node_key) input_nodes = set(input_nodes) unique_node_keys = list( set(unique_node_keys + genome.get_output_nodes_keys()) - input_nodes) nodes = {} for node_key in unique_node_keys: node = NodeGene(key=node_key).random_initialization() node.set_mean(0) node.set_std(STD) nodes[node_key] = node connections = {} for connection_key, weight in zip(graph, connection_weights): connection = ConnectionGene(key=connection_key) connection.set_mean(weight) connection.set_std(STD) connections[connection_key] = connection genome.connection_genes = connections genome.node_genes = nodes return genome
def mutate_add_node(self, genome: Genome): # TODO: careful! this can add multihop-jumps to the network possible_connections_to_split = list(genome.connection_genes.keys()) # Choose a random connection to split k = min(len(possible_connections_to_split), self.architecture_mutation_power) connection_to_split_keys = random.sample(possible_connections_to_split, k) for connection_to_split_key in connection_to_split_keys: new_node_key = genome.get_new_node_key() new_node = NodeGene(key=new_node_key).random_initialization() genome.node_genes[new_node_key] = new_node connection_to_split = genome.connection_genes[ connection_to_split_key] i, o = connection_to_split_key # add connection between input and the new node new_connection_key_i = (i, new_node_key) new_connection_i = ConnectionGene(key=new_connection_key_i) new_connection_i.set_mean(mean=1.0), new_connection_i.set_std( std=0.0000001) genome.connection_genes[new_connection_key_i] = new_connection_i # add connection between new node and output new_connection_key_o = (new_node_key, o) new_connection_o = ConnectionGene( key=new_connection_key_o).random_initialization() new_connection_o.set_mean(mean=connection_to_split.get_mean()) new_connection_o.set_std(std=connection_to_split.get_std()) genome.connection_genes[new_connection_key_o] = new_connection_o # delete connection # Careful: neat-python disable the connection instead of deleting # conn_to_split.enabled = False del genome.connection_genes[connection_to_split_key] logger.network( f'Genome {genome.key}. Mutation: Add a Node: {new_node_key} between {i}->{o}' ) return genome
def create_from_julia_dict(genome_dict: dict): config = get_configuration() genome = Genome(key=genome_dict["key"], id=None, genome_config=config) # reconstruct nodes and connections connection_genes_dict = genome_dict['connections'] for key_str, connection_gene_dict in connection_genes_dict.items(): connection_key = Genome._get_connection_key_from_key_str(key_str) connection_gene = ConnectionGene(key=connection_key) connection_gene.set_mean(connection_gene_dict['mean_weight']) connection_gene.set_std(connection_gene_dict['std_weight']) genome.connection_genes[connection_gene.key] = connection_gene node_genes_dict = genome_dict['nodes'] for key_str, node_gene_dict in node_genes_dict.items(): node_key = int(key_str) node_gene = NodeGene(key=node_key) node_gene.set_mean(node_gene_dict['mean_bias']) node_gene.set_std(node_gene_dict['std_bias']) genome.node_genes[node_gene.key] = node_gene genome.calculate_number_of_parameters() return genome
def add_connection(self, key, mean=None, std=None): connection = ConnectionGene(key=key) connection.set_mean(mean) connection.set_std(std) self.connection_genes[key] = connection