How to use Python with Octeract Engine

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



Which operating system are you using?




Python on Windows

Before reaching this guide, you’ll have:



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.7.x you can go to step 3.

If Python is not installed or if you have a different version installed, go to https://www.python.org/downloads and download the Python 3.7.x 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.7.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: 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 4: 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 octeract import *
m = Model()
                
            
This will import the necessary functions from the Octeract Python library and start an empty model.




Step 5: Fill your Model

Enter:
                
#Define variables and variable bounds
m.add_variable("x1", 0, 1)
m.add_variable("x2", 0, 1)
m.add_variable("x3", 0, 1)
m.add_variable("x4", 0, 1)
m.add_variable("x5", 0, 1)

#Define and objective function
m.set_objective("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")

#Define a constraint
m.add_constraint("20*x1 + 12*x2 + 11*x3 + 7*x4 + 4*x5 <= 40")
                
            




Step 6: Solve your Model

Finally, to solve your model, enter:
                
m.global_solve()
                
            
Alternatively, to make Octeract Engine solve in parallel, enter
                
n = 4
m.global_solve(n)
                
            
where 4 can be any number of cores offered by your subscription.

If successful, it will print out the optimal solution for your model.


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





Python 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

To check if you already have Python installed, enter:
                
python3 --version
                
            
If the version is not returned, relaunch your terminal and re-enter the command.




Step 3: 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 4: 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 octeract import *
m = Model()
                
            
This will import the necessary functions from the Octeract Python library and start an empty model.




Step 5: Fill your Model

Enter:
                
#Define variables and variable bounds
m.add_variable("x1", 0, 1)
m.add_variable("x2", 0, 1)
m.add_variable("x3", 0, 1)
m.add_variable("x4", 0, 1)
m.add_variable("x5", 0, 1)

#Define and objective function
m.set_objective("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")

#Define a constraint
m.add_constraint("20*x1 + 12*x2 + 11*x3 + 7*x4 + 4*x5 <= 40")
                
            




Step 6: Solve your Model

Finally, to solve your model, enter:
                
m.global_solve()
                
            
Alternatively, to make Octeract Engine solve in parallel, enter
                
n = 4
m.global_solve(n)
                
            
where 4 can be any number of cores offered by your subscription.

If successful, it will print out the optimal solution for your model.

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


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/htg1006