三九宝宝网宝宝教育教学论文

VB或C的蚁群算法源码

01月02日 编辑 39baobao.com

[检测编辑框的密码是否正确易语言源码]看你这密码存在什么地方的,以什么方式存的,一般的是 密码+扰乱码+MD5 得到的,即不可逆; '普通验证 .版本 2 .如果 (编辑1.内容=密码 ) 信息框 (“正确”, 0, ) .否则 信息框 (“错误”...+阅读

//建议使用vc2005以上的编译器,加油!!~!#include #include #include #include #include using namespace std; const int iAntCount=34;//蚂蚁数量 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[iCityCount];//最有路径列表 double rnd(int low,double uper)//获得随机数 { double p=(rand()/(double)RAND_MAX)*((uper)-(low))+(low); return (p); }; int rnd(int uper) { return (rand()%uper); }; class GInfo//tsp地图信息,包含了信息素,城市距离,和信息素变化矩阵 { public: double m_dDeltTrial[iCityCount][iCityCount]; double m_dTrial[iCityCount][iCityCount]; double distance[iCityCount][iCityCount]; }; GInfo Map; class ant { private: int ChooseNextCity();//选择城市 double prob[iCityCount]; int m_iCityCount; int AllowedCity[iCityCount];//没有走过的城市 public: void addcity(int city); int tabu[iCityCount]; void Clear(); void UpdateResult(); double m_dLength; double m_dShortest; void move(); ant(); void move2last(); }; void ant::move2last() { int i; for(i=0;i if (AllowedCity[i]==1) { addcity(i); break; } } void ant::Clear() { m_dLength=0; int i; for(i=0; i { prob[i]=0; AllowedCity[i]=1; i=tabu[iCityCount-1]; m_iCityCount=0; addcity(i); } } ant::ant() { m_dLength=m_dShortest=0; m_iCityCount=0; int i; for(i=0;i AllowedCity[i]=1; prob[i]=0; } } void ant::addcity(int city) { //add city to tabu; tabu[m_iCityCount]=city; m_iCityCount++; AllowedCity[city]=0; } int ant::ChooseNextCity() { //Update the probability of path selection //select a path from tabu[m_iCityCount-1] to next int i; int j=10000; double temp=0; int curCity=tabu[m_iCityCount-1]; for (i=0;i if((AllowedCity[i]==1)) { temp+=pow((1.0/Map.distance[curCity][i]),beta)*pow((Map.m_dTrial[curCity][i]),alpha); } } double sel=0; for (i=0;i if((AllowedCity[i]==1)) { prob[i]=pow((1.0/Map.distance[curCity][i]),(int)beta)*pow((Map.m_dTrial[curCity][i]),(int)alpha)/temp; sel+=prob[i]; } else prob[i]=0; } double mRate=rnd(0,sel); double mSelect=0; for ( i=0;i if((AllowedCity[i]==1)) mSelect+=prob[i] ; if (mSelect>=mRate) {j=i;break;} } if (j==10000) { temp=-1; for (i=0;i if((AllowedCity[i]==1)) if (temp) { temp=pow((1.0/Map.distance[curCity][i]),beta)*pow((double)(Map.m_dTrial[curCity][i]),alpha); j=i; } } } return j; } void ant::UpdateResult() { // Update the length of tour int i; for(i=0;i m_dLength+=Map.distance[tabu[i]][tabu[i+1]]; m_dLength+=Map.distance[tabu[iCityCount-1]][tabu[0]]; } 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[iAntCount]; 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[ants[i].tabu[j]][ants[i].tabu[j+1]]+=Q/ants[i].m_dLength ; Map.m_dDeltTrial[ants[i].tabu[j+1]][ants[i].tabu[j]]+=Q/ants[i].m_dLength; } Map.m_dDeltTrial[ants[i].tabu[iCityCount-1]][ants[i].tabu[0]]+=Q/ants[i].m_dLength; Map.m_dDeltTrial[ants[i].tabu[0]][ants[i].tabu[iCityCount-1]]+=Q/ants[i].m_dLength; } for (i=0;i for (j=0;j { Map.m_dTrial[i][j]=(rou*Map.m_dTrial[i][j]+Map.m_dDeltTrial[i][j] ); Map.m_dDeltTrial[i][j]=0; } } } void project::initmap() { int i; int j; for(i=0;i for (j=0;j { Map.m_dTrial[i][j]=1; Map.m_dDeltTrial[i][j]=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; int y; }cc[iCityCount]; for (int i=0;i { in>>cc[i].num>>cc[i].x>>cc[i].y; besttour[i]=0; } int j; for(int i=0;i for (j=0;j { { Map.distance[i][j]=sqrt(pow((double)(cc[i].x-cc[j].x),2)+pow((double)(cc[i].y-cc[j].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[i].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[iCityCount]; while (max { for(j=0;j { for (i=0;i ants[j].move(); } for(j=0;j { ants[j].move2last(); ants[j].UpdateResult (); } //find out the best solution of the step and put it into temp int t; temp=ants[0].m_dLength; for (t=0;t temptour[t]=ants[0].tabu[t]; for(j=0;j { if (temp>ants[j].m_dLength) { temp=ants[j].m_dLength; for ( t=0;t temptour[t]=ants[j].tabu[t]; } } if(temp m_dLength=temp; for ( t=0;t besttour[t]=temptour[t]; } printf("%d : %f\n",max,m_dLength); UpdateTrial(); for(j=0;j ants[j].Clear(); max++; } printf("The shortest toure is : %f\n",m_dLength); for ( int t=0;t printf(" %d ",besttour[t]); } int main() { project TSP; TSP.GetAnt()...

