当前位置: 首页>>代码示例>>C++>>正文


C++ Chromosome::getSanitized方法代码示例

本文整理汇总了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;
	}
}
开发者ID:vincenthugues,项目名称:genetic-algorithm-tests,代码行数:78,代码来源:main.cpp


注:本文中的Chromosome::getSanitized方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。