How to use AMPL with the Engine

AMPL

Get Started

Want to solve a problem written in AMPL? Let us guide you through it.

Which operating system are you using?

New Windows
New Linux

AMPL on Windows


Our section on solving your first problem has all the instructions you need to get off the mark. After going through this, you’ll have:

Whether AMPL is your preferred modelling language or you would like to test it out, you’re all set to start solving with the Engine.

Step 1: Download AMPL

In order to download the language, go to AMPL’s download a free demo page. Scroll down the page to where it says AMPL Command Line download for Windows. You can download either one of the zipped files:  ampl.mswin32.zip for the 32-bit version OR ampl.mswin64.zip for the 64-bit version.

download AMPL

Step 2: Install AMPL

On your PC, unzip and extract the folder ampl.mswin32 or ampl.mswin64 from the zip file you downloaded. To start an AMPL command interface session, you’ll need to double-click on the sw.exe icon in your AMPL folder. In the command interface, type ampl. Press Enter. You’ll see ampl: This prompt means that you can now type AMPL commands.

install ampl

Step 3: Write a Model in AMPL

Let’s create a model. To do this, open text editor on your PC. For example, Notepad, Notepad++, etc. To write our model, let’s define the variables, write an expression to be minimised and, finally, set some constraints. For a quick model set-up, copy and paste the snippet below into your text editor.


var x1 := 1;
var x2 := 1;
var x3;
var x4 := 1;
var x5;

minimize Expression: 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;

subject to Constraint: 20*x1 + 12*x2 + 11*x3 + 7*x4 + 4*x5 <= 40;
subject to x1_lim: 0 <= x1 <= 1; 
subject to x2_lim: 0 <= x2 <= 1;
subject to x3_lim: 0 <= x3 <= 1;
subject to x4_lim: 0 <= x4 <= 1;
subject to x5_lim: 0 <= x5 <= 1;

Step 4: Save the Model

Now you can save the model on your PC. It'll need to be saved in the Octeract folder that was created when you installed Octeract Engine. This is the easiest way to solve with the Engine. The model will need to be saved as a .mod file. Let's save it as my_example.mod

save model

Step 5: Solve the Model

In AMPL's command interface, where AMPL is running, call the model by using the command model my_example.mod; When doing this, you'll need to include the whole path where you've saved it. For example: C:\Users\emmel\Downloads\Engine\Octeract\my_example.mod; From there, specify the solver that'll be asked to solve the problem and call the Engine to solve. After running these commands, the problem will be processed and you'll see the solution. To display the new variable values on screen, use the display command.

ampl: model C:\Users\emmel\Downloads\Engine\Octeract\my_example.mod;
ampl: option solver octeract-engine;
ampl: solve;
ampl: display x1, x2, x3, x4, x5;
========================================
 Octeract Engine v1.05.08    
 Copyright (c) Octeract Ltd, 2020
========================================

Loading problem...
Preprocessing problem... 100% complete
Presolve time : 0.04s

-----------------------------------------------------------------------------------------
 Iteration            GAP               LLB          BUB       Pool    Time     Mem  
-----------------------------------------------------------------------------------------
     8        1.253e-08 (  0.00%)   -1.700e+01   -1.700e+01      1     0.1s    21.0MB

Objective value at global solution: -1.700e+01
Solved_To_Global_Optimality
Solution file written to: C:\Users\emmel\AppData\Local\Temp\\at2760.octsol
Solved_To_Global_Optimality
x1 = 1
x2 = 1
x3 = 5.57032e-11
x4 = 1
x5 = 5.27602e-11

The full solution is written and stored in a file. This can be located on your PC by using the path in the "Solution file written to" line you see in the command interface. For example, C:\Users\emmel\AppData\Local\Temp\ From there, you'll be able to find the solution file (.octsol) and open it in text editor. This will allow you to view a more detailed version of the solution.

And that's it. Well done!
You've just solved an optimisation problem in AMPL.

Want to try solving with Pyomo on Windows? We'll show you how.

AMPL on Linux


