Installing JuMP¶
At first add the JuMP package by running the following code in the notebook:
In [2]:
Pkg.add("JuMP")
INFO: Nothing to be done
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")
INFO: Nothing to be done
In [3]:
Pkg.add("Cbc")
INFO: Nothing to be done
In [4]:
Pkg.add("Clp")
INFO: Nothing to be done
In [5]:
Pkg.add("CPLEX") # Working installation of CPLEX is needed in advance
INFO: Nothing to be done
In [6]:
Pkg.add("Gurobi") # Working installation of Gurobi is needed in advance
INFO: Nothing to be done
If you have not updated your Julia packages in a while, a good idea might be updating them.
In [7]:
Pkg.update()
INFO: Updating METADATA... INFO: Computing changes... INFO: No packages to install, update or remove
In [13]:
println("Hello World!")
Hello World!
The very first example¶
At first let us try to solve a very simple and trivial optimization problem using JuMP to check if everything is working properly.
minimizesubject tox+yx+y≤1x≥0,y≥0x,y∈Rminimizex+ysubject tox+y≤1x≥0,y≥0x,y∈R
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))
The optimization problem to be solved is: Min x + y Subject to x + y <= 1 x >= 0 y >= 0 Objective value: 0.0 x = 0.0 y = 0.0
This was certainly not the most exciting optimization problem to so