智能算法之Matlab实现(1)——遗传算法(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\gatbx'] 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 < MAXGEN
FitnV = ranking(ObjV);%allocate the adaptness
SelCh = select('sus',Chrom,FitnV,GGAP);%select
SelCh = recombin('xovsp',SelCh,px);%recombine
SelCh = mut(SelCh, pm);%mutate
X = bs2rv(SelCh,FieldD);%to dec
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 < MAXGEN
FitnV = ranking(-ObjV);%allocate the adaptness
SelCh = select('sus',Chrom,FitnV,GGAP);%select
SelCh = recombin('xovsp',SelCh,px);%recombine
SelCh = mut(SelCh, pm);%mutate
XY = bs2rv(SelCh,FieldD);%bin to 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实现(1)——遗传算法(1)的更多相关文章
- 【智能算法】超详细的遗传算法(Genetic Algorithm)解析和TSP求解代码详解
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 文章声明 此文章部分资料和代码整合自网上,来源太多已经无法查明出处,如侵犯您的权利,请联系我删除. 00 目录 遗传算法定义 生 ...
- 群智能优化算法-测试函数matlab源码
群智能优化算法测试函数matlab源代码 global M; creatematrix(2); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %画ackley图. %%%% ...
- KFCM算法的matlab程序(用FCM初始化聚类中心)
KFCM算法的matlab程序(用FCM初始化聚类中心) 在“聚类——KFCM”这篇文章中已经介绍了KFCM算法,现在用matlab程序对iris数据库进行实现,用FCM初始化聚类中心,并求其准确度与 ...
- 【智能算法】用模拟退火(SA, Simulated Annealing)算法解决旅行商问题 (TSP, Traveling Salesman Problem)
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 文章声明 此文章部分资料和代码整合自网上,来源太多已经无法查明出处,如侵犯您的权利,请联系我删除. 01 什么是旅行商问题(TS ...
- 算法(二)之遗传算法(SGA)
算法(二)之遗传算法(SGA) 遗传算法(Genetic Algorithm)又叫基因进化算法或进化算法,是模拟达尔文的遗传选择和自然淘汰的生物进化过程的计算模型,属于启发式搜索算法一种. 下面通过下 ...
- GMM算法的matlab程序
GMM算法的matlab程序 在“GMM算法的matlab程序(初步)”这篇文章中已经用matlab程序对iris数据库进行简单的实现,下面的程序最终的目的是求准确度. 作者:凯鲁嘎吉 - 博客园 h ...
- GMM算法的matlab程序(初步)
GMM算法的matlab程序 在https://www.cnblogs.com/kailugaji/p/9648508.html文章中已经介绍了GMM算法,现在用matlab程序实现它. 作者:凯鲁嘎 ...
- KFCM算法的matlab程序
KFCM算法的matlab程序 在“聚类——KFCM”这篇文章中已经介绍了KFCM算法,现在用matlab程序对iris数据库进行简单的实现,并求其准确度. 作者:凯鲁嘎吉 - 博客园 http:// ...
- FCM算法的matlab程序2
FCM算法的matlab程序2 在“FCM算法的matlab程序”这篇文章中已经用matlab程序对iris数据库进行实现,并求解准确度.下面的程序是另一种方法,是最常用的方法:先初始化聚类中心,在进 ...
随机推荐
- LINUX对超级用户和普通用户的理解
什么是超级用户 在所有Linux系统中,系统都是通过UID来区分用户权限级别的,而UID为0的用户被系统约定为是具有超级权限.超级用户具有在系统约定的最高权限满园内操作,所以说超级用户可以完成系统管理 ...
- https://vjudge.net/problem/2198221/origin
https://vjudge.net/problem/2198221/origin逆向思维,原题是人出来,我们处理成人进去,算出来每个人的曼哈顿距离,然后从大到小排序,距离长的先入.走的距离+这个人从 ...
- Liferay 7:Liferay DXP解决方案
分享是美德,欢迎探讨技术 这个作者很厉害呀,写的博客都是解决很刁钻问题的.强烈推荐 http://www.liferaysolution.com/2017/06/captcha-recaptcha-w ...
- IDEA工具实现反编译操作
1.File - Settings... 2.在搜索框中输入“byte” - 勾选 Java Byte code Decompiler选项 点击 OK 键 3.弹出重启IDEA的选择框 选择“rest ...
- 洛谷P4022 熟悉的文章
题意:给定一个串集合s,每次给定一个串t,询问一个最大的L,使得存在一种划分能把t划分成若干个子串, 其中好的子串总长不小于0.9|t|.好的子串定义为长度不小于L且是s中某一个串的子串. 解:发现这 ...
- ubuntn16.04指令
基础知识: ubuntn中的/表示根目录,包括bin,mnt等文件夹 /home表示家目录,/home/user表示用户下的家目录,/root表示root目录 常用指令: 进入root : sudo ...
- Eclipse 出现select type (? = any character,*= any String,Tz=TimeZone)
在eclipse中想运行project的时候,往往是右键项目名称---->run As --->Java Application 但是会弹出窗口显示select type (? = any ...
- angular4 路由重用策略 RouterReuseStrategy
单页面应用现在是主流,随之而来的缺点:页面间切换时不能保存状态 angular4出了一个RouteReuseStrategy路由重用策略可以让组件所有的state和渲染好的html存起来,然后在切回去 ...
- Web前端开发工程师需要掌握哪些核心技能?
Web前端开发所涉及的内容主要包括W3C标准中的结构.行为和表现,那么这三项中我们需要掌握的核心技能是什么呢? 1.开发语言 HTML发展历史有二十多年,历经多次版本更新,HTML5和CSS3的出现又 ...
- 【洛谷】P1567 统计天数
P1567 统计天数 题目背景 统计天数 题目描述 炎热的夏日,KC非常的不爽.他宁可忍受北极的寒冷,也不愿忍受厦门的夏天.最近,他开始研究天气的变化.他希望用研究的结果预测未来的天气. 经历千辛万苦 ...