📄 psotsp.cpp
字号:
// PSOTSP.cpp: implementation of the PSOTSP class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PSOTSP.h"
#include "time.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern HWND hWndMain;
extern int hour;
extern int minute;
extern int second;
extern int millisecond;
extern int nowround;
extern double * gb;
extern clock_t avetime;
PSOTSP::PSOTSP(Adaptability * adapt,Parameter * param)
{
int dimension=(int)param->CharToInt(param->GetCombo(5));
double e1=param->CharToInt(param->GetCombo(6));
double e2=param->CharToInt(param->GetCombo(7));
double e3=param->CharToInt(param->GetCombo(8));
scale=(int)param->CharToInt(param->GetCombo(1));
maxgen=(int)param->CharToInt(param->GetCombo(2));
g=new int(0);
rand=new Rand;
adaptation=adapt;
file1=fopen("location.zn","w");
file2=fopen("solution.zn","w");
if(strcmp(param->GetCombo(3),"插入")==0)
{
cspeed=new SpeedTSPInsert(dimension,e1,e2,e3,maxgen,scale);
loc=new LocationTSP1(dimension,adapt,file1,file2);
}
if(strcmp(param->GetCombo(3),"对换")==0)
{
cspeed=new SpeedTSPExchange(dimension,e1,e2,e3);
loc=new LocationTSP2(dimension,adapt,file1,file2);
}
if(strcmp(param->GetCombo(3),"分步")==0)
{
cspeed=new SpeedTSPBatch(dimension,e1,e2,e3);
loc=new LocationTSPBatch(dimension,adapt,file1,file2);
}
if(strcmp(param->GetCombo(3),"片断继承")==0)//速度中城市没有重复
{
cspeed=new SpeedTSPLocation(dimension,e1,e2,e3);
loc=new LocationTSPLocation(dimension,adapt,file1,file2);
}
if(strcmp(param->GetCombo(3),"片断继承2")==0)//速度中城市有重复
{
cspeed=new SpeedTSPLocation2(dimension,e1,e2,e3);
loc=new LocationTSPLocation2(dimension,adapt,file1,file2);
}
if(strcmp(param->GetCombo(3),"片断继承3")==0)//随机选择,遇到重复选择好的保留
{
cspeed=new SpeedTSPLocation3(dimension,e1,e2,e3,adaptation);
loc=new LocationTSPLocation3(dimension,adapt,file1,file2);
}
if(strcmp(param->GetCombo(3),"片断继承4")==0)//排序后选择,选择概率由最优个体决定,遇到重复选择好的保留
{
cspeed=new SpeedTSPLocation4(dimension,e1,e2,e3,adaptation);
loc=new LocationTSPLocation4(dimension,adapt,file1,file2);
}
if(strcmp(param->GetCombo(3),"片断继承5")==0)//对所有路径排序后选择,选择概率由最优个体决定,遇到重复选择好的保留
{
cspeed=new SpeedTSPLocation5(dimension,e1,e2,e3,adaptation);
loc=new LocationTSPLocation5(dimension,adapt,file1,file2);
}
if(strcmp(param->GetCombo(3),"片断继承6")==0)//对所有路径排序后选择,选择概率由最优个体和当前个体决定,遇到重复选择好的保留
{
cspeed=new SpeedTSPLocation6(dimension,e1,e2,e3,adaptation);
loc=new LocationTSPLocation6(dimension,adapt,file1,file2);
}
if(strcmp(param->GetCombo(3),"片断继承7")==0)//对所有路径排序后选择,选择概率由最优个体和当前个体决定,遇到重复选择好的保留
{
cspeed=new SpeedTSPLocationR(dimension,e1,e2,e3,adaptation);
loc=new LocationTSPLocation6(dimension,adapt,file1,file2);
}
sworm=new ParticleTSP *[scale];
for(int i=0;i<scale;i++)
sworm[i]=new ParticleTSP(dimension,rand);
if(strcmp(param->GetCombo(4),"有")==0)
{
maintain=new MaintainSequence(dimension,dimension,(AdaptabilityTSP *)adapt,rand);
signalmain=1;
}
else
signalmain=2;
/* fprintf(file1,"%f ",loc_max);
fprintf(file1,"%f ",loc_min);
fprintf(file1,"%d ",dimension);
fprintf(file1,"%d ",maxgen);
fprintf(file1,"%s ",param->GetCombo(4));
fprintf(file1,"%s ",param->GetCombo(3));
fprintf(file1,"%s ",param->GetCombo(11));
fprintf(file1,"%s ",param->GetCombo(12));
fprintf(file1,"%f ",e1);
fprintf(file1,"%f ",e2);
fprintf(file1,"%f ",e3);
fprintf(file1,"%f ",lp);
fprintf(file1,"%d ",number);
fprintf(file1,"%d\n",scale);*/
eva=new EvaluationTSP(scale);
}
void PSOTSP::Run()
{
clock_t star;
clock_t now;
star=clock();
// char szTitle[128];
// FILE * temp=fopen("1.zn","w");
int n=0;
while(n!=maxgen)
{
if(signalmain==1)
{
int t=n%scale;
double * temp=sworm[t]->GetLocation();
maintain->Maintain(temp);
double s=adaptation->adaption(temp);
sworm[t]->SetLocation(temp);
sworm[t]->SetSolution(s);
}
eva->SetOptim(sworm,g);
for(int j=0;j<scale;j++)
{
cspeed->NewSpeed(sworm,j,*g,rand,n);
loc->NewLocation(sworm,j);
}
n+=1;
/*
for(j=0;j<scale;j++)
{
fprintf(temp,"%d ",sworm[j]->GetSL());
}
fprintf(temp,"\n");
*/
}
now=clock();
avetime+=now-star;
gb[nowround]=sworm[*g]->GetOS();
double time=(double)(now-star)/CLOCKS_PER_SEC;
millisecond=((int)(time*100))%100;
second=((int)time)%60;
minute=((int)time-second)/60%60;
hour=(((int)time-second)/60-minute)/60;
// wsprintf(szTitle,"迭代次数%d 运行时间%d%d:%d%d:%d%d.%d%d",n,hour/10,hour%10,minute/10,minute%10,second/10,second%10,millisecond/10,millisecond%10);
// SetWindowText(hWndMain,szTitle);
// fclose(temp);
}
PSOTSP::~PSOTSP()
{
delete g;
delete cspeed;
delete eva;
delete loc;
for(int i=0;i<scale;i++)
delete sworm[i];
delete[] sworm;
fclose(file1);
fclose(file2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -