Skip to content

Instantly share code, notes, and snippets.

@fedden
Created November 27, 2017 15:08
Show Gist options
  • Save fedden/535d8b7da3f1617cbcd7ab1842caabcd to your computer and use it in GitHub Desktop.
Save fedden/535d8b7da3f1617cbcd7ab1842caabcd to your computer and use it in GitHub Desktop.
# Construct the class
ga = GA(dna_size=2, # Each dna will have size elements
dna_bounds=(-5.11, 5.11), # DNA cannot optimise outside of these bounds
dna_start_position=[3.1, 3.1], # DNA begins randomly around this position
elitism=0.5, # We keep the 50% fittest individuals
population_size=500, # We have 500 genes in the gene pool
mutation_rate=0.01, # Roguhly every 100 dice rolls we mutate
mutation_sigma=0.1, # The gene will mutated at a bound of (-0.1, 0.1)
mutation_decay=0.999, # The mutation size will decay each step
mutation_limit=0.01) # The mutation size will never get smaller than this
# Loop and optimise each step
for optimisation_step in range(amount_optimisation_steps):
# Get the current gene pool
positions = ga.get_solutions()
# Evaluate the genes using a (currently) non-existant evaluation function
fitnesses = [evaluate(pos) for pos in positions]
# Report how well each the genes performed to the
# class, so it can internally optimise.
ga.set_fitnesses(fitnesses)
# Get the current best.
best_position, best_fitness = ga.get_best()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment