本文整理汇总了C++中ControllerPtr::crossOverWithMP方法的典型用法代码示例。如果您正苦于以下问题:C++ ControllerPtr::crossOverWithMP方法的具体用法?C++ ControllerPtr::crossOverWithMP怎么用?C++ ControllerPtr::crossOverWithMP使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ControllerPtr
的用法示例。
在下文中一共展示了ControllerPtr::crossOverWithMP方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getNextCandidate
ControllerPtr PopulationControlArchitecture::getNextCandidate() {
currentTime++;
ControllerPtr candidate;
// if there is no full list of active solutions yet, generate a new candidate randomly
if (activeSolutions.size() < mu) {
candidate = this->createRandomGenome();
if (debug) {
cout << "Generated " << candidate->ToString() << endl;
}
}
// get news from the population, spreading the active solution
population->update(currentTime);
// if there is a full list of active solutions, mutate an existing solution or re-evaluate an active to create a new candidate
if (activeSolutions.size() == mu) {
// decide whether to re-evaluate or mutate a controller
if (Rand::randouble() < reEvaluationRate) { // re-evaluate
// pick one of the active solutions for re-evaluation
candidate = activeSolutions[Rand::randint(0, activeSolutions.size())];
if (debug) {
cout << "Selected re-evaluation of " << candidate->ToShortString() << endl;
}
} else { // mutate existing
// parent selection: random from the population
ControllerPtr parentA = selectParentByBinaryTournament(ControllerPtr());
if (debug) {
cout << "Selected first parent: " << parentA->ToShortString() << endl;
}
// crossover or clone; crossover only if there is a second active solution in the hivemind
if (population->getActiveCount() > 1 && Rand::randouble() < crossoverRate) {
if (debug) {
cout << "Selecting second parent, from a choice of " << population->getActiveCount() << endl;
}
// crossover
// select a second parent, excluding the first from the selection
vector<ControllerPtr> *parents = new vector<ControllerPtr>();
parents->push_back(parentA);
while (parents->size()<(unsigned) numberOfParents) {
if (debug) {
cout << parents->size() << endl;
}
ControllerPtr tmpC = selectParentsByBinaryTournament(parents);
parents->push_back(tmpC);
}
// create a candidate from crossover between the parents
candidate = parentA->crossOverWithMP(parents);
if (debug) {
cout << " Created " << candidate->ToShortString() << " from crossover between " << parents->at(0)->ToShortString();
for (uint i = 1; i < parents->size(); i++) cout << " and " << parents->at(i)->ToShortString();
cout << endl;
}
parents->clear();
delete parents;
} else {
// clone
candidate = parentA->Clone();
if (debug) {
cout << "Spawned " << candidate->ToShortString() << " from " << parentA->ToShortString() << endl;
}
}
// mutation
if (Rand::randouble() < mutationRate) {
//cout << "Before mutation: " << candidate->ToString() << endl;
candidate->mutate();
//cout << "After mutation: " << candidate->ToString() << endl;
if (debug) {
cout << "Mutated " << candidate->ToShortString() << endl;
}
}
}
}
return candidate;
}