導航:首頁 > 源碼編譯 > 人工蜂群演算法程序

人工蜂群演算法程序

發布時間:2022-10-07 00:01:18

1. 優化演算法筆記(七)差分進化演算法

(以下描述,均不是學術用語,僅供大家快樂的閱讀)
差分進化演算法(Differential Evolution Algorithm,DE)是一種基於群體的進化演算法,它模擬了群體中的個體的合作與競爭的過程。演算法原理簡單,控制參數少,只有交叉概率和縮放比例因子,魯棒性強,易於實現。
差分進化演算法中,每一個個體的基因表示待求問題的一個候選解。每次迭代將先進行變異操作,選擇一個或多個個體的基因作為基,然後選擇不同的個體的差分來構成差分基因,最後將作為基的基因與差分基因相加來得出新的個體。交叉操作將新的個體將於父代的對應個體交叉,然後進行選擇操作,比較交叉後的個體與父代的對應個體,選擇較優的個體保留至下一代。在迭代完成之後將選擇種群中最優個體的基因作為解。
差分進化演算法可以算是我所使用過的優化演算法中大魔王級別的演算法,雖然它每個方面都沒有強到離譜,但是綜合起來的效果好於大多數演算法。它就像一個每個科目都能考到90分(百分制)的學生,雖然沒門課都不是最優秀的,但是論綜合,論總分,它有極大的概率是第一名。

在我研究優化演算法的小路上,我的目標就是找到一個能打敗大魔王或是能在大多數方面壓制魔王的演算法。

這次的主角就選魔王軍吧(或者蟻王軍,為了與蟻群演算法區別還是叫魔王軍吧),個體則稱之為魔王兵。
魔王兵的能力取決於它們的基因,它們可以根據環境或者需要改變自己的基因使得自己更加強大,更方便的處理問題,問題的維度與基因維度相同。

表示第i個魔王兵在進化了第t次後的基因,該個體有D位基因。
與遺傳演算法同為進化演算法的差分進化演算法,它們的操作(運算元)也都非常相似的,都是交叉,變異和選擇,流程也幾乎一樣(遺傳演算法先交叉後變異,差分進化演算法先變異後交叉)。

說到差分進化演算法中的變異,我就想到一句論語 「三人行,必有我師焉。擇其善者而從之,其不善者而改之。」 ,其實這句論語已經向我們說明了差分進化演算法的整個流程:
「三人行,必有我師焉」——變異,交叉。
「擇其善者而從之,其不善者而改之」——選擇。
差分進化演算法中,當一個魔王兵變異時,它會先找來3個小夥伴,當然是隨機找來3個小夥伴,避免同化。在一個小夥伴的基因上加上另外兩個小夥伴基因之差作為自己的目標基因。其變異公式如下:

表示第i個魔王兵找到了編號為r1、r2和r3的三個魔王兵,當然了i、r1、r2、r3為互不相同的整數,F為縮放比例因子,通常 ,一般取F=0.5。 為第i個魔王兵交叉後的目標基因圖紙,不過這是個半成品,再經過交叉後,目標基因圖紙才算完成。
其實現在我們已經有了5個基因圖紙了 ,接下來將進行交叉操作。由於變異操作,差分進化演算法的種群中個體數至少為4,即魔王軍中至少有4個小兵。

交叉操作中,魔王兵i會將目標基因圖紙 進行加工得到 ,加工過程如下:

其中 。 為交叉概率,其值越大,發生交叉的概率越大,一般取 。 為{1,2,…,D}中的隨機整數,其作用是保證交叉操作中至少有一維基因來自變異操作產生的基因,不能讓交叉操作的努力白費。
從公式上可以看出交叉操作實際上是從變異操作得出的基因圖紙上選擇至少一位基因來替換自己的等位基因,得到最終的基因圖紙。

選擇操作相對簡單,魔王兵i拿到了最終的基因圖紙 ,大喊一聲,進化吧,魔王兵i的基因改變了。它拿出了能力測量器fitness function,如果發現自己變強了,那麼就將基因 保留到下一代,否則它選擇放棄進化,讓自己還原成 。

實驗又來啦,還是那個實驗 ,簡單、易算、好畫圖。
實驗1 :參數如下

圖中可以看出在第20代時,群體已經非常集中了,在來看看最終得出的結果。

這結果真是好到令人發指,惡魔在心中低語「把其他的優化演算法都丟掉吧」。不過別往心裡去,任何演算法都有優缺點,天下沒有免費的午餐,要想獲得某種能力必須付出至少相應的代價。
實驗2:
將交叉率CR設為0,即每次交叉只選擇保留一位變異基因。

看看了看圖,感覺跟實驗1中相比沒有什麼變化,那我們再來看看結果。

結果總體來說比實驗1好了一個數量級。為什麼呢?個人感覺應該是每次只改變一位基因的局部搜索能力比改變多位基因更強。下面我們將交叉率CR設為1來看看是否是這樣。
實驗3:
將交叉率CR設為1,即每次交叉只選擇保留一位原有基因。

實驗3的圖與實驗1和實驗2相比好像也沒什麼差別,只是收斂速度好像快了那麼一點點。再來看看結果。

發現結果比實驗2的結果還要好?那說明了實驗2我得出的結論是可能是錯誤的,交叉率在該問題上對差分進化演算法的影響不大,它們結果的差異可能只是運氣的差異,畢竟是概率演算法。
實驗4:
將變異放縮因子設為0,即變異只與一個個體有關。

收斂速度依然很快,不過怎麼感覺結果不對,而且個體收斂的路徑好像遺傳演算法,當F=0,時,差分進化演算法退化為了沒有變異、選擇操作的遺傳演算法,結果一定不會太好。

果然如此。下面我們再看看F=2時的實驗。
實驗5:
將變異放縮因子設為2。

實驗5的圖可以明顯看出,群體的收斂速度要慢了許多,到第50代時,種群還未完全收斂於一點,那麼在50代時其結果也不會很好,畢竟演算法還未收斂就停止進化了。

結果不算很好但也算相對穩定。

通過上面5個實驗,我們大致了解了差分進化演算法的兩個參數的作用。
交叉率CR,影響基因取自變異基因的比例,由於至少要保留一位自己的基因和變異的基因導致CR在該問題上對演算法性能的影響不大(這個問題比較簡單,維度較低,影響不大)。
變異放縮因子F,影響群體的收斂速度,F越大收斂速度越慢,F絕對值越小收斂速度越快,當F=0是群體之間只會交換基因,不會變異基因。

