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

📄 locationtsplocation6.cpp

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

#include "stdafx.h"
#include "LocationTSPLocation6.h"

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

LocationTSPLocation6::LocationTSPLocation6(int d,Adaptability * a,FILE * f1,FILE * f2):LocationTSP(d,a,f1,f2)
{

}

void LocationTSPLocation6::NewLocation(ParticleTSP * * sworm,int i)
{
	double * location=sworm[i]->GetLocation();
	int * * speed=sworm[i]->GetSpeed();
	int length=sworm[i]->GetSL();
	int * n_s_location=new int[dimension];			//新速度中有哪些城市
	for(int j=0;j<dimension;j++)
		n_s_location[j]=-1;
////////////////////////////////////////////////////////////////////////
/*
	FILE * ftemp=fopen("1.zn","w");

	fprintf(ftemp,"%d\n",length);

		for(j=0;j<length;j++)
		{
			fprintf(ftemp,"(%d    %d)  ",speed[j][0],speed[j][1]);
		}
		fprintf(ftemp,"\n");

		for(j=0;j<dimension;j++)
		{
			fprintf(ftemp,"%f    ",location[j]);
		}
		fprintf(ftemp,"\n");
*/
/////////////////////////////////////////////////////////////////////////////
	for(j=0;j<length;j++)
	{
		if(rand->random(1)>0.8)
			continue;
		//找出速度中的片断在编码中的位置
		int p1=0;			
		while(location[p1]!=speed[j][0])
			p1++;
		//若该子速度在当前路径中已存在
		if((location[(p1+1)%dimension]==speed[j][1])||(location[(p1-1+dimension)%dimension]==speed[j][1]))
			continue;
		int p2=0;
		while(location[p2]!=speed[j][1])
			p2++;

		if(p1>p2)
		{
			int temp=p1;
			p1=p2;
			p2=temp;
			temp=speed[j][0];
			speed[j][0]=speed[j][1];
			speed[j][1]=temp;
		}

		if((n_s_location[speed[j][0]]>=0)&&(n_s_location[speed[j][1]]>=0))
		{
			if(n_s_location[speed[j][0]]==(int)location[p1+1])
			{
				if(n_s_location[speed[j][1]]==(int)location[p2-1])
				{
					int i=1;
					int temp=(int)location[p2];
					location[p2]=location[p2-1];
					location[p2-1]=temp;
				}
				for(int i=0;i<(p2-p1-1)/2;i++)
				{
					int temp=(int)location[p1+i];
					location[p1+i]=location[p2-2-i];
					location[p2-2-i]=temp;
				}
			}
			else
			{
				if(n_s_location[speed[j][1]]==(int)location[(p2+1)%dimension])
				{
					int temp=(int)location[p2];
					location[p2]=location[(p2+1)%dimension];
					location[(p2+1)%dimension]=temp;
				}
				for(int i=0;i<(p2-p1+1)/2;i++)
				{
					int temp=(int)location[p1+i+1];
					location[p1+i+1]=location[(p2+1-i)%dimension];
					location[(p2+1-i)%dimension]=temp;
				}
			}
		}
		else
		{
			if(n_s_location[speed[j][0]]>=0)
			{
				n_s_location[speed[j][1]]=speed[j][0];
				if((int)location[p1+1]==n_s_location[(int)location[p1]])
				{				//***p1***p2p2+1***
					for(int i=0;i<(p2-p1)/2;i++)				//翻转
					{
						int temp=(int)location[p1+i];
						location[p1+i]=location[p2-i-1];
						location[p2-i-1]=temp;
					}
				}
				else
				{			
					for(int i=0;i<(p2-p1)/2;i++)				//翻转
					{
						int temp=(int)location[p1+i+1];
						location[p1+i+1]=location[p2-i];
						location[p2-i]=temp;
					}
				}							
			}
			else
			{
				if(n_s_location[speed[j][1]]>=0)
				{
					n_s_location[speed[j][0]]=speed[j][1];
					if((int)location[p2-1]==n_s_location[(int)location[p2]])										
					{
						for(int i=0;i<(p1-p2)/2;i++)				//翻转
						{
							int temp=(int)location[p1-i+1];
							location[p1+i+1]=location[p2-i];
							location[p2-i]=temp;
						}
					}
					else											
						for(int i=0;i<(p2-p1)/2;i++)				//翻转
						{
							int temp=(int)location[p1+i];
							location[p1+i]=location[p2-i-1];
							location[p2-i-1]=temp;
						}
				}
				else
				{
					n_s_location[speed[j][0]]=speed[j][1];
					n_s_location[speed[j][1]]=speed[j][0];
					for(int i=0;i<(p2-p1)/2;i++)				//翻转
					{
						int temp=(int)location[p1+i];
						location[p1+i]=location[p2-i-1];
						location[p2-i-1]=temp;
					}
				}
			}
		}
	}
	for(j=0;j<dimension;j++)
	{
		fprintf(file1,"%f    ",location[j]);
	}
	fprintf(file1,"\n");
/////////////////////////////////////////////////////////////////
/*	
		for(j=0;j<dimension;j++)
		{
			fprintf(ftemp,"%f    ",location[j]);
		}
		fprintf(ftemp,"\n");/**/

//////////////////////////////////////////////////////////////////
	sworm[i]->SetLocation(location);
	double n=adapt->adaption(location);
	sworm[i]->SetSolution(n);
	fprintf(file2,"%f\n",n);
	delete[] location;
	delete[] n_s_location;
	for(j=0;j<length;j++)
		delete[] speed[j];
	delete[] speed;

//	fclose(ftemp);
}

LocationTSPLocation6::~LocationTSPLocation6()
{

}

⌨️ 快捷键说明

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