4.3. 使用 C++ 语言调用 MindOpt

本节通过一个简单的示例展示如何使用 C++ 语言调用 MindOpt 实现优化模型的读取与求解。

4.3.1. 编辑.cpp文件

以下我们将展示如何在 .cpp 文件中调用 MindOpt C++ API 读取优化问题模型文件并进行求解。

首先导入 C++ 头文件:

8#include "MindoptCpp.h"

然后创建环境和优化模型:

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

之后使用 MDOModel::read() 来读取 MPS/LP 格式的优化问题:

35        m.read(argv[1]);

最后使用 MDOModel::optimize() 来求解问题,并调用 MDOModel::get() 来得到最优解的目标函数值。

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

以下为完整的源代码文件 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}

在安装路径 <MDOHOME>/<VERSION>/examples/cpp 下可以找到更多 C++ 语言相关示例文件。

4.3.2. Linux平台上编译

我们在安装路径 <MDOHOME>/<VERSION>/examples/cpp 下提供了示例文件。在 linux 平台上有两个版本的 C++ 静态库。 libmindoptcpp_g++4.8.a 适用于 g++ 编译器版本大于等于 4.8 且小于 5.2, libmindoptcpp_g++5.2.a 适用于 g++ 编译器版本大于等于5.2。 以 linux x86, g++ 5.5 为例,在此路径下执行以下指令编译示例代码、链接 MindOpt 动态库并执行优化求解:

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

同时我们提供了 Makefile 脚本,可以通过 make 指令来编译和执行示例代码:

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

# 使用 'make test' 命令自动编译和执行所有示例
make test

4.3.3. macOS平台上编译

我们在安装路径 <MDOHOME>/<VERSION>/examples/cpp 下提供了示例文件。以 macOS x86 为例,在此路径下执行以下指令编译示例代码、链接 MindOpt 动态库并执行优化求解:

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

同时我们提供了 Makefile 脚本,可以通过 make 指令来编译和执行示例代码:

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

# 使用 'make test' 命令自动编译和执行所有示例
make test

4.3.4. Windows平台上编译

对于 Windows 平台用户,首先安装 Visual Studio 2019 或以上版本,之后打开 x64 Native Tools Command Prompt (在开始菜单中键入 cmd 即可看到此程序) 并切换路径至 <MDOHOME>\<VERSION>\examples\cpp,在此路径下执行以下指令编译示例代码、链接 MindOpt 动态库并执行优化求解:

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

同时我们也提供了 Makefile 脚本,可以通过 nmake 指令来编译和执行示例代码:

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

# 使用 'nmake test' 命令自动编译和执行所有示例
nmake test

Note

  • 为使应用程序能够正确地定位到动态库,用户需在环境变量中指定动态库的路径。若环境变量未指定,则用户需按照操作系统查找动态库的逻辑,将动态库文件置于合适位置。关于环境变量的设置,请参阅 安装说明

  • 关于 C++ 语言编译器版本的支持,请参阅 支持平台 中的说明。