# How to use Python with the Engine

## Get Started

Need to solve a problem in Python? Let us walk you through it.

### Python on Windows

In our section on solving your first problem, you’ll find all the instructions you need to kick off your journey with Octeract Engine. After going through the guide, you’ll have:

Whether Python is your programming language of choice or you’re trying it out, you’ll be in the know after these four steps on using it and the Engine.

### Step 1: Create an Empty Model

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


from octeract import *
m = Model()



### Step 2: Write a Model in Python

Continuing in text editor, you can add more elements to the model, such as variables and variable bounds, an objective function and constraints. You can use the model below by copying and pasting the snippet below into your text editor. In order to view a summary of the solution in PowerShell, the final command in the code block will need to be included in your model set-up.

#Define variables and variable bounds

#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")

#View the model
print(m)

#Solve the model to global optimality using 4 cores
m.global_solve(4);

### Step 3: Save the Model

Now you can go ahead and save the model on your PC. It'll need to be saved as a .py file. Let's save it as my_example_python.py

### Step 4: Solve the Model

Open a Powershell session on your PC. You can call the model, using python3.7 my_example_python.py You'll find this command in the snippet below. When doing this, you'll need to include the whole path where the file is saved. For example: C:\Users\emmel\Downloads\Engine. After running this command, the problem will be processed and you'll see information about the Engine's solving process and the global optimum.

 python3.7 C:\Users\emmel\Downloads\Engine\my_example_python.py

========================================
Octeract Engine v1.05.08
========================================

Preprocessing problem... 100% complete
Presolve time : 0.02s

-----------------------------------------------------------------------------------------
Iteration            GAP               LLB          BUB       Pool      Time     Mem
-----------------------------------------------------------------------------------------
0       1.890e+01 (   inf%)   -1.890e+01      1.253e-08      0      0.0s    23.0MB

268      1.253e-08 (  0.00%)   -1.700e+01     -1.700e+01      0      0.1s    23.0MB

Objective value at global solution: -1.700e+01
Solved_To_Global_Optimality
Solution file written to: C:\Users\emmel\AppData\Local\Temp\\F0GQ8K6Z8SXXA9v1_octeract.octsol
Total time : 0.16s


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

### And you're all done. Nice job! You've just solved a problem using Python.

Want to have a go at using C++ in Windows? We can show you how.

### Python on Linux

In our section on solving your first problem, you'll find all the instructions you need to get started with Octeract Engine. After going through the guide, you'll have:

Whether you're familiar with Python or not, you'll soon be able to start solving with the Engine.

### Step 1: Get Up-to-date

Python is already available and installed on Linux. However, you might need install Python 3.7 on your PC, if the Python version already installed is not 3.7.

### Step 2: Create an Empty Model

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

from octeract import *
m = Model()


### Step 3: Write a Model in Python

In text editor, start writing a model by defining some variables and setting variable bounds. From there, define an objective function and a constraint for the model. To finish off, we'll state that we want the problem to be solved to global optimality, using the Engine and 4 cores. Save the model as my_example_python.py



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")

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

print(m)

m.global_solve(4);

### Step 4: Solve the Model

In a Shell Session on your PC, you can call the model using python3.7 my_example_python.py You'll find this command in the snippet below. When doing this, you'll need to include the whole path where the file is saved. For example: C/Users/emmel/Downloads/Engine. After running this command, the problem will be processed and you'll see the solution.

 python3.7 C/Users/emmel/Downloads/Engine/my_example_python.py

========================================
Octeract Engine v1.05.08
========================================

Preprocessing problem... 100% complete
Presolve time : 0.02s

-----------------------------------------------------------------------------------------
Iteration            GAP               LLB          BUB       Pool      Time     Mem
-----------------------------------------------------------------------------------------
0       1.890e+01 (   inf%)   -1.890e+01      1.253e-08      0      0.0s    23.0MB

268      1.253e-08 (  0.00%)   -1.700e+01     -1.700e+01      0      0.1s    23.0MB

Objective value at global solution: -1.700e+01
Solved_To_Global_Optimality
Solution file written to: C/Users/emmel/AppData/Local/Temp//F0GQ8K6Z8SXXA9v1_octeract.octsol
Total time : 0.16s


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

Want to try using C++ with Linux? We can show you the steps to take.