0

I am reading the documentation of the Simplex Algorithm provided in the Scipy package of python, but the example shown in the last at this documentation page is solving a minimization problem. Whereas I want to do a maximization. How would you alter the parameters in order to perform a maximization if we can do maximization using this package?

user152468
  • 3,202
  • 6
  • 27
  • 57
Anonymous
  • 93
  • 2
  • 9

1 Answers1

3

Every maximization problem can be transformed into a minimization problem by multiplying the c-vector by -1: Say you have the 2-variable problem from the documentation, but want to maximize c=[-1,4]

from scipy.optimize import linprog
import numpy
c = numpy.array([-1, 4]) # your original c for maximization
c *= -1 # negate the objective coefficients
A = [[-3, 1], [1, 2]]
b = [6, 4]
x0_bnds = (None, None)
x1_bnds = (-3, None)
res = linprog(c, A, b, bounds=(x0_bnds, x1_bnds))
print("Objective = {}".format(res.get('fun') * -1)) # don't forget to retransform your objective back!

outputs

>>> Objective = 11.4285714286
Gregor
  • 1,333
  • 9
  • 16
  • Thanks, it worked. One more question is that how to define upper and lower bound in the same [example](https://docs.scipy.org/doc/scipy/reference/optimize.linprog-simplex.html) in python. For example, for the following case how would you do it in python: Minimize: f = -1*x[0] + 4*x[1] Subject to: -10 <= -3*x[0] + 1*x[1] <= 6 – Anonymous Jun 09 '17 at 04:35
  • You are asking for *ranged rows*, that is, inequalities with 2 different right hand sides (the naming upper bound is usually used for variable bounds). The trick is to use 2 inequalities. You can enter the right inequality (<=6) directly. Coefficients and right hand side of the left -10 <= ... part must be multplied with -1 to express the desired second inequality in the right sense. – Gregor Jun 09 '17 at 07:16