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

Added mutation factor

parent 9a40da64
No related branches found
No related tags found
1 merge request!1Evaluations
......@@ -31,10 +31,10 @@ class Brain:
clone.directions[i] = copy.copy(self.directions[i])
return clone
# mutates the brain by setting some of the directions to random movements
def mutate(self):
def mutate(self, mutation_factor): # mutates the brain by setting some of the directions to random movements
for i in range(len(self.directions)):
if random.random() < 0.1: # changes roughly 10% of the movements
if random.random() <= mutation_factor:
self.directions[i] = np.random.uniform(-1, 1, 4)
def save(self):
......
......@@ -5,11 +5,13 @@ import matplotlib.pyplot as plt
INCREASE_BY = 5
BRAIN_SIZE = 50
POP_SIZE = 50
MUTATION_FACTOR = 0.1 # 0 <= x <= 1
GAME_CANCELLED = False
LOAD_BRAIN = True
RENDER_BEST = False
if __name__ == '__main__':
population = Population(POP_SIZE, BRAIN_SIZE, LOAD_BRAIN)
population = Population(POP_SIZE, BRAIN_SIZE,MUTATION_FACTOR, LOAD_BRAIN, RENDER_BEST)
while GAME_CANCELLED is False: # this is our game
if population.all_players_finished(): # this is our genetic algorithm after one generation of players
......
......@@ -9,9 +9,10 @@ MAX_STEPS = 1599 # after 1600 steps the Environment gives us a done anyway.
class Population:
def __init__(self, size, brain_size, load_brain):
def __init__(self, size, brain_size, mutation_factor, load_brain, render_best):
self.size = size
self.brain_size = brain_size
self.mutation_factor = mutation_factor
self.fitness_sum = 0.0
self.gen = 1
self.best_walker_index = 0 # index of the best player in self.players
......@@ -21,7 +22,7 @@ class Population:
self.envs = []
self.fitnesses = None
for i in range(self.size):
self.walkers.append(Walker(self.brain_size, load_brain))
self.walkers.append(Walker(self.brain_size, load_brain, render_best))
self.reset_environments()
if load_brain:
self.mutate_babies()
......@@ -89,7 +90,7 @@ class Population:
def mutate_babies(self): # mutates all the brains of the babies
for i in range(1, len(self.walkers)): # we don't want to mutate the champion's brain
self.walkers[i].brain.mutate()
self.walkers[i].brain.mutate(self.mutation_factor)
def set_best_walker(self): # finds the player with the highest fitness and sets it as the best one
max_index = np.argmax(self.fitnesses)
......
......@@ -3,13 +3,14 @@ import gym
class Walker:
def __init__(self, brain_size, load_brain):
def __init__(self, brain_size, load_brain, render_best):
self.brain = Brain(brain_size, load_brain) # new brain with X instructions
self.dead = False
self.reached_goal = False
self.is_best = False # true if this dot is the best dot from the previous generation
self.fitness = 0.0
self.env = gym.make('BipedalWalker-v3')
self.render_best = render_best
# self.pos = copy.copy(self.map.startpoint)
def update(self): # moves the dot according to the brains directions
......@@ -25,7 +26,7 @@ class Walker:
elif done is True:
self.reached_goal = True
self.fitness += 10000000
if self.is_best:
if self.is_best and self.render_best:
self.env.render()
""" def get_fitness(self):
......@@ -40,7 +41,7 @@ class Walker:
self.env.reset()
def get_baby(self):
baby = Walker(0, False)
baby = Walker(0, False, self.render_best)
baby.brain = self.brain.clone() # babies have the same brain as their parents
self.env.close()
return baby
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment