Skip to content

Instantly share code, notes, and snippets.

@gsurma
Last active December 23, 2018 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsurma/e133626f494409ea3f60590383f530c9 to your computer and use it in GitHub Desktop.
Save gsurma/e133626f494409ea3f60590383f530c9 to your computer and use it in GitHub Desktop.
def genetic_evolution(self, env):
print "population_size: " + str(self.population_size) +\
", mutation_rate: " + str(self.mutation_rate) +\
", selection_rate: " + str(self.selection_rate) +\
", random_weight_range: " + str(self.random_weight_range)
population = None
while True:
print('{{"metric": "generation", "value": {}}}'.format(self.generation))
# 1. Selection
parents = self._strongest_parents(population, env)
self._save_model(parents) # Saving main model based on the current best two chromosomes
# 2. Crossover (Roulette selection)
pairs = []
while len(pairs) != self.population_size:
pairs.append(self._pair(parents))
base_offsprings = []
for pair in pairs:
offsprings = self._crossover(pair[0][0], pair[1][0])
base_offsprings.append(offsprings[-1])
# 3. Mutation
new_population = self._mutation(base_offsprings)
population = new_population
self.generation += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment