Installing Python and Gurobi
Configuring Python
Create workspace:
$ export PROJECT_PATH="$HOME/project" # You can change the path if you prefer
$ mkdir -p $PROJECT_PATH # -p creates parent directories as needed
The export
command tells the shell to make the contents of PROJECT_PATH
available to child processes of this shell.
Check and install pip:
$ python3 -m pip3 --version
$ python3 -m pip install --user -U pip # -U == --upgrade
[...]
Successfully installed pip-21.1.3
Creating an isolated environment
Install virtual environments to work on different projects without conflicting library version.
- Remember to use
sudo
to install inroot
instead of user. - If you want to access the system’s site packages, use
--system-site-packages
option.
python3 -m pip install --user -U virtualenv
Output:
Collecting virtualenv
[...]
Successfully installed virtualenv
Create isolated Python environment
cd $PROJECT_PATH
virtualenv my_env
Output:
Using base prefix '[...]'
New python executable in [...]/project/my_env/bin/python3.6
Also creating executable in [...]/project/my_env/bin/python
Installing setuptools, pip, wheel...done.
To activate the environment my_env
type:
cd $PROJECT_PATH
source my_env/bin/activate
source
reads and executes the content of a file (generally set of commands).
To deactivate a virtual environment type:
deactivate
Installing modules
Install all data science modules:
python3 -m pip install -U jupyter matplotlib numpy pandas scipy scikit-learn
Install Gurobi:
python3 -m pip install -U gurobipy
Installing Gurobi license
Install your license using the command:
grbgetkey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Find your user code at your user page: https://www.gurobi.com/downloads/licenses/.
Open the .bashrc
file at your home directory:
nano .bashrc
Add to the end of ~/.bashrc
the path of your Gurobi license (e.g., ~/gurobi.lic
):
export GRB_LICENSE_FILE=~/gurobi.lic
Save .bashrc
file (using nano
type: Ctrl+X
, y
, and Enter
to write in ~/bashrc
).
Check the variable GRB_LICENSE_FILE
:
cat $GRB_LICENSE_FILE
You should see something like:
# DO NOT EDIT THIS FILE except as noted
#
# License ID 663205
# Gurobi license for TU Delft
ORGANIZATION=TU Delft
TYPE=ACADEMIC
VERSION=9
HOSTNAME=mtt-mme
HOSTID=ef04c6a9
SOCKETS=2
USERNAME=testuser
EXPIRATION=2022-07-02
KEY=Z4RXY094
CKEY=Z4RXY094
Re-enter the terminal to update the changes. Test if Gurobi is working:
gurubi.sh
You should see something like:
Python 3.7.4 (default, Oct 29 2019, 10:15:53)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
Academic license - for non-commercial use only - expires 2022-07-02
Using license file /home/testuser/gurobi.lic
Set parameter LogFile to value gurobi.log
Gurobi Interactive Shell (linux64), Version 9.1.2
Copyright (c) 2021, Gurobi Optimization, LLC
Type "help()" for help
gurobi>
Test connection with gurobipy
Create a file to save the mip model:
nano mip1.py
And paste the following example (see more examples):
#!/usr/bin/env python3.8
# Copyright 2021, Gurobi Optimization, LLC
# This example formulates and solves the following simple MIP model:
# maximize
# x + y + 2 z
# subject to
# x + 2 y + 3 z <= 4
# x + y >= 1
# x, y, z binary
import gurobipy as gp
from gurobipy import GRB
try:
# Create a new model
m = gp.Model("mip1")
# Create variables
x = m.addVar(vtype=GRB.BINARY, name="x")
y = m.addVar(vtype=GRB.BINARY, name="y")
z = m.addVar(vtype=GRB.BINARY, name="z")
# Set objective
m.setObjective(x + y + 2 * z, GRB.MAXIMIZE)
# Add constraint: x + 2 y + 3 z <= 4
m.addConstr(x + 2 * y + 3 * z <= 4, "c0")
# Add constraint: x + y >= 1
m.addConstr(x + y >= 1, "c1")
# Optimize model
m.optimize()
for v in m.getVars():
print('%s %g' % (v.varName, v.x))
print('Obj: %g' % m.objVal)
except gp.GurobiError as e:
print('Error code ' + str(e.errno) + ': ' + str(e))
except AttributeError:
print('Encountered an attribute error')
Run the file:
python3 mip1.py
The result should be:
Academic license - for non-commercial use only - expires 2022-07-02
Using license file /home/testuser/gurobi.lic
Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (linux64)
Thread count: 32 physical cores, 32 logical processors, using up to 32 threads
Optimize a model with 2 rows, 3 columns and 5 nonzeros
Model fingerprint: 0x98886187
Variable types: 0 continuous, 3 integer (3 binary)
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [1e+00, 2e+00]
Bounds range [1e+00, 1e+00]
RHS range [1e+00, 4e+00]
Found heuristic solution: objective 2.0000000
Presolve removed 2 rows and 3 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Explored 0 nodes (0 simplex iterations) in 0.00 seconds
Thread count was 1 (of 32 available processors)
Solution count 2: 3 2
Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
x 1
y 0
z 1
Obj: 3