Message Boards Message Boards

GurobiLink: an interface to Gurobi numerical optimization solver

Posted 2 years ago

GitHub repository: https://github.com/WolframResearch/GurobiLink

Since version 12.2 Wolfram Language users can solve their optimization problems with the Gurobi Optimizer, by using the built-in Method -> "GUROBI" option. When you select this option, your optimization problem is formulated with GurobiLink into a form that the Gurobi Optimizer understands. To allow users to understand and improve this GurobiLink package we have made it available on GitHub. The following text is from its README file:

GurobiLink is a package implemented in Wolfram Language and C++ using LibraryLink that provides an interface to the Gurobi numerical optimization solver. Gurobi can be used with the same ease as the built-in solvers and has excellent performance for particularly large or complicated (e.g. mixed-integer) models. It supports continuous and mixed-integer linear programming (LP), quadratic programming (QP), quadratically-constrained programming (QCP) and other classes of problems.

The package requires a Gurobi license (free academic licenses and evaluation licenses for commercial users are available, please see the Gurobi documentation for license installation instructions). It works with Gurobi version 9.0 and later and is bundled with Wolfram Language products (such as Mathematica or Wolfram Desktop) version 12.2 and later.

GurobiLink makes the solver accessible as a plug-in through the Wolfram Language optimization method framework and is used to implement Method -> "Gurobi" in functons like NMinimize or ConvexOptimization. This allows seamless integration of the Wolfram Language modeling capabilities with the high performance of Gurobi, for example

In[1]:= ConvexOptimization[Total[x], Norm[x] <= 1, Element[x, Vectors[10, Integers]],
            Method -> "Gurobi"] // AbsoluteTiming

Out[1]= {0.004534, {x -> {0, 0, -1, 0, 0, 0, 0, 0, 0, 0}}}

How to build

The build requires CMake 3.15 or later, a C++ compiler with support for C++11, as well as installations of Wolfram Language and the Gurobi Optimizer.

The general steps for building the complete paclet are

git clone https://github.com/WolframResearch/GurobiLink.git GurobiLink
cd GurobiLink
mkdir build
cd build
cmake <options> ..
cmake --build . --target install

which will place the result by default in the GurobiLink/build/install directory.

The typical CMake options required for building are

  • WOLFRAM_INSTALLATION -- Wolfram layout location, for example, /usr/local/Wolfram/Mathematica/12.3
  • GUROBI_VERSION -- version number, for example, 9.1.2
  • GUROBI_HOME -- where Gurobi is installed, for example, /opt/gurobi912/linux64

It may not always be necessary to specify all of these, since the build system provides reasonable defaults.

The built paclet can then be enabled in a particular Wolfram Language session using PacletDirectoryLoad or packed into a .paclet archive using CreatePacletArchive.

The .paclet format is suitable for distribution or permanent installation using PacletInstall.

Some platform-specific examples follow:

Windows

Create a build directory, e.g. C:\dev\git\Paclets\GurobiLink\build

cd C:\dev\git\Paclets\GurobiLink
mkdir build
cd build

Configure the build using the 64-bit Visual Studio 2017 generator (see cmake --help for other possible CMake generators)

cmake -G "Visual Studio 15 2017 Win64" ^ -DWOLFRAM_INSTALLATION="C:\Program Files\Wolfram Research\Mathematica\12.3"^ -DGUROBI_VERSION=9.1.2^ -DGUROBI_HOME="C:\gurobi912\win64"^ -DINSTALL_PDB=ON ^ ..

The generated solution can be opened in the Visual Studio IDE using

cmake --open .

To build, select the appropriate configuration (for example, Debug), right-click the desired target (for example, INSTALL) and choose 'Build' from the context menu.

Alternatively, to build the debug configuration from the command line use

cmake --build . --config Debug --target INSTALL

The development version of the paclet will be assembled in build\install and can be enabled in a Wolfram Language session by

PacletDirectoryLoad["C:\\dev\\git\\Paclets\\GurobiLink\\build\\install\\GurobiLink"];

Sanity-check the build by running the included basic test file:

TestReport["C:\\dev\\git\\Paclets\\GurobiLink\\Tests\\GurobiLink_Basic.wlt"]

macOS

Create a build directory, e.g. ~/git/Paclets/GurobiLink/build

cd ~/git/Paclets/GurobiLink
mkdir build
cd build

Configure the build using the default Unix Makefiles generator (see cmake --help for other possible CMake generators)

cmake -DWOLFRAM_INSTALLATION="/Applications/Mathematica12.3.app/Contents" \
      -DGUROBI_VERSION=9.1.2 \
      -DGUROBI_HOME=/Library/gurobi912/mac64
  ..

Build the debug configuration from the command line

cmake --build . --config Debug --target install

The development version of the paclet will be assembled in build/install and can be enabled in a Wolfram Language session by

PacletDirectoryLoad["~/git/Paclets/GurobiLink/build/install/GurobiLink"];

Sanity-check the build by running the included basic test file:

TestReport["~/git/Paclets/GurobiLink/Tests/GurobiLink_Basic.wlt"]

See also

POSTED BY: Arnoud Buzing
3 Replies

Thanks Arnoud for integrating Gurobi to Mathematica! However, I cannot make it work under my setup. I have Mathematica 13.3, Gurobi 11.0.0, have a valid academic Gurobi license, which the Gurobi shell recognizes. But Mathematica does not, and it keeps returning my optimization calls unevaluated, with an error message:

ConvexOptimization::opmlicwf: The method Gurobi cannot be used since a valid license could not be found. See get a license for Gurobi.

How to resolve this issue? The documentation does not help at all.

POSTED BY: Istvan Zachar

Note that this post came directly from the recently open sourced GurobiLink:

https://github.com/WolframResearch/GurobiLink

Since version 12.2 of the Wolfram Language, various optimization functions already support the Method->"GUROBI" without any hassle:

https://reference.wolfram.com/language/ref/method/Gurobi.html

The open sourcing of the code is intended for people who want to study and improve this functionality.

POSTED BY: Arnoud Buzing

enter image description here -- you have earned Featured Contributor Badge enter image description here Your exceptional post has been selected for our editorial column Staff Picks http://wolfr.am/StaffPicks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!

POSTED BY: Moderation Team
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract