4

All the scipy examples are using the older versions and I'm looking for an example on how to use the newer version.

https://docs.scipy.org/doc/scipy/reference/optimize.linprog-simplex.html

I created a super simple code and it prints that the problem is unbounded. Any help is appreciated.

Minimize x - y

with
x >= 2
y <= 3

In standard form
-x - s1 = -2
y + s2 = 3

One of many solutions should be x = 2, y = 3

c = [1, -1]
A = [[-1, 0, -1, 0],[0, 1, 0, 1]]
b = [-2, 3]
linprog(c, method="simplex", options={'A': A, 'b': b})

result
------------------
     con: array([], dtype=float64)
     fun: -inf
 message: 'The problem is (trivially) unbounded because there are no non-trivial constraints and a) at least one decision variable is unbounded above and its corresponding cost is negative, or b) at least one decision variable is unbounded below and its corresponding cost is positive. '
     nit: 0
   slack: array([], dtype=float64)
  status: 3
 success: False
       x: array([ 0., inf])
Thirlan
  • 712
  • 1
  • 8
  • 25

1 Answers1

3

scipy.optimize.linprog(c, method='simplex', callback=None, options={'c0': None, 'A': None, 'b': None, 'postsolve_args': None, 'maxiter': 1000, 'tol': 1e-09, 'disp': False, 'bland': False}, x0=None)

Minimize a linear objective function subject to linear equality and non-negativity constraints

It means that you can't solve your problem while using this interface. Read the Note in the documentation.

You should use top level linprog module instead. As you do not have equality and inequality constraints, your A_ub, b_ub, A_eq and b_eq matrices are None and you only need to define bounds.

c = [1, -1]
x_bounds = [2, None]
y_bounds = [None, 3]
res = linprog(c, bounds=[x_bounds, y_bounds], method='simplex')

res
Out[]: 
     fun: -1.0
 message: 'Optimization terminated successfully.'
     nit: 2
   slack: array([0., 0., 0., 0.])
  status: 0
 success: True
       x: array([ 2., 3.])
igrinis
  • 9,040
  • 10
  • 29