差分進化演算法大魔王已經如此強大了,那麼還有什麼可以改進的呢?當然有下面一一道來。
方案1 .將3人行修改為5人行,以及推廣到2n+1人行。
實驗6:
將3人行修改為5人行,變異公式如下:

五人行的實驗圖看起來好像與之前並沒有太大的變化,我們再來看看結果。

結果沒有明顯提升,反而感覺比之前的結果差了。反思一下五人行的優缺點,優點,取值范圍更大,缺點,情況太多,減慢搜索速度。

可以看出演算法的收斂速度比之前的變慢了一點,再看看結果。

比之前差。

差分進化演算法的學習在此也告一段落。差分進化演算法很強大,也很簡單、簡潔,演算法的描述都充滿了美感,不愧是大魔王。不過這里並不是結束,這只是個開始,終將找到打敗大魔王的方法,讓新的魔王誕生。
由於差分進化演算法足夠強,而文中實驗的問題較為簡單導致演算法的改進甚至越改越差(其實我也不知道改的如何,需要大量實驗驗證)。在遙遠的將來,也會有更加復雜的問題來檢驗魔王的能力,總之,後會無期。
以下指標純屬個人yy,僅供參考

目錄
上一篇 優化演算法筆記(六)遺傳演算法
下一篇 優化演算法筆記(八)人工蜂群演算法

優化演算法matlab實現(七)差分進化演算法matlab實現

2. 有沒有人有多目標人工蜂群演算法的MATLAB代碼。發我一份 不勝感激!!

http://emuch.net/bbs/attachment.php?tid=3808850&aid=11221&pay=yes
裡面有多個文件
其中之一
%/* ABC algorithm coded using MATLAB language */

%/* Artificial Bee Colony (ABC) is one of the most recently defined algorithms by Dervis Karaboga in 2005, motivated by the intelligent behavior of honey bees. */

%/* Referance Papers*/

%/*D. Karaboga, AN IDEA BASED ON HONEY BEE SWARM FOR NUMERICAL OPTIMIZATION,TECHNICAL REPORT-TR06, Erciyes University, Engineering Faculty, Computer Engineering Department 2005.*/

%/*D. Karaboga, B. Basturk, A powerful and Efficient Algorithm for Numerical Function Optimization: Artificial Bee Colony (ABC) Algorithm, Journal of Global Optimization, Volume:39, Issue:3,pp:459-171, November 2007,ISSN:0925-5001 , doi: 10.1007/s10898-007-9149-x */

%/*D. Karaboga, B. Basturk, On The Performance Of Artificial Bee Colony (ABC) Algorithm, Applied Soft Computing,Volume 8, Issue 1, January 2008, Pages 687-697. */

%/*D. Karaboga, B. Akay, A Comparative Study of Artificial Bee Colony Algorithm, Applied Mathematics and Computation, 214, 108-132, 2009. */

%/*Copyright ?2009 Erciyes University, Intelligent Systems Research Group, The Dept. of Computer Engineering*/

%/*Contact:
%Dervis Karaboga ([email protected] )
%Bahriye Basturk Akay ([email protected])
%*/

clear all
close all
clc

%/* Control Parameters of ABC algorithm*/
NP=20; %/* The number of colony size (employed bees+onlooker bees)*/
FoodNumber=NP/2; %/*The number of food sources equals the half of the colony size*/
limit=100; %/*A food source which could not be improved through "limit" trials is abandoned by its employed bee*/
maxCycle=2500; %/*The number of cycles for foraging {a stopping criteria}*/

%/* Problem specific variables*/
objfun='Sphere'; %cost function to be optimized
D=100; %/*The number of parameters of the problem to be optimized*/
ub=ones(1,D)*100; %/*lower bounds of the parameters. */
lb=ones(1,D)*(-100);%/*upper bound of the parameters.*/

runtime=1;%/*Algorithm can be run many times in order to see its robustness*/

%Foods [FoodNumber][D]; /*Foods is the population of food sources. Each row of Foods matrix is a vector holding D parameters to be optimized. The number of rows of Foods matrix equals to the FoodNumber*/
%ObjVal[FoodNumber]; /*f is a vector holding objective function values associated with food sources */
%Fitness[FoodNumber]; /*fitness is a vector holding fitness (quality) values associated with food sources*/
%trial[FoodNumber]; /*trial is a vector holding trial numbers through which solutions can not be improved*/
%prob[FoodNumber]; /*prob is a vector holding probabilities of food sources (solutions) to be chosen*/
%solution [D]; /*New solution (neighbour) proced by v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) j is a randomly chosen parameter and k is a randomlu chosen solution different from i*/
%ObjValSol; /*Objective function value of new solution*/
%FitnessSol; /*Fitness value of new solution*/
%neighbour, param2change; /*param2change corrresponds to j, neighbour corresponds to k in equation v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij})*/
%GlobalMin; /*Optimum solution obtained by ABC algorithm*/
%GlobalParams[D]; /*Parameters of the optimum solution*/
%GlobalMins[runtime]; /*GlobalMins holds the GlobalMin of each run in multiple runs*/

GlobalMins=zeros(1,runtime);

for r=1:runtime

% /*All food sources are initialized */
%/*Variables are initialized in the range [lb,ub]. If each parameter has different range, use arrays lb[j], ub[j] instead of lb and ub */

Range = repmat((ub-lb),[FoodNumber 1]);
Lower = repmat(lb, [FoodNumber 1]);
Foods = rand(FoodNumber,D) .* Range + Lower;

ObjVal=feval(objfun,Foods);
Fitness=calculateFitness(ObjVal);

%reset trial counters
trial=zeros(1,FoodNumber);

%/*The best food source is memorized*/
BestInd=find(ObjVal==min(ObjVal));
BestInd=BestInd(end);
GlobalMin=ObjVal(BestInd);
GlobalParams=Foods(BestInd,:);

iter=1;
while ((iter <= maxCycle)),

%%%%%%%%% EMPLOYED BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%
for i=1:(FoodNumber)

%/*The parameter to be changed is determined randomly*/
Param2Change=fix(rand*D)+1;

%/*A randomly chosen solution is used in procing a mutant solution of the solution i*/
neighbour=fix(rand*(FoodNumber))+1;

%/*Randomly selected solution must be different from the solution i*/
while(neighbour==i)
neighbour=fix(rand*(FoodNumber))+1;
end;

sol=Foods(i,:);
% /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
sol(Param2Change)=Foods(i,Param2Change)+(Foods(i,Param2Change)-Foods(neighbour,Param2Change))*(rand-0.5)*2;

