**Description:** This notebook is an introduction to JuMP. The topics described are as follows:

- Installing Julia and JuMP
- Representing vectors in Julia
- Structure of a JuMP model
- Solving general purpose linear programming problem
- Solving general purpose integer programming problem

**Author:** Shuvomoy Das Gupta

**License:**

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Julia is a new programming language.

- Free and open-source
- Syntax similar to MATLAB, speed similar to C

JuMP is a modelling language for mathematical optimization [1]. It is embedded in Julia.

- Very user-friendly
- Speed similar to special purpose commercial modelling language like AMPL
- Solver independent code: same code will run for both commercial and open-source solvers
- Very easy to implement solver callback and problem modification

Go to the download page of Julia, download the appropriate installer and run it. Now you can start an interactive Julia session!

IJulia will allow us to create powerful graphical notebooks, which is very convenient for the tutorial. We can insert codes, text, mathematical formulas and multimedia etc. in the same notebook. To install IJulia:

Install Anaconda from http://continuum.io/downloads. If you are on Windows, then while running the Anaconda installer please check the options

*Add Anaconda to the System Path*and also*Register Anaconda as default Python version of the system*.Now start a Julia interactive session. Type the following

`Pkg.add("IJulia")`

To open a new notebook

- In the Julia interactive session, run
`Using IJulia`

and then`notebook()`

. It will open the Home for IJulia in your web browser.

- The directory location can be checked by the command
`pwd()`

. If you want to change the directory to something else, the before running`Using IJulia`

and`notebook()`

, run`cd(path to your preferred directory)`

, e.g.,`cd("E:\\Dropbox\\Julia_Workspaces")`

. - Click on
*New Notebook*on the top right corner. In the new notebook, you can execute any Julia command pressing SHIFT+ENTER.

- In the Julia interactive session, run

At first add the JuMP package by running the following code in the notebook:

In [2]:

```
Pkg.add("JuMP")
```

We need to install a solver package. Let's install the open source solvers GLPK, Cbc and Clp by typing in `Pkg.add("GLPKMathProgInterface")`

, `Pkg.add("Cbc")`

and `Pkg.add("Clp")`

respectively. Let's add the Julia package associated with CPLEX by typing in `Pkg.add("CPLEX")`

. The other choices are `"CPLEX"`

, `"Cbc"`

, `"Clp"`

, `"Gurobi"`

and `"MOSEK"`

.

It should be noted that, in order to use commercial solvers such as CPLEX, Gurobi and Mosek in JuMP, we will require working installations of them with appropriate licences. Both Gurobi and Mosek are free for academic use. CPLEX is free for faculty members and graduate teaching assistants.

In [2]:

```
Pkg.add("GLPKMathProgInterface")
```

In [3]:

```
Pkg.add("Cbc")
```

In [4]:

```
Pkg.add("Clp")
```

In [5]:

```
Pkg.add("CPLEX") # Working installation of CPLEX is needed in advance
```

In [6]:

```
Pkg.add("Gurobi") # Working installation of Gurobi is needed in advance
```

If you have not updated your Julia packages in a while, a good idea might be updating them.

In [7]:

```
Pkg.update()
```

In [13]:

```
println("Hello World!")
```

At first let us try to solve a very simple and trivial optimization problem using JuMP to check if everything is working properly.

\begin{align} \text{minimize} \qquad & x+y \\ \text{subject to} \quad \quad & x+y \leq 1 \\ \qquad \qquad & x \geq 0, y \geq 0 \\ \qquad \qquad & x,y \in \mathbb{R} \end{align}Here is the JuMP code to solve the mentioned problem:

In [3]:

```
using JuMP # Need to say it whenever we use JuMP
using GLPKMathProgInterface # Loading the GLPK module for using its solver
#MODEL CONSTRUCTION
#--------------------
myModel = Model(solver=GLPKSolverLP())
# Name of the model object. All constraints and variables of an optimization problem are associated
# with a particular model object. The name of the model object does not have to be myModel, it can be yourModel too! The argument of Model,
# solver=GLPKsolverLP() means that to solve the optimization problem we will use GLPK solver.
#VARIABLES
#---------
# A variable is modelled using @defVar(name of the model object, variable name and bound, variable type)
# Bound can be lower bound, upper bound or both. If no variable type is defined, then it is treated as
#real. For binary variable write Bin and for integer use Int.
@defVar(myModel, x >= 0) # Models x >=0
# Some possible variations:
# @defVar(myModel, x, Binary) # No bound on x present, but x is a binary variable now
# @defVar(myModel, x <= 10) # This one defines a variable with lower bound x <= 10
# @defVar(myModel, 0 <= x <= 10, Int) # This one has both lower and upper bound, and x is an integer
@defVar(myModel, y >= 0) # Models y >= 0
#OBJECTIVE
#---------
@setObjective(myModel, Min, x + y) # Sets the objective to be minimized. For maximization use Max
#CONSTRAINTS
#-----------
@addConstraint(myModel, x + y <= 1) # Adds the constraint x + y <= 1
#THE MODEL IN A HUMAN-READABLE FORMAT
#------------------------------------
println("The optimization problem to be solved is:")
print(myModel) # Shows the model constructed in a human-readable form
#SOLVE IT AND DISPLAY THE RESULTS
#--------------------------------
status = solve(myModel) # solves the model
println("Objective value: ", getObjectiveValue(myModel)) # getObjectiveValue(model_name) gives the optimum objective value
println("x = ", getValue(x)) # getValue(decision_variable) will give the optimum value of the associated decision variable
println("y = ", getValue(y))
```

This was certainly not the most exciting optimization problem to solve. This was for test purpose only. However, before going into the structure of a JuMP model, let us learn how to represent vectors in Julia.

A column vector, $y=(y_1, y_2, \ldots, y_n)= \begin{pmatrix} y_1 \\ y_2 \\ . \\ . \\ y_n \end{pmatrix} \in \mathbb{R}^n$ will be written in Julia as

`[y[1];y[2];...;y[n]]`

.For example to create column vector $\begin{pmatrix} 3 \\ 2.4 \\ 9.1 \end{pmatrix}$ use:

`[3; 2.4; 9.1]`

.

In [19]:

```
[3; 2.4; 9.1] # Column vector
```

Out[19]:

A row vector, $z=(z_1 \; z_2 \; \ldots \; z_n) \in \mathbb{R}^{1 \times n}$ will be written in Julia as

`[z[1] y[2]...z[n]]`

.For example to create row vector $(1.2 \; 3.5 \; 8.21)$ use:

`[1.2 3.5 8.21]`

.

In [11]:

```
[1.2 3.5 8.21] # Row vector
```

Out[11]:

- To create a $m \times n$ matrix

write:

`[A[1,1] A[1,2] A[1,3]... A[1,n];`

`... ;`

`A[m,1] A[m,2] ... A[m,n]]`

.

So the matrix

$$ A = \begin{pmatrix} 1 & 1 & 9 & 5 \\ 3 & 5 & 0 & 8 \\ 2 & 0 & 6 & 13 \end{pmatrix} $$

is represented in Julia as:

`A= [ 1 1 9 5; 3 5 0 8; 2 0 6 13 ]`

In [12]:

```
# Generating a matrix
A= [
1 1 9 5;
3 5 0 8;
2 0 6 13
]
```

Out[12]:

$A_{ij}$ can be accessed by `A[i,j]`

,the $i$th row of the matrix A is represented by `A[i,:]`

, the $j$th column of the matrix A is represented by `A[:,j]`

.

The size of a matrix $A$ can be determined by running the command `size(A)`

. If we write `numRows, numCols = size(A)`

, then `numRows`

and `numCols`

will contain the total number of rows and columns of A respectively.

In [13]:

```
numRows, numCols = size(A)
println(
"A has ", numRows, " rows and ", numCols, " columns \n",
"A[3,3] is ", A[3,3], "\n",
"The 3rd column of A is ", A[:,3], "\n",
"The 2nd row of A is ", A[2,:]
)
```

