creature bestCreature; //a pointer to the best creature creature tcreature; //a pointer to the currently tested creature int bestCreatureID; //the index number for the best creature float bestFitness=0; //the fitness value of the currently best creature int sampleAmount=150; //the initial sample of creatures tested creature clist[]=new creature[sampleAmount]; //the list of creatures being tested int maxTime=500; //maximum amount of steps to test a creature with int steps=0; //the current step of simulation int cc=0; //id tag of current creature being tested int mutations=0; int maxMutations=100; String messageBuffer[]=new String[36]; void initTextBuffer() { for(int i=0;i0&&bestCreature!=null) { position p=new position(width/2,height-35); textFont(uiFont,15); textMode(ALIGN_CENTER); fill(180); text("current best fitness: "+bestFitness,p.x,p.y); text("creature average speed: "+bestCreature.avgSpeed+"px/frm",p.x,p.y+kern); text("press 'q' to see creature",p.x,p.y+kern*2); textMode(ALIGN_LEFT); } } void evolution() { if(mutations>maxMutations) { simulationMode=true; //once all the testing is over, we see the result if(bestCreature==null) //this means no good creatures were found... currentCreature=tcreature; else { float success=0; for(int i=0;i0) success++; } float ratio=success/clist.length*100; currentCreature=bestCreature; //this means we found a good creature // println("best creature fitness: "+bestCreature.fitness); // println("simulation success rate: "+ratio+"%"); currentCreature.rebuild(); } } else if(cc>=clist.length) { cc=0; mutations++; clist[0]=new creature(bestCreature); int amt=int(clist.length*.2); // println("generating "+amt+" mutations.."); toBuffer("generating "+amt+" mutations.."); for(int i=1;i50&&tcreature.distanceTravelled<1) { // println(" creature is inactive (dt: "+tcreature.distanceTravelled+")"); toBuffer(" creature is inactive (dt: "+tcreature.distanceTravelled+")"); curFitness=0; //immediately kill creatures that are not moving i=4; break; } if(t>100) { // lightSource=lightSource.displace(getHeading(lightSource,lightDest),5); } if(curFitness<-100) { // println(" creature is going the wrong way"); // i=4; // break; } tcreature.update(lightSource); //update the creature per time step curFitness=fitness(tcreature,t,lightSource); //evaluate the creature's fitness at every time step } switch(i) { case 0: // println(" trial 1 fitness: "+curFitness); toBuffer(" trial 1 fitness: "+curFitness); // println(" distance to target: "+dist(tcreature.p,lightSource)); TF1=curFitness; break; case 1: println(" trial 2 fitness: "+curFitness); println(" distance to target: "+dist(tcreature.p,lightSource)); TF2=curFitness; break; case 2: println(" trial 3 fitness: "+curFitness); println(" distance to target: "+dist(tcreature.p,lightSource)); TF3=curFitness; break; case 3: println(" trial 4 fitness: "+curFitness); println(" distance to target: "+dist(tcreature.p,lightSource)); TF4=curFitness; break; } if(curFitness0) display(bestCreature); pop(); push(); translate(0-(width),0); if(cc-1>=0) display(clist[cc-1]); pop(); push(); if(cc+1bestFitness) { bestFitness=clist[i].fitness; bestCreature=clist[i]; // println(" winner found. new best fitness: "+bestFitness); toBuffer(" winner found. new best fitness: "+bestFitness); } } } cc++; }