5.1.2. C 的LP建模和优化¶
在本节中,我们将使用 MindOpt C API,以按行输入的形式来建模以及求解 线性规划问题示例 中的问题。
首先,引入头文件:
24#include "Mindopt.h"
创建优化模型:
54 CHECK_RESULT(MDOemptyenv(&env));
55 CHECK_RESULT(MDOstartenv(env));
56 CHECK_RESULT(MDOnewmodel(env, &m, MODEL_NAME, 0, NULL, NULL, NULL, NULL, NULL));
接下来,我们通过 MDOsetintattr()
将目标函数设置为 最小化,并调用 MDOaddvar()
来添加四个优化变量(关于 MDOsetintattr()
和 MDOaddvar()
的详细使用方式,请参考 C API):
61 /* Change to minimization problem. */
62 CHECK_RESULT(MDOsetintattr(m, MODEL_SENSE, MDO_MINIMIZE));
63
64 /* Add variables. */
65 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, 10.0, MDO_CONTINUOUS, "x0"));
66 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 2.0, 0, MDO_INFINITY, MDO_CONTINUOUS, "x1"));
67 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_CONTINUOUS, "x2"));
68 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_CONTINUOUS, "x3"));
我们使用以下四列数组来定义线性约束,并初始化数据: row1_idx
和 row2_idx
分别表示第一和第二个约束中非零元素的位置(索引),而 row1_val
和 row2_val
则是与之相对应的非零数值。
46 int row1_idx[] = { 0, 1, 2, 3 };
47 double row1_val[] = { 1.0, 1.0, 2.0, 3.0 };
48 int row2_idx[] = { 0, 2, 3 };
49 double row2_val[] = { 1.0, -1.0, 6.0 };
我们调用 MDOaddconstr()
输入约束:
70 /* Add constraints. */
71 CHECK_RESULT(MDOaddconstr(m, 4, row1_idx, row1_val, MDO_GREATER_EQUAL, 1.0, "c0"));
72 CHECK_RESULT(MDOaddconstr(m, 3, row2_idx, row2_val, MDO_EQUAL, 1.0, "c1"));
问题输入完成后,再调用 MDOoptimize()
求解优化问题:
77 CHECK_RESULT(MDOoptimize(m));
求解完成后,可以通过获取属性值的方式来获取对应解的优化目标值和变量的取值:
78 CHECK_RESULT(MDOgetintattr(m, STATUS, &status));
79 if (status == MDO_OPTIMAL)
80 {
81 CHECK_RESULT(MDOgetdblattr(m, OBJ_VAL, &obj));
82 printf("The optimal objective value is: %f\n", obj);
83 for (i = 0; i < 4; ++i)
84 {
85 CHECK_RESULT(MDOgetdblattrelement(m, X, i, &x));
86 printf("x[%d] = %f\n", i, x);
87 }
88 }
89 else
90 {
91 printf("No feasible solution.\n");
92 }
最后,调用 MDOfreemodel()
和 MDOfreeenv()
来释放模型:
27#define RELEASE_MEMORY \
28 MDOfreemodel(m); \
29 MDOfreeenv(env);
97 RELEASE_MEMORY;
示例 MdoLoEx1.c 提供了完整源代码:
1/**
2 * Description
3 * -----------
4 *
5 * Linear optimization (row-wise input).
6 *
7 * Formulation
8 * -----------
9 *
10 * Minimize
11 * obj: 1 x0 + 2 x1 + 1 x2 + 1 x3
12 * Subject To
13 * c0 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
14 * c1 : 1 x0 - 1 x2 + 6 x3 = 1
15 * Bounds
16 * 0 <= x0 <= 10
17 * 0 <= x1
18 * 0 <= x2
19 * 0 <= x3
20 * End
21 */
22#include <stdio.h>
23#include <stdlib.h>
24#include "Mindopt.h"
25
26/* Macro to check the return code */
27#define RELEASE_MEMORY \
28 MDOfreemodel(m); \
29 MDOfreeenv(env);
30#define CHECK_RESULT(code) { int res = code; if (res != 0) { fprintf(stderr, "Bad code: %d\n", res); RELEASE_MEMORY; return (res); } }
31#define MODEL_NAME "LP_01"
32#define MODEL_SENSE "ModelSense"
33#define STATUS "Status"
34#define OBJ_VAL "ObjVal"
35#define X "X"
36
37int main(void)
38{
39 /* Variables. */
40 MDOenv *env;
41 MDOmodel *m;
42 double obj, x;
43 int status, i;
44
45 /* Model data. */
46 int row1_idx[] = { 0, 1, 2, 3 };
47 double row1_val[] = { 1.0, 1.0, 2.0, 3.0 };
48 int row2_idx[] = { 0, 2, 3 };
49 double row2_val[] = { 1.0, -1.0, 6.0 };
50
51 /*------------------------------------------------------------------*/
52 /* Step 1. Create environment and model. */
53 /*------------------------------------------------------------------*/
54 CHECK_RESULT(MDOemptyenv(&env));
55 CHECK_RESULT(MDOstartenv(env));
56 CHECK_RESULT(MDOnewmodel(env, &m, MODEL_NAME, 0, NULL, NULL, NULL, NULL, NULL));
57
58 /*------------------------------------------------------------------*/
59 /* Step 2. Input model. */
60 /*------------------------------------------------------------------*/
61 /* Change to minimization problem. */
62 CHECK_RESULT(MDOsetintattr(m, MODEL_SENSE, MDO_MINIMIZE));
63
64 /* Add variables. */
65 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, 10.0, MDO_CONTINUOUS, "x0"));
66 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 2.0, 0, MDO_INFINITY, MDO_CONTINUOUS, "x1"));
67 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_CONTINUOUS, "x2"));
68 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_CONTINUOUS, "x3"));
69
70 /* Add constraints. */
71 CHECK_RESULT(MDOaddconstr(m, 4, row1_idx, row1_val, MDO_GREATER_EQUAL, 1.0, "c0"));
72 CHECK_RESULT(MDOaddconstr(m, 3, row2_idx, row2_val, MDO_EQUAL, 1.0, "c1"));
73
74 /*------------------------------------------------------------------*/
75 /* Step 3. Solve the problem and populate optimization result. */
76 /*------------------------------------------------------------------*/
77 CHECK_RESULT(MDOoptimize(m));
78 CHECK_RESULT(MDOgetintattr(m, STATUS, &status));
79 if (status == MDO_OPTIMAL)
80 {
81 CHECK_RESULT(MDOgetdblattr(m, OBJ_VAL, &obj));
82 printf("The optimal objective value is: %f\n", obj);
83 for (i = 0; i < 4; ++i)
84 {
85 CHECK_RESULT(MDOgetdblattrelement(m, X, i, &x));
86 printf("x[%d] = %f\n", i, x);
87 }
88 }
89 else
90 {
91 printf("No feasible solution.\n");
92 }
93
94 /*------------------------------------------------------------------*/
95 /* Step 4. Free the model. */
96 /*------------------------------------------------------------------*/
97 RELEASE_MEMORY;
98
99 return 0;
100}