# How to use JuMP with the Engine

### Get Started

Need to solve a problem written in JuMP? Let us be your guide.

### JuMP on Windows

Our getting started section has all the installation instructions you need to get off the ground. After going through the installation guide, you’ll have:

Whether JuMP is your go-to modelling language or you would like to try it out, you’re five steps away from knowing how to start solving with the Engine.

### Step 1: Install Julia

Go to https://julialang.org/downloads/ and select the executable for the Windows version you are using (64 bit or 32 bit). Follow the instructions on the installation wizard to install Julia.

### Step 2: Install JuMP

After installing Julia, you can now install JuMP as an optimisation modeling language. You’ll need to install solvers for JuMP, as well. You can see how to write the name of your selected solver from the table in the “Getting Solvers” section here: https://jump.dev/JuMP.jl/v0.20/installation/. To do so, open a Windows PowerShell session then copy and paste the code in the snippet below. Note: You can do this also in a script when you write your model (see Step 3).

julia

import Pkg
Pkg.add ("SolverNameAsInJuMPForm")

### Step 3: Write a Model in JuMP

Let’s write a model in JuMP. To do this, open text editor on your PC. For example, Notepad, Notepad++, etc. In the text editor, you can create model by using the commands in the snippet.

using Pkg

try
@eval using JuMP
catch
println("Installing JuMP...")

#Install JuMP package
@eval using JuMP
end

try
@eval using AmplNLWriter
catch
println("Installing AmplNLWriter...")

#Install package through which Octeract Engine is called
@eval using AmplNLWriter
end

println("Create model...")

#Declare a model m and  solve with Octeract Engine
m = Model(() -> AmplNLWriter.Optimizer("octeract-engine"))

#Define variables and variable bounds
@variable(m, 0 <= x1 <= 1)
@variable(m, 0 <= x2 <= 1)
@variable(m, 0 <= x3 <= 1)
@variable(m, 0 <= x4 <= 1)
@variable(m, 0 <= x5 <= 1)

#Define an objective function to be minimised
@objective(m, Min, 42*x1 - 0.5*(100*x1*x1 + 100*x2*x2 +
100*x3*x3 + 100*x4*x4 + 100*x5*x5) + 44*x2 + 45*x3 +
47*x4 + 47.5*x5)

#define a constraint
@constraint(m, con, 20*x1 + 12*x2 + 11*x3 + 7*x4 + 4*x5
<= 40)

println("Optimizing model...")

#optimise model
optimize!(m)

#print value of objective function
println("Objective value: ", objective_value(m)) 

### Step 4: Save the Model

Now, you can save the model on your PC. It will need to be saved as a .jl file. Let's save is it as julia_example.jl

### Step 5: Solve the Model

In PowerShell, navigate to Julia's bin folder (for example, C:\Users\Octeract\Downloads\julia-1.4.2\bin). From there, you can call the model using the command in the snippet below. When doing this, you'll need to include the entire path where the file was saved. For example, C:Users\Octeract\Desktop\julia_example.jl)
After running this command, the problem will be processed and you'll see the solution.

.\julia my_example.jl. 

PS C:\Users\Octeract\AppData\Local\Programs\Julia\Julia-1.4.2\bin>
.\julia C:\Users\Octeract\Desktop\julia_example.jl
Create model...
Optimizing model...
Solved_To_Global_Optimality
Objective value: -16.999999987452583 

### And that's it - you're done! You've just solved an optimisation problem using Julia.

Would you like to try solving with Pyomo on Windows? We'll show you how to do it.

### JuMP on Linux

In our getting started section, you'll find all the installation instructions you need to get off the mark. After going through the installation guide, you'll have:

Whether JuMP is your go-to modeling language or you would like to try it out, you're a few steps away from knowing how to start solving with the Engine.

### Step 1: Install Julia

Go to https://julialang.org/downloads/ and select the tarball for the Linux version you're using (64 bit or 32 bit). Copy and paste the command, shown in the snippet, into the shell. Press Enter.
You might have downloaded another version of Julia so you need to untar whatever-version-name.tar.gz

tar -xvzf julia-1.4.2-linux-x86_64.tar.gz 

### Step 2: Write a Model in JuMP

To do this, open text editor. For example Notepad, Notepad++, etc. In the text editor, you can write a model by using the commands in the snippet. Once the model is written save the model as my_example.jl

using Pkg

try
@eval using JuMP
catch
println("Installing JuMP...")

#Install JuMP package
@eval using JuMP
end

try
@eval using AmplNLWriter
catch
println("Installing AmplNLWriter...")

#Install package through which Octeract Engine is called
@eval using AmplNLWriter
end

println("Create model...")

#Declare a model m and  solve with Octeract Engine
m = Model(() -> AmplNLWriter.Optimizer("octeract-engine"))

#Define variables and variable bounds
@variable(m, 0 <= x1 <= 1)
@variable(m, 0 <= x2 <= 1)
@variable(m, 0 <= x3 <= 1)
@variable(m, 0 <= x4 <= 1)
@variable(m, 0 <= x5 <= 1)

#Define an objective function to be minimised
@objective(m, Min, 42*x1 - 0.5*(100*x1*x1 + 100*x2*x2 +
100*x3*x3 + 100*x4*x4 + 100*x5*x5) + 44*x2 + 45*x3 +
47*x4 + 47.5*x5)

#define a constraint
@constraint(m, con, 20*x1 + 12*x2 + 11*x3 + 7*x4 + 4*x5
<= 40)

println("Optimizing model...")

#optimise model
optimize!(m)

#print value of objective function
println("Objective value: ", objective_value(m)) 

### Step 3: Solve the Model

In the Shell session, navigate to the bin folder (for example, c/Users/Octeract/Downloads/julia-1.4.2/bin) and write: ./julia my_example.jl When doing this, you'll need to provide the whole path where you saved the file. For example: c/users/Octeract/Desktop/my_example.jl This command is shown in the snippet. After you've run this command, the problem will be processed and the solution displayed.

./julia my_example.jl 

PS C/Users/Octeract/AppData/Local/Programs/Julia/Julia-1.4.2/bin>
.\julia C/Users/Octeract/Desktop/my_example.jl
Create model...
Optimizing model...
Solved_To_Global_Optimality
Objective value: -16.999999987452583 

### And that's it - you're done! You've just solved an optimisation problem using Julia.

Would you like to try solving with Pyomo on Linux? We'll show you how to do it.