Package: sage-numerical-backends-gurobi
sage-numerical-backends-gurobi: Gurobi mixed integer linear programming backend for SageMathGurobiBackendhas previously been available as part of theSageMathsource tree,
from which it is built as an "optional extension" if the proprietary Gurobi library and header files have been symlinked into$SAGE_LOCALmanually.Because of the proprietary nature of the Gurobi software,GurobiBackendis not available in any binary distributions of SageMath.The present standalone Python packagesage-numerical-backends-gurobihas been created from the SageMath sources, version 9.0.beta10; the in-tree version ofGurobiBackendhas been removed inSage ticket #28175.  The present package can be installed on top of various Sage installations usingpip, including older versions of Sage such as 8.1 (as shipped by Ubuntu bionic 18.04LTS).Installation of GurobiInstall Gurobi according to the instructions on the website,
which includes obtaining a license key.On a Linux system, after unpacking the Gurobi archive in the desired location,
such as/opt, set the environment variableGUROBI_HOMEto the directory containing the subdirectoriesbin,lib, ...:$ export GUROBI_HOME=/opt/gurobi900/linux64Then adjust yourPATH(or create symbolic links) so that the interactive Gurobi shellgurobi.shcan be found from yourPATH:$ export PATH="$GUROBI_HOME/bin:$PATH"On macOS, the Gurobi installer should make the interactive Gurobi shellgurobi.shavailable in/usr/local/binand therefore from yourPATH.Verify this by typing the shell commandgurobi.sh::$ gurobi.sh
Python 3.7.4 (default, Aug 27 2019, 11:27:39)
...
Gurobi Interactive Shell (mac64), Version 9.0.0
Copyright (c) 2019, Gurobi Optimization, LLC
Type "help()" for help
gurobi>If this does not work, adjust yourPATH(or create symbolic links) so
thatgurobi.shis found.Installation of this packageThis package finds the Gurobi installation using theGUROBI_HOMEenvironment variable.  (On macOS, it suffices to havegurobi.shin yourPATH.)
An alternative method of build configuration is to set compiler/linker flags appropriately.InSageMath 9.1 and newer, this package is available as an optional SPKG and can be installed using$ sage -i sage_numerical_backends_gurobiAlternatively, you can install this package from PyPI using$ sage -python -m pip install sage-numerical-backends-gurobior from a checked out source tree using$ sage -python -m pip install .or from GitHub using$ sage -python -m pip install git+https://github.com/sagemath/sage-numerical-backends-gurobi(Seebuild.ymlin the related package sage-numerical-backends-coin packagefor details about package prerequisites on various systems.)Using this packageTo obtain a solver (backend) instance:sage: from sage_numerical_backends_gurobi.gurobi_backend import GurobiBackend
sage: GurobiBackend()
<sage_numerical_backends_gurobi.gurobi_backend.GurobiBackend object at 0x7fb72c2c7528>Equivalently:sage: from sage_numerical_backends_gurobi.gurobi_backend import GurobiBackend
sage: from sage.numerical.backends.generic_backend import get_solver
sage: get_solver(solver=GurobiBackend)
<sage_numerical_backends_gurobi.gurobi_backend.GurobiBackend object at 0x7fe21ffbe2b8>To use this solver (backend) withMixedIntegerLinearProgram:sage: from sage_numerical_backends_gurobi.gurobi_backend import GurobiBackend
sage: M = MixedIntegerLinearProgram(solver=GurobiBackend)
sage: M.get_backend()
<sage_numerical_backends_gurobi.gurobi_backend.GurobiBackend object at 0x7fb72c2c7868>To make it available as the solver named'Gurobi', we need to make the new module
known assage.numerical.backends.gurobi_backend(note dots, not underscores), using
the following commands:sage: import sage_numerical_backends_gurobi.gurobi_backend as gurobi_backend, sage.numerical.backends as backends, sys
sage: sys.modules['sage.numerical.backends.gurobi_backend'] = backends.gurobi_backend = gurobi_backendIf these commands are executed in a Sage session before anyMixedIntegerLinearProgramis created, then
the new'Gurobi'solver wins over the'GLPK'solver in the selection of the default MIP backend.
To select the'Gurobi'solver explicitly as the default MIP backend, additionally use the following command.sage: default_mip_solver('Gurobi')To make these settings permanent, add the above 2 + 1 commands to your~/.sage/init.sagefile.
Note that this setting will not affect doctesting (sage -t) because this file is ignored in doctesting mode.Running doctestsTo run the (limited) testsuite of this package, use:$ sage setup.py testIf no Gurobi license is available, the testing is skipped without error.To run the Sage testsuite with the default MIP solver set to the backend provided by this package, use:$ sage setup.py check_sage_testsuiteIf no Gurobi license is available, the testing is skipped without error.Running tests with toxThe doctests can also be invoked usingtox:$ tox -e local
$ tox -e local check_sage_testsuite.pyTesting multiple installed Gurobi versions in parallel (seetox.ini):$ tox -p autoOverriding the default solver by patching the Sage installationAnother method is to patch the module in permanently to the sage installation (at your own risk).
This method will affect doctesting.$ sage -c 'import os; import sage.numerical.backends as dm; import sage_numerical_backends_gurobi.gurobi_backend as sm; s = sm.__file__; f = os.path.basename(s); d = os.path.join(dm.__path__[0], f); (os.path.exists(d) or os.path.lexists(d)) and os.remove(d); os.symlink(s, d);'Or use the scriptpatch_into_sage_module.pyin the source distribution that does the same:$ sage -c 'load("patch_into_sage_module.py")'
Success: Patched in the module as sage.numerical.backends.gurobi_backendVerify withcheck_get_solver_with_name.pythat the patching script has worked:$ sage -c 'load("check_get_solver_with_name.py")'
Success: get_solver(solver='gurobi') gives <sage_numerical_backends_gurobi.gurobi_backend.GurobiBackend object at 0x7f8f20218528>