How to use C++ with the Engine

C++ new

Get Started

 
 

Want to solve a problem written in C++. Let’s do it!

 

Which operating system are you using?

 
 
New Windows
 
New Linux

 

C++ on Windows


In our section on solving your first problem, you’ll find all the instructions you need to get set up. Once you’ve gone through the starter guide, you’ll have:

Not sure how to compile a C++ program? No problem. We can take you through it on Visual Studio. Let’s use C++ with the Engine to solve a problem.

Step 1: Write a Model in C++

We’ll start by creating a model in C++. To do this, open an IDE session. For example, Visual Studio. Copy and paste the snippet to create a model in your session. Once you have the program, save the model as my_example in C++.

 
#include "octeract.h" //Add Octeract's header file 

using namespace octeract; 

int main()
{
     auto m = Model() //Define an empty model

     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);  //Declare variables and variable bounds

     //Set an 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"); 

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

     //Solve model to global optimality using 4 cores        
     m.global_solve(4);
 
     return 0; 
} //End program
 

Step 2: Include the Header

To compile the program, we'll firstly need to include header octeract.h. This can be found where the Engine was installed on your PC. In the Octeract folder, there's a folder named "include" where octeract.h is located. This is the file we'll need to include.

 
include header
 

Step 3: Link to Octeract Libraries

As a next step, we need to link to Octeract libraries. The Octeract library can also be found in the directory where the Engine was installed on your PC. In the Octeract folder, you'll find a "bin" folder. Inside this folder, "libocteract.lib" is located. This is the library that the compiler needs to link to.

 
Link to Octeract Libraries
 

Step 4: Solve the Model

Once these two actions are complete, the program can be compiled. As per standard procedure in C++, compiling the program will create an .exe file. In our case, "my_example.exe" will be created and can be run from a PowerShell session. All you have to do is open PowerShell and run the .exe file. The whole directory where it is saved needs to be included. You can find an example in the snippet. From there, the solution will be displayed on the screen.

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

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

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

    641      1.253e-08 (  0.00%)   -1.700e+01   -1.700e+01      0      0.2s    24.0MB

Objective value at global solution: -1.700e+01
Solved_To_Global_Optimality
C:\Users\Octeract\App\Data\Local\Temp\4jpUClQhpauqEPrs_octeract.octsol
Total time : 0.20s
 

A more detailed version of the solution is written and stored in a file on your PC. You'll be able to access it using the path in the line "Solution file written to". For example: C:Users\Octeract\App\Data\Local\Temp From there, you can view the solution file (.octsol) in text editor.

You did it - well done!
You've just solved a problem using C++

Want to try your hand at solving with Python in Windows? We can guide you through it.

C++ on Linux


In our section on solving your first problem, you'll find all the steps to take to get up and running. Once you've gone through this guide, you'll have:

Without further ado, let's start solving with the Engine.

Step 1: Write a Model in C++

We'll start by creating a model in C++. To do this, open a text editor IDE session. Copy and paste the snippet to create a model. Once you have the program in your session, save the model as my_example in C++.

 
#include "octeract.h" //Add Octeract's header file 

using namespace octeract; 

int main() 
{
     auto m = Model() //Define an empty model

     //Declare 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); 

     //Set an 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"); 

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

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

     return 0; 
} //End program 
 

Step 2: Compile the Program

Open a Shell Session. Copy and paste the command in the snippet. This will include the header, "octeract.h" and link to Octeract Libraries. Once this command is run successfully, the program has been compiled. A file will be created. In our case, this is called my_example

clang++-11 -Wl,-R/c/Users/Octeract/Downloads/octeract/lib -L./octeract/lib -locteract -I./octeract/include .octeract/example/c++/api_example.cpp -o my_example

Step 3: Solve the Model

In your Shell Session, you can run this file. To do this, use the name of the file in the command. There is an example in the snippet. From there, the solution will be displayed on the screen. An example of what your final screen should look like can be seen below.

 
./my_example
 
Solved model C++

A more detailed version of the solution is written and stored in a file on your PC. You'll be able to access it using the path in the line "Solution file written to". You can then view the solution file (.octsol) in text editor.

That's it - well done!
You've just solved a problem using C++

Want to try solving with Python on Linux? We can guide you through it.

Leave a Reply