O'Reilly logo

Genetic Algorithms and Machine Learning for Programmers by Frances Buontempo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Did It Work?

You can run the genetic algorithm like this:

 def​ ​fire​():
  epochs = 10
  items = 12
  height = 5
  width = 10
 
  generation = random_tries(items)
  generation0 = list(generation) ​# save to contrast with last epoch
 
 for​ i ​in​ range(1, epochs):
  results = []
  generation = crossover(generation, width)
  mutate(generation)
 
  display_start_and_finish(generation0, generation, height, width)

What you do in the display function is up to you. Plotting plots the arcs of the cannonballs from the first and last generation, so shows any improvement on the initial random attempts. There are other ways to assess your solutions. Counting discusses a few things worth counting at each epoch to see if the GA is learning. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required