48 unsigned long long rdtsc(){
50 __asm__ __volatile__ (
"rdtsc" :
"=a" (lo),
"=d" (hi));
51 return ((
unsigned long long)hi << 32) | lo;
56 NeuroEvolution::NeuroEvolution(std::string suff, std::string config, std::string path) :
63 resourcePath = FileHelpers::getResourcePath(path);
70 std::string configPath = resourcePath + config;
73 myconfigdataaa.readFile(configPath);
74 populationSize=myconfigdataaa.getintvalue(
"populationSize");
75 numberOfElementsToMutate=myconfigdataaa.getintvalue(
"numberOfElementsToMutate");
76 numberOfChildren=myconfigdataaa.getintvalue(
"numberOfChildren");
77 numberOfTestsBetweenGenerations=myconfigdataaa.getintvalue(
"numberOfTestsBetweenGenerations");
78 numberOfControllers=myconfigdataaa.getintvalue(
"numberOfControllers");
79 leniencyCoef=myconfigdataaa.getDoubleValue(
"leniencyCoef");
80 coevolution=myconfigdataaa.getintvalue(
"coevolution");
81 seeded = myconfigdataaa.getintvalue(
"startSeed");
83 bool learning = myconfigdataaa.getintvalue(
"learning");
85 if (populationSize < numberOfElementsToMutate + numberOfChildren)
87 throw std::invalid_argument(
"Population will grow with given parameters");
93 for(
int j=0;j<numberOfControllers;j++)
95 cout<<
"creating Populations"<<endl;
102 for(
int i = 0; i < numberOfControllers; i++)
106 ss<< resourcePath <<
"logs/bestParameters-"<<this->suffix<<
"-"<<i<<
".nnw";
107 seededPop->loadFromFile(ss.str().c_str());
112 evolutionLog.open((resourcePath +
"logs/evolution"+suffix+
".csv").c_str(),ios::out);
113 if (!evolutionLog.is_open())
115 throw std::runtime_error(
"Logs does not exist. Please create a logs folder in your build directory or update your cmake file");
120 NeuroEvolution::~NeuroEvolution()
124 for(std::size_t i = 0; i < populations.size(); i++)
126 delete populations[i];
132 void NeuroEvolution::mutateEveryController()
134 for(std::size_t i=0;i<populations.size();i++)
136 populations.at(i)->mutate(&eng,numberOfElementsToMutate);
140 void NeuroEvolution::combineAndMutate()
142 for(std::size_t i=0;i<populations.size();i++)
144 populations.at(i)->combineAndMutate(&eng, numberOfElementsToMutate, numberOfChildren);
151 double aveScore1 = 0.0;
152 double aveScore2 = 0.0;
155 double maxScore1,maxScore2;
157 for(std::size_t i=0;i<scoresOfTheGeneration.size();i++)
159 aveScore1+=scoresOfTheGeneration[i][0];
160 aveScore2+=scoresOfTheGeneration[i][1];
162 aveScore1 /= scoresOfTheGeneration.size();
163 aveScore2 /= scoresOfTheGeneration.size();
166 for(std::size_t i=0;i<populations.size();i++)
168 populations.at(i)->orderPopulation();
171 evolutionLog<<generationNumber*numberOfTestsBetweenGenerations<<
","<<aveScore1<<
","<<aveScore2<<
",";
172 evolutionLog<<populations.at(0)->getMember(0)->maxScore<<
","<<populations.at(0)->getMember(0)->maxScore1<<
","<<populations.at(0)->getMember(0)->maxScore2<<endl;
177 ofstream logfileLeader;
178 for(std::size_t i=0;i<populations.size();i++)
181 ss <<
resourcePath <<
"logs/bestParameters-"<<suffix<<
"-"<<i<<
".nnw";
182 populations[i]->getMember(0)->saveToFile(ss.str().c_str());
186 double diffclock(clock_t clock1,clock_t clock2)
188 double diffticks=clock1-clock2;
189 double diffms=(diffticks*10)/CLOCKS_PER_SEC;
193 vector <NeuroEvoMember *> NeuroEvolution::nextSetOfControllers()
197 testsToDo=numberOfTestsBetweenGenerations;
199 testsToDo=populationSize;
201 if(currentTest == testsToDo)
204 if (numberOfChildren == 0)
206 mutateEveryController();
212 cout<<
"mutated the populations"<<endl;
213 this->scoresOfTheGeneration.clear();
218 currentTest=populationSize - numberOfElementsToMutate - numberOfChildren;
221 selectedControllers.clear();
222 for(std::size_t i=0;i<populations.size();i++)
226 selectedOne=rand()%populationSize;
228 selectedOne=currentTest;
231 selectedControllers.push_back(populations.at(i)->getMember(selectedOne));
236 return selectedControllers;
239 void NeuroEvolution::updateScores(vector <double> multiscore)
241 if(multiscore.size()==2)
242 this->scoresOfTheGeneration.push_back(multiscore);
244 multiscore.push_back(-1.0);
245 double score=1.0* multiscore[0] - 0.0 * multiscore[1];
246 for(std::size_t oneElem=0;oneElem<selectedControllers.size();oneElem++)
248 NeuroEvoMember * controllerPointer=selectedControllers.at(oneElem);
250 controllerPointer->pastScores.push_back(score);
251 double prevScore=controllerPointer->maxScore;
254 double newScore= leniencyCoef * prevScore + (1.0 - leniencyCoef) * score;
255 controllerPointer->maxScore=newScore;
259 controllerPointer->maxScore=score;
260 controllerPointer->maxScore1=multiscore[0];
261 controllerPointer->maxScore2=multiscore[1];
267 payloadLog.open((
resourcePath +
"logs/scores.csv").c_str(),ios::app);
268 payloadLog<<multiscore[0]<<
","<<multiscore[1]<<endl;
Convenience function for combining strings with ints, mostly for naming structures.
void orderAllPopulations()
A class to read a learning configuration from a .ini file.
A series of functions to assist with file input/output.
Top level class for NeuroEvolution.