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 /* Create an empty model. */
59 CHECK_RESULT(MDOemptyenv(&env));
60 CHECK_RESULT(MDOstartenv(env));
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 /*------------------------------------------------------------------*/
66 /* Change to minimization problem. */
67 CHECK_RESULT(MDOsetintattr(m, MODEL_SENSE, MDO_MINIMIZE));
68
69 /* Add variables. */
70 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, 10.0, MDO_INTEGER, "x0"));
71 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 2.0, 0, MDO_INFINITY, MDO_INTEGER, "x1"));
72 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_INTEGER, "x2"));
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 /* Model data. */
50 int row1_idx[] = { 0, 1, 2, 3 };
51 double row1_val[] = { 1.0, 1.0, 2.0, 3.0 };
52 int row2_idx[] = { 0, 2, 3 };
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"));
Once the model is constructed, we call MDOoptimize()
to solve the problem:
81 /*------------------------------------------------------------------*/
Then, we can retrieive the optimal objective value and solutions as follows:
82 CHECK_RESULT(MDOoptimize(m));
83
84 /*------------------------------------------------------------------*/
85 /* Step 4. Retrive model status and objective. */
86 /* For MIP(MILP,MIQP, MIQCP) problems, if the solving process */
87 /* terminates early due to reasons such as timeout or interruption, */
88 /* the model status will indicate termination by timeout (or */
89 /* interruption, etc.). However, suboptimal solutions may still */
90 /* exist, making it necessary to check the SolCount property. */
91 /*------------------------------------------------------------------*/
92 CHECK_RESULT(MDOgetintattr(m, STATUS, &status));
93 CHECK_RESULT(MDOgetintattr(m, SOL_COUNT, &solcount));
94 if (status == MDO_OPTIMAL || status == MDO_SUB_OPTIMAL || solcount != 0)
95 {
96 CHECK_RESULT(MDOgetdblattr(m, OBJ_VAL, &obj));
97 printf("The optimal objective value is %f\n", obj);
Lastly, we call MDOfreemodel()
and MDOfreeenv()
to free the model:
30#define RELEASE_MEMORY \
31 MDOfreemodel(m); \
32 MDOfreeenv(env);
102 printf("x%d = %f\n", i, x);
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 SOL_COUNT "SolCount"
37#define STATUS "Status"
38#define OBJ_VAL "ObjVal"
39#define X "X"
40
41int main(void)
42{
43 /* Creat Model. */
44 MDOenv *env;
45 MDOmodel *m;
46 double obj, x;
47 int i, solcount, status;
48
49 /* Model data. */
50 int row1_idx[] = { 0, 1, 2, 3 };
51 double row1_val[] = { 1.0, 1.0, 2.0, 3.0 };
52 int row2_idx[] = { 0, 2, 3 };
53 double row2_val[] = { 1.0, -1.0, 6.0 };
54
55 /*------------------------------------------------------------------*/
56 /* Step 1. Create environment and model. */
57 /*------------------------------------------------------------------*/
58 /* Create an empty model. */
59 CHECK_RESULT(MDOemptyenv(&env));
60 CHECK_RESULT(MDOstartenv(env));
61 CHECK_RESULT(MDOnewmodel(env, &m, MODEL_NAME, 0, NULL, NULL, NULL, NULL, NULL));
62
63 /*------------------------------------------------------------------*/
64 /* Step 2. Input model. */
65 /*------------------------------------------------------------------*/
66 /* Change to minimization problem. */
67 CHECK_RESULT(MDOsetintattr(m, MODEL_SENSE, MDO_MINIMIZE));
68
69 /* Add variables. */
70 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, 10.0, MDO_INTEGER, "x0"));
71 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 2.0, 0, MDO_INFINITY, MDO_INTEGER, "x1"));
72 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_INTEGER, "x2"));
73 CHECK_RESULT(MDOaddvar(m, 0, NULL, NULL, 1.0, 0, MDO_INFINITY, MDO_CONTINUOUS, "x3"));
74
75 /* Add constraints. */
76 CHECK_RESULT(MDOaddconstr(m, 4, row1_idx, row1_val, MDO_GREATER_EQUAL, 1.0, "c0"));
77 CHECK_RESULT(MDOaddconstr(m, 3, row2_idx, row2_val, MDO_EQUAL, 1.0, "c1"));
78
79 /*------------------------------------------------------------------*/
80 /* Step 3. Solve the problem. */
81 /*------------------------------------------------------------------*/
82 CHECK_RESULT(MDOoptimize(m));
83
84 /*------------------------------------------------------------------*/
85 /* Step 4. Retrive model status and objective. */
86 /* For MIP(MILP,MIQP, MIQCP) problems, if the solving process */
87 /* terminates early due to reasons such as timeout or interruption, */
88 /* the model status will indicate termination by timeout (or */
89 /* interruption, etc.). However, suboptimal solutions may still */
90 /* exist, making it necessary to check the SolCount property. */
91 /*------------------------------------------------------------------*/
92 CHECK_RESULT(MDOgetintattr(m, STATUS, &status));
93 CHECK_RESULT(MDOgetintattr(m, SOL_COUNT, &solcount));
94 if (status == MDO_OPTIMAL || status == MDO_SUB_OPTIMAL || solcount != 0)
95 {
96 CHECK_RESULT(MDOgetdblattr(m, OBJ_VAL, &obj));
97 printf("The optimal objective value is %f\n", obj);
98
99 for (i = 0; i < 4; ++i)
100 {
101 CHECK_RESULT(MDOgetdblattrelement(m, X, i, &x));
102 printf("x%d = %f\n", i, x);
103 }
104 }
105 else
106 {
107 printf("No feasible solution exists\n");
108 }
109
110 /*------------------------------------------------------------------*/
111 /* Step 4. Free the model. */
112 /*------------------------------------------------------------------*/
113 RELEASE_MEMORY;
114
115 return 0;
116}