
http://www.ma-xy.com
第一章 优化工具 YALMIP 简介 1.2 YALMIP 应用实例
1.2.2 解线性规划和二次规划
用 Yalmip 求解线性规划和二次规划。以线性回归问题为例,我们设定线性回归模型为
y = xw + ε
ˆy = xw
其中:x 是 n × p 矩阵。现在要求使下面的目标最小的 w
1. min
w
J
1
(w) = ||y − ˆy||
∞
= max
i
{|y
i
− ˆy
i
|}
2. min
w
J
2
(w) = ||y − ˆy||
1
=
n
i=1
|y
i
− ˆy
i
|
3. min
w
J
3
(w) = ||y − ˆy||
2
=
n
i=1
(y
i
− ˆy
i
)
2
= (y − ˆy)
T
(y − ˆy)
更一般的,我们可以设置目标为离差 y − ˆy 的 p 阶范数
min
w
||y − ˆy||
p
对上面的目标 1、2 和 3,目标 2 为线性规划问题,目标 3 为二次规划问题,并且这些优化
问题不存在约束条件。上述 3 个目标的 Yalmip 的程序为
1 %% 线 性 规 划&二 次 规 划−−−−线 性 回 归 示 例
2 w = [ 1 2 3 4 5 6 ] ’ ;
3 n = ( 0 : 0 . 0 2 : 2 * pi ) ’ ;
4 x = [ s i n (n) si n (2*n) si n (3*n) si n (4*n) si n (5*n) si n (6*n) ] ;
5 e = (−4+8*rand ( le ngth (n ) ,1) ) ;
6 e ( 1 00:115 ) = 3 0;
7 y = x*w+e ;
8 pl o t (n , y) ;
9 %第 一 种 方 法
10 what = sdpvar ( 6 , 1 ) ;
11 r e s i d u a l s = y−x*what ;
12 o ptimize ( [ ] , norm( r e s id u a l s , i n f ) ) ;
13 w_Linf = value (what ) ;
14 o ptimize ( [ ] , norm( r e s id u a l s , 1 ) ) ;
15 w_L1 = value (what ) ;
16 o ptimize ( [ ] , norm( r e s id u a l s , 2 ) ) ;
17 w_L2 = value (what ) ;
18 pl o t (n , [ y x*w_L1 x*w_L2 x*w_Linf ] ) ;
19 %第 二 种 方 法
20 bound = sdpvar ( leng th ( r e s i d u a l s ) ,1 ) ;
21 Co nst rai nts = [−bound <= r e s i d u a l s <= bound ] ;
22 o ptimize ( Constraints , sum(bound) ) ;
23 w_L1 = value (what ) ;
24 %
25 o ptimize ( [ ] , r e si d u a l s ’ * r e s i d u a l s ) ;
26 w_L2 = value (what ) ;
27 %
28 bound = sdpvar ( 1 , 1 ) ;
29 Co nst rai nts = [−bound <= r e s i d u a l s <= bound ] ;
http://www.ma-xy.com 3 http://www.ma-xy.com