防灾数学分享 http://blog.sciencenet.cn/u/fzmath 防灾科技学院数学教研室

博文

MATLAB优化工具箱遗传算法函数ga()用法示例

已有 24158 次阅读 2017-9-11 22:09 |系统分类:教学心得

用法格式

X = ga(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON,options) minimizes

   with the default optimization parameters replaced by values in OPTIONS.

   OPTIONS can be created with the OPTIMOPTIONS function. See OPTIMOPTIONS

   for details. For a list of options accepted by ga refer to the

   documentation.

X = ga(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a structure

   that has the following fields:

       fitnessfcn: <Fitness function>

            nvars: <Number of design variables>

            Aineq: <A matrix for inequality constraints>

            bineq: <b vector for inequality constraints>

              Aeq: <Aeq matrix for equality constraints>

              beq: <beq vector for equality constraints>

               lb: <Lower bound on X>

               ub: <Upper bound on X>

          nonlcon: <Nonlinear constraint function>

           intcon: <Index vector for integer variables>

          options: <Options created with optimoptions('ga',...)>

         rngstate: <State of the random number generator>

   

先看只有自变量区间约束的例子。

例1 一元函数 $f(x)=x\sin 10\pi x+2,x\in(-1,2)$ ,求 $f(x)$ 最大值

fun1 = @(x)x.*sin(10*pi*x)+2;ezplot(fun1,[-1,2]])

>> f1= @(x)(-x*sin(10*pi*x)-2);[x,fval] = ga(f1,1,[],[],[],[],-1,2)

Optimization terminated: average change in the fitness value less than options.FunctionTolerance.


x =    1.4507

fval =   -3.4503

当 x =  1.4507,f(x)取最大值 fmax = 3.4503.


例2 二次规划:已知

              $p_1=0.5; p_2=6.0; y = p_1*x_1^2 + x_2^2 -x_1*x_2 -2*x_1 - p2*x_2$

建立子函数

function y = lincontest6(x)

%LINCONTEST6 A quadratic objective function (from Optimization Toolbox)

p1=0.5;p2=6.0;y = p1*x(1)^2 + x(2)^2 -x(1)*x(2) -2*x(1) - p2*x(2);

%Derivative of the function above

% dy = [(2*p1*x(1) - x(2) -2);(2*x(2) -x(1) -p2)];

主函数

A = [1 1; -1 2; 2 1];  b = [2; 2; 3];  lb = zeros(2,1);

    % Use mutation function which can handle constraints

    options = optimoptions('ga','MutationFcn',@mutationadaptfeasible);

    [x,fval,exitflag] = ga(@lincontest6,2,A,b,[],[],lb,[],[],options)

Optimization terminated: maximum number of generations exceeded.

x =

   0.6673    1.3336

fval =

  -8.2253

exitflag =

    0

例 3 约束规划:

目标函数 $f(x_1,x_2)=\text{e}^{x_1}(4x_1^2+2x_2^2+4x_1x_2+2x_2+a_1)$

约束函数 $1.5+x_1x_2-x_1-x_2\leqslant 0\\ -x_1x_2-a_2\leqslant 0$

当 $a_1=1,a_2=10.$

目标函数:myfit=@(x,a1)exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + a1);

约束条件:

        function [c,ceq] = myconstr(x,a2)

        c = [1.5 + x(1)*x(2) - x(1) - x(2);

              -x(1)*x(2) - a2];

        % No nonlinear equality constraints:

         ceq = [];

测试主程序:

     a1 = 1; a2 = 10; % define parameters first

     % Mutation function for constrained minimization

     options = optimoptions('ga','MutationFcn',@mutationadaptfeasible);

     [x,fval] = ga(@(x)myfit(x,a1),2,[],[],[],[],[],[],@(x)myconstr(x,a2),options)

运行结果

Optimization terminated: average change in the fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.

x =

  -5.1427    1.9447

fval =

   0.4571

注意多次运行结果不相同,主要是ga默认参数不合适。


例4 混合整数规划:设 $f(x_1,x_2,x_3)=(x_1-0.2)^2+(x_2-1.7)^2+(x_3-5.1)^2$ ,其中 $x_2,x_3$ 为整数,求最小值

>>fun = @(x) (x(1) - 0.2)^2 + (x(2) - 1.7)^2 + (x(3) -5.1)^2;

[x,fval] = ga(fun,3,[],[],[],[],[],[],[],[2 3])

Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance

and constraint violation is less than options.ConstraintTolerance.

x =

   0.2000    2.0000    5.0000

fval =

   0.1000


例5 其他的算法:模拟退火算法

>> fun=@(x)sin(3*x(1)*x(2))+(x(1)-0.1)*(x(2)-1)+x(1)^2+x(2)^2;

x0 = rand(1,2); [x,fval] = fmincon(fun,x0,[],[],[],[],[-1,-3],[3,3])

[x,fval] = simulannealbnd(fun,rand(2,1),[-1;-3],[3;3])


Local minimum found that satisfies the constraints.


Optimization completed because the objective function is non-decreasing in

feasible directions, to within the default value of the optimality tolerance,

and constraints are satisfied to within the default value of the constraint tolerance.


<stopping criteria details>

x =

   0.8768   -0.5503

fval =

  -1.1251

Optimization terminated: change in best function value less than options.FunctionTolerance.

x =

   0.8752

  -0.5547

fval =

  -1.1250



options = optimoptions('ga','MutationFcn',@mutationadaptfeasible,'PopulationSize',200,'MaxGenerations',300);

[x,fval] = ga(@(x)myfit(x,a1),2,[],[],[],[],[],[],@(x)myconstr(x,a2),options)






https://blog.sciencenet.cn/blog-292361-1075475.html

上一篇:MATLAB中的两个常用数值优化函数fminsearch()和fminunc()
下一篇:用MATLAB绘制空间曲面曲面及其切平面
收藏 IP: 124.238.133.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-23 23:24

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部