本文整理汇总了C++中Population::addIndividual方法的典型用法代码示例。如果您正苦于以下问题:C++ Population::addIndividual方法的具体用法?C++ Population::addIndividual怎么用?C++ Population::addIndividual使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Population
的用法示例。
在下文中一共展示了Population::addIndividual方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
srand(time(NULL));
Population population;
for (int i = 0; i < POPULATION_COUNT; ++i) {
Individual ind;
Chromosome chromosome(targetString.size());
chromosome.randomize();
float fitness = evaluate(chromosome);
ind.setChromosome(chromosome);
ind.setFitness(fitness);
population.addIndividual(ind);
}
population.sortPopulation();
std::string solution = "";
Individual bestInd;
int generationCount = 0;
int nth = 64;
for (int i = 0; i < GENERATION_COUNT; ++i) {
generationCount++;
evolve(population);
population.sortPopulation();
if (bestInd < population[0]) {
bestInd = population[0];
}
if (i % nth == 0) {
std::cout << "Generation: " << generationCount << std::endl;
std::cout << "Best individual: " << bestInd << std::endl;
}
if (bestInd.getFitness() >= 1){
break;
}
}
std::cout << std::endl;
std::cout << "Solved on generation " << generationCount << '!' << std::endl;
std::cout << bestInd << std::endl << std::endl;
std::cout << "Press enter to exit";
std::cin.get();
return 0;
}
示例2: evolve
void evolve(Population& population)
{
Population newGeneration;
while (newGeneration < population) {
Chromosome chromosome1 = select(population).getChromosome();
Chromosome chromosome2 = select(population).getChromosome();
crossover(chromosome1, chromosome2);
mutate(chromosome1, chromosome2);
Individual newInd1;
newInd1.setChromosome(chromosome1);
newInd1.setFitness(evaluate(chromosome1));
newGeneration.addIndividual(newInd1);
if (newGeneration < population) {
Individual newInd2;
newInd2.setChromosome(chromosome2);
newInd2.setFitness(evaluate(chromosome2));
newGeneration.addIndividual(newInd2);
}
}
population = newGeneration;
}
示例3: evolve
//essentially an update
void Evolve::evolve(Population &pop)
{
typedef std::pair<MEMBER*, SCORE> data_pair;
auto sort = findBest(pop);
std::vector<data_pair>fin_sort(sort.begin(), sort.end() );
std::sort(fin_sort.begin(), fin_sort.end(), [](const data_pair& that, const data_pair& thus)
{
return that.second < thus.second;
}
);
//find two best and copy them temporarily
auto end_it = fin_sort.rbegin();
MEMBER first = std::move(*end_it->first);
end_it++;
MEMBER second = std::move(*end_it->first);
//checks best
if(sort.rbegin()->second > best.first)
{
best = {sort.rbegin()->second, *first};
}
int START = 1;
//adds to mode
decltype(mode)::value_type data = {START, {first->getLemons(), first->getSugar(), first->getPrice()} };
int val = DATA_FIND(mode, data);
if(val != 0)
{
//adds one to mode if value is already placed
decltype(data) newdata = {val, data.second};
int pos = std::find(mode.begin(), mode.end(), decltype(newdata){newdata}) - mode.begin();
mode.at(pos).first++;
}
else
{
mode.push_back(data);
}
//debug
std::cout << first->getLemons() << std::endl;
std::cout << first->getSugar() << std::endl;
std::cout << first->getPrice() << std::endl;
std::cout << "Profit assuming 100 chances: " << sort.rbegin()->second << std::endl;
//clear the old vector
clear(pop);
//generate rest to fill up population size
for(int i = 0; i < POP_SIZE - 2; i++)
{
pop.addIndividual(generate(*first, *second) );
}
//add in parents
pop.addIndividual(std::move(first) );
pop.addIndividual(std::move(second) );
}