float objective(GAGenome & c) { GA2DBinaryStringGenome & genome = (GA2DBinaryStringGenome &)c; short **pattern = (short **)c.userData(); float value=0.0; for(int i=0; i<genome.width(); i++) for(int j=0; j<genome.height(); j++) value += (float)(genome.gene(i,j) == pattern[i][j]); return(value); }
/** * The objective is the distance */ float Objective(GAGenome & g) { double x1, y1, z1; double x2, y2, z2; GeneticExperience *experience = (GeneticExperience *)g.userData(); if (experience == NULL) { cout << "Score: unable to find the experience" << endl; return 0; } cout << "Scoring... " << flush; experience->updateSplines(g); VREPClient &vrep = experience->getVrep(); //Main Loop vrep.start(); x1 = vrep.readPositionTrackerX(); y1 = vrep.readPositionTrackerY(); z1 = vrep.readPositionTrackerZ(); for (double t=0; t<SIMULATION_TIME; t+=0.02) { //Display state //Do next step vrep.nextStep(); //Compute motors move primitive_step(vrep, experience->getSplines()); } x2 = vrep.readPositionTrackerX(); y2 = vrep.readPositionTrackerY(); z2 = vrep.readPositionTrackerZ(); //End simulation vrep.stop(); double score = sqrt(pow(x1-x2,2)+pow(y1-y2,2)+pow(z1-z2,2)); cout << score << endl; return score; }