5.2.3. 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 "MindoptCpp.h"

Create an optimization model model:

36    MDOEnv env = MDOEnv();
37    MDOModel model = MDOModel(env);

Next, we set the optimization sense to minimization via MDOModel::set() and add four decision variables using MDOModel::addVar() (please refer to Attributes for the detailed usages of model attributes, and C++ API for other CPP API information):

44        /* Change to minimization problem. */
45        model.set(MDO_IntAttr_ModelSense, MDO_MINIMIZE);
46
47        /* Add variables. */
48        std::vector<MDOVar> x;
49        x.push_back(model.addVar(0.0, 10.0,         1.0, MDO_INTEGER,    "x0"));
50        x.push_back(model.addVar(0.0, MDO_INFINITY, 2.0, MDO_INTEGER,    "x1"));
51        x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, MDO_INTEGER,    "x2"));
52        x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, MDO_CONTINUOUS, "x3"));

Next, we input the linear constraints into the model model:

54        /* Add constraints. */
55        model.addConstr(1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3] >= 1.0, "c0");
56        model.addConstr(1.0 * x[0]              - 1.0 * x[2] + 6.0 * x[3] == 1.0, "c1");

Once the model is constructed, we call Model.optimize() to solve the problem:

61        model.optimize();

Lastly, we can retrieve the optimal objective value and solutions by using MDOModel::get() to get attribute ObjVal and X. Please refer to Attributes for more detailed explanantion.

62        if(model.get(MDO_IntAttr_Status) == MDO_OPTIMAL)
63        {
64            cout << "Optimal objective value is: " << model.get(MDO_DoubleAttr_ObjVal) << endl;
65            cout << "Decision variables: " << endl;
66            int i = 0;
67            for (auto v : x)
68            {
69                cout << "x[" << i++ << "] = " << v.get(MDO_DoubleAttr_X) << endl;
70            }
71        }
72        else
73        {
74            cout<< "No feasible solution." << endl;
75        }

Complete example codes are provided in MdoMiLoEx1.cpp.

 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 <iostream>
26#include <vector>
27#include "MindoptCpp.h"
28
29using namespace std;
30
31int main(void)
32{
33    /*------------------------------------------------------------------*/
34    /* Step 1. Create environment and model.                            */
35    /*------------------------------------------------------------------*/
36    MDOEnv env = MDOEnv();
37    MDOModel model = MDOModel(env);
38
39    try
40    {
41        /*------------------------------------------------------------------*/
42        /* Step 2. Input model.                                             */
43        /*------------------------------------------------------------------*/
44        /* Change to minimization problem. */
45        model.set(MDO_IntAttr_ModelSense, MDO_MINIMIZE);
46
47        /* Add variables. */
48        std::vector<MDOVar> x;
49        x.push_back(model.addVar(0.0, 10.0,         1.0, MDO_INTEGER,    "x0"));
50        x.push_back(model.addVar(0.0, MDO_INFINITY, 2.0, MDO_INTEGER,    "x1"));
51        x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, MDO_INTEGER,    "x2"));
52        x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, MDO_CONTINUOUS, "x3"));
53
54        /* Add constraints. */
55        model.addConstr(1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3] >= 1.0, "c0");
56        model.addConstr(1.0 * x[0]              - 1.0 * x[2] + 6.0 * x[3] == 1.0, "c1");
57
58        /*------------------------------------------------------------------*/
59        /* Step 3. Solve the problem and populate optimization result.      */
60        /*------------------------------------------------------------------*/
61        model.optimize();
62        if(model.get(MDO_IntAttr_Status) == MDO_OPTIMAL)
63        {
64            cout << "Optimal objective value is: " << model.get(MDO_DoubleAttr_ObjVal) << endl;
65            cout << "Decision variables: " << endl;
66            int i = 0;
67            for (auto v : x)
68            {
69                cout << "x[" << i++ << "] = " << v.get(MDO_DoubleAttr_X) << endl;
70            }
71        }
72        else
73        {
74            cout<< "No feasible solution." << endl;
75        }
76    } 
77    catch (MDOException& e) 
78    { 
79        cout << "Error code = " << e.getErrorCode() << endl;
80        cout << e.getMessage() << endl;
81    } 
82    catch (...) 
83    { 
84        cout << "Error during optimization." << endl;
85    }
86
87    return static_cast<int>(MDO_OKAY);
88}