Skip to content
Snippets Groups Projects
Commit 1e690422 authored by Tobias Döring's avatar Tobias Döring
Browse files

Added plotting of rewards

parent 7ef0794a
Branches
No related tags found
No related merge requests found
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment