Example #1
0
def test_minimal():
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'test_configuration')

    pop = Population(config_path)
    pe = parallel.ParallelEvaluator(4, eval_fitness)
    pop.run(pe.evaluate, 400)
def test_minimal():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 1.0

    # creates the population
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))
    config.save_best = True

    pop = Population(config)
    # run the simulation for up to 20 generations
    pop.run(eval_fitness, 20)

    # Test save_checkpoint with defaults
    pop.save_checkpoint()

    # get statistics
    best_unique = pop.statistics.best_unique_genomes(1)
    best_unique = pop.statistics.best_unique_genomes(2)
    avg_fitness = pop.statistics.get_average_fitness()
    assert len(avg_fitness) == 1
    assert all(f == 1 for f in avg_fitness)

    # Change fitness threshold and do another run.
    config.max_fitness_threshold = 1.1
    pop = Population(config)
    # runs the simulation for 20 generations
    pop.run(eval_fitness, 20)

    # get statistics
    avg_fitness = pop.statistics.get_average_fitness()
    assert len(avg_fitness) == 20
    assert all(f == 1 for f in avg_fitness)
def test_minimal():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 1.0

    # creates the population
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))

    pop = Population(config)
    # run the simulation for up to 20 generations
    pop.run(eval_fitness, 20)

    # get statistics
    avg_fitness = pop.statistics.get_average_fitness()
    assert len(avg_fitness) == 1
    assert all(f == 1 for f in avg_fitness)

    # Change fitness threshold and do another run.
    config.max_fitness_threshold = 1.1
    pop = Population(config)
    # runs the simulation for 20 generations
    pop.run(eval_fitness, 20)

    # get statistics
    avg_fitness = pop.statistics.get_average_fitness()
    assert len(avg_fitness) == 20
    assert all(f == 1 for f in avg_fitness)
def test_checkpoint():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 0.99

    # creates the population
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))

    pop = Population(config)
    pop.run(eval_fitness, 20)

    t = tempfile.NamedTemporaryFile(delete=False)
    t.close()
    pop.save_checkpoint(t.name)

    pop2 = Population(config)
    pop2.load_checkpoint(t.name)
def test_checkpoint():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 0.99

    # creates the population
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))

    pop = Population(config)
    pop.run(eval_fitness, 20)

    t = tempfile.NamedTemporaryFile(delete=False)
    t.close()
    pop.save_checkpoint(t.name)

    pop2 = Population(config)
    pop2.load_checkpoint(t.name)
def test_config_options():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 1.0

    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))

    for hn in (0, 1, 2):
        config.hidden_nodes = hn
        for fc in (0, 1):
            config.fully_connected = fc
            for act in activation_functions.functions.keys():
                config.allowed_activation = [act]
                for ff in (0, 1):
                    config.feedforward = ff

                    pop = Population(config)
                    pop.run(eval_fitness, 250)
def test_config_options():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 1.0

    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))

    for hn in (0, 1, 2):
        config.hidden_nodes = hn
        for fc in (0, 1):
            config.fully_connected = fc
            for act in activation_functions.functions.keys():
                config.allowed_activation = [act]
                for ff in (0, 1):
                    config.feedforward = ff

                    pop = Population(config)
                    pop.run(eval_fitness, 250)
Example #8
0
    time_out = config.fitness_threshold

    proper_game = play(game=proper_tetris, brain=brain, time_out=time_out)

    return proper_game.score


evaluator = ParallelEvaluator(
    num_workers=CPU_COUNT,
    eval_function=fitness_f,
)

if __name__ == '__main__':
    print('CPU_COUNT:', CPU_COUNT)

    p = Population(MAIN_CONFIG)

    p.add_reporter(StdOutReporter(True))
    stats = StatisticsReporter()
    p.add_reporter(stats)
    p.add_reporter(Checkpointer(50))

    try:
        winner = p.run(evaluator.evaluate, 1000)
    except KeyboardInterrupt:
        pass

    stats.save()

    print(stats.best_genome())