linear programming in python?

允我心安 提交于 2019-11-28 16:13:33
Ali

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.

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

  • 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.

  • 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.

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.

The only time a graph is used to solve a linear program is for a homework problem. In all other cases, linear programming problems are solved through matrix linear algebra.

As for Python, while there are some pure-Python libraries, most people use a native library with Python bindings. There is a wide variety of free and commercial libraries for linear programming. For a detailed list, see Linear Programming in Wikipedia or the Linear Programming Software Survey in OR/MS Today.

Disclaimer: I currently work for Gurobi Optimization and formerly worked for ILOG, which provided CPLEX.

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.

For solving the linear programming problem, you can use the scipy.optimize.linprog module in SciPy, which uses the Simplex algorithm.

I would recommend using the PuLP python package. It has a nice interface and you can use differenty types of algorithms to solve LP.

lpsolve is the easiest to me. No need to install separate solver. It comes with in the package.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!