MATLAB非线性规划—简单入门fmincon函数传参数

MATLAB非线性规划—简单入门fmincon函数传参数

  • 一、标准型
    • 1.非线性规划标准型
    • 2.MATLAB非线性规划标准型
  • 二、算例
    • 1、目标函数
    • 2、上下界约束
    • 3、线性约束
    • 4、非线性约束
  • 三、目标函数与非线性约束**传参**
    • 1. 仅目标函数--传参
    • 2.目标函数与非线性约束--传参

一、标准型

1.非线性规划标准型

MATLAB非线性规划—简单入门fmincon函数传参数_第1张图片

2.MATLAB非线性规划标准型

x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)

{ f u n f ( x ) 目标函数 l b , u b l b ≤ x ≤ u b 上下界约束 A , b A ⋅ x ≤ b 线性不等式约束 A e q , b e q A e q ⋅ x = b e q 线性等式约束 n o n l c o n c ( x ) ≤ 0 , c e q ( x ) = 0 非线性等式约束 , 非线性不等式约束 x 0 初始最优解猜测值 x 返回最优解 o p t i o n s 求解器参数设置 \left\{\begin{array}{lll}{\rm fun}&f(x)& {目标函数}\\ {\rm lb,ub}&lb\leq x\leq ub & {上下界约束}\\ {\rm A,b}&A\cdot x\leq b&{线性不等式约束}\\ {\rm Aeq,beq}&Aeq\cdot x=beq&{线性等式约束}\\ {\rm nonlcon}&c(x)\leq0,ceq(x)=0&{非线性等式约束,非线性不等式约束}\\ {\rm x0} & {初始最优解猜测值}\\ {\rm x} & {返回最优解}\\ {\rm options} & {求解器参数设置} \end{array}\right. funlb,ubA,bAeq,beqnonlconx0xoptionsf(x)lbxubAxbAeqx=beqc(x)0,ceq(x)=0初始最优解猜测值返回最优解求解器参数设置目标函数上下界约束线性不等式约束线性等式约束非线性等式约束,非线性不等式约束

二、算例

      算例:
min ⁡ x f ( x ) = x 1 2 + x 2 2 [ − 2 − 3 ] ≤ [ x 1 x 2 ] ≤ [ 2 3 ] [ − 6 − 7 ] ≤ [ 3 1 1 2 ] [ x 1 x 2 ] ≤ [ 5 7 ] [ 1 1 3 1 ] [ x 1 x 2 ] = [ 8 7 ] x 1 2 + 2 x 1 x 2 ≤ 5 x 1 = x 2 2 \begin{array}{l} \mathop {\min }\limits_x f(x) = {x_1}^2 + {x_2}^2\\ \left[ {\begin{array}{l} { - 2}\\ { - 3} \end{array}} \right] \le \left[ {\begin{array}{l} {{x_1}}\\ {{x_2}} \end{array}} \right] \le \left[ {\begin{array}{l} 2\\ 3 \end{array}} \right]\\ \left[ {\begin{array}{l} { - 6}\\ { - 7} \end{array}} \right] \le \left[ {\begin{array}{l} 3&1\\ 1&2 \end{array}} \right]\left[ {\begin{array}{l} {{x_1}}\\ {{x_2}} \end{array}} \right] \le \left[ {\begin{array}{l} 5\\ 7 \end{array}} \right]\\ \left[ {\begin{array}{l} 1&1\\ 3&1 \end{array}} \right]\left[ {\begin{array}{l} {{x_1}}\\ {{x_2}} \end{array}} \right] = \left[ {\begin{array}{l} 8\\ 7 \end{array}} \right]\\ {x_1}^2 + 2{x_1}{x_2} \le 5\\ {x_1}={x_2}^2 \end{array} xminf(x)=x12+x22[23][x1x2][23][67][3112][x1x2][57][1311][x1x2]=[87]x12+2x1x25x1=x22

%初始猜测值
x0 = [-1,2];
%上下界约束
lb = [-2;-3];
ub = [2;3]
%线性不等式约束
A1 = [3 1;
      1 2];
A2 = -[3 1;
      1 2];
b1 = [5;7];
b2 = -[-6; -7];
A = [A1;A2]
b = [b1;b2];
Aeq = [1 1 ;
      3  1];
beq = [8;7];
%线性等式约束
Aeq = [1 1 ;
      3  1];
beq = [8;7];
%优化求解器选项配置
options = optimoptions('fmincon','Display','iter','Algorithm','sqp') %新手可将 options  = []

%优化求解器返回最优解
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,...
                      nonlcon,options)%返回优化后变量
                       
%目标函数
function f = fun(x)
f = p1*x(1)^2 + p2*x(2)^2;%目标函数值
end

%非线性约束
function [c,ceq] = nonlcon(x)
c= x(1)^2+2*x1*x2-5;%非线性不等式约束
ceq = x(1)-x(2)^2;%非线性等式约束
end

1、目标函数

a) 目标函数(不带梯度)

function [f] = fun(x)
f = x(1)^2 + x(2)^2;
end
变量 作用
x 待优化变量
f 返回目标函数在x处的值
b) 目标函数(带梯度)
function [f,gradf] = fun(x)
f = x(1)^2 + x(2)^2;
gradf = [2*x(1);2*x(2)];
end
变量 作用
x 待优化变量
f: 返回目标函数在x处的值
gradf 返回目标函数在x处的梯度

fmincon优化目标函数,梯度可带可不带,但是在(1)(2)情况下,最好带上:
( 1 ) 梯度容易求出 ( 2 ) 加快优化速度 (1)梯度容易求出(2)加快优化速度 (1)梯度容易求出(2)加快优化速度

2、上下界约束

[ − 2 − 3 ] ≤ [ x 1 x 2 ] ≤ [ 2 3 ] \left[ {\begin{array}{l} { - 2}\\ { - 3} \end{array}} \right] \le \left[ {\begin{array}{l} {{x_1}}\\ {{x_2}} \end{array}} \right] \le \left[ {\begin{array}{l} 2\\ 3 \end{array}} \right] [23][x1x2][23]
可知
l b = [ − 2 − 3 ] , u b = [ 2 3 ] lb = \left[ {\begin{array}{l} { - 2}\\ { - 3} \end{array}} \right],ub =\left[ {\begin{array}{l} { 2}\\ { 3} \end{array}} \right] lb=[23],ub=[23]

lb = [-2;-3];
ub = [2;3]

3、线性约束

A ⋅ x ≤ b [ − 6 − 7 ] ≤ [ 3 1 1 2 ] [ x 1 x 2 ] ≤ [ 5 7 ] A e q ⋅ x ≤ b e q [ 1 1 3 1 ] [ x 1 x 2 ] = [ 8 7 ] \begin{array}{ll}A\cdot x\le b &\left[\begin{array}{c}-6\\-7\end{array}\right]\leq\left[\begin{array}{cc}3&1\\1&2\end{array}\right]\left[\begin{array}{c}x_1\\x_2\end{array}\right]\leq\left[\begin{array}{c}5\\7\end{array}\right]\\ Aeq \cdot x\le beq &\left[\begin{array}{cc}1&1\\3&1\end{array}\right]\left[\begin{array}{c}x_1\\x_2\end{array}\right]=\left[\begin{array}{c}8\\7\end{array}\right]\end{array} AxbAeqxbeq[67][3112][x1x2][57][1311][x1x2]=[87]
可知
A 1 = [ 3 1 1 2 ] , b 1 = [ 5 7 ] A 2 = − [ 3 1 1 2 ] , b 2 = − [ − 6 − 7 ] A e q = [ 1 1 3 1 ] , b e q = [ 8 7 ] \begin{array}{l} A_1 = \left[ {\begin{array}{l} 3&1\\ 1&2 \end{array}} \right],b_1 = \left[ {\begin{array}{l} 5\\ 7 \end{array}} \right]\\ A_2 =- \left[ {\begin{array}{l} 3&1\\ 1&2 \end{array}} \right], b_2 =- \left[ {\begin{array}{l} -6\\ -7 \end{array}} \right] \\ Aeq =\left[ {\begin{array}{l} 1&1\\ 3&1 \end{array}} \right],beq = \left[ {\begin{array}{l} 8\\ 7 \end{array}} \right] \end{array} A1=[3112],b1=[57]A2=[3112],b2=[67]Aeq=[1311],beq=[87]

