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

📄 speedtsplocation.cpp

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

#include "stdafx.h"
#include "SpeedTSPLocation.h"

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

SpeedTSPLocation::SpeedTSPLocation(int d,double e1,double e2,double e3):SpeedTSP(d,e1,e2,e3)
{

}

void SpeedTSPLocation::NewSpeed(ParticleTSP * * sworm,int i,int pmax,Rand * rand,int gen)
{
	int * * speed=sworm[i]->GetSpeed();				//取当前速度
	int length=sworm[i]->GetSL();					//去速度中对换的个数
//	double * location=sworm[i]->GetLocation();		//取当前位置
//	double * location1=sworm[i]->GetLocation();
	double * i_optim=sworm[i]->GetIOL();			//个体最优解
	double * g_optim=sworm[pmax]->GetIOL();			//全局最优解
//	int * s_location=sworm[i]->GetSS();				//取兑换中有哪些城市
	int * n_s_location=new int[dimension];			//新速度中有哪些城市
	for(int j=0;j<dimension;j++)
		n_s_location[j]=0;

	int * * newspeed=new int *[dimension];		//新速度
	for(j=0;j<dimension;j++)					//
		newspeed[j]=new int[2];

	int newlength=0;								//新速度长度初值为0
	double p;										//随机数变量

	p=rand->randint(dimension);					//从全局最优解中选取
	for(j=0;j<p;j++)
	{
		int p1=rand->randint(dimension);
		if((n_s_location[(int)g_optim[p1]]!=1)&&(n_s_location[(int)g_optim[p1+1]]!=1))
		{
			n_s_location[(int)g_optim[p1]]=1;
			n_s_location[(int)g_optim[p1+1]]=1;
			newspeed[newlength][0]=(int)i_optim[p1];
			newspeed[newlength][1]=(int)i_optim[p1+1];
			newlength++;
		}
	}

	p=rand->randint(dimension);					//从局部最优解中选取
	for(j=0;j<p;j++)
	{
		int p1=rand->randint(dimension);
		if((n_s_location[(int)i_optim[p1]]!=1)&&(n_s_location[(int)i_optim[p1+1]]!=1))//速度中没有重复的城市
		{
			n_s_location[(int)i_optim[p1]]=1;
			n_s_location[(int)i_optim[p1+1]]=1;
			newspeed[newlength][0]=(int)i_optim[p1];
			newspeed[newlength][1]=(int)i_optim[p1+1];
			newlength++;
		}
	}

	for(j=0;j<length;j++)							//从原有速度中选取
	{
		if((n_s_location[speed[j][0]]==0)&&(n_s_location[speed[j][1]]==0))//速度中没有重复的城市
		{
			n_s_location[speed[j][0]]=1;
			n_s_location[speed[j][1]]=1;
			newspeed[newlength][0]=speed[j][0];
			newspeed[newlength][1]=speed[j][1];
			newlength++;
		}
	}

	sworm[i]->SetSpeed(newspeed,newlength);
	for(j=0;j<length;j++)
	{
		delete[] speed[j];
	}
	for(j=0;j<dimension;j++)
	{
		delete[] newspeed[j];
	}
	delete[] speed;
	delete[] newspeed;
//	delete[] location;
//	delete[] location1;
	delete[] i_optim;
	delete[] g_optim;
}

SpeedTSPLocation::~SpeedTSPLocation()
{

}

⌨️ 快捷键说明

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