matlab一般求解方式
示例
可以看出
1
2
x
T
H
x
=
2
x
1
2
−
4
x
1
x
2
+
4
x
2
2
\frac12x^THx=2x_1^2-4x_1x_2+4x_2^2
21xTHx=2x12−4x1x2+4x22
即
[
x
1
x
2
]
[
a
b
c
d
]
[
x
1
x
2
]
=
a
x
1
2
+
(
b
+
c
)
x
1
x
2
+
d
x
2
2
=
4
x
1
2
−
8
x
1
x
2
+
8
x
2
2
\left[ \begin{matrix} x_1 & x_2 \\ \end{matrix} \right] \left[ \begin{matrix} a&b \\ c&d \\ \end{matrix} \right] \left[ \begin{matrix} x_1 \\ x_2 \\ \end{matrix} \right]=ax_1^2+(b+c)x_1x_2+dx_2^2=4x_1^2-8x_1x_2+8x_2^2
[x1x2][acbd][x1x2]=ax12+(b+c)x1x2+dx22=4x12−8x1x2+8x22
即
{ a = 4 d = 8 b + c = − 8 \left\{ \begin{aligned} a=4\\ d=8\\ b+c=-8 \end{aligned} \right. ⎩⎪⎨⎪⎧a=4d=8b+c=−8
但是H必须是对称矩阵(二次型),所以b=c=-4
所以matlab代码
% 二次规划
clc
h=[4,-4;-4,8];
f=[-6;-3];
a=[1 1;4 1];
b=[3;9];
[x,value]=quadprog(h,f,a,b,[],[],zeros(2,1))
结果
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 =
1.9500
1.0500
value =
-11.0250
>>
外罚函数法
罚函数类似于拉格朗日对偶问题里面的松弛变量(KKT乘子),把非线性的约束放到目标函数里去,变成一个无约束的极值问题
P
(
x
,
M
)
P(x,M)
P(x,M)被称为增广目标函数,原优化问题被转化为以
P
(
x
,
M
)
P(x,M)
P(x,M)为目标函数的无约束极值问题
min
P
(
x
,
M
)
\min P(x,M)
minP(x,M)
两个问题的最优解一样
非常聪明的方式
结果
>> main
警告: Gradient must be provided for trust-region algorithm; using quasi-newton
algorithm instead.
> In fminunc (line 395)
In main (line 1)
Local minimum possible.
fminunc stopped because the size of the current step is less than
the default value of the step size tolerance.
<stopping criteria details>
Local minimum possible.
fminunc stopped because it cannot decrease the objective function
along the current search direction.
<stopping criteria details>
x =
1.2205
0.8829
y =
10.2692
% 主函数
[x,y]=fminunc('fa',rand(2,1))
% 外罚函数法求解二次规划
function f=fa(x);
M=50000;
g=x(1)^2+x(2)^2+8;
f=g-M*min(x(1),0)-M*min(x(2),0)-M*min(x(1).^2-x(2),0)+M*abs(-x(1)-x(2).^2+2);