A1 = [3 1;
      1 2];
A2 = -[3 1;
      1 2];
b1 = [5;7];
b2 = -[-6; -7];
A = [A1;A2]
b = [b1;b2];
Aeq = [1 1 ;
      3  1];
beq = [8;7];

4、非线性约束

c ( x ) ≤ 0 x 1 2 + 2 x 1 x 2 ≤ 5 c e q ( x ) = 0 x 1 = x 2 2 \begin{array}{ll} c(x)\leq0&{x_1}^2 + 2{x_1}{x_2} \le 5\\ ceq(x)=0&{x_1}={x_2}^2 \end{array} c(x)0ceq(x)=0x12+2x1x25x1=x22
改写
x 1 2 + 2 x 1 x 2 − 5 ≤ 0 x 1 − x 2 2 = 0 \begin{array}{l} {x_1}^2 + 2{x_1}{x_2} -5\le 0\\ {x_1}-{x_2}^2=0 \end{array} x12+2x1x250x1x22=0

function [c,ceq] = nonlcon(x)
c= x(1)^2+2*x1*x2-5;
ceq = x(1)-x(2)^2;
end
变量 作用
x 待优化变量
c: 返回非线性不等式约束
ceq: 返回非线性等式约束

三、目标函数与非线性约束传参

      我们往往会遇到待优化函数或者非线性约束中带参数情况,有两种解决变法:

1. 仅目标函数–传参

f = p 1 x 1 2 + p 2 x 2 2 x 1 2 + 2 x 1 x 2 ≤ 5 \begin{array}{l} f = {p_1}{x_1}^2 + {p_2}{x_2}^2\\ {x_1}^2 + 2{x_1}{x_2} \le 5 \end{array} f=p1x12+p2x22x12+2x1x25

x0 = [-1,2];
p1 = 2; p2 =2;

%优化求解器选项配置
options = optimoptions('fmincon','Display','iter','Algorithm','sqp') 
%新手可将 options  = []

%优化求解器返回最优解
x = fmincon(@fun,x0,[],[],[],[],[],[],@nonlcon,options,p1,p2)%返回优化后变量

function [f,gradf] = fun(x,p1,p2)
f = p1*x(1)^2 + p2*x(2)^2;
gradf = [2*p1*x(1);2*p2*x(2)];
end

function [c,ceq] = nonlcon(x,p1,p2)
c = x(1)^2+2*x(1)*x(2)-5;
ceq = [];
end

2.目标函数与非线性约束–传参

f = p 1 x 1 2 + p 2 x 2 2 c 1 x 1 2 + c 2 x 1 x 2 ≤ 5 \begin{array}{l} f = {p_1}{x_1}^2 + {p_2}{x_2}^2\\ {c_1}{x_1}^2 + {c_2}{x_1}{x_2} \le 5 \end{array} f=p1x12+p2x22c1x12+c2x1x25

x0 = [-1,2];
p1 = 2;p2 =2;
c1 = 2;c2 =2;

%优化求解器选项配置
options = optimoptions('fmincon','Display','iter','Algorithm','sqp') 
%新手可将 options  = []

%优化求解器返回最优解
x = fmincon(@(x)fun(x,p1,p2),x0,[],[],[],[],[],[],...
                       @(x)nonlcon(x,c1,c2),options)%返回优化后变量
                       
function [f,gradf] = fun(x,p1,p2)
f = p1*x(1)^2 + p2*x(2)^2;
gradf = [2*p1*x(1);2*p2*x(2)];
end

function [c,ceq] = nonlcon(x,c1,c2)
c = c1*x(1)^2+c2*x(1)*x(2)-5;
ceq = [];
end

对于fmincon没有的项一定要用[]占位
options表示优化器的配置选项,如上例:

options = optimoptions('fmincon','Display','iter','Algorithm','sqp')
变量 作用
fmincon 优化求解函数
Display iter: 显示迭代过程
Algorithm sqp: 求解算法 SQP
… \dots 当前行未完,连接下一行
详情见: https://www.mathworks.com/help/optim/ug/fmincon.html

你可能感兴趣的:(MATLAB,算法,matlab)