5.3.2. 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.2.1. Input by row: MdoQoEx1

Load the header file.

24#include "Mindopt.h"

Create an optimization model.

87    /*------------------------------------------------------------------*/
88    /* Step 1. Create a model and change the parameters.                */
89    /*------------------------------------------------------------------*/
90    /* Create an empty model. */
91    MDO_CHECK_CALL(Mdo_createMdl(&model));

Call Mdo_setIntAttr() to set the target function to Minimization, call Mdo_addCol() to add four optimization variables, and define the upper and lower bounds, names, and types of the variables. For more information about how to use Mdo_setIntAttr() and Mdo_addCol(), see C API.

 93    /*------------------------------------------------------------------*/
 94    /* Step 2. Input model.                                             */
 95    /*------------------------------------------------------------------*/
 96    /* Change to minimization problem. */
 97    MDO_CHECK_CALL(Mdo_setIntAttr(model, MDO_INT_ATTR_MIN_SENSE, MDO_YES));
 98
 99    /* Add variables. */
100    MDO_CHECK_CALL(Mdo_addCol(model, 0.0, 10.0,         1.0, 0, NULL, NULL, "x0", MDO_NO));
101    MDO_CHECK_CALL(Mdo_addCol(model, 0.0, MDO_INFINITY, 1.0, 0, NULL, NULL, "x1", MDO_NO));
102    MDO_CHECK_CALL(Mdo_addCol(model, 0.0, MDO_INFINITY, 1.0, 0, NULL, NULL, "x2", MDO_NO));
103    MDO_CHECK_CALL(Mdo_addCol(model, 0.0, MDO_INFINITY, 1.0, 0, NULL, NULL, "x3", MDO_NO));

Note

The non-zero elements in the matrix will be input by column later. Therefore, in Mdo_addCol(), replace the size , indices , and value parameters of non-zero elements that are input by column with 0, NULL, and NULL, respectively. In other words, the problem has no constraints for the moment.

Now, add non-zero elements and their upper and lower bounds of linear constraints. Use the following four arrays to define linear constraints. row1_idx and row2_idx indicate the position (index) for a non-zero element in the first and second constraints, respectively, and row1_val and row2_val are the non-zero values of these elements.

48    const int    row1_idx[] = { 0,   1,   2,   3   };
49    const double row1_val[] = { 1.0, 1.0, 2.0, 3.0 };
50    const int    row2_idx[] = { 0,    2,   3   };
51    const double row2_val[] = { 1.0, -1.0, 6.0 };

Call Mdo_addRow() to input the constraints.

105    /* Add constraints.
106     * Note that the nonzero elements are inputted in a row-wise order here.
107     */
108    MDO_CHECK_CALL(Mdo_addRow(model, 1.0, MDO_INFINITY, 4, row1_idx, row1_val, "c0"));
109    MDO_CHECK_CALL(Mdo_addRow(model, 1.0, 1.0,          3, row2_idx, row2_val, "c1"));

Add the quadratic coefficient \(Q\) of the target function in quadratic programming. Three parameters are used. qo_col1 and qo_col2 record the two variable indexes of all non-zero elements in the quadratic term, respectively, and qo_values is the corresponding non-zero coefficient value.

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.

53    /* Quadratic objective matrix Q.
54     * 
55     *  Note.
56     *  1. The objective function is defined as c^Tx + 1/2 x^TQx, where Q is stored with coordinate format.
57     *  2. Q will be scaled by 1/2 internally.
58     *  3. To ensure the symmetricity of Q, user needs to input only the lower triangular part.
59     * 
60     * Q = [ 1.0  0.5  0    0   ]
61     *     [ 0.5  1.0  0    0   ]
62     *     [ 0.0  0.0  1.0  0   ]
63     *     [ 0    0    0    1.0 ]
64     */
65    const int qo_col1[] = 
66    {
67        0, 
68        1,   1,
69                  2,
70                       3  
71    };
72    const int qo_col2[] =
73    {
74        0,
75        0,   1,
76                  2,
77                       3
78    };
79    const double qo_values[] =
80    {
81        1.0,
82        0.5, 1.0,
83                  1.0, 
84                       1.0
85    };

Call Mdo_setQuadraticElements() to set the quadratic term of the object.

112    /* Add quadratic objective term. */
113    MDO_CHECK_CALL(Mdo_setQuadraticElements(model, 5, qo_col1, qo_col2, qo_values));

Call Mdo_solveProb() to solve the optimization problem and call Mdo_displayResults() to view the optimization result.

114    /*------------------------------------------------------------------*/
115    /* Step 3. Solve the problem and populate the result.               */
116    /*------------------------------------------------------------------*/
117    /* Solve the problem. */
118    MDO_CHECK_CALL(Mdo_solveProb(model));
119    Mdo_displayResults(model);

Call Mdo_freeMdl() to release the memory.

121    /*------------------------------------------------------------------*/
122    /* Step 4. Free the model.                                          */
123    /*------------------------------------------------------------------*/
124    /* Free the model. */
125    Mdo_freeMdl(&model);

The linked file MdoQoEx1.c provides complete source code:

  1/**
  2 *  Description
  3 *  -----------
  4 *
  5 *  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 *  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 <stdio.h>
 24#include "Mindopt.h"
 25
 26/* Macro to check the return code */
 27#define MDO_CHECK_CALL(MDO_CALL)                                    \
 28    code = MDO_CALL;                                                \
 29    if (code != MDO_OKAY)                                           \
 30    {                                                               \
 31        Mdo_explainResult(model, code, str);                        \
 32        Mdo_freeMdl(&model);                                        \
 33        fprintf(stderr, "===================================\n");   \
 34        fprintf(stderr, "Error   : code <%d>\n", code);             \
 35        fprintf(stderr, "Reason  : %s\n", str);                     \
 36        fprintf(stderr, "===================================\n");   \
 37        return (int)code;                                           \
 38    }
 39
 40int main(void)
 41{
 42    /* Variables. */
 43    char str[1024] = { "\0" };
 44    MdoMdl * model = NULL;
 45    MdoResult code = MDO_OKAY;
 46    MdoStatus status = MDO_UNKNOWN;
 47
 48    const int    row1_idx[] = { 0,   1,   2,   3   };
 49    const double row1_val[] = { 1.0, 1.0, 2.0, 3.0 };
 50    const int    row2_idx[] = { 0,    2,   3   };
 51    const double row2_val[] = { 1.0, -1.0, 6.0 };
 52
 53    /* Quadratic objective matrix Q.
 54     * 
 55     *  Note.
 56     *  1. The objective function is defined as c^Tx + 1/2 x^TQx, where Q is stored with coordinate format.
 57     *  2. Q will be scaled by 1/2 internally.
 58     *  3. To ensure the symmetricity of Q, user needs to input only the lower triangular part.
 59     * 
 60     * Q = [ 1.0  0.5  0    0   ]
 61     *     [ 0.5  1.0  0    0   ]
 62     *     [ 0.0  0.0  1.0  0   ]
 63     *     [ 0    0    0    1.0 ]
 64     */
 65    const int qo_col1[] = 
 66    {
 67        0, 
 68        1,   1,
 69                  2,
 70                       3  
 71    };
 72    const int qo_col2[] =
 73    {
 74        0,
 75        0,   1,
 76                  2,
 77                       3
 78    };
 79    const double qo_values[] =
 80    {
 81        1.0,
 82        0.5, 1.0,
 83                  1.0, 
 84                       1.0
 85    };
 86
 87    /*------------------------------------------------------------------*/
 88    /* Step 1. Create a model and change the parameters.                */
 89    /*------------------------------------------------------------------*/
 90    /* Create an empty model. */
 91    MDO_CHECK_CALL(Mdo_createMdl(&model));
 92
 93    /*------------------------------------------------------------------*/
 94    /* Step 2. Input model.                                             */
 95    /*------------------------------------------------------------------*/
 96    /* Change to minimization problem. */
 97    MDO_CHECK_CALL(Mdo_setIntAttr(model, MDO_INT_ATTR_MIN_SENSE, MDO_YES));
 98
 99    /* Add variables. */
100    MDO_CHECK_CALL(Mdo_addCol(model, 0.0, 10.0,         1.0, 0, NULL, NULL, "x0", MDO_NO));
101    MDO_CHECK_CALL(Mdo_addCol(model, 0.0, MDO_INFINITY, 1.0, 0, NULL, NULL, "x1", MDO_NO));
102    MDO_CHECK_CALL(Mdo_addCol(model, 0.0, MDO_INFINITY, 1.0, 0, NULL, NULL, "x2", MDO_NO));
103    MDO_CHECK_CALL(Mdo_addCol(model, 0.0, MDO_INFINITY, 1.0, 0, NULL, NULL, "x3", MDO_NO));
104
105    /* Add constraints.
106     * Note that the nonzero elements are inputted in a row-wise order here.
107     */
108    MDO_CHECK_CALL(Mdo_addRow(model, 1.0, MDO_INFINITY, 4, row1_idx, row1_val, "c0"));
109    MDO_CHECK_CALL(Mdo_addRow(model, 1.0, 1.0,          3, row2_idx, row2_val, "c1"));
110
111    /* Add quadratic objective term. */
112    MDO_CHECK_CALL(Mdo_setQuadraticElements(model, 5, qo_col1, qo_col2, qo_values));
113
114    /*------------------------------------------------------------------*/
115    /* Step 3. Solve the problem and populate the result.               */
116    /*------------------------------------------------------------------*/
117    /* Solve the problem. */
118    MDO_CHECK_CALL(Mdo_solveProb(model));
119    Mdo_displayResults(model);
120 
121    /*------------------------------------------------------------------*/
122    /* Step 4. Free the model.                                          */
123    /*------------------------------------------------------------------*/
124    /* Free the model. */
125    Mdo_freeMdl(&model);
126       
127    return (int)code;
128}