A skeleton version of the programs will appear when you run
update63 in a terminal window.
A genetic algorithm is an abstract simulation of some of the key ideas from biological evolution. A population of candidate solutions, where each candidate solution is represented as a series of 1's and 0's, is randomly generated. Then evolution proceeds as a sequence of generations. Each generation, the fitness of every candidate solution in the population is determined. Next, parents are selected from the population based on their fitness. With some probability, a crossover operation is done on these parents. Crossover is a simple model of sexual reproduction that recombines the genetic material of two parents to produce two children. Finally, with some low probability, mutation may occur on the children's' genetic material. In this way, a new population is produced on each generation. Over many generations, the average fitness of the population tends to increase and more highly fit candidate solutions are created.
In this lab you will be given a skeleton definition of a GeneticAlgorithm class that you will complete. Then you will test the completed genetic algorithm on a simple problem of finding candidate solutions that maximize the sum of their values.
Once you are convinced that the genetic algorithm is working properly, you will then apply it to the more interesting problem of evolving symmetrical grid-based images. An example is shown below.
Open the ga.py file that was copied into your cs63/labs/4 directory when you did an update63. Read through the methods that you will need to implement. Recall that there are three main operators used in a genetic algorithm: selection, crossover, and mutation. Below is a review of how they should function.
compute totalFitness of population spin = random number between 1 and the totalFitness partialFitness = 0 for each individual in the population: partialFitness += fitness(individual) if partialFitness >= spin: return individual
0 1 2 3 4 5 6 7 parent1: [1, 1, 1, 0, 0, 0, 0, 0] parent2: [0, 1, 0, 1, 0, 1, 0, 1] ^ crossPoint: 3 child1: [1, 1, 1, 1, 0, 1, 0, 1] child2: [0, 1, 0, 0, 0, 0, 0, 0]Test that your crossover method is working properly by printing out the parents, the selected crossover point, and the resulting children.
child before mutation: [1, 1, 1, 1, 0, 1, 0, 1] child after mutation: [1, 0, 1, 1, 0, 1, 0, 1]Because the child is represented as a list, any change you make will be permanent, so it does not need to be returned from the method.
Once you have implemented all of the class methods, test whether you can evolve a solution of all 1's using the class called SumGA provided at the bottom of the ga.py file.
If you cannot consistently evolve a solution of all 1's, then there may be a problem with your genetic operators or with your methods that control the evolutionary process.
Before you begin evolving images, you'll need to understand how to use the Grid class to draw two-dimensional, grid-based images.
To construct a Grid object, you'll need to pass the desired number of columns and rows as parameters. The origin (0,0) is located in the bottom left corner. Once you have a grid object you can set the color of particular cells as well as set the title. For example, try the following code to produce the picture shown below.
from Grid import * g = Grid(10,10) g.setTitle("diagonals") for i in range(g.getWidth()): g.setBlack(i, i) for i in range(g.getWidth()): g.setWhite(i, g.getWidth()-i-1) g.update() g.done()
Notice that you must call the update method to actually make the window update its color values.
Once you are comfortable using the Grid class and its methods, you can move on to the next section.
Using the SumGA class given in the ga.py file as a model, complete the implementation of the ImageGA class given in the imageGA.py file. Your goal is to create a fitness function that can evolve images that are symmetrical across the vertical axis. Another example is shown below.
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]would represent this 4x4 image, where the the origin is in the lower-left corner:
Once you have successfully created a GA that can evolve images with vertical symmetry, feel free to try other ideas, such as:
In comments, be sure to give a clear explanation of your fitness function and your goal for the evolutionary process.
Once you are satisfied with your program, hand it in by typing handin63 in a terminal window.