Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linear programming in python?

I need to make a linear programming model. Here are the inequalities I'm using (for example):

6x + 4y <= 24
x + 2y <= 6
-x + y <= 1
y <= 2

I need to find the area described by these inequalities, and shade it in a graph, as well as keep track of the vertices of the bounding lines of this area, and draw the bounding line in a different color. See the graph below for an example of what I'm looking for.

image of the points of intersection.

I'm using Python 3.2, numpy, and matplotlib. Are there better modules for linear programming in Python?

like image 820
user24562 Avatar asked May 22 '12 07:05

user24562


People also ask

What is linear programming in Python?

Linear Programming is basically a subset of optimization. Linear programming or linear optimization is an optimization technique wherein we try to find an optimal value for a linear objective function for a system of linear constraints using a varying set of decision variables.

What linear programming means?

linear programming, mathematical modeling technique in which a linear function is maximized or minimized when subjected to various constraints. This technique has been useful for guiding quantitative decisions in business planning, in industrial engineering, and—to a lesser extent—in the social and physical sciences.


3 Answers

UPDATE: The answer has become somewhat outdated in the past 4 years, here is an update. You have many options:

  • If you do not have to do it Python then it is a lot more easier to do this in a modeling langage, see Any good tools to solve integer programs on linux?

  • I personally use Gurobi these days through its Python API. It is a commercial, closed-source product but free for academic research.

  • With PuLP you can create MPS and LP files and then solve them with GLPK, COIN CLP/CBC, CPLEX, or XPRESS through their command-line interface. This approach has its advantages and disadvantages.

  • The OR-Tools from Google is an open source software suite for optimization, tuned for tackling the world's toughest problems in vehicle routing, flows, integer and linear programming, and constraint programming.

  • Pyomo is a Python-based, open-source optimization modeling language with a diverse set of optimization capabilities.

  • SciPy offers linear programming: scipy.optimize.linprog. (I have never tried this one.)

  • Apparently, CVXOPT offers a Python interface to GLPK, I did not know that. I have been using GLPK for 8 years now and I can highly recommend GLPK. The examples and tutorial of CVXOPT seem really nice!

  • You can find other possibilites at in the Wikibook under GLPK/Python. Note that many of these are not necessarily resticted to GLPK.

like image 195
Ali Avatar answered Oct 07 '22 18:10

Ali


I'd recommend the package cvxopt for solving convex optimization problems in Python. A short example with Python code for a linear program is in cvxopt's documentation here.

like image 20
silvado Avatar answered Oct 07 '22 20:10

silvado


The other answers have done a good job providing a list of solvers. However, only PuLP has been mentioned as a Python library to formulating LP models.

Another great option is Pyomo. Like PuLP, you can send the problem to any solver and read the solution back into Python. You can also manipulate solver parameters. A classmate and I compared the performance of PuLP and Pyomo back in 2015 and we found Pyomo could generate .LP files for the same problem several times more quickly than PuLP.

like image 35
Sesquipedalism Avatar answered Oct 07 '22 20:10

Sesquipedalism