25 #include "evodataviewer.h"
27 #include "randomgenerator.h"
28 #include <configurationhelper.h>
30 #include <QThreadPool>
31 #include <QtConcurrentMap>
32 #include <QtAlgorithms>
40 #pragma warning(disable:4996)
146 void runEvaluatorThreadForEvoga(EvaluatorThreadForEvoga* e)
176 int Evoga::mrand(
int i)
193 loadedIndividuals = 1;
195 savePopulationEachNGenerations = 0;
196 stopEvolution =
false;
197 averageIndividualFitnessOverGenerations =
true;
198 isStepByStep =
false;
205 if (statfit != NULL) {
206 for(
int i = 0; i < nogenerations; i++) {
215 void Evoga::setSeed(
int s)
223 double Evoga::drand()
225 return (
double)rand()/(double)RAND_MAX;
228 double Evoga::getNoise(
double minn,
double maxn)
236 return drand()*nrange+minn;
239 int Evoga::mutate(
int w,
double mut)
246 for(ii=0;ii < 8;ii++) {
250 for(ii=0;ii < 8;ii++) {
269 void Evoga::putGenome(
int fromgenome,
int tobestgenome)
271 if (tobestgenome < this->nreproducing) {
272 for (
int i = 0; i < this->glen; i++) {
273 bestgenome[tobestgenome][i] = genome[fromgenome][i];
280 void Evoga::getGenome(
int frombestgenome,
int togenome,
int mut)
282 for(
int i = 0; i < this->glen; i++) {
284 genome[togenome][i] = bestgenome[frombestgenome][i];
287 genome[togenome][i] = mutate(bestgenome[frombestgenome][i], mutation);
289 genome[togenome][i] = mutate(bestgenome[frombestgenome][i], mutations[i]);
295 void Evoga::copyGenes(
int from,
int to,
int mut)
297 for(
int i = 0; i < this->glen; i++) {
299 genome[to][i] = genome[from][i];
302 genome[to][i] = mutate(genome[from][i], mutation);
304 genome[to][i] = mutate(genome[from][i], mutations[i]);
311 void Evoga::reproduce()
323 this->computeFStat();
326 for(bi=0;bi<this->nreproducing;bi++) {
329 for(i=0;i< this->popSize;i++) {
336 this->putGenome(bx,bi);
339 if ((bi+1)<=this->savebest && cgen< this->nogenerations) {
340 sprintf(sbuffer,
"B%dS%d.gen",bi+1,this->currentSeed);
342 if ((fp=fopen(sbuffer,
"w")) == NULL) {
343 Logger::error(QString(
"I cannot open file B%1S%2.gen").arg(bi+1).arg(this->currentSeed));
346 if ((fp=fopen(sbuffer,
"a")) == NULL) {
347 Logger::error(QString(
"I cannot open file B%1S%2.gen").arg(bi+1).arg(this->currentSeed));
351 fprintf(fp,
"**NET : s%d_%d.wts\n",cgen,bx);
352 saveagenotype(fp,bx);
356 tfitness[bx]=-9999.0;
361 for(bi=0;bi<this->nreproducing;bi++)
362 for(i =0;i< this->noffspring;i++) {
363 if(this->elitism && bi==0)
364 this->getGenome(bi,bx,0);
366 this->getGenome(bi,bx,1);
372 for (i=0;i<this->popSize;i++) tfitness[i]=0.0;
377 void Evoga::saveBestInd()
390 sprintf(sbuffer,
"B%dS%d.gen",bi+1,this->currentSeed);
392 if ((fp=fopen(sbuffer,
"w")) == NULL) {
393 Logger::error(QString(
"I cannot open file B%1S%2.gen").arg(bi+1).arg(this->currentSeed));
396 if ((fp=fopen(sbuffer,
"a")) == NULL) {
397 Logger::error(QString(
"I cannot open file B%1S%2.gen").arg(bi+1).arg(this->currentSeed));
402 for(i=0;i<this->popSize;i++) {
403 ffit=this->tfitness[i]/this->ntfitness[i];
411 fprintf(fp,
"**NET : s%d_%d.wts\n",cgen,bi);
412 saveagenotype(fp,bi);
418 void Evoga::mreproduce()
430 this->computeFStat();
433 for(bi=0;bi<this->nreproducing;bi++) {
436 for(i=0;i< this->popSize;i++) {
443 this->putGenome(bx,bi);
446 if ((bi+1)<=this->savebest && cgen< this->nogenerations) {
447 sprintf(sbuffer,
"B%dS%d.gen",bi+1,this->currentSeed);
449 if ((fp=fopen(sbuffer,
"w")) == NULL) {
450 Logger::error(QString(
"I cannot open file B%1S%2.gen").arg(bi+1).arg(this->currentSeed));
453 if ((fp=fopen(sbuffer,
"a")) == NULL) {
454 Logger::error(QString(
"I cannot open file B%1S%2.gen").arg(bi+1).arg(this->currentSeed));
458 fprintf(fp,
"**NET : s%d_%d.wts\n",cgen,bx);
459 saveagenotype(fp,bx);
468 for(bi=0;bi<this->nreproducing;bi++)
469 for(i =0;i< this->noffspring;i++) {
470 if(this->elitism && bi==0)
471 this->getGenome(bi,bx,0);
473 this->getGenome(bi,bx,1);
479 for (i=0;i<this->popSize;i++) tfitness[i]=9999.0;
485 void Evoga::printPop()
487 for(
int i = 0; i < this->popSize; i++) {
488 QString output = QString(
"Fit %1 | ").arg(tfitness[i]);
489 for(
int l = 0; l < this->glen; l++) {
490 output += QString(
"%1 ").arg(this->genome[i][l]);
496 void Evoga::printBest()
498 for(
int i = 0; i < bestgenome.size(); i++) {
499 QString output = QString(
"Best %d | ").arg(i);
500 for (
int s = 0; s < this->glen; s++) {
501 output += QString(
"%1 ").arg(this->bestgenome[i][s]);
507 void Evoga::computeFStat()
515 for(i=0;i<this->popSize;i++) {
516 if(tfitness[i]<min) min=tfitness[i];
517 if(tfitness[i]>max) max=tfitness[i];
520 this->faverage=av/(double)this->popSize;
523 this->statfit[this->cgen][0]=this->fmax;
524 this->statfit[this->cgen][1]=this->faverage;
525 this->statfit[this->cgen][2]=this->fmin;
528 void Evoga::computeFStat2()
539 for(i=0;i<this->popSize;i++) {
540 if((tfitness[i]/ntfitness[i])<min) {
541 min=tfitness[i]/ntfitness[i];
543 if((tfitness[i]/ntfitness[i])>max) max=tfitness[i]/ntfitness[i];
544 av+=(tfitness[i]/ntfitness[i]);
546 this->faverage=av/(double)this->popSize;
549 this->statfit[this->cgen][0]=this->fmax;
550 this->statfit[this->cgen][1]=this->faverage;
551 this->statfit[this->cgen][2]=this->fmin;
554 void Evoga::saveagenotype(FILE *fp,
int ind)
557 fprintf(fp,
"DYNAMICAL NN\n");
558 for (j=0; j < this->glen; j++)
559 fprintf(fp,
"%d\n", this->genome[ind][j]);
560 fprintf(fp,
"END\n");
564 void Evoga::saveallg()
570 sprintf(filename,
"G%dS%d.gen",cgen,currentSeed);
571 if ((fp=fopen(filename,
"w+")) == NULL) {
575 for(i=0;i<this->popSize;i++) {
576 fprintf(fp,
"**NET : %d_%d_%d.wts\n",cgen,0,i);
577 this->saveagenotype(fp,i);
583 void Evoga::saveFStat()
587 sprintf(sbuffer,
"statS%d.fit",currentSeed);
589 fp=fopen(sbuffer ,
"w");
591 fp=fopen(sbuffer ,
"a");
594 fprintf(fp,
"%.3f %.3f %.3f\n",fmax,faverage,fmin);
606 void Evoga::loadgenotype(FILE *fp,
int ind)
611 fscanf(fp,
"DYNAMICAL NN\n");
612 for (j=0; j <this->glen; j++) {
613 fscanf(fp,
"%d\n",&v);
614 this->genome[ind][j]=v;
619 int Evoga::loadallg(
int gen,
const char *filew)
627 sprintf(filename,
"G%dS%d.gen", gen, seed);
629 sprintf(filename,
"%s", filew);
632 if ((fp = fopen(filename,
"r")) != NULL) {
636 fscanf(fp,
"%s : %s\n", flag, message);
637 if (strcmp(flag,
"**NET") == 0) {
638 loadgenotype(fp, genome.addOne());
643 Logger::info(QString(
"Loaded ind: %1").arg(genome.size()));
646 Logger::error(QString(
"File %1 could not be opened").arg(filename));
649 loadedIndividuals = genome.size();
651 return genome.size();
657 int Evoga::loadStatistics(
char *filename)
664 if ((fp=fopen(filename,
"r")) != NULL) {
665 while(fscanf(fp,
"%f %f %f\n",&max,&av,&min)!=EOF) {
666 this->statfit[i][0]=
max;
667 this->statfit[i][1]=av;
668 this->statfit[i][2]=
min;
680 void Evoga::randomizePop()
682 for (
int i = 0; i < genome.size(); i++) {
683 for(
int g = 0; g < glen; g++) {
684 genome[i][g] = mrand(256);
689 void Evoga::setInitialPopulation(
int* ge)
694 for(i=0; i<genome.size(); i++)
695 for(g=0; g<glen; g++)
698 genome[i][g] = mrand(256);
700 genome[i][g] = ge[g];
704 void Evoga::setMutations(
float* mut)
707 for(
int i=0; i<glen; i++)
708 mutations[i] = mut[i];
711 int* Evoga::getGenes(
int ind)
713 return this->genome[ind];
716 int* Evoga::getBestGenes(
int ind)
718 return this->bestgenome[ind];
721 void Evoga::resetGenerationCounter()
730 Evonet* evonet = getResource<Evonet>(
"evonet" );
731 if (evonet->pheFileLoaded()) {
733 Evonet* evonet = getResource<Evonet>(
"evonet" );
734 float* muts =
new float[evonet->freeParameters()];
735 int* pheParams =
new int[evonet->freeParameters()];
738 evonet->getMutations(muts);
742 evonet->copyPheParameters(pheParams);
743 setInitialPopulation(pheParams);
763 int startGeneration=0;
768 genome.resize(popSize * 2);
770 final_mrate = mutation;
772 Logger::info(
"Number of replications: " + QString::number(nreplications));
775 QVector<EvaluatorThreadForEvoga*> evaluators(popSize, NULL);
776 if (numThreads > 1) {
777 for (
int i = 0; i < evaluators.size(); i++) {
782 QThreadPool::globalInstance()->setMaxThreadCount(numThreads);
785 for(rp=0;rp<nreplications;rp++) {
790 resetGenerationCounter();
800 for (
int i=0;i<popSize+1;i++) {
807 DataChunk statTest(QString(
"stattest"),Qt::blue,2000,
false);
808 if (statTest.loadRawData(QString(statfile),0)) {
809 startGeneration=statTest.getIndex();
811 Logger::info(
"Recovering from startGeneration: " + QString::number(startGeneration));
813 loadallg(startGeneration,genFile);
814 cgen=startGeneration;
815 mutation=mutation-startGeneration*mutationdecay;
816 if (mutation<final_mrate) mutation=final_mrate;
820 for(gn=startGeneration;gn<nogenerations;gn++) {
825 if (numThreads <= 1) {
829 for(
id=0;
id<popSize;
id++) {
834 if (averageIndividualFitnessOverGenerations) {
844 copyGenes(
id, popSize,1);
846 ntfitness[popSize]=0;
851 if (averageIndividualFitnessOverGenerations) {
852 tfitness[popSize] += fit;
853 ntfitness[popSize]++;
855 tfitness[popSize] = fit;
856 ntfitness[popSize] = 1;
865 for(
int wi=0;wi<popSize;wi++) {
866 if (ntfitness[wi] == 0.0) {
869 mfit=tfitness[wi]/ntfitness[wi];
876 if ((tfitness[popSize]/ntfitness[popSize])>minfit) {
877 copyGenes(popSize, minid,0);
878 tfitness[minid]=tfitness[popSize];
879 ntfitness[minid]=ntfitness[popSize];
888 for (
int i = 0; i < popSize; i++) {
889 evaluators[i]->getExperiment()->initGeneration(gn);
894 for (
int i = 0; i < popSize; i++) {
895 evaluators[i]->setGenotype(i);
900 QFuture<void> evaluationFuture = QtConcurrent::map(evaluators, runEvaluatorThreadForEvoga);
901 evaluationFuture.waitForFinished();
905 for (
int i = 0; i < popSize; i++) {
906 if (averageIndividualFitnessOverGenerations) {
907 tfitness[evaluators[i]->getGenotypeId()] += evaluators[i]->getFitness();
908 ntfitness[evaluators[i]->getGenotypeId()]++;
910 tfitness[evaluators[i]->getGenotypeId()] = evaluators[i]->getFitness();
911 ntfitness[evaluators[i]->getGenotypeId()] = 1;
917 for (
int i = 0; i < popSize; i++) {
918 copyGenes(i, popSize + i, 1);
919 tfitness[popSize + i] = 0;
920 ntfitness[popSize + i] = 0;
921 evaluators[i]->setGenotype(popSize + i);
926 evaluationFuture = QtConcurrent::map(evaluators, runEvaluatorThreadForEvoga);
927 evaluationFuture.waitForFinished();
931 for (
int i = 0; i < popSize; i++) {
932 if (averageIndividualFitnessOverGenerations) {
933 tfitness[evaluators[i]->getGenotypeId()] += evaluators[i]->getFitness();
934 ntfitness[evaluators[i]->getGenotypeId()]++;
936 tfitness[evaluators[i]->getGenotypeId()] = evaluators[i]->getFitness();
937 ntfitness[evaluators[i]->getGenotypeId()] = 1;
943 for (
int i = 0; i < popSize; i++) {
944 evaluators[i]->getExperiment()->endGeneration(gn);
952 QVector<FitnessAndId> parents(popSize);
953 for (
int i = 0; i < popSize; i++) {
954 parents[i].fitness = tfitness[i] / ntfitness[i];
957 QVector<FitnessAndId> children(popSize);
958 for (
int i = 0; i < popSize; i++) {
959 children[i].fitness = tfitness[popSize + i] / ntfitness[popSize + i];
960 children[i].id = popSize + i;
968 for (
int i = 0; i < popSize; i++) {
969 if (parents[p].fitness > children[c].fitness) {
974 copyGenes(children[c].
id, parents[popSize - 1 - c].
id, 0);
975 tfitness[parents[popSize - 1 - c].id] = tfitness[children[c].id];
976 ntfitness[parents[popSize - 1 - c].id] = ntfitness[children[c].id];
991 if (mutation > final_mrate) {
992 mutation -= mutationdecay;
994 mutation = final_mrate;
997 if ((savePopulationEachNGenerations != 0) && (gn % savePopulationEachNGenerations == 0)) {
1001 Logger::info(QString(
"Generation %1 took %2 minutes").arg(gn+1).arg((
double)evotimer.elapsed()/60000.0, 0,
'f', 2));
1008 for (
int i = 0; i < evaluators.size(); i++) {
1009 delete evaluators[i];
1022 int startGeneration=0;
1027 genome.resize(popSize);
1029 for(rp=0;rp<nreplications;rp++) {
1032 resetGenerationCounter();
1046 DataChunk statTest(QString(
"stattest"),Qt::blue,2000,
false);
1047 if (statTest.loadRawData(QString(statfile),0)) {
1048 startGeneration=statTest.getIndex();
1050 Logger::info(
"Recovering from startGeneration: " + QString::number(startGeneration));
1052 loadallg(startGeneration,genFile);
1056 for(gn=startGeneration;gn<nogenerations;gn++) {
1060 for(
id=0;
id<popSize;
id++) {
1074 if(savePopulationEachNGenerations!=0 && gn%savePopulationEachNGenerations == 0)
1077 Logger::info(QString(
"Generation %1 took %2 minutes").arg(gn+1).arg((
double)evotimer.elapsed()/60000.0, 0,
'f', 2));
1087 unsigned int generateRandomSeed() {
1090 unsigned long int stackMem = (
unsigned long int)( generateRandomSeed );
1093 unsigned long int startTime = GetTickCount();
1095 unsigned long int startTime = time(NULL);
1098 unsigned long int randSeed = 0;
1100 stackMem=stackMem-startTime; stackMem=stackMem-randSeed; stackMem=stackMem^(randSeed >> 13);
1101 startTime=startTime-randSeed; startTime=startTime-stackMem; startTime=startTime^(stackMem << 8);
1102 randSeed=randSeed-stackMem; randSeed=randSeed-startTime; randSeed=randSeed^(startTime >> 13);
1103 stackMem=stackMem-startTime; stackMem=stackMem-randSeed; stackMem=stackMem^(randSeed >> 12);
1104 startTime=startTime-randSeed; startTime=startTime-stackMem; startTime=startTime^(stackMem << 16);
1105 randSeed=randSeed-stackMem; randSeed=randSeed-startTime; randSeed=randSeed^(startTime >> 5);
1106 stackMem=stackMem-startTime; stackMem=stackMem-randSeed; stackMem=stackMem^(randSeed >> 3);
1107 startTime=startTime-randSeed; startTime=startTime-stackMem; startTime=startTime^(stackMem << 10);
1108 randSeed=randSeed-stackMem; randSeed=randSeed-startTime; randSeed=randSeed^(startTime >> 15);
1110 return randSeed%10000;
1119 if ( evolutionType !=
"steadyState" && evolutionType !=
"generational" ) {
1120 Logger::error(
"Evoga - evolutionType has been wrongly setted. It can assume only 'steadyState' or 'generational' values" );
1121 evolutionType =
"steadyState";
1129 if ( checkSeed ==
"auto" ) {
1130 seed = generateRandomSeed();
1132 seed = checkSeed.toInt();
1134 Logger::info( QString(
"Evoga - Random seed setted to ")+QString::number(seed) );
1140 averageIndividualFitnessOverGenerations =
ConfigurationHelper::getBool(params, prefix +
"averageIndividualFitnessOverGenerations",
true);
1151 Logger::info(
"Created EvoRobotExperiment " + params.
getValue(prefix+
"Experiment/type") +
" from group " + prefix +
"Experiment" );
1155 savedConfigurationParameters = params;
1156 savedExperimentPrefix = prefix +
"Experiment";
1169 d.
describeInt(
"nreplications" ).
def(10).
limits(1,
MaxInteger).
help(
"The number of which the evolution process will be replicated with a different random initial population");
1170 d.
describeInt(
"nreproducing" ).
def(20).
limits(1,
MaxInteger).
help(
"The number of individual allowed to produce offsprings; The size of populazion will be nreproducing x noffspring");
1171 d.
describeInt(
"noffspring" ).
def(5).
limits(1,
MaxInteger).
help(
"The number of offsprings generated by an individual; The size of populazion will be nreproducing x noffspring");
1172 d.
describeInt(
"seed" ).
def(1234).
limits(0,
MaxInteger).
help(
"The number used to initialize the random number generator; when a new replication will start, this value will be incremented by one to guarantee a truly different initial population for the next replica");
1176 d.
describeInt(
"savePopulationEachNGenerations").
def(0).
limits(0,
MaxInteger).
help(
"If is zero only the population of the last generation are saved into a file; otherwise it saves the population each N generations done");
1177 d.
describeReal(
"mutation_rate").
def(0.05).
limits(0,100).
help(
"The rate at a mutation will occur during a genotype copy; a real value below 1 (i.e. 0.12) is considered as a rate (i.e. 0.12 correspond to 12% of mutation); a value egual or above 1 is considered as a percentage of mutation (i.e. 25 correspond to 25% of mutation, or 0.25 rate of mutation)");
1178 d.
describeReal(
"mutation_decay").
def(0.01).
limits(0,1).
help(
"At first generation the mutation rate will be always 0.5, and at each generation done the mutation rate will be decreased by this value until it reachs the mutation_rate value");
1180 d.
describeBool(
"averageIndividualFitnessOverGenerations").
def(
true).
help(
"Whether to average the current fitness with the previous one or not");
1191 statfit =
new double*[nogenerations];
1193 for(
int i = 0; i < nogenerations; i++) {
1194 statfit[i] =
new double[3];
1199 mutations =
new float[glen];
1200 for(
int mi = 0; mi < glen; mi++) {
1206 popSize = noffspring * nreproducing;
1209 genome.setGenomeLength(glen);
1210 genome.resize(popSize);
1211 for (
int i = 0; i < genome.size(); i++) {
1212 for (
int r = 0; r < glen; r++) {
1218 bestgenome.setGenomeLength(glen);
1219 bestgenome.resize(nreproducing);
1226 for (
int i = 0; i < nogenerations; i++) {
1227 statfit[i][0] = 0.0;
1228 statfit[i][1] = 0.0;
1229 statfit[i][2] = 0.0;
1232 Logger::info(
"Evoga Configured - Number of genes: " + QString::number(glen));
1237 stopEvolution =
false;
1238 if ( evolutionType ==
"steadyState" ) {
1240 }
else if ( evolutionType ==
"generational" ) {
1243 Logger::error( QString(
"Evoga - request to execute a unrecognized evolution type: %1").arg(evolutionType) );
1248 stopEvolution =
true;
1249 waitForNextStep.wakeAll();
1253 if ( isStepByStep && !stopEvolution ) {
1255 mutexStepByStep.lock();
1256 waitForNextStep.wait( &mutexStepByStep );
1257 mutexStepByStep.unlock();
1259 return stopEvolution;
1263 return stopEvolution;
1267 stopEvolution =
false;
1271 isStepByStep = enable;
1274 waitForNextStep.wakeAll();
1279 return isStepByStep;
1283 waitForNextStep.wakeAll();
1293 QVector<EvoRobotExperiment*> v;
1315 return nreplications;
1319 return nogenerations;
1327 mutation = mutation_rate;
1332 return loadallg(-1, filename.toAscii().data());
1337 return loadedIndividuals;
1342 return getGenes(
id);
1347 return "statS" + QString::number(seed) + QString(
".fit");
1352 return "B0S" + QString::number(seed) + QString(
".gen");
1362 return "G" + QString::number(generation) +
"S" + QString::number(seed) + QString(
".gen");
1378 #if defined(_MSC_VER)
1379 #pragma warning(pop)