def test_endless(): evaluator = MultiEnvEvaluator( make_net, activate_net, batch_size=4, make_env=make_endless_env, max_env_steps=10, ) evaluator.eval_genome(None, None)
def test_multi(): evaluator = MultiEnvEvaluator(make_net, activate_net, batch_size=4, make_env=make_env) returns = evaluator.eval_genome(None, None) assert returns == (2 + 2 + 6 - 12) / 4
def test_optimal(): envs = [ StrictTMazeEnv(init_reward_side=i, n_trials=100) for i in [1, 0, 1, 0] ] evaluator = MultiEnvEvaluator(make_net, activate_net, envs=envs, batch_size=4, max_env_steps=1600) fitness = evaluator.eval_genome(None, None) assert fitness == 98.8
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__), "tmaze.cfg") config = neat.Config( neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path, ) total_grad_steps = 2 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 if grad_steps == total_grad_steps: return else: execute_back_prop(genome_dict, champ_key, config) grad_steps += 1 eval_genomes(genomes, config, grad_steps) 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