导航:首页 > 源码编译 > 蚁群算法源代码

蚁群算法源代码

发布时间:2022-07-12 12:57:11

Ⅰ 蚁群算法是什么

蚁群算法,又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型算法。 它由Marco Dorigo于1992年在他的博士论文中提出,其灵感来源于蚂蚁在寻找食物过程中发现路径的行为。蚁群算法是一种模拟进化算法,初步的研究表明该算法具有许多优良的性质。针对PID控制器参数优化设计问题,将蚁群算法设计的结果与遗传算法设计的结果进行了比较,数值仿真结果表明,蚁群算法具有一种新的模拟进化优化方法的有效性和应用价值。

原理
设想,如果我们要为蚂蚁设计一个人工智能的程序,那么这个程序要多么复杂呢?首先,你要让蚂蚁能够避开障碍物,就必须根据适当的地形给它编进指令让他们能够巧妙的避开障碍物,其次,要让蚂蚁找到食物,就需要让他们遍历空间上的所有点;再次,如果要让蚂蚁找到最短的路径,那么需要计算所有可能的路径并且比较它们的大小,而且更重要的是,你要小心翼翼地编程,因为程序的错误也许会让你前功尽弃。这是多么不可思议的程序!太复杂了,恐怕没人能够完成这样繁琐冗余的程序。

然而,事实并没有你想得那么复杂,上面这个程序每个蚂蚁的核心程序编码不过100多行!为什么这么简单的程序会让蚂蚁干这样复杂的事情?答案是:简单规则的涌现。事实上,每只蚂蚁并不是像我们想象的需要知道整个世界的信息,他们其实只关心很小范围内的眼前信息,而且根据这些局部信息利用几条简单的规则进行决策,这样,在蚁群这个集体里,复杂性的行为就会凸现出来。这就是人工生命、复杂性科学解释的规律!那么,这些简单规则是什么呢?

Ⅱ 蚁群算法求解TSP问题的源程序及简要说明

该程序试图对具有31个城市的VRP进行求解,已知的最优解为784.1,我用该程序只能优化到810左右,应该是陷入局部最优,但我不知问题出在什么地方。请用过蚁群算法的高手指教。
蚁群算法的matlab源码,同时请指出为何不能优化到已知的最好解

%
%
% the procere of ant colony algorithm for VRP
%
% % % % % % % % % % %

%initialize the parameters of ant colony algorithms
load data.txt;
d=data(:,2:3);
g=data(:,4);

m=31; % 蚂蚁数
alpha=1;
belta=4;% 决定tao和miu重要性的参数
lmda=0;
rou=0.9; %衰减系数
q0=0.95;
% 概率
tao0=1/(31*841.04);%初始信息素
Q=1;% 蚂蚁循环一周所释放的信息素
defined_phrm=15.0; % initial pheromone level value
QV=100; % 车辆容量
vehicle_best=round(sum(g)/QV)+1; %所完成任务所需的最少车数
V=40;

% 计算两点的距离
for i=1:32;
for j=1:32;
dist(i,j)=sqrt((d(i,1)-d(j,1))^2+(d(i,2)-d(j,2))^2);
end;
end;

%给tao miu赋初值
for i=1:32;
for j=1:32;
if i~=j;
%s(i,j)=dist(i,1)+dist(1,j)-dist(i,j);
tao(i,j)=defined_phrm;
miu(i,j)=1/dist(i,j);
end;
end;
end;

for k=1:32;
for k=1:32;
deltao(i,j)=0;
end;
end;

best_cost=10000;
for n_gen=1:50;

print_head(n_gen);

for i=1:m;
%best_solution=[];
print_head2(i);
sumload=0;
cur_pos(i)=1;
rn=randperm(32);
n=1;
nn=1;
part_sol(nn)=1;
%cost(n_gen,i)=0.0;
n_sol=0; % 由蚂蚁产生的路径数量
M_vehicle=500;
t=0; %最佳路径数组的元素数为0

while sumload<=QV;

for k=1:length(rn);
if sumload+g(rn(k))<=QV;
gama(cur_pos(i),rn(k))=(sumload+g(rn(k)))/QV;
A(n)=rn(k);
n=n+1;
end;
end;

fid=fopen('out_customer.txt','a+');
fprintf(fid,'%s %i\t','the current position is:',cur_pos(i));
fprintf(fid,'\n%s','the possible customer set is:')
fprintf(fid,'\t%i\n',A);
fprintf(fid,'------------------------------\n');
fclose(fid);

p=compute_prob(A,cur_pos(i),tao,miu,alpha,belta,gama,lmda,i);
maxp=1e-8;
na=length(A);
for j=1:na;
if p(j)>maxp
maxp=p(j);
index_max=j;
end;
end;

old_pos=cur_pos(i);
if rand(1)<q0
cur_pos(i)=A(index_max);
else
krnd=randperm(na);
cur_pos(i)=A(krnd(1));
bbb=[old_pos cur_pos(i)];
ccc=[1 1];
if bbb==ccc;
cur_pos(i)=A(krnd(2));
end;
end;

tao(old_pos,cur_pos(i))=taolocalupdate(tao(old_pos,cur_pos(i)),rou,tao0);%对所经弧进行局部更新

sumload=sumload+g(cur_pos(i));

nn=nn+1;
part_sol(nn)=cur_pos(i);
temp_load=sumload;

if cur_pos(i)~=1;
rn=setdiff(rn,cur_pos(i));
n=1;
A=[];
end;

if cur_pos(i)==1; % 如果当前点为车场,将当前路径中的已访问用户去掉后,开始产生新路径
if setdiff(part_sol,1)~=[];
n_sol=n_sol+1; % 表示产生的路径数,n_sol=1,2,3,..5,6...,超过5条对其费用加上车辆的派遣费用
fid=fopen('out_solution.txt','a+');
fprintf(fid,'%s%i%s','NO.',n_sol,'条路径是:');
fprintf(fid,'%i ',part_sol);
fprintf(fid,'\n');
fprintf(fid,'%s','当前的用户需求量是:');
fprintf(fid,'%i\n',temp_load);
fprintf(fid,'------------------------------\n');
fclose(fid);

% 对所得路径进行路径内3-opt优化
final_sol=exchange(part_sol);

for nt=1:length(final_sol); % 将所有产生的路径传给一个数组
temp(t+nt)=final_sol(nt);
end;
t=t+length(final_sol)-1;

sumload=0;
final_sol=setdiff(final_sol,1);
rn=setdiff(rn,final_sol);
part_sol=[];
final_sol=[];
nn=1;
part_sol(nn)=cur_pos(i);
A=[];
n=1;

end;
end;

if setdiff(rn,1)==[];% 产生最后一条终点不为1的路径
n_sol=n_sol+1;
nl=length(part_sol);
part_sol(nl+1)=1;%将路径的最后1位补1

% 对所得路径进行路径内3-opt优化
final_sol=exchange(part_sol);

for nt=1:length(final_sol); % 将所有产生的路径传给一个数组
temp(t+nt)=final_sol(nt);
end;

cost(n_gen,i)=cost_sol(temp,dist)+M_vehicle*(n_sol-vehicle_best); %计算由蚂蚁i产生的路径总长度

for ki=1:length(temp)-1;
deltao(temp(ki),temp(ki+1))=deltao(temp(ki),temp(ki+1))+Q/cost(n_gen,i);
end;

if cost(n_gen,i)<best_cost;
best_cost=cost(n_gen,i);
old_cost=best_cost;
best_gen=n_gen; % 产生最小费用的代数
best_ant=i; %产生最小费用的蚂蚁
best_solution=temp;
end;

if i==m; %如果所有蚂蚁均完成一次循环,,则用最佳费用所对应的路径对弧进行整体更新
for ii=1:32;
for jj=1:32;
tao(ii,jj)=(1-rou)*tao(ii,jj);
end;
end;

for kk=1:length(best_solution)-1;
tao(best_solution(kk),best_solution(kk+1))=tao(best_solution(kk),best_solution(kk+1))+deltao(best_solution(kk),best_solution(kk+1));
end;
end;

fid=fopen('out_solution.txt','a+');
fprintf(fid,'%s%i%s','NO.',n_sol,'路径是:');
fprintf(fid,'%i ',part_sol);
fprintf(fid,'\n');
fprintf(fid,'%s %i\n','当前的用户需求量是:',temp_load);
fprintf(fid,'%s %f\n','总费用是:',cost(n_gen,i));
fprintf(fid,'------------------------------\n');
fprintf(fid,'%s\n','最终路径是:');
fprintf(fid,'%i-',temp);
fprintf(fid,'\n');
fclose(fid);
temp=[];
break;
end;
end;

end;
end;
我现在也在研究它,希望能共同进步.建义可以看一下段海滨的关于蚁群算法的书.讲的不错,李士勇的也可以,还有一本我在图书馆见过,记不得名字了.

Ⅲ 求基本蚁群算法C++代码

if (featureData[i]!=0)
{
str1.Format("%d",featureData[i]);
pDC->TextOut( nDeltaHorz*((i-pFrm->m_startPIN)+2)+20, nDeltaVert*8, str1 );
}

Ⅳ 求教:蚁群算法选择最短路径问题

这个例子其实是当初数模比赛时用来完成碎片拼接的,但其所用到原理还是求解最短路径的原理。但这里的最短路径和数据结构中最短路径有一定的区别。在数据结构中,对于最短路径的求解常用的一般有Dijkstra算法与Floyd算法,但对于要求出一条经过所有的点的并且要求路径最短,这些算法还是有一定的局限性的。而蚁群算法则很好地满足了这些条件。话说回来,很想吐槽一下网络流传的一些蚁群算法的例子,当初学习这个时候,身边也没有相关的书籍,只好到网上找例子。网上关于这个算法源代码的常见的有2个版本,都是出自博客,但是在例子都代码是不完整的,缺失了一部分,但就是这样的例子,居然流传甚广,我很好奇那些转载这些源码的人是否真的有去学习过这些,去调试过。当然,我下面的例子也是无法直接编译通过的,因为涉及到图像读取处理等方面的东西,所以就只贴算法代码部分。但是对于这个问题蚁群算法有一个比较大的缺点,就是收敛很慢,不过对于数量小的路径,效果还是很好的。function bestqueue =aco1(nt,nc_max,m ,st, sd ,Alpha ,Beta ,Rho ,Q,gethead,getend)%参数解释:%nt 路径所经过的点的个数;%nc_max 迭代的次数;%m 蚂蚁的个数;%st 起点序号;%sd 终点序号;%Alpha 信息素系数;�ta 启发因子系数;%Rho 蒸发系数;% Q 信息量;%gethead getend 是用来求距离矩阵的,可根据实际情况修改
% nt = 209;%碎片个数full = zeros(nt,nt);tic;%初始化距离矩阵for i =1:nt for t = 1:nt if i ~= t full(i,t) = sum(abs(getend(:,i) - gethead(:,t))); else full(i,t) = inf; end endend% a =full(156,187)eta = 1./full;%启发因子,取距离的倒数% eta% e = eta(4,2)tau = ones(nt,nt);%信息素矩阵% tabu = zeros(nt,nt);%禁忌矩阵,取蚂蚁数量和碎片数量一致,以减少迭代次数nc =1;%初始化迭代次数;rbest=zeros(nc_max,nt);%各代最佳路线rbest(:,1) = (linspace(st,st,nc_max))';rbest(:,nt) =(linspace(sd,sd,nc_max))'; lbest=zeros(nc_max,1);%各代最佳路线的长度pathlen = 0;%临时记录每代最佳路线长度stime = 1;%记录代数进度for i = 1:nc_max % 代数循环 delta_tau=zeros(nt,nt);%初始化改变量 stime for t = 1:m % 对蚂蚁群体的循环, tabu=zeros(1,nt);%禁忌向量,标记已访问的碎片,初试值设为0,访问之后则变为1; viseted = zeros(1,nt);%记录已访问的元素的位置 tabu(st) = 1;%st为起点,在此表示为碎片矩阵的编号,因为已经将蚁群放在起点,故也应将禁忌向量和位置向量的状态进行修改 tabu(sd) =1;%同上 visited(nt) = sd ;%同上; visited(1) = st;%同上; ht = 0; for r = 2:nt-1 %记录了还没访问的图片编号 vp = 1;%visited指示量 pp = [];%置空的概率向量 jc = 0; %获取尚未访问的位置的向量。 wv = zeros( nt -2 - ht ); for k =1 : nt if tabu(k) == 0 jc = jc +1; wv(jc) = k; end end% a =(tau(visited(end),ju(3))^Alpha)*(eta(visited(end),ju(3))^Beta)% visited(end) %计算选择的概率 for k=1:length(wv) pp(k)=(tau(visited(vp),wv(k))^Alpha)*(eta(visited(vp),wv(k))^Beta);%下一张碎片的选择概率计算,p =(信息素^信息素系数)*(启发因子^启发因子系数) end pp=pp./(sum(pp));%归一化 pcum =cumsum(pp); psl = find(pcum >= rand);%轮盘赌法 to_visit= wv(psl(1)) ;%完成选点 tabu(to_visit) =1; visited(r) = to_visit; ht =ht +1;%已访问碎片个数变化 vp =vp+1; end %路径变化信息 %对单个蚂蚁的路径进行统计 sum1 =0; for pr = 1:nt -1 x = visited(pr); y = visited(pr+1) ; sum1 =sum1 + full(x,y); end% vcell{t} =visited;%元胞记录每个蚂蚁的路径,即碎片顺序;% msum(t) = sum1; %信息素变化; for ww=1:(nt-1) delta_tau(visited(ww),visited(ww+1))=delta_tau(visited(ww),visited(ww+1)) + Q/sum1; end% delta_tau(visited(end),visited(1))=delta_tau(visited(end),visited(1))+Q/(sum1/100);% if t == m & i == nc_max % bestqueue = visited% end if t == m bestqueue = visited end end tau=(1-Rho).*tau+delta_tau; %完成信息素的更新,找出现有的最新的最佳路径,即信息素最多的路径; stime =stime +1;end toc;

Ⅳ 蚁群优化算法的蚁群优化算法

开本: 16开
所属分类: 图书 >> 计算机/网络 >> 人工智能
定价:¥43.00 主要内容包括蚁群算法基本原理、蚁群算法在TSP及其扩展问题求解中的应用、蚁群算法在VRP及其扩展问题求解中的应用、蚁群算法在最优树问题求解中的应用、蚁群算法在整数规划问题求解中的应用、一般连续优化问题的蚁群算法以及多目标蚁群算法等。书中还给出了一些主要算法的Delphi程序实现源代码,可供参考或修改使用。
本书可供运筹学、管理科学、系统工程、计算机科学等有关专业的高校师生、科研人员和工程技术人员阅读参考。

Ⅵ 急求蚁群算法解决 VRPTW问题的matlab代码,最好是ACS或者MMAS的!

function [R_best,L_best,L_ave,Shortest_Route,Shortest_Length]=ACATSP(C,NC_max,m,Alpha,Beta,Rho,Q)
%%=========================================================================
%% ACATSP.m
%% Ant Colony Algorithm for Traveling Salesman Problem
%% ChengAihua,PLA Information Engineering University,ZhengZhou,China
%% Email:[email protected]
%% All rights reserved
%%-------------------------------------------------------------------------
%% 主要符号说明
%% C n个城市的坐标,n×2的矩阵
%% NC_max 最大迭代次数
%% m 蚂蚁个数
%% Alpha 表征信息素重要程度的参数
%% Beta 表征启发式因子重要程度的参数
%% Rho 信息素蒸发系数
%% Q 信息素增加强度系数
%% R_best 各代最佳路线
%% L_best 各代最佳路线的长度
%% 运行可能要很久,需要耐心等待
%%=========================================================================

n=length(C); %n 为市个数
for i=1:n %坐标矩阵转换为距离矩阵
for j=1:n
D(i,j)=sqrt((x(i,1)-x(j,1))^2+(x(i,2)-x(j,2))^2);
end
end
for i=1:n %Eta为启发因子,这里设为距离的倒数
for j=1:n %原文作者少考虑的当D=0是MATLAB提示出错
if i~=j
Eta(i,j)=1./D(i,j);
end
end
end
for i=1:n
Eta(i,i)=0;
end
Tau=ones(n,n); %Tau为信息素矩阵
Tabu=zeros(m,n); %存储并记录路径的生成
NC=1; %迭代计数器
R_best=zeros(NC_max,n); %各代最佳路线
L_best=inf.*ones(NC_max,1); %各代最佳路线的长度
L_ave=zeros(NC_max,1); %各代路线的平均长度

while NC<=NC_max %停止条件之一:达到最大迭代次数
%%第二步:将m只蚂蚁放到n个城市上
Randpos=[];
for i=1:(ceil(m/n))
Randpos=[Randpos,randperm(n)];
end
Tabu(:,1)=(Randpos(1,1:m))';

%%第三步:m只蚂蚁按概率函数选择下一座城市,完成各自的周游
for j=2:n
for i=1:m
visited=Tabu(i,1:(j-1)); %已访问的城市
J=zeros(1,(n-j+1)); %待访问的城市
P=J; %待访问城市的选择概率分布
Jc=1;
for k=1:n
if length(find(visited==k))==0
J(Jc)=k;
Jc=Jc+1;
end
end
%下面计算待选城市的概率分布
for k=1:length(J)
P(k)=(Tau(visited(end),J(k))^Alpha)*(Eta(visited(end),J(k))^Beta);
end
P=P/(sum(P));
%按概率原则选取下一个城市
Pcum=cumsum(P);
Select=find(Pcum>=rand);
to_visit=J(Select(1));
Tabu(i,j)=to_visit;
end
end
if NC>=2
Tabu(1,:)=R_best(NC-1,:);
end

%%第四步:记录本次迭代最佳路线
L=zeros(m,1);
for i=1:m
R=Tabu(i,:);
for j=1:(n-1)
L(i)=L(i)+D(R(j),R(j+1));
end
L(i)=L(i)+D(R(1),R(n));
end
L_best(NC)=min(L);
pos=find(L==L_best(NC));
R_best(NC,:)=Tabu(pos(1),:);
L_ave(NC)=mean(L);
NC=NC+1;

%%第五步:更新信息素
Delta_Tau=zeros(n,n);
for i=1:m
for j=1:(n-1)
Delta_Tau(Tabu(i,j),Tabu(i,j+1))=Delta_Tau(Tabu(i,j),Tabu(i,j+1))+Q/L(i);
end
Delta_Tau(Tabu(i,n),Tabu(i,1))=Delta_Tau(Tabu(i,n),Tabu(i,1))+Q/L(i);
end
Tau=(1-Rho).*Tau+Delta_Tau;

%%第六步:禁忌表清零
Tabu=zeros(m,n);
end

%%第七步:输出结果
Pos=find(L_best==min(L_best));
Shortest_Route=R_best(Pos(1),:);
Shortest_Length=L_best(Pos(1));
DrawRoute(C,Shortest_Route) %调用函数绘图

Ⅶ VB或C++的蚁群算法源码

//建议使用vc2005以上的编译器,加油!!~!

#include <stdio.h>
#include <cmath>
#include <iostream>
#include <fstream>
#include <time.h>
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<iCityCount;i++)

if (AllowedCity[i]==1)
{
addcity(i);
break;
}
}

void ant::Clear()
{
m_dLength=0;
int i;
for(i=0; i<iCityCount;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<iCityCount;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<iCityCount;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<iCityCount;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<iCityCount;i++) {

if((AllowedCity[i]==1))
mSelect+=prob[i] ;
if (mSelect>=mRate) {j=i;break;}
}
if (j==10000)
{
temp=-1;
for (i=0;i<iCityCount;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<iCityCount-1;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<iAntCount;i++) {
for (j=0;j<iCityCount-1;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<iCityCount;i++) {
for (j=0;j<iCityCount;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<iCityCount;i++)
for (j=0;j<iCityCount;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<iCityCount;i++)
{
in>>cc[i].num>>cc[i].x>>cc[i].y;
besttour[i]=0;
}
int j;
for(int i=0;i<iCityCount;i++)
for (j=0;j<iCityCount;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<iAntCount;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<iItCount)
{
for(j=0;j<iAntCount;j++)
{
for (i=0;i<iCityCount-1;i++)
ants[j].move();
}
for(j=0;j<iAntCount;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<iCityCount;t++)
temptour[t]=ants[0].tabu[t];
for(j=0;j<iAntCount;j++)
{
if (temp>ants[j].m_dLength) {
temp=ants[j].m_dLength;
for ( t=0;t<iCityCount;t++)
temptour[t]=ants[j].tabu[t];
}
}
if(temp<m_dLength){
m_dLength=temp;
for ( t=0;t<iCityCount;t++)
besttour[t]=temptour[t];
}
printf("%d : %f\n",max,m_dLength);
UpdateTrial();
for(j=0;j<iAntCount;j++)
ants[j].Clear();
max++;
}
printf("The shortest toure is : %f\n",m_dLength);
for ( int t=0;t<iCityCount;t++)
printf(" %d ",besttour[t]);
}
int main()
{
project TSP;
TSP.GetAnt();
TSP.StartSearch();
return 0;
}

Ⅷ 求“基于蚁群算法的城市配电网规划”的源代码!!!!!

阅读全文

与蚁群算法源代码相关的资料

热点内容
喷油螺杆制冷压缩机 浏览:578
python员工信息登记表 浏览:376
高中美术pdf 浏览:160
java实现排列 浏览:512
javavector的用法 浏览:981
osi实现加密的三层 浏览:231
大众宝来原厂中控如何安装app 浏览:913
linux内核根文件系统 浏览:242
3d的命令面板不见了 浏览:525
武汉理工大学服务器ip地址 浏览:148
亚马逊云服务器登录 浏览:524
安卓手机如何进行文件处理 浏览:70
mysql执行系统命令 浏览:929
php支持curlhttps 浏览:142
新预算法责任 浏览:443
服务器如何处理5万人同时在线 浏览:250
哈夫曼编码数据压缩 浏览:425
锁定服务器是什么意思 浏览:383
场景检测算法 浏览:616
解压手机软件触屏 浏览:348