ken1983414 发表于 2006-11-15 21:01

基本蚁群算法程序

//基本蚁群算法程序

//程序在vc++6.0下面同过,对原来的做了一点修改。
//你可以使用本代码,如果感到对你有用的话,请通知作者,作者会很高兴。
//通讯地址:fashionxu@163.com
//by FashionXu
#include
#include
#include
#include
using namespace std;


const int iAntCount=34;//ant numbers
const int iCityCount=51;
const int iItCount=2000;
const double Q=100;
const double alpha=1;
const double beta=5;
const double rou=0.5;

int besttour;

doublernd(int low,double uper)
{
double p=(rand()/(double)RAND_MAX)*((uper)-(low))+(low);

return (p);
};
int rnd(int uper)
{
return (rand()%uper);
};

class GInfo
{
public:
double m_dDeltTrial;//信息素增量
double m_dTrial;//信息素痕迹
double distance;//距离
};


GInfo Map;
class ant
{
private:
int ChooseNextCity();
double prob;//转移概率
int m_iCityCount;
int AllowedCity;
public:
void addcity(int city);
int tabu;//行走路径
void Clear();
void UpdateResult();
double m_dLength;
double m_dShortest;
void move();
ant();
void move2last();
};
void ant::move2last()
{
int i;
for(i=0;iif (AllowedCity==1)
{
   addcity(i);
   break;
}
}
void ant::Clear()
{
m_dLength=0;
int i;
for(i=0;i {
prob=0;
AllowedCity=1;
}
i=tabu;
m_iCityCount=0;
addcity(i);
}
ant::ant()
{
m_dLength=m_dShortest=0;
m_iCityCount=0;
int i;
for(i=0;i {
AllowedCity=1;
prob=0;
}
}
void ant::addcity(int city)
{
//add city to tabu;
tabu=city;
m_iCityCount++;
AllowedCity=0;
}
int ant::ChooseNextCity()
{
//Update the probability of path selection
//select a path from tabu to next


int i;
int j=10000;
double temp=0;
int curCity=tabu;
for (i=0;i
{
if((AllowedCity==1))
{
   temp+=pow((1.0/Map.distance),beta)*pow((Map.m_dTrial),alpha);
                        //距离                           //残留信息量
}
}
double sel=0;
for (i=0;i
{
if((AllowedCity==1))
{
   prob=pow((1.0/Map.distance),beta)*pow((Map.m_dTrial),alpha)/temp;
   sel+=prob;
}
else
   prob=0;
}
double mRate=rnd(0,sel);
double mSelect=0;

for ( i=0;i
{
if((AllowedCity==1))
   mSelect+=prob ;
if (mSelect>=mRate)
{j=i;break;}
}

if (j==10000)
{
temp=-1;
for (i=0;i
{
   if((AllowedCity==1))
    if (temp   
   {
   temp=pow((1.0/Map.distance),beta)*pow((Map.m_dTrial),alpha);
   j=i;
    }
}
}

return j;

}
void ant::UpdateResult()
{
// Update the length of tour
int i;
for(i=0;im_dLength+=Map.distance]];
m_dLength+=Map.distance]];
}
void ant::move()
{
//the ant move to next town and add town ID to tabu.
int j;
j=ChooseNextCity();
addcity(j);
}
class project
{
public:

void UpdateTrial();
double m_dLength;
void initmap();
ant ants;
void GetAnt();
void StartSearch();
project();
};
void project::UpdateTrial()
{
//calculate the changes of trial information
int i;
int j;

for(i=0;i {
for (j=0;j{
   Map.m_dDeltTrial.tabu].tabu]+=Q/ants.m_dLength ;
   Map.m_dDeltTrial.tabu].tabu]+=Q/ants.m_dLength;
}
Map.m_dDeltTrial.tabu].tabu]+=Q/ants.m_dLength;
Map.m_dDeltTrial.tabu].tabu]+=Q/ants.m_dLength;
}
for (i=0;i {
for (j=0;j{
   Map.m_dTrial=(rou*Map.m_dTrial+Map.m_dDeltTrial );
   Map.m_dDeltTrial=0;
}

}


}
void project::initmap()
{
int i;
int j;
for(i=0;ifor (j=0;j{

   Map.m_dTrial=1;
   Map.m_dDeltTrial=0;
}
}
project::project()
{
//initial map,read map infomation from file . et.
initmap();
m_dLength=10e9;


ifstream in("eil51.tsp");

struct city
{
int num;
int x;
inty;
}cc;

for (int i=0;i
{
in>>cc.num>>cc.x>>cc.y;
besttour=0;
}
int j;
for(i=0;ifor (j=0;j
{
   {
    Map.distance=sqrt(pow((cc.x-cc.x),2)+pow((cc.y-cc.y),2));
   }
}


}
void project::GetAnt()
{
//randomly put ant into map
int i=0;
int city;
srand( (unsigned)time( NULL ) +rand());
for (i=0;i {
city=rnd(iCityCount);
ants.addcity(city);
}

}
void project::StartSearch()
{
//begin to find best solution
int max=0;//every ant tours times
int i;
int j;
double temp;
int temptour;
while (max
{
for(j=0;j

{
   for (i=0;i    ants.move();
}

for(j=0;j
{
   ants.move2last();
   ants.UpdateResult ();
}

//find out the best solution of the step and put it into temp
int t;
temp=ants.m_dLength;
for (t=0;t   
temptour=ants.tabu;
for(j=0;j
{
   if (temp>ants.m_dLength)
   {
    temp=ants.m_dLength;
    for ( t=0;t   temptour=ants.tabu;
   }

}

if(temp   
m_dLength=temp;
   for ( t=0;t
{
   besttour=temptour;
}
printf("%d : %f\n",max,m_dLength);
UpdateTrial();

for(j=0;j   ants.Clear();

max++;

}
printf("The shortest toure is : %f\n",m_dLength);

for ( int t=0;tprintf(" %d ",besttour);

}
int main()
{

project TSP;
TSP.GetAnt();
TSP.StartSearch();
return 0;
}

[ 本帖最后由 风花雪月 于 2006-11-20 08:43 编辑 ]

xingconglan 发表于 2007-1-5 11:23

请问有matlab版的吗?

风花雪月 发表于 2007-1-29 13:04

原帖由 xingconglan 于 2007-1-5 11:23 发表
请问有matlab版的吗?

去matlab版找找看,这里不讨论matlab问题

mgtong 发表于 2007-2-6 22:58

求助

有C版本的吗??
万分感激

风花雪月 发表于 2007-2-28 14:35

原帖由 mgtong 于 2007-2-6 22:58 发表
有C版本的吗??
万分感激

简单修改一下上面的程序差不多就能运行了,别偷懒了

yansijie 发表于 2007-5-21 20:58

谢谢啦

一支小布丁 发表于 2007-6-6 21:09

似乎代码有些问题

风花雪月 发表于 2007-6-8 10:44

原帖由 一支小布丁 于 2007-6-6 21:09 发表 http://www.chinavib.com/forum/images/common/back.gif
似乎代码有些问题

把错误信息给一下,我没有装vc6.0

lijinhan83 发表于 2007-7-20 19:42

哥们,你都include什么了啊,怎么都看不到

风花雪月 发表于 2007-7-23 16:35

原帖由 lijinhan83 于 2007-7-20 19:42 发表 http://www.chinavib.com/forum/images/common/back.gif
哥们,你都include什么了啊,怎么都看不到

应该是

#include <iostream>
#include <fstream>
#include <math.h>
#include <time.h>

lijinhan83 发表于 2007-7-26 15:07

谢谢风花雪月的支持,试试先了。

lijinhan83 发表于 2007-7-26 15:17

错误提示

错误提示,在vc++6.0下运行提示
--------------------Configuration: 基本蚁群算法程序 - Win32 Debug--------------------
Compiling...
基本蚁群算法程序.cpp
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(66) : error C2143: syntax error : missing ';' before 'if'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(66) : error C2143: syntax error : missing ')' before 'if'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(76) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(76) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(89) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(89) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(112) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(112) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(121) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(121) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(134) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(134) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(145) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(145) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(148) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(162) : error C2146: syntax error : missing ';' before identifier 'm_dLength'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(163) : error C2146: syntax error : missing ')' before identifier 'm_dLength'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(163) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(190) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(190) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(191) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(191) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(198) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(198) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(199) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(199) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(212) : error C2143: syntax error : missing ';' before 'for'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(212) : error C2143: syntax error : missing ')' before 'for'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(212) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(212) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(235) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(235) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(240) : error C2143: syntax error : missing ';' before 'for'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(240) : error C2143: syntax error : missing ')' before 'for'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(241) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(241) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(255) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(255) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(270) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(273) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(273) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(274) : error C2146: syntax error : missing ';' before identifier 'ants'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(275) : error C2143: syntax error : missing ')' before '}'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(275) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(275) : error C2143: syntax error : missing ';' before '}'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(278) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(278) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(287) : error C2146: syntax error : missing ';' before identifier 'temptour'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(288) : error C2143: syntax error : missing ')' before 'for'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(288) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(289) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(289) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(293) : error C2146: syntax error : missing ';' before identifier 'temptour'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(294) : error C2143: syntax error : missing ')' before '}'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(294) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(294) : error C2143: syntax error : missing ';' before '}'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(299) : error C2146: syntax error : missing ')' before identifier 'm_dLength'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(301) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(301) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(307) : error C2146: syntax error : missing ';' before identifier 'ants'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(309) : error C2146: syntax error : missing ')' before identifier 'max'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(309) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(314) : error C2146: syntax error : missing ';' before identifier 'printf'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(316) : error C2143: syntax error : missing ')' before '}'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(316) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(316) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.

基本蚁群算法程序.obj - 66 error(s), 0 warning(s)

风花雪月 发表于 2007-7-29 15:35

原帖由 lijinhan83 于 2007-7-26 15:17 发表 http://www.chinavib.com/forum/images/common/back.gif
错误提示,在vc++6.0下运行提示
--------------------Configuration: 基本蚁群算法程序 - Win32 Debug--------------------
Compiling...
基本蚁群算法程序.cpp
d:\dev-cpp\小工程\基本蚁群算法程序.cpp( ...

http://www.chinavib.com/forum/thread-49303-1-1.html

请勿重复发帖
页: [1]
查看完整版本: 基本蚁群算法程序