⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rankselector.cpp

📁 我自己写的一个用于图编码的遗传算法
💻 CPP
字号:
#include "RankSelector.h"
#include <algorithm>

void RankSelector::applySelect(Genome*& child1,Genome*& child2)
{
	prepare();
	RouletteSelector::applySelect(child1,child2);
}


void RankSelector::prepare()
{
	clean();

	//按照适应度(可能是经过尺度变化的),传递一个函子(functor)
	std::sort(population->begin(), population->end(), SortByDescendingComparator());

	int genePoolSize = population->size();

	//根据n*(n+1)/2的公式计算出1+2+...+n
	totalFitness = double((genePoolSize * (genePoolSize + 1)) / 2);

	double currentTotal = 0.0;

	for (int i = 0; i < genePoolSize; ++i)  
	{
		currentTotal += (double(genePoolSize - i) / totalFitness);
		wheel.push_back(currentTotal);
	}    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -