对于标准的线性规划我们可以写成如下的形式,如果有不等式约束,则写出他的对偶问题:
我们以一个实例来看
解一下线性规划问题,通过linprog求解器和sedumi求解器分别求解。注意,使用sedumi求解前需要写出对偶形式,对对偶进行求解。
%%
clc
clear all
tic
b=[1 4]';
A=[-1 1 0 1 1;0 0 -1 1 2];
c=[0 2 0 3.5 6];
[x, y, info]=sedumi(A,b,c)
toc
time = toc
%%
tic
clear all
f = [-1;-4];
b = [0 2 0 3.5 6]';
A = [-1 1 0 1 1; 0 0 -1 1 2]';
options = optimoptions('linprog','Algorithm','dual-simplex','Display','iter','MaxIterations',20)
[x,fval,exitflag,output,lambda] = linprog(f,A,b,[],[],[],[],options)
toc
time = toc
我们对求解器运行时间也做了计时,进行对比:
SeDuMi 1.3 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 2, order n = 6, dim = 6, blocks = 1
nnz(A) = 7 + 0, nnz(ADA) = 4, nnz(L) = 3
it : b*y gap delta rate t/tP* t/tD* feas cg cg prec
0 : 4.58E+01 0.000
1 : 8.27E+00 1.37E+01 0.000 0.2992 0.9000 0.9000 1.99 1 1 1.3E+00
2 : 1.15E+01 3.16E+00 0.000 0.2304 0.9000 0.9000 1.81 1 1 2.4E-01
3 : 1.19E+01 5.77E-01 0.000 0.1826 0.9000 0.9000 1.19 1 1 4.1E-02
4 : 1.20E+01 2.97E-03 0.000 0.0051 0.9990 0.9990 1.01 1 1
iter seconds digits c*x b*y
4 0.2 Inf 1.2000000000e+01 1.2000000000e+01
|Ax-b| = 0.0e+00, [Ay-c]_+ = 0.0E+00, |x|= 2.2e+00, |y|= 3.0e+00
Detailed timing (sec)
Pre IPM Post
6.900E-02 1.440E-01 1.700E-02
Max-norms: ||b||=4, ||c|| = 6,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.80888.
x =
(1,1) 1
(5,1) 2
y =
0
3
info =
struct with fields:
iter: 4
feasratio: 1
pinf: 0
dinf: 0
numerr: 0
timing: [0.0690 0.1440 0.0170]
wallsec: 0.2300
cpusec: 0.2500
Elapsed time is 0.250347 seconds.
time =
0.2504
options =
linprog options:
Options used by current Algorithm ('dual-simplex'):
(Other available algorithms: 'interior-point', 'interior-point-legacy')
Set properties:
Algorithm: 'dual-simplex'
Display: 'iter'
MaxIterations: 20
Default properties:
ConstraintTolerance: 1.0000e-04
MaxTime: Inf
OptimalityTolerance: 1.0000e-07
LP preprocessing removed 1 inequalities, 0 equalities,
0 variables, and 1 non-zero elements.
Iter Time Fval Primal Infeas Dual Infeas
0 0.002 0.000000e+00 0.000000e+00 2.672696e+00
2 0.002 -1.200000e+01 0.000000e+00 0.000000e+00
Optimal solution found.
x =
0
3
fval =
-12
exitflag =
1
output =
struct with fields:
iterations: 2
constrviolation: 0
message: 'Optimal solution found.'
algorithm: 'dual-simplex'
firstorderopt: 0
lambda =
struct with fields:
lower: [2×1 double]
upper: [2×1 double]
eqlin: []
ineqlin: [5×1 double]
Elapsed time is 0.992132 seconds.
time =
0.9922
linprog之前博客已经解释过,我们现在对sedumi做一次解释,因为对偶问题是最大化问题,这里求解信息中第一个b*y值可以看到在不断增大,在关注一个feasibility值,表示结果置信度,最后为0.999。gap是对偶间隙,这个对偶间隙要求越小越好,也可以看到,最后gap也缩小到e-3了。sedumi中还给出了计算时间,包括预处理时间,处理时间,后处理时间,这个求解过程占用时间都分别给出来了。
我们通过tic toc计时,发现sedumi求解速度比linprog快速很多。