Matlab智能算法之遗传算法(1)

以往写过的一篇文章了,旧了

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\genetic']

addpath(str)

----------------

mac:

str = ['/Applications/MATLAB.app/toolbox/genetic'];

addpath(str);

----------------

可能有些同学出现过这个问题,

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)——单变量函数求最值

类似模板的东西,求解此函数最大值:

有如下代码:

%% 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寻优函数

FieldD = [PRECI;lb;ub;1;0;1;1];%区域描述器

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

SelCh= select('sus',Chrom,FitnV,GGAP);%selectSelCh= recombin('xovsp',SelCh,px);%recombineSelCh= mut(SelCh,pm);%mutate

X= bs2rv(SelCh,FieldD);%todec

ObjVSel= 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-gen

X= 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)

运行结果:

(3)应用实例(2)——双变量函数求最值

那么有如下代码:

%% 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寻优函数

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

SelCh= select('sus',Chrom,FitnV,GGAP);%selectSelCh= recombin('xovsp',SelCh,px);%recombineSelCh= mut(SelCh,pm);%mutate

XY= bs2rv(SelCh,FieldD);%binto dec

X= 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-gen

XY= 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)

运行结果:

(4)应用实例(3)——遗传算法接力优化(采用系统GAtool工具箱)

优化此函数:

第一个文件(主文件):

%主程序:本程序采用遗传算法接力进化,

%将上次进化结束后得到的最终种群作为下次输入的初始种群

clc;

close all;

clear all;

%进化的代数

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

评价函数文件,与主文件一起保存,名字为ff.m

%子函数:适应度函数同时也是目标函数,函数存储名称为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;

else

f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);

end

程序结果:

matlab bs2rv.m,Matlab智能算法之遗传算法(一)相关推荐

  1. matlab bs2rv.m,matlab遗传算法工具箱中的例子不能用?

    matlab遗传算法工具箱中的例子不能用,我也发现了,求助 figure(1); fplot('variable.*sin(10*pi*variable)+2.0',[-1,2]);   %画出函数曲 ...

  2. matlab bs2rv.m,matlab遗传算法工具箱gatbx(直接可用版)

    [实例简介] 这是最好用的matlab遗传算法工具箱:gatbx,工具箱可直接使用.安装说明及使用简介可见<matlab遗传算法gatbx工具箱介绍及安装说明>一文. [实例截图] [核心 ...

  3. matlab——智能算法之粒子群优化算法、模拟退火算法、遗传算法

    智能算法之粒子群优化算法: %% 初始化种群 f= % 函数表达式 % 求这个函数的最大值 figure(1);ezplot(f,[0,0.01,20]); N = 50; % 初始种群个数 d = ...

  4. 《MATLAB智能算法30个案例》:第1章 谢菲尔德大学的MATLAB遗传算法工具箱

    <MATLAB智能算法30个案例>:第1章 谢菲尔德大学的MATLAB遗传算法工具箱 1. 前言 2. MATLAB 仿真示例一 3. MATLAB 仿真示例二 4. 小结 1. 前言 & ...

  5. 《MATLAB智能算法30个案例》:第4章 基于遗传算法的TSP算法

    <MATLAB智能算法30个案例>:第4章 基于遗传算法的TSP算法 1. 前言 2. MATLAB 仿真示例 3. 小结 1. 前言 <MATLAB智能算法30个案例分析>是 ...

  6. 《MATLAB智能算法30个案例》:第2章 基于遗传算法和非线性规划的函数寻优算法

    <MATLAB智能算法30个案例>:第2章 基于遗传算法和非线性规划的函数寻优算法 1. 前言 2. MATLAB 仿真示例一 3. MATLAB 仿真示例二 4. MATLAB 仿真示例 ...

  7. 《MATLAB智能算法30个案例》:第8章 基于量子遗传算法的函数寻优算法

    <MATLAB智能算法30个案例>:第8章 基于量子遗传算法的函数寻优算法 1. 前言 2. MATLAB 仿真示例 3. 小结 1. 前言 <MATLAB智能算法30个案例分析&g ...

  8. 《MATLAB智能算法30个案例》:第9章 基于遗传算法的多目标优化算法

    <MATLAB智能算法30个案例>:第9章 基于遗传算法的多目标优化算法 1. 前言 2. MATLAB 仿真示例 3. 小结 1. 前言 <MATLAB智能算法30个案例分析> ...

  9. 《MATLAB智能算法30个案例》:第20章 基于遗传模拟退火算法的聚类算法

    <MATLAB智能算法30个案例>:第20章 基于遗传模拟退火算法的聚类算法 1. 前言 2. MATLAB 仿真示例 3. 小结 1. 前言 <MATLAB智能算法30个案例分析& ...

最新文章

  1. 【C++】C++11 STL算法(二):修改序列的操作(Modifying sequence operations)
  2. Pandas基础复习-DataFrame
  3. PMCAFF | 11张图告诉你产品经理的正确思维方式
  4. Webstorm出现NoGitBinary时配置Git环境变量
  5. MATLAB 的运算符
  6. 超赞!这些 “电子” 艺术品,真绝了!
  7. GhostNet论文
  8. Solidworks教程:利用Simulation模拟热传导
  9. 武汉年会签到,抽奖,摇一摇,微信上墙,互动大屏
  10. thinkphp5实战系列(二)前台模板的引入
  11. python两张图片无缝合成一张,Python实现拼接多张图片的方法
  12. Android不同手机屏幕分辨率自适应
  13. mysql memos
  14. N诺刷题——字符串、排序、查找、链表
  15. HTML基础(四)常用的内联元素
  16. STM32F411RE项目开发-1-点亮LD2小灯
  17. 1421. 净现值查询
  18. 程序员为何痴迷深夜写代码?
  19. bootstrap checks failed [1]: the default discovery settings are unsuitable for production use; at l
  20. Clion 打包exe无法运行 且 cmd窗口中文乱码

热门文章

  1. interrupt 1 using 1
  2. 软件测试——JUnit中的参数化测试
  3. 共轭矩阵与自共轭矩阵
  4. 如何查看Python安装目录
  5. linux下配置多网卡或多IP的方法
  6. windows 下怎样利用NET-SNMP 发送和接收trap
  7. RDP(远程桌面)优化
  8. 仿写strncmp函数
  9. html下拉框只读,HTML元素(如select下拉框)设置为只读
  10. ios 摇一摇不走响应方法_猫咪不和主人亲近?这几种方法让它变得黏人,赶都赶不走|猫|宠物猫|主人...