// Genetic Algorithm, Evolving Shakespeare // Daniel Shiffman // Demonstration of using a genetic algorithm to perform a search // setup() // # Step 1: The Population // # Create an empty population (an array or ArrayList) // # Fill it with DNA encoded objects (pick random values to start) // draw() // # Step 1: Selection // # Create an empty mating pool (an empty ArrayList) // # For every member of the population, evaluate its fitness based on some criteria / function, // and add it to the mating pool in a manner consistant with its fitness, i.e. the more fit it // is the more times it appears in the mating pool, in order to be more likely picked for reproduction. // # Step 2: Reproduction Create a new empty population // # Fill the new population by executing the following steps: // 1. Pick two "parent" objects from the mating pool. // 2. Crossover -- create a "child" object by mating these two parents. // 3. Mutation -- mutate the child's DNA based on a given probability. // 4. Add the child object to the new population. // # Replace the old population with the new population // // # Rinse and repeat PFont f,fb; String phrase; int popmax; float mutationRate; Population popul; static int numpoints = 566; void setup() { size(800,600); fb = createFont("Courier",24,true); f = createFont("Arial",12,true); char[] desiredstate = new char[numpoints]; for(int i=0; i < numpoints; i++) desiredstate[i]='T'; phrase = new String(desiredstate); popmax = 10000; mutationRate = 0.001f; // Create a population with a target phrase, mutation rate, and population max popul = new Population(phrase,mutationRate,popmax); } void draw() { // Generate mating pool popul.naturalSelection(); // Create next generation popul.generate(); // Calculate fitness popul.calcFitness(); displayInfo(); // If we found the target phrase, stop if (popul.finished()) { noLoop(); } } void displayInfo() { background(255); // Display current status of population String answer = popul.getBest(); textFont(fb); textAlign(LEFT); fill(0); for(int i=0; i < (numpoints/50); i++) text(answer.substring(i*50, i*50+49),20,100+25*i); if(numpoints%50!=0) text(answer.substring(answer.length()-numpoints%50), 20, 100+(numpoints/50)*25); textFont(f); int vertloc=500; text("total generations: " + popul.getGenerations(),20,vertloc); text("average fitness: " + nf(popul.getAverageFitness(),0,2),20,vertloc+15);//155); text("total population: " + popmax,20,vertloc+30);//170); text("mutation rate: " + mutationRate * 100 + "%",20,vertloc+45);//185); }