導航:首頁 > 源碼編譯 > 蟻源碼

蟻源碼

發布時間:2023-02-07 15:05:53

A. 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;
}

B. 用螞蟻源碼建站有法律風險嗎

如果是開放的源碼,注意點就可以了

C. matlab 智能演算法30個案例分析書中第二十二章講得是用蟻群演算法求TSP 問題,源代碼中有load

把計算後工作空間的數據保存就是*.mat文件了。

D. 蟻群演算法求解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;
我現在也在研究它,希望能共同進步.建義可以看一下段海濱的關於蟻群演算法的書.講的不錯,李士勇的也可以,還有一本我在圖書館見過,記不得名字了.

E. 用螞蟻演算法來實現公交線網優化,誰有源代碼

我只告訴你什麼是螞蟻演算法: 蟻群演算法(ant colony optimization, ACO),又稱螞蟻演算法,是一種用來在圖中尋找優化路徑的機率型技術。它由Marco Dorigo於1992年在他的博士論文中引入,其靈感來源於螞蟻在尋找食物過程中發現路徑的行為。
為什麼小小的螞蟻能夠找到食物?他們具有智能么?設想,如果我們要為螞蟻設計一個人工智慧的程序,那麼這個程序要多麼復雜呢?首先,你要讓螞蟻能夠避開障礙物,就必須根據適當的地形給它編進指令讓他們能夠巧妙的避開障礙物,其次,要讓螞蟻找到食物,就需要讓他們遍歷空間上的所有點;再次,如果要讓螞蟻找到最短的路徑,那麼需要計算所有可能的路徑並且比較它們的大小,而且更重要的是,你要小心翼翼的編程,因為程序的錯誤也許會讓你前功盡棄。這是多麼不可思議的程序!太復雜了,恐怕沒人能夠完成這樣繁瑣冗餘的程序。
然而,事實並沒有你想得那麼復雜,上面這個程序每個螞蟻的核心程序編碼不過100多行!為什麼這么簡單的程序會讓螞蟻干這樣復雜的事情?答案是:簡單規則的涌現。事實上,每隻螞蟻並不是像我們想像的需要知道整個世界的信息,他們其實只關心很小范圍內的眼前信息,而且根據這些局部信息利用幾條簡單的規則進行決策,這樣,在蟻群這個集體里,復雜性的行為就會凸現出來。這就是人工生命、復雜性科學解釋的規律!那麼,這些簡單規則是什麼呢?下面詳細說明:
1、范圍:
螞蟻觀察到的范圍是一個方格世界,螞蟻有一個參數為速度半徑(一般是3),那麼它能觀察到的范圍就是3*3個方格世界,並且能移動的距離也在這個范圍之內。
2、環境:
螞蟻所在的環境是一個虛擬的世界,其中有障礙物,有別的螞蟻,還有信息素,信息素有兩種,一種是找到食物的螞蟻灑下的食物信息素,一種是找到窩的螞蟻灑下的窩的信息素。每個螞蟻都僅僅能感知它范圍內的環境信息。環境以一定的速率讓信息素消失。
3、覓食規則:
在每隻螞蟻能感知的范圍內尋找是否有食物,如果有就直接過去。否則看是否有信息素,並且比較在能感知的范圍內哪一點的信息素最多,這樣,它就朝信息素多的地方走,並且每隻螞蟻多會以小概率犯錯誤,從而並不是往信息素最多的點移動。螞蟻找窩的規則和上面一樣,只不過它對窩的信息素做出反應,而對食物信息素沒反應。
4、移動規則:
每隻螞蟻都朝向信息素最多的方向移,並且,當周圍沒有信息素指引的時候,螞蟻會按照自己原來運動的方向慣性的運動下去,並且,在運動的方向有一個隨機的小的擾動。為了防止螞蟻原地轉圈,它會記住最近剛走過了哪些點,如果發現要走的下一點已經在最近走過了,它就會盡量避開。
5、避障規則:
如果螞蟻要移動的方向有障礙物擋住,它會隨機的選擇另一個方向,並且有信息素指引的話,它會按照覓食的規則行為。
7、播撒信息素規則:
每隻螞蟻在剛找到食物或者窩的時候撒發的信息素最多,並隨著它走遠的距離,播撒的信息素越來越少。
根據這幾條規則,螞蟻之間並沒有直接的關系,但是每隻螞蟻都和環境發生交互,而通過信息素這個紐帶,實際上把各個螞蟻之間關聯起來了。比如,當一隻螞蟻找到了食物,它並沒有直接告訴其它螞蟻這兒有食物,而是向環境播撒信息素,當其它的螞蟻經過它附近的時候,就會感覺到信息素的存在,進而根據信息素的指引找到了食物。
說了這么多,螞蟻究竟是怎麼找到食物的呢?
在沒有螞蟻找到食物的時候,環境沒有有用的信息素,那麼螞蟻為什麼會相對有效的找到食物呢?這要歸功於螞蟻的移動規則,尤其是在沒有信息素時候的移動規則。首先,它要能盡量保持某種慣性,這樣使得螞蟻盡量向前方移動(開始,這個前方是隨機固定的一個方向),而不是原地無謂的打轉或者震動;其次,螞蟻要有一定的隨機性,雖然有了固定的方向,但它也不能像粒子一樣直線運動下去,而是有一個隨機的干擾。這樣就使得螞蟻運動起來具有了一定的目的性,盡量保持原來的方向,但又有新的試探,尤其當碰到障礙物的時候它會立即改變方向,這可以看成一種選擇的過程,也就是環境的障礙物讓螞蟻的某個方向正確,而其他方向則不對。這就解釋了為什麼單個螞蟻在復雜的諸如迷宮的地圖中仍然能找到隱蔽得很好的食物。
當然,在有一隻螞蟻找到了食物的時候,其他螞蟻會沿著信息素很快找到食物的。
螞蟻如何找到最短路徑的?這一是要歸功於信息素,另外要歸功於環境,具體說是計算機時鍾。信息素多的地方顯然經過這里的螞蟻會多,因而會有更多的螞蟻聚集過來。假設有兩條路從窩通向食物,開始的時候,走這兩條路的螞蟻數量同樣多(或者較長的路上螞蟻多,這也無關緊要)。當螞蟻沿著一條路到達終點以後會馬上返回來,這樣,短的路螞蟻來回一次的時間就短,這也意味著重復的頻率就快,因而在單位時間里走過的螞蟻數目就多,灑下的信息素自然也會多,自然會有更多的螞蟻被吸引過來,從而灑下更多的信息素……;而長的路正相反,因此,越來越多地螞蟻聚集到較短的路徑上來,最短的路徑就近似找到了。也許有人會問局部最短路徑和全局最短路的問題,實際上螞蟻逐漸接近全局最短路的,為什麼呢?這源於螞蟻會犯錯誤,也就是它會按照一定的概率不往信息素高的地方走而另闢蹊徑,這可以理解為一種創新,這種創新如果能縮短路途,那麼根據剛才敘述的原理,更多的螞蟻會被吸引過來。
引申:
跟著螞蟻的蹤跡,你找到了什麼?通過上面的原理敘述和實際操作,我們不難發現螞蟻之所以具有智能行為,完全歸功於它的簡單行為規則,而這些規則綜合起來具有下面兩個方面的特點:
1、多樣性
2、正反饋
多樣性保證了螞蟻在覓食的時候不置走進死胡同而無限循環,正反饋機制則保證了相對優良的信息能夠被保存下來。我們可以把多樣性看成是一種創造能力,而正反饋是一種學習強化能力。正反饋的力量也可以比喻成權威的意見,而多樣性是打破權威體現的創造性,正是這兩點小心翼翼的巧妙結合才使得智能行為涌現出來了。
引申來講,大自然的進化,社會的進步、人類的創新實際上都離不開這兩樣東西,多樣性保證了系統的創新能力,正反饋保證了優良特性能夠得到強化,兩者要恰到好處的結合。如果多樣性過剩,也就是系統過於活躍,這相當於螞蟻會過多的隨機運動,它就會陷入混沌狀態;而相反,多樣性不夠,正反饋機制過強,那麼系統就好比一潭死水。這在蟻群中來講就表現為,螞蟻的行為過於僵硬,當環境變化了,螞蟻群仍然不能適當的調整。
既然復雜性、智能行為是根據底層規則涌現的,既然底層規則具有多樣性和正反饋特點,那麼也許你會問這些規則是哪裡來的?多樣性和正反饋又是哪裡來的?我本人的意見:規則來源於大自然的進化。而大自然的進化根據剛才講的也體現為多樣性和正反饋的巧妙結合。而這樣的巧妙結合又是為什麼呢?為什麼在你眼前呈現的世界是如此栩栩如生呢?答案在於環境造就了這一切,之所以你看到栩栩如生的世界,是因為那些不能夠適應環境的多樣性與正反饋的結合都已經死掉了,被環境淘汰了!
參數說明:
最大信息素:螞蟻在一開始擁有的信息素總量,越大表示程序在較長一段時間能夠存在信息素。信息素消減的速度:隨著時間的流逝,已經存在於世界上的信息素會消減,這個數值越大,那麼消減的越快。
錯誤概率表示這個螞蟻不往信息素最大的區域走的概率,越大則表示這個螞蟻越有創新性。
速度半徑表示螞蟻一次能走的最大長度,也表示這個螞蟻的感知范圍。
記憶能力表示螞蟻能記住多少個剛剛走過點的坐標,這個值避免了螞蟻在本地打轉,停滯不前。而這個值越大那麼整個系統運行速度就慢,越小則螞蟻越容易原地轉圈。
-----例子-----
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>蟻群演算法js版</title>
<style>
.ant{
position:absolute;
background-color:#000000;
overflow:hidden;
width:2px;
height:2px;
}
.food{
position:absolute;
background-color:#0000ff;
overflow:hidden;
width:2px;
height:2px;
}
.nest{
position:absolute;
background-color:#ff0000;
overflow:hidden;
width:2px;
height:2px;
}
</style>
<script type="text/JavaScript">
//============================
//系統參數初始化
//----------------------------
//生命體數量與軌跡長度
Unit=10;Path=30;
//生命體速度上下限
v0=2;vM=10;
//生命體加速度變化范圍
Kr=0.1;Kv=0.1*(vM-v0);
//生命體運動范圍
x0=0;xM=document.documentElement.clientWidth;
y0=0;yM=document.documentElement.clientHeight;
//生命體出生地(巢穴)
xi0=x0+(xM-x0)*Math.random();
yi0=y0+(yM-y0)*Math.random();
str0='<div class="ant" style="left:'+xi0+';top:'+yi0+';"></div>';
//食物所在地
xf=x0+(xM-x0)*Math.random();
yf=y0+(yM-y0)*Math.random();
//氣味感知范圍
R_2=5*5;
//============================
var r=new Array();
var v=new Array();
var dr=new Array();
var dv=new Array();
var x=new Array();
var y=new Array();
var life=new Array();
//單擊暫停
var xi0,yi0,xf,yf;
var Time0,str0;
window.status='pause';
function document.onclick(){
if(window.status=='pause'){
window.status=0;
nest.style.left=xi0;
nest.style.top=yi0;
food.style.left=xf;
food.style.top=yf;
//測試初始化時間用
Time0=(new Date()).getTime();
init(0);
}else{
window.status='pause';
}
}
//窗口大小調整後刷新頁面以調整系統參數
function window.onresize(){
// window.location.href=document.location;
}
//初始化函數
function init(i){
if(window.status!='pause'&&i<Unit){
if(!life){
document.body.appendChild(life=document.createElement(str0));
x=xi0;
y=yi0;
r=Math.random();
v=1/Math.random();
dr=Kr*Math.random();
dv=Kv*Math.random();
}
Move(i);
window.status=i+1;
setTimeout('init('+(i+1)+')',i);
// }else{
// alert('生成耗時:'+((new Date()).getTime()-Time0)+'ms');
}
}
//運動函數
Total=Unit*Path;
P2=2*Math.PI;
function Move(i){
if(window.status!='pause'){
k=i%Unit;
X=x[k];
Y=y[k];
R=r[k];
V=v[k];
if(!life){
str='<div class="ant" style="left:'+X+';top:'+Y+';"></div>';
document.body.appendChild(life=document.createElement(str));
}
obj=life;
R+=dr[k]*(2*Math.random()-1);
V+=dv[k]*(2*Math.random()-1);
X+=Math.sin(P2*R)*V;
Y+=Math.cos(P2*R)*V;
//遇到食物原路返回並減小角度變化
distance=(X-xf)*(X-xf)+(Y-yf)*(Y-yf);
if(distance<R_2){
R+=0.5;
r/=2;
v*=2;
}
distance=(X-xi0)*(X-xi0)+(Y-yi0)*(Y-yi0);
if(distance<R_2){
R+=0.5;
r/=2;
v*=2;
}
/*----------------------------------
/*================================*/
//碰撞邊界反彈
R=(X<x0||X>xM)?-R:R;
R=(Y<y0||Y>yM)?0.5-R:R;
X=x[k]+Math.sin(P2*R)*V;
Y=y[k]+Math.cos(P2*R)*V;
/*================================*/
//溢出邊界重生(類似流星效果)
if(X<x0||X>xM||Y<y0||Y>yM){
X=xi0;
Y=yi0;
}
/*----------------------------------
/*================================*/
//邊界限制
x[k]=X=(X<x0)?x0:(X>xM)?xM-2:X;
y[k]=Y=(Y<y0)?y0:(Y>yM)?yM-2:Y;
r[k]=R>1?R-1:R<0?R+1:R;
v[k]=V=(V<v0)?v0:((V<vM)?V:vM);
/*================================*/
obj.style.left=x[k]=X;
obj.style.top=y[k]=Y;
setTimeout('Move('+(i+Unit)%Total+')',Unit);
}
}
//根據瀏覽器自動載入動畫
switch(navigator.appName.toLowerCase()){
case "netscape":
window.addEventListener("load",document.onclick,false);
break;
case "microsoft internet explorer":
default:
window.attachEvent("onload",document.onclick);
break;
}
</script>
</head>
<body scroll="no">
<div id="food" class="food"></div>
<div id="nest" class="nest"></div>
</body>
</html>

