5.4.3. C++ 的QCP建模和优化

在本节中,我们将使用 MindOpt C++ API,以按行输入的形式来建模以及求解 二次约束规划问题示例 中的问题。

首先,引入头文件:

25#include "MindoptCpp.h"

并创建优化模型:

32    /*------------------------------------------------------------------*/
33    /* Step 1. Create environment and model.                            */
34    /*------------------------------------------------------------------*/
35    MDOEnv env = MDOEnv();
36    MDOModel model = MDOModel(env);

接下来,我们通过 MDOModel::set() 将目标函数设置为 最小化,并调用 MDOModel::addVar() 来添加四个优化变量,定义其下界、上界、名称和类型,以及其在目标函数中线性项的系数(有关 MDOModel::set()MDOModel::addVar() 的详细使用方式,请参考 C++ API):

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

接着我们开始设置二次目标函数。我们创建一个二次表达式 MDOQuadExpr, 再调用 MDOQuadExpr::addTerms 来设置目标函数线性部分。 obj_idx 表示线性部分的索引,obj_val 表示与 obj_idx 中的索引相对应的非零系数值,obj_nnz 代表线性部分的非零元的个数。

54        /* Linear part in the objective: 1 x0 + 1 x1 + 1 x2 + 1 x3 */
55        int    obj_nnz   = 4;
56        MDOVar obj_idx[] = { x[0], x[1], x[2], x[3] };
57        double obj_val[] = { 1.0,  1.0,  1.0,  1.0 };
85        /* Create objective. */
86        MDOQuadExpr obj = MDOQuadExpr(0.0);
87        /* Add linear terms */
88        obj.addTerms(obj_val, obj_idx, obj_nnz);

然后,调用 MDOQuadExpr::addTerms 来设置目标的二次项系数 \(Q\)。 其中,qo_values 表示要添加的二次项的系数,qo_col1qo_col2 表示与qo_values相对应的二次项的第一个变量和第二个变量,qo_nnz 表示二次项中的非零元个数。

58        /* Quadratic part in the objective: 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1] */
59        int    qo_nnz      = 5;
60        MDOVar qo_col1[]   = { x[0], x[1], x[2], x[3], x[0] };
61        MDOVar qo_col2[]   = { x[0], x[1], x[2], x[3], x[1] };
62        double qo_values[] = { 0.5,  0.5,  0.5,  0.5,  0.5 };
89        /* Add quadratic terms */
90        obj.addTerms(qo_values, qo_col1, qo_col2, qo_nnz);

最后,我们调用 MDOModel::setObjective 设定优化目标与方向。

104        /* Set optimization sense. */
105        model.setObjective(obj, MDO_MINIMIZE);

现在我们开始添加二次约束。构建二次约束中的二次表达式的方式与二次目标函数类似。

64        /* Linear part in the first constraint: 1 x0 + 1 x1 + 2 x2 + 3 x3 */
65        int    c1_nnz   = 4; 
66        MDOVar c1_idx[] = { x[0], x[1], x[2], x[3] };
67        double c1_val[] = { 1.0,  1.0,  2.0,  3.0 };
68        /* Quadratic part in the first constraint: - 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1] */
69        int    qc1_nnz      = 5;
70        MDOVar qc1_col1[]   = { x[0], x[1], x[2], x[3], x[0] };
71        MDOVar qc1_col2[]   = { x[0], x[1], x[2], x[3], x[1] };
72        double qc1_values[] = { -0.5, -0.5, -0.5, -0.5, -0.5 };
93        MDOQuadExpr c1 = MDOQuadExpr(0.0);
94        c1.addTerms(c1_val, c1_idx, c1_nnz);
95        c1.addTerms(qc1_values, qc1_col1, qc1_col2, qc1_nnz);
74        /* Linear part in the second constraint: 1 x0 - 1 x2 + 6 x3 */
75        int    c2_nnz   = 3;
76        MDOVar c2_idx[] = { x[0], x[2], x[3] };
77        double c2_val[] = { 1.0,  -1.0, 6.0 };
78        /* Quadratic part in the second constraint: 1/2 [x1^2] */
79        int    qc2_nnz      = 1;
80        MDOVar qc2_col1[]   = { x[1] };
81        MDOVar qc2_col2[]   = { x[1] };
82        double qc2_values[] = { 0.5 };
 99        MDOQuadExpr c2 = MDOQuadExpr(0.0);
100        c2.addTerms(c2_val, c2_idx, c2_nnz);
101        c2.addTerms(qc2_values, qc2_col1, qc2_col2, qc2_nnz);

然后,我们调用 MDOModel::addQConstr 将二次约束添加至模型中。

96        model.addQConstr(c1, MDO_GREATER_EQUAL, 1.0, "c0");
102        model.addQConstr(c2, MDO_LESS_EQUAL, 1.0, "c1");

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

110        /* Solve the problem. */
111        model.optimize();

示例 MdoQcoEx1.cpp 提供了完整源代码:

  1/**
  2 *  Description
  3 *  -----------
  4 *
  5 *  Quadratically constrained quadratic optimization (row-wise input).
  6 *
  7 *  Formulation
  8 *  -----------
  9 *
 10 *  Minimize
 11 *    obj: 1 x0 + 1 x1 + 1 x2 + 1 x3
 12 *         + 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1]
 13 *
 14 *  Subject To
 15 *   c0 : 1 x0 + 1 x1 + 2 x2 + 3 x3 - 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1] >= 1
 16 *   c1 : 1 x0 - 1 x2 + 6 x3 + 1/2 [x1^2] <= 1
 17 *  Bounds
 18 *    0 <= x0 <= 10
 19 *    0 <= x1
 20 *    0 <= x2
 21 *    0 <= x3
 22 *  End
 23 */
 24#include <iostream>
 25#include "MindoptCpp.h"
 26#include <vector>
 27
 28using namespace std;
 29
 30int main(void)
 31{
 32    /*------------------------------------------------------------------*/
 33    /* Step 1. Create environment and model.                            */
 34    /*------------------------------------------------------------------*/
 35    MDOEnv env = MDOEnv();
 36    MDOModel model = MDOModel(env);
 37    
 38    try 
 39    {
 40        /*------------------------------------------------------------------*/
 41        /* Step 2. Input model.                                             */
 42        /*------------------------------------------------------------------*/
 43        /* Change to minimization problem. */
 44        model.set(MDO_IntAttr_ModelSense, MDO_MINIMIZE);
 45
 46        /* Add variables. */
 47        std::vector<MDOVar> x;
 48        x.push_back(model.addVar(0.0, 10.0,         0.0, MDO_CONTINUOUS, "x0"));
 49        x.push_back(model.addVar(0.0, MDO_INFINITY, 0.0, MDO_CONTINUOUS, "x1"));
 50        x.push_back(model.addVar(0.0, MDO_INFINITY, 0.0, MDO_CONTINUOUS, "x2"));
 51        x.push_back(model.addVar(0.0, MDO_INFINITY, 0.0, MDO_CONTINUOUS, "x3"));
 52
 53        /* Prepare model data. */
 54        /* Linear part in the objective: 1 x0 + 1 x1 + 1 x2 + 1 x3 */
 55        int    obj_nnz   = 4;
 56        MDOVar obj_idx[] = { x[0], x[1], x[2], x[3] };
 57        double obj_val[] = { 1.0,  1.0,  1.0,  1.0 };
 58        /* Quadratic part in the objective: 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1] */
 59        int    qo_nnz      = 5;
 60        MDOVar qo_col1[]   = { x[0], x[1], x[2], x[3], x[0] };
 61        MDOVar qo_col2[]   = { x[0], x[1], x[2], x[3], x[1] };
 62        double qo_values[] = { 0.5,  0.5,  0.5,  0.5,  0.5 };
 63
 64        /* Linear part in the first constraint: 1 x0 + 1 x1 + 2 x2 + 3 x3 */
 65        int    c1_nnz   = 4; 
 66        MDOVar c1_idx[] = { x[0], x[1], x[2], x[3] };
 67        double c1_val[] = { 1.0,  1.0,  2.0,  3.0 };
 68        /* Quadratic part in the first constraint: - 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1] */
 69        int    qc1_nnz      = 5;
 70        MDOVar qc1_col1[]   = { x[0], x[1], x[2], x[3], x[0] };
 71        MDOVar qc1_col2[]   = { x[0], x[1], x[2], x[3], x[1] };
 72        double qc1_values[] = { -0.5, -0.5, -0.5, -0.5, -0.5 };
 73
 74        /* Linear part in the second constraint: 1 x0 - 1 x2 + 6 x3 */
 75        int    c2_nnz   = 3;
 76        MDOVar c2_idx[] = { x[0], x[2], x[3] };
 77        double c2_val[] = { 1.0,  -1.0, 6.0 };
 78        /* Quadratic part in the second constraint: 1/2 [x1^2] */
 79        int    qc2_nnz      = 1;
 80        MDOVar qc2_col1[]   = { x[1] };
 81        MDOVar qc2_col2[]   = { x[1] };
 82        double qc2_values[] = { 0.5 };
 83
 84        /* Construct model. */
 85        /* Create objective. */
 86        MDOQuadExpr obj = MDOQuadExpr(0.0);
 87        /* Add linear terms */
 88        obj.addTerms(obj_val, obj_idx, obj_nnz);
 89        /* Add quadratic terms */
 90        obj.addTerms(qo_values, qo_col1, qo_col2, qo_nnz);
 91
 92        /* Add 1st quadratic constraint. */
 93        MDOQuadExpr c1 = MDOQuadExpr(0.0);
 94        c1.addTerms(c1_val, c1_idx, c1_nnz);
 95        c1.addTerms(qc1_values, qc1_col1, qc1_col2, qc1_nnz);
 96        model.addQConstr(c1, MDO_GREATER_EQUAL, 1.0, "c0");
 97
 98        /* Add 2nd quadratic constraint. */
 99        MDOQuadExpr c2 = MDOQuadExpr(0.0);
100        c2.addTerms(c2_val, c2_idx, c2_nnz);
101        c2.addTerms(qc2_values, qc2_col1, qc2_col2, qc2_nnz);
102        model.addQConstr(c2, MDO_LESS_EQUAL, 1.0, "c1");
103
104        /* Set optimization sense. */
105        model.setObjective(obj, MDO_MINIMIZE);
106
107        /*------------------------------------------------------------------*/
108        /* Step 3. Solve the problem and populate optimization result.                */
109        /*------------------------------------------------------------------*/
110        /* Solve the problem. */
111        model.optimize();
112
113        if(model.get(MDO_IntAttr_Status) == MDO_OPTIMAL)
114        {
115            cout << "Optimal objective value is: " << model.get(MDO_DoubleAttr_ObjVal) << endl;
116            cout << "Decision variables:" << endl;
117            int i = 0;
118            for (auto v : x)
119            {
120                cout << "x[" << i++ << "] = " << v.get(MDO_DoubleAttr_X) << endl;
121            }
122        }
123        else
124        {
125            cout<< "No feasible solution." << endl;
126        }
127        
128    }
129    catch (MDOException& e) 
130    { 
131        std::cout << "Error code = " << e.getErrorCode() << std::endl;
132        std::cout << e.getMessage() << std::endl;
133    } 
134    catch (...) 
135    { 
136        std::cout << "Error during optimization." << std::endl;
137    }
138    
139    return static_cast<int>(MDO_OKAY);
140}