本文整理汇总了C++中Chromosome::getSanitized方法的典型用法代码示例。如果您正苦于以下问题:C++ Chromosome::getSanitized方法的具体用法?C++ Chromosome::getSanitized怎么用?C++ Chromosome::getSanitized使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Chromosome
的用法示例。
在下文中一共展示了Chromosome::getSanitized方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: startGenerations
// Starts the algorithm with the required parameters
void startGenerations(int targetNumber, uint maxGenerations, uint geneCountMin, uint geneCountMax, uint logThreshold)
{
bool solutionFound = false;
int generation = 0;
Chromosome *fittestChromosome = NULL;
float bestFitness = 0;
// Run the simulation until a solution has been found or the maximum number of generations has been reached
for (unsigned int i = 0; !solutionFound && (maxGenerations == 0 || i < maxGenerations); ++i)
{
bool writeLog = logThreshold && (generation % logThreshold == 0);
if (writeLog)
std::cout << "Generation " << generation << '\n';
Chromosome *newChromosome = NULL;
if (fittestChromosome)
newChromosome = new Chromosome(*fittestChromosome);
else
newChromosome = new Chromosome(geneCountMin, geneCountMax);
int result = 0;
eval_expr(newChromosome->getSanitized(), result);
if (writeLog)
std::cout << "Chromosome result: " << result << '\n';
if (result != targetNumber) // Solution not yet found
{
float fitness = getFitness(result, targetNumber);
if (writeLog)
std::cout << "Chromosome fitness: " << fitness;
if (fitness > bestFitness)
{
if (writeLog)
std::cout << " -> selected\n";
if (fittestChromosome)
delete fittestChromosome;
fittestChromosome = newChromosome;
bestFitness = fitness;
}
else
{
if (writeLog)
std::cout << " -> rejected\n";
delete newChromosome;
}
}
else // Solution found
{
if (fittestChromosome)
delete fittestChromosome;
fittestChromosome = newChromosome;
solutionFound = true;
}
if (writeLog)
std::cout << std::endl;
++generation;
}
std::cout << "Target number: " << targetNumber << '\n'
<< "Solution " << (solutionFound ? "" : "not ") << "found after " << generation << " generations" << std::endl;
if (fittestChromosome)
{
if (solutionFound)
fittestChromosome->displayInfo();
delete fittestChromosome;
}
}