How to use Pyomo with Octeract Engine

explanation   practical insight  
Would you like to solve a problem using Pyomo? Let us take you through the steps.



Which operating system are you using?




Pyomo on Windows

Before reaching this guide, you’ll have:


Note

This guide is written for Command Prompt. Some additional steps may be required when using Powershell.


Step 1: Launch 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.




Step 2: Install Python

Check if Python is already installed on your machine by opening the Command Prompt and typing:
                
python --version
                
            
If the given version is >= 3.6 you can go to step 3.

If Python is not installed or if you have an earlier version installed, go to https://www.python.org/downloads and download the Python installer.

You can check your installation by closing and reopening Command Prompt then repeating the beginning of this step.

Run the Python installer and go through the installation process. Make sure that the "Add Python 3.x to PATH" is ticked.

Python should be correctly installed at this point. You can check your installation by repeating the beginning of this step.




Step 3: Install Pyomo

Enter:
                
python -m pip install --user pyomo
                
            
Look out for any error messages in the output.

This should output version information for Pyomo if installation is successful. If this fails, refer to the Pyomo installation manual for more detail.




Step 4: Use the Python Interpreter

In your Command Prompt session, enter:
                
python
                
            
This will open the Python shell, where you can enter Python commands.

Alternatively, you can use a Python editor of your choice. Save the file as your_file.py and enter:
                
python "C:\path\to\your_file.py"
                
            
This will run your Python commands in one step.




Step 5: Start your Model

From now on, all commands will either be entered in the Python shell or in a Python file to be run later.

Enter:
                
from pyomo.environ import *
model = m = ConcreteModel()
                
            
This will import the necessary functions from Pyomo and start an empty model.




Step 6: Fill your Model

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

m.obj = Objective(sense=minimize, 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)
m.e2 = Constraint(expr=20*x1 + 12*x2 + 11*x3 + 7*x4 + 4*x5 <= 40)
                
            
If you'd prefer to get creative, refer to the list of modeling components for Pyomo.




Step 7: Set the Number of Cores (optional)

To make Octeract Engine solve in parallel, enter:
                
import os
os.environ["octeract_options"] = "num_cores=4"
                
            
You can replace 4 with any number of cores offered by your subscription.




Step 8: Solve your Model

Finally, to solve your model, enter:
                
results = SolverFactory("octeract-engine").solve(m, tee=True, keepfiles=False, load_solutions=False)
print(results)
m.solutions.load_from(results)
                
            
If successful, it will print out the optimal solution for your model.

Alternatively, to skip the printing, instead enter:
                
SolverFactory("octeract-engine").solve(m, tee=True, keepfiles=False)
                
            

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





Pyomo on Linux

Before reaching this guide, you’ll have:



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.




Step 2: Install Python

Pyomo uses the Python language to build models. To check if you already have Python installed, enter:
                
python --version
                
            
If the version is not returned, relaunch your terminal and re-enter the command.

If this returns version information for Python (version 3.6 or newer), proceed to the next step.

Otherwise install Python and check again.




Step 3: Install pip

You will need pip to retrieve Pyomo for your Python installation. To check if you already have pip installed, enter:
                
python3 -m pip --version
                
            
If this returns version information for pip, proceed to the next step.

Otherwise install pip and check again.




Step 4: Install Pyomo

Enter:
                
python3 -m pip install --user pyomo
                
            
Look out for any error messages in the output. To verify that it has been sucessfully installed, enter:
                
pyomo --version
                
            
This should output version information for Pyomo if installation is successful. If this fails, refer to the Pyomo installation manual for more detail.




Step 5: Use the Python Interpreter

Enter:
                
python3
                
            
This will open the Python shell, where you can enter Python commands.

Alternatively, you can use a Python editor of your choice. Save the file as your_file.py and enter:
                
python3 "/path/to/your_file.py"
                
            
This will run your Python commands in one step.




Step 6: Start your Model

From now on, all commands will either be entered in the Python shell or in a Python file to be run later.

Enter:
                
from pyomo.environ import *
model = m = ConcreteModel()
                
            
This will import the necessary functions from Pyomo and start an empty model.




Step 7: Fill your Model

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

m.obj = Objective(sense=minimize, 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)
m.e2 = Constraint(expr=20*x1 + 12*x2 + 11*x3 + 7*x4 + 4*x5 <= 40)
                
            
If you'd prefer to get creative, refer to the list of modeling components for Pyomo.




Step 8: Set the Number of Cores (optional)

To make Octeract Engine solve in parallel, enter:
                
import os
os.environ["octeract_options"] = "num_cores=4"
                
            
You can replace 4 with any number of cores offered by your subscription.




Step 9: Solve your Model

Finally, to solve your model, enter:
                
results = SolverFactory("octeract-engine").solve(m, tee=True, keepfiles=False, load_solutions=False)
print(results)
m.solutions.load_from(results)
                
            
If successful, it will print out the optimal solution for your model.
Alternatively, to skip the printing, instead enter:
                
SolverFactory("octeract-engine").solve(m, tee=True, keepfiles=False)
                
            

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


Was this helpful?
Please make sure you fill the comments section before sending.

Thank you for your comments.
Please contact us if you need any further support.
htg/htg1005