40 NeuroEvoPopulation::NeuroEvoPopulation(
int populationSize,
configuration& config) :
43 compareAverageScores=
true;
44 clearScoresBetweenGenerations=
false;
45 this->compareAverageScores=config.getintvalue(
"compareAverageScores");
46 this->clearScoresBetweenGenerations=config.getintvalue(
"clearScoresBetweenGenerations");
48 for(
int i=0;i<populationSize;i++)
50 cout<<
" creating members"<<endl;
55 NeuroEvoPopulation::~NeuroEvoPopulation()
57 for(std::size_t i=0;i<controllers.size();i++)
59 delete controllers[i];
63 void NeuroEvoPopulation::mutate(std::tr1::ranlux64_base_01 *engPntr,std::size_t numMutate)
65 if(numMutate>controllers.size()/2)
67 cout<<
"Trying to mutate more than half of the population"<<endl;
70 for(std::size_t i=0;i<numMutate;i++)
73 int copyTo = this->controllers.size()-1-i;
74 controllers.at(copyTo)->copyFrom(controllers.at(copyFrom));
75 controllers.at(copyTo)->mutate(engPntr);
80 void NeuroEvoPopulation::combineAndMutate(std::tr1::ranlux64_base_01 *eng, std::size_t numToMutate, std::size_t numToCombine)
82 std::tr1::uniform_real<double> unif(0, 1);
84 if(numToMutate + numToCombine > controllers.size())
86 throw std::invalid_argument(
"Population will grow in size with these parameters");
89 std::vector<double> probabilities = generateMatingProbabilities();
91 std::vector<NeuroEvoMember*> newControllers;
92 for(
int i = 0; i < numToCombine; i++)
95 double val1 = unif(*eng);
96 double val2 = unif(*eng);
98 int index1 = getIndexFromProbability(probabilities, val1);
99 int index2 = getIndexFromProbability(probabilities, val2);
114 newController->copyFrom(controllers[index1], controllers[index2], eng);
118 newController->mutate(eng);
121 newControllers.push_back(newController);
124 for(
int i = 0; i < numToMutate; i++)
126 double val1 = unif(*eng);
127 int index1 = getIndexFromProbability(probabilities, val1);
129 newController->copyFrom(controllers[index1]);
130 newController->mutate(eng);
131 newControllers.push_back(newController);
135 const std::size_t n = controllers.size();
136 const std::size_t m = newControllers.size();
137 while(controllers.size() > n - m)
139 controllers.pop_back();
142 controllers.insert(controllers.end(), newControllers.begin(), newControllers.end());
144 assert(controllers.size() == n);
148 void NeuroEvoPopulation::orderPopulation()
151 for(std::size_t i=0;i<this->controllers.size();i++)
153 double ave = std::accumulate(controllers[i]->pastScores.begin(),controllers[i]->pastScores.end(),0);
154 ave /= (double) controllers[i]->pastScores.size();
155 controllers[i]->averageScore=ave;
156 if(clearScoresBetweenGenerations)
157 controllers[i]->pastScores.clear();
160 if(compareAverageScores)
161 sort(controllers.begin(),controllers.end(),this->comparisonFuncForAverage);
163 sort(controllers.begin(),controllers.end(),this->comparisonFuncForMax);
167 std::vector<double> NeuroEvoPopulation::generateMatingProbabilities()
169 double totalScore = 0.0;
170 const std::size_t n = controllers.size();
171 for (std::size_t i = 0; i < n; i++)
173 totalScore += controllers[i]->maxScore;
176 std::vector<double> probabilties;
177 probabilties.push_back(controllers[0]->maxScore / totalScore);
178 for (std::size_t i = 1; i < n; i++)
180 double nextScore = controllers[i]->maxScore / totalScore + probabilties[i - 1];
181 probabilties.push_back(nextScore);
185 assert(probabilties[n - 1] >= .99);
190 int NeuroEvoPopulation::getIndexFromProbability(std::vector<double>& probs,
double val)
194 while(i < (probs.size() - 1) && probs[i] < val)
204 return elm1->averageScore > elm2->averageScore;
208 return elm1->maxScore > elm2->maxScore;
A set of members that gets updated and mutated.