Install lp_solve on Mac OS X and call it with python.

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.

environment

Mac OS X El Capitan 10.11.6 Python 2.7.12

$ brew tap homebrew/science
$ brew install lp_solve

Installation of lp_solve body and Python interface

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.

Lp_splve notation in Python

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.

from now on

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

References

Linear programming solver Install the lpsolve driver for Python lp_solve API reference INSTALL LPSOLVE FOR PYTHON

Recommended library than lp_solve

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

Install lp_solve on Mac OS X and call it with python.
Install selenium on Mac and try it with python
Install PyQt5 with homebrew on Mac OS X Marvericks (10.9.2)
Install Sphinx on Mac OS X
Install mitmproxy on Mac OS X
Install pgmagick on Mac OS X 10.9
Word Count with Apache Spark and python (Mac OS X)
Test Python with Miniconda on OS X and Linux with travis-ci
How to install Theano on Mac OS X with homebrew
Install CaboCha in Ubuntu environment and call it with Python.
Get started with the Python framework Django on Mac OS X
Install mecab on Sakura shared server and call it from python
Install Python on Mac
Install Python 3 on Mac
Install Python 3.4 on Mac
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Run Zookeeper x python (kazoo) on Mac OS X
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Install shogun with python modular (OS X Yosemite)
Shpinx (Python documentation builder) on Mac OS X
Install Python3 on Mac and build environment [Definitive Edition]
Install pygame on python3.4 on mac
Very easy to install SciPy on Mac OS X
How to install caffe on OS X with macports
Install pandas 0.14 on python3.4 [on Mac]
Build a Python development environment on Mac OS X
mac OS X 10.15.x pyenv Python If you can't install
Memo on Mac OS X
Using multiple versions of Python on Mac OS X (2) Usage
Using NAOqi 2.4.2 Python SDK on Mac OS X El Capitan
Install Python 3.8 on Ubuntu 18.04 (OS standard)
Install Python 3.8 on Ubuntu 20.04 (OS standard)
Install Python 3.7 Anaconda on MAC, but Python 2
Install Scipy on Mac OS Sierra
Install python3 on Mac (El Capitan)
Install Python 3.9 on Ubuntu 20.04 (OS standard?)
Install Python 2.7 on Ubuntu 20.04 (OS standard?)
Steps to install python3 on mac
Install python with mac vs code
Call C / C ++ from Python on Mac
I learned MNIST with Caffe and tried to draw it (MAC OS X El Capitan)
How to install OpenCV on Cloud9 and run it in Python
Using multiple versions of Python on Mac OS X (1) Multiple Ver installation
Build a Python environment on your Mac with Anaconda and PyCharm
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Continuation ・ Notes on preparing the Python development environment on Mac OS X
How to install Theano on Mac OS X 10.10 (using pyenv, anaconda)
Install matplotlib on OS X El Capitan
A memo with Python2.7 and Python3 on CentOS
Follow active applications on Mac with Python
Installed aws-cli On Mac OS X Lion
How to erase Python 2.x on Mac.
scipy stumbles with pip install on python 2.7.8
Notes on building Python and pyenv on Mac
Build Python environment with Anaconda on Mac
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
Installing PIL with Python 3.x on macOS
Run NASA CEA on Mac OS X
Deploy a Python app on Google App Engine and integrate it with GitHub
Put OpenCV in OS X with Homebrew and input / output video with python
Put MeCab binding for Python with pip on Windows, mac and Linux