Exemple #1
0
    def cross(self, genome1, genome2, gene_tracker):
        child_genome = Genome()
        for node in genome1.nodes:
            child_genome.add_node(node.copy())

        for connection in genome1.connections:
            new_connection = connection.copy()
            if genome2.contains_connection(new_connection.in_node_id,
                                           new_connection.out_node_id):
                other_connection = genome2.get_connection(
                    new_connection.in_node_id, new_connection.out_node_id)
                if not new_connection.enabled or not other_connection.enabled:
                    should_enable = random.random() > self.disable_probability
                    if new_connection.enabled != should_enable:
                        new_connection.disable(
                        ) if new_connection.enabled else new_connection.enable(
                        )
                if random.choice([True, False]):
                    new_connection.weight = other_connection.weight
            child_genome.add_connection(new_connection)

        return child_genome
Exemple #2
0
def get_genome_from_standard_network(network: nn.Module,
                                     key=1,
                                     std=0.00001) -> Genome:
    parameters = network.state_dict()
    genome = Genome(key=key)

    n_layers = len(parameters) // 2
    nodes_per_layer = {}
    # process nodes
    node_key_index = 0
    for i in range(n_layers):
        biases = parameters[f'layer_{i}.bias'].numpy()
        nodes_index = list(range(node_key_index, node_key_index + len(biases)))
        nodes_per_layer[i] = nodes_index
        node_key_index += len(nodes_index)
        for bias, key in zip(biases, nodes_index):
            genome.add_node(key=key, mean=bias, std=std)
            # node_key_index += 1
    # nodes_per_layer[i+1] = list(range(-1, -genome.n_input - 1, -1))
    nodes_per_layer[i + 1] = list(range(-genome.n_input, 0))

    # process connections
    for i in range(n_layers):
        weights = parameters[f'layer_{i}.weight'].numpy()
        n_output, n_input = weights.shape
        output_keys = nodes_per_layer[i]
        # if nodes_per_layer.get(i+1):
        input_keys = nodes_per_layer[i + 1]

        for output_key, output_index in zip(output_keys, range(n_output)):

            for input_key, input_index in zip(input_keys, range(n_input)):
                key = (input_key, output_key)
                genome.add_connection(key=key,
                                      mean=weights[output_index][input_index],
                                      std=std)
    return genome
genome.add_node(Node(9, 1, ReLU(), 1.0))
genome.add_node(Node(10, 1, ReLU(), 1.0))

genome.add_node(Node(11, 2, ReLU(), 1.0))
genome.add_node(Node(12, 2, ReLU(), 1.0))
genome.add_node(Node(13, 2, ReLU(), 1.0))
genome.add_node(Node(14, 2, ReLU(), 1.0))

genome.add_node(Node(15, 3, ReLU(), 1.0))
genome.add_node(Node(16, 3, ReLU(), 1.0))
genome.add_node(Node(17, 3, ReLU(), 1.0))

genome.add_node(Node(4, 4, ReLU(), 1.0))
genome.add_node(Node(5, 4, ReLU(), 1.0))

genome.add_connection(Connection(1, 1, 6, True, 0, 0.5))
genome.add_connection(Connection(1, 1, 7, True, 0, -1.0))
genome.add_connection(Connection(1, 1, 9, True, 0, -0.4))

genome.add_connection(Connection(1, 2, 6, True, 0, 0.7))
genome.add_connection(Connection(1, 2, 7, True, 0, -0.1))

genome.add_connection(Connection(1, 3, 6, False, 0, 0.6))
genome.add_connection(Connection(1, 3, 9, True, 0, -0.18))
genome.add_connection(Connection(1, 3, 8, True, 0, 0.72))
genome.add_connection(Connection(1, 3, 7, True, 0, -0.9))

genome.add_connection(Connection(1, 6, 11, True, 0, 0.6))
genome.add_connection(Connection(1, 6, 13, False, 0, -0.5))
genome.add_connection(Connection(1, 7, 14, True, 0, 1.0))
genome.add_connection(Connection(1, 7, 12, True, 0, -0.9))