matlab一般求解方式
示例
可以看出
即
即
但是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乘子),把非线性的约束放到目标函数里去,变成一个无约束的极值问题
被称为增广目标函数,原优化问题被转化为以为目标函数的无约束极值问题
两个问题的最优解一样
非常聪明的方式
结果
>> 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);
来源:https://blog.csdn.net/qq_36607894/article/details/100107267