% /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
ind=find(sol<lb);
sol(ind)=lb(ind);
ind=find(sol>ub);
sol(ind)=ub(ind);

%evaluate new solution
ObjValSol=feval(objfun,sol);
FitnessSol=calculateFitness(ObjValSol);

% /*a greedy selection is applied between the current solution i and its mutant*/
if (FitnessSol>Fitness(i)) %/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/
Foods(i,:)=sol;
Fitness(i)=FitnessSol;
ObjVal(i)=ObjValSol;
trial(i)=0;
else
trial(i)=trial(i)+1; %/*if the solution i can not be improved, increase its trial counter*/
end;

end;

%%%%%%%%%%%%%%%%%%%%%%%% CalculateProbabilities %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%/* A food source is chosen with the probability which is proportioal to its quality*/
%/*Different schemes can be used to calculate the probability values*/
%/*For example prob(i)=fitness(i)/sum(fitness)*/
%/*or in a way used in the metot below prob(i)=a*fitness(i)/max(fitness)+b*/
%/*probability values are calculated by using fitness values and normalized by dividing maximum fitness value*/

prob=(0.9.*Fitness./max(Fitness))+0.1;

%%%%%%%%%%%%%%%%%%%%%%%% ONLOOKER BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

i=1;
t=0;
while(t<FoodNumber)
if(rand<prob(i))
t=t+1;
%/*The parameter to be changed is determined randomly*/
Param2Change=fix(rand*D)+1;

%/*A randomly chosen solution is used in procing a mutant solution of the solution i*/
neighbour=fix(rand*(FoodNumber))+1;

%/*Randomly selected solution must be different from the solution i*/
while(neighbour==i)
neighbour=fix(rand*(FoodNumber))+1;
end;

sol=Foods(i,:);
% /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
sol(Param2Change)=Foods(i,Param2Change)+(Foods(i,Param2Change)-Foods(neighbour,Param2Change))*(rand-0.5)*2;

% /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
ind=find(sol<lb);
sol(ind)=lb(ind);
ind=find(sol>ub);
sol(ind)=ub(ind);

%evaluate new solution
ObjValSol=feval(objfun,sol);
FitnessSol=calculateFitness(ObjValSol);

% /*a greedy selection is applied between the current solution i and its mutant*/
if (FitnessSol>Fitness(i)) %/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/
Foods(i,:)=sol;
Fitness(i)=FitnessSol;
ObjVal(i)=ObjValSol;
trial(i)=0;
else
trial(i)=trial(i)+1; %/*if the solution i can not be improved, increase its trial counter*/
end;
end;

i=i+1;
if (i==(FoodNumber)+1)
i=1;
end;
end;

%/*The best food source is memorized*/
ind=find(ObjVal==min(ObjVal));
ind=ind(end);
if (ObjVal(ind)<GlobalMin)
GlobalMin=ObjVal(ind);
GlobalParams=Foods(ind,:);
end;

%%%%%%%%%%%% SCOUT BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%/*determine the food sources whose trial counter exceeds the "limit" value.
%In Basic ABC, only one scout is allowed to occur in each cycle*/

ind=find(trial==max(trial));
ind=ind(end);
if (trial(ind)>limit)
Bas(ind)=0;
sol=(ub-lb).*rand(1,D)+lb;
ObjValSol=feval(objfun,sol);
FitnessSol=calculateFitness(ObjValSol);
Foods(ind,:)=sol;
Fitness(ind)=FitnessSol;
ObjVal(ind)=ObjValSol;
end;

fprintf('Ýter=%d ObjVal=%g\n',iter,GlobalMin);
iter=iter+1;

end % End of ABC

GlobalMins(r)=GlobalMin;
end; %end of runs

save all

3. 人工蜂群演算法的蜜蜂采蜜機理

蜜蜂是一種群居昆蟲,雖然單個昆蟲的行為極其簡單,但是由單個簡單的個體所組成的群體卻表現出極其復雜的行為。真實的蜜蜂種群能夠在任何環境下,以極高的效率從食物源(花朵)中採集花蜜;同時,它們能適應環境的改變。
蜂群產生群體智慧的最小搜索模型包含基本的三個組成要素:食物源、被僱傭的蜜蜂(employed foragers)和未被僱傭的蜜蜂(unemployed foragers);兩種最為基本的行為模型:為食物源招募(recruit)蜜蜂和放棄(abandon)某個食物源。
(1)食物源:食物源的價值由多方面的因素決定,如:它離蜂巢的遠近,包含花蜜的豐富程度和獲得花蜜的難易程度。使用單一的參數,食物源的「收益率」(profitability),來代表以上各個因素。
(2)被僱用的蜜蜂:也稱引領蜂(Leader),其與所採集的食物源一一對應。引領蜂儲存有某一個食物源的相關信息(相對於蜂巢的距離、方向、食物源的豐富程度等)並且將這些信息以一定的概率與其他蜜蜂分享。
(3)未被僱用的蜜蜂:其主要任務是尋找和開採食物源。有兩種未被僱用的蜜蜂:偵查蜂(Scouter)和跟隨蜂(Follower)。偵察蜂搜索蜂巢附近的新食物源;跟隨蜂等在蜂巢裡面並通過與引領蜂分享相關信息找到食物源。一般情況下,偵察蜂的平均數目是蜂群的5%-20%。
在群體智慧的形成過程中,蜜蜂間交換信息是最為重要的一環。舞蹈區是蜂巢中最為重要的信息交換地。蜜蜂的舞蹈叫做搖擺舞。食物源的信息在舞蹈區通過搖擺舞的形式與其他蜜蜂共享,引領蜂通過搖擺舞的持續時間等來表現食物源的收益率,故跟隨蜂可以觀察到大量的舞蹈並依據收益率來選擇到哪個食物源采蜜。收益率與食物源被選擇的可能性成正比。因而,蜜蜂被招募到某一個食物源的概率與食物源的收益率成正比。
初始時刻,蜜蜂以偵察蜂的身份搜索。其搜索可以由系統提供的先驗知識決定,也可以完全隨機。經過一輪偵查後,若蜜蜂找到食物源,蜜蜂利用它本身的存儲能力記錄位置信息並開始采蜜。此時,蜜蜂將成為「被僱用者」。蜜蜂在食物源采蜜後回到蜂巢卸下蜂蜜然後將有如下選擇:
(1)放棄食物源而成為非僱傭蜂。
(2)跳搖擺舞為所對應的食物源招募更多的蜜蜂,然後回到食物源采蜜。
(3)繼續在同一個食物源采蜜而不進行招募。
對於非僱傭蜂有如下選擇:
(1)轉變成為偵察蜂並搜索蜂巢附近的食物源。其搜索可以由先驗知識決定,也可以完全隨機。
(2)在觀察完搖擺舞後被僱用成為跟隨蜂,開始搜索對應食物源鄰域並采蜜。

4. java人工蜂群演算法求解TSP問題

一、人工蜂群演算法的介紹

人工蜂群演算法(Artificial Bee Colony, ABC)是由Karaboga於2005年提出的一種新穎的基於群智能的全局優化演算法,其直觀背景來源於蜂群的采蜜行為,蜜蜂根據各自的分工進行不同的活動,並實現蜂群信息的共享和交流,從而找到問題的最優解。人工蜂群演算法屬於群智能演算法的一種。

二、人工蜂群演算法的原理

1、原理

標準的ABC演算法通過模擬實際蜜蜂的采蜜機制將人工蜂群分為3類: 采蜜蜂、觀察蜂和偵察蜂。整個蜂群的目標是尋找花蜜量最大的蜜源。在標準的ABC演算法中,采蜜蜂利用先前的蜜源信息尋找新的蜜源並與觀察蜂分享蜜源信息;觀察蜂在蜂房中等待並依據采蜜蜂分享的信息尋找新的蜜源;偵查蜂的任務是尋找一個新的有價值的蜜源,它們在蜂房附近隨機地尋找蜜源。

假設問題的解空間是

代碼:

[cpp]view plain

  • #include<iostream>

  • #include<time.h>

  • #include<stdlib.h>

  • #include<cmath>

  • #include<fstream>

  • #include<iomanip>

  • usingnamespacestd;

  • constintNP=40;//種群的規模,采蜜蜂+觀察蜂

  • constintFoodNumber=NP/2;//食物的數量,為采蜜蜂的數量

  • constintlimit=20;//限度,超過這個限度沒有更新采蜜蜂變成偵查蜂

  • constintmaxCycle=10000;//停止條件

  • /*****函數的特定參數*****/

  • constintD=2;//函數的參數個數

  • constdoublelb=-100;//函數的下界

  • constdoubleub=100;//函數的上界

  • doubleresult[maxCycle]={0};

  • /*****種群的定義****/

  • structBeeGroup

  • {

  • doublecode[D];//函數的維數

  • doubletrueFit;//記錄真實的最小值

  • doublefitness;

  • doublerfitness;//相對適應值比例

  • inttrail;//表示實驗的次數,用於與limit作比較

  • }Bee[FoodNumber];

  • BeeGroupNectarSource[FoodNumber];//蜜源,注意:一切的修改都是針對蜜源而言的

  • BeeGroupEmployedBee[FoodNumber];//采蜜蜂

  • BeeGroupOnLooker[FoodNumber];//觀察蜂

  • BeeGroupBestSource;//記錄最好蜜源

  • /*****函數的聲明*****/

  • doublerandom(double,double);//產生區間上的隨機數

  • voidinitilize();//初始化參數

  • doublecalculationTruefit(BeeGroup);//計算真實的函數值

  • doublecalculationFitness(double);//計算適應值

  • voidCalculateProbabilities();//計算輪盤賭的概率

  • voidevalueSource();//評價蜜源

  • voidsendEmployedBees();

  • voidsendOnlookerBees();

  • voidsendScoutBees();

  • voidMemorizeBestSource();

  • /*******主函數*******/

  • intmain()

  • {

  • ofstreamoutput;

  • output.open("dataABC.txt");

  • srand((unsigned)time(NULL));

  • initilize();//初始化

  • MemorizeBestSource();//保存最好的蜜源

  • //主要的循環

  • intgen=0;

  • while(gen<maxCycle)

  • {

  • sendEmployedBees();

  • CalculateProbabilities();

  • sendOnlookerBees();

  • MemorizeBestSource();

  • sendScoutBees();

  • MemorizeBestSource();

  • output<<setprecision(30)<<BestSource.trueFit<<endl;

  • gen++;

  • }

  • output.close();

  • cout<<"運行結束!!"<<endl;

  • return0;

  • }

  • /*****函數的實現****/

  • doublerandom(doublestart,doubleend)//隨機產生區間內的隨機數

  • {

  • returnstart+(end-start)*rand()/(RAND_MAX+1.0);

  • }

  • voidinitilize()//初始化參數

  • {

  • inti,j;

  • for(i=0;i<FoodNumber;i++)

  • {

  • for(j=0;j<D;j++)

  • {

  • NectarSource[i].code[j]=random(lb,ub);

  • EmployedBee[i].code[j]=NectarSource[i].code[j];

  • OnLooker[i].code[j]=NectarSource[i].code[j];

  • BestSource.code[j]=NectarSource[0].code[j];

  • }

  • /****蜜源的初始化*****/

  • NectarSource[i].trueFit=calculationTruefit(NectarSource[i]);

  • NectarSource[i].fitness=calculationFitness(NectarSource[i].trueFit);

  • NectarSource[i].rfitness=0;

  • NectarSource[i].trail=0;

  • /****采蜜蜂的初始化*****/

  • EmployedBee[i].trueFit=NectarSource[i].trueFit;

  • EmployedBee[i].fitness=NectarSource[i].fitness;

  • EmployedBee[i].rfitness=NectarSource[i].rfitness;

  • EmployedBee[i].trail=NectarSource[i].trail;

  • /****觀察蜂的初始化****/

  • OnLooker[i].trueFit=NectarSource[i].trueFit;

  • OnLooker[i].fitness=NectarSource[i].fitness;

  • OnLooker[i].rfitness=NectarSource[i].rfitness;

  • OnLooker[i].trail=NectarSource[i].trail;

  • }

  • /*****最優蜜源的初始化*****/

  • BestSource.trueFit=NectarSource[0].trueFit;

  • BestSource.fitness=NectarSource[0].fitness;

  • BestSource.rfitness=NectarSource[0].rfitness;

  • BestSource.trail=NectarSource[0].trail;

  • }

  • doublecalculationTruefit(BeeGroupbee)//計算真實的函數值

  • {

  • doubletruefit=0;

  • /******測試函數1******/

  • truefit=0.5+(sin(sqrt(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))*sin(sqrt(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))-0.5)

  • /((1+0.001*(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))*(1+0.001*(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1])));

  • returntruefit;

  • }

  • doublecalculationFitness(doubletruefit)//計算適應值

  • {

  • doublefitnessResult=0;

  • if(truefit>=0)

  • {

  • fitnessResult=1/(truefit+1);

  • }else

  • {

  • fitnessResult=1+abs(truefit);

  • }

  • returnfitnessResult;

  • }

  • voidsendEmployedBees()//修改采蜜蜂的函數

  • {

  • inti,j,k;

  • intparam2change;//需要改變的維數

  • doubleRij;//[-1,1]之間的隨機數

  • for(i=0;i<FoodNumber;i++)

  • {

  • param2change=(int)random(0,D);//隨機選取需要改變的維數

  • /******選取不等於i的k********/

  • while(1)

  • {

  • k=(int)random(0,FoodNumber);

  • if(k!=i)

  • {

  • break;

  • }

  • }

  • for(j=0;j<D;j++)

  • {

  • EmployedBee[i].code[j]=NectarSource[i].code[j];

  • }

  • /*******采蜜蜂去更新信息*******/

  • Rij=random(-1,1);

  • EmployedBee[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);

  • /*******判斷是否越界********/

  • if(EmployedBee[i].code[param2change]>ub)

  • {

  • EmployedBee[i].code[param2change]=ub;

  • }

  • if(EmployedBee[i].code[param2change]<lb)

  • {

  • EmployedBee[i].code[param2change]=lb;

  • }

  • EmployedBee[i].trueFit=calculationTruefit(EmployedBee[i]);

  • EmployedBee[i].fitness=calculationFitness(EmployedBee[i].trueFit);

  • /******貪婪選擇策略*******/

  • if(EmployedBee[i].trueFit<NectarSource[i].trueFit)

  • {

  • for(j=0;j<D;j++)

  • {

  • NectarSource[i].code[j]=EmployedBee[i].code[j];

  • }

  • NectarSource[i].trail=0;

  • NectarSource[i].trueFit=EmployedBee[i].trueFit;

  • NectarSource[i].fitness=EmployedBee[i].fitness;

  • }else

  • {

  • NectarSource[i].trail++;

  • }

  • }

  • }

  • voidCalculateProbabilities()//計算輪盤賭的選擇概率

  • {

  • inti;

  • doublemaxfit;

  • maxfit=NectarSource[0].fitness;

  • for(i=1;i<FoodNumber;i++)

  • {

  • if(NectarSource[i].fitness>maxfit)

  • maxfit=NectarSource[i].fitness;

  • }

  • for(i=0;i<FoodNumber;i++)

  • {

  • NectarSource[i].rfitness=(0.9*(NectarSource[i].fitness/maxfit))+0.1;

  • }

  • }

  • voidsendOnlookerBees()//采蜜蜂與觀察蜂交流信息,觀察蜂更改信息

  • {

  • inti,j,t,k;

  • doubleR_choosed;//被選中的概率

  • intparam2change;//需要被改變的維數

  • doubleRij;//[-1,1]之間的隨機數

  • i=0;

  • t=0;

  • while(t<FoodNumber)

  • {

  • R_choosed=random(0,1);

  • if(R_choosed<NectarSource[i].rfitness)//根據被選擇的概率選擇

  • {

  • t++;

  • param2change=(int)random(0,D);

  • /******選取不等於i的k********/

  • while(1)

  • {

  • k=(int)random(0,FoodNumber);

  • if(k!=i)

  • {

  • break;

  • }

  • }

  • for(j=0;j<D;j++)

  • {

  • OnLooker[i].code[j]=NectarSource[i].code[j];

  • }

  • /****更新******/

  • Rij=random(-1,1);

  • OnLooker[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);

  • /*******判斷是否越界*******/

  • if(OnLooker[i].code[param2change]<lb)

  • {

  • OnLooker[i].code[param2change]=lb;

  • }

  • if(OnLooker[i].code[param2change]>ub)

  • {

  • OnLooker[i].code[param2change]=ub;

  • }

  • OnLooker[i].trueFit=calculationTruefit(OnLooker[i]);

  • OnLooker[i].fitness=calculationFitness(OnLooker[i].trueFit);

  • /****貪婪選擇策略******/

  • if(OnLooker[i].trueFit<NectarSource[i].trueFit)

  • {

  • for(j=0;j<D;j++)

  • {

  • NectarSource[i].code[j]=OnLooker[i].code[j];

  • }

  • NectarSource[i].trail=0;

  • NectarSource[i].trueFit=OnLooker[i].trueFit;

  • NectarSource[i].fitness=OnLooker[i].fitness;

  • }else

  • {

  • NectarSource[i].trail++;

  • }

  • }

  • i++;

  • if(i==FoodNumber)

  • {

  • i=0;

  • }

  • }

  • }

  • 5. 請求c# c語言達人幫忙翻譯程序 可以出錢 需要將人工蜂群演算法的c程序翻譯成c#帶有窗體顯示的 詳情看補充

    你直接去收人工蜂群演算法撒

    6. 優化演算法筆記(二)優化演算法的分類

    (以下描述,均不是學術用語,僅供大家快樂的閱讀)

    在分類之前,我們先列舉一下常見的優化演算法(不然我們拿什麼分類呢?)。
    1遺傳演算法Genetic algorithm
    2粒子群優化演算法Particle Swarm Optimization
    3差分進化演算法Differential Evolution
    4人工蜂群演算法Artificial Bee Colony
    5蟻群演算法Ant Colony Optimization
    6人工魚群演算法Artificial Fish Swarm Algorithm
    7杜鵑搜索演算法Cuckoo Search
    8螢火蟲演算法Firefly Algorithm
    9灰狼演算法Grey Wolf Optimizer
    10鯨魚演算法Whale Optimization Algorithm
    11群搜索演算法Group search optimizer
    12混合蛙跳演算法Shuffled Frog Leaping Algorithm
    13煙花演算法fireworks algorithm
    14菌群優化演算法Bacterial Foraging Optimization
    以上優化演算法是我所接觸過的演算法,沒接觸過的演算法不能隨便下結論,知之為知之,不知為不知。其實到目前為止優化演算法可能已經有幾百種了,我們不可能也不需要全面的了解所有的演算法,而且優化演算法之間也有較大的共性,深入研究幾個之後再看其他優化演算法上手速度會灰常的快。
    優化演算法從提出到現在不過50-60年(遺傳演算法1975年提出),雖種類繁多但大多較為相似,不過這也很正常,比較香蕉和人的基因相似度也有50%-60%。當然演算法之間的相似度要比香蕉和人的相似度更大,畢竟人家都是優化演算法,有著相同的目標,只是實現方式不同。就像條條大路通羅馬,我們可以走去,可以坐汽車去,可以坐火車去,也可以坐飛機去,不管使用何種方式,我們都在去往羅馬的路上,也不會說坐飛機去要比走去更好,交通工具只是一個工具,最終的方案還是要看我們的選擇。

    上面列舉了一些常見的演算法,即使你一個都沒見過也沒關系,後面會對它們進行詳細的介紹,但是對後面的分類可能會有些許影響,不過問題不大,就先當總結看了。
    再對優化演算法分類之前,先介紹一下演算法的模型,在筆記(一)中繪制了優化演算法的流程,不過那是個較為簡單的模型,此處的模型會更加復雜。上面說了優化演算法有較大的相似性,這些相似性主要體現在演算法的運行流程中。
    優化演算法的求解過程可以看做是一個群體的生存過程。

    有一群原始人,他們要在野外中尋找食物,一個原始人是這個群體中的最小單元,他們的最終目標是尋找這個環境中最容易獲取食物的位置,即最易存活下來的位置。每個原始人都去獨自尋找食物,他們每個人每天獲取食物的策略只有採集果實、製作陷阱或者守株待兔,即在一天之中他們不會改變他們的位置。在下一天他們會根據自己的策略變更自己的位置。到了某一天他們又聚在了一起,選擇了他們到過的最容易獲取食物的位置定居。
    一群原始人=優化演算法中的種群、群體;
    一個原始人=優化演算法中的個體;
    一個原始人的位置=優化演算法中個體的位置、基因等屬性;
    原始人變更位置=優化演算法中總群的更新操作;
    該位置獲取食物的難易程度=優化演算法中的適應度函數;
    一天=優化演算法中的一個迭代;
    這群原始人最終的定居位置=優化演算法所得的解。
    優化演算法的流程圖如下:

    對優化演算法分類得有個標准,按照不同的標准分類也會得到不一樣的結果。首先說一下我所使用的分類標准(動態更新,有了新的感悟再加):

    按由來分類比較好理解,就是該演算法受何種現象啟發而發明,本質是對現象分類。

    可以看出演算法根據由來可以大致分為有人類的理論創造而來,向生物學習而來,受物理現象啟發。其中向生物學習而來的演算法最多,其他類別由於舉例有偏差,不是很准確,而且物理現象也經過人類總結,有些與人類現象相交叉,但仍將其獨立出來。
    類別分好了,那麼為什麼要這么分類呢?

    當然是因為要湊字數啦,啊呸,當然是為了更好的理解學習這些演算法的原理及特點。
    向動物生存學習而來的演算法一定是一種行之有效的方法,能夠保證演算法的效率和准確性,因為,如果使用該策略的動物無法存活到我們可以對其進行研究,我們也無法得知其生存策略。(而這也是一種倖存者偏差,我們只能看到行之有效的策略,但並不是我們沒看到的策略都是垃圾,畢竟也發生過小行星撞地球這種小概率毀滅性事件。講個冷笑話開cou心一shu下:一隻小恐龍對他的小夥伴說,好開心,我最喜歡的那顆星星越來越亮了(完)。)但是由於生物的局限性,人們所創造出的演算法也會有局限性:我們所熟知的生物都生存在三維空間,在這些環境中,影響生物生存的條件比較有限,反應到演算法中就是這些演算法在解決較低維度的問題時效果很好,當遇到超高維(維度>500)問題時,結果可能不容樂觀,沒做過實驗,我也不敢亂說。

    按更新過程分類相對復雜一點,主要是根據優化演算法流程中更新位置操作的方式來進行分類。更新位置的操作按我的理解可大致分為兩類:1.跟隨最優解;2.不跟隨最優解。
    還是上面原始人的例子,每天他有一次去往其他位置狩獵的機會,他們採用何種方式來決定今天自己應該去哪裡呢?
    如果他們的策略是「跟隨最優解」,那麼他們選取位置的方式就是按一定的策略向群體已知的最佳狩獵位置(歷史最佳)或者是當前群體中的最佳狩獵位置(今天最佳)靠近,至於是直線跑過去還是蛇皮走位繞過去,這個要看他們群體的策略。當然,他們的目的不是在最佳狩獵位置集合,他們的目的是在過去的途中看是否能發現更加好的狩獵位置,去往已經到過的狩獵地點再次狩獵是沒有意義的,因為每個位置獲取食物的難易程度是固定的。有了目標,大家都會朝著目標前進,總有一日,大家會在謀個位置附近相聚,相聚雖好但不利於後續的覓食容易陷入局部最優。
    什麼是局部最優呢?假設在當前環境中有一「桃花源」,擁有上帝視角的我們知道這個地方就是最適合原始人們生存的,但是此地入口隱蔽「山有小口,彷彿若有光」、「初極狹,才通人。」,是一個難以發現的地方。如果沒有任何一個原始人到達了這里,大家向著已知的最優位置靠近時,也難以發現這個「桃源之地」,而當大家越聚越攏之後,「桃源」被發現的可能性越來越低。雖然原始人們得到了他們的解,但這並不是我們所求的「桃源」,他們聚集之後失去了尋求「桃源」的可能,這群原始人便陷入了局部最優。

    如果他們的策略是「不跟隨最優解」,那麼他們的策略是什麼呢?我也不知道,這個應該他們自己決定。畢竟「是什麼」比「不是什麼」的范圍要小的多。總之不跟隨最優解時,演算法會有自己特定的步驟來更新個體的位置,有可能是隨機在自己附近找,也有可能是隨機向別人學習。不跟隨最優解時,原始人們應該不會快速聚集到某一處,這樣一來他們的選擇更具多樣性。
    按照更新過程對上面的演算法分類結果如下

    可以看出上面不跟隨最優解的演算法只有遺傳演算法和差分進化演算法,他們的更新策略是與進化和基因的重組有關。因此這些不跟隨最優解的演算法,他們大多依據進化理論更新位置(基因)我把他們叫做進化演算法,而那些跟隨群體最優解的演算法,他們則大多依賴群體的配合協作,我把這些演算法叫做群智能演算法。

    目前我只總結了這兩種,分類方法,如果你有更加優秀的分類方法,我們可以交流一下:

    目錄
    上一篇 優化演算法筆記(一)優化演算法的介紹
    下一篇 優化演算法筆記(三)粒子群演算法(1)

    7. 人工蜂群演算法limit怎麼確定

    可以通過保持其他參數不變,選擇不同的limit值進行實驗,看哪個效果好來確定。不知道你的問題是不是這個意思。

    8. 人工蜂群演算法里太多比喻了,能不能就演算法本身的步驟來講講

    直接給你JAVA代碼吧,看的簡單易懂
    import java.lang.Math;

    public class beeColony {

    /* Control Parameters of ABC algorithm*/
    int NP=20; /* The number of colony size (employed bees+onlooker bees)*/
    int FoodNumber = NP/2; /*The number of food sources equals the half of the colony size*/
    int limit = 100; /*A food source which could not be improved through "limit" trials is abandoned by its employed bee*/
    int maxCycle = 2500; /*The number of cycles for foraging {a stopping criteria}*/

    /* Problem specific variables*/
    int D = 100; /*The number of parameters of the problem to be optimized*/
    double lb = -5.12; /*lower bound of the parameters. */
    double ub = 5.12; /*upper bound of the parameters. lb and ub can be defined as arrays for the problems of which parameters have different bounds*/

    int runtime = 30; /*Algorithm can be run many times in order to see its robustness*/

    int dizi1[]=new int[10];
    double Foods[][]=new double[FoodNumber][D]; /*Foods is the population of food sources. Each row of Foods matrix is a vector holding D parameters to be optimized. The number of rows of Foods matrix equals to the FoodNumber*/
    double f[]=new double[FoodNumber]; /*f is a vector holding objective function values associated with food sources */
    double fitness[]=new double[FoodNumber]; /*fitness is a vector holding fitness (quality) values associated with food sources*/
    double trial[]=new double[FoodNumber]; /*trial is a vector holding trial numbers through which solutions can not be improved*/
    double prob[]=new double[FoodNumber]; /*prob is a vector holding probabilities of food sources (solutions) to be chosen*/
    double solution[]=new double[D]; /*New solution (neighbour) proced by v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) j is a randomly chosen parameter and k is a randomlu chosen solution different from i*/

    double ObjValSol; /*Objective function value of new solution*/
    double FitnessSol; /*Fitness value of new solution*/
    int neighbour, param2change; /*param2change corrresponds to j, neighbour corresponds to k in equation v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij})*/

    double GlobalMin; /*Optimum solution obtained by ABC algorithm*/
    double GlobalParams[]=new double[D]; /*Parameters of the optimum solution*/
    double GlobalMins[]=new double[runtime];
    /*GlobalMins holds the GlobalMin of each run in multiple runs*/
    double r; /*a random number in the range [0,1)*/

    /*a function pointer returning double and taking a D-dimensional array as argument */
    /*If your function takes additional arguments then change function pointer definition and lines calling "...=function(solution);" in the code*/

    // typedef double (*FunctionCallback)(double sol[D]);

    /*benchmark functions */

    // double sphere(double sol[D]);
    // double Rosenbrock(double sol[D]);
    // double Griewank(double sol[D]);
    // double Rastrigin(double sol[D]);

    /*Write your own objective function name instead of sphere*/
    // FunctionCallback function = &sphere;

    /*Fitness function*/
    double CalculateFitness(double fun)
    {
    double result=0;
    if(fun>=0)
    {
    result=1/(fun+1);
    }
    else
    {

    result=1+Math.abs(fun);
    }
    return result;
    }

    /*The best food source is memorized*/
    void MemorizeBestSource()
    {
    int i,j;

    for(i=0;i<FoodNumber;i++)
    {
    if (f[i]<GlobalMin)
    {
    GlobalMin=f[i];
    for(j=0;j<D;j++)
    GlobalParams[j]=Foods[i][j];
    }
    }
    }

    /*Variables are initialized in the range [lb,ub]. If each parameter has different range, use arrays lb[j], ub[j] instead of lb and ub */
    /* Counters of food sources are also initialized in this function*/

    void init(int index)
    {
    int j;
    for (j=0;j<D;j++)
    {
    r = ( (double)Math.random()*32767 / ((double)32767+(double)(1)) );
    Foods[index][j]=r*(ub-lb)+lb;
    solution[j]=Foods[index][j];
    }
    f[index]=calculateFunction(solution);
    fitness[index]=CalculateFitness(f[index]);
    trial[index]=0;
    }

    /*All food sources are initialized */
    void initial()
    {
    int i;
    for(i=0;i<FoodNumber;i++)
    {
    init(i);
    }
    GlobalMin=f[0];
    for(i=0;i<D;i++)
    GlobalParams[i]=Foods[0][i];

    }

    void SendEmployedBees()
    {
    int i,j;
    /*Employed Bee Phase*/
    for (i=0;i<FoodNumber;i++)
    {
    /*The parameter to be changed is determined randomly*/
    r = ((double) Math.random()*32767 / ((double)(32767)+(double)(1)) );
    param2change=(int)(r*D);

    /*A randomly chosen solution is used in procing a mutant solution of the solution i*/
    r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
    neighbour=(int)(r*FoodNumber);

    /*Randomly selected solution must be different from the solution i*/
    // while(neighbour==i)
    // {
    // r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
    // neighbour=(int)(r*FoodNumber);
    // }
    for(j=0;j<D;j++)
    solution[j]=Foods[i][j];

    /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
    r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
    solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;

    /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
    if (solution[param2change]<lb)
    solution[param2change]=lb;
    if (solution[param2change]>ub)
    solution[param2change]=ub;
    ObjValSol=calculateFunction(solution);
    FitnessSol=CalculateFitness(ObjValSol);

    /*a greedy selection is applied between the current solution i and its mutant*/
    if (FitnessSol>fitness[i])
    {

    /*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/
    trial[i]=0;
    for(j=0;j<D;j++)
    Foods[i][j]=solution[j];
    f[i]=ObjValSol;
    fitness[i]=FitnessSol;
    }
    else
    { /*if the solution i can not be improved, increase its trial counter*/
    trial[i]=trial[i]+1;
    }

    }

    /*end of employed bee phase*/

    }

    /* A food source is chosen with the probability which is proportioal to its quality*/
    /*Different schemes can be used to calculate the probability values*/
    /*For example prob(i)=fitness(i)/sum(fitness)*/
    /*or in a way used in the metot below prob(i)=a*fitness(i)/max(fitness)+b*/
    /*probability values are calculated by using fitness values and normalized by dividing maximum fitness value*/
    void CalculateProbabilities()
    {
    int i;
    double maxfit;
    maxfit=fitness[0];
    for (i=1;i<FoodNumber;i++)
    {
    if (fitness[i]>maxfit)
    maxfit=fitness[i];
    }

    for (i=0;i<FoodNumber;i++)
    {
    prob[i]=(0.9*(fitness[i]/maxfit))+0.1;
    }

    }

    void SendOnlookerBees()
    {

    int i,j,t;
    i=0;
    t=0;
    /*onlooker Bee Phase*/
    while(t<FoodNumber)
    {

    r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
    if(r<prob[i]) /*choose a food source depending on its probability to be chosen*/
    {
    t++;

    /*The parameter to be changed is determined randomly*/
    r = ((double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
    param2change=(int)(r*D);

    /*A randomly chosen solution is used in procing a mutant solution of the solution i*/
    r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
    neighbour=(int)(r*FoodNumber);

    /*Randomly selected solution must be different from the solution i*/
    while(neighbour == i)
    {
    //System.out.println(Math.random()*32767+" "+32767);
    r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
    neighbour=(int)(r*FoodNumber);
    }
    for(j=0;j<D;j++)
    solution[j]=Foods[i][j];

    /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
    r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
    solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;

    /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
    if (solution[param2change]<lb)
    solution[param2change]=lb;
    if (solution[param2change]>ub)
    solution[param2change]=ub;
    ObjValSol=calculateFunction(solution);
    FitnessSol=CalculateFitness(ObjValSol);

    /*a greedy selection is applied between the current solution i and its mutant*/
    if (FitnessSol>fitness[i])
    {
    /*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/
    trial[i]=0;
    for(j=0;j<D;j++)
    Foods[i][j]=solution[j];
    f[i]=ObjValSol;
    fitness[i]=FitnessSol;
    }
    else
    { /*if the solution i can not be improved, increase its trial counter*/
    trial[i]=trial[i]+1;
    }
    } /*if */
    i++;
    if (i==FoodNumber-1)
    i=0;
    }/*while*/

    /*end of onlooker bee phase */
    }

    /*determine the food sources whose trial counter exceeds the "limit" value. In Basic ABC, only one scout is allowed to occur in each cycle*/
    void SendScoutBees()
    {
    int maxtrialindex,i;
    maxtrialindex=0;
    for (i=1;i<FoodNumber;i++)
    {
    if (trial[i]>trial[maxtrialindex])
    maxtrialindex=i;
    }
    if(trial[maxtrialindex]>=limit)
    {
    init(maxtrialindex);
    }
    }

    double calculateFunction(double sol[])
    {
    return Rastrigin (sol);
    }
    double sphere(double sol[])
    {
    int j;
    double top=0;
    for(j=0;j<D;j++)
    {
    top=top+sol[j]*sol[j];
    }
    return top;
    }

    double Rosenbrock(double sol[])
    {
    int j;
    double top=0;
    for(j=0;j<D-1;j++)
    {
    top=top+100*Math.pow((sol[j+1]-Math.pow((sol[j]),(double)2)),(double)2)+Math.pow((sol[j]-1),(double)2);
    }
    return top;
    }

    double Griewank(double sol[])
    {
    int j;
    double top1,top2,top;
    top=0;
    top1=0;
    top2=1;
    for(j=0;j<D;j++)
    {
    top1=top1+Math.pow((sol[j]),(double)2);
    top2=top2*Math.cos((((sol[j])/Math.sqrt((double)(j+1)))*Math.PI)/180);

    }
    top=(1/(double)4000)*top1-top2+1;
    return top;
    }

    double Rastrigin(double sol[])
    {
    int j;
    double top=0;

    for(j=0;j<D;j++)
    {
    top=top+(Math.pow(sol[j],(double)2)-10*Math.cos(2*Math.PI*sol[j])+10);
    }
    return top;
    }
    }

    使用方法是:
    public class test {
    static beeColony bee=new beeColony();

    public static void main(String[] args) {
    int iter=0;
    int run=0;
    int j=0;
    double mean=0;
    //srand(time(NULL));
    for(run=0;run<bee.runtime;run++)
    {
    bee.initial();
    bee.MemorizeBestSource();
    for (iter=0;iter<bee.maxCycle;iter++)
    {
    bee.SendEmployedBees();
    bee.CalculateProbabilities();
    bee.SendOnlookerBees();
    bee.MemorizeBestSource();
    bee.SendScoutBees();
    }
    for(j=0;j<bee.D;j++)
    {
    //System.out.println("GlobalParam[%d]: %f\n",j+1,GlobalParams[j]);
    System.out.println("GlobalParam["+(j+1)+"]:"+bee.GlobalParams[j]);
    }
    //System.out.println("%d. run: %e \n",run+1,GlobalMin);
    System.out.println((run+1)+".run:"+bee.GlobalMin);
    bee.GlobalMins[run]=bee.GlobalMin;
    mean=mean+bee.GlobalMin;
    }
    mean=mean/bee.runtime;
    //System.out.println("Means of %d runs: %e\n",runtime,mean);
    System.out.println("Means of "+bee.runtime+"runs: "+mean);

    }

    }

    9. 人工蜂群演算法的matlab的編程詳細代碼,最好有基於人工蜂群演算法的人工神經網路的編程代碼

    蟻群演算法(ant colony optimization, ACO),又稱螞蟻演算法,是一種用來在圖中尋找優化路徑的機率型演算法。它由Marco Dorigo於1992年在他的博士論文中提出,其靈感來源於螞蟻在尋找食物過程中發現路徑的行為。蟻群演算法是一種模擬進化演算法,初步的研究表明該演算法具有許多優良的性質。針對PID控制器參數優化設計問題,將蟻群演算法設計的結果與遺傳演算法設計的結果進行了比較,數值模擬結果表明,蟻群演算法具有一種新的模擬進化優化方法的有效性和應用價值。


    參考下蟻群訓練BP網路的代碼。

    閱讀全文

    與人工蜂群演算法程序相關的資料

    熱點內容
    app地推業績統計在哪裡 瀏覽:993
    維語電影網站大全 瀏覽:958
    程序員骨腫瘤上熱搜 瀏覽:847
    聚優電影 瀏覽:45
    國企保底工資演算法 瀏覽:730
    視聽說伺服器地址是什麼意思 瀏覽:657
    一部男主叫大志的電影叫 瀏覽:650
    安卓反編譯後編譯不回來 瀏覽:195
    快穿肉文推薦 瀏覽:263
    lol新手推薦什麼伺服器 瀏覽:283
    尼桑奇駿壓縮機 瀏覽:170
    android模態對話框 瀏覽:793
    手機為什麼無法接到伺服器 瀏覽:627
    背景虛化人物清晰哪個app 瀏覽:657
    android開發職位 瀏覽:764
    勒索病毒加密文件特徵識別 瀏覽:815
    小車控制源碼 瀏覽:9
    程序員右手筋脈疼痛沒力 瀏覽:841
    手機視頻太大如何壓縮 瀏覽:555
    出租伺服器怎麼用 瀏覽:229