?? 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));
}
//按照適應(yīng)度的降序排列
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 + -