def _init_connections(self, input_nodes): for input_node in self._nodes[0:input_nodes + 1]: random_output_node = choice(self._nodes[input_nodes + 1:]) new_connection = ConnectGene(input_node, random_output_node, -1) new_connection.set_innovation_number( self._generation.get_innovation_number(new_connection)) input_node.append_con_out(new_connection) random_output_node.append_con_in(new_connection) self._generation.put_mutation(new_connection) self._connections.append(new_connection)
def _create_new_connections(self, found, node, connection_to_split): innov_1 = self._generation.innovation_number() innov_2 = self._generation.innovation_number() + 1 in_connection = ConnectGene(connection_to_split.input_node(), node, innov_1, weight_type="previous", weight=connection_to_split.weight()) out_connection = ConnectGene(node, connection_to_split.output_node(), innov_2, weight_type="one") both = 0 if found: for mut in self._generation.mutations(): if mut == in_connection: in_connection.set_innovation_number( mut.innovation_number()) both += 1 if mut == out_connection: out_connection.set_innovation_number( mut.innovation_number()) both += 1 if both == 2: break else: self._generation.increase(2) self._generation.mutations().append(in_connection) self._generation.mutations().append(out_connection) return in_connection, out_connection
def _pick_nodes(self): without_output = [] for node in self._nodes: if node.type() is not NeuronType.OUTPUT: without_output.append(node) in_node = choice(without_output) out_node = choice(self._nodes[self._input_nodes + 1:]) while out_node == in_node or ConnectGene(out_node, in_node, 0) in self._connections: out_node = choice(self._nodes[self._input_nodes + 1:]) return in_node, out_node
def _pick_connection(self): from_node, to_node = self._pick_nodes() connection = ConnectGene(from_node, to_node, -1) has_connection = True count = 0 while has_connection and count < 10: if connection in self._connections: from_node, to_node = self._pick_nodes() connection = ConnectGene(from_node, to_node, -1) else: has_connection = False from_node.append_con_out(connection) to_node.append_con_in(connection) count += 1 innov_number = self._generation.get_innovation_number(connection) connection.set_innovation_number(innov_number) return connection, has_connection