Gurobi/fr: Difference between revisions
No edit summary |
(Updating to match new version of source page) |
||
Line 11: | Line 11: | ||
===Academic Usage Agreement=== | ===Academic Usage Agreement=== | ||
<div class="mw-translate-fuzzy"> | |||
== Allocations interactives == | |||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
où | |||
* https://www.gurobi.com/documentation/8.1/refman/recording_api_calls.html | |||
** https://www.gurobi.com/documentation/8.1/refman/parameter_descriptions.html | |||
*** https://www.gurobi.com/documentation/8.1/refman/optimization_status_codes.html | |||
**** https://www.gurobi.com/documentation/8.1/refman/attributes.html | |||
</div> | |||
$ module load gurobi | |||
$ gurobi_cl 1> /dev/null && echo Success || echo Fail | |||
<div class="mw-translate-fuzzy"> | |||
===Ligne de commande=== | ===Ligne de commande=== | ||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
=== Gurobi-Python === | |||
</div> | |||
[gra-login2:~] salloc --time=1:00:0 --cpus-per-task=8 --mem=1G --account=def-xyz | [gra-login2:~] salloc --time=1:00:0 --cpus-per-task=8 --mem=1G --account=def-xyz | ||
Line 22: | Line 39: | ||
[gra800:~] gurobi_cl --help | [gra800:~] gurobi_cl --help | ||
<div class="mw-translate-fuzzy"> | |||
# Create environment file in current directory setting the number of threads: | |||
echo "Threads ${SLURM_CPUS_ON_NODE:-1}" > gurobi.env | |||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
== Environnements virtuels Python == | |||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
=== Interpréteur interactif === | === Interpréteur interactif === | ||
Line 38: | Line 65: | ||
gurobi> m.runtime see **** | gurobi> m.runtime see **** | ||
gurobi> help() | gurobi> help() | ||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
Cette procédure est effectuée une seule fois pour chaque système. | |||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
{{Commands|prompt=[name@server ~] $ | |||
| module load gurobi/9.0.1 python/3.7 | |||
| virtualenv --no-download ~/env_gurobi | |||
Using base prefix '/cvmfs/soft.computecanada.ca/easybuild/software/2017/Core/python/3.7.4' | |||
New python executable in /home/name/env_gurobi/bin/python | |||
Installing setuptools, pip, wheel... | |||
done. | |||
| source ~/env_gurobi/bin/activate | |||
}} | |||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
===Répéter des appels API=== | ===Répéter des appels API=== | ||
Il est possible d'enregistrer des appels API et de rejouer l'enregistrement avec la commande | Il est possible d'enregistrer des appels API et de rejouer l'enregistrement avec la commande | ||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
Nous pouvons maintenant activer Gurobi et l'environnement avec | |||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
Remarquez que nous utilisons maintenant <code>python</code> plutôt que <code>gurobi.sh</code>. | |||
</div> | |||
= | <div class="mw-translate-fuzzy"> | ||
== Comment citer Gurobi == | |||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
# use a version >= 9.0.3 | # use a version >= 9.0.3 | ||
module load StdEnv/2020 | module load StdEnv/2020 | ||
module load gurobi/9.1.0 | module load gurobi/9.1.0 | ||
</div> | |||
gurobi_cl ${GUROBI_HOME}/examples/data/coins.lp | gurobi_cl ${GUROBI_HOME}/examples/data/coins.lp | ||
}} | }} | ||
=== Gurobi | === Job using Gurobi Python === | ||
This is an example jobscript for a model using [https://www.gurobi.com/documentation/8.1/refman/py_python_api_overview.html#sec:Python Gurobi-Python]. | |||
{{File | {{File | ||
|name=gurobi-py_example.sh | |name=gurobi-py_example.sh | ||
Line 99: | Line 132: | ||
}} | }} | ||
== | == Using Gurobi in Python virtual environments == | ||
Gurobi | Gurobi brings it's own version of Python but that one does not contain any 3rd-party Python packages except Gurobi. In order to use Gurobi together with popular Python | ||
packages like NumPy, Matplotlib, Pandas and others, we need to create a [[Python#Creating_and_using_a_virtual_environment|virtual Python environment]] in which we can install both <code>gurobipy</code> and e.g. <code>pandas</code>. | |||
Before we start, we need to decide which combination of versions for Gurobi and Python to use. | |||
{{Commands|prompt=[name@server ~] $ | {{Commands|prompt=[name@server ~] $ | ||
Line 121: | Line 155: | ||
}} | }} | ||
We see that <code>gurobi/8.1.1</code> brings it's own installation of <code>python2.7/</code> and Python packages for Python 2.7, 3.5, 3.6 and 3.7 (<code>pythonX.Y_utf32/</code>), | |||
while <code>gurobi/9.0.1</code> by default uses <code>python3.7/</code> and brings Python packages for Python 2.7, 3.5, 3.6, 3.7 and 3.8 (<code>pythonX.Y_utf32/</code>). | |||
In this example we want to create a Python environment based on <code>python/3.7</code> in which we want to use <code>gurobi/9.0.1</code> and install the Pandas package. | |||
=== | === Creating a Python virtual environments with Gurobi === | ||
These steps need to be done only once per system. | |||
The first step is to load the modules to [[Python#Creating_and_using_a_virtual_environment|create the virtual environment]] and activate it. | |||
{{Commands|prompt=[name@server ~] $ | {{Commands|prompt=[name@server ~] $ | ||
Line 142: | Line 176: | ||
}} | }} | ||
Now that the environment has been activated we can install the Python packages we want to use, in this case <code>pandas</code>. | |||
{{Commands|prompt=(env_gurobi) [name@server ~] $ | {{Commands|prompt=(env_gurobi) [name@server ~] $ | ||
Line 154: | Line 188: | ||
}} | }} | ||
The third step is to install gurobipy into the environment: | |||
{{Commands|prompt=(env_gurobi) [name@server ~] $ | {{Commands|prompt=(env_gurobi) [name@server ~] $ | ||
Line 177: | Line 211: | ||
}} | }} | ||
=== | === Using the Gurobi-enabled virtual environment === | ||
Python scripts can now import both Pandas and Gurobi: | |||
import pandas as pd | import pandas as pd | ||
Line 187: | Line 221: | ||
# [...] | # [...] | ||
Once created we can activate Gurobi and the environment with: | |||
module load gurobi/9.0.1 | module load gurobi/9.0.1 | ||
Line 193: | Line 227: | ||
python my_gurobi_script.py | python my_gurobi_script.py | ||
Note that we now use <code>python</code> instead of <code>gurobi.sh</code>! | |||
And this is an example job script that we can use: | |||
{{File | {{File | ||
|name=gurobi-py_example.sh | |name=gurobi-py_example.sh | ||
Line 216: | Line 250: | ||
}} | }} | ||
== | == Cite Gurobi == | ||
Please see [https://support.gurobi.com/hc/en-us/articles/360013195592-How-do-I-cite-Gurobi-software-for-an-academic-publication- How do I cite Gurobi software for an academic publication?] |
Revision as of 20:11, 23 April 2021
Gurobi est une suite logicielle du commerce qui permet de résoudre des problèmes complexes d'optimisation. Nous abordons ici son utilisation pour la recherche sur les grappes de Calcul Canada. Des renseignements additionnels seront fournis dès que disponibles.
Limites de la licence
Calcul Canada dispense le soutien technique pour la licence gratuite disponible sur Graham, Cedar, Béluga et Niagara. Cette licence permet 4096 utilisations simultanées (avec jetons) et l'optimisation distribuée sur un maximum de 100 nœuds. Un utilisateur peut faire exécuter plusieurs tâches en simultané. Vous devez cependant accepter certaines conditions. Faites parvenir un courriel au soutien technique avec l'entente (Academic Usage Agreement) dûment complétée; vous pourrez ensuite utiliser les applications après un délai de quelques jours.
Academic Usage Agreement
Allocations interactives
où
* https://www.gurobi.com/documentation/8.1/refman/recording_api_calls.html ** https://www.gurobi.com/documentation/8.1/refman/parameter_descriptions.html *** https://www.gurobi.com/documentation/8.1/refman/optimization_status_codes.html **** https://www.gurobi.com/documentation/8.1/refman/attributes.html
$ module load gurobi $ gurobi_cl 1> /dev/null && echo Success || echo Fail
Ligne de commande
Gurobi-Python
[gra-login2:~] salloc --time=1:00:0 --cpus-per-task=8 --mem=1G --account=def-xyz [gra800:~] module load gurobi [gra800:~] gurobi_cl Record=1 Threads=8 Method=2 ResultFile=p0033.sol LogFile=p0033.log $GUROBI_HOME/examples/data/p0033.mps [gra800:~] gurobi_cl --help
- Create environment file in current directory setting the number of threads:
echo "Threads ${SLURM_CPUS_ON_NODE:-1}" > gurobi.env
Environnements virtuels Python
Interpréteur interactif
[gra-login2:~] salloc --time=1:00:0 --cpus-per-task=8 --mem=1G --account=def-xyz [gra800:~] module load gurobi [gra800:~] echo "Record 1" > gurobi.env see * [gra800:~] gurobi.sh gurobi> m = read('/cvmfs/restricted.computecanada.ca/easybuild/software/2017/Core/gurobi/8.1.1/examples/data/glass4.mps') gurobi> m.Params.Threads = 8 see ** gurobi> m.Params.Method = 2 gurobi> m.Params.ResultFile = "glass4.sol" gurobi> m.Params.LogFile = "glass4.log" gurobi> m.optimize() gurobi> m.write('glass4.lp') gurobi> m.status see *** gurobi> m.runtime see **** gurobi> help()
Cette procédure est effectuée une seule fois pour chaque système.
[name@server ~] $ module load gurobi/9.0.1 python/3.7
[name@server ~] $ virtualenv --no-download ~/env_gurobi
Using base prefix '/cvmfs/soft.computecanada.ca/easybuild/software/2017/Core/python/3.7.4'
New python executable in /home/name/env_gurobi/bin/python
Installing setuptools, pip, wheel...
done.
[name@server ~] $ source ~/env_gurobi/bin/activate
Répéter des appels API
Il est possible d'enregistrer des appels API et de rejouer l'enregistrement avec la commande
Nous pouvons maintenant activer Gurobi et l'environnement avec
Remarquez que nous utilisons maintenant python
plutôt que gurobi.sh
.
Comment citer Gurobi
- use a version >= 9.0.3
module load StdEnv/2020 module load gurobi/9.1.0
gurobi_cl ${GUROBI_HOME}/examples/data/coins.lp }}
Job using Gurobi Python
This is an example jobscript for a model using Gurobi-Python.
#!/bin/bash
#SBATCH --account=def-group # some account
#SBATCH --time=0-00:30 # time limit (D-HH:MM)
#SBATCH --cpus-per-task=1 # number of CPUs (threads) to use
#SBATCH --mem-per-cpu=1000M # memory per CPU (in MB)
# use a version <= 9.0.2
module load StdEnv/2016.4 # or StdEnv/2018.3
module load gurobi/9.0.2
# Create environment file in current directory setting the number of threads:
echo "Threads ${SLURM_CPUS_ON_NODE:-1}" > gurobi.env
gurobi.sh ${GUROBI_HOME}/examples/python/facility.py
Using Gurobi in Python virtual environments
Gurobi brings it's own version of Python but that one does not contain any 3rd-party Python packages except Gurobi. In order to use Gurobi together with popular Python
packages like NumPy, Matplotlib, Pandas and others, we need to create a virtual Python environment in which we can install both gurobipy
and e.g. pandas
.
Before we start, we need to decide which combination of versions for Gurobi and Python to use.
[name@server ~] $ module load gurobi/8.1.1
[name@server ~] $ cd $EBROOTGUROBI/lib
[name@server ~] $ ls -dF python*
python2.7/ python2.7_utf32/ python3.6_utf32/
python2.7_utf16/ python3.5_utf32/ python3.7_utf32/
[name@server ~] $ module load gurobi/9.0.1
[name@server ~] $ cd $EBROOTGUROBI/lib
[name@server ~] $ ls -dF python*
python2.7_utf16/ python3.5_utf32/ python3.7/ python3.8_utf32/
python2.7_utf32/ python3.6_utf32/ python3.7_utf32/
[name@server ~] $ cd
We see that gurobi/8.1.1
brings it's own installation of python2.7/
and Python packages for Python 2.7, 3.5, 3.6 and 3.7 (pythonX.Y_utf32/
),
while gurobi/9.0.1
by default uses python3.7/
and brings Python packages for Python 2.7, 3.5, 3.6, 3.7 and 3.8 (pythonX.Y_utf32/
).
In this example we want to create a Python environment based on python/3.7
in which we want to use gurobi/9.0.1
and install the Pandas package.
Creating a Python virtual environments with Gurobi
These steps need to be done only once per system.
The first step is to load the modules to create the virtual environment and activate it.
[name@server ~] $ module load gurobi/9.0.1 python/3.7
[name@server ~] $ virtualenv --no-download ~/env_gurobi
Using base prefix '/cvmfs/soft.computecanada.ca/easybuild/software/2017/Core/python/3.7.4'
New python executable in /home/name/env_gurobi/bin/python
Installing setuptools, pip, wheel...
done.
[name@server ~] $ source ~/env_gurobi/bin/activate
Now that the environment has been activated we can install the Python packages we want to use, in this case pandas
.
The third step is to install gurobipy into the environment:
(env_gurobi) [name@server ~] $ cd $EBROOTGUROBI
(env_gurobi) [name@server ~] $ python setup.py build --build-base /tmp/${USER} install
running build
running build_py
creating /tmp/name
creating /tmp/name/lib
creating /tmp/name/lib/gurobipy
copying lib/python3.7_utf32/gurobipy/__init__.py -> /tmp/name/lib/gurobipy
copying lib/python3.7_utf32/gurobipy/gurobipy.so -> /tmp/name/lib/gurobipy
running install
running install_lib
creating /home/name/env_gurobi/lib/python3.7/site-packages/gurobipy
copying /tmp/name/lib/gurobipy/gurobipy.so -> /home/name/env_gurobi/lib/python3.7/site-packages/gurobipy
copying /tmp/name/lib/gurobipy/__init__.py -> /home/name/env_gurobi/lib/python3.7/site-packages/gurobipy
byte-compiling /home/name/env_gurobi/lib/python3.7/site-packages/gurobipy/__init__.py to __init__.cpython-37.pyc
running install_egg_info
Writing /home/name/env_gurobi/lib/python3.7/site-packages/gurobipy-9.0.1-py3.7.egg-info
(env_gurobi) [name@server ~] $ cd
Using the Gurobi-enabled virtual environment
Python scripts can now import both Pandas and Gurobi:
import pandas as pd import numpy as np import gurobipy as gurobi from gurobipy import * # [...]
Once created we can activate Gurobi and the environment with:
module load gurobi/9.0.1 source ~/env_gurobi/bin/activate python my_gurobi_script.py
Note that we now use python
instead of gurobi.sh
!
And this is an example job script that we can use:
#!/bin/bash
#SBATCH --time=0-00:30 # time limit (D-HH:MM)
#SBATCH --cpus-per-task=1 # number of CPUs (threads) to use
#SBATCH --mem-per-cpu=1000M # memory per CPU (in MB)
module load StdEnv/2016.4
module load gurobi/9.0.1
source ~/env_gurobi/bin/activate
# Create environment file in current directory setting the number of threads:
echo "Threads ${SLURM_CPUS_ON_NODE:-1}" > gurobi.env
python my_gurobi_script.py
Cite Gurobi
Please see How do I cite Gurobi software for an academic publication?