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


C++ Evaluator::nextSetting方法代码示例

本文整理汇总了C++中Evaluator::nextSetting方法的典型用法代码示例。如果您正苦于以下问题:C++ Evaluator::nextSetting方法的具体用法?C++ Evaluator::nextSetting怎么用?C++ Evaluator::nextSetting使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Evaluator的用法示例。


在下文中一共展示了Evaluator::nextSetting方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: advanceGeneration

void GeneticAlgorithm::advanceGeneration(Evaluator& evaluator, std::vector<std::shared_ptr<Solution>>& genes) {
	//解のソート
	std::sort(genes.begin(), genes.end(), [](std::shared_ptr<Solution> lhs, std::shared_ptr<Solution> rhs){
		return lhs->getFitness() < rhs->getFitness();
	});

	//適合度の正規化
	//最小1・大きいほど良い
	std::vector<double> weights;
	const double maxFitness = genes.back()->getFitness();
	for (std::shared_ptr<Solution> gene : genes) {
		weights.push_back(maxFitness - gene->getFitness() + 1.0);
	}

	/*for (std::shared_ptr<ProgramRecord> gene : genes) {
		std::cout << gene->getFitness() << " ";
	}
	std::cout << std::endl;*/

	//スケーリング
	scaling(weights);

	/*for (double weight : weights) {
		std::cout << weight << " ";
	}
	std::cout << std::endl;*/

	//子の作成
	std::vector<std::shared_ptr<Solution>> offspring;
	const int reproductionSize = round((double)genes.size() * reproductionRate);
	const int elitismSize = std::min((int)round((double)genes.size() * elitismRate), reproductionSize);
	while (offspring.size() < (unsigned int)(reproductionSize - elitismSize)) {
		//選択
		int parentIndex1 = selection(weights, randomEngine);
		weights.erase(weights.begin() + parentIndex1);
		int parentIndex2 = selection(weights, randomEngine);
		if (parentIndex2 >= parentIndex1) {
			++parentIndex2;
		}
		std::shared_ptr<Solution> parent1 = genes[parentIndex1];
		std::shared_ptr<Solution> parent2 = genes[parentIndex2];

		//子の作成
		std::shared_ptr<Solution> child1 = parent1->createChild();
		std::shared_ptr<Solution> child2 = parent2->createChild();
		parent1->addChild(child2);
		parent2->addChild(child1);

		std::shared_ptr<GeneticOperableBase> geno1 = std::dynamic_pointer_cast<GeneticOperableBase>(child1->getProgram());
		std::shared_ptr<GeneticOperableBase> geno2 = std::dynamic_pointer_cast<GeneticOperableBase>(child2->getProgram());

		//交叉
		if (crossover != "None") {
			geno1->crossover(crossover, *geno2, randomEngine);
		}

		//変異
		if (mutation != "None") {
			geno1->mutation(mutation, randomEngine);
			geno2->mutation(mutation, randomEngine);
		}

		offspring.push_back(child1);
		offspring.push_back(child2);
	}
	for (int i = 0; offspring.size() < (unsigned int)reproductionSize; ++i) {
		std::shared_ptr<Solution> parent = genes[i];
		std::shared_ptr<Solution> child = parent->createChild();
		parent->addChild(child);
		offspring.push_back(child);
	}
	assert(offspring.size() == static_cast<unsigned int>(reproductionSize));

	//評価
	evaluation(evaluator, offspring, *getHistory(), randomEngine);

	//再挿入
	const int prevGeneration = genes[0]->getGeneration();
	reinsertion(genes, offspring);

	//旧世代の更新
	for (std::shared_ptr<Solution>& gene : genes) {
		if (gene->getGeneration() == prevGeneration) {
			gene = gene->createChild(false);
		}
	}
	evaluator.nextSetting();
}
开发者ID:kryozahiro,项目名称:gamesolver,代码行数:88,代码来源:GeneticAlgorithm.cpp


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