linear programming in python?

2024/11/18 19:56:21

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?

Answer

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.

https://en.xdnf.cn/q/26528.html

Related Q&A

Beginner Python Practice? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Pylint: Disable specific warnings for specific folder

We have a Python project laid out like this:project/ ├── .pylintrc ├── module1.py ├── module2.py └── tests/├── test_module1.py└── test_module2.pyOur unit and function tests reside in …

PyCharm: Configuring multi-hop remote Interpreters via SSH

To connect to the computer at my office I need to run ssh twice. First to connect to the host-1 and then from host-1 to host-2 and each one has different credentials. However the configuration menu in …

Return results from multiple models with Django REST Framework

I have three models — articles, authors and tweets. Im ultimately needing to use Django REST Framework to construct a feed that aggregates all the objects using the Article and Tweet models into one r…

Comparing XML in a unit test in Python

I have an object that can build itself from an XML string, and write itself out to an XML string. Id like to write a unit test to test round tripping through XML, but Im having trouble comparing the tw…

Passing a tuple as command line argument

My requirement is to pass a tuple as command line argument like --data (1,2,3,4)I tried to use the argparse module, but if I pass like this it is receiving as the string (1,2,3,4). I tried by giving ty…

Setting exit code in Python when an exception is raised

$ cat e.py raise Exception $ python e.py Traceback (most recent call last):File "e.py", line 1, in <module>raise Exception Exception $ echo $? 1I would like to change this exit code fr…

cmake error the source does not appear to contain CMakeLists.txt

Im installing opencv in ubuntu 16.04. After installing the necessary prerequisites I used the following command:-kvs@Hunter:~/opencv_contrib$ mkdir build kvs@Hunter:~/opencv_contrib$ cd build kvs@Hunte…

Overlay an image segmentation with numpy and matplotlib

I am trying to overlay two images. The first one is a 512x512 NumPy array (from a CT image). The second one is also a 512x512 NumPy array but I am just interested in the pixels where the value is large…

Why isnt my variable set when I call the other function? [duplicate]

This question already has answers here:How do I get ("return") a result (output) from a function? How can I use the result later?(4 answers)How can I fix a TypeError that says an operator (…