5.1.2. LP Modeling and Optimization in CΒΆ
In this section, we will utilize MindOpt C API to model and solve the linear optimization problem in Example of Linear Programming.
First of all, include the header files:
24#include "Mindopt.h"
Create an optimization model m
:
54 CHECK_RESULT(MDOemptyenv(&env));
55 CHECK_RESULT(MDOstartenv(env));
56 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 the detailed usages of MDOsetintattr()
and MDOaddvar()
):
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"));
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.
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 };
We call MDOaddconstr()
to input the linear constraints into the model m
:
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"));
Once the model is constructed, we call MDOoptimize()
to solve the problem:
77 CHECK_RESULT(MDOoptimize(m));
Then, we can retrieive the optimal objective value and solutions as follows:
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 }
Lastly, we call MDOfreemodel()
and MDOfreeenv()
to free the model:
27#define RELEASE_MEMORY \
28 MDOfreemodel(m); \
29 MDOfreeenv(env);
97 RELEASE_MEMORY;
Complete example codes are provided in 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}