예제 #1
0
    def test_fully_connected_hidden_nodirect_old(self):
        """
        full (no specification re direct/nodirect) with hidden nodes;
        should output warning re docs/code conflict.
        """
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'full'
        config.num_hidden = 2

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        print("\nThis should output a warning:", file=sys.stderr)
        g.configure_new(config)  # TODO: Test for emitted warning

        print(g)
        self.assertEqual(set(g.nodes), {0, 1, 2})
        self.assertEqual(len(g.connections), 6)

        # Check that each input is connected to each hidden node.
        for i in config.input_keys:
            for h in (1, 2):
                assert ((i, h) in g.connections)

        # Check that each hidden node is connected to the output.
        for h in (1, 2):
            assert ((h, 0) in g.connections)

        # Check that inputs are not directly connected to the output
        for i in config.input_keys:
            assert ((i, 0) not in g.connections)
예제 #2
0
def run(path):
    global G_gen
    global G_hall_of_fame

    config = neat.config.Config(*C_NEAT_CONFIG_DEFAULTS, path)
    pop = neat.Population(config)

    pop.add_reporter(neat.StdOutReporter(True))
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)

    best_fitness = -1000
    winner = neat.DefaultGenome(0)

    graph = Graph(C_FITNESS_THRESHOLD, C_HANDS_PER_GENERATION, C_POP_PER_GEN)

    while best_fitness < C_FITNESS_THRESHOLD:
        winner = pop.run(eval_genomes, 1)

        G_hall_of_fame.append(winner.fitness)
        G_ao10.append(average(G_hall_of_fame))

        graph.update(G_hall_of_fame, G_ao10, G_gen)

        best_fitness = winner.fitness

    print("Winner (after %s hand of blackjack):" %
          (C_HANDS_PER_GENERATION * C_POP_PER_GEN * G_gen))
    print(winner)
    with open("winner-feedforward", 'wb') as f:
        pickle.dump(winner, f)
    print("Winner saved")
예제 #3
0
    def test_fully_connected_hidden(self):
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'full'
        config.num_hidden = 2

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(config)

        print(g)
        self.assertEqual(set(iterkeys(g.nodes)), {0, 1, 2})
        self.assertEqual(len(g.connections), 6)

        # Check that each input is connected to each hidden node.
        for i in config.input_keys:
            for h in (1, 2):
                assert ((i, h) in g.connections)

        # Check that each hidden node is connected to the output.
        for h in (1, 2):
            assert ((h, 0) in g.connections)

        # Check that inputs are not directly connected to outputs
        for i in config.input_keys:
            assert ((i, 0) not in g.connections)
예제 #4
0
    def test_fully_connected_hidden_direct(self):
        """full with direct input-output connections (and also via hidden hodes)."""
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'full_direct'
        config.num_hidden = 2

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(config)

        print(g)
        self.assertEqual(set(g.nodes), {0, 1, 2})
        self.assertEqual(len(g.connections), 8)

        # Check that each input is connected to each hidden node.
        for i in config.input_keys:
            for h in (1, 2):
                assert ((i, h) in g.connections)

        # Check that each hidden node is connected to the output.
        for h in (1, 2):
            assert ((h, 0) in g.connections)

        # Check that inputs are directly connected to the output
        for i in config.input_keys:
            assert ((i, 0) in g.connections)
예제 #5
0
    def test_unconnected_hidden(self):
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'unconnected'
        config.num_hidden = 2

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(self.config.genome_config)

        print(g)
        self.assertEqual(set(iterkeys(g.nodes)), {0, 1, 2})
        assert (not g.connections)
예제 #6
0
    def test_fs_neat_nohidden(self):
        """fs_neat not connecting hidden nodes."""
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'fs_neat_nohidden'
        config.num_hidden = 2

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(config)

        print(g)
        self.assertEqual(set(g.nodes), {0, 1, 2})
        self.assertEqual(len(g.connections), 1)
예제 #7
0
    def test_partially_connected_no_hidden(self):
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'partial'
        config.connection_fraction = 0.5
        config.num_hidden = 0

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(config)

        print(g)
        self.assertEqual(set(iterkeys(g.nodes)), {0})
        self.assertLess(len(g.connections), 2)
예제 #8
0
    def test_unconnected_no_hidden(self):
        """Unconnected network with only input and output nodes."""
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'unconnected'
        config.num_hidden = 0

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(self.config.genome_config)

        print(g)
        self.assertEqual(set(g.nodes), {0})
        assert (not g.connections)
예제 #9
0
    def test_partially_connected_hidden_nodirect(self):
        """partial with no direct input-output connections, only via hidden nodes."""
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'partial_nodirect'
        config.connection_fraction = 0.5
        config.num_hidden = 2

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(config)

        print(g)
        self.assertEqual(set(g.nodes), {0, 1, 2})
        self.assertLess(len(g.connections), 6)
예제 #10
0
    def test_fs_neat_no_hidden(self):
        """
        fs_neat with no hidden nodes
        (equivalent to fs_neat_hidden and fs_neat_nohidden with no hidden nodes).
        """
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'fs_neat'
        config.num_hidden = 0

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(config)

        print(g)
        self.assertEqual(set(g.nodes), {0})
        self.assertEqual(len(g.connections), 1)
예제 #11
0
    def test_fully_connected_no_hidden(self):
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'full'
        config.num_hidden = 0

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(config)

        print(g)
        self.assertEqual(set(iterkeys(g.nodes)), {0})
        self.assertEqual(len(g.connections), 2)

        # Check that each input is connected to the output node
        for i in config.input_keys:
            assert ((i, 0) in g.connections)
예제 #12
0
    def test_fs_neat_hidden_old(self):
        """
        fs_neat (without hidden/nohidden specification) with hidden;
        should output warning about doc/code conflict.
        """
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'fs_neat'
        config.num_hidden = 2

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        print("\nThis should output a warning:", file=sys.stderr)
        g.configure_new(config)  # TODO: Test for emitted warning

        print(g)
        self.assertEqual(set(g.nodes), {0, 1, 2})
        self.assertEqual(len(g.connections), 1)
예제 #13
0
    def test_partially_connected_no_hidden(self):
        """
        partial with no hidden nodes
        (equivalent to partial_nodirect and partial_direct with no hidden nodes)
        """
        gid = 42
        config = self.config2.genome_config
        config.initial_connection = 'partial'
        config.connection_fraction = 0.5
        config.num_hidden = 0

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(config)

        print(g)
        self.assertEqual(set(g.nodes), {0})
        self.assertLess(len(g.connections), 2)
예제 #14
0
파일: test_genome.py 프로젝트: susano0/NEAT
    def test_partially_connected_hidden_direct(self):
        """
        partial with (potential) direct input-output connections
        (and also, potentially, via hidden hodes).
        """
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'partial_direct'
        config.connection_fraction = 0.5
        config.num_hidden = 2

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(config)

        print(g)
        self.assertEqual(set(iterkeys(g.nodes)), {0, 1, 2})
        self.assertLess(len(g.connections), 8)
예제 #15
0
    def test_partially_connected_hidden_nodirect_old(self):
        """
        partial (no specification re direct/nodirect) with hidden nodes;
        should output warning re docs/code conflict.
        """
        gid = 42
        config = self.config2.genome_config
        config.initial_connection = 'partial'
        config.connection_fraction = 0.5
        config.num_hidden = 2

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        print("\nThis should output a warning:", file=sys.stderr)
        g.configure_new(config)  # TODO: Test for emitted warning

        print(g)
        self.assertEqual(set(g.nodes), {0, 1, 2})
        self.assertLess(len(g.connections), 6)
