简体   繁体   中英

Linear programming using scipy.optimize.linprog returns suboptimal solution

I'm trying to solve the following linear programming problem in Python 2.7 and for some reason, linprog is not returning the correct results.

Minimize: -x2 -x3

such that:

x0 + 0.33*x2 + 0.67*x3 = 0.5
x1 + 0.67*x2 + 0.33*x3 = 0.5
x0 + x1 + x2 + x3 = 1.0

Here's my code:

from scipy.optimize import linprog

a_eq = [[1.0, 0.0, 0.33, 0.67],
        [0.0, 1.0, 0.67, 0.33], 
        [1, 1, 1, 1]]

b_eq = [0.5, 0.5, 1.0]

c = [0, 0, -1.0, -1.0]

x = linprog(c=c, A_eq=a_eq, b_eq=b_eq)
print x

Here's the output of the above:

fun: -0.0
message: 'Optimization terminated successfully.'
nit: 4
slack: array([], dtype=float64)
status: 0
success: True
x: array([ 0.5,  0.5,  0. ,  0. ])

Clearly, the following solution is more optimal:

x: array([0.0, 0.0, 0.5, 0.5])

which makes the objective function value:

fun: -1.0

I did find some issues reported in github . Could this be what I'm facing or am I doing something wrong? Any help will be greatly appreciated! Thanks.

I did find some issues reported in github . Could this be what I'm facing...?

Exactly:

It turns out that A_eq in the problem is rank-deficient. After finding and removing rows that are a linear combination of others, linprog's solution agrees with the other.

The matrix a_eq is rank deficient. The last row is a linear combination of the first two rows. This makes the row redundant for the constraint so we can simply remove it and the corresponding entry in b_eq :

a_eq = [[1.0, 0.0, 0.33, 0.67],
        [0.0, 1.0, 0.67, 0.33]]

b_eq = [0.5, 0.5]

This results in the optimal solution x: array([ 0. , 0. , 0.5, 0.5]) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM