Setting up Julia JuMP

Notes

You could find an installation guide on the official website: Installation Guide · JuMP Here in my tutorial I will provide some notes on the pitfalls that I experienced.

Basic JuMP Installation

  1. Download and install Julia: Installing Julia You just need to download an installer according to your OS. At the end of the installation, make sure to check “add Julia to PATH”, so you add an environment variable for Julia.
  2. In the Julia interactive session (or read-eval-print loop - “REPL”), enter the following to install JuMP:
import Pkg
Pkg.add("JuMP")
  1. Install an optimization solver by entering the following in REPL:
import Pkg
Pkg.add("Clp")

Clp is a free optimization solver. We could also use other solvers, like Gurobi, as long as JuMP supports them. The list of solvers supported can be found here: Installation Guide · JuMP

  1. Test a code. Save the following code in the path C:\Julia with the name Problem.jl.
using JuMP, Clp
model = Model(Clp.Optimizer);
@variable(model, 0 <= x <= 2);
@variable(model, 0 <= y <= 30);
@objective(model, Max, 5x + 3 * y);
@constraint(model, con, 1x + 5y <= 3);
optimize!(model);
println(objective_value(model));

Get into the folder that you saved the file using the following command:

cd("C:\\Julia")

Yes, there are two slashes in the path!!!
Then use the following command to run the code:

include("Problem.jl")

Then you should be able to see something like this:

Coin0506I Presolve 0 (-1) rows, 0 (-2) columns and 0 (-2) elements
Clp3002W Empty problem - 0 rows, 0 columns and 0 elements
Clp0000I Optimal - objective value 10.6
Coin0511I After Postsolve, objective 10.6, infeasibilities - dual 0 (0), primal 0 (0)
Clp0032I Optimal objective 10.6 - 0 iterations time 0.022, Presolve 0.01
10.6

It tells us the objective value is 10.6.

Intall Gurobi and Get Gurobi to Work with JuMP

  1. Register a Gurobi account: https://www.gurobi.com/ Then login using your account information.
  2. Get a free academic license at End User License Agreement Academic - Gurobi Optimization At last, copy the license key for Gurobi.
  3. Dowload the Gurobi Optimizer: https://www.gurobi.com/downloads/gurobi-software/ Choose the right version. If you installed Julia under Windows, then download the Windows version of Gurobi.
  4. Install Gurobi using the installer. After it is installed, run Gurobi, and it will ask you for the license key at the first run. Paste your license key into the Gurobi command window.
  5. Restart your computer.
  6. Go back to the Julia REPL, put in the following command:
import Pkg
Pkg.add("Gurobi")
Pkg.build("Gurobi")
  1. Test a code. Save the following code in the path C:\Julia with the name Problem1.jl.
using JuMP, Gurobi
model = Model(Gurobi.Optimizer);
@variable(model, 0 <= x <= 2);
@variable(model, 0 <= y <= 30);
@objective(model, Max, 5x + 3 * y);
@constraint(model, con, 1x + 5y <= 3);
optimize!(model);
println(objective_value(model));

Get into the folder that you saved the file using the following command:

cd("C:\\Julia")

Yes, there are two slashes in the path!!!
Then use the following command to run the code:

include("Problem1.jl")

Then you should be able to see something like this:

Academic license - for non-commercial use only - expires 2021-06-08
Gurobi Optimizer version 9.1.1 build v9.1.1rc0 (win64)
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 1 rows, 2 columns and 2 nonzeros
Model fingerprint: 0xa62c9e92
Coefficient statistics:
  Matrix range     [1e+00, 5e+00]
  Objective range  [3e+00, 5e+00]
  Bounds range     [2e+00, 3e+01]
  RHS range        [3e+00, 3e+00]
Presolve removed 1 rows and 2 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    1.0600000e+01   0.000000e+00   0.000000e+00      0s

Solved in 0 iterations and 0.00 seconds
Optimal objective  1.060000000e+01

User-callback calls 28, time in user-callback 0.00 sec
10.6

It tells us the objective value is 10.6.

===================================================

Atom has been sunset, so it is recommended to skip the following step and use Visual Studio Code instead

Install Juno (an IDE for Julia)

Here is the official installation guide: Installation Instructions · Juno Documentation

  1. Download, install and open Atom. If you have it already, make sure it’s up to date (version 1.41+).
  2. In Atom, go to Settings ( Ctrl+, , or Cmd+, on macOS. Note that this is Control plus comma!) and go to the “Install” panel.
  3. Type uber-juno into the search box and hit enter. Click the install button on the package of the same name.
  4. Atom will then set up Juno for you, installing the required Atom and Julia packages.
  5. Try opening the REPL with Juno > Open REPL or Ctrl-J Ctrl-O ( Cmd-J Cmd-O on macOS), and then press Enter in the REPL to start a Julia session. If it says Julia cannot be found but you have already installed Julia, that is probably because you didn’t add Julia to PATH.
  6. You could also create a new file to edit your code. The code will be colored and easier to read than editing in a plain text editor.

===================================================

Julia in Visual Studio Code

  1. Install VS Code for your platform: Download Visual Studio Code - Mac, Linux, Windows.
  2. Open the Julia extension on the VS Code Marketplace and press Install; or manually install by doing the following steps:
  • Start VS Code.
  • Inside VS Code, go to the Extensions view by clicking View on the top menu bar and then selecting Extensions.
  • In the Extensions view, search for the term “julia” in the Marketplace search box, then select the Julia extension (julialang.language-julia) and select the Install button.
  • Restart VS Code.

Add a package to a new environment, not the global environment

In this example, I would like to create a new environment and add the GLPK solver to this environment.

  1. You could use the command pwd() to see which path you are in. If you are in the folder that you want to create a new project, go to Step 4. otherwise, continue to Step 2.
  2. Use the command cd() to get into the folder that you want to create the new project. For example, C:\Julia, then the command should be
cd("C:\\Julia")
  1. You could even create a new folder under the Julia folder using the command:
mkdir("GLPKOptPrj")

Then use cd("GLPKOptPrj") to get into the GLPKOptPrj folder to create your new project.

  1. Press ] to enter the package manager mode, and then:
  • Type activate . and then press Enter to activate the environment.
  • Type add GLPK to add the GLPK solver to the environment.
  • Press Backspace or Ctrl+C to exit the package manager mode.
  • Now you are ready to run your code.
  1. Once you restart Atom, if you need to use the GLPK solver, you need to first get into the folder using cd("C:\\Julia\\GLPKOptPrj"), press ] to enter the package manager mode, and then type activate . and then press Enter to activate the environment. Then press Backspace or Ctrl+C to exit the package manager mode, and you are ready to run the code. But if you installed the package in the global environment, you don’t need to activate the environment each time.