Maximization Problem
Since the linprog function from Python’s SciPy library is programmed to
solve minimization problems, it is necessary to perform a transformation
to the original objective function. Every minimization problem can be
transformed into a maximization problem my multiplying the
coefficients of the objective function by -1 (i.e. by changing their signs).
Let us consider the following maximization problem to
be solved:
IMPLEMENT THE ABOVE LINIAR PROGRAMMING PROBLEM USING
PYTHON
IMPLEMENTATION:
Import numpy as np
From scipy.optimize import linprog
#set the inequality constraints matrix
#the in equality constraints must be in form of <=
A=np.array( [ [1,0], [2,3], [1,1], [-1,0], [0,-1] ] )
#set the inequality constraints vector
B=np.array([16,19,8,0,0])
#set the coefficient of the linear objective function vector
#when maximizing change the sign of the c vector coefficient
C=np.array([-5,-7])
#solving linear programming problem
Res=linprog(C,A-ub=A,B-ub=B)
#print res
Print(‘Optimal value:’, round(res.fun*-1,ndigits=2),
‘\nx values:’,res.x,
‘\nNumber of iterations performed:’,res.nit,
‘\nStatus:’,res.message)
EXPLINATION:
Step 1: - Import required libraries
Step 2: - Set inequality constrains matrix
Step 3: - The inequality constrains must be in the form of <=
Step 4: - Set the inequality constraints vector
Step 5: - Set the coefficients of the linear objective function vector
Step 6: - Now solve the linear programming problem using the imported library
Step 7: - Now print the output.
Output:
Optimal value: 46.0
x values: [5. 3.]
Number of iterations performed: 2
Status: Optimization terminated successfully.