diff --git a/MutateActions/main.py b/MutateActions/main.py index 7a1fc626d9b001d3fdf549a653504e3e4fdd1cc6..b81ae78277734b51a6bea91463d93f6c8a6139f1 100644 --- a/MutateActions/main.py +++ b/MutateActions/main.py @@ -1,19 +1,39 @@ from population import Population import time import matplotlib.pyplot as plt +import pickle INCREASE_BY = 5 BRAIN_SIZE = 50 POP_SIZE = 50 -MUTATION_FACTOR = 0.1 # 0 <= x <= 1 +MUTATION_FACTOR = 0.2 # 0 <= x <= 1 GAME_CANCELLED = False -LOAD_BRAIN = True +LOAD_BRAIN = False RENDER_BEST = False +TEST_WALKER = True + +if TEST_WALKER: + LOAD_BRAIN = True + if __name__ == '__main__': population = Population(POP_SIZE, BRAIN_SIZE,MUTATION_FACTOR, LOAD_BRAIN, RENDER_BEST) + rewards = [] + + if TEST_WALKER: + rewards = [] + with open('rewards.p', 'rb') as fp: + rewards = pickle.load(fp) + plt.title(f'{POP_SIZE}, {MUTATION_FACTOR}') + plt.xlabel('Episodes') + plt.ylabel('Rewards') + plt.plot(rewards) + plt.savefig(f'./models/{POP_SIZE}, {MUTATION_FACTOR}.png') + plt.show() + while GAME_CANCELLED is False: # this is our game + if population.all_players_finished(): # this is our genetic algorithm after one generation of players population.natural_selection() population.mutate_babies() @@ -24,6 +44,12 @@ if __name__ == '__main__': print(f'Best Index: {population.best_walker_index}') print(f'Best Fitness: {population.fitnesses[population.best_walker_index]}') print(f'Max Steps: {population.max_steps}') + + rewards.append(population.fitnesses[population.best_walker_index]) + + if population.gen % 10 == 0: + with open("rewards.p", 'wb') as fp: + pickle.dump(rewards, fp) else: population.update() # time.sleep(0.1)