Python neat.DefaultGenome() Examples

The following are 22 code examples of neat.DefaultGenome(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module neat , or try the search function .
Example #1
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #2
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #3
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #4
Source File: trainer.py    From go_dino with GNU General Public License v3.0 6 votes vote down vote up
def main():
    local_dir = os.path.dirname(__file__)
    config = Config(neat.DefaultGenome, neat.DefaultReproduction,
                    neat.DefaultSpeciesSet, neat.DefaultStagnation,
                    os.path.join(local_dir, 'train_config.txt'))
    config.save_best = True
    config.checkpoint_time_interval = 3

    pop = population.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.StdOutReporter(True))
    pop.add_reporter(neat.StatisticsReporter())
    pop.add_reporter(neat.Checkpointer(2))
    winner = pop.run(eval_fitness, 100)
    with open('winner.pkl', 'wb') as f:
        pickle.dump(winner, f) 
Example #5
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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(g.nodes), {0, 1, 2})
        self.assertLess(len(g.connections), 8) 
Example #6
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #7
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #8
Source File: cppn.py    From poet with Apache License 2.0 5 votes vote down vote up
def __init__(self, cppn_config_path='config-cppn', genome_path=None):
        self.cppn_config_path = os.path.dirname(__file__) + '/' + cppn_config_path
        self.genome_path = genome_path
        self.hardcore = False
        self.cppn_config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, self.cppn_config_path)
        self.cppn_genome = None
        self.altitude_fn = lambda x: x
        if genome_path is not None:
            self.cppn_genome = pickle.load(open(genome_path, 'rb'))
        else:
            start_cppn_genome = PrettyGenome('0')
            start_cppn_genome.configure_new(self.cppn_config.genome_config)
            start_cppn_genome.nodes[0].activation = 'identity'
            self.cppn_genome = start_cppn_genome
        self.reset_altitude_fn() 
Example #9
Source File: main.py    From PyTorch-NEAT with Apache License 2.0 5 votes vote down vote up
def run(n_generations):
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    config_path = os.path.join(os.path.dirname(__file__), "neat.cfg")
    config = neat.Config(
        neat.DefaultGenome,
        neat.DefaultReproduction,
        neat.DefaultSpeciesSet,
        neat.DefaultStagnation,
        config_path,
    )

    evaluator = MultiEnvEvaluator(
        make_net, activate_net, make_env=make_env, max_env_steps=max_env_steps
    )

    def eval_genomes(genomes, config):
        for _, genome in genomes:
            genome.fitness = evaluator.eval_genome(genome, config)

    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    reporter = neat.StdOutReporter(True)
    pop.add_reporter(reporter)
    logger = LogReporter("neat.log", evaluator.eval_genome)
    pop.add_reporter(logger)

    pop.run(eval_genomes, n_generations) 
Example #10
Source File: main.py    From TensorFlow-NEAT with Apache License 2.0 5 votes vote down vote up
def run(n_generations):
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    config_path = os.path.join(os.path.dirname(__file__), "neat.cfg")
    config = neat.Config(
        neat.DefaultGenome,
        neat.DefaultReproduction,
        neat.DefaultSpeciesSet,
        neat.DefaultStagnation,
        config_path,
    )

    evaluator = MultiEnvEvaluator(
        make_net, activate_net, make_env=make_env, max_env_steps=max_env_steps
    )

    def eval_genomes(genomes, config):
        for _, genome in genomes:
            genome.fitness = evaluator.eval_genome(genome, config)

    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    reporter = neat.StdOutReporter(True)
    pop.add_reporter(reporter)
    logger = LogReporter("./logs/neat.json", evaluator.eval_genome)
    pop.add_reporter(logger)

    pop.run(eval_genomes, n_generations) 
Example #11
Source File: play_winner.py    From go_dino with GNU General Public License v3.0 5 votes vote down vote up
def main():
    local_dir = os.path.dirname(__file__)
    config = Config(neat.DefaultGenome, neat.DefaultReproduction,
                    neat.DefaultSpeciesSet, neat.DefaultStagnation,
                    os.path.join(local_dir, 'train_config.txt'))
    with open('winner.pkl', 'rb') as f:
        winner = pickle.load(f)
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nOutput:')
    winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
    print('Score:', dino_api.play_game(GetCommand(winner_net))) 
Example #12
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        """
        Determine path to configuration file. This path manipulation is
        here so that the script will run successfully regardless of the
        current working directory.
        """
        local_dir = os.path.dirname(__file__)
        config_path = os.path.join(local_dir, 'test_configuration')
        self.config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                                  neat.DefaultSpeciesSet, neat.DefaultStagnation,
                                  config_path)
        config2_path = os.path.join(local_dir, 'test_configuration2')
        self.config2 = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                                   neat.DefaultSpeciesSet, neat.DefaultStagnation,
                                   config2_path) 
Example #13
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #14
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_fully_connected_hidden_nodirect(self):
        """full with no direct input-output connections, only via hidden nodes."""
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'full_nodirect'
        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), 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) 
Example #15
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #16
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_fs_neat_hidden(self):
        """fs_neat with connecting hidden nodes."""
        gid = 42
        config = self.config.genome_config
        config.initial_connection = 'fs_neat_hidden'
        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), 3) 
Example #17
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_unconnected_hidden(self):
        """Unconnected network with hidden nodes."""
        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(g.nodes), {0, 1, 2})
        assert (not g.connections) 
Example #18
Source File: test_genome.py    From neat-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #19
Source File: main.py    From TensorFlow-NEAT with Apache License 2.0 4 votes vote down vote up
def run(n_generations, n_processes):
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    config_path = os.path.join(os.path.dirname(__file__), "neat.cfg")
    config = neat.Config(
        neat.DefaultGenome,
        neat.DefaultReproduction,
        neat.DefaultSpeciesSet,
        neat.DefaultStagnation,
        config_path,
    )

    envs = [t_maze.TMazeEnv(init_reward_side=i, n_trials=100) for i in [1, 0, 1, 0]]

    evaluator = MultiEnvEvaluator(
        make_net, activate_net, envs=envs, batch_size=batch_size, max_env_steps=1000
    )

    if n_processes > 1:
        pool = multiprocessing.Pool(processes=n_processes)

        def eval_genomes(genomes, config):
            fitnesses = pool.starmap(
                evaluator.eval_genome, ((genome, config) for _, genome in genomes)
            )
            for (_, genome), fitness in zip(genomes, fitnesses):
                genome.fitness = fitness

    else:

        def eval_genomes(genomes, config):
            for i, (_, genome) in enumerate(genomes):
                try:
                    genome.fitness = evaluator.eval_genome(
                        genome, config, debug=DEBUG and i % 100 == 0
                    )
                except Exception as e:
                    print(genome)
                    raise e

    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    reporter = neat.StdOutReporter(True)
    pop.add_reporter(reporter)
    logger = LogReporter("./logs/adaptive.json", evaluator.eval_genome)
    pop.add_reporter(logger)

    winner = pop.run(eval_genomes, n_generations)

    print(winner)
    final_performance = evaluator.eval_genome(winner, config)
    print("Final performance: {}".format(final_performance))
    generations = reporter.generation + 1
    return generations 
Example #20
Source File: test_recurrent.py    From TensorFlow-NEAT with Apache License 2.0 4 votes vote down vote up
def test_match_neat():
    assert tf.executing_eagerly()

    with open("tests/test-genome.pkl", "rb") as f:
        genome = pickle.load(f)

    # use tanh since neat sets output nodes with no inputs to 0
    # (sigmoid would output 0.5 for us)
    def neat_tanh_activation(z):
        return float(tf.tanh(2.5 * tf.convert_to_tensor(z, dtype=tf.float64)))

    for node in genome.nodes.values():
        node.response = 0.5

    config = neat.Config(
        neat.DefaultGenome,
        neat.DefaultReproduction,
        neat.DefaultSpeciesSet,
        neat.DefaultStagnation,
        "tests/test-config.cfg",
    )

    for _ in range(500):
        genome.mutate(config.genome_config)
        # print(genome)

        neat_net = neat.nn.RecurrentNetwork.create(genome, config)
        for i, (node, _activation, aggregation, bias, response, links) in enumerate(
            neat_net.node_evals
        ):
            neat_net.node_evals[i] = (
                node,
                neat_tanh_activation,
                aggregation,
                bias,
                response,
                links,
            )

        tf_net = RecurrentNet.create(
            genome, config, activation=tanh_activation, prune_empty=True
        )

        for _ in range(5):
            inputs = np.random.randn(12)
            # print(inputs)
            neat_result = neat_net.activate(inputs)
            tf_result = tf_net.activate([inputs])[0].numpy()
            assert np.allclose(neat_result, tf_result, atol=1e-8) 
