4.2. 使用 C 语言调用 MindOpt

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

4.2.1. 编辑.c文件

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

首先导入 C 头文件:

9#include "Mindopt.h"

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

25    MDOenv* env;
26    MDOemptyenv(&env);
27    MDOstartenv(env);
28    MDOmodel* model;

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

34    CHECK_RESULT(MDOreadmodel(env, argv[1], &model));

之后使用 MDOoptimize() 来求解问题,并使用 MDOgetdblattr() 来查看最优解的目标函数。

40    CHECK_RESULT(MDOoptimize(model));
41    double obj;
42    CHECK_RESULT(MDOgetdblattr(model, MDO_DBL_ATTR_OBJ_VAL, &obj));

最后,使用 MDOfreemodel() 来释放内存空间。

50    MDOfreemodel(&model);
51    MDOfreeenv(&env);

以下为完整的源代码文件 read_mps.c

 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 <stdlib.h>
 9#include "Mindopt.h"
10
11
12/* Macro to check the return code */
13#define CHECK_RESULT(code) { int res = code; if (res != 0) { fprintf(stderr, "Bad code: %d\n", res); exit(res); } }
14
15int main(int argc, char* argv[])
16{
17    if (argc != 2)
18    {
19        return 0;
20    }
21    /*------------------------------------------------------------------*/
22    /* Step 1. Create a environment and model.                          */
23    /*------------------------------------------------------------------*/
24    /* Create an empty model. */
25    MDOenv* env;
26    MDOemptyenv(&env);
27    MDOstartenv(env);
28    MDOmodel* model;
29
30    /*------------------------------------------------------------------*/
31    /* Step 2. Input model.                                             */
32    /*------------------------------------------------------------------*/
33    /* Read model from file. */
34    CHECK_RESULT(MDOreadmodel(env, argv[1], &model));
35
36    /*------------------------------------------------------------------*/
37    /* Step 3. Solve the problem and print the result.                  */
38    /*------------------------------------------------------------------*/
39    /* Solve the problem. */
40    CHECK_RESULT(MDOoptimize(model));
41    double obj;
42    /* Print the result. */
43    CHECK_RESULT(MDOgetdblattr(model, MDO_DBL_ATTR_OBJ_VAL, &obj));
44    printf("Obj value: %f\n", obj);
45
46    /*------------------------------------------------------------------*/
47    /* Step 4. Free the model ane the environment.                      */
48    /*------------------------------------------------------------------*/
49    /* Free the model. */
50    MDOfreemodel(&model);
51    /* Free the environment. */
52    MDOfreeenv(&env);
53    return 0;
54}

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

4.2.2. Linux平台上编译

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

cc -std=c99 -O2 -DNDEBUG -Wl,-rpath,../../linux64-x86/lib -I../../linux64-x86/include -o read_mps read_mps.c -L../../linux64-x86/lib -lmindopt
./read_mps ../data/afiro.mps

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

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

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

4.2.3. macOS平台上编译

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

cc -std=c99 -O2 -DNDEBUG -Wl,-rpath,../../osx64-x86/lib -I../../osx64-x86/include -o read_mps read_mps.c -L../../osx64-x86/lib -lmindopt
./read_mps ../data/afiro.mps

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

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

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

4.2.4. Windows平台上编译

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

cl /I ../../win64-x86/include read_mps.c /link /LIBPATH:../../win64-x86/lib mindopt_1_0_0.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 语言编译器版本的支持,请参阅 支持平台 中的说明。