5.2.3. C++ 的MILP建模和优化

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

首先,引入头文件:

27#include "MindoptCpp.h"

并创建优化模型:

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

接下来,我们通过 MDOModel::set() 设置模型属性 ModelSense,将目标函数设置为 最小化,并调用 MDOModel::addVar() 来添加四个优化变量(有关模型属性内容及其设置可参考 属性, 其他API请参考 C++ API):

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

接着,我们开始添加线性约束:

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

问题输入完成后,再调用 MDOModel::optimize() 求解优化问题:

61        model.optimize();

最后用 MDOModel::get() 查看模型属性值 ObjVal 来查看优化解的目标值,可以用 MDOModel::get() 查看变量属性值 X 来查看优化解的目标值。更多的解获取可参考 属性 和头文件定义。

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        }

示例 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}