Linear program solvers: Lpsolve
In the following articles we will solve the instance of a Minimum cost flow problem described in (1). Node 1 is the source node, nodes 2 and 3 are the transshipment nodes and node 4 is the sink node.
As lpsolve can read the DIMACS file format we don’t have to transform a graph structure into a plain linear program. The XLI (eXtensible language interface) does this for us.
A good description of the DIMACS Minimum cost flow problem file format can be found here.
So for solving our MCFP we just have to write down the MCFP in DIMACS file format with a text editor of our choice (I called the file ’simple_MCFP.net‘) and let lpsolve do the dirty work:
c Problem line (nodes, links)
p min 4 5
c Node descriptor lines (supply+ or demand-)
n 1 4
n 4 -4
c Arc descriptor lines (from, to, minflow, maxflow, cost)
a 1 2 0 4 2
a 1 3 0 2 2
a 2 3 0 2 1
a 2 4 0 3 3
a 3 4 0 5 1
Here is the python code for solving a minimum cost flow problem in DIMACS file format with the python API of lpsolve:
from lpsolve55 import * # full file path to DIMACS file path = 'simple_MCFP.net' # read DIMACS file and create linear program lp = lpsolve('read_XLI', 'xli_DIMACS', path) # solve linear program lpsolve('solve', lp) # get total optimum optimum = lpsolve('get_objective', lp) print optimum #should be 14
When you are working with the python API of lpsolve you’ll always have to call the function lpsolve() and pass the C method you want to access as first argument as string, e.g. lpsolve(’solve‘, lp). So obviously this API is a very thin layer upon the C library underneath.
The usage of the python API of lpsolve is not really pythonic because lpsolve is written in C – but hey: it works! The complete list of functions can be found here.