How to use JuMP with Octeract Engine

engine   guidance   starter guide  
Would you like to solve a problem using Julia with JuMP? Let us take you through the steps.

Which operating system are you using?



Julia with JuMP on Windows

Before reaching this guide, you’ll have:
  • installed the Engine.
  • installed your authentication token.

Step 1: Install Julia

Go to https://julialang.org/downloads/ and obtain the Windows -in 64-bit(installer) flavour- downloadable.

Double-click the installer and follow the instructions on-screen to install.

Make sure that Add Julia to PATH is selected in the Select Additional Tasks section of the installer wizard. Otherwise you will not be able to start Julia from the command line.


Step 2: Launch Julia in the Command Prompt

Click on the Windows icon at the bottom left of your screen and enter Command Prompt. You should now see the Command Prompt icon and be able to launch it.

Note

This guide will not work with Powershell. It is possible to use Powershell for running the Engine through Julia/JuMP, but extra steps may be required depending on your system.
Start a Julia interactive shell by entering this command in the command prompt:
                
julia
                
            
You should see a printout of the Julia logo, and a julia> prompt, that looks like this:
                
_
_       _ _(_)_     |  Documentation: https://docs.julialang.org
(_)     | (_) (_)    |
_ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` |  |
| | |_| | | | (_| |  |  Version 1.6.1 (2021-04-23)
_/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia>
                
            


Step 3: Install JuMP

To install JuMP we are going to use the built-in package manager of the Julia language. We are also going to install some auxiliary packages that are necessary in order to use Octeract Engine later.

Type the following lines in the Julia prompt:
                
import Pkg
Pkg.add("JuMP")
Pkg.add("AmplNLWriter")
                
            
This may take a minute or two depending on the speed of your internet connection and your computer hardware.


Step 4: Run a Model with JuMP and Octeract Engine

Paste the following model in your interactive Julia prompt:
                
using Pkg
using JuMP
using AmplNLWriter

m = Model(() -> AmplNLWriter.Optimizer("octeract-engine"))

@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)

@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)

ENV["octeract_options"] = "num_cores=4"

optimize!(m)

println("Objective value: ", objective_value(m))
println("Solved!")
                
            
The interactive Julia prompt relies on a new line being entered to execute a command*. If the script hangs after being pasted from the web on the last line, press Enter to resume it.
*The model can also be entered line by line, if for some reason your prompt is not accepting the formatting of the whole text block.
Note this line in the above script:
                
ENV["octeract_options"] = "num_cores=4"
                
            
It sets the number of cores that Octeract Engine will solve with. You can adjust that number up to the number of cores allowed by your authentication token, or remove it completely to solve in serial.

To exit the Julia prompt, enter:
                
exit()
                
            

And that's it - you're done!
You've just solved your first model with JuMP and Octeract Engine!





Julia with JuMP on Linux

Before reaching this guide, you’ll have:
  • installed the Engine.
  • installed your authentication token.


Step 1: Launch a Terminal

Open your terminal of choice. You can search for terminal in your start menu or try the shortcut ctrl+shift+t.

Note

This guide assumes that you are running an unmodified bash shell. You can use another shell if you like, but this guide may not work as designed.


Step 2: Install Julia

Go to https://julialang.org/downloads/ and obtain the Generic Linux on x86 (in 64-bit flavour) downloadable. There are rare cases where you would need to download the 64-bit(musl) version, depending on your system. If you do not know what this means, you should proceed with the standard downloadable.

Note where the archive was downloaded, you will need its location later.

The Julia archive can be extracted anywhere, but it would require extra steps to get it to work seamlessly. If you want to follow these steps, you can find more information at https://julialang.org/downloads/platform/#linux_and_freebsd.

For a quick setup, the command below will install Julia in your home directory for your user only, and will make sure it is seamlessly usable from anywhere on your system:
Replace <JULIA_ARCHIVE> with the full path to the downloaded Julia archive.
                
mkdir -p ~/julia && tar zxvf "<JULIA_ARCHIVE>" -C ~/julia --strip-components 1 && echo "export PATH=\"\$PATH:/home/$USER/julia/bin\"" >> ~/.bashrc && source ~/.bashrc
                
            
For example, if you downloaded the Julia archive to /home/user/Downloads/julia-1.6.1-linux-x86_64.tar.gz you would replace <JULIA_ARCHIVE> with that path in the above command.

Note that the "~/" home directory designator will not work in this command, when replacing <JULIA_ARCHIVE> with the path.

To verify that Julia was installed correctly, run the following command:
                
julia --version
                
            
You should get no errors, and a printout of the version of Julia you just installed.


Step 3: Install JuMP

Start a Julia interactive shell by entering this command in the terminal:
                
julia
                
            
You should see a printout of the Julia logo, and a julia> prompt, that looks like this:
                
_
_       _ _(_)_     |  Documentation: https://docs.julialang.org
(_)     | (_) (_)    |
_ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` |  |
| | |_| | | | (_| |  |  Version 1.6.1 (2021-04-23)
_/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia>
                
            
To install JuMP we are going to use the built-in package manager of the Julia language. We are also going to install some auxiliary packages that are necessary in order to use Octeract Engine later.

Type the following lines in the Julia prompt:
                
import Pkg
Pkg.add("JuMP")
Pkg.add("AmplNLWriter")
                
            
This may take a minute or two depending on the speed of your internet connection and your computer hardware.


Step 4: Run a Model with JuMP and Octeract Engine

Copy and paste the following model in your interactive Julia prompt:
                
using Pkg
using JuMP
using AmplNLWriter

m = Model(() -> AmplNLWriter.Optimizer("octeract-engine"))

@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)

@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)

ENV["octeract_options"] = "num_cores=4"

optimize!(m)

println("Objective value: ", objective_value(m))
println("Solved!")
                
            
The interactive Julia prompt relies on a new line being entered to execute a command*. If the script hangs after being pasted from the web on the last line, press Enter to resume it.
*The model can also be entered line by line, if for some reason your prompt is not accepting the formatting of the whole text block.
Note this line in the above script:
                
ENV["octeract_options"] = "num_cores=4"
                
            
It sets the number of cores that Octeract Engine will solve with. You can adjust that number up to the number of cores allowed by your authentication token, or remove it completely to solve in serial.

To exit the Julia prompt, enter:
                
exit()
                
            

And that's it - you're done!
You've just solved your first model with JuMP and Octeract Engine!

htg/htg1004