In our section on solving your first problem, you'll find all the instructions you need to get up and running. After going through this guide, you'll have:

Whether AMPL is your go-to modelling language or you're just trying it out, with a few steps you'll be in the know on how to solve using this language with the Engine.

Step 1: Download AMPL

In order to download the language, go to AMPL's download a free demo page. Scroll down the page to where it says AMPL Command Line download for Linux. You can download either one of the distribution archives: ampl.linux32.tgz for a 32-bit version OR ampl.linux64.tgz for a 64-bit version.

download AMPL Linux

Step 2: Install AMPL

Open a shell session to extract the contents of the downloaded file. Use one of the two commands in the snippet below, depending on the version you've downloaded. Once the extraction is complete, a directory either named ampl.linux32 or ampl.linux64 will be created. Start an AMPL command interface session and use the cd command to go to the AMPL directory. Type ./ampl at the system prompt and you'll see ampl: which means you can now type AMPL commands.

 tar xzf ampl.linux32.tgz 
 tar xzf ampl.linux64.tgz

Step 3: Write a Model in AMPL

Let's create a model. To do this, open text editor on your PC. For example, Notepad, Notepad++, etc. To write our model, let's define the variables, write an expression to be minimised and, finally, set some constraints. For a quick model set-up, copy and paste the snippet below into your text editor.


var x1 := 1;
var x2 := 1;
var x3;
var x4 := 1;
var x5;

minimize Expression: 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;

subject to Constraint: 20*x1 + 12*x2 + 11*x3 + 7*x4 + 4*x5 <= 40;
subject to x1_lim: 0 <= x1 <= 1; 
subject to x2_lim: 0 <= x2 <= 1;
subject to x3_lim: 0 <= x3 <= 1;
subject to x4_lim: 0 <= x4 <= 1;
subject to x5_lim: 0 <= x5 <= 1;

Step 4: Save the Model

Now you can save the model on your PC. It'll need to be saved in the Octeract folder that was created when you installed Octeract Engine. This is the easiest way to solve with the Engine. The model will need to be saved as a .mod file. Let's save it as my_example.mod

save model

Step 5: Solve the Model

At the system prompt, where AMPL is running, call the model by using the command model my_example.mod; When doing this in the command interface, you'll need to include the whole path where you've saved the file. For example: C/Users/emmel/Downloads/Octeract/my_example.mod; From there, call the solver to solve the problem. You'll need to provide the whole path where the Engine is untared. After running these commands, the problem will be processed and you'll see the solution. To display the new variable values on screen, use the display command.

ampl: model C/Users/emmel/Downloads/Octeract/my_example.mod;
ampl: option solver C/Users/emmel/Downloads/Octeract/bin/octeract-engine;
ampl: solve;
ampl: display x1, x2, x3, x4, x5;
========================================
 Octeract Engine v1.05.08    
 Copyright (c) Octeract Ltd, 2020
========================================

Loading problem...
Preprocessing problem... 100% complete
Presolve time : 0.04s

-----------------------------------------------------------------------------------------
 Iteration            GAP               LLB          BUB       Pool    Time     Mem  
-----------------------------------------------------------------------------------------
     8        1.253e-08 (  0.00%)   -1.700e+01   -1.700e+01      1     0.1s    21.0MB

Objective value at global solution: -1.700e+01
Solved_To_Global_Optimality
Solution file written to: C/Users/emmel/AppData/Local/Temp//at2760.octsol
Solved_To_Global_Optimality
x1 = 1
x2 = 1
x3 = 5.57032e-11
x4 = 1
x5 = 5.27602e-11

The full solution is written and stored in a file. This can be located on your PC by using the path in the "Solution file written to" line you see in the command interface. For example, C/Users/emmel/AppData/Local/Temp/. From there, you'll be able to find the solution file (.octsol) and open it in text editor. This will allow you to view a more detailed version of the solution.

There you have it!
You've just solved a problem using AMPL.

Want to try solving with Pyomo on Linux? We'll show you how.

Leave a Reply

%d bloggers like this: