fbpx

How to use Pyomo with Octeract Engine

Pyomo

Get Started

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

Which operating system are you using?

New Windows
New Linux

 

Pyomo 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 Pyomo 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.

Step1: Install Pyomo

To install this optimisation modelling language, you’ll need to open a Windows PowerShell session. Copy and paste the command, shown in the snippet, into the PowerShell. Press Enter. Wait a few moments and Pyomo will be installed on your PC.

 
 
python -m pip install --user pyomo

 

Step 2: Create an Empty Model

Let’s create a simple model in Pyomo. To do this, open text editor on your PC. For example, Notepad, Notepad++, etc. In the text editor, you can create an empty model by using the command in the snippet.

 

from pyomo.environ import* 
model = m = ConcreteModel()

 

Step 3: Write a Model in Pyomo

Continuing in text editor, you can then define some variables with variable bounds. For ease of use, let’s assign different names to the variables. From here, write an objective function which states that we want to minimise. Then define a constraint for the optimisation problem. The finishing touch to writing the model: specify the solver that will be asked to solve the problem i.e. Octeract Engine.

 
m.x1 = Var(within=Reals,bounds=(0,1),initialize=1) 
m.x2 = Var(within=Reals,bounds=(0,1),initialize=1)
m.x3 = Var(within=Reals,bounds=(0,1),initialize=None)
m.x4 = Var(within=Reals,bounds=(0,1),initialize=1)
m.x5 = Var(within=Reals,bounds=(0,1),initialize=None)

x1 = m.x1
x2 = m.x2
x3 = m.x3
x4 = m.x4
x5 = m.x5

m.obj = Objective(expr = 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, sense=minimize)

m.e2 = Constraint(expr = 20*x1 + 12*x2 + 11*x3 + 7*x4 + 4*x5 <= 40) 

SolverFactory("octeract-engine").solve(m,tee=True,keepfiles=False) 

 

Step 4: Save the Model

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

 
model save
 

Step 5: Solve the Model

In PowerShell, you can call the model using python my_example.py. This command is in the snippet below. When doing this, you will need to include the entire path where the file was saved. For example, C:Users\emmel\Downloads\Engine\my_example.py
After running this command, the problem will be processed and you'll see the solution.

 
python C:\Users\emmel\Downloads\Engine\my_example.py
========================================
 Octeract Engine v1.05.08
 Copyright (c) Octeract Ltd, 2020
========================================

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

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

Objective value at global solution: -1.700e+01
Solved_To_Global_Optimality
Solution file written to: C:\Users\emmel\AppData\Local\Temp\\tmpyond8zjz.pyomo.octsol
 

The full solution is written and stored in a file. This can be located on your PC by using the path in the last line you see in the PowerShell. For example, C:Users\emmel\AppData\LocalTemp\
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 - you're done!
You've just solved an optimisation problem using Pyomo.

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

Pyomo 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 Pyomo is your preferred modelling 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 Pyomo

In order to install this optimisation modelling language, you'll need to open a shell session. Copy and paste the command, shown in the snippet, into the shell. Press Enter. After a few moments, Pyomo will be installed on your PC. If you get an error message, you'll need to run the sudo apt commands to get the latest packages, upgrade and then install pip. These commands are shown below. From there, the pip3 install pyomo command will work.

 
pip3 install pyomo
sudo apt update 
sudo apt upgrade
sudo apt install python3-pip
 

Step 2: Create an Empty Model

Let's create an empty model in Pyomo. To do this, open text editor. For example Notepad, Notepad++, etc. In the text editor, you can create an empty model by using the command in the snippet.

 

from pyomo.environ import* 
model = m = ConcreteModel()

 

Step 3: Write a Model in Pyomo

Continuing in text editor, you can then define some variables with variable bounds. For ease of use, let's assign different names to the variables. From here, write an objective function which states that we want to minimise. Then define a constraint for the optimisation problem. The final touch to writing the model: specify the solver that will be asked to solve the problem i.e. Octeract Engine. Then save the model as my_example.py

 
m.x1 = Var(within=Reals,bounds=(0,1),initialize=1) 
m.x2 = Var(within=Reals,bounds=(0,1),initialize=1)
m.x3 = Var(within=Reals,bounds=(0,1),initialize=None)
m.x4 = Var(within=Reals,bounds=(0,1),initialize=1)
m.x5 = Var(within=Reals,bounds=(0,1),initialize=None)

x1 = m.x1
x2 = m.x2
x3 = m.x3
x4 = m.x4
x5 = m.x5

m.obj = Objective(expr = 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, sense=minimize)

m.e2 = Constraint(expr = 20*x1 + 12*x2 + 11*x3 + 7*x4 + 4*x5 <= 40) 

SolverFactory("octeract-engine").solve(m,tee=True,keepfiles=False) 

 

Step 4: Solve the Model

In the shell session, you can call the model by using the command: python3.7 my_example.py When doing this, you'll need to provide the whole path where you saved the file. For example: C/Users/emmel/Downloads/Engine/. This command is shown in the snippet. After you've run this command, the problem will be processed and the solution displayed.

 
python3.7 C/Users/emmel/Downloads/Engine/my_example.py
========================================
 Octeract Engine v1.05.08
 Copyright (c) Octeract Ltd, 2020
========================================

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

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

Objective value at global solution: -1.700e+01
Solved_To_Global_Optimality
Solution file written to: C/Users/emmel/AppData/Local/Temp//tmpyond8zjz.pyomo.octsol
 

A more in-depth version of the solution is written and stored in a solution file (.octsol). You'll be able to locate this file on your PC using the path in the last line displayed in the shell session. For example: C/Users/emmel/AppData/Local/Temp/. You'll be able to view the full solution in text editor.

And you're all done!
You've just solved a problem using Pyomo

Would you like to try using AMPL with Linux? Let us show you how.

Leave a Reply