Example #21
Source File: main.py    From PyTorch-NEAT with Apache License 2.0 4 votes vote down vote up
def run(n_generations, n_processes):
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    config_path = os.path.join(os.path.dirname(__file__), "neat.cfg")
    config = neat.Config(
        neat.DefaultGenome,
        neat.DefaultReproduction,
        neat.DefaultSpeciesSet,
        neat.DefaultStagnation,
        config_path,
    )

    envs = [t_maze.TMazeEnv(init_reward_side=i, n_trials=100) for i in [1, 0, 1, 0]]

    evaluator = MultiEnvEvaluator(
        make_net, activate_net, envs=envs, batch_size=batch_size, max_env_steps=1000
    )

    if n_processes > 1:
        pool = multiprocessing.Pool(processes=n_processes)

        def eval_genomes(genomes, config):
            fitnesses = pool.starmap(
                evaluator.eval_genome, ((genome, config) for _, genome in genomes)
            )
            for (_, genome), fitness in zip(genomes, fitnesses):
                genome.fitness = fitness

    else:

        def eval_genomes(genomes, config):
            for i, (_, genome) in enumerate(genomes):
                try:
                    genome.fitness = evaluator.eval_genome(
                        genome, config, debug=DEBUG and i % 100 == 0
                    )
                except Exception as e:
                    print(genome)
                    raise e

    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    reporter = neat.StdOutReporter(True)
    pop.add_reporter(reporter)
    logger = LogReporter("log.json", evaluator.eval_genome)
    pop.add_reporter(logger)

    winner = pop.run(eval_genomes, n_generations)

    print(winner)
    final_performance = evaluator.eval_genome(winner, config)
    print("Final performance: {}".format(final_performance))
    generations = reporter.generation + 1
    return generations 
Example #22
Source File: test_recurrent.py    From PyTorch-NEAT with Apache License 2.0 4 votes vote down vote up
def test_match_neat():
    with open("tests/test-genome.pkl", "rb") as f:
        genome = pickle.load(f)

    # use tanh since neat sets output nodes with no inputs to 0
    # (sigmoid would output 0.5 for us)
    def neat_tanh_activation(z):
        return float(torch.tanh(2.5 * torch.tensor(z, dtype=torch.float64)))

    for node in genome.nodes.values():
        node.response = 0.5

    config = neat.Config(
        neat.DefaultGenome,
        neat.DefaultReproduction,
        neat.DefaultSpeciesSet,
        neat.DefaultStagnation,
        "tests/test-config.cfg",
    )

    for _ in range(500):
        genome.mutate(config.genome_config)
        # print(genome)

        neat_net = neat.nn.RecurrentNetwork.create(genome, config)
        for i, (node, _activation, aggregation, bias, response, links) in enumerate(
            neat_net.node_evals
        ):
            neat_net.node_evals[i] = (
                node,
                neat_tanh_activation,
                aggregation,
                bias,
                response,
                links,
            )

        torch_net = RecurrentNet.create(
            genome, config, activation=tanh_activation, prune_empty=True
        )

        for _ in range(5):
            inputs = np.random.randn(12)
            # print(inputs)
            neat_result = neat_net.activate(inputs)
            torch_result = torch_net.activate([inputs])[0].numpy()
            assert np.allclose(neat_result, torch_result, atol=1e-8)