5.2.2. MILP Modeling and Optimization in CΒΆ

In this section, we will utilize MindOpt C API to model and solve the MILP problem in Example of Mixed-Integer Linear Programming.

First of all, include the header files:

27#include "Mindopt.h"

Create an optimization model m:

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

Next, we set the optimization sense to minimization via MDOsetintattr() and add four decision variables using MDOaddvar() (please refer to C API for more detailed usages of 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"));

Then we set the constraint matrix \(A\). There are two rows in \(A\) and we use the following four arrays to represent this matrix. In this context, row1_idx and row2_idx represent the positions of the non-zero elements in the first and second row of \(A\), respectively. Meanwhile, row1_val and row2_val hold the values of corresponding non-zero elements.

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 };

We call MDOaddconstr() to input the linear constraints into the model m:

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"));

Once the model is constructed, we call MDOoptimize() to solve the problem:

81    CHECK_RESULT(MDOoptimize(m));

Then, we can retrieive the optimal objective value and solutions as follows:

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    }

Lastly, we call MDOfreemodel() and MDOfreeenv() to free the model:

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

Complete example codes are provided in 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}