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        /*------------------------------------------------------------------*/
63        /* Step 4. Retrive model status and objective.                      */
64        /* For MIP(MILP,MIQP, MIQCP) problems, if the solving process       */
65        /* terminates early due to reasons such as timeout or interruption, */
66        /* the model status will indicate termination by timeout (or        */
67        /* interruption, etc.). However, suboptimal solutions may still     */
68        /* exist, making it necessary to check the SolCount property.       */
69        /*------------------------------------------------------------------*/
70        if (model.get(MDO_IntAttr_Status) == MDO_OPTIMAL || model.get(MDO_IntAttr_Status) == MDO_SUB_OPTIMAL ||
71            model.get(MDO_IntAttr_SolCount) != 0)
72        {
73            cout << "Optimal objective value is: " << model.get(MDO_DoubleAttr_ObjVal) << endl;
74            cout << "Decision variables: " << endl;

示例 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.                                       */
60        /*------------------------------------------------------------------*/
61        model.optimize();
62
63        /*------------------------------------------------------------------*/
64        /* Step 4. Retrive model status and objective.                      */
65        /* For MIP(MILP,MIQP, MIQCP) problems, if the solving process       */
66        /* terminates early due to reasons such as timeout or interruption, */
67        /* the model status will indicate termination by timeout (or        */
68        /* interruption, etc.). However, suboptimal solutions may still     */
69        /* exist, making it necessary to check the SolCount property.       */
70        /*------------------------------------------------------------------*/
71        if (model.get(MDO_IntAttr_Status) == MDO_OPTIMAL || model.get(MDO_IntAttr_Status) == MDO_SUB_OPTIMAL ||
72            model.get(MDO_IntAttr_SolCount) != 0)
73        {
74            cout << "Optimal objective value is: " << model.get(MDO_DoubleAttr_ObjVal) << endl;
75            cout << "Decision variables: " << endl;
76            int i = 0;
77            for (auto v : x)
78            {
79                cout << "x[" << i++ << "] = " << v.get(MDO_DoubleAttr_X) << endl;
80            }
81        }
82        else
83        {
84            cout<< "No feasible solution." << endl;
85        }
86    } 
87    catch (MDOException& e) 
88    { 
89        cout << "Error code = " << e.getErrorCode() << endl;
90        cout << e.getMessage() << endl;
91    } 
92    catch (...) 
93    { 
94        cout << "Error during optimization." << endl;
95    }
96
97    return static_cast<int>(MDO_OKAY);
98}