F. 求蟻群演算法matlab或者,c,或者vb源碼,用在多機器人協作,尋跡,最有路徑的,,程序!!謝謝,回答好的,

clear,close all;
loc = [0.3663, 0.9076; 0.7459, 0.8713; 0.4521, 0.8465;
0.7624, 0.7459; 0.7096, 0.7228; 0.0710, 0.7426;
0.4224, 0.7129; 0.5908, 0.6931; 0.3201, 0.6403;
0.5974, 0.6436; 0.3630, 0.5908; 0.6700, 0.5908;
0.6172, 0.5495; 0.6667, 0.5446; 0.1980, 0.4686;
0.3498, 0.4488; 0.2673, 0.4274; 0.9439, 0.4208;
0.8218, 0.3795; 0.3729, 0.2690; 0.6073, 0.2640;
0.4158, 0.2475; 0.5990, 0.2261; 0.3927, 0.1947;
0.5347, 0.1898; 0.3960, 0.1320; 0.6287, 0.0842;
0.5000, 0.0396; 0.9802, 0.0182; 0.6832, 0.8515];

% loc=rand(50,2);
NumCity=length(loc);
for i = 1:NumCity,
for j = 1:NumCity,
distance(i, j) = norm(loc(i, :) - loc(j, :));
end
end
distance=distance+eye(NumCity).*eps;
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^^^^^^^ Initialize AC Parameters ^^^^^^^^^^^^^
% P: ant α β ρ η Q
% V: 31 1 5 0.1 1./distance 100
ant=31; a=1; b=5; p=0.1; E=1./distance; Q=100;
%^^^^^^^^^^^^
t=ones(NumCity); Nm=200;
Tabu=zeros(ant,NumCity); R_best=zeros(Nm,NumCity);
L_ave=zeros(Nm,1); L_best=inf.*ones(Nm,1);
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tic
for Nc=1:Nm
Tabu=[];
%^^^^^^^^^^^^^^^^^^ Put Ants into Cities ^^^^^^^^^^^^^
Randpos=[];
for i=1:(ceil(ant/NumCity)),Randpos=[Randpos,randperm(NumCity)];end
Tabu(:,1)=(Randpos(1,1:ant))';
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^ Ants According To Probability Choose Next City ^^^^^^^^^^^^^
for j=2:NumCity,
for i=1:ant,
VisitedCity=Tabu(i,1:(j-1));
UnVisited=zeros(1,(NumCity-j+1));
P=UnVisited;
Jc=1;
for k=1:NumCity
if length(find(VisitedCity==k))==0
UnVisited(Jc)=k;
Jc=Jc+1;
end
end
%^^^^^^^^^^^^
for k=1:length(UnVisited)
P(k)=(t(VisitedCity(end),UnVisited(k)).^a)...
*(E(VisitedCity(end),UnVisited(k)).^b);
end
P=P./sum(P);
%^^^^^^^^^^^^
Pcum=cumsum(P);
Select=find(Pcum>=rand);
% Select=find(P==max(P));
ToVisit=UnVisited(Select(1));
Tabu(i,j)=ToVisit;
end
end
if Nc>=2,Tabu(1,:)=R_best(Nc-1,:);end
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^ Record the Best Path ^^^^^^^^^^^^^
L=zeros(ant,1);
dt=zeros(NumCity);
for i=1:ant
R=Tabu(i,:);
for j=1:(NumCity-1), L(i)=L(i)+distance(R(j),R(j+1));end
L(i)=L(i)+distance(R(1),R(NumCity));
for j=1:(NumCity-1)
dt(Tabu(i,j),Tabu(i,j+1))=dt(Tabu(i,j),Tabu(i,j+1))+Q./(L(i));
end
dt(Tabu(i,NumCity),Tabu(i,1))=dt(Tabu(i,NumCity),Tabu(i,1))+Q./L(i);
end
L_best(Nc)=min(L);
pos=find(L==L_best(Nc));
R_best(Nc,:)=Tabu(pos(1),:);
L_ave(Nc)=mean(L);
t=(1-p).*t+dt;
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^ Updata the t ^^^^^^^^^^^^^
% dt=zeros(NumCity);
% for i=1:ant
% for j=1:NumCity-1
% dt(Tabu(i,j),Tabu(i,j+1))=dt(Tabu(i,j),Tabu(i,j+1))+Q./(L(i));
% end
% dt(Tabu(i,NumCity),Tabu(i,1))=dt(Tabu(i,NumCity),Tabu(i,1))+Q./L(i);
% end
% t=(1-p).*t+dt;
% Tabu=zeros(ant,NumCity);
end
toc
Pos=find(L_best==min(L_best));
Shortest_Route=R_best(Pos(1),:);
Shortest_Length=L_best(Pos(1));
subplot(1,2,1)
DrawRoute(loc,Shortest_Route)
subplot(1,2,2)
plot(L_best)
hold on
plot(L_ave)

閱讀全文

與蟻源碼相關的資料

熱點內容
千聊免費課程可以重新加密嗎 瀏覽:507
python能代替php嗎 瀏覽:252
phpexcel樣式 瀏覽:265
安卓手機有沒有什麼軟體可以阻止彈廣告的 瀏覽:306
linux區域網搭建伺服器 瀏覽:690
python編譯器mac 瀏覽:293
windows的doc命令 瀏覽:463
nfc全加密門禁卡 瀏覽:636
身份信息被加密 瀏覽:482
我的鹽城app怎麼添加不了家庭成員 瀏覽:493
php商城並發 瀏覽:348
熊貓繪畫app怎麼做出大佬的筆刷 瀏覽:603
雲存儲伺服器知識 瀏覽:461
伺服器cpu是什麼指令集 瀏覽:591
糖貓t10怎麼安裝app 瀏覽:992
電腦加密u盤怎麼使用 瀏覽:518
linux如何升級php版本升級 瀏覽:841
二級程序員c語言難度 瀏覽:353
批處理編譯qt 瀏覽:67
鐵友app怎麼查詢機票訂單 瀏覽:197