xxxxxxxxxx
133
var rockets=[];
var matingpool=[];
var startx=50;
var starty=0;
var maxspeed=3;
var gravity=2;
var lifespan=120;
var count=0;
var numPop= 20;
var evolution=0;
var maxfit=0;
var mutation=0.10;
var mutateRange=2;
var penalty=1;
var bonus=1;
var hits=0;
var targetx;
var targety;
function setup() {
createCanvas(400, 400);
starty=height-50;
targetx=width-50;
targety=height-50;
for(var i=0; i<numPop; i++)
{
rockets.push(new Rocket());
}
evolution++;
//frameRate(60);
}
function draw() {
background(220);
text("Evolution: " + evolution, 50,50);
text("Hits: " + hits, 50, 60);
circle(targetx, targety, 20);
if(count>lifespan)
{
//rockets=[];
count=0;
maxfit=0;
for(var i=0; i<rockets.length; i++)
{
rockets[i].evaluate();
if(rockets[i].fitness>maxfit)
{
maxfit=rockets[i].fitness;
}
}
for(var i=0; i<rockets.length; i++)
{
rockets[i].fitness/=maxfit;
}
matingpool=[];
for(var i=0; i<rockets.length; i++)
{
var n= rockets[i].fitness*100;
for(var j=0; j<n; j++)
{
matingpool.push(rockets[i]);
}
}
rockets=[];
hits=0;
evolution++;
for(var i=0; i<numPop; i++)
{
var parrentA=random(matingpool);
var parrentB=random(matingpool);
var childDNA=[];
childDNA.push((parrentA.dna[0]+parrentB.dna[0])/2);
childDNA.push((parrentA.dna[1]+parrentB.dna[1])/2);
// Chance of mutation
if(random(0,1)>mutation)
{
childDNA[0]+=(random(mutateRange*-1, mutateRange)/100) * childDNA[0];
}
if(random(0,1)<mutation)
{
childDNA[1]+=(random(mutateRange*-1, mutateRange)/100) * childDNA[0];
}
rockets.push(new Rocket(childDNA[0], childDNA[1]));
}
}
for(var i=0; i<rockets.length; i++)
{
rockets[i].update();
rockets[i].show();
if((dist(rockets[i].x, rockets[i].y, targetx, targety) < 20)&& (rockets[i].success==false))
{
rockets[i].success=true;
hits++;
//console.log("WINNER Evolution:" + evolution);
//evolution=0;
}
}
count++;
}
function mousePressed()
{
//rockets.push(new Rocket(mouseX, mouseY));
}
function fitness()
{
}