// A class to describe a population of lines class Population { int MAX=9; //population maximum float mutationRate; //mutation rate Line[] population; //arraylist to hold the current population ArrayList darwin; //ArrayList which we will use for our "mating pool" int generations; //number of generations float[] fitness =new float[9]; int x_; int y_; int rr,gg,bb; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*initialize the population*// Population(float m,DNA[] d) { mutationRate = m; population = new Line[MAX]; darwin = new ArrayList(); generations = 1; for(int i=0;i250){ if(mousePressed==true) { if(mouseButton== LEFT){ if(mouseX<260&&mouseX>240) { strokeWeight(3); population[0].plot(); } if(mouseX<300&&mouseX>280) { strokeWeight(3); population[1].plot(); } if(mouseX<340&&mouseX>320) { strokeWeight(3); population[2].plot(); } if(mouseX<380&&mouseX>360) { strokeWeight(3); population[3].plot(); } if(mouseX<420&&mouseX>400) { strokeWeight(3); population[4].plot(); } if(mouseX<460&&mouseX>440) { strokeWeight(3); population[5].plot(); } if(mouseX<500&&mouseX>480) { strokeWeight(3); population[6].plot(); } if(mouseX<540&&mouseX>520) { strokeWeight(3); population[7].plot(); } if(mouseX<580&&mouseX>560) { strokeWeight(3); population[8].plot(); } } } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////generate a mating pool void naturalSelection() { /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////clear the ArrayList darwin.clear(); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Calculate total fitness of whole population float totalFitness = getTotalFitness(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Calculate *normalized* fitness for each member of the population ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////based on normalized fitness, each member will get added to the mating pool a certain number of times a la roulette wheel ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////a higher fitness = more entries to mating pool = more likely to be picked as a parent //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////a lower fitness = fewer entries to mating pool = less likely to be picked as a parent for (int i = 0; i < population.length; i++) { float fitnessNormal = population[i].getFitness() / totalFitness; int n = (int) (fitnessNormal * 100.0f); //int n = (int) (fitnessNormal); for (int j = 0; j < n; j++) { darwin.add(population[i]); } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*creat a new generation**// void generate() { ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////refill the population with children from the mating pool for (int i=0; i < population.length;i++) { int m = int(random(darwin.size())); int d = int(random(darwin.size())); //pick two parents Line mom = (Line) darwin.get(m); Line dad = (Line) darwin.get(d); //get their genes DNA momgenes = mom.getGenes(); DNA dadgenes = dad.getGenes(); //mate their genes DNA child = momgenes.mate(dadgenes); //mutate their genese child.mutate(mutationRate); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////fill the new population with the new child? if(i==0) { x_=51; y_=290; rr=236;gg=27;bb=35; } if(i==1) { x_=51+420; y_=290; rr=255;gg=241;bb=0; } if(i==2) { x_=51+420*2; y_=290; rr=40;gg=180;bb=114; } if(i==3) { x_=51; y_=490; rr=0;gg=173;bb=238; } if(i==4) { x_=51+420; y_=490; rr=46;gg=48;bb=145; } if(i==5) { x_=51+420*2; y_=490; rr=235;gg=0;bb=139; } if(i==6) { x_=51; y_=690; rr=144;gg=38;bb=142; } if(i==7) { x_=51+420; y_=690; rr=246;gg=146;bb=29; } if(i==8) { x_=51+2*420; y_=690; rr=200;gg=200;bb=200; } population[i] = new Line(child,x_,y_,rr,gg,bb,0.6); } for (int i = 0; i < population.length; i++) { saving[i]=""; } generations++; } int getGenerations() { return generations; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////compute total fitness for the population float getTotalFitness() { float total = 0; for (int i = 0; i < population.length; i++) { total += population[i].getFitness(); } return total; } }