




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、遺傳算法程序(一) :說明:fga.m為遺傳算法的主程序;采用二進制Gray編碼,采用基于輪盤賭法的非線性排 名選擇 , 均勻交叉 ,變異操作 ,而且還引入了倒位操作 !function BestPop,Trace=fga(FUN,LB,UB,eranum,popsize,pCross,pMutation,pInversion,options)% BestPop,Trace=fmaxga(FUN,LB,UB,eranum,popsize,pcross,pmutation)% Finds a maximum of a function of several variables.% fmaxga
2、solves problems of the form:% max F(X) subject to: LB = X = UB% BestPop- 最優(yōu)的群體即為最優(yōu)的染色體群% Trace- 最佳染色體所對應的目標函數值% FUN- 目標函數% LB- 自變量下限% UB- 自變量上限% eranum- 種群的代數 ,取 100-1000(默認 200)% popsize- 每一代種群的規(guī)模;此可取50-200( 默認 100)% pcross- 交叉概率 ,一般取 0.5-0.85 之間較好 (默認 0.8)% pmutation - 初始變異概率 ,一般取 0.05-0.2 之間較好 (默
3、認 0.1)% pInversion - 倒位概率 ,一般取 0.050.3 之間較好 (默認 0.2)% options- 1*2 矩陣 ,options(1)=0 二進制編碼 (默認 0),option(1)=0 十進制編%碼,option(2)設定求解精度(默認1e-4)% T1=clock;if nargin0)error( 數據輸入錯誤 ,請重新輸入 (LBUB):);ends=sprintf(程序運行需要約 %.4f秒鐘時間,請稍等 ,(eranum*popsize/1000);disp(s);global m n NewPop children1 children2 VarNum
4、 bounds=LB;UB;bits=;VarNum=size(bounds,1);precision=options(2);% 由求解精度確定二進制編碼長度 bits=ceil(log2(bounds(:,2)-bounds(:,1) ./ precision);% 由設定精度劃分區(qū)間 Pop=InitPopGray(popsize,bits);% 初始化種群m,n=size(Pop);NewPop=zeros(m,n);children1=zeros(1,n);children2=zeros(1,n);pm0=pMutation;BestPop=zeros(era num, n);% 分配
5、初始解空間 BestPop,TraceTrace=zeros(eranum,length(bits)+1);i=1;while i=eranumfor j=1:mvalue(j)=feval(FUN(1,:),(b2f(Pop(j,:),bounds,bits);% 計算適應度endMaxValue,Index=max(value);BestPop(i,:)=Pop(Index,:);Trace(i,1)=MaxValue;Trace(i,(2:length(bits)+1)=b2f(BestPop(i,:),bounds,bits);selectpop=NonlinearRankSelect
6、(FUN,Pop,bounds,bits);% 非線性排名選擇 CrossOverPop=CrossOver(selectpop,pCross,round(unidrnd(eranum-i)/eranum);%采用多點交叉和均勻交叉,且逐步增大均勻交叉的概率%round(unidrnd(eranum-i)/eranum)MutationPop=Mutation(CrossOverPop,pMutation,VarNum);% 變異 InversionPop=Inversion(MutationPop,pInversion);% 倒位Pop=In vers ion Pop;% 更新pMutati
7、o n=pmO+(iA4)*(pCross/3-pmO)/(era nu mM);%隨著種群向前進化,逐步增大變異率至1/2 交叉率p(i)=pMutation;i=i+1;endt=1:eranum;plot(t,Trace(:,1);title(函數優(yōu)化的遺傳算法);xlabel(進化世代數(era nu m);ylabel(每一代最優(yōu)適應度(maxfitness);MaxFval,I=max(Trace(:,1);X=Trace(I,(2:length(bits)+1);hold on; plot(I,MaxFval,*);text(I+5,MaxFval,FMAX= num2str(M
8、axFval);str1=sprintf ( 進化到 %d 代 ,自變量為 %s 時,得本次求解的最優(yōu)值 %fn 對應染色體是: %s,I,num2str(X),MaxFval,num2str(BestPop(I,:);disp(str1);%figure(2);plot(t,p);% 繪制變異值增大過程T2=clock;elapsed_time=T2-T1;if elapsed_time(6)0elapsed_time(6)=elapsed_time(6)+60; elapsed_time(5)=elapsed_time(5)-1;endif elapsed_time(5)1 時,b(i)=
9、mod(a(i-1)+a(i),2)%其中原二進制串:a(1)a(2).a(n),Gray 串:b(1)b(2).b(n) initpop(i,:)=pop(1:end-1);endinitpop(popsize,:)=ones(1,len);%The whole one encoding individual%解碼function fval = b2f(bval,bounds,bits)% fval - 表征各變量的十進制數% bval - 表征各變量的二進制編碼串% bounds - 各變量的取值范圍% bits - 各變量的二進制編碼長度scale=(bou nds(:,2)-bou n
10、ds(:,1)./(24bits-1); %The range of the variables numV=size(bounds,1);cs=0 cumsum(bits);for i=1:numVa=bval(cs(i)+1):cs(i+1);fval(i)=sum(2.A(size(a,2)-1:-1:0).*a)*scale(i)+bou nds(i,1);end%選擇操作%采用基于輪盤賭法的非線性排名選擇%各個體成員按適應值從大到小分配選擇概率:%P(i)=(q/1-(1-q)An)*(1-q)9,其中 P(O)PP(n), sum(P(i)=1function selectpop=N
11、onlinearRankSelect(FUN,pop,bounds,bits)global m nselectpop=zeros(m,n);fit=zeros(m,1);for i=1:mfit(i)=feval(FUN(1,:),(b2f(pop(i,:),bounds,bits);% 以函數值為適應值做排名依據 endselectprob=fit/sum(fit);% 計算各個體相對適應度 (0,1)q=max(selectprob);% 選擇最優(yōu)的概率 x=zeros(m,2);x(:,1)=m:-1:1;y x(:,2)=sort(selectprob);r=q/(1-(1-q)Am)
12、;%標準分布基值newfit(x(:,2)=r*(1-q).A(x(:,1)-1);% 生成選擇概率newfit=cumsum(newfit);% 計算各選擇概率之和 rNums=sort(rand(m,1);fitIn=1;newIn=1;while newIn=mif rNums(newIn)newfit(fitIn) selectpop(newIn,:)=pop(fitIn,:); newIn=newIn+1;elsefitIn=fitIn+1;endend%交叉操作function NewPop=CrossOver(OldPop,pCross,opts)%OldPop 為父代種群, p
13、cross 為交叉概率 global m n NewPopr=rand(1,m); y1=find(r=pCross); len=length(y1);if len2&mod(len,2)=1% 如果用來進行交叉的染色體的條數為奇數,將其調整為偶數 y2(length(y2)+1)=y1(len);y1(len)=;endif length(y1)=2for i=0:2:length(y1)-2if opts=0NewPop(y1(i+1),:),NewPop(y1(i+2),:)=EqualCrossOver(OldPop(y1(i+1),:),OldPop(y1(i+2),:); else
14、NewPop(y1(i+1),:),NewPop(y1(i+2),:)=MultiPointCross(OldPop(y1(i+1),:),OldPop(y1(i+2),:); endendendNewPop(y2,:)=OldPop(y2,:);%采用均勻交叉function children1,children2=EqualCrossOver(parent1,parent2)global n children1 children2hidecode=round(rand(1,n);% 隨機生成掩碼 crossposition=find(hidecode=1);holdposition=fin
15、d(hidecode=0); children1(crossposition)=parent1(crossposition);% 掩碼為 1,父 1 為子 1 提供基因 children1(holdposition)=parent2(holdposition);% 掩碼為 0,父 2 為子 1 提供基因 children2(crossposition)=parent2(crossposition);% 掩碼為 1,父 2 為子 2 提供基因 children2(holdposition)=parent1(holdposition);% 掩碼為 0,父 1 為子 2 提供基因%采用多點交叉,交叉
16、點數由變量數決定function Children1,Children2=MultiPointCross(Parent1,Parent2)global n Children1 Children2 VarNumChildren1=Parent1;Children2=Parent2;Points=sort(unidrnd(n,1,2*VarNum);for i=1:VarNumChildren1(Points(2*i-1):Points(2*i)=Parent2(Points(2*i-1):Points(2*i);Children2(Points(2*i-1):Points(2*i)=Parent
17、1(Points(2*i-1):Points(2*i);end%變異操作function NewPop=Mutation(OldPop,pMutation,VarNum) global m n NewPop r=rand(1,m); position=find(r=1for i=1:lenk=unidrnd(n,1,VarNum); % 設置變異點數,一般設置 1 點 for j=1:length(k)if OldPop(position(i),k(j)=1 OldPop(position(i),k(j)=0;elseOldPop(position(i),k(j)=1;endendendend
18、NewPop=OldPop;%倒位操作function NewPop=Inversion(OldPop,pInversion)global m n NewPopNewPop=OldPop;r=rand(1,m);PopIn=find(r=1for i=1:lend=sort(unidrnd(n,1,2);if d(1)=1&d(2)=nNewPop(PopIn(i),1:d(1)-1)=OldPop(PopIn(i),1:d(1)-1);NewPop(PopIn(i),d(1):d(2)=OldPop(PopIn(i),d(2):-1:d(1);NewPop(PopIn(i),d(2)+1:n
19、)=OldPop(PopIn(i),d(2)+1:n); endend end遺傳算法程序(二)function youhuafunD=code;N=50; % Tunable maxgen=50;% Tunablecrossrate=0.5; %Tunable muterate=0.08; %Tunable generation=1;num = length(D); fatherrand=randint(num,N,3); score = zeros(maxgen,N); while generation1450)|(min(F2)=900)error(DATA property F2 ex
20、ceed its range (900,1450) end% get group property F1 of data, according to F2 value F4=zeros(size(F1);for ite=11:-1:1 index=find(F2best.max_fadvance_k=advance_k+1; x_better(advance_k)=fitness.x; best.max_f=max(f); best.popus=popus; best.x=fitness.x;endif mod(advance_k,SPEEDUP_INTER)=0RANGE=minmax(x_
21、better);RANGEadvance=0;endendreturn;function popus=init% 初始化M=50;%種群個體數目N=30;%編碼長度popus=round(rand(M,N);return;function fitness=fit(popus,RANGE)% 求適應度M,N=size(popus);fitn ess=zeros(M,1);% 適應度f=zeros(M,1);% 函數值A=RANGE(1);B=RANGE(2);初始取值范圍0 255for m=1:Mx=0;for n=1:Nx=x+popus(m, n)*(2( n-1);endx=x*(B-A
22、)/(2AN)+A;for k=1:5 f(m,1)=f(m,1)-(k*sin(k+1)*x+k);endendf_std=(f-min(f)./(max(f)-min(f);% 函數值標準化 fitness.f=f;fitness.f_std=f_std;fitness.x=x;return;function picked=choose(popus,fitness)% 選擇 f=fitness.f;f_std=fitness.f_std;M,N=size(popus);choose_N=3; picked=zeros(choose_N,2); p=zeros(M,1);%選擇 choose
23、_N 對雙親 %記錄選擇好的雙親%選擇概率d_order=zeros(M,1);%把父代個體按適應度從大到小排序f_t=sort(f,descend);% 將適應度按降序排列for k=1:Mx=find(f=f_t(k);% 降序排列的個體序號 d_order(k)=x(1);endfor m=1:Mpopus_t(m,:)=popus(d_order(m),:);endpopus=popus_t;f=f_t;p=f_std./sum(f_std);%選擇概率c_p=cumsum(p);%累積概率for cn=1:choose_Npicked(cn,1)=roulette(c_p); % 輪盤賭picked(cn,2)=roulette(c_p); % 輪盤賭 popus=intercross(popus,picked(cn,:);% 雜交 endpopus=aberrance(popus,picked);% 變異 return;function popus=interc
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 天津大學《數據庫系統(tǒng)設計實踐》2023-2024學年第二學期期末試卷
- 菏澤學院《醫(yī)學影像設備安裝與維修學》2023-2024學年第二學期期末試卷
- 肇慶醫(yī)學高等??茖W?!犊删幊炭刂破骷夹g》2023-2024學年第二學期期末試卷
- 信陽航空職業(yè)學院《建筑設計基礎二》2023-2024學年第二學期期末試卷
- 哈爾濱傳媒職業(yè)學院《畢業(yè)論文寫作》2023-2024學年第二學期期末試卷
- 湖州學院《微信小程序開發(fā)與實踐》2023-2024學年第二學期期末試卷
- 重慶移通學院《Pthon數據分析與挖掘基礎》2023-2024學年第二學期期末試卷
- 廣州航海學院《體育(上)》2023-2024學年第二學期期末試卷
- 四川應用技術職業(yè)學院《工程制圖與CAD》2023-2024學年第二學期期末試卷
- 溫州科技職業(yè)學院《智能汽車傳感技術》2023-2024學年第二學期期末試卷
- 廣東省梅州市梅縣區(qū)2023-2024學年八年級上學期期末數學試題
- 2025屆江蘇省南通市海門市海門中學高三最后一模數學試題含解析
- 2024年世界職業(yè)院校技能大賽高職組“聲樂、器樂表演組”賽項參考試題庫(含答案)
- 2024數據中心綜合布線工程設計
- 胸外科講課全套
- 2024年下半年中煤科工集團北京華宇工程限公司中層干部公開招聘易考易錯模擬試題(共500題)試卷后附參考答案
- 2023年國家公務員錄用考試《申論》真題(副省卷)及答案解析
- 2025屆上海市寶山區(qū)行知中學物理高一第一學期期末檢測試題含解析
- 流感課件名稱教學課件
- 學生課業(yè)發(fā)展的測量與評價
- 二年級上冊勞動《理床鋪》課件
評論
0/150
提交評論