4.3. Call MindOpt with C++

This section uses a simple example to show how to use the C++ language to call MindOpt to read and solve the optimization model.

4.3.1. Edit .cpp File

Below we will show how to call MindOpt C++ API in the .cpp file to read the optimization problem model file and solve it.

First import the C++ header file:

8#include "MindoptCpp.h"

Then create the environment and optimize the model:

26    MDOEnv env = MDOEnv();
27    MDOModel m = MDOModel(env);

Then use MDOModel::read() to read the optimization problem in MPS/LP format:

35        m.read(argv[1]);

Finally, use MDOModel::optimize() to solve the problem, and call MDOModel::get() to get the objective function value of the optimal solution.

40        m.optimize();
41        cout << "Total meal cost: " << m.get(MDO_DoubleAttr_ObjVal) << endl;

Below is the complete source code file read_mps.cpp.

 1/**
 2 *  Description
 3 *  -----------
 4 *
 5 *  Read model from an MPS/LP file, and call optimizer to solve the problem.
 6 */
 7#include <iostream>
 8#include "MindoptCpp.h"
 9
10
11using namespace std;
12
13int main(
14        int argc,
15        char * argv[])
16{
17    if (argc != 2)
18    {
19        return 0;
20    }
21
22    /*------------------------------------------------------------------*/
23    /* Step 1. Create a model and change the parameters.                */
24    /*------------------------------------------------------------------*/
25    /* Create an empty model. */
26    MDOEnv env = MDOEnv();
27    MDOModel m = MDOModel(env);
28
29    try
30    {
31        /*--------------------------------------------------------------*/
32        /* Step 2. Input model.                                         */
33        /*--------------------------------------------------------------*/
34        /* Read model from file. */
35        m.read(argv[1]);
36        /*--------------------------------------------------------------*/
37        /* Step 3. Solve the problem and print the result.              */
38        /*--------------------------------------------------------------*/
39        /* Solve the problem. */
40        m.optimize();
41        cout << "Total meal cost: " << m.get(MDO_DoubleAttr_ObjVal) << endl;
42    }
43    catch (MDOException & e)
44    {
45        std::cerr << "===================================" << std::endl;
46        std::cerr << "Error   : code <" << e.getErrorCode() << ">" << std::endl;
47        std::cerr << "Reason  : " << e.getMessage() << std::endl;
48        std::cerr << "===================================" << std::endl;
49
50        return static_cast<int>(e.getErrorCode());
51    }
52
53    return 0;
54}

You can find more C++ language related example files under the installation path <MDOHOME>/<VERSION>/examples/cpp.

4.3.2. Compile on Linux Platforms

We provide example files under the installation path <MDOHOME>/<VERSION>/examples/cpp. There are two versions of the C++ static library on the linux platform. libmindoptcpp_g++4.8.a for g++ compiler version greater than or equal to 4.8 and less than 5.2, libmindoptcpp_g++ +5.2.a for g++ compiler version greater than or equal to 5.2. Taking linux x86, g++ 5.5 as an example, execute the following commands in this path to compile the sample code, link MindOpt dynamic library and perform optimization solution:

c++ -std=c++11 -O2 -DNDEBUG -Wl,-rpath,../../linux64-x86/lib -I../../linux64-x86/include -o read_mps read_mps.cpp -L../../linux64-x86/lib -lmindoptcpp_g++5.2 -lmindopt
./read_mps ../data/afiro.mps

At the same time, we provide a Makefile script, which can compile and execute the sample code through the make command:

# Compile and execute
make read_mps
./read_mps ../data/afiro.mps

# Automatically compile and execute all examples using the 'make test' command
make test

4.3.3. Compile on macOS Platforms

We provide example files under the installation path <MDOHOME>/<VERSION>/examples/cpp. Taking macOS x86 as an example, execute the following commands in this path to compile the sample code, link MindOpt dynamic library and perform optimization solution:

c++ -std=c++11 -O2 -DNDEBUG -Wl,-rpath,../../osx64-x86/lib -I../../osx64-x86/include -o read_mps read_mps.cpp -L../../osx64-x86/lib -lmindoptcpp -lmindopt
./read_mps ../data/afiro.mps

At the same time, we provide a Makefile script, which can compile and execute the sample code through the make command:

# Compile and execute
make read_mps
./read_mps ../data/afiro.mps

# Automatically compile and execute all examples using the 'make test' command
make test

4.3.4. Compile on Windows Platform

For Windows platform users, first install and use Visual Studio 2019 or above, then open x64 Native Tools Command Prompt (You can see the program by typing cmd in the Start menu) and switch the path to <MDOHOME>\<VERSION>\examples\cpp, under this path Execute the following commands to compile the sample code, link the MindOpt dynamic library and perform an optimization solution:

cl /I ../../win64-x86/include read_mps.cpp /link /LIBPATH:../../win64-x86/lib mindopt_1_0_0.lib mindopt_c++mt.lib
read_mps.exe ..\data\afiro.mps

At the same time, we also provide a Makefile script, which can compile and execute the sample code through the nmake command:

# Compile and execute
nmake read_mps
read_mps.exe ..\data\afiro.mps

# Automatically compile and execute all examples with the 'nmake test' command
nmake test

Note

  • In order for the application to correctly locate the dynamic library, the user needs to specify the path of the dynamic library in the environment variable. If the environment variable is not specified, the user needs to place the dynamic library file in an appropriate location according to the logic of the operating system to find the dynamic library. For environment variable settings, see Installation Instructions.

  • For supported C++ language compiler versions, see Supported Platform.