Linear programming at scipy.optimize.linprog

I just checked a simple linear programming task with scipy.optimize.linprog:

1*x[1] + 2x[2] -> max 1*x[1] + 0*x[2] <= 5 0*x[1] + 1*x[2] <= 5 1*x[1] + 0*x[2] >= 1 0*x[1] + 1*x[2] >= 1 1*x[1] + 1*x[2] <= 6 

And I got a very strange result, I expected x [1] to be 1, and x [2] to be 5, but:

 >>> print optimize.linprog([1, 2], A_ub=[[1, 1]], b_ub=[6], bounds=(1, 5), method='simplex') status: 0 slack: array([ 4., 4., 4., 0., 0.]) success: True fun: 3.0 x: array([ 1., 1.]) message: 'Optimization terminated successfully.' nit: 2 

Can someone explain why I got this strange result?

+5
source share
1 answer

optimize.linprog always minimizes your target function. If you want to maximize instead, you can use this max(f(x)) == -min(-f(x))

 from scipy import optimize optimize.linprog( c = [-1, -2], A_ub=[[1, 1]], b_ub=[6], bounds=(1, 5), method='simplex' ) 

This will give you the expected result with a value of -f(x) = -11.0

  slack: array([ 0., 4., 0., 4., 0.]) message: 'Optimization terminated successfully.' nit: 3 x: array([ 1., 5.]) status: 0 success: True fun: -11.0 
+12
source

All Articles