How to use C++ with Octeract Engine

explanation   practical insight   starter guide  
Want to solve a problem written in C++. Let’s do it!



Which operating system are you using?




C++ on Windows

Before reaching this guide, you'll have:

This guide is specifically for compiling C++ program on Visual Studio.


Step 1: Create your Visual Studio Project

  • Download and open Visual Studio
  • Click on Create a new Project
  • Select C++ as the language
  • Click on Console App template
  • Click on Next
  • Select a name for the project and click on Create



Step 2: Build Project

  • Click on Build and then Build
    <Name of your project>
  • Verify that there is no error
  • Remove the code in the current window
  • Copy the following code:
                
#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
                
            

  • Click on Project then
    <Your Project Name>
    Properties then C/C++ then General and add
                    
            C:/Program Files (x86)/Octeract/include/
                    
                
    to the Additional Include Directories

  • Click on Project then
    <Your Project Name>
    Properties then Linker then General and add
                    
            C:/Program Files (x86)/Octeract/bin
                    
                
    to the Additional Library Directories

  • Click on Project then
    <Your Project Name>
    Properties then C/C++ then Code Generation then Runtime Library and set it to Multi-threaded DLL (/MD)

  • Click on Project then
    <Your Project Name>
    Properties then Apply

  • Finally click Build then Build
    <Your Project Name>




Step 3: Run the Example

Click on the green arrow found at the top of your Visual Studio screen, in the middle.


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





C++ on Linux


Before reaching this guide, you'll have:



Step 1: Open your Terminal

Open your favorite terminal.




Step 2: Get a C++ Compiler Installed

For Ubuntu:
                
apt install clang
                
            



For Centos7:
                
yum install centos-release-scl-rh
yum install llvm-toolset-7.0-clang
alternatives --install /usr/bin/clang++  clang++\    /opt/rh/llvm-toolset-7/root/usr/bin/clang++ 100
                
            



For Centos8:
                
yum install epel-release
yum install gcc-toolset-10
alternatives --install /usr/bin/g++  g++\    /opt/rh/gcc-toolset-10/root/usr/bin/g++ 100
                
            
Watch out for step 3 you will need to change
clang++
for
g++




Step 3: 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 4: Compile the Program

To compile your freshly written program please execute the following command:
                
clang++ < path-to-api-exmple.cpp > -locteract -o api-example
                
            



Step 5: 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.
                
./api-example
                
            

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


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