48 unsigned long long rdtsc(){
50 __asm__ __volatile__ (
"rdtsc" :
"=a" (lo),
"=d" (hi));
51 return ((
unsigned long long)hi << 32) | lo;
56 AnnealEvolution::AnnealEvolution(std::string suff, std::string config, std::string path) :
73 std::string configPath = resourcePath + config;
76 myconfigdataaa.readFile(configPath);
77 populationSize=myconfigdataaa.getintvalue(
"populationSize");
78 numberOfElementsToMutate=myconfigdataaa.getintvalue(
"numberOfElementsToMutate");
79 numberOfTestsBetweenGenerations=myconfigdataaa.getintvalue(
"numberOfTestsBetweenGenerations");
80 numberOfSubtests=myconfigdataaa.getintvalue(
"numberOfSubtests");
81 numberOfControllers=myconfigdataaa.getintvalue(
"numberOfControllers");
82 leniencyCoef=myconfigdataaa.getDoubleValue(
"leniencyCoef");
83 coevolution=myconfigdataaa.getintvalue(
"coevolution");
84 seeded = myconfigdataaa.getintvalue(
"startSeed");
86 bool learning = myconfigdataaa.getintvalue(
"learning");
91 for(
int j=0;j<numberOfControllers;j++)
99 for(
int i = 0; i < numberOfControllers; i++)
103 ss<< resourcePath <<
"logs/bestParameters-"<<this->suffix<<
"-"<<i<<
".nnw";
104 seededPop->loadFromFile(ss.str().c_str());
109 evolutionLog.open((resourcePath +
"logs/evolution" + suffix +
".csv").c_str(),ios::out);
110 if (!evolutionLog.is_open())
112 throw std::runtime_error(
"Logs does not exist. Please create a logs folder in your build directory or update your cmake file");
117 AnnealEvolution::~AnnealEvolution()
121 for(std::size_t i = 0; i < populations.size(); i++)
123 delete populations[i];
129 void AnnealEvolution::mutateEveryController()
131 for(std::size_t i=0;i<populations.size();i++)
133 populations.at(i)->mutate(&eng,numberOfElementsToMutate, Temp);
139 void AnnealEvolution::orderAllPopulations()
142 double aveScore1 = 0.0;
143 double aveScore2 = 0.0;
146 double maxScore1,maxScore2;
148 for(std::size_t i=0;i<scoresOfTheGeneration.size();i++)
150 aveScore1+=scoresOfTheGeneration[i][0];
151 aveScore2+=scoresOfTheGeneration[i][1];
153 aveScore1 /= scoresOfTheGeneration.size();
154 aveScore2 /= scoresOfTheGeneration.size();
157 for(std::size_t i=0;i<populations.size();i++)
159 populations.at(i)->orderPopulation();
161 evolutionLog<<generationNumber*numberOfTestsBetweenGenerations<<
","<<aveScore1<<
","<<aveScore2<<
",";
162 evolutionLog<<populations.at(0)->getMember(0)->maxScore<<
","<<populations.at(0)->getMember(0)->maxScore1<<
","<<populations.at(0)->getMember(0)->maxScore2<<endl;
168 ofstream logfileLeader;
169 for(std::size_t i=0;i<populations.size();i++)
172 ss <<
resourcePath <<
"logs/bestParameters-" << suffix <<
"-" << i <<
".nnw";
174 populations[i]->getMember(0)->saveToFile(ss.str().c_str());
179 double diffclock(clock_t clock1,clock_t clock2)
181 double diffticks=clock1-clock2;
182 double diffms=(diffticks*10)/CLOCKS_PER_SEC;
187 vector <AnnealEvoMember *> AnnealEvolution::nextSetOfControllers()
191 testsToDo=numberOfTestsBetweenGenerations;
193 testsToDo=populationSize;
195 if(currentTest == testsToDo)
197 orderAllPopulations();
198 mutateEveryController();
201 this->scoresOfTheGeneration.clear();
206 currentTest=populationSize-numberOfElementsToMutate;
209 selectedControllers.clear();
210 for(std::size_t i=0;i<populations.size();i++)
214 selectedOne=rand()%populationSize;
216 selectedOne=currentTest;
219 selectedControllers.push_back(populations.at(i)->getMember(selectedOne));
224 if (subTests == numberOfSubtests)
231 return selectedControllers;
234 void AnnealEvolution::updateScores(vector <double> multiscore)
236 if(multiscore.size()==2)
237 this->scoresOfTheGeneration.push_back(multiscore);
239 multiscore.push_back(-1.0);
240 double score=1.0* multiscore[0] - 0.0 * multiscore[1];
244 payloadLog.open((
resourcePath +
"logs/scores.csv").c_str(),ios::app);
245 payloadLog<<multiscore[0]<<
","<<multiscore[1];
247 for(std::size_t oneElem=0;oneElem<selectedControllers.size();oneElem++)
251 controllerPointer->pastScores.push_back(score);
252 double prevScore=controllerPointer->maxScore;
255 double newScore= leniencyCoef * prevScore + (1.0 - leniencyCoef) * score;
256 controllerPointer->maxScore=newScore;
260 controllerPointer->maxScore=score;
261 controllerPointer->maxScore1=multiscore[0];
262 controllerPointer->maxScore2=multiscore[1];
264 std::size_t n = controllerPointer->statelessParameters.size();
265 for (std::size_t i = 0; i < n; i++)
267 payloadLog <<
"," << controllerPointer->statelessParameters[i];
Convenience function for combining strings with ints, mostly for naming structures.
A class to read a learning configuration from a .ini file.
A series of functions to assist with file input/output.
static std::string getResourcePath(std::string relPath)
Contains the definition of class AnnealEvolution. Adapting NeuroEvolution to do Simulated Annealing...
unsigned long long rdtsc()
Rand seeding simular to the evolution and terrain classes.