5.5.3. MIQP Modeling and Optimization in C++ΒΆ

In this section, we will utilize MindOpt C++ API to model and solve the problem in Example of Mixed Integer Quadratic Programming.

Include the header files:

27#include <vector>

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(). Then, we call MDOModel::addVar() to add four variables, which define upper bounds, lower bounds, names and types. (for more details on MDOModel::set() and MDOModel::addVar(), please refer to C++ API)

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,         0.0, MDO_INTEGER, "x0"));
50        x.push_back(model.addVar(0.0, MDO_INFINITY, 0.0, MDO_CONTINUOUS, "x1"));
51        x.push_back(model.addVar(0.0, MDO_INFINITY, 0.0, MDO_CONTINUOUS, "x2"));

We call MDOaddconstr() to input the linear constraints into model:

52        x.push_back(model.addVar(0.0, MDO_INFINITY, 0.0, MDO_CONTINUOUS, "x3"));
53
54        /* Add constraints. */

Then, we create a quadratic expression MDOQuadExpr and call MDOQuadExpr::addTerms to set the linear part of the objective function. obj_idx represents the indices of the linear terms, obj_val represents the corresponding non-zero coefficient values in obj_idx, and obj_nnz represents the number of non-zero elements in the linear part.

56        model.addConstr(1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3], MDO_EQUAL, 1.0, "c1");
57        
58        /*Create a QuadExpr. */
59        MDOQuadExpr obj = MDOQuadExpr(0.0);
60
61        /* Add objective linear term.*/
62        const MDOVar obj_idx[] = { x[0], x[1], x[2], x[3]};
63        const double obj_val[] = { 1.0, 1.0, 1.0, 1.0};

We call MDOQuadExpr::addTerms to set the quadratic terms of the objective. Here, qo_values represents the coefficients of all the non-zero quadratic terms, while qo_col1 and qo_col2 respectively represent its row and column indices. qo_nnz represents the number of non-zero quadratic terms.

64        obj.addTerms(obj_val, obj_idx, 4);
65
66        /* Add quadratic objective matrix Q.
67         *
68         *  Note.
69         *  1. The objective function is defined as c^Tx + 1/2 x^TQx, where Q is stored with coordinate format.
70         *  2. Q will be scaled by 1/2 internally.

Lastly, we call MDOModel::setObjective to set the objective and the direction to be optimized.

72         *

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

61        model.optimize();

The complete example code is shown in MdoMIQPEx1.cpp :

  1/**
  2 *  Description
  3 *  -----------
  4 *
  5 *  Linear 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 *  Subject To
 14 *   c0 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
 15 *   c1 : 1 x0 - 1 x2 + 6 x3 = 1
 16 *  Bounds
 17 *    0 <= x0 <= 10
 18 *    0 <= x1
 19 *    0 <= x2
 20 *    0 <= x3
 21 *  Integers
 22 *  x0 
 23 *  End
 24 */
 25#include <iostream>
 26#include "MindoptCpp.h"
 27#include <vector>
 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,         0.0, MDO_INTEGER, "x0"));
 50        x.push_back(model.addVar(0.0, MDO_INFINITY, 0.0, MDO_CONTINUOUS, "x1"));
 51        x.push_back(model.addVar(0.0, MDO_INFINITY, 0.0, MDO_CONTINUOUS, "x2"));
 52        x.push_back(model.addVar(0.0, MDO_INFINITY, 0.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], MDO_GREATER_EQUAL, 1.0, "c0");
 56        model.addConstr(1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3], MDO_EQUAL, 1.0, "c1");
 57        
 58        /*Create a QuadExpr. */
 59        MDOQuadExpr obj = MDOQuadExpr(0.0);
 60
 61        /* Add objective linear term.*/
 62        const MDOVar obj_idx[] = { x[0], x[1], x[2], x[3]};
 63        const double obj_val[] = { 1.0, 1.0, 1.0, 1.0};
 64        obj.addTerms(obj_val, obj_idx, 4);
 65
 66        /* Add quadratic objective matrix Q.
 67         *
 68         *  Note.
 69         *  1. The objective function is defined as c^Tx + 1/2 x^TQx, where Q is stored with coordinate format.
 70         *  2. Q will be scaled by 1/2 internally.
 71         *  3. To ensure the symmetricity of Q, user needs to input only the lower triangular part.
 72         *
 73         * Q = [ 1.0  0.5  0    0   ]
 74         *     [ 0.5  1.0  0    0   ]
 75         *     [ 0.0  0.0  1.0  0   ]
 76         *     [ 0    0    0    1.0 ]
 77         */
 78
 79        const double qo_values[] =
 80        {
 81            1.0,
 82            0.5, 1.0,
 83                    1.0, 
 84                        1.0
 85        };
 86        const MDOVar qo_col1[] = 
 87        {
 88            x[0], 
 89            x[1],   x[1],
 90                    x[2],
 91                           x[3]  
 92        };
 93        const MDOVar qo_col2[] =
 94        {
 95            x[0],
 96            x[0],   x[1],
 97                      x[2],
 98                           x[3]
 99        };
100
101        obj.addTerms(qo_values, qo_col1, qo_col2, 5);
102
103        model.setObjective(obj, MDO_MINIMIZE);
104
105        /*------------------------------------------------------------------*/
106        /* Step 3. Solve the problem and populate optimization result.                */
107        /*------------------------------------------------------------------*/
108        /* Solve the problem. */
109        model.optimize();
110
111        if(model.get(MDO_IntAttr_Status) == MDO_OPTIMAL)
112        {
113            cout << "Optimal objective value is: " << model.get(MDO_DoubleAttr_ObjVal) << endl;
114            cout << "Decision variables:" << endl;
115            int i = 0;
116            for (auto v : x)
117            {
118                cout << "x[" << i++ << "] = " << v.get(MDO_DoubleAttr_X) << endl;
119            }
120        }
121        else
122        {
123            cout<< "No feasible solution." << endl;
124        }
125        
126    }
127    catch (MDOException& e) 
128    { 
129        std::cout << "Error code = " << e.getErrorCode() << std::endl;
130        std::cout << e.getMessage() << std::endl;
131    } 
132    catch (...) 
133    { 
134        std::cout << "Error during optimization." << std::endl;
135    }
136    
137    return static_cast<int>(MDO_OKAY);
138}