linear programming in python? - 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.
.
I'm using Python 3.2, numpy, and matplotlib. Are there better modules for linear programming in Python?

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.

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

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.

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

Here is a graphical representation of the problem, inspired from How to visualize feasible region for linear programming (with arbitrary inequalities) in Numpy/MatplotLib?
import numpy as np
import matplotlib.pyplot as plt
m = np.linspace(0,5,200)
x,y = np.meshgrid(m,m)
plt.imshow(((6*x+4*y<=24)&(x+2*y<=6)&(-x+y<=1)&(y<=2)&(x>=0)&(y>=0)).astype(int),
extent=(x.min(),x.max(),y.min(),y.max()),origin='lower',cmap='Greys',alpha=0.3);
# plot constraints
x = np.linspace(0, 5, 2000)
# 6*x+4*y<=24
y0 = 6-1.5*x
# x+2*y<=6
y1 = 3-0.5*x
# -x+y<=1
y2 = 1+x
# y <= 2
y3 = (x*0) + 2
# x >= 0
y4 = x*0
plt.plot(x, y0, label=r'$6x+4y\leq24$')
plt.plot(x, y1, label=r'$x+2y\leq6$')
plt.plot(x, y2, label=r'$-x+y\leq1$')
plt.plot(x, 2*np.ones_like(x), label=r'$y\leq2$')
plt.plot(x, y4, label=r'$x\geq0$')
plt.plot([0,0],[0,3], label=r'$y\geq0$')
xv = [0,0,1,2,3,4,0]; yv = [0,1,2,2,1.5,0,0]
plt.plot(xv,yv,'ko--',markersize=7,linewidth=2)
for i in range(len(xv)):
plt.text(xv[i]+0.1,yv[i]+0.1,f'({xv[i]},{yv[i]})')
plt.xlim(0,5); plt.ylim(0,3); plt.grid(); plt.tight_layout()
plt.legend(loc=1); plt.xlabel('x'); plt.ylabel('y')
plt.show()
The problem is missing an objective function so any of the shaded points satisfy the inequalities. If it did have an objective function (e.g. Maximize x+y) then many capable Python solvers can handle this problem. Here is a Linear Programming example in GEKKO that also supports mixed integer, nonlinear, and differential constraints.
from gekko import GEKKO
m = GEKKO(remote=False)
x,y = m.Array(m.Var,2,lb=0)
m.Equations([6*x+4*y<=24,x+2*y<=6,-x+y<=1,y<=2])
m.Maximize(x+y)
m.solve(disp=False)
Large scale LP problems are solved in matrix form or in sparse matrix form where only the non-zeros of the matrices are stored. There is a tutorial on LP solutions with a few examples that I developed for a university course.

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.

Related

Solving minimization problem over discrete matrices with constraints

I'm trying to solve an order minimization problem with python. Therefore I distribute M orders over N workers. Every worker has a basic energy-level X_i which is gathered in the vector X. Also, every order has a specific energy consumption E_j which is gathered in E. With that being said I'm trying to solve the following problem
where Y is some optimal energy level, with the norm beeing the 2-norm. Under the constraints, that any column adds up to exactly one, since an order should be done and could only be done by one worker. I looked at scipy.optimize but it doesn't seem to support this sort of optimization as far as I can tell.
Does one know any tools in Python for this sort of discrete optimization problem?
The answer depends on the norm. If you want the 2-norm, this is a MIQP (Mixed Integer Quadratic Programming) problem. It is convex, so there are quite a number of solvers around (e.g. Cplex, Gurobi, Xpress -- these are commercial solvers). It can also be handled by an MINLP solver such as BonMin (open source). Some modeling tools that can help are Pyomo and CVXPY.
If you want the 1-norm, this can be formulated as a linear MIP (Mixed Integer Programming) model. There are quite a few MIP solvers such as Cplex, Gurobi, Xpress (commercial) and CBC, GLPK (open source). Some modeling tools are Pyomo, CVXPY, and PuLP.

Solving homogeneous linear systems using Sympy