예제 #16
0
    def test_fully_connected_no_hidden(self):
        """
        full with no hidden nodes
        (equivalent to full_nodirect and full_direct with no hidden nodes)
        """
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'full'
        config.num_hidden = 0

        g = neat.DefaultGenome(gid)
        self.assertEqual(gid, g.key)
        g.configure_new(config)

        print(g)
        self.assertEqual(set(g.nodes), {0})
        self.assertEqual(len(g.connections), 2)

        # Check that each input is connected to the output node
        for i in config.input_keys:
            assert ((i, 0) in g.connections)
예제 #17
0
def genome_and_network_fixture(gid: int):
    genome = neat.DefaultGenome(gid)
    genome.configure_new(CONFIG.genome_config)
    network = neat.nn.FeedForwardNetwork.create(genome, CONFIG)
    return genome, network
예제 #18
0
def keras2neat(neat_config,
               keras_h5_path,
               new_genome_key,
               run_name='1',
               generation=''):
    new_genome_key = str(new_genome_key)

    print("[keras2neat] Loading model data..")
    model_data = load_keras2neat_model_data(keras_h5_path)
    print("[keras2neat] Loaded.")

    # Use a random key
    new_genome = neat.DefaultGenome(new_genome_key)

    num_inputs = model_data[0]['kernel'].shape[0]
    num_outputs = model_data[-1]['kernel'].shape[1]

    new_node_id = num_outputs
    prev_layer_size = None
    total_layer_size = 0
    prev_layer_first_id = 0
    prev_layer_last_id = 0
    layer_last_id = 0
    complete = False

    # create the output nodes
    for o in range(num_outputs):
        new_node = new_genome.create_node(neat_config.genome_config, o)
        # set the bias of the node
        setattr(new_node, 'bias', model_data[-1]["bias"][o])
        new_genome.nodes[o] = new_node

    for layer_id, layer in enumerate(model_data):
        if complete:
            break
        layer_size = layer["bias"].shape[0]
        layer_first_node_id = new_node_id
        total_layer_size += layer_size
        if layer_id != 0:
            prev_layer_first_id = new_node_id - prev_layer_size
            prev_layer_last_id = prev_layer_first_id + prev_layer_size
            layer_last_id = layer_first_node_id + layer_size
        for j in range(layer_size):
            # each entry in ['bias'] represents a node in this layer
            # create a new node
            new_node = new_genome.create_node(neat_config.genome_config,
                                              new_node_id)
            # set the bias of the node
            setattr(new_node, 'bias', layer["bias"][j])
            # add the node to the genome
            new_genome.nodes[new_node_id] = new_node

            # add the connections to the previous layer. NOTE: Assumes a fully connected network
            if layer_id == 0:
                # connect to the inputs (ID is negative)
                for k in range(-1, -num_inputs - 1, -1):
                    #print("connecting {} to {}".format(k, new_node_id))
                    new_genome.add_connection(neat_config.genome_config, k,
                                              new_node_id,
                                              layer["kernel"][k][j], True)
            elif layer_id == len(model_data) - 1:
                # connect the outputs
                for p in range(prev_layer_size):
                    for o in range(num_outputs):
                        # connect the prev_layer to the output
                        new_genome.add_connection(neat_config.genome_config,
                                                  p + prev_layer_first_id, o,
                                                  layer['kernel'][p][o], True)
                complete = True
                break
            else:
                #print("Connecting hidden layer {}..".format(layer_id))
                # connect to the previous layer
                for k_i, k in enumerate(layer['kernel']):
                    new_genome.add_connection(neat_config.genome_config,
                                              k_i + prev_layer_first_id,
                                              new_node_id, k[j], True)
            new_node_id += 1
            prev_layer_size = layer_size

    visualize.draw_net(neat_config,
                       new_genome,
                       view=False,
                       filename="eval/run_{}/keras2neat/{}_gen_{}".format(
                           run_name, new_genome_key, generation))

    return new_genome