Skip to content
Snippets Groups Projects
Commit d94e96c2 authored by Philip Maas's avatar Philip Maas
Browse files

Added functionality to load and save a brain

parent 3a796e17
Branches
No related tags found
1 merge request!1Evaluations
import numpy as np import numpy as np
import random import random
import copy import copy
import pickle
class Brain: class Brain:
def __init__(self, size): def __init__(self, size, load_brain):
self.directions = [] self.directions = []
self.step = 0 self.step = 0
if load_brain:
self.load()
else:
self.increase_moves(size) self.increase_moves(size)
def get_move(self): def get_move(self):
...@@ -22,7 +26,7 @@ class Brain: ...@@ -22,7 +26,7 @@ class Brain:
# returns a copy of the given brain # returns a copy of the given brain
def clone(self): def clone(self):
clone = Brain(len(self.directions)) clone = Brain(len(self.directions), False)
for i in range(len(self.directions)): for i in range(len(self.directions)):
clone.directions[i] = copy.copy(self.directions[i]) clone.directions[i] = copy.copy(self.directions[i])
return clone return clone
...@@ -33,8 +37,15 @@ class Brain: ...@@ -33,8 +37,15 @@ class Brain:
if random.random() < 0.1: # changes roughly 10% of the movements if random.random() < 0.1: # changes roughly 10% of the movements
self.directions[i] = np.random.uniform(-1, 1, 4) self.directions[i] = np.random.uniform(-1, 1, 4)
def save(self):
with open('best_brain', 'wb') as fp:
pickle.dump(self.directions, fp)
def load(self):
with open('best_brain', 'rb') as fp:
self.directions = pickle.load(fp)
if __name__ == '__main__': # for debugging if __name__ == '__main__': # for debugging
brain_inst = Brain(100) brain_inst = Brain(100, True)
print(brain_inst.directions) print(brain_inst.directions)
print(len(brain_inst.directions)) print(len(brain_inst.directions))
import gym
from population import Population from population import Population
import time import time
import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
INCREASE_BY = 5 INCREASE_BY = 5
BRAIN_SIZE = 50 BRAIN_SIZE = 50
POP_SIZE = 50 POP_SIZE = 50
GAME_CANCELLED = False GAME_CANCELLED = False
LOAD_BRAIN = True
if __name__ == '__main__': if __name__ == '__main__':
population = Population(POP_SIZE, BRAIN_SIZE) population = Population(POP_SIZE, BRAIN_SIZE, LOAD_BRAIN)
while GAME_CANCELLED is False: # this is our game while GAME_CANCELLED is False: # this is our game
if population.all_players_finished(): # this is our genetic algorithm after one generation of players if population.all_players_finished(): # this is our genetic algorithm after one generation of players
......
import numpy as np import numpy as np
from walker import Walker
import gym
import random import random
import logging import logging
import copy import copy
from walker import Walker
MAX_STEPS = 10000 MAX_STEPS = 1599 # after 1600 steps the Environment gives us a done anyway.
class Population: class Population:
def __init__(self, size, brain_size): def __init__(self, size, brain_size, load_brain):
self.size = size self.size = size
self.brain_size = brain_size self.brain_size = brain_size
self.fitness_sum = 0.0 self.fitness_sum = 0.0
...@@ -23,13 +21,14 @@ class Population: ...@@ -23,13 +21,14 @@ class Population:
self.envs = [] self.envs = []
self.fitnesses = None self.fitnesses = None
for i in range(self.size): for i in range(self.size):
self.envs.append(gym.make('BipedalWalker-v3')) self.walkers.append(Walker(self.brain_size, load_brain))
self.walkers.append(Walker(self.envs[i], self.brain_size))
self.reset_environments() self.reset_environments()
if load_brain:
self.mutate_babies()
def reset_environments(self): def reset_environments(self):
for env in self.envs: for walker in self.walkers:
env.reset() walker.reset_environment()
def update(self): def update(self):
for walker in self.walkers: for walker in self.walkers:
...@@ -62,7 +61,7 @@ class Population: ...@@ -62,7 +61,7 @@ class Population:
#new_walkers.append(Walker(self.envs[i], self.brain_size)) #new_walkers.append(Walker(self.envs[i], self.brain_size))
self.calculate_fitness_sum() self.calculate_fitness_sum()
self.set_best_walker() self.set_best_walker()
self.walkers[self.best_walker_index].brain.save()
# the champion lives on # the champion lives on
new_walkers = [self.walkers[self.best_walker_index].get_baby()] new_walkers = [self.walkers[self.best_walker_index].get_baby()]
new_walkers[0].is_best = True new_walkers[0].is_best = True
......
from brain import Brain from brain import Brain
import gym
class Walker: class Walker:
def __init__(self, env, brain_size): def __init__(self, brain_size, load_brain):
self.brain = Brain(brain_size) # new brain with X instructions self.brain = Brain(brain_size, load_brain) # new brain with X instructions
self.dead = False self.dead = False
self.reached_goal = False self.reached_goal = False
self.is_best = False # true if this dot is the best dot from the previous generation self.is_best = False # true if this dot is the best dot from the previous generation
self.fitness = 0.0 self.fitness = 0.0
self.env = env self.env = gym.make('BipedalWalker-v3')
# self.pos = copy.copy(self.map.startpoint) # self.pos = copy.copy(self.map.startpoint)
def update(self): # moves the dot according to the brains directions def update(self): # moves the dot according to the brains directions
...@@ -36,7 +36,11 @@ class Walker: ...@@ -36,7 +36,11 @@ class Walker:
self.fitness = 1 / (self.map.get_closest_distance(self.pos[X], self.pos[Y]) ** 2) self.fitness = 1 / (self.map.get_closest_distance(self.pos[X], self.pos[Y]) ** 2)
return self.fitness""" return self.fitness"""
def reset_environment(self):
self.env.reset()
def get_baby(self): def get_baby(self):
baby = Walker(self.env, 0) baby = Walker(0, False)
baby.brain = self.brain.clone() # babies have the same brain as their parents baby.brain = self.brain.clone() # babies have the same brain as their parents
self.env.close()
return baby return baby
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment