5.3.3. QP modeling and optimization in C++

This topic describes how to use the C++ API of MindOpt to build a model and solve the problem in Examples of quadratic programming problems.

5.3.3.1. Input by row: MdoQoEx1

Load the header file.

25#include "MindoptCpp.h"

Create an optimization model.

31    /*------------------------------------------------------------------*/
32    /* Step 1. Create a model and change the parameters.                */
33    /*------------------------------------------------------------------*/
34    /* Create an empty model. */
35    MdoModel model;

Call mindopt::MdoModel::setIntAttr() to set the target function to Minimization, call mindopt::MdoModel::addVar() to add four optimization variables, and define the upper bound, lower bound, names, and types of the variables. For more information about how to use mindopt::MdoModel::setIntAttr() and mindopt::MdoModel::addVar(), see C++ API.

39        /*------------------------------------------------------------------*/
40        /* Step 2. Input model.                                             */
41        /*------------------------------------------------------------------*/
42        /* Change to minimization problem. */
43        model.setIntAttr(MDO_INT_ATTR::MIN_SENSE, MDO_YES);
44
45        /* Add variables. */
46        std::vector<MdoVar> x;
47        x.push_back(model.addVar(0.0, 10.0,         1.0, "x0", MDO_NO));
48        x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, "x1", MDO_NO));
49        x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, "x2", MDO_NO));
50        x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, "x3", MDO_NO));

Add linear constraints.

53        model.addCons(1.0, MDO_INFINITY, 1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3], "c0");
54        model.addCons(1.0, 1.0,          1.0 * x[0]              - 1.0 * x[2] + 6.0 * x[3], "c1");

Call mindopt::MdoModel::setQuadraticElements() to set the quadratic coefficient \(Q\) for the object. The former two groups of vectors indicate the indexes of the two variables of all non-zero elements in the quadratic term, respectively, and the last group of vectors indicate the corresponding non-zero coefficient values.

Note

To ensure the symmetry of \(Q\), you only need to input the triangle part, which will be multiplied by 1/2 inside the solver.

56        /* Add quadratic objective matrix Q.
57         *
58         *  Note.
59         *  1. The objective function is defined as c^Tx + 1/2 x^TQx, where Q is stored with coordinate format.
60         *  2. Q will be scaled by 1/2 internally.
61         *  3. To ensure the symmetricity of Q, user needs to input only the lower triangular part.
62         *
63         * Q = [ 1.0  0.5  0    0   ]
64         *     [ 0.5  1.0  0    0   ]
65         *     [ 0.0  0.0  1.0  0   ]
66         *     [ 0    0    0    1.0 ]
67         */
68        model.setQuadraticElements
69        (
70            { x[0], x[1], x[1], x[2], x[3] },
71            { x[0], x[0], x[1], x[2], x[3] },
72            {  1.0,  0.5,  1.0,  1.0,  1.0 }
73        );

Call mindopt::MdoModel::solveProb() to solve the optimization problem, and call mindopt::MdoModel::displayResults() to view the optimization result.

75        /*------------------------------------------------------------------*/
76        /* Step 3. Solve the problem and populate the result.               */
77        /*------------------------------------------------------------------*/
78        /* Solve the problem. */
79        model.solveProb();
80        model.displayResults();

The linked file MdoQoEx1.cpp provides complete source code:

 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 *  End
22 */
23#include <iostream>
24#include <vector>
25#include "MindoptCpp.h"
26
27using namespace mindopt;
28
29int main(void)
30{
31    /*------------------------------------------------------------------*/
32    /* Step 1. Create a model and change the parameters.                */
33    /*------------------------------------------------------------------*/
34    /* Create an empty model. */
35    MdoModel model;
36
37    try 
38    {
39        /*------------------------------------------------------------------*/
40        /* Step 2. Input model.                                             */
41        /*------------------------------------------------------------------*/
42        /* Change to minimization problem. */
43        model.setIntAttr(MDO_INT_ATTR::MIN_SENSE, MDO_YES);
44
45        /* Add variables. */
46        std::vector<MdoVar> x;
47        x.push_back(model.addVar(0.0, 10.0,         1.0, "x0", MDO_NO));
48        x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, "x1", MDO_NO));
49        x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, "x2", MDO_NO));
50        x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, "x3", MDO_NO));
51
52        /* Add constraints. */
53        model.addCons(1.0, MDO_INFINITY, 1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3], "c0");
54        model.addCons(1.0, 1.0,          1.0 * x[0]              - 1.0 * x[2] + 6.0 * x[3], "c1");
55
56        /* Add quadratic objective matrix Q.
57         *
58         *  Note.
59         *  1. The objective function is defined as c^Tx + 1/2 x^TQx, where Q is stored with coordinate format.
60         *  2. Q will be scaled by 1/2 internally.
61         *  3. To ensure the symmetricity of Q, user needs to input only the lower triangular part.
62         *
63         * Q = [ 1.0  0.5  0    0   ]
64         *     [ 0.5  1.0  0    0   ]
65         *     [ 0.0  0.0  1.0  0   ]
66         *     [ 0    0    0    1.0 ]
67         */
68        model.setQuadraticElements
69        (
70            { x[0], x[1], x[1], x[2], x[3] },
71            { x[0], x[0], x[1], x[2], x[3] },
72            {  1.0,  0.5,  1.0,  1.0,  1.0 }
73        );
74
75        /*------------------------------------------------------------------*/
76        /* Step 3. Solve the problem and populate the result.               */
77        /*------------------------------------------------------------------*/
78        /* Solve the problem. */
79        model.solveProb();
80        model.displayResults();
81    }
82    catch (MdoException & e)
83    {
84        std::cerr << "===================================" << std::endl;
85        std::cerr << "Error   : code <" << e.getResult() << ">" << std::endl;
86        std::cerr << "Reason  : " << model.explainResult(e.getResult()) << std::endl;
87        std::cerr << "===================================" << std::endl;
88
89        return static_cast<int>(e.getResult());
90    }
91
92    return static_cast<int>(MDO_OKAY);
93}