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

📄 select2.cpp

📁 c++语言实现的遗传算法的例子,需要的快下啊
💻 CPP
字号:
// Select2.cpp: implementation of the Select2 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Select2.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Select2::Select2(int s):Select(s)
{
	proportion=new int[scale];
}

int Select2::SelectPop(Rand * r)
{
	int temp=r->randint(scale*(scale+1));
	int m=0;
	int i=0;
	while(temp>=m)
	{
		m=m+proportion[i];
		i++;
	}
	--i;
	return i;
}

void Select2::GetProportion(Popular * * pop)
{
	double * solution=new double[scale];
	int * temp=new int[scale];
	for(int i=0;i<scale;i++)
	{
		temp[i]=0;
		solution[i]=pop[i]->GetAdapt();
	}
	for(i=0;i<scale;i++)
		for(int j=0;j<i;j++)
		{
			if(solution[i]>solution[j])
				temp[i]+=1;
			else
				temp[j]+=1;
		}
	for(i=0;i<scale;i++)
		proportion[i]=2*(temp[i]+1);
	delete[] temp;
	delete[] solution;
}

Select2::~Select2()
{
	delete[] proportion;
}

⌨️ 快捷键说明

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