📄 locationtsplocation4.cpp
字号:
// LocationTSPLocation4.cpp: implementation of the LocationTSPLocation4 class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LocationTSPLocation4.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
LocationTSPLocation4::LocationTSPLocation4(int d,Adaptability * a,FILE * f1,FILE * f2):LocationTSP(d,a,f1,f2)
{
}
void LocationTSPLocation4::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(n_s_location[speed[j][0]]>=0)
{
int temp=speed[j][0];
speed[j][0]=speed[j][1];
speed[j][1]=temp;
}
n_s_location[speed[j][0]]=speed[j][1];
//找出速度中的片断在编码中的位置
int p1=0;
while(location[p1]!=speed[j][0])
p1++;
int p2=0;
while(location[p2]!=speed[j][1])
p2++;
if(n_s_location[(int)location[p2]]>=0)
{
if((int)location[p2+1]==n_s_location[(int)location[p2]])
{
if(p2<p1) //***p2p2+1***p1***
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 //***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
{
if(p2<p1) //***p2-1p2***p1***
{
for(int i=0;i<(p1-p2)/2;i++) //翻转
{
int temp=(int)location[p1-i];
location[p1-i]=location[p2+i+1];
location[p2+i+1]=temp;
}
}
else //***p1***p2-1p2***
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
{
n_s_location[speed[j][1]]=speed[j][0];
if(p1>p2) //***p2***p1***
{
int temp=p1;
p1=p2;
p2=temp;
}
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;
}
}
}
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[] n_s_location;
delete[] location;
for(j=0;j<length;j++)
delete[] speed[j];
delete[] speed;
// fclose(ftemp);
}
LocationTSPLocation4::~LocationTSPLocation4()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -