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) :
65 resourcePath = FileHelpers::getResourcePath(path);
72 std::string configPath = resourcePath + config;
75 myconfigdataaa.readFile(configPath);
76 populationSize=myconfigdataaa.getintvalue(
"populationSize");
77 numberOfElementsToMutate=myconfigdataaa.getintvalue(
"numberOfElementsToMutate");
78 numberOfTestsBetweenGenerations=myconfigdataaa.getintvalue(
"numberOfTestsBetweenGenerations");
79 numberOfControllers=myconfigdataaa.getintvalue(
"numberOfControllers");
80 leniencyCoef=myconfigdataaa.getDoubleValue(
"leniencyCoef");
81 coevolution=myconfigdataaa.getintvalue(
"coevolution");
82 seeded = myconfigdataaa.getintvalue(
"startSeed");
84 bool learning = myconfigdataaa.getintvalue(
"learning");
89 for(
int j=0;j<numberOfControllers;j++)
97 for(
int i = 0; i < numberOfControllers; i++)
101 ss<< resourcePath <<
"logs/bestParameters-"<<this->suffix<<
"-"<<i<<
".nnw";
102 seededPop->loadFromFile(ss.str().c_str());
107 evolutionLog.open((resourcePath +
"logs/evolution" + suffix +
".csv").c_str(),ios::out);
108 if (!evolutionLog.is_open())
110 throw std::runtime_error(
"Logs does not exist. Please create a logs folder in your build directory or update your cmake file");
115 AnnealEvolution::~AnnealEvolution()
119 for(std::size_t i = 0; i < populations.size(); i++)
121 delete populations[i];
127 void AnnealEvolution::mutateEveryController()
129 for(std::size_t i=0;i<populations.size();i++)
131 populations.at(i)->mutate(&eng,numberOfElementsToMutate, Temp);
137 void AnnealEvolution::orderAllPopulations()
140 double aveScore1 = 0.0;
141 double aveScore2 = 0.0;
144 double maxScore1,maxScore2;
146 for(std::size_t i=0;i<scoresOfTheGeneration.size();i++)
148 aveScore1+=scoresOfTheGeneration[i][0];
149 aveScore2+=scoresOfTheGeneration[i][1];
151 aveScore1 /= scoresOfTheGeneration.size();
152 aveScore2 /= scoresOfTheGeneration.size();
155 for(std::size_t i=0;i<populations.size();i++)
157 populations.at(i)->orderPopulation();
159 evolutionLog<<generationNumber*numberOfTestsBetweenGenerations<<
","<<aveScore1<<
","<<aveScore2<<
",";
160 evolutionLog<<populations.at(0)->getMember(0)->maxScore<<
","<<populations.at(0)->getMember(0)->maxScore1<<
","<<populations.at(0)->getMember(0)->maxScore2<<endl;
166 ofstream logfileLeader;
167 for(std::size_t i=0;i<populations.size();i++)
170 ss <<
resourcePath <<
"logs/bestParameters-" << suffix <<
"-" << i <<
".nnw";
172 populations[i]->getMember(0)->saveToFile(ss.str().c_str());
177 double diffclock(clock_t clock1,clock_t clock2)
179 double diffticks=clock1-clock2;
180 double diffms=(diffticks*10)/CLOCKS_PER_SEC;
185 vector <AnnealEvoMember *> AnnealEvolution::nextSetOfControllers()
189 testsToDo=numberOfTestsBetweenGenerations;
191 testsToDo=populationSize;
193 if(currentTest == testsToDo)
195 orderAllPopulations();
196 mutateEveryController();
199 this->scoresOfTheGeneration.clear();
204 currentTest=populationSize-numberOfElementsToMutate;
207 selectedControllers.clear();
208 for(std::size_t i=0;i<populations.size();i++)
212 selectedOne=rand()%populationSize;
214 selectedOne=currentTest;
217 selectedControllers.push_back(populations.at(i)->getMember(selectedOne));
222 return selectedControllers;
225 void AnnealEvolution::updateScores(vector <double> multiscore)
227 if(multiscore.size()==2)
228 this->scoresOfTheGeneration.push_back(multiscore);
230 multiscore.push_back(-1.0);
231 double score=1.0* multiscore[0] - 0.0 * multiscore[1];
235 payloadLog.open((
resourcePath +
"logs/scores.csv").c_str(),ios::app);
236 payloadLog<<multiscore[0]<<
","<<multiscore[1];
238 for(std::size_t oneElem=0;oneElem<selectedControllers.size();oneElem++)
242 controllerPointer->pastScores.push_back(score);
243 double prevScore=controllerPointer->maxScore;
246 double newScore= leniencyCoef * prevScore + (1.0 - leniencyCoef) * score;
247 controllerPointer->maxScore=newScore;
251 controllerPointer->maxScore=score;
252 controllerPointer->maxScore1=multiscore[0];
253 controllerPointer->maxScore2=multiscore[1];
255 std::size_t n = controllerPointer->statelessParameters.size();
256 for (std::size_t i = 0; i < n; i++)
258 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.
Contains the definition of class AnnealEvolution. Adapting NeuroEvolution to do Simulated Annealing...