Free software lp-solve that solves linear programming problems. The procedure to run this on a Mac terminal and the procedure to call it from Python were not organized anywhere, so I will note it. Also, since I didn't know how to use it after it was installed, I will write the Python code as a simple example at the end.
Mac OS X El Capitan 10.11.6 Python 2.7.12
$ brew tap homebrew/science
$ brew install lp_solve
First, download the latest version of the file from https://sourceforge.net/projects/lpsolve/files/lpsolve/. The following two files are required. (Latest as of October 10, 2016: 5.5.2.5)
lp_solve_5.5.2.5_source.tar.gz
lp_solve_5.5.2.5_Python_source.tar.gz
Unzip these ($ tar xfvz) and copy the following / extra in lp_solve_5.5.2.5_Python_source.tar.gz into /lp_solve_5.5 in lp_solve_5.5.2.5_source.tar.gz.
If the directory hierarchy looks like this ↓, lp_solve_5.5/ ├ lp_solve ├ lpsolve55 ├ extra ├ : └ demo
In lp_solve / and lpsolve55 /,
$ sh ccc.osx
will do. At this time, a large number of errors will occur depending on the environment, but the necessary files will be generated. If you do lp_solve / → lpsolve55 / and sh ccc.osx, you should see that lpsolve55 / bin is generated. Copy liblpsolve55.a and liblpsolve55.dylib in / lpsolve55 / bin / osx64 /, if generated, into / usr / loca / lib.
Finally, run setpy.py inside /lp_solve_5.5/extra/Python. But before that, modify the following part in setpy.py.
setpy.py
:
:
windir = getenv('windir')
if windir == None:
WIN32 = 'NOWIN32'
LPSOLVE55 = '../../lpsolve55/bin/ux32' #→ lpsolve55/bin/Fixed to osx64
else:
WIN32 = 'WIN32'
LPSOLVE55 = '../../lpsolve55/bin/win32'
setup (name = "lpsolve55",
version = "5.5.0.9",
description = "Linear Program Solver, Interface to lpsolve",
author = "Peter Notebaert",
:
:
After saving, the rest is in the terminal
$ python setpy.py install
Then all the installation is completed.
If you get a'malloc.h' file not found error here, rewrite the specified line (probably #include \ <malloc.h>) to #include \ <stdlib.h>. Delete stdlib.h if it is already included. (Reference: malloc.h on OS X)
Let's check if it works from Python normally.
$python
Python 2.7.12 (default, Aug 3 2016, 23:22:34)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lpsolve55 import *
>>> lpsolve()
lpsolve Python Interface version 5.5.0.9
using lpsolve version 5.5.2.5
Usage: ret = lpsolve('functionname', arg1, arg2, ...)
>>>
If it looks like this, it's a success.
For example, if you want to solve the following problem
Objective function
min -400x1 -300x2 + 100x3
Constraint
s.t. \qquad\qquad\qquad\qquad\qquad\\
60x1 +40x2 + 10x3 \le 3800\\
30x1 +20x2 -40 x3 \ge 1200\\
5 \ge x1 \ge -\infty \\
20 \ge x2, x3\\
int\ x1,x2,x3
Add constraint expressions as follows.
from lpsolve55 import *
# 0,3 → Constraint expression is 0 line, variable is 3 Make this LP
lp = lpsolve('make_lp',0,3)
#Addition of objective function "min": -400x1 -300x2 +100x3」
lpsolve('set_obj_fn', lp, [-400, -300, 100] )
#Constraint expression added "60x1+40x2 +10x3 =< 3800」, 「LE」 = 「<=」、 「GE」 = 「>=」、 「EQ」 = 「=」
lpsolve('add_constraint', lp, [60,40,10], LE, 3800)
# 30x1 +20x2 -40x3 => 1200
lpsolve('add_constraint', lp, [30,20,-40], GE, 1200)
#Lower limit setting of variables,"Infinite"Express infinity with
lpsolve('set_lowbo',lp,1,-Infinite)
#Variable upper limit setting, multiple settings can be set at the same time in the list
lpsolve('set_upbo',lp,[5,20,20])
#Integer constraint, 0-1 constraint is"set_binary"
lpsolve('set_int'lp,[1,2,3])
# const.lp is generated and outputs the formulation
lpsolve('write_lp',lp,'const.lp')
#Solve the problem
lpsolve('solve',lp)
#Optimal solution output, assignable to variables. Type is list
print lpsolve('get_variables',lp)
Roughly, I arranged the commands that I think I often use. Find other commands at the lp_solve API reference. I am also studying.
~~ What I'm currently interested in is whether there is a function to stop the calculation of lp_solve at some point. For example, I'm looking for something like "If it doesn't end in 20s, I'll output a provisional solution at that point." ~~
(Addition: 2016/10/7) There was.
lpsolve('set_timeout',lp,TIME) #TIME is the number of seconds-Set to 1. TIME for 1 minute= 59
It is possible with.
Linear programming solver Install the lpsolve driver for Python lp_solve API reference INSTALL LPSOLVE FOR PYTHON
Python has a more flexible mathematical optimization library called PuLP. These articles by @SaitoTsutomu are very helpful. Please see together. Mathematical model from the beginning Python in optimization Mathematical Optimization Modeler (PuLP) Cheat Sheet (Python)
Recommended Posts