In this post I’ll show you the steps you need to take, if you want to use the powerful GUROBI solver from Matlab in order to solve Mixed Integer Linear Programs.
Install the GUROBI Solver
Go to the Gurobi website, create an account and download the version applicable for your operating system here.
Acquire and install a GUROBI License
, if you don’t yet have one and apply for a license. After having acquired a license, navigate to ‘Downloads & Licenses’, download the license file and install it, where Gurobi is looking for. Gurobi will first check your home directory which takes precedence, then one of the following locations:
- C:\gurobi\ on Windows
- /opt/gurobi/ on Linux
- /Library/gurobi/ on Mac OS X
You can also define an environment variable named GRB_LICENSE_FILE
to point to the license file.
Get an Academic License
In case you’re eligible to acquire an academic license – i.e. if you’re working for a university – perform the following steps. To get your fee academic license make sure you open the following website when connected to your university campus network or connect to your school network via VPN. Then open this site here. There you’ll be presented something like the following page:
Copy the grbgetkey command together with the license-hash-key into your clipboard, open a console / shell window, paste the command with the key and hit ENTER.
H:\>grbgetkey *******-*****-******-*******
info : grbgetkey version 9.1.0, build v9.1.0rc0
info : Contacting Gurobi license server...
info : License file for license ID ****** was successfully retrieved
info : License expires at the end of the day on 2021-01-03
info : Saving license file...
In which directory would you like to store the Gurobi license file?
[hit Enter to store it in C:\Users\LEN]:
A license file already exists in 'C:\Users\LEN\gurobi.lic'
Continue? [Y/n] Y
info : License ****** written to file C:\Users\LEN\gurobi.lic
If you’ve already got an old license installed, you’ll get a warning and will be asked, whether you want to replace the existing license with the new academic license. If that’s what you intend to do, confirm with ‘Y’.
Enable Matlab to use GUROBI
Now launch your Matlab Environment. In the Code Editor hit Ctrl-O to open the File-Open Dialog and navigate to the location, you just installed Gurobi to. In my case this is ‘D:\gurobi910’. Look for a os-Folder containing a matlab folder, containing a script named ‘gurobi_setup.m’. In my case this is here:
D:\gurobi910\win64\matlab
On Linux or OS X you might have to check /opt/gurobi/ or /Library/gurobi/, respectively.
After having selected the ‘gurobi_setup.m’ script in the file-open dialog, press from the Matlab Navigation bar and confirm to change to the current folder. In the Matlab Console you’ll get the following output:
>> gurobi_setup
The MATLAB interface for Gurobi 9.1.0 has been installed.
The directory
D:\gurobi910\win64\matlab\
has been added to the MATLAB path.
To use Gurobi regularly, you must save this new path definition.
To do this, type the command
savepath
at the MATLAB prompt. Please consult the MATLAB documentation
if necessary.
Now call the ‘savepath’ command from the Matlab console in order to add the path indicated to the matlab path collection.
Now your Matlab installation is able to handle gurobi calls. Let’s test it with a linear program example script provided by GUROBI here on the Gurobi website or here in my gist.
Open the script diet.m in Matlab and again choose the run-button in order to launch it and confirm changing to the required folder.
If you got everything right, Matlab would then pass the linear model to your Gurobi solver, solve it and provide the solution in the Matlab console. This should look something like this:
>> diet
Academic license - for non-commercial use only - expires 2021-01-03
Gurobi Optimizer version 9.1.0 build v9.1.0rc0 (win64)
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 4 rows, 13 columns and 40 nonzeros
Model fingerprint: 0xd7d456fd
Coefficient statistics:
Matrix range [1e+00, 2e+03]
Objective range [9e-01, 3e+00]
Bounds range [7e+01, 2e+03]
RHS range [0e+00, 0e+00]
Presolve removed 0 rows and 3 columns
Presolve time: 0.02s
Presolved: 4 rows, 10 columns, 37 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 0.0000000e+00 1.472500e+02 0.000000e+00 0s
4 1.1828861e+01 0.000000e+00 0.000000e+00 0s
Solved in 4 iterations and 0.06 seconds
Optimal objective 1.182886111e+01
Cost: 11.828861
Buy:
hamburger 0.604514
milk 6.97014
ice cream 2.59132
Nutrition:
calories 1800
protein 91
fat 59.0559
sodium 1779
Adding constraint at most 6 servings of dairy
Academic license - for non-commercial use only - expires 2021-01-03
Gurobi Optimizer version 9.1.0 build v9.1.0rc0 (win64)
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 5 rows, 13 columns and 42 nonzeros
Model fingerprint: 0x719a7d95
Coefficient statistics:
Matrix range [1e+00, 2e+03]
Objective range [9e-01, 3e+00]
Bounds range [7e+01, 2e+03]
RHS range [6e+00, 6e+00]
Presolve removed 0 rows and 3 columns
Presolve time: 0.10s
Presolved: 5 rows, 10 columns, 39 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 0.0000000e+00 1.472500e+02 0.000000e+00 0s
Solved in 3 iterations and 0.13 seconds
Infeasible model
No solution
>>
Congratulation! You just managed to use the GUROBI solver from your Matlab script!
There are different ways to hand over a model to GUROBI using Matlab. You’ll find detailled resources here on the website of GUROBI.