【OR】Matlab求解最优化问题(2) 非线性优化

实验环境

Matlab 2018b.

非线性优化

非线性优化算法主要有梯度类算法和牛顿法两大类,包括DFP方法BFGS方法,约束变尺度(SQP)算法(Han, Powell)和Lagrange乘子法(Powell,Hestenes),随着计算机技术的发展,80年代出现了信赖域算法,稀疏拟牛顿法,大规模问题求解算法和并行计算算法,90年代出现了内点法和有限存储算法,目前免费的非线性求解软件包括LANCELOTMINPACTENMINSNOPT等.

无约束非线性优化

无约束优化的一般形式为
min ⁡ f ( x ) , x ∈ R n \min f(x), x\in\mathbb{R}^n minf(x),xRn
其中 f f f为非线性函数.
一个典型的非线性函数banana function
f ( x ) = 100 × ( x 2 − x 1 2 ) 2 − ( 1 − x 1 ) 2 f(x)=100\times(x_2-x_1^2)^2-(1-x_1)^2 f(x)=100×(x2x12)2(1x1)2
函数图像绘制如下
banana

绘图代码

%% Banana function
axis off;
x=-2:0.2:2;
y=-1:0.2:3;
[xx, yy]=meshgrid(x, y); % 网格化操作
zz=100*(yy-xx.^2).^2+(1-xx).^2;
% 绘制三维图
surfc(xx, yy, zz);

约束非线性优化

约束非线性优化的一般形式为
min ⁡ f ( x ) s . t . { g i ( x ) ≤ 0 , i = 1 , 2 , … , m h j ( x ) = 0 , j = 1 , 2 , … , l \min f(x)\\ s.t.\begin{cases} g_i(x)\leq 0, i=1, 2, \dots, m\\ h_j(x)=0, j=1,2 ,\dots, l \end{cases} minf(x)s.t.{gi(x)0,i=1,2,,mhj(x)=0,j=1,2,,l

Matlab求解函数

fminunc(无约束)

优化计算中需要使用函数的导数,如果用户没有提供目标函数的导数,那么matlab会采用差分方法计算函数的导数值.
函数语法:

x=fminunc(fun, x0)
x=fminunc(fun, x0, options)
[x, fval, exitflag, output, grad, hessian]=fminunc(...)

输出信息中
grad表示最优点/迭代最终点的导数
hessian表示最优点/迭代最终点的二阶导数
求解代码

clear all;
clc;
close all;
Bana_Func=@(x)(100*(x(2)-x(1)^2)^2+(1-x(1))^2);

options=optimset('display', 'iter');
x=[-1.9, 2];
[x, fval, exitflag, output]=fminunc(Bana_Func, x, options)

提供导数信息计算
目标函数及导函数

function [f, g]=BanaFuncGrad(x)
f=100*(x(2)-x(1)^2)^2+(1-x(2))^2;
g=[100*(4*x(1)^3-4*x(1)*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1)^2)];

求解函数

options=optimset('HessUpdate', 'bfgs', 'gradobj', 'on', 'display', 'iter');
x=[-1.9, 2];
[x, fval, exitflag, output]=fminunc(@BanaFuncGrad, x, options)

从求解信息中可以发现,提供目标函数的导函数信息会减少迭代次数.

fminsearch

fminsearch函数求解算法是可变多面体算法(Neldero-Mead Simplex)
函数语法

x=fminsearch(fun, x0)
x=fminsearch(fun, x0, options)
[x, fval, exitflag, output]=fminsearch(...)

目标函数文件BanaFunc.m

function f=BanaFunc(x)
f=100*(x(2)-x(1)^2)^2+(1-x(1))^2

求解函数

options=optimset('display', 'iter');
x=[-1.9, 2];
[x, fval, exitflag, output]=fminsearch(@BanaFunc, x, options)

fmincon

fmincon是matlab主要的内置约束最优化求解函数,该函数的求解的优化问题的主要形式为
min ⁡ f ( x ) s . t . { c ( x ) ≤ 0 c e q ( x ) = 0 A ⋅ x ≤ b A e q ⋅ x = b e q l b ≤ x ≤ u b \min f(x)\\ s.t. \begin{cases} c(x)\leq 0\\ ceq(x)=0\\ A\cdot x\leq b\\ Aeq\cdot x=beq\\ lb\leq x\leq ub \end{cases} minf(x)s.t.c(x)0ceq(x)=0AxbAeqx=beqlbxub
对于中等约束优化问题,使用序列二次优化(SQP)算法,对于大规模约束优化使用基于内点反射牛顿法的信赖域算法,对于大规模线性系统使用共轭梯度算法(PCG).
例 1:求解优化问题
min ⁡ f ( x ) = x 1 2 + x 2 2 + x 3 2 s . t . { − x 1 − x 2 − x 3 + 72 ≤ 0 x i ≥ 0 \min f(x)=x_1^2+x_2^2+x_3^2\\ s.t. \begin{cases} -x_1-x_2-x_3+72\leq 0\\ x_i\geq 0 \end{cases} minf(x)=x12+x22+x32s.t.{x1x2x3+720xi0
函数文件func_1.m

function f = func_1(x)
f=x(1)^2+x(2)^2+x(3)^2;

约束文件con_1.m

function [c, ceq]=con_1(x)
c=72-x(1)-x(2)-x(3); % 不等式约束
ceq=[]; % 等式约束

求解函数

%% 非线性约束优化
options=optimset('display', 'iter');
x0=[10, 10, 10]; % 起始迭代点
lb=[0, 0, 0];
[x, fval, exitflag, output]=fmincon(@func_1, x0, [], [], [], [], lb, [], @con_1, options)

求解结果
ans

大规模优化问题

测试大规模优化问题
min ⁡ f = ∑ i = 1 100 [ x ( i ) − 1 i ] 2 \min f=\sum_{i=1}^{100}[x(i)-\frac{1}{i}]^2 minf=i=1100[x(i)i1]2
目标函数文件func_2.m

function f=func_2(x)
v=x-1./(1:100);
f=v*v';

求解函数

%% 大规模优化
x0=1*ones(1,100);
options=optimset('LargeScale', 'on', 'display', 'iter', 'TolFun', 1e-8);
[x, fval, exitflag, output]=fminunc(@func_2, x0, options)

含参数优化

函数为
min ⁡ x f ( x , a ) = a 1 sin ⁡ ( x 1 ) + a 2 x 2 2 \min_x f(x, a)=a_1\sin(x_1)+a_2x_2^2 xminf(x,a)=a1sin(x1)+a2x22
参数函数文件为func_3.m

function f=func_3(x, a)
f=a(1)*sin(x(1))+a(2)*x(2)^2;

求解函数

%% 参数优化
a=[1, 1];
x0=[0, 0];
[x, fval, exitflag, output]=fminsearch(@(x)func_3(x, a), x0)

参考资料

金融数量分析 北京航天航空大学出版社 郑志勇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Quant0xff

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值