4.3. Using C++

4.3.1. Brief description

// Method 1: A new method for creating model is introduced from version 0.19.0.
using mindopt::MdoEnv;
MdoEnv env;
MdoModel model(env);
model.readProb(filename);
model.solveProb();
model.displayResults();

// Method 2: The old method for creating model is still supported. It will be removed in future version.
/*
MdoModel model;
model.readProb(filename);
model.solveProb();
model.displayResults();
*/

The following describes how to use C++ to compile and connect to the DLL of MindOpt and provides a compilation example. The example describes how to read an optimization model from the MPS file and solve an optimization problem.

Note

The DLL is connected only when the application runs. Therefore, you must specify the path of the DLL in an environment variable so that the application can correctly locate the DLL. If the corresponding environment variable is not specified, you must place the DLL beside the application. For more information about how to set environment variables, see Installation instructions. For more information about the support for C++ compiler versions, see Supported platforms.

Warning

The Linux/OSX DLL of mindoptcpp is automatically generated when the following example is run. In other words, you must run this sample at least once before calling the C++ API.

4.3.2. Windows

MindOpt has been tested in Visual Studio 2017/2019. For more information about the compilation examples of the C++ programming language, see <MDOHOME>/<VERSION>/examples/CPP.

To use Visual Studio to compile a DLL, perform the following steps:

  1. Create an empty Visual Studio project.

  2. Select x64 as the solution platform.

  3. Set the include directory (<MDOHOME>/<VERSION>/win64-x86/include).

  4. Set the library directory (<MDOHOME>/<VERSION>/win64-x86/lib).

  5. Add the library files (Release mode: mindopt_x_x_x.lib and mindoptcpp_x_x_x.lib; Debug mode: mindopt_x_x_x.lib and mindoptcpp_x_x_xd.lib).

Note

Visual Studio uses different DLLs (msvcrt.lib for multi-threaded DLL or msvcrtd.lib for multi-threaded DLL debug) in different compilation environments (Release mode or Debug mode). You must select the correct MindOpt DLL (mindoptcpp_x_x_x.lib or mindoptcpp_x_x_xd.lib) to avoid running errors.

Note

You may encounter an error during compilation if you use Visual Studio of an earlier version. In this case, you can compile the C++ API mindoptcpp to avoid errors. Compilation procedure is as follows:

  1. Create an empty Visual Studio project.

  2. Select x64 as the solution platform.

  3. Set the include directories (<MDOHOME>/<VERSION>/win64-x86/include and <MDOHOME>/<VERSION>/win64-x86/src).

  4. Set the library directory (<MDOHOME>/<VERSION>/win64-x86/lib).

  5. Add the library file (mindopt_x_x_x.lib).

  6. Add the source code of the C++ API mindoptcpp (<MDOHOME>/<VERSION>/win64-x86/src/MindoptCppModel.cpp).

  7. Right-click the target node, choose Attributes-> C/C++ > Preprocessor > Preprocessor Definition, and add MDO_BUILDING_DLL.

4.3.3. Linux

For more information about the compilation examples of the C++ programming language, see <MDOHOME>/<VERSION>/examples/CPP. The compilation method and test command are as follows:

cd <MDOHOME>/<VERSION>/examples/CPP
make -f Makefile all
make -f Makefile test

Note

You must run this sample at least once to generate the DLL of mindoptcpp.

The makefile file used during compilation is as follows:

INCPATHS=-I../../linux64-x86/include -I.
LIBPATHS=-L../../linux64-x86/lib
MINDOPTLIB=-lmindopt -lmindoptcpp

CCOPT=-std=c++11
LDOPT=-Wl,-rpath-link,./../../linux64-x86/lib -Wl,-rpath,'$$ORIGIN/../../linux64-x86/lib' -pthread -lm -lstdc++ -Wl,--no-as-needed -ldl
CC=g++ -m64
LD=g++ -m64

EXAMPLES=mindoptcpp\
      MdoMps

mindoptcpp:
      make install -C ../../linux64-x86/src

MdoMps: MdoMps.cpp
      $(CC) -c $(INCPATHS) $(CCOPT) -o MdoMps.o MdoMps.cpp
      $(LD) $(LIBPATHS) MdoMps.o  $(MINDOPTLIB) $(LDOPT) -o MdoMps

4.3.4. OSX

For more information about the compilation examples of the C++ programming language, see <MDOHOME>/<VERSION>/examples/CPP. The compilation method and test command are as follows:

cd <MDOHOME>/<VERSION>/examples/CPP
make -f Makefile all
make -f Makefile test

Note

You must run this sample at least once to generate the DLL of mindoptcpp.

The makefile file for compilation is as follows:

INCPATHS=-I../../osx64-x86/include
LIBPATHS=-L../../osx64-x86/lib
MINDOPTLIB=-lmindopt.x.x.x -lmindoptcpp.x.x.x -rpath @executable_path/../../osx64-x86/lib/ -lpthread

CCOPT=-std=c++11
LDOPT=-Wl,-headerpad,128
CC=clang++
LD=clang++

%.o: %.cpp
      $(CC) -c -g $(INCPATHS) $(CCOPT) -o $@ $<

mindoptcpp:
      make install -C ../../osx64-x86/src

MdoMps: MdoMps.o
      $(CC) -g $(LIBPATHS) $(LDOPT) -o $@ $< $(MINDOPTLIB)
      install_name_tool -change libmindopt.x.x.x.dylib `pwd`/../../osx64-x86/lib/libmindopt.x.x.x.dylib $@
      install_name_tool -change libmindoptcpp.x.x.x.dylib `pwd`/../../osx64-x86/lib/libmindoptcpp.x.x.x.dylib $@

4.3.5. C++ compilation example: MdoMps

The following part describes how to call the C++ API of MindOpt to read an optimization problem file in the MPS/LP format and solve the problem.

Load the C++ header file:

8#include "MindoptCpp.h"

Create an optimization model:

21    /*------------------------------------------------------------------*/
22    /* Step 1. Create a model and change the parameters.                */
23    /*------------------------------------------------------------------*/
24    /* Create an empty model. */
25    MdoModel model;

Call mindopt::MdoModel::readProb() to read an optimization problem file in the MPS/LP format:

29        /*------------------------------------------------------------------*/
30        /* Step 2. Input model.                                             */
31        /*------------------------------------------------------------------*/
32        /* Read model from file. */
33        model.readProb(argv[1]);

Call mindopt::MdoModel::solveProb() to solve the optimization problem, and call mindopt::MdoModel::displayResults() to view the optimization result.

35        /*------------------------------------------------------------------*/
36        /* Step 3. Solve the problem and populate the result.               */
37        /*------------------------------------------------------------------*/
38        /* Solve the problem. */
39        model.solveProb();
40        model.displayResults();

MdoMps.cpp is the source code file.

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

You can find more C++ example files under <MDOHOME>/<VERSION>/examples/CPP of the installation package.