Suppose $x,y \in \mathbb{R}^n$. Then $x^T y =\sum_{i=1}^{n} {x_i y_i}$ is written as `dot(x,y)`

.

In [14]:

```
y=[1; 2; 3; 4]
x=[5; 6; 7; 8]
xTy=dot(x,y)
```

Out[14]:

Any JuMP model that describes an optimization problem must have four parts:

**Model Object**,**Variables**,**Objective**,**Constraints**.

Any instance of an optimization problem corresponds to a model object. This model object is associated with all the variables, constraints and objective of the instance. It is constructed using `modelName = Model(solver=`

*solver of our preference*`)`

. If no solver is specified, then `ClpSolver()`

and/or `CbcSolver()`

will be used by default. Here `modelName`

is any valid name. We will limit ourselves in the open source solvers such as:

- Linear Programming Solver:
`ClpSolver(), GLPKSolverLP()`

- Mixed Integer Programming Solver:
`GLPKSolverMIP() CbcSolver()`

In [2]:

```
using JuMP
myModel = Model() # ClpSolver() and/or CbcSolver() will be used based on the problem
```

Out[2]:

Variables are defined using `@defVar`

macro, which takes up to three input arguments. The *first* argument is the name of the model. Then the *second* argument contains the name of the variable, and a bound on the variable if it exists. The *third* argument is not needed if the variable is real. When the variable is binary or integer, then `Bin`

or `Int`

, respectively, is used in place of the third argument.

Suppose the model object is `myModel`

.

- To describe a variable $z \in \mathbb{R}$ such that $0 \leq z \leq 10$ write

In [4]:

```
@defVar(myModel, 0 <= z <= 10)
```

Out[4]:

- Now consider a decision variable $x \in \mathbb{R}^n$, and it has a bound $l \preceq x \preceq u$, where naturally $l, u \in \mathbb{R}^n$. For that we write

In [16]:

```
# INPUT DATA, CHANGE THEM TO YOUR REQUIREMENT
#-------------------------------------------
n = 10
l = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
u = [10; 11; 12; 13; 14; 15; 16; 17; 18; 19]
```

Out[16]:

In [17]:

```
# VARIABLE DEFINITION
# -------------------
@defVar(myModel, l[i] <= x[i=1:n] <= u [i])
```

Out[17]:

In [18]:

```
# INPUT DATA, CHANGE THEM TO YOUR REQUIREMENT
#-------------------------------------------
n = 4 # dimension of x
m = 3 # dimension of y
p = 2 # dimensin of z
a = [0; 1; 2]
b = [3; 4; 7]
```

Out[18]:

In [19]:

```
# VARIABLE DEFINITION
# -------------------
@defVar(myModel, x[i=1:n] >= 0)
```

Out[19]:

In [20]:

```
@defVar(myModel, a[i] <= y[i=1:m] <= b[i], Int)
```

Out[20]:

In [21]:

```
@defVar(myModel, z[i=1:p], Bin)
```

Out[21]:

Constraints are added by using `@addConstraint`

macro. The first argument is the model object the constraint is associated with, the second argument is the reference to that constraint and the third argument is the constraint description. The constraint reference comes handy when we want to manipulate the constraint later or access the dual variables associated with it. If no constraint reference is needed, then the second argument is the constraint description.

Let's give some examples on writing constraints in JuMP. Suppose the model name is `yourModel`

.

In [5]:

```
yourModel = Model()
```

Out[5]:

- Consider variables $x, y \in \mathbb{R}$ which are coupled by the constraints $5 x +3 y \leq 5$. We write this as

`@addConstraint(yourModel, 5*x + 3*y <= 5)`

Naturally,`x`

and`y`

have to be defined first using`@defVar`

macro.

In [6]:

```
@defVar(yourModel, x)
@defVar(yourModel, y)
@addConstraint(yourModel, 5*x + 3*y <= 5)
```

Out[6]:

`conRef1`

