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. ...

Get Genetic Algorithms and Machine Learning for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.