📄 tournamentselector.cpp
字号:
#include "TournamentSelector.h"
#include <algorithm>
#include "MyRandom.h"
inline void TournamentSelector::clean()
{
//这里tournamentContainer是通过基类的population的clone方法获得的
tournamentContainer->clear();
}
inline void TournamentSelector::prepare()
{
tournamentContainer=population->clone();//这里有new的行为
clean();
}
void TournamentSelector::applySelect(Genome*& child1,Genome*& child2)
{
prepare();
int genePoolSize = population->size();
tournamentContainer->clear();
Population::PopIterator itr;
for (itr=population->begin(); itr !=population->end(); itr++)
{
int pos = MyRandom::randomInt(0, genePoolSize - 1);
tournamentContainer->addGenome(population->getGenome(pos));
}
//按照适应度的降序排列
std::sort(population->begin(), population->end(), SortByDescendingComparator());
//选择最好的一个,也就是第1个
child1 = tournamentContainer->getGenome(0);
//重复,为child2挑选
tournamentContainer->clear();
for (itr=population->begin(); itr !=population->end(); itr++)
{
int pos = MyRandom::randomInt(0, genePoolSize - 1);
tournamentContainer->addGenome(population->getGenome(pos));
}
std::sort(population->begin(), population->end(), SortByDescendingComparator());
child2 = tournamentContainer->getGenome(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -