日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

智能算法之Matlab实现(1)——遗传算法(1)

發(fā)布時間:2025/4/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 智能算法之Matlab实现(1)——遗传算法(1) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

遺傳算法的過程在這里先不介紹了,可能在接下來的幾篇文章會介紹,這里介紹些實用的。

(1)Sheffield遺傳算法工具箱的安裝

我共享了下修改過文件名和后綴名的原版工具箱,地址為:http://pan.baidu.com/s/1inVKE

安裝方法

將整個文件夾復制到matlab安裝文件夾中的toolbox文件夾

例如:C:\Program Files\MATLAB\R2013b\toolbox文件夾。

然后在Command Window里面輸入:

str = ['C:\Program Files\MATLAB\R2013b\toolbox\gatbx']addpath(str)

可能有些同學出現(xiàn)過這個問題,

Undefined function or method 'crtbp' for input arguments of type 'double'

或者是:

Cannot find an exact (case-sensitive) match for 'crtbp.m'
The closest match is C:\Program Files\MATLAB\R2012a\toolbox\gatbx\CRTBP.M
To change the file extension, cd to the file's folder, type:
?? movefile CRTBP.M CRTBP.m_bad; movefile CRTBP.m_bad CRTBP.m
and then cd back.

這是因為新舊Matlab版本對于M文件的文件名要求不盡相同,將其全部改為小寫文件名和文件后綴名即可。

(2)應用實例(1)——單變量函數(shù)求最值

類似模板的東西,求解此函數(shù)最大值:

有如下代碼:

%% Do Some Cleaning clc clear all; close all; %% Set the initial parameters lb = 1; ub = 2;%x belongs to [1,2] %% Plot figure(1); hold on; ezplot('sin(10*pi*X)/X',[lb,ub]); xlabel('x/X') ylabel('y/Y') %% Define the parameters of GA NIND = 40;%size of the group MAXGEN = 20;%max generations PRECI = 20;%length of a individual GGAP = 0.95;%gap px = 0.7;%the possibility of cross production pm = 0.1;%the possibility of mutation trace = zeros(2,MAXGEN);%init value of algorithm尋優(yōu)函數(shù) FieldD = [PRECI;lb;ub;1;0;1;1];%區(qū)域描述器 Chrom = crtbp(NIND,PRECI);%creat random discrete group %% Optimizations gen = 0;%counter of generations X = bs2rv(Chrom,FieldD);%bin to dec ObjV = sin(10 * pi * X) ./ X;%cal the f(x) while gen < MAXGENFitnV = ranking(ObjV);%allocate the adaptnessSelCh = select('sus',Chrom,FitnV,GGAP);%selectSelCh = recombin('xovsp',SelCh,px);%recombineSelCh = mut(SelCh, pm);%mutateX = bs2rv(SelCh,FieldD);%to decObjVSel = sin(10 * pi * X)./ X;%cal the next-gen's target f(x)[Chrom, ObjV] = reins(Chrom, SelCh, 1, 1, ObjV, ObjVSel);%reinsert them to father-genX = bs2rv(Chrom, FieldD);gen = gen + 1;%counter+=1%get every gen's answers and it's nums, Y stant for best f(x), I for%nums;[Y I] = min(ObjV);trace(1,gen) = X(I);trace(2,gen) = Y; end %% Plot plot(trace(1,:),trace(2,:),'bo');%plot every gen's answer grid on; plot(X,ObjV,'b*'); hold off %% Plot the evolution figure(2); plot(1:MAXGEN,trace(2,:)); grid on xlabel('count of generations') ylabel('answer') title('procedure') bestY = trace(2,end) bestX = trace(1,end)

運行結(jié)果:

(3)應用實例(2)——雙變量函數(shù)求最值

那么有如下代碼:

%% Do Some Cleaning clc clear all; close all; %% Set the initial parameters lbx = -2; ubx = 2;%x belongs to [-2,2] lby = -2; uby = 2;%y belongs to [-2,2] %% Plot figure(1); ezmesh('y*sin(2*pi*x) + x*cos(2*pi*y)',[lbx,ubx,lby,uby],50); xlabel('x/X') ylabel('y/Y') hold on; %% Define the parameters of GA NIND = 40;%size of the group MAXGEN = 20;%max generations PRECI = 20;%length of a individual GGAP = 0.95;%gap px = 0.7;%the possibility of cross production pm = 0.01;%the possibility of mutation trace = zeros(3,MAXGEN);%init value of algorithm尋優(yōu)函數(shù) FieldD = [PRECI PRECI;lbx lby;ubx uby;1 1;0 0;1 1;1 1];%Field discriber Chrom = crtbp(NIND,PRECI*2);%creat random discrete group %% Optimizations gen = 0;%counter of generations XY = bs2rv(Chrom,FieldD);%bin to dec X = XY(:,1); Y = XY(:,2); ObjV = Y.*sin(2*pi*X) + X.*cos(2*pi*Y);%cal the f(x,y) while gen < MAXGENFitnV = ranking(-ObjV);%allocate the adaptnessSelCh = select('sus',Chrom,FitnV,GGAP);%selectSelCh = recombin('xovsp',SelCh,px);%recombineSelCh = mut(SelCh, pm);%mutateXY = bs2rv(SelCh,FieldD);%bin to decX = XY(:,1);Y = XY(:,2);ObjVSel = Y.*sin(2*pi*X) + X.*cos(2*pi*Y);%cal the next-gen's target f(x)[Chrom, ObjV] = reins(Chrom, SelCh, 1, 1, ObjV, ObjVSel);%reinsert them to father-genXY = bs2rv(Chrom,FieldD);gen = gen + 1;%counter+=1%get every gen's answers and it's nums, Y stant for best f(x), I for%nums;[Y, I] = max(ObjV);trace(1:2,gen) = XY(I,:);trace(3,gen) = Y; end %% Plot plot3(trace(1,:),trace(2,:),trace(3,:),'bo');%plot every gen's answer grid on; plot3(XY(:,1),XY(:,2),ObjV,'b*'); hold off %% Plot the evolution figure(2); plot(1:MAXGEN,trace(3,:)); grid on xlabel('count of generations') ylabel('answer') title('procedure') bestX = trace(1,end) bestY = trace(2,end) bestZ = trace(3,end)

運行結(jié)果:

(4)應用實例(3)——遺傳算法接力優(yōu)化(采用系統(tǒng)GAtool工具箱)

優(yōu)化此函數(shù):

第一個文件(主文件):

%主程序:本程序采用遺傳算法接力進化, %將上次進化結(jié)束后得到的最終種群作為下次輸入的初始種群 clc; close all; clear all; %進化的代數(shù) T=100; optionsOrigin=gaoptimset('Generations',T/2); [x,fval,reason,output,finnal_pop]=ga(@ff,2,optionsOrigin); %進行第二次接力進化 options1=gaoptimset('Generations',T/2,'InitialPopulation',finnal_pop,...'PlotFcns',@gaplotbestf); [x,fval,reason,output,finnal_pop]=ga(@ff,2,options1); Bestx=x BestFval=fval

評價函數(shù)文件,與主文件一起保存,名字為ff.m

%子函數(shù):適應度函數(shù)同時也是目標函數(shù),函數(shù)存儲名稱為ch14_2f.m function f=ff(x) g1=1.5+x(1)*x(2)-x(1)-x(2); g2=-x(1)*x(2); if(g1>0||g2>10)f=100; elsef=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1); end

程序結(jié)果:

至此,第一部分結(jié)束,下一篇將介紹遺傳算法的基本內(nèi)容。

轉(zhuǎn)載于:https://www.cnblogs.com/zhengnanlee/p/3439649.html

《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的智能算法之Matlab实现(1)——遗传算法(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。