蚁群算法解决旅行商问题c++


蚁群优化算法解决旅行商问题(TSP),基于c++编程平台经过调试,能够达到预期效果
资源截图
代码片段和文件信息
// Ant_System_Alogrithm.cpp: implementation of the CAnt_System_Alogrithm class.
//
//////////////////////////////////////////////////////////////////////

#include “stdafx.h“
#include “ASA.h“
#include “stdlib.h“
#include “stdio.h“
#include “math.h“
#include “Ant_System_Alogrithm.h“
#include “GamblingPad.h“

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

CAnt_System_Alogrithm::CAnt_System_Alogrithm()
{

}

//功能:初始化环境和蚁群信息
void CAnt_System_Alogrithm::init()
{
   FILE  *fp;
   if((fp=fopen(“el51.txt““r“))==NULL)
   {
::AfxMessageBox(“cannot open kroa100 file“);
    exit(0);
   }
   int ijxy;
   for(j=0; j   {
      fscanf(fp“%d%d%d“&i&x&y);
      citypos[i-1].x=x;
      citypos[i-1].y=y;
   }
   fclose(fp);
    
   //初始化城市距离dis_city[i][j],η[i][j]г[i][j]
   for(i=0; i   {
  dis_city[i][i] = 0;
  yita[i][i]=1;
  tao[i][i]=C+0.5;
      for(j=i+1; j   { 
     dis_city[i][j]  =(int)(  sqrt(    pow(citypos[i].x-citypos[j].x2 ) 
                             +pow(citypos[i].y-citypos[j].y 2)  )  +0.5 
   );
        dis_city[j][i]  =dis_city[i][j];
yita[i][j]=yita[j][i]=1.0/dis_city[i][j];
tao [i][j]=tao [j][i]=C+0.5;
  }
   }
   
   //初始化每只蚂蚁信息
   ::srand( (unsigned)time( NULL ) );
   for (i=0; i   {
   Ant[i].len=0;      //当前累积长度为0
   Ant[i].tailpos=0;  //路径末尾指示器为0
   Ant[i].curpos = rand()%CITY_NUM;   //当前位置随机设定
   //路径信息为空,所有顶点都未访问
   for (j=0; j    {
   Ant[i].path[j]=-1;
   Ant[i].flag[j]=FALSE;
   }
   //将当前位置加入路径信息
   Ant[i].path[Ant[i].tailpos++]=Ant[i].curpos;
   Ant[i].flag[Ant[i].curpos]=TRUE;
   }
    bestresult.len=999999; //初始化最优解长度
}

//功能:执行蚁群算法
void CAnt_System_Alogrithm::run()
{
int ik;
int nc;
int minlen=10000000;
int    NOofbestAnt =0;

//反复执行NCMAX次训练
for (nc=0; nc {
//每只蚂蚁都遍历城市一遍
for (k=0; k {
      findPath(k);
if (Ant[k].len {
minlen=Ant[k].len;
                NOofbestAnt=k;
}
}

//与最优解比较,更新最优解
if (Ant[NOofbestAnt].len
//更新信息素
        updateTao();

//初始化每只蚂蚁的信息,准备下一次循环
for (k=0; k {
        Ant[k].len=0;               //当前累积长度为0
        Ant[k].tailpos=0;           //路径末尾指示器为0
    //Ant[k].curpos = rand()%CITY_NUM;   //当前位置随机设定
for (i=0; i Ant[k].flag[i]=FALSE;

//将当前位置加入路径信息
Ant[k].path[Ant[k].tailpos++]=Ant[k].curpos;
        Ant[k].flag[Ant[k].curpos]=TRUE;
}

}//end of for (nc=0; nc}


void CAnt_System_Alogrithm::updateTao()
{
int ijk;
double temp;
dou

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       7300  2005-03-13 16:48  蚁群ASA算法Ant_System_Alogrithm.cpp

     文件       3569  2010-04-06 17:45  蚁群ASA算法Ant_System_Alogrithm.dsp

     文件        548  2010-04-06 17:46  蚁群ASA算法Ant_System_Alogrithm.dsw

     文件       1497  2005-03-13 16:24  蚁群ASA算法Ant_System_Alogrithm.h

     文件      33792  2010-04-06 17:46  蚁群ASA算法Ant_System_Alogrithm.ncb

     文件      48640  2010-04-06 17:46  蚁群ASA算法Ant_System_Alogrithm.opt

     文件       1616  2010-04-06 17:45  蚁群ASA算法Ant_System_Alogrithm.plg

     文件      24908  2005-03-09 13:58  蚁群ASA算法ASA.APS

     文件       1668  2005-03-09 17:12  蚁群ASA算法ASA.clw

     文件       3636  2005-03-09 13:57  蚁群ASA算法ASA.cpp

     文件       4591  2005-03-09 17:12  蚁群ASA算法ASA.dsp

     文件        531  2005-03-09 13:57  蚁群ASA算法ASA.dsw

     文件       1334  2005-03-09 13:57  蚁群ASA算法ASA.h

     文件      82944  2005-03-13 16:52  蚁群ASA算法ASA.ncb

     文件      53760  2005-03-13 16:52  蚁群ASA算法ASA.opt

     文件       1187  2005-03-13 16:48  蚁群ASA算法ASA.plg

     文件       8321  2005-03-09 13:57  蚁群ASA算法ASA.rc

     文件       1392  2005-03-11 16:54  蚁群ASA算法ChildView.cpp

     文件       1267  2005-03-09 13:57  蚁群ASA算法ChildView.h

     文件      30145  2010-04-06 17:45  蚁群ASA算法DebugAnt_System_Alogrithm.obj

     文件    5099840  2010-04-06 17:45  蚁群ASA算法DebugAnt_System_Alogrithm.pch

     文件      25600  2010-04-06 17:45  蚁群ASA算法DebugAnt_System_Alogrithm.pdb

     文件     189440  2010-04-06 17:45  蚁群ASA算法Debugvc60.idb

     文件      69632  2010-04-06 17:45  蚁群ASA算法Debugvc60.pdb

     目录          0  2010-04-06 17:45  蚁群ASA算法Debug

     文件        612  2002-10-22 19:48  蚁群ASA算法el51.txt

     文件        954  2005-03-13 16:23  蚁群ASA算法GamblingPad.cpp

     文件        840  2005-03-13 16:24  蚁群ASA算法GamblingPad.h

     文件       3280  2005-03-09 13:57  蚁群ASA算法MainFrm.cpp

     文件       1744  2005-03-09 13:57  蚁群ASA算法MainFrm.h

............此处省略15个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)