Difference between revisions of "Gurobi/Note"

From ScientificComputing
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
===Installing the R,Matlab and Python interfaces locally===
+
Gurobi provides different interfaces (Python, Matlab, R) that can be used. Please find below instructions on how to install the interfaces locally for any version of Python, Matlab or R.
 +
 
 +
===Matlab interface===
 +
For using the Matlab interface, you would need to load your favorite Matlab and Gurobi module and then add the path to the Gurobi Matlab package, using the Matlab command <tt>addpath</tt>:
 +
 
 +
addpath('/cluster/apps/gurobi/__VERSION__/x86_64/matlab')
 +
 
 +
where <tt>__VERSION__</tt> needs to be replace with the Gurobi version for which you loaded the corresponding module. For Gurobi installations (9.0.2, 9.1.1) in the new software stack, the path that needs to be added is
 +
 
 +
addpath('/cluster/apps/nss/gurobi/__VERSION__/x86_64/matlab')
 +
 
 +
Please find below an example to use the Matlab interface with Gurobi 9.0.0:
 +
 
 +
[sfux@eu-login-47 test1]$ '''cp /cluster/apps/gurobi/9.0.0/x86_64/examples/matlab/mip1.m .'''
 +
[sfux@eu-login-47 test1]$ '''module load new gcc/4.8.2 gurobi/9.0.0 matlab/R2017b'''
 +
[sfux@eu-login-47 test1]$ '''matlab -nodisplay -nojvm -singleCompThread'''
 +
MATLAB is selecting SOFTWARE OPENGL rendering.
 +
 +
                                                            < M A T L A B (R) >
 +
                                                  Copyright 1984-2017 The MathWorks, Inc.
 +
                                                  R2017b (9.3.0.713579) 64-bit (glnxa64)
 +
                                                            September 14, 2017
 +
 +
 
 +
To get started, type one of these: helpwin, helpdesk, or demo.
 +
For product information, visit www.mathworks.com.
 +
 
 +
>> '''addpath('/cluster/apps/gurobi/9.0.0/x86_64/matlab')'''
 +
>> '''mip1'''
 +
          status: 'OPTIMAL'
 +
      versioninfo: [1x1 struct]
 +
          runtime: 0.0014
 +
          objval: 3
 +
                x: [3x1 double]
 +
            slack: [2x1 double]
 +
    poolobjbound: 3
 +
            pool: [1x2 struct]
 +
          mipgap: 0
 +
        objbound: 3
 +
        objboundc: 3
 +
        itercount: 0
 +
    baritercount: 0
 +
        nodecount: 0
 +
 +
x 1
 +
y 0
 +
z 1
 +
Obj: 3.000000e+00
 +
>>
 +
 
 +
If you would like to install the Gurobi Matlab interface permanently, then it is sufficient to create a file
 +
 
 +
$HOME/matlab/startup.m
 +
 
 +
that contains the <tt>addpath</tt> command. Matlab will on startup automatically check the startup.m file.
 +
 
 +
===R interface===
 +
For using the R interface, you would need to load your favorite R and Gurobi module, start an interactive R session and then use the following R command to install the interface:
 +
 
 +
install.packages('__TARBALL__', repos=NULL)
 +
 
 +
where <tt>__TARBALL__</tt> needs to be replace with the corresponding path for the Gurobi R package. Please find below the list of options for __TARBALL__:
 +
 
 +
/cluster/apps/gurobi/6.0.2/x86_64/R/gurobi_6.0-2_R_x86_64-unknown-linux-gnu.tar.gz
 +
/cluster/apps/gurobi/6.5.1/x86_64/R/gurobi_6.5-1_R_x86_64-unknown-linux-gnu.tar.gz
 +
/cluster/apps/gurobi/7.0.2/x86_64/R/gurobi_7.0-2_R_x86_64-pc-linux-gnu.tar.gz
 +
/cluster/apps/gurobi/8.0.0/x86_64/R/gurobi_8.0-0_R_x86_64-pc-linux-gnu.tar.gz
 +
/cluster/apps/gurobi/8.1.1/x86_64/R/gurobi_8.1-1_R_3.5.0.tar.gz
 +
/cluster/apps/gurobi/9.0.0/x86_64/R/gurobi_9.0-0_R_3.6.1.tar.gz
 +
 
 +
The interface only needs to be installed locally once and should afterwards automatically be found by R.
 +
 
 +
===Python interface===
 +
The local installation of the Python interface requires an initial setup, since it uses distutils instead of pip. In a first step, you need to create a directory structure in your <tt>$HOME</tt>. The installation requires the path
 +
 
 +
$HOME/python/lib64/pythonX.Y/site-packages
 +
 
 +
with X.Y being the first two digits of the Python version. In the following example, we use Python 2.7.14
 +
 +
[sfux@eu-login-39 ~]$ '''mkdir $HOME/python'''
 +
[sfux@eu-login-39 ~]$ '''cd $HOME/python'''
 +
[sfux@eu-login-39 python]$ '''mkdir -p lib64/python2.7/site-packages'''
 +
[sfux@eu-login-39 python]$ '''export PYTHONPATH=$HOME/python/lib64/python2.7/site-packages:$PYTHONPATH'''
 +
[sfux@eu-login-39 python]$ '''module load new gcc/4.8.2 gurobi/9.0.0 python/2.7.14'''
 +
Autoloading openblas/0.2.13_seq
 +
[sfux@eu-login-39 python]$ '''cd /cluster/apps/gurobi/9.0.0/x86_64/'''
 +
 
 +
After this, you can install the package:
 +
 
 +
[sfux@eu-login-39 x86_64]$ '''python setup.py build -b $SCRATCH install --prefix=$HOME/python'''
 +
running build
 +
running build_py
 +
running install
 +
running install_lib
 +
creating /cluster/home/sfux/python/lib64/python2.7/site-packages/gurobipy
 +
copying /cluster/scratch/sfux/lib/gurobipy/gurobipy.so -> /cluster/home/sfux/python/lib64/python2.7/site-packages/gurobipy
 +
copying /cluster/scratch/sfux/lib/gurobipy/__init__.py -> /cluster/home/sfux/python/lib64/python2.7/site-packages/gurobipy
 +
byte-compiling /cluster/home/sfux/python/lib64/python2.7/site-packages/gurobipy/__init__.py to __init__.pyc
 +
running install_egg_info
 +
Writing /cluster/home/sfux/python/lib64/python2.7/site-packages/gurobipy-9.0.0-py2.7.egg-info
 +
[sfux@eu-login-39 x86_64]$
 +
 
 +
Then the package is installed in $HOME/python. To use the package, you would need to load the modules and set the $PYTHONPATH:
 +
 +
[sfux@eu-login-37 ~]$ '''module load new gcc/4.8.2 gurobi/9.0.0 python/2.7.14'''
 +
Autoloading openblas/0.2.13_seq
 +
[sfux@eu-login-37 ~]$ '''export PYTHONPATH=$HOME/python/lib64/python2.7/site-packages:$PYTHONPATH'''
 +
[sfux@eu-login-37 ~]$ '''python'''
 +
Python 2.7.14 (default, Oct  4 2017, 08:15:29)
 +
[GCC 4.8.2] on linux2
 +
Type "help", "copyright", "credits" or "license" for more information.
 +
Using license file /cluster/apps/gurobi/9.0.0/x86_64/gurobi.lic
 +
Set parameter TokenServer to value lic-gurobi.ethz.ch
 +
Set parameter LogFile to value gurobi.log
 +
 +
Gurobi Interactive Shell (linux64), Version 9.0.0
 +
Copyright (c) 2019, Gurobi Optimization, LLC
 +
Type "help()" for help
 +
 +
gurobi>
 +
 
 +
To automatically set the <tt>$PYTHONPATH</tt> on login, you can add the line with the export statement into your .bashrc or .bash_profile file.

Latest revision as of 06:38, 20 July 2021

Gurobi provides different interfaces (Python, Matlab, R) that can be used. Please find below instructions on how to install the interfaces locally for any version of Python, Matlab or R.

Matlab interface

For using the Matlab interface, you would need to load your favorite Matlab and Gurobi module and then add the path to the Gurobi Matlab package, using the Matlab command addpath:

addpath('/cluster/apps/gurobi/__VERSION__/x86_64/matlab')

where __VERSION__ needs to be replace with the Gurobi version for which you loaded the corresponding module. For Gurobi installations (9.0.2, 9.1.1) in the new software stack, the path that needs to be added is

addpath('/cluster/apps/nss/gurobi/__VERSION__/x86_64/matlab')

Please find below an example to use the Matlab interface with Gurobi 9.0.0:

[sfux@eu-login-47 test1]$ cp /cluster/apps/gurobi/9.0.0/x86_64/examples/matlab/mip1.m .
[sfux@eu-login-47 test1]$ module load new gcc/4.8.2 gurobi/9.0.0 matlab/R2017b
[sfux@eu-login-47 test1]$ matlab -nodisplay -nojvm -singleCompThread
MATLAB is selecting SOFTWARE OPENGL rendering.

                                                           < M A T L A B (R) >
                                                 Copyright 1984-2017 The MathWorks, Inc.
                                                 R2017b (9.3.0.713579) 64-bit (glnxa64)
                                                           September 14, 2017

 
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
 
>> addpath('/cluster/apps/gurobi/9.0.0/x86_64/matlab')
>> mip1
          status: 'OPTIMAL'
     versioninfo: [1x1 struct]
         runtime: 0.0014
          objval: 3
               x: [3x1 double]
           slack: [2x1 double]
    poolobjbound: 3
            pool: [1x2 struct]
          mipgap: 0
        objbound: 3
       objboundc: 3
       itercount: 0
    baritercount: 0
       nodecount: 0 

x 1
y 0
z 1
Obj: 3.000000e+00
>> 

If you would like to install the Gurobi Matlab interface permanently, then it is sufficient to create a file

$HOME/matlab/startup.m

that contains the addpath command. Matlab will on startup automatically check the startup.m file.

R interface

For using the R interface, you would need to load your favorite R and Gurobi module, start an interactive R session and then use the following R command to install the interface:

install.packages('__TARBALL__', repos=NULL)

where __TARBALL__ needs to be replace with the corresponding path for the Gurobi R package. Please find below the list of options for __TARBALL__:

/cluster/apps/gurobi/6.0.2/x86_64/R/gurobi_6.0-2_R_x86_64-unknown-linux-gnu.tar.gz
/cluster/apps/gurobi/6.5.1/x86_64/R/gurobi_6.5-1_R_x86_64-unknown-linux-gnu.tar.gz
/cluster/apps/gurobi/7.0.2/x86_64/R/gurobi_7.0-2_R_x86_64-pc-linux-gnu.tar.gz
/cluster/apps/gurobi/8.0.0/x86_64/R/gurobi_8.0-0_R_x86_64-pc-linux-gnu.tar.gz
/cluster/apps/gurobi/8.1.1/x86_64/R/gurobi_8.1-1_R_3.5.0.tar.gz
/cluster/apps/gurobi/9.0.0/x86_64/R/gurobi_9.0-0_R_3.6.1.tar.gz

The interface only needs to be installed locally once and should afterwards automatically be found by R.

Python interface

The local installation of the Python interface requires an initial setup, since it uses distutils instead of pip. In a first step, you need to create a directory structure in your $HOME. The installation requires the path

$HOME/python/lib64/pythonX.Y/site-packages

with X.Y being the first two digits of the Python version. In the following example, we use Python 2.7.14

[sfux@eu-login-39 ~]$ mkdir $HOME/python
[sfux@eu-login-39 ~]$ cd $HOME/python
[sfux@eu-login-39 python]$ mkdir -p lib64/python2.7/site-packages
[sfux@eu-login-39 python]$ export PYTHONPATH=$HOME/python/lib64/python2.7/site-packages:$PYTHONPATH
[sfux@eu-login-39 python]$ module load new gcc/4.8.2 gurobi/9.0.0 python/2.7.14
Autoloading openblas/0.2.13_seq
[sfux@eu-login-39 python]$ cd /cluster/apps/gurobi/9.0.0/x86_64/

After this, you can install the package:

[sfux@eu-login-39 x86_64]$ python setup.py build -b $SCRATCH install --prefix=$HOME/python
running build
running build_py
running install
running install_lib
creating /cluster/home/sfux/python/lib64/python2.7/site-packages/gurobipy
copying /cluster/scratch/sfux/lib/gurobipy/gurobipy.so -> /cluster/home/sfux/python/lib64/python2.7/site-packages/gurobipy
copying /cluster/scratch/sfux/lib/gurobipy/__init__.py -> /cluster/home/sfux/python/lib64/python2.7/site-packages/gurobipy
byte-compiling /cluster/home/sfux/python/lib64/python2.7/site-packages/gurobipy/__init__.py to __init__.pyc
running install_egg_info
Writing /cluster/home/sfux/python/lib64/python2.7/site-packages/gurobipy-9.0.0-py2.7.egg-info
[sfux@eu-login-39 x86_64]$

Then the package is installed in $HOME/python. To use the package, you would need to load the modules and set the $PYTHONPATH:

[sfux@eu-login-37 ~]$ module load new gcc/4.8.2 gurobi/9.0.0 python/2.7.14
Autoloading openblas/0.2.13_seq
[sfux@eu-login-37 ~]$ export PYTHONPATH=$HOME/python/lib64/python2.7/site-packages:$PYTHONPATH
[sfux@eu-login-37 ~]$ python
Python 2.7.14 (default, Oct  4 2017, 08:15:29)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Using license file /cluster/apps/gurobi/9.0.0/x86_64/gurobi.lic
Set parameter TokenServer to value lic-gurobi.ethz.ch
Set parameter LogFile to value gurobi.log

Gurobi Interactive Shell (linux64), Version 9.0.0
Copyright (c) 2019, Gurobi Optimization, LLC
Type "help()" for help

gurobi> 

To automatically set the $PYTHONPATH on login, you can add the line with the export statement into your .bashrc or .bash_profile file.