Using Gurobi with R and Python

Using Gurobi with RStudio

  1. Register a Gurobi account as an academic user at https://pages.gurobi.com/registration . Please register as a student using your university email address and don’t lose the password.

  2. Request for a license at https://www.gurobi.com/downloads/end-user-license-agreement-academic/ and you will obtain a Gurobi key. Copy and paste the Gurobi key to the terminal.

  3. Go to https://www.gurobi.com/downloads/gurobi-optimizer-eula/ , read and accept the End User License Agreement. Then download the current version of Gurobi optimizer. Install the optimizer with default settings.

  4. Run the following R command in RStudio:

install.packages('<R-package-file>', repos=NULL)

install.packages('slam')

In <R-package-file>, put the directory of your Gurobi installation. (The default <R-package-file> for Gurobi 9.1.2 is /opt/gurobi912/linux64 for Linux, c:\gurobi912\win64 for 64-bit Windows, and /Library/gurobi912/mac64 for Mac). You should browse the <installdir>/R directory to find the exact name of the file for your platform (the Linux package is in file gurobi_9.1-2_R_4.0.2.tar.gz, the Windows package is in file gurobi_9.1-2.zip, and the Mac package is in file gurobi_9.1-2_R_4.0.2.tgz).

For example, on my machine, the first command looks like this:

install.packages('/Library/gurobi912/mac64/R/gurobi_9.1-2_R_4.0.2.tgz', repos=NULL)

  1. Now you should be able to solve linear programming problem with Gurobi in RStudio. Try running the following piece of code:

# This example formulates and solves the following simple LP model:

# maximize

# x + 2 y + 3 z

# subject to

# x + y <= 1

# y + z <= 1

library(Matrix)

library(gurobi)

M <- list()

M$A <- matrix(c(1,1,0,0,1,1), nrow=2, byrow=T)

M$obj <- c(1,2,3)

M$modelsense <- 'max'

M$rhs <- c(1,1)

M$sense <- c('<', '<')

result <- gurobi(M)

print(result$objval)

print(result$x)


Using Gurobi with Anaconda Python in Jupyter Notebook

Anaconda Python Installation

Python is a popular language for research computing, data science AND and general-purpose programming. We recommend using Anaconda, an all-in-one installer of python and many of it's most useful packages for scientific computing. Please make sure you install Python version 3.x (e.g., 3.6 is fine).

We can learn Python using the Jupyter notebook, a programming environment that runs in a web browser. For this to work you will need a reasonably up-to-date browser. The current versions of the Chrome, Safari and Firefox browsers are all supported (some older browsers, including Internet Explorer version 9 and below, are not).

Windows

  1. Open (https://www.anaconda.com/download/#windows) with your web browser.

  2. Download the Python 3 installer for Windows.

  3. Install Python 3 using all of the defaults for installation except make sure to check Add Anaconda to my PATH environment variable.

macOS

  1. Open (https://www.anaconda.com/download/#macos) with your web browser.

  2. Download the Python 3 installer for OS X.

  3. Install Python 3 using all of the defaults for installation.

Installing Gurobipy

  1. Register a Gurobi account as an academic user at https://pages.gurobi.com/registration . Please register as a student using your university email address and don’t lose the password.

  2. Go to https://anaconda.org/Gurobi/gurobi. Copy the command and paste it to your terminal. When you see Proceed ([y]/n)?, type y and press Enter

If that doesn't work (due to your geographical location), you may want to try pip install -i https://pypi.gurobi.com gurobipy

  1. Request for a license at https://www.gurobi.com/downloads/end-user-license-agreement-academic/ and you will obtain a Gurobi key. Copy and paste the Gurobi key to the terminal.

  2. Enter jupyter notebook at the terminal. Start a new notebook for this tutorial, rename it and save it in your folder.

  3. Try running the following code in your jupyter notebook:

import gurobipy as grb

M = grb.Model('my_model')

  1. If your code runs without an error message, then you have successfully installed the Gurobi for this unit.

  2. You can try solving the follwing problem:

import gurobipy as grb

import numpy as np

# This example formulates and solves the following simple LP model:

# maximize

# x + 2 y + 3 z

# subject to

# x + y <= 1

# y + z <= 1

M = grb.Model('my_model')

x = M.addMVar(3)

A = np.array([[1,1,0],[0,1,1]])

c = np.array([1,2,3])

b = np.array([1,1])

M.setObjective(c@x, grb.GRB.MAXIMIZE)

M.addConstr(A@x <= b)

M.optimize()

print(M.objVal)

print(x.x)


Note: Some Mac users may encounter an error from Jupyter Notebook saying the kernel is dead after running Step 5 in the installation guide. This is probably due to a memory access violation. To check if your machine encounters such problem, please type the following into the terminal line by line:

python

import gurobipy as grb


If the machine would spit out an error message “segmentation faulty:11”, then you can try the following steps to fix the problem:


  1. Copy and paste the following command into the terminal, please proceed with yes for all the changes when asked: conda config --add channels conda-forge

  2. Copy and paste the following command into the terminal, please proceed with yes for all the changes when asked: conda update -n base conda. Close the current terminal when done.

  3. Start a new terminal and copy and paste the following command into the terminal: conda update --all

  4. Type conda list into the terminal and you should see most of your packages are associated with a particular channel conda-forge

  5. Try Step 5 from above again.

Please come forward to us (during the tutorial or during consultation hours) if you need help with the Gurobi installation. You will need Gurobi for all of your assessments in this unit.

You may need to install a couple of popular packages in this new environment such as numpy and pandas. But they can be easily installed with one line in the terminal. Google the package that you need and look for the installation command that starts with conda install ...