智能優(yōu)化算法源代碼_第1頁
智能優(yōu)化算法源代碼_第2頁
智能優(yōu)化算法源代碼_第3頁
智能優(yōu)化算法源代碼_第4頁
智能優(yōu)化算法源代碼_第5頁
已閱讀5頁,還剩34頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、人工螞蟻算法%function x,y, minvalue = aa(func) % example x, y,minvalue = aa('foxhole') clc; tic; subplot(2,2,1); % plot 1draw(func);title(func, ' function');%初始化各參數(shù)ant=100;%螞蟻規(guī)模echo=200;%迭代次數(shù) step=0.01*rand(1);%局部搜索時(shí)的步長temp=0,0;%各子區(qū)間長度start1=-100;end1=100;start2=-100;end2=100;len1=(end1-st

2、art1)/ant;len2=(end2-start2)/ant;%p = 0.2;%初始化螞蟻位置 for i=1:antx(i,1)=(start1+(end1-start1)*rand(1);x(i,2)=(start2+(end2-start2)*rand(1);%func=aa_foxhole_func(x(i,1),x(i,2);val=feval(func,x(i,1),x(i,2); t0(i)=exp(-val);%初始信息素,隨函數(shù)值大,信息素濃度小,反之亦然 %* end; %至此初始化完成for echo=1:echo %開始尋優(yōu) %p0函數(shù)定義,p0為全局轉(zhuǎn)移選擇因子

3、a1=0.9;b1=(1/echo)*2*log(1/2);f1=a1*exp(b1*echo);a2=0.225;b2=(1/echo)*2*log(2);f2=a2*exp(b2*echo);if echo<=(echo/2) p0=f1;else p0=f2;end;%p函數(shù)定義,p為信息素蒸發(fā)系數(shù)a3=0.1;b3=(1/echo).*log(9);p=a3*exp(b3*echo);lamda=0.10+(0.14-0.1)*rand(1);%全局轉(zhuǎn)移步長參數(shù)wmax=1.0+(1.4-1.0)*rand(1);%步長更新參數(shù)上限wmin=0.2+(0.8-0.2)*rand(

4、1);%步長更新參數(shù)下限%尋找初始最優(yōu)值 t_best=t0(1); for j=1:ant if t0(j)>=t_best t_best=t0(j); bestindex=j; end; end; w=wmax-(wmax-wmin)*(echo/echo); %局部搜索步長更新參數(shù) for j_g=1:ant %全局轉(zhuǎn)移概率求取,當(dāng)該螞蟻隨在位置不是bestindex時(shí) if j_g=bestindex r=t0(bestindex)-t0(j_g); prob(j_g)=exp(r)/exp(t0(bestindex); else %當(dāng)j_g=bestindex的時(shí)候進(jìn)行局部搜索

5、 if rand(1)<0.5 temp(1,1)=x(bestindex,1)+w*step; temp(1,2)=x(bestindex,2)+w*step; else temp(1,1)=x(bestindex,1)-w*step; temp(1,2)=x(bestindex,2)-w*step; end; prob(j_g)=0;%bestindex的螞蟻不進(jìn)行全局轉(zhuǎn)移 end; x1_t=temp(1,1); x2_t=temp(1,2); x1_b=x(bestindex,1); x2_b=x(bestindex,2); %func1 = aa_foxhole_func(x1

6、_t,x2_t); %* %f1_t=func1; f1_t=feval(func,x(i,1),x(i,2); f1_b=feval(func,x1_b,x2_b); %f1_t=(x1_t-1).2+(x2_t-2.2).2+1; %func2 = aa_foxhole_func(x1_b,x2_b); %* %f1_b=func2; %f1_b=(x1_b-1).2+(x2_b-2.2).2+1; if exp(-f1_t)>exp(-f1_b) x(bestindex,1)=temp(1,1); x(bestindex,2)=temp(1,2); end; end; for j_

7、g_tr=1:ant if prob(j_g_tr)<p0 x(j_g_tr,1)=x(j_g_tr,1)+lamda*(x(bestindex,1)-x(j_g_tr,1);%xi=xi+lamda*(xbest-xi) x(j_g_tr,2)=x(j_g_tr,2)+lamda*(x(bestindex,2)-x(j_g_tr,2);%xi=xi+lamda*(xbest-xi) x(j_g_tr,1)=bound(x(j_g_tr,1),start1,end1); x(j_g_tr,2)=bound(x(j_g_tr,2),start2,end2); else x(j_g_tr,1

8、)=x(j_g_tr,1)+(-1)+2*rand(1)*len1;%xi=xi+rand(-1,1)*len1 x(j_g_tr,2)=x(j_g_tr,2)+(-1)+2*rand(1)*len2;%xi=xi+rand(-1,1)*len2 x(j_g_tr,1)=bound(x(j_g_tr,1),start1,end1); x(j_g_tr,2)=bound(x(j_g_tr,2),start2,end2); end; end;%信息素更新 subplot(2,2,2); % plot 1 bar(x(bestindex,1) x(bestindex,2),0.25); %color

9、map (cool); axis(0 3 -40 40 ) ; title (date;'iteration ', num2str(echo);xlabel('min_x = ',num2str(x(bestindex,1),' ', 'min_y = ', num2str(x(bestindex,2); for t_t=1:ant %func=aa_foxhole_func(x(t_t,1),x(t_t,2); val1=feval(func,x(t_t,1),x(t_t,2); t0(t_t)=(1-p)*t0(t_t)+(e

10、xp(-val1); %* end; c_iter,i_iter=max(t0); %求取每代全局最優(yōu)解 minpoint_iter=x(i_iter,1),x(i_iter,2); %func3 = aa_foxhole_func(x(i_iter,1),x(i_iter,2); %* val2=feval(func,x(i_iter,1),x(i_iter,2); minvalue_iter= val2; %minvalue_iter=(x(i_iter,1)-1).2+(x(i_iter,2)-2.2).2+1; min_local(echo)=minvalue_iter;%保存每代局部

11、最優(yōu)解 subplot(2,2,3);% plot 2 plot(x(bestindex,1),x(bestindex,2),'rs','markerfacecolor','r', 'markersize',8), grid on; title ('global min value = ', num2str(minvalue_iter); hold on; plot(x(:,1),x(:,2),'g.'), pause (0.02); hold off ; axis(-100 100 -100 10

12、0); grid on; %將每代全局最優(yōu)解存到min_global矩陣中 if echo >= 2 if min_local(echo)<min_global(echo-1) min_global(echo)=min_local(echo); else min_global(echo)=min_global(echo-1); end; else min_global(echo)=minvalue_iter; end; subplot(2,2,4);% plot 3 min_global=min_global' index(:,1)=1:echo; plot(echo, m

13、in_global(echo),'y*') %axis(0 echo 0 10); hold on; title (func,' (x) = ', num2str(minvalue_iter),'color','r'); xlabel('iteration'); ylabel('f(x)'); grid on; end;%echo循環(huán)結(jié)束 c_max,i_max=max(t0); minpoint=x(i_max,1),x(i_max,2); %func3 = aa_foxhole_func(x(i

14、_max,1),x(i_max,2); %* %minvalue = func3; minvalue=feval(func,x(i_max,1),x(i_max,2); x=x(bestindex,1); y=x(bestindex,2); runtime=toc人工免疫算法function x,y,fx,vfx,vmfit,p,vpm = ai(func,gen,n,pm,per); % example x,y,fx = ai('foxhole')subplot(2,2,1);draw(func);title( func, ' function');if na

15、rgin = 1, % gen = 200; n = round(size(p,1)/2); pm = 0.0005; per = 0.0; fat = 10; %gen = 250; n = size(p,1); pm = 0.01; per = 0.0; fat = .1; p = cadeia(200,44,0,0,0); gen = 40; n = size(p,1); pm = 0.2; per = 0.0; fat = 0.1;end; while n <= 0, n = input('n has to be at least one. type a new valu

16、e for n: ');end; xmin=-100;xmax=100;ymin=-100;ymax=100; x = decode(p(:,1:22),xmin,xmax); y = decode(p(:,23:end),ymin,ymax); %fit = eval(f);%fit=ai_foxhole_func(x,y);%fit=feval(func,x' y');%imprime(1,vxp,vyp,vzp,x,y,fit,1,1); % hypermutation controlling parameterspma = pm; itpm = gen; pmr

17、 = 0.8; % general defintionsvpm = ; vfx = ; vmfit = ; valfx = 1; n,l = size(p); it = 0; print = 1; % generationswhile it <= gen & valfx <= 100, x = decode(p(:,1:22),xmin,xmax); y = decode(p(:,23:end),ymin,ymax); t = ; cs = ; %fit = eval(f); %fit=ai_foxhole_func(x,y);% fit=feval(func,x'

18、 y'); a,ind = sort(fit); valx = x(ind(end-n+1:end); valy = y(ind(end-n+1:end); fx = a(end-n+1:end); % n best individuals (maximization) % reproduction t,pcs = reprod(n,fat,n,ind,p,t); % hypermutation m = rand(size(t,1),l) <= pm; t = t - 2 .* (t.*m) + m; t(pcs,:) = p(fliplr(ind(end-n+1:end),:)

19、; % new re-selection (multi-peak solution) x = decode(t(:,1:22),xmin,xmax); y = decode(t(:,23:end),ymin,ymax); %fit=ai_foxhole_func(x,y);% fit=feval(func,x' y'); %fit = eval(f); pcs = 0 pcs; for i=1:n, out(i),bcs(i) = min(fit(pcs(i)+1:pcs(i+1); % mimimazion problem %* bcs(i) = bcs(i) + pcs(i

20、); end; p(fliplr(ind(end-n+1:end),:) = t(bcs,:); % editing (repertoire shift) nedit = round(per*n); it = it + 1; p(ind(1:nedit),:) = cadeia(nedit,l,0,0,0); pm = pmcont(pm,pma,pmr,it,itpm); valfx = min(fx); %* vpm = vpm pm; vfx = vfx valfx; vmfit = vmfit mean(fit); disp(sprintf('it.: %d pm: %.4f

21、x: %2.2f y: %2.2f av.: %2.2f f(x,y): %2.3f',it,pm,valx(1),valy(1),vmfit(1),valfx); subplot(2,2,2); % plot 1 bar(valx(1) valy(1),0.25); axis(0 3 -40 40 ) ; title ('iteration ', num2str(it); pause (0.1); subplot(2,2,3); % plot 2 plot(valx(1),valy(1),'rs','markerfacecolor',&

22、#39;r', 'markersize',8) hold on; %plot(x(:,1),x(:,2),'k.'); set(gca,'color','g') hold off; grid on; axis(-100 100 -100 100 ) ; title('global min = ',num2str(valfx); xlabel('min_x= ',num2str(valx(1),' min_y= ',num2str(valy(1); subplot(2,2,4)

23、; % plot 3 plot(it,valfx ,'k.') axis(0 gen 0 10); hold on; title (func ,'(x) = ', num2str(valfx); xlabel('iteration'); ylabel('f(x)'); grid on;end; % end while %imprime(print,vxp,vyp,vzp,x,y,fit,it,1);x = valx(1); y = valy(1); fx = min(fx); %*% x = p(ind(end),1:22); y

24、 = p(ind(end),23:44); fx = max(fx); % plot 4 % - % internal subfunctions% - % % printfunction = imprime(print,vx,vy,vz,x,y,fx,it,mit);% x,fx -> actual values% vxplot, vplot -> original (base) functionif print = 1, if rem(it,mit) = 0, mesh(vx,vy,vz); hold on; axis(-100 100 -100 100 0 500); xlab

25、el('x'); ylabel('y'); zlabel('f(x,y)'); plot3(x,y,fx,'k*'); drawnow; hold off; end;end; % reproductionfunction t,pcs = reprod(n,fat,n,ind,p,t);% n -> number of clones% fat -> multiplying factor% ind -> best individuals% t -> temporary population% pcs ->

26、 final position of each cloneif n = 1, cs = n; t = ones(n,1) * p(ind(1),:);else, for i=1:n, % cs(i) = round(fat*n/i); cs(i) = round(fat*n); pcs(i) = sum(cs); t = t; ones(cs(i),1) * p(ind(end-i+1),:); end;end; % control of pmfunction pm = pmcont(pm,pma,pmr,it,itpm);% pma -> initial value% pmr ->

27、; control rate% itpm -> iterations for restoringif rem(it,itpm) = 0, pm = pm * pmr; if rem(it,10*itpm) = 0, pm = pma; end;end; % decodify bitstringsfunction x = decode(v,min,max);% x -> real value (precision: 6)% v -> binary string (length: 22)v = fliplr(v); s = size(v);aux = 0:1:21; aux =

28、ones(s(1),1)*aux;x1 = sum(v.*2.aux)');x = min + (max-min)*x1 ./ 4194303;function ab,ag = cadeia(n1,s1,n2,s2,bip)%default parameter value seetingif nargin = 2, n2 = n1; s2 = s1; bip = 1;elseif nargin = 4, bip = 1;end;% antibody (ab) chainsab = 2 .* rand(n1,s1) - 1;%create n1 row s1 column array,

29、its value range is between -1 or 1if bip = 1, ab = hardlims(ab);else, ab = hardlim(ab);end;% antigen (ag) chainsag = 2 .* rand(n2,s2) - 1;if bip = 1, ag = hardlims(ag);else, ag = hardlim(ag);end;% end function cadeia%-免疫粒子群優(yōu)化算法(artificial immune - particle swarm optimization) function x,y,result=pso

30、_ai(func)% example x, y,minvalue = pso_ai('foxhole')clc; subplot(2,2,1); % plot 1draw(func);title(func, ' function');ticformat long;%-給定初始化條件-c1=1.4962; %學(xué)習(xí)因子1c2=1.4962; %學(xué)習(xí)因子2w=0.7298; %慣性權(quán)重maxdt=200; %最大迭代次數(shù)d=2; %搜索空間維數(shù)(未知數(shù)個(gè)數(shù))n=100; %初始化群體個(gè)體數(shù)目eps=10(-20); %設(shè)置精度(在已知最小值時(shí)候用)ds=10; %每隔

31、ds次循環(huán)就檢查最優(yōu)個(gè)體是否變優(yōu)replacep=0.6; %粒子的概率大于replacep將被免疫替換mind=1e-015; %粒子間的最小距離psum=0; %個(gè)體最佳的和range=100;count = 0;%-初始化種群的個(gè)體- for i=1:n for j=1:d x(i,j)=-range+2*range*rand; %隨機(jī)初始化位置 v(i,j)=randn; %隨機(jī)初始化速度 endend %-先計(jì)算各個(gè)粒子的適應(yīng)度,并初始化pi和pg- for i=1:n %p(i)=foxhole(x(i,:),d); %fitness是計(jì)算各個(gè)粒子適應(yīng)度的函數(shù),見文件fitness

32、.m %* p(i)=feval(func,x(i,:); y(i,:)=x(i,:);endpg=x(1,:); %pg為全局最優(yōu)for i=2:n if feval(func,x(i,:)<feval(func,pg) %* pg=x(i,:); endend %-進(jìn)入主要循環(huán),按照公式依次迭代,直到滿足精度要求- for t=1:maxdt for i=1:n v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:)+c2*rand*(pg-x(i,:); x(i,:)=x(i,:)+v(i,:); if feval(func,x(i,:)<p(i) %*

33、 p(i)=feval(func,x(i,:); %* y(i,:)=x(i,:); end if p(i)<feval(func,pg) %* pg=y(i,:); subplot(2,2,2); % plot 1 bar(pg,0.25); axis(0 3 -40 40 ) ; title ('iteration ', num2str(t); pause (0.1); subplot(2,2,3); % plot 2 plot(pg(1,1),pg(1,2),'rs','markerfacecolor','r', &#

34、39;markersize',8) hold on; plot(x(:,1),x(:,2),'k.'); set(gca,'color','g') hold off; grid on; axis(-100 100 -100 100 ) ; title('global min = ',num2str(p(i); xlabel('min_x= ',num2str(pg(1,1),' min_y= ',num2str(pg(1,2); end end pbest(t)=feval(func,pg)

35、 ; %* % if foxhole(pg,d)<eps %如果結(jié)果滿足精度要求則跳出循環(huán)% break;% end %-開始進(jìn)行免疫 if t>ds if mod(t,ds)=0 && (pbest(t-ds+1)-pbest(t)<1e-020 %如果連續(xù)ds代數(shù),群體中的最優(yōu)沒有明顯變優(yōu),則進(jìn)行免疫. %在函數(shù)測試的過程中發(fā)現(xiàn),經(jīng)過一定代數(shù)的更新,個(gè)體最優(yōu)不完全相等,但變化非常非常小, %我認(rèn)為這個(gè)時(shí)候也應(yīng)用免疫了,所以我沒有用“pbest(t-ds+1)pbest(t)”作為判斷條件, %不過“(pbest(t-ds+1)-pbest(t)<1e

36、-020”是否合理也值得探討。 for i=1:n %先計(jì)算出個(gè)體最優(yōu)的和 psum=psum+p(i); end for i=1:n %免疫程序 for j=1:n %計(jì)算每個(gè)個(gè)體與個(gè)體i的距離 distance(j)=abs(p(j)-p(i); end num=0; for j=1:n %計(jì)算與第i個(gè)個(gè)體距離小于mind的個(gè)數(shù) if distance(j)<mind num=num+1; end end pf(i)=p(n-i+1)/psum; %計(jì)算適應(yīng)度概率 pd(i)=num/n; %計(jì)算個(gè)體濃度 a=rand; %隨機(jī)生成計(jì)算替換概率的因子 pr(i)=a*pf(i)+(1

37、-a)*pd(i); %計(jì)算替換概率 end for i=1:n if pr(i)>replacep x(i,:)=-range+2*range*rand(1,d); subplot(2,2,4); axi; % plot 4 plot(x(i,1),x(i,2),'k*'); grid on; axis(-100 100 -100 100 ) ; title('new min = ',num2str( feval(func,x(i,:); %* xlabel('immune ',num2str(count); pause (0.2); c

38、ount=count+1; end end end end end %-最后給出計(jì)算結(jié)果 x=pg(1,1);y=pg(1,2); result=feval(func,pg); %* toc%-算法結(jié)束-function probabolity(n,i)pf=p(n-i)/psum;%適應(yīng)度概率disp(pf);for jj=1:n distance(jj)=abs(p(jj)-p(i);endnum=0;for ii=1:n if distance(ii)<mind num=num+1; endendpd=num/n; %個(gè)體濃度pr=a*pf+(1-a)*pd; %替換概率% res

39、ult=pr;差分進(jìn)化算法function sol= de(func) % example sol= de('foxhole')ticpopsize= 100;lb=-100 -100;ub=100 100; sol = diffevolve(func, popsize, lb, ub);tocend function sol, fval, evals = diffevolve(varargin)%diffevolve differential evolution optimization% usage:% sol = diffevolve(problem)% sol = di

40、ffevolve(func, popsize, lb, ub) % sol = diffevolve(func, popsize, lb, ub, 'option1', value1, .) % sol, fval = diffevolve(.)% sol, fval, evals = diffevolve(.)% % diffevolve(func, popsize, lb, ub) tries to find the global optimum of % the fitness-function func, using a transversal differential evolution % st

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論