(it could be any valid name). Then the same constraint have to be written as:

`@addConstraint(yourModel, conRef1, 6*x + 4*y >= 5)`

When we would need the dual value after solving the problem we just write `println(getDual(conRef1))`

.

In [7]:

```
@addConstraint(yourModel, conRef1, 6*x + 4*y >= 5)
```

Out[7]:

- Consider a variable $x \in \mathbb{R}^4$, a coefficient vector $a=(1, -3, 5, -7)$ We want to write a constraint of the form $\sum_{i=1}^4{a_i x_i} \leq 3$. In JuMP we write:

In [8]:

```
a = [1; -3; 5; 7]
@defVar(yourModel, w[1:4])
@addConstraint(yourModel, sum{a[i]*w[i],i=1:4} <= 3)
```

Out[8]:

Objective is set using `@setObjective`

macro. It has three arguments. The first argument is as usual the model object. The second one is either `Max`

if we want to maximize the objective function, or `Min`

when we want to minimize. The last argument is the description of the objective which has similar syntax to that of constraint definition.

For the previous model, consider the decision variable $w \in \mathbb{R}^4$ and cost vector $c = (2, 3 , 4, 5)$. We want to minimize $c^T w$. In JuMP we would write:

In [10]:

```
c = [2; 3; 4; 5]
@setObjective(yourModel, Min, sum{c[i]*w[i],i=1:4})
```

Out[10]:

problem Let us try to write the JuMP code for the following standard form optimization problem:

$$ \begin{align} & \text{minimize} && c^T x \\ & \text{subject to} && A x = b \\ & && x \succeq 0 \\ & && x \in \mathbb{R}^n \end{align} $$where, $n = 4$, $c=(1, 3, 5, 2)$, $A = \begin{pmatrix} 1 & 1 & 9 & 5 \ 3 & 5 & 0 & 8 \ 2 & 0 & 6 & 13 \end{pmatrix}$ and $b=(7, 3, 5)$. The symbol $\succeq$ ($\preceq$) stands for element-wise greater (less) than or equal to.

Let us input different parts of the JuMP code one by one and see the corresponding outputs to detect if everything is okay. Of course we could input the whole code at once.

In [72]:

```
using JuMP # Need to say it whenever we use JuMP
using GLPKMathProgInterface # Loading the package for using the GLPK solver
```

In [74]:

```
#MODEL CONSTRUCTION
#------------------
sfLpModel = Model(solver=GLPKSolverLP()) # Name of the model object
```

Out[74]:

In [75]:

```
#INPUT DATA
#----------
c = [1; 3; 5; 2]
A= [
1 1 9 5;
3 5 0 8;
2 0 6 13
]
b = [7; 3; 5]
m, n = size (A) # m = number of rows of A, n = number of columns of A
```

Out[75]:

In [76]:

```
#VARIABLES
#---------
@defVar(sfLpModel, x[1:n] >= 0) # Models x >=0
```

Out[76]:

In [77]:

```
#CONSTRAINTS
#-----------
for i=1:m # for all rows do the following
@addConstraint(sfLpModel, sum{A[i,j]*x[j] , j=1:n} == b[i]) # the ith row
# of A*x is equal to the ith component of b
end # end of the for loop
```

In [78]:

```
#OBJECTIVE
#---------
@setObjective(sfLpModel, Min, sum{c[j]*x[j], j=1:n}) # minimize c'x
```

Out[78]:

In [79]:

```
#THE MODEL IN A HUMAN-READABLE FORMAT
#------------------------------------
println("The optimization problem to be solved is:")
print(sfLpModel) # Shows the model constructed in a human-readable form
```

In [80]:

```
status = solve(sfLpModel) # solves the model
```

Out[80]:

In [81]:

```
#SOLVE IT AND DISPLAY THE RESULTS
#--------------------------------
println("Objective value: ", getObjectiveValue(sfLpModel)) # getObjectiveValue(model_name) gives the optimum objective value
println("Optimal solution is x = \n", getValue(x)) # getValue(decision_variable) will give the optimum value
# of the associated decision variable
```

