📄 simt.c
字号:
fclose(fp); } else{ startGeneration=0; }/******************************************************************** * * The main GA loop for startgeneration -> generations * * */#ifdef GUI sem_wait(&gui_run); /* Test of monitor */ monNet = new Monitor(300,900); monLinks = new Monitor(400,1100); for(ii=0; ii < nN->individs[0]->getNrOfNodes(); ii++){ sprintf(tmpStr,"node %d",ii); monNet->addWatch(tmpStr,nN->individs[0]->getActivationPointer(ii)); for(int iii=0; iii < nN->individs[0]->getNrOfLinks(ii); iii++){ sprintf(tmpStr,"%d,%d",ii,iii); monLinks->addWatch(tmpStr,nN->individs[0]->getLinkWeightPointer(ii,iii)); } } monNet->toggleRulers(); monNet->createMonitorWindow(); monLinks->createMonitorWindow();#endif /* GUI */ /*############################################################REAL###############*/ if(useSerialLine){ /* Real environment */ real = new Real(); real->init(); for(g=startGeneration;g<nrOfGenerations;g++){ if (verboseLevel>0) printf("\nGeneration : %d\n",g); if (verboseLevel>0) printf(" Running amoeba lives \n"); /* Loop trough all individs */ for(n=0; n < nrOfIndivids;n++){ printf(" i%d \n",n); /* Connect ANN output to robot */ annPtr = nN->individs[n];#ifdef GUI for(int opium=0,ii=0; ii < annPtr->getNrOfNodes(); ii++){ monNet->redefineWatch(ii,annPtr->getActivationPointer(ii)); for(int iii=0; iii < annPtr->getNrOfLinks(ii); iii++){ monLinks->redefineWatch(opium,annPtr->getLinkWeightPointer(ii,iii)); opium++; } } monNet->toggleColor(); if(userUpdate){ monLinks->toggleColor(); monLinks->update(); monLinks->update(); monLinks->update(); } #endif /* The main individ loop for epoch=0 -> amoebaepochs */ for (e=0;e<amoebaEpochs;e++){ /**************************************************/ /* we reset activation values to 0 in the ANN */ annPtr->resetNet(); /**************************************************/ /* Let the robot interact with the environment */ /* for nrOfSteps time steps */ for(run = 0; run < nrOfSteps; run++){ /**************************************************/ /* Let the world generate sensor values for */ /* robot with index 0 -> create inputs values to */ /* ANN */ real->setInput(0); usleep(5000); for(int s=0; s < nrOfSensors; s++){ annPtr->setInput(s,input[0][mapping[0][s]]); } /**************************************************/ /* Activate the ANN for individ n */ /* WARNING the input array for the active robot */ /* will be undefined after this call. */ /* HOWEVER the simoutput array will be stable */ annPtr->activateNet(); /**************************************************/ /* Run the robot with the output from the ANN */ /* */ /* If you are going to use a localistic encoding */ /* of the motor actions this is the place to */ /* modify the code. */ /* */ /* runStep expects two values between 0.0 and 1.0 */ /* the first for the left motor and the second for*/ /* the right motor */ i1=annPtr->getOutput(0); i2=annPtr->getOutput(1); real->runStep(i1,i2,0); // robot 0 usleep(5000); #ifdef GUI if(gfxUpdate) monNet->update();#endif /* GUI */ /**************************************************/ /* If you want to insert other things that the net*/ /* should modify (send it output values to) this */ /* would be a great place do so */ } /* End of time loop */ } /* End of epoch loop */ real->nextIndivid(); } /* End of individ loop */ } /* End of generation loop */ } /*############################################################SIMULATED##########*/ else{/* Simulated environment*/ for(g=startGeneration;g<nrOfGenerations;g++){ if (verboseLevel>0) printf("\nGeneration : %d\n",g); if (verboseLevel>0) printf(" Running amoeba lives\n"); /* Loop trough all individs */ for(n=0; n < nrOfIndivids;n++){ /* * Connect ANN output to robot * */ annPtr = nN->individs[n];#ifdef GUI for(int opium=0,ii=0; ii < annPtr->getNrOfNodes(); ii++){ monNet->redefineWatch(ii,annPtr->getActivationPointer(ii)); for(int iii=0; iii < annPtr->getNrOfLinks(ii); iii++){ monLinks->redefineWatch(opium,annPtr->getLinkWeightPointer(ii,iii)); opium++; } } monNet->toggleColor(); if(userUpdate){ monLinks->toggleColor(); monLinks->update(); monLinks->update(); monLinks->update(); }#endif /* The main individ loop for epoch=0 -> amoebaepochs */ for (e=0;e<amoebaEpochs;e++){ /* Reset robots, new startpos and such thingis */ /* make sure no objects are taken anymore */ /* if GUI redraw world */ world->resetEnvironment(); /**************************************************/ /* we reset activation values to 0 in the ANN */ annPtr->resetNet(); fitness = 0; /**************************************************/ /* We want to be able to see where the robot was*/ /* in the beginning. So save current status */ world->robot[0]->saveState(); startPx=world->robot[0]->getX(); startPy=world->robot[0]->getY(); /**************************************************/ /* Let the robot interact with the environment */ /* for nrOfSteps time steps */ for(run = 0; run < nrOfSteps; run++){ /**************************************************/ /* Let the world generate sensor values for */ /* robot with index 0 -> create inputs values to */ /* ANN */ world->setInput(0); for(int s=0; s < nrOfSensors; s++){ annPtr->setInput(s,input[0][mapping[0][s]]); } /**************************************************/ /* Activate the ANN for individ n */ /* WARNING the input array for the active robot */ /* will be undefined after this call. */ /* HOWEVER the simoutput array will be stable */ annPtr->activateNet(); /**************************************************/ /* Run the robot with the output from the ANN */ /* */ /* If you are going to use a localistic encoding */ /* of the motor actions this is the place to */ /* modify the code. */ /* */ /* runStep expects two values between 0.0 and 1.0 */ /* the first for the left motor and the second for*/ /* the right motor */ i1=annPtr->getOutput(0); i2=annPtr->getOutput(1); world->runStep(i1,i2,0); // robot 0 #ifdef GUI if(gfxUpdate) monNet->update(); if(gui_stepping) sem_wait(&gui_step);#endif /* GUI */ /**************************************************/ /* If you want to insert other things that the net*/ /* should modify (send it output values to) this */ /* would be a great place to do so */ /**************************************************/ /* Insert your fitness function here */ /**************************************************/ if(world->checkCrash(0)){ /* He messed up we dont want him... */ run=nrOfSteps; /* so we "end" his life */ } if(ffitness==1){ fitness += world->robot[0]->mot1 + world->robot[0]->mot2; } if(ffitness==2){ // world->inZone(0); /* this occures in setInput when we have a ground sensor*/ fitness += world->robot[0]->mot1 + world->robot[0]->mot2; } /**************************************************/ /* We want to be able to see where the robot was */ /* the last time step. So save current status */ world->robot[0]->saveState(); } /* End of time loop */ /**************************************************/ /* Set/(add) fitness for individ(n) and epoch(e) */ /* with new fitness(fitness) */ if(ffitness==1){ /* euclidian distance between start and stop*/ fitness += g_distPoint(startPx,startPy, world->robot[0]->getX(),world->robot[0]->getY())*10000; fitness = fitness / 100000; } else if(ffitness==2){ fitness += 1000 * world->zonesVisited(0); world->resetZones(); } nN->setFitness(n,e,fitness); } /* End of epoch loop */ } /* End of individ loop */ if(verboseLevel>0) printf(" Making mutations\n"); if(verboseLevel>0) printf(" Best results : "); /* If last, first or saveGeneration then save weights */ if (g%saveGeneration == 0 || g+1 == nrOfGenerations){ sprintf(tmpStr,"%s/%s%d.wts",logPath,weightsFile,g); if ((fp=fopen(tmpStr, "w")) == NULL){ printf("I can't open weight file %s for writing\n",tmpStr); exit(1); } nN->saveAllIndividsWeights(fp); fflush(fp); fclose(fp); } /* Save fitness results and close file */ if (g <nrOfGenerations){ sprintf(tmpStr,"%s/%s",logPath,fitnessFile); if ((fp=fopen(tmpStr, "a")) == NULL){ printf("I can't open fitness log file %s for appending",tmpStr); exit(1); } nN->saveBestIndividsFitness(nrOfIndividsToLog,fp,verboseLevel); fclose(fp); } /*********************************************************/ /* We create the next generation (live long and prosper) */ /* and reset all old fitness. */ /* If you want to use a new selection method change */ /* add a case with your selection method (document it in */ /* test.opt) */ switch(selectionMethod){ case 0: nN->createNewGenerationWithWeightMutation(nrOfFathers,nrOfKids,bitMutateProbability); break; case 1: nN->tournamentSelectionWithMutation(bitMutateProbability); break; default: nN->createNewGenerationWithWeightMutation(nrOfFathers,nrOfKids,bitMutateProbability); break; } nN->resetFitness(); } }#ifdef GUI monLinks->deleteMonitorWindow(); monNet->deleteMonitorWindow(); delete(monLinks); delete(monNet);#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -