optimizationscipysimplex

Simplex algorithm in scipy package python


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?


Solution

  • 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