In [5]:

```
using JuMP
using GLPKMathProgInterface
#MODEL CONSTRUCTION
#------------------
sfLpModel = Model(solver=GLPKSolverLP())
#INPUT DATA
#----------
c = [1; 3; 5; 2]
A= [
1 1 9 5;
3 5 0 8;
2 0 6 13
]
b = [7; 3; 5]
m, n = size (A) # m = number of rows of A, n = number of columns of A
#VARIABLES
#---------
@defVar(sfLpModel, x[1:n] >= 0) # Models x >=0
#CONSTRAINTS
#-----------
for i=1:m # for all rows do the following
@addConstraint(sfLpModel, sum{A[i,j]*x[j] , j=1:n} == b[i]) # the ith row
# of A*x is equal to the ith component of b
end # end of the for loop
#OBJECTIVE
#---------
@setObjective(sfLpModel, Min, sum{c[j]*x[j], j=1:n}) # minimize c'x
#THE MODEL IN A HUMAN-READABLE FORMAT
#------------------------------------
println("The optimization problem to be solved is:")
print(sfLpModel) # Shows the model constructed in a human-readable form
@time begin
status = solve(sfLpModel) # solves the model
end
#SOLVE IT AND DISPLAY THE RESULTS
#--------------------------------
println("Objective value: ", getObjectiveValue(sfLpModel)) # getObjectiveValue(model_name) gives the optimum objective value
println("Optimal solution is x = \n", getValue(x)) # getValue(decision_variable) will give the optimum value
# of the associated decision variable
```

Let us try to write the JuMP code for the following standard form optimization problem:

$$ \begin{align} & \text{minimize} && c^T x + d^T y\\ & \text{subject to} && A x + B y= f \\ & && x \succeq 0, y \succeq 0 \\ & && x \in \mathbb{R}^n, y \in \mathbb{Z}^p \end{align} $$Here, $A \in \mathbb{R}^{m \times n}, B \in \mathbb{R}^{m \times p}, c \in \mathbb{R}^n, d \in \mathbb{R}^p, f \in \mathbb{R}^m$. The data were randomly generated. The symbol $\succeq$ ($\preceq$) stands for element-wise greater (less) than or equal to.

In [27]:

```
n = 5
p = 4
m = 3
A=
[0.7511 -0.1357 0.7955 -0.4567 0.1356
-0.6670 -0.3326 0.1657 -0.5519 -0.9367
1.5894 -0.1302 -0.4313 -0.4875 0.4179]
B=
[-0.09520 -0.28056 -1.33978 0.6506
-0.8581 -0.3518 1.2788 1.5114
-0.5925 1.3477 0.1589 0.03495]
c=[0.3468,0.8687,0.1200,0.5024,0.2884]
d=[0.2017,0.2712,0.4997,0.9238]
f = [0.1716,0.3610,0.0705]
```

Out[27]:

In [28]:

```
using JuMP
using GLPKMathProgInterface
sfMipModel = Model(solver = GLPKSolverMIP())
@defVar(sfMipModel, x[1:n] >=0)
@defVar(sfMipModel, y[1:p] >= 0, Int)
@setObjective(sfMipModel, Min, sum{c[i] * x[i], i in 1:n}+sum{d[i]*y[i], i in 1:p})
for i in [1:m]
@addConstraint(sfMipModel, sum{A[i,j]*x[j], j in [1:n]}+ sum{B[i,j]*y[j], j in [1:p]} == f[i])
end
print(sfMipModel, "\n")
statusMipModel = solve(sfMipModel)
print("Status of the problem is ", statusMipModel, "\n")
if statusMipModel == :Optimal
print ("Optimal objective value = ", getObjectiveValue(sfMipModel), "\nOptimal x = ", getValue(x), "\nOptimal y = ", getValue(y))
end
```

[1] M. Lubin and I. Dunning, “Computing in Operations Research using Julia”, INFORMS Journal on Computing, to appear, 2014. arXiv:1312.1431