1. Home
  2. Docs
  3. Tutorials
  4. Pyomo tutorial
  5. Pyomo on Linux

Pyomo on Linux

Before reaching this guide, you’ll have:

  • downloaded the engine.
  • installed the engine.

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.

How can we help?