5.2.2. C 的MILP建模和优化

在本节中,我们将使用 MindOpt C API,以按行输入的形式来建模以及求解 混合整数线性规划问题示例 中的问题。

首先,引入头文件:

25#include <stdio.h>

创建优化模型:

58    CHECK_RESULT(MDOemptyenv(&env));
59    CHECK_RESULT(MDOstartenv(env));
60    CHECK_RESULT(MDOnewmodel(env, &m, MODEL_NAME, 0, NULL, NULL, NULL, NULL, NULL));

接下来,我们通过 MDOsetintattr() 将目标函数设置为 最小化,并调用 MDOaddvar() 来添加四个优化变量。(更多API和使用方式,请参考 C API):

65    /* Change to minimization problem. */
66    CHECK_RESULT(MDOsetintattr(m, MODEL_SENSE, MDO_MINIMIZE));
67
68    /* Add variables. */
69    CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0,         10.0, MDO_INTEGER,    "x0"));
70    CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 2.0, 0, MDO_INFINITY, MDO_INTEGER,    "x1"));
71    CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_INTEGER,    "x2"));
72    CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_CONTINUOUS, "x3"));

接下来添加线性约束,需要定义其中的非零元及其左右边界。我们使用以下四列数组来定义线性约束,其中, row1_idxrow2_idx 分别表示第一和第二个约束中非零元素的位置(索引),而 row1_valrow2_val 则是与之相对应的非零数值。

49    int    row1_idx[] = { 0,   1,   2,   3   };
50    double row1_val[] = { 1.0, 1.0, 2.0, 3.0 };
51    int    row2_idx[] = { 0,    2,   3   };
52    double row2_val[] = { 1.0, -1.0, 6.0 };

调用 MDOaddconstr() 来输入约束:

74    /* Add constraints. */
75    CHECK_RESULT(MDOaddconstr(m, 4, row1_idx, row1_val, MDO_GREATER_EQUAL, 1.0, "c0"));
76    CHECK_RESULT(MDOaddconstr(m, 3, row2_idx, row2_val, MDO_EQUAL,         1.0, "c1"));

问题输入完成后,再调用 MDOoptimize() 求解优化问题。

81    CHECK_RESULT(MDOoptimize(m));

然后,我们可以通过获取属性值的方式来获取求解后的最优值 (optimal value) 和最优解 (optimal solution).

82    CHECK_RESULT(MDOgetintattr(m, STATUS, &status));
83    if (status == MDO_OPTIMAL) 
84    {
85        CHECK_RESULT(MDOgetdblattr(m, OBJ_VAL, &obj));
86        printf("The optimal objective value is %f\n", obj);
87        
88        for (i = 0; i < 4; ++i) 
89        {
90            CHECK_RESULT(MDOgetdblattrelement(m, X, i, &x));
91            printf("x%d = %f\n", i, x);
92        }
93    }
94    else 
95    {
96        printf("No feasible solution exists\n");
97    }

最后,调用 MDOfreemodel()MDOfreeenv() 来释放模型:

30#define RELEASE_MEMORY  \
31    MDOfreemodel(m);    \
32    MDOfreeenv(env);
102    RELEASE_MEMORY;

示例 MdoMiloEx1.c 提供了完整源代码:

  1/**
  2 *  Description
  3 *  -----------
  4 *
  5 *  Mixed Integer 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 *  Integers
 21 *    x0 x1 x2
 22 *  End
 23 */
 24
 25#include <stdio.h>
 26#include <stdlib.h>
 27#include "Mindopt.h"
 28
 29/* Macro to check the return code */
 30#define RELEASE_MEMORY  \
 31    MDOfreemodel(m);    \
 32    MDOfreeenv(env);
 33#define CHECK_RESULT(code) { int res = code; if (res != 0) { fprintf(stderr, "Bad code: %d\n", res); RELEASE_MEMORY; exit(res); } }
 34#define MODEL_NAME  "MILP_01"
 35#define MODEL_SENSE "ModelSense"
 36#define STATUS      "Status"
 37#define OBJ_VAL     "ObjVal"
 38#define X           "X"
 39
 40int main(void)
 41{
 42    /* Creat Model. */
 43    MDOenv *env;
 44    MDOmodel *m;
 45    double obj, x;
 46    int status, i;
 47
 48    /* Model data. */
 49    int    row1_idx[] = { 0,   1,   2,   3   };
 50    double row1_val[] = { 1.0, 1.0, 2.0, 3.0 };
 51    int    row2_idx[] = { 0,    2,   3   };
 52    double row2_val[] = { 1.0, -1.0, 6.0 };
 53
 54    /*------------------------------------------------------------------*/
 55    /* Step 1. Create environment and model.                            */
 56    /*------------------------------------------------------------------*/
 57    /* Create an empty model. */
 58    CHECK_RESULT(MDOemptyenv(&env));
 59    CHECK_RESULT(MDOstartenv(env));
 60    CHECK_RESULT(MDOnewmodel(env, &m, MODEL_NAME, 0, NULL, NULL, NULL, NULL, NULL));
 61
 62    /*------------------------------------------------------------------*/
 63    /* Step 2. Input model.                                             */
 64    /*------------------------------------------------------------------*/
 65    /* Change to minimization problem. */
 66    CHECK_RESULT(MDOsetintattr(m, MODEL_SENSE, MDO_MINIMIZE));
 67
 68    /* Add variables. */
 69    CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0,         10.0, MDO_INTEGER,    "x0"));
 70    CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 2.0, 0, MDO_INFINITY, MDO_INTEGER,    "x1"));
 71    CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_INTEGER,    "x2"));
 72    CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_CONTINUOUS, "x3"));
 73
 74    /* Add constraints. */
 75    CHECK_RESULT(MDOaddconstr(m, 4, row1_idx, row1_val, MDO_GREATER_EQUAL, 1.0, "c0"));
 76    CHECK_RESULT(MDOaddconstr(m, 3, row2_idx, row2_val, MDO_EQUAL,         1.0, "c1"));
 77
 78    /*------------------------------------------------------------------*/
 79    /* Step 3. Solve the problem and populate optimization result.      */
 80    /*------------------------------------------------------------------*/
 81    CHECK_RESULT(MDOoptimize(m));
 82    CHECK_RESULT(MDOgetintattr(m, STATUS, &status));
 83    if (status == MDO_OPTIMAL) 
 84    {
 85        CHECK_RESULT(MDOgetdblattr(m, OBJ_VAL, &obj));
 86        printf("The optimal objective value is %f\n", obj);
 87        
 88        for (i = 0; i < 4; ++i) 
 89        {
 90            CHECK_RESULT(MDOgetdblattrelement(m, X, i, &x));
 91            printf("x%d = %f\n", i, x);
 92        }
 93    }
 94    else 
 95    {
 96        printf("No feasible solution exists\n");
 97    }
 98
 99    /*------------------------------------------------------------------*/
100    /* Step 4. Free the model.                                          */
101    /*------------------------------------------------------------------*/
102    RELEASE_MEMORY;
103
104    return 0;
105}