How to use C++ with Octeract Engine
explanation practical insight starter guideC++ on Windows
Before reaching this guide, you'll have:
- downloaded the Engine.
- installed the Engine.
- installed your authentication token.
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
- Next to the green arrow check that you have selected Release for the configuration and x64 for the platform
- Click on Project then
Properties then C/C++ then General and add<Your Project Name>< Path-To-Your-Octeract-Installation >/include/
- Click on Project then
Properties then Linker then General and add<Your Project Name>< Path-To-Your-Octeract-Installation >/bin/
- Click on Project then
Properties then Linker then Input in Additional Dependencies add libocteract.lib, then click Apply and OK.<Your Project Name> - Click on Project then
Properties then General then C++ Language Standard then set it to ISO C++ 17 Standard, then click Apply and OK.<Your Project Name> - Click on Project then
Properties then C/C++ then Code Generation then Runtime Library and set it to Multi-threaded DLL (/MD), then click Apply and OK.<Your Project Name> - Finally click Build then Build<Your Project Name>
Step 3: Run the Example
First verify that your license is accesible (either in the path or next to your api executable). Then click on the green arrow located 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:
- downloaded the Engine.
- installed the Engine.
- installed your authentication token.
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 4 you will need to change
clang++
forg++
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 > -I< path-to-include-installation > -L< path-to-library-installation > -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 command should look like can be seen below.
LD_LIBRARY_PATH=< path-to-library-installation >:$LD_LIBRARY_PATH ./api-example