/* * The call to wb_robot_step is mandatory for the function * supervisor_node_was_found to be able to work correctly. */ static void reset(void) { int i; for(i=0; i<POP_SIZE; i++) fitness[i]=-1; srand(time(0)); pF1= fopen("initPop.txt","w+"); //pF2= fopen("real2IntGenome.txt","w+"); //pF3= fopen("encodedPop.txt","w+"); // Initiate the emitter used to send genomes to the experiment emitter = wb_robot_get_device("emittersupervisor"); // Initiate the receiver used to receive fitness receiver = wb_robot_get_device("receiversupervisor"); wb_receiver_enable(receiver, TIME_STEP); // Create a supervised node for the robot robot = wb_supervisor_node_get_from_def("EPUCK"); assert(robot!=NULL); pattern=wb_supervisor_node_get_from_def("FLOOR"); pattern_rotation = wb_supervisor_node_get_field(pattern,"rotation"); assert(pattern!=NULL); assert(pattern_rotation!=NULL); trans_field = wb_supervisor_node_get_field(robot,"translation"); rot_field = wb_supervisor_node_get_field(robot,"rotation"); ctrl_field= wb_supervisor_node_get_field(robot,"controller"); wb_robot_step(0); wb_robot_step(0); //this is magic // set the robot controller to nn const char *controller_name = "drive2"; wb_supervisor_field_set_sf_string(ctrl_field,controller_name); //check whether robot was found if (wb_supervisor_node_get_type(robot) == WB_NODE_NO_NODE) puts("Error: node EPUCK not found!!!"); if(EVOLVING) { //Open log files f1= fopen ("../../data/fitness.txt", "wt"); f2= fopen ("../../data/genomes.txt", "wt"); //pR= fopen("savePop.txt","w+"); //initial weights randomly initializePopulation(); //rtoi(); //real to int conversion before encoding popEncoder(); //puts("NEW EVOLUTION"); //puts("GENERATION 0"); // send genomes to experiment resetRobotPosition(); wb_emitter_send(emitter, (void *)pop_bin[evaluated_inds], GENOME_LENGTH*sizeof(_Bool)); //instead save binary genomes to a file //puts("Genes sent."); } else { // testing best individual // Read best genome from bestgenome.txt and initialize weights. f3= fopen ("../../data/bestgenome.txt", "rt"); fscanf(f3,"%d %d", &generation, &evaluated_inds); //TODO either read binary genome or make sure it is decoded prior to storage for(i=0;i<GENOME_LENGTH;i++) fscanf(f3,"%d ",&pop_bin[0][i]); //printf("TESTING INDIVIDUAL %d, GENERATION %d\n", evaluated_inds, generation); // send genomes to experiment resetRobotPosition(); // wb_emitter_send(emitter, (void *)pop[0], NB_GENES*sizeof(double)); } return; }
static void set_controller(int robot, const char *controller) { if (robot_controller[robot]) wb_supervisor_field_set_sf_string(robot_controller[robot], controller); }