0% found this document useful (0 votes)
30 views2 pages

Python Linear Programming Guide

The document discusses transforming a maximization problem into a minimization problem that can be solved using the linprog function from SciPy. It explains that every maximization problem can be transformed by multiplying the objective function coefficients by -1. It then provides an example linear programming maximization problem and its implementation in Python using linprog, which requires changing the sign of the objective function coefficients to correctly solve the maximization.

Uploaded by

likhith chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Python Linear Programming Guide

The document discusses transforming a maximization problem into a minimization problem that can be solved using the linprog function from SciPy. It explains that every maximization problem can be transformed by multiplying the objective function coefficients by -1. It then provides an example linear programming maximization problem and its implementation in Python using linprog, which requires changing the sign of the objective function coefficients to correctly solve the maximization.

Uploaded by

likhith chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

You might also like