class Creature { //** THIS STUFF IS COMING FROM VECTOR3D CLASS**// Vector3D loc; Vector3D vel; Vector3D acc; float r; //**FITNESS AND DNA**// float fitness; DNA genes; boolean stopped; // understand whether we are stucked. int finish; // what was my finish place //CONSTRUCTOR Creature(Vector3D l, DNA dna, int f) { acc = new Vector3D(0.0,0.0,0.0); // acceleration vel = new Vector3D(0.0,0.0,0.0); // velocity loc = l.copy(); // println(loc); r = 20.0f; // unit we use in building our creature. genes = dna; stopped = false; finish = f; } // **** FITNESS FUNCTION **** // // distance = distance from target // finish = what order did I finish // f(distance, finish) = (1.0f / finish^1.5)*(1.0f / distance^6); // a lower finish is rewarded (exponentially) and/or shorter distance to target void calcFitness() { float d = v3d.distance(loc,target); // distance between the creature and target if(d < diam /2) { // if they are so close to each other, stay where it is d = 10.0f; } // reward finishing faster and getting closer fitness = (1.0f / pow(finish, 1.5f)) * (1.0f / (pow(d,6))); } void setFinish(int f) { finish = f; } // run in relation to all the obstacle // if I am stuck don't bother updating or checking for intersection void run(ArrayList o) { if(!stopped) { update(); // if I hit an edge or an obstacle if ((borders())) { stopped = true; } } //draw me render(); } // EDGES FUNCTION boolean borders() { if((loc.x() < 0) || (loc.y() < 0) || (loc.y() > width) || (loc.y() > height)) { return true; } else { return false; } } // DID I MAKE IT TO THE TARGET** // boolean finished() { float d = v3d.distance(loc,target); if(d< diam/2) { stopped = true; return true; } return false; } void update() { if(!finished()) { // where are we? Our location will tell us what steering vector // to look up in our DNA int x = (int) loc.x()/gridscale; int y = (int) loc.y()/gridscale; x = constrain(x,0,width/gridscale-1); // make sure we are not off the edge y = constrain(y,0,height/gridscale-1); /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// //Get the steering vector out of our genes in the right spot acc.add(genes.getGene(x+y*width/gridscale)); /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// // println(genes.getGene(x+y*width/gridscale)); // this is the stuff from vector3D acc.mult(maxforce); vel.add(acc); vel.limit(maxspeed); loc.add(vel); acc.setXYZ(0,0,0); } } void render() { //draws a triangle rotated in the direction of velocity float theta = vel.heading2D() + radians(180); if(stopped) fill(255,25); else fill(255); noStroke(); pushMatrix(); translate(loc.x(),loc.y()); rotateZ(theta); fill(100); fill(100,0,0,random(40)); beginShape(TRIANGLES); vertex(0, -1*r); vertex(-2*r, 0); vertex(0, 1*r); endShape(); popMatrix(); fill(100,0,0,10); pushMatrix(); translate(loc.x()+5,loc.y()+5); ellipse(-r,-r,10,10); popMatrix(); } float getFitness() { return fitness; } DNA getGenes() { return genes; } boolean stopped() { return stopped; } }