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

📄 actsp.cpp

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

#include "stdafx.h"
#include "ACTsp.h"
#include "TSP.h"

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

extern int hour;
extern int minute;
extern int second;
extern int millisecond;
extern int nowround;
extern double * gb;
extern clock_t avetime;

ACTsp::ACTsp(Adaptability * adapt,Parameter * param)
{
	int dimension=(int)param->CharToInt(param->GetCombo(5));
	double expp=param->CharToInt(param->GetCombo(6));
	double expd=param->CharToInt(param->GetCombo(7));
	scale=(int)param->CharToInt(param->GetCombo(1));
	double initp=param->CharToInt(param->GetCombo(13));
	double pd=1-param->CharToInt(param->GetCombo(14));
	char * name=new char[strlen(param->GetCombo(5))];
	strcpy(name,param->GetCombo(5));
	strcat(name,".zn");
	antcolony=new Ant *[scale];
	RouteInfo * rinfo;
	if(strcmp(param->GetCombo(15),"TSP")==0)
	//初始化蚁群
	{
		for(int i=0;i<scale;i++)
			antcolony[i]=new AntTsp(adapt,dimension);
	//路径信息
		rinfo=new RouteInfoTsp(dimension,pd,initp,name);
	}
	maxgen=(int)param->CharToInt(param->GetCombo(2));
	rand=new Rand;
	//信息释放方式
	UpdateInfo * update;
	if(strcmp(param->GetCombo(3),"Q")==0)
	{
		update=new UpdateInfo2(rinfo,dimension);
	}
	if(strcmp(param->GetCombo(3),"Q/dij")==0)
	{
		update=new UpdateInfo4(rinfo,dimension);
	}
	if(strcmp(param->GetCombo(3),"Q/L")==0)
	{
		update=new UpdateInfo1(rinfo,dimension);
	}
	if(strcmp(param->GetCombo(3),"正比于最大浓度之差")==0)
	{
		update=new UpdateInfo3(rinfo,dimension,initp/100,initp);
	}
	//信息更新方式
	action=new NextActionTsp(antcolony,rinfo,update,scale,dimension,expd,expp);
	if(strcmp(param->GetCombo(4),"全部更新")==0)
		signal=1;
	if(strcmp(param->GetCombo(4),"只更新最优")==0)
		signal=2;
	file1=fopen("location.zn","w");
	file2=fopen("solution.zn","w");
/*	FILE * file ;
	file=fopen("2.zn","w");
//	fprintf(file,"%f    %f    %f    %d\n",ph,result,dif[0],length);
	
	fprintf(file,"%f  ",loc_max);
	fprintf(file,"%f  ",loc_min);
	fprintf(file,"%d  ",dimension);
	fprintf(file,"%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(file,"%d\n",scale);fclose(file);*/
}

void ACTsp::Run()
{
	clock_t star;
	star=clock();
	clock_t now;
	double time;
	char szTitle[128];
	int n=0;
	while(n<maxgen)
	{
		action->InitProportion();
		action->NewRoute(rand);
		if(signal==1)
			action->UpdateIA();
		if(signal==2)
			action->UpdateIB();
		for(int i=0;i<scale;i++)
			antcolony[i]->Store(file1,file2);
		n++;
		now=clock();
		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);
	}
	avetime+=now-star;
	gb[nowround]=action->GetBS();
}

ACTsp::~ACTsp()
{
	for(int i=0;i<scale;i++)
		delete antcolony[i];
	delete[] antcolony;
	fclose(file1);
	fclose(file2);
	delete action;
	delete rand;
}

⌨️ 快捷键说明

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