Gurobi: Difference between revisions

2,349 bytes added ,  6 months ago
m
no edit summary
mNo edit summary
mNo edit summary
Line 153: Line 153:
  python3.11
  python3.11


<!--T:95-->
=== HowTo Install Gurobi for Python ===
We see that <code>gurobi/8.1.1</code> brings its 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>).  While <code>gurobi/11.0.1</code> by comparison only brings Python <code>3.11</code> along.


=== Creating the virtual environment === <!--T:10-->
As explained at the bottom of [[https://support.gurobi.com/hc/en-us/articles/360044290292-How-do-I-install-Gurobi-for-Python|How-do-I-install-Gurobi-for-Python]] the previous recommended approach for installing gurobi for python with setup.py has been deprecated as of version Gurobi 11.  For continuity a modified version of the instruction (usable under StdEnv/2023) is provided in the following section for Gurobi 10 and older.  Following this a new section is provided showing how to install gurobi for python from a wheel for version Gurobi 11 and greater.
 
=== Gurobi Versions 10.0.3 (and older) === <!--T:10-->


<!--T:97-->
<!--T:97-->
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.  The following steps need to be done only once per system.  First load the modules to [[Python#Creating_and_using_a_virtual_environment|create the virtual environment]] and activate it:
The following steps need to be done once per system and are usable with StdEnv/2023 and older.  First load the modules to [[Python#Creating_and_using_a_virtual_environment|create the virtual environment]] and activate it:


<!--T:105-->
<!--T:105-->
{{Commands|prompt=[name@server ~] $
{{Commands|prompt=[name@server ~] $
| module load gurobi/9.0.1 python/3.7
| module load gurobi/10.0.3 python
| virtualenv --no-download  ~/env_gurobi
| 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
| source ~/env_gurobi/bin/activate
}}
}}
Now that the environment has been activated, install the Python packages we want to use (in this case <code>pandas</code>) for example:
Now install any Python packages we want to use (in this case <code>pandas</code>) for example:


<!--T:110-->
<!--T:110-->
{{Commands|prompt=(env_gurobi) [name@server ~] $
{{Commands|prompt=(env_gurobi) [name@server ~] $
|  pip install --no-index  pandas
|  pip install --no-index  pandas
  Ignoring pip: markers 'python_version < "3"' don't match your environment
  Looking in links:
  /cvmfs/soft.computecanada.ca/custom/python/wheelhouse/nix/avx2
  /cvmfs/soft.computecanada.ca/custom/python/wheelhouse/nix/generic
  /cvmfs/soft.computecanada.ca/custom/python/wheelhouse/generic
  Collecting pandas,
  Collecting numpy>1.13.3 (from pandas)
  [...]
  Successfully installed numpy-1.18.4 pandas-1.0.3 python-dateutil-2.8.1 pytz-2020.1 six-1.15.0
}}
}}
The third and final step is to install gurobipy into the environment:
Next install gurobipy into the environment.  Note that as of StdEnv/2023 the install can no longer be done under $EBROOTGUROBI as previously documented using the command <code>python setup.py build --build-base /tmp/${USER} install</code> since a fatal <code>error: could not create 'gurobipy.egg-info': Read-only file system</code> crash message will occur.  Instead the required files need to copied to elsewhere (such as /tmp/$USER) and installed from there, for example:
 
<!--T:113-->
{{Commands|prompt=(env_gurobi) [name@server ~] $
| mkdir /tmp/$USER
| cp -r $EBROOTGUROBI/{lib,setup.py} /tmp/$USER
| cd /tmp/$USER
| python setup.py install
(env_gurobi) [roberpj@gra-login1:/tmp/roberpj] python setup.py install
/home/roberpj/env_gurobi/lib/python3.11/site-packages/setuptools/_core_metadata.py:158: SetuptoolsDeprecationWarning: Invalid config.
!!
 
        ********************************************************************************
        newlines are not allowed in `summary` and will break in the future
        ********************************************************************************
 
!!
  write_field('Summary', single_line(summary))
removing /tmp/roberpj/build
(env_gurobi) [roberpj@gra-login1:/tmp/roberpj]
}}
=== Gurobi Versions 11.0.0 (and newer) === <!--T:11-->
 
<!--T:97-->
Once again, following steps need to be done once per system and are usable with StdEnv/2023 and older.  First load the modules to [[Python#Creating_and_using_a_virtual_environment|create the virtual environment]] and activate it.  Version 11.0.0 is skipped since it has been observed to seg fault in at least one example versus Version 11.0.1 which runs smoothly.
 
<!--T:105-->
{{Commands|prompt=[name@server ~] $
| module load gurobi/11.0.1 python
| virtualenv --no-download  ~/env_gurobi
| source ~/env_gurobi/bin/activate
}}
As before, install any needed Python packages (in this case <code>pandas</code>) for example:
 
<!--T:110-->
{{Commands|prompt=(env_gurobi) [name@server ~] $
|  pip install --no-index  pandas
}}
Next install gurobipy into the environment. As mentioned above and in [[https://support.gurobi.com/hc/en-us/articles/360044290292-How-do-I-install-Gurobi-for-Python|this article]] the use of setup.py to install Gurobi for python is deprecated starting with Gurobi 11.  Both pip and conda are given as alternatives, however since conda should not be used on alliance systems, the pip approach will be demonstrated here.
 
The installation is slightly complicated since Alliance linux systems are setup with gentoo prefix.  As a result neither A) the recommended command to download and install the gurobipy extension from the public PyPI server <code>pip install gurobipy==11.0.1</code> mentioned in the article line or B) the offline command to install the wheel with <code>python -m pip install --find-links <wheel-dir> --no-index gurobipy</code>, will work.  Instead a script available from the alliance maybe utilized to download and modify the existing wheel into an usable format under a new name.  There is one caveat which is for each new Gurobi version the researcher must go into https://pypi.org/project/gurobipy/11.0.1/#history, click on the desired version followed by the <code>Download files</code> button in the left hand menu and finally copy the https link for the wheel file named gurobipy-11.0.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl in the case of Gurobi 11.0.1 and paste its link as the --url argument as shown in the following:


<!--T:112-->
<!--T:112-->
{{Commands|prompt=(env_gurobi) [name@server ~] $
{{Commands|prompt=(env_gurobi) [name@server ~] $
| cd $EBROOTGUROBI
| wget https://github.com/ComputeCanada/wheels_builder/blob/main/unmanylinuxize.sh
| python setup.py build --build-base /tmp/${USER} install
| ./unmanylinuxize.sh --package gurobipy --version 11.0.1 --url
  running build
https://files.pythonhosted.org/packages/c5/c6/7cb959ea454c58169ecc6d165c8e3e915769d963df1bab61f5c5bc0052e6/
  running build_py
gurobipy-11.0.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
  creating /tmp/name
'gurobipy-11.0.1-cp312-cp312-linux_x86_64.whl' -> '../gurobipy-11.0.1-cp312-cp312-linux_x86_64.whl'
  creating /tmp/name/lib
| python -m pip install --find-links ~/pythonwheels --no-index gurobipy}}
  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
}}


=== Using the virtual environment === <!--T:120-->  
=== Using the virtual environment === <!--T:120-->  
Line 229: Line 245:
<!--T:203-->
<!--T:203-->
{{Commands|prompt=[name@server ~] $
{{Commands|prompt=[name@server ~] $
| module load gurobi/9.0.1
| module load gurobi/11.0.1
   source ~/env_gurobi/bin/activate
   source ~/env_gurobi/bin/activate
   (env_gurobi) python my_gurobi_script.py
   (env_gurobi) python my_gurobi_script.py
Line 244: Line 260:
#!/bin/bash
#!/bin/bash
#SBATCH --time=0-00:30        # time limit (D-HH:MM)
#SBATCH --time=0-00:30        # time limit (D-HH:MM)
#SBATCH --cpus-per-task=1     # number of CPUs to use
#SBATCH --cpus-per-task=4     # number of CPUs to use
#SBATCH --mem-per-cpu=1000M  # memory per CPU (in MB)
#SBATCH --mem-per-cpu=1000M  # memory per CPU (in MB)


<!--T:205-->
<!--T:205-->
module load StdEnv/2016.4
module load StdEnv/2023
module load gurobi/9.0.1
module load gurobi/11.0.1


<!--T:206-->
<!--T:206-->
cc_staff
1,857

edits