I'm teaching myself linear algebra, and I'm trying to learn the corresponding Numpy and Sympy code alongside it.
My book presented the following matrix:
example1 = Matrix([[3,5,-4,0],[-3,-2,4,0],[6,1,-8,0]])
with the instructions to determine if there is a nontrivial solution. The final solution would be x = x3 * Matrix([[4\3],[0],[1]]). (Using Jupyter's math mode, I used the following to represent the solution:)
$$\pmb{x} =
\begin{bmatrix}x_1\\x_2\\x_3\end{bmatrix} =
\begin{bmatrix}\frac{4}{3}x_3\\0\\x_3\end{bmatrix} =
x_3\begin{bmatrix}\frac{4}{3}\\0\\1\end{bmatrix} \\
= x_3\pmb{v} \text{, where }\pmb{v} = \begin{bmatrix}\frac{4}{3}\\0\\1\end{bmatrix}$$
How can I now solve this in Sympy? I've looked through the documentation, but I didn't see anything, and I'm at a bit of a loss. I know that errors tend to be thrown for free variables. Is there a way to determine nontrivial solutions and the corresponding general solution using Sympy, considering that nontrivial solutions are reliant upon free variables? Or is np.linalg generally more preferred for this type of problem?
This is a linear system so
>>> linsolve(Matrix([[3,5,-4,0],[-3,-2,4,0],[6,1,-8,0]]))
FiniteSet((4*tau0/3, 0, tau0))
The tau0 is the free parameter that you refer to as x3.

Solving reaction-diffusion system with Theano

I am new to Theano, and I try to implement a numerical integrator of a reaction-diffusion system - FitzHugh–Nagumo model of this version:
For now my expressions are:
import theano as th
import theano.tensor as T
u = T.dmatrix('u')
v = T.dmatrix('v')
e = T.dscalar('e')
a0 = T.dscalar('a0')
a1 = T.dscalar('a1')
dudt = u - u**3 -v
dvdt = e*(u - a1*v - a0)
So I haven't implemented the finite-differences laplacian operator yet. My question is whether there is a smart way of doing it in Theano?
Is there any reason for using Theano? There are other ways in Python to solve a system of coupled non-linear ODEs.
The reaction-diffusion system definition from Google seems to suggest that u(x,y,t), v(x,y,t).
I am not a user of Theano, but it looks like casting the problem in the form of an equation like b = Ax is the way to go.
Some resources that I came across on Google for using Theano and generally solving PDEs are below.
Expressing the Laplacian using Theano
Solving a reaction-diffusion problem using numpy
Github project using Theano to solve the shallow water PDE
An interesting example of a similar, but simpler problem, solved using convolutional networks on Google's tensorflow can be found here:
https://www.tensorflow.org/versions/r0.7/tutorials/pdes/index.html
In particular they use the following definition of the diffusion kernel:
laplace_k = make_kernel([[0.5, 1.0, 0.5],
[1.0, -6., 1.0],
[0.5, 1.0, 0.5]])
I see two coupled, first order, non-linear, ordinary differential equations here.
Update: Now your equations are clear - Laplacians are there; two coupled nonlinear PDEs. Much better.
You need a finite difference or finite element approach for your spatial discretization. Your choice, of course, but I'd prefer a finite element approach over finite differences.
You also need some kind of numerical integration in time. An implicit error correcting scheme would be best.
I looked quickly at the Theano docs. I didn't see anything to help you with your spatial discretization problem. Once you accomplish that you'll have matrix equations that you can solve, but I don't believe Theano will help you formulate the problem.
I'll admit that I'm not a Theano maven.

Constraints on fitting parameters with Python and ODRPACK

I'm using the ODRPACK library in Python to fit some 1d data. It works quite well, but I have one question: is there any possibility to make constraints on the fitting parameters? For example if I have a model y = a * x + b and for physical reasons parameter a can by only in range (-1, 1). I've found that such constraints can be done in original Fortran implementation of the ODRPACK95 library, but I can't find how to do that in Python.
Of course, I can implement my functions such that they will return very big values, if the fitting parameters are out of bounds and chi squared will be big too, but I wonder if there is a right way to do that.
I'm afraid that the older FORTRAN-77 version of ODRPACK wrapped by scipy.odr does not incorporate constraints. ODRPACK95 is a later extension of the original ODRPACK library that predates the scipy.odr wrappers, and it is unclear that we could legally include it in scipy. There is no explicit licensing information for ODRPACK95, only the general ACM TOMS non-commercial license.

binary linear programming solver in Python

I have a Python script in which I need to solve a linear programming problem. The catch is that the solution must be binary. In other words, I need an equivalent of MATLAB's bintprog function. NumPy and SciPy do not seem to have such a procedure. Does anyone have suggestions on how I could do one of these three things:
Find a Python library which includes such a function.
Constrain the problem such that it can be solved by a more general linear programming solver.
Interface Python with MATLAB so as to make direct use of bintprog.
Just to be rigorous, if the problem is a binary programming problem, then it is not a linear program.
You can try CVXOPT. It has a integer programming function (see this). To make your problem a binary program, you need to add the constrain 0 <= x <= 1.
Edit: You can actually declare your variable as binary, so you don't need to add the constrain 0 <= x <= 1.
cvxopt.glpk.ilp = ilp(...)
Solves a mixed integer linear program using GLPK.
(status, x) = ilp(c, G, h, A, b, I, B)
PURPOSE
Solves the mixed integer linear programming problem
minimize c'*x
subject to G*x <= h
A*x = b
x[I] are all integer
x[B] are all binary
This is a half-answer, but you can use Python to interface with GLPK (through python-glpk). GLPK supports integer linear programs. (binary programs are just a subset of integer programs).
http://en.wikipedia.org/wiki/GNU_Linear_Programming_Kit
Or you could simply write your problem in Python and generate an MPS file (which most standard LP/MILP (CPLEX, Gurobi, GLPK) solvers will accept). This may be a good route to take, because as far as I am aware, there aren't any high quality MILP solvers that are native to Python (and there may never be). This will also allow you to try out different solvers.
http://code.google.com/p/pulp-or/
As for interfacing Python with MATLAB, I would just roll my own solution. You could generate a .m file and then run it from the command line
% matlab -nojava myopt.m
Notes:
If you're an academic user, you can get a free license to Gurobi, a high performance LP/MILP solver. It has a Python interface.
http://www.gurobi.com/
OpenOpt is a Python optimization suite that interfaces with different solvers.
http://en.wikipedia.org/wiki/OpenOpt
I develop a package called gekko (pip install gekko) that solves large-scale problems with linear, quadratic, nonlinear, and mixed integer programming (LP, QP, NLP, MILP, MINLP) and is released under the MIT License. A binary variable is declared as an integer variable type with lower bound 0 and upper bound 1 as b=m.Var(integer=True,lb=0,ub=1). Here is a more complete problem with the use of m.Array() to define multiple binary variables:
from gekko import GEKKO
m = GEKKO()
x,y = m.Array(m.Var,2,integer=True,lb=0,ub=1)
m.Maximize(y)
m.Equations([-x+y<=1,
3*x+2*y<=12,
2*x+3*y<=12])
m.options.SOLVER = 1
m.solve()
print('Objective: ', -m.options.OBJFCNVAL)
print('x: ', x.value[0])
print('y: ', y.value[0])

Categories

Resources