在MATLAB中,遗传算法GA工具箱可以求解带约束的非线性多变量函数(Constrained nonlinear multivariable function)的最小值,即可以用来求解非线性规划问题。今天主要是以实际的例子详细讲解遗传算法GA工具箱求解非线性规划。
MATLAB中,非线性规划模型的写法如下:
基本语法
Bash
[x,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
Bash
x的返回值是决策向量x的取值,fval的返回值是目标函数f(x)的取值
fun是用M文件定义的函数f(x),代表了(非)线性目标函数。
nvars表示变量个数。
A,b,Aeq,beq定义了线性约束 ,如果没有线性约束,则A=[],b=[],Aeq=[],beq=[]。
lb和ub是变量x的下界和上界,如果下界和上界没有约束,则lb=[],ub=[],也可以
写成lb的各分量都为 -inf,ub的各分量都为inf。
nonlcon是用M文件定义的非线性向量函数约束,没有的话可以设置为[]。
options定义了优化参数,不填写表示使用Matlab默认的参数设置。
实例1
程序
Bash
clc;
clear all;
close all;
% 适应度函数
fun = @(x) -20*exp(-0.2*sqrt((x(1)^2+x(2)^2)/2))-exp((cos(2*pi*x(1))+cos(2*pi*x(2)))/2)+22.71289;
%绘制三维图形
x01 = -10:0.01:10;
x02 = -10:0.01:10;
[x01,x02] = meshgrid(x01,x02);
fz = -20*exp(-0.2.*sqrt((x01.^2+x02.^2)/2))-exp((cos(2*pi.*x01)+cos(2*pi.*x02))./2)+22.71289;
figure;
mesh(x01,x02,fz);%网格曲面图下
xlabel('x');
ylabel('y');
zlabel('f');
grid on;
figure;
meshc(x01,x02,fz);%网格曲面图下的等高线图
xlabel('x');
ylabel('y');
zlabel('f');
grid on;
nvars = 2;
%在做约束条件为线性的模型时,参数nonlcon直接传入空矩阵即可,代表不使用。
A = [];%线性不等式约束系数矩阵
b = [];%线性不等式约束增广矩阵
Aeq = [];%线性等式约束系数矩阵
beq = [];%线性等式约束增广矩阵
lb = [];%变量下界
ub = [];%变量上界
nonlcon = [];%非线性约束条件
options = [];%参数设置
%[x_best,fval]=ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options);
[x1,fval1] = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
% 设置允许误差 绘制求解过程图形 变异率,交叉率分别为0.05, 0.5 种群数量 最大迭代次数
options1 = optimoptions('ga','ConstraintTolerance',1e-6,'PlotFcn', @gaplotbestf,'MutationFcn', {@mutationuniform, 0.05},'CrossoverFcn', {@crossoverintermediate, 0.5},'PopulationSize', 300, 'Generations', 800,'Display','iter');
[x2,fval2] = ga(fun,nvars ,A,b,Aeq,beq,lb,ub,nonlcon,options1)
文本结果
Bash
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x1 =
0.0469 0.0362
fval1 =
0.2535
Best Mean Stall
Generation Func-count f(x) f(x) Generations
1 600 2.588 11.25 0
2 885 0.06103 7.647 0
3 1170 0.06103 5.595 1
4 1455 0.06103 4.116 2
5 1740 0.06103 2.763 3
6 2025 0.01783 1.94 0
7 2310 0.004489 1.289 0
8 2595 -0.001242 0.8767 0
9 2880 -0.003537 0.5818 0
10 3165 -0.003835 0.366 0
11 3450 -0.005023 0.368 0
12 3735 -0.005023 0.3274 1
13 4020 -0.005232 0.3456 0
14 4305 -0.005271 0.4032 0
15 4590 -0.005332 0.3936 0
16 4875 -0.005355 0.3366 0
17 5160 -0.005377 0.3325 0
18 5445 -0.005386 0.436 0
19 5730 -0.005387 0.4951 0
20 6015 -0.00539 0.4731 0
21 6300 -0.005391 0.4269 0
22 6585 -0.005392 0.3854 0
23 6870 -0.005392 0.3869 0
24 7155 -0.005392 0.5266 0
25 7440 -0.005392 0.5939 0
26 7725 -0.005392 0.2434 0
27 8010 -0.005392 0.4383 0
28 8295 -0.005392 0.4326 0
29 8580 -0.005392 0.2679 0
30 8865 -0.005392 0.3528 0
Best Mean Stall
Generation Func-count f(x) f(x) Generations
31 9150 -0.005392 0.2848 0
32 9435 -0.005392 0.358 0
33 9720 -0.005392 0.4661 0
34 10005 -0.005392 0.2923 0
35 10290 -0.005392 0.4091 0
36 10575 -0.005392 0.4092 0
37 10860 -0.005392 0.2371 1
38 11145 -0.005392 0.2454 0
39 11430 -0.005392 0.4219 0
40 11715 -0.005392 0.305 0
41 12000 -0.005392 0.313 0
42 12285 -0.005392 0.3297 1
43 12570 -0.005392 0.3402 2
44 12855 -0.005392 0.3861 0
45 13140 -0.005392 0.4246 1
46 13425 -0.005392 0.6954 2
47 13710 -0.005392 0.3886 3
48 13995 -0.005392 0.5573 4
49 14280 -0.005392 0.5237 0
50 14565 -0.005392 0.511 1
51 14850 -0.005392 0.4553 2
52 15135 -0.005392 0.4186 3
53 15420 -0.005392 0.2199 4
54 15705 -0.005392 0.3842 5
55 15990 -0.005392 0.3388 6
56 16275 -0.005392 0.2137 7
57 16560 -0.005392 0.1891 8
58 16845 -0.005392 0.2349 9
59 17130 -0.005392 0.1764 10
60 17415 -0.005392 0.4863 11
Best Mean Stall
Generation Func-count f(x) f(x) Generations
61 17700 -0.005392 0.5644 12
62 17985 -0.005392 0.5084 13
63 18270 -0.005392 0.5214 14
64 18555 -0.005392 0.483 15
65 18840 -0.005392 0.554 16
66 19125 -0.005392 0.5391 17
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x2 =
1.0e-16 *
0.4702 0.3736
fval2 =
-0.0054
>>
实例2
主程序
Bash
clc;
clear all;
close all;
Pop = 500;%遗传算法的种群数量
tol = 1e-6;%允许误差
p1 = 0.01;%变异率
p2 = 0.6;%交叉率
MAX = 200;%最大种群数量
%设置ga工具箱参数
options = optimoptions('ga','ConstraintTolerance',tol,'PlotFcn', @gaplotbestf,'MigrationFraction',p1,'CrossoverFraction', p2,'PopulationSize', Pop, 'Generations', MAX,'Display','iter');
nvars = 3;%变量个数
%在做约束条件为线性的模型时,参数nonlcon直接传入空矩阵即可,代表不使用。
A = [];%线性不等式约束系数矩阵
b = [];%线性不等式约束增广矩阵
Aeq = [];%线性等式约束系数矩阵
beq = [];%线性等式约束增广矩阵
lb = [0 0 0]';%变量下界
ub = [];%变量上界
nonlcon = @fun2;%非线性约束条件
[x,fval] = ga(@fun1,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
[x1,fval1,exitflag,output,population,scores]= ga(@fun1,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
fun1.m程序
Bash
function f=fun1(x);
%适应度函数
f=x(1).^2+x(2).^2+x(3).^2+8;
end
fun2.m程序
Bash
function [c,ceq]=fun2(x)
%入口参数 x为自变量的行向量
% c(x)<=0
% ceq(x) = 0;
c(1,1)=-x(1)^2+x(2)-x(3)^3;
c(2,1)=-x(1)+x(2)^2+x(3)^3-20;
ceq(1,1)=-x(1)-x(2)^2+2;
ceq(2,1)=x(2)+2*x(3)^2-3;
end
fun3.m程序
Bash
function [c,ceq]=fun3(x)
% c(x)<=0
% ceq(x) = 0;
c = [-x(1)^2+x(2)-x(3)^3;
-x(1)+x(2)^2+x(3)^3-20];
ceq=[-x(1)-x(2)^2+2;
x(2)+2*x(3)^2-3];
end
运行结果
Bash
Best Max Stall
Generation Func-count f(x) Constraint Generations
1 25285 12.6388 4.454e-09 0
2 55260 10.7279 0.006643 0
3 88085 10.7196 4.242e-05 0
4 183610 10.719 3.735e-05 0
5 230685 10.719 2.552e-06 0
6 255435 10.719 2.552e-06 1
7 343835 10.719 6.388e-08 0
Optimization terminated: average change in the fitness value less than options.FunctionTolerance
and constraint violation is less than options.ConstraintTolerance.
x =
0.6985 1.1408 0.9642
fval =
10.7190
Best Max Stall
Generation Func-count f(x) Constraint Generations
1 25290 13.2142 1.493e-11 0
2 50040 12.7929 0.004295 0
3 145565 11.953 0.005854 0
4 175540 11.9651 4.378e-05 0
5 271065 11.9552 4.936e-05 0
6 332390 11.9558 1.998e-06 0
7 357140 11.9558 1.998e-06 1
8 398515 11.9558 7.015e-07 0
Optimization terminated: average change in the fitness value less than options.FunctionTolerance
and constraint violation is less than options.ConstraintTolerance.
x1 =
1.5249 0.6893 1.0749
fval1 =
11.9558
exitflag =
1
output =
包含以下字段的 struct:
problemtype: 'nonlinearconstr'
rngstate: [1×1 struct]
generations: 8
funccount: 398515
message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance? and constraint violation is less than options.ConstraintTolerance.'
maxconstraint: 7.0154e-07
>>
参考内容
[1] 知乎作者xyh0626林深见鹿的文章《Matlab遗传算法工具箱的使用及实例(非线性规划)》,链接
https://zhuanlan.zhihu.com/p/419899419
[2] 知乎作者练先森的回答《MATLAB中遗传算法gaot工具箱用法?》,链接
https://www.zhihu.com/question/29912565/answer/141115881?utm_source=qq