以下为关联文档:

c语言实现二叉树的先序中序后序的递归和非递归算法和层次遍历#include// malloc()等 #include// 标准输入输出头文件,包括EOF(=^Z或F6),NULL等 #include// atoi(),exit() #include// 数学函数头文件,包括floor(),ceil(),abs()等#define Cle...

几种数字签名方案算法的研究与设计随着信息技术的飞速发展和信息设备的广泛应用,信息时代虽然带给我们无限商机与方便,但也充斥着隐患与危险.例如网络容易受到攻击,(略)的泄密、数据被篡改,轻则引发企业、部门工作...

在C语言编程中数据结构与算法是怎么体现的或者说怎么理解数据结举个例子,你写个程序,要存全校学生的信息。 首先学生数量可能是变动的,你不可能写成固定数组。 你可以思考下怎么存。 这时候就可以建立一个链表,在每次输入一个新生时把他挂入...

数据结构与算法分析 c语言描述怎么样Data Structures and Algorithm Analysis in C 原书曾被评为20世纪顶尖的30部计算机著作之一,作者Mark Allen Weiss在数据结构和算法分析方面卓有建树,他的数据结构和算法分析...

急!高手请进!c语言中数据结构与算法是用来干嘛的算法中的代码一般都是伪代码,,告诉你的是一种思想, 复制过来肯定不能直接使用,,你可以稍微的改改 ,编译一下,便可执行,得到你想要的结果,不是很难。 伪代码中一般不怎么声明,拿来直接...

C数据结构算法这是什么关系1、C 计算机编程语言。(计算机可以识别,并且执行的程序就是靠这些高级计算机语言实现) 2、数据结构指的是数据之间的相互关系,即数据的组织形式。 1.数据结构一般包括以下三方面...

C语言数据结构与算法分析C语言描述Position不是一个类型,起码C语言中,我写那么多年代码没见过这个类型 。你该把整段代码贴上来。我猜你看的那段代码是伪代码,Position是自定义类型。若Position是类名,那么Positi...

蚁群算法求解最短路的C代码你好,希望对你有所帮助using System;using System.Collections.Generic;using System.Text;namespace AntSystem{ public class AA { /**/////// 对信息量的重视程度 ///priv...

蚁群算法的内容蚁群算法又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型算法。它由Marco Dorigo于1992年在他的博士论文中提出,其灵感来源于蚂蚁在寻找食物过程中发现路径的行为。蚁群算...

推荐阅读
图文推荐