# 5.3.4. Python 的QP建模与优化¶

## 5.3.4.1. 按行输入: mdo_qo_ex1¶

25from mindoptpy import *

32    # Step 1. Create a model and change the parameters.
33    model = MdoModel()

36        # Step 2. Input model.
37        # Change to minimization problem.
38        model.set_int_attr(MDO_INT_ATTR.MIN_SENSE, 1)
39
41        x = []
42        x.append(model.add_var(0.0,         10.0, 1.0, None, "x0", False))
43        x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x1", False))
44        x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x2", False))
45        x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x3", False))

47        # Note that the nonzero elements are inputted in a row-wise order here.
48        model.add_cons(1.0, MDO_INFINITY, 1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3], "c0")
49        model.add_cons(1.0,          1.0, 1.0 * x[0]              - 1.0 * x[2] + 6.0 * x[3], "c1")

Note

53        #
54        #  Note.
55        #  1. The objective function is defined as c^Tx + 1/2 x^TQx, where Q is stored with coordinate format.
56        #  2. Q will be scaled by 1/2 internally.
57        #  3. To ensure the symmetricity of Q, user needs to input only the lower triangular part.
58        #
59        # Q = [ 1.0  0.5  0    0   ]
60        #     [ 0.5  1.0  0    0   ]
61        #     [ 0.0  0.0  1.0  0   ]
62        #     [ 0    0    0    1.0 ]
63        model.set_quadratic_elements([ x[0], x[1], x[1], x[2], x[3] ], [ x[0], x[0], x[1], x[2], x[3] ], [  1.0,  0.5,  1.0,  1.0,  1.0 ])

65        # Step 3. Solve the problem and populate the result.
66        model.solve_prob()
67        model.display_results()

78        model.free_mdl()

mdo_qo_ex1.py 提供了完整源代码：

1"""
2/**
3 *  Description
4 *  -----------
5 *
6 *  Linear optimization (row-wise input).
7 *
8 *  Formulation
9 *  -----------
10 *
11 *  Minimize
12 *    obj: 1 x0 + 1 x1 + 1 x2 + 1 x3
13 *         + 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1]
14 *  Subject To
15 *   c1 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
16 *   c2 : 1 x0 - 1 x2 + 6 x3 = 1
17 *  Bounds
18 *    0 <= x0 <= 10
19 *    0 <= x1
20 *    0 <= x2
21 *    0 <= x3
22 *  End
23 */
24"""
25from mindoptpy import *
26
27
28if __name__ == "__main__":
29
30    MDO_INFINITY = MdoModel.get_infinity()
31
32    # Step 1. Create a model and change the parameters.
33    model = MdoModel()
34
35    try:
36        # Step 2. Input model.
37        # Change to minimization problem.
38        model.set_int_attr(MDO_INT_ATTR.MIN_SENSE, 1)
39
41        x = []
42        x.append(model.add_var(0.0,         10.0, 1.0, None, "x0", False))
43        x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x1", False))
44        x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x2", False))
45        x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x3", False))
46
48        # Note that the nonzero elements are inputted in a row-wise order here.
49        model.add_cons(1.0, MDO_INFINITY, 1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3], "c0")
50        model.add_cons(1.0,          1.0, 1.0 * x[0]              - 1.0 * x[2] + 6.0 * x[3], "c1")
51
53        #
54        #  Note.
55        #  1. The objective function is defined as c^Tx + 1/2 x^TQx, where Q is stored with coordinate format.
56        #  2. Q will be scaled by 1/2 internally.
57        #  3. To ensure the symmetricity of Q, user needs to input only the lower triangular part.
58        #
59        # Q = [ 1.0  0.5  0    0   ]
60        #     [ 0.5  1.0  0    0   ]
61        #     [ 0.0  0.0  1.0  0   ]
62        #     [ 0    0    0    1.0 ]
63        model.set_quadratic_elements([ x[0], x[1], x[1], x[2], x[3] ], [ x[0], x[0], x[1], x[2], x[3] ], [  1.0,  0.5,  1.0,  1.0,  1.0 ])
64
65        # Step 3. Solve the problem and populate the result.
66        model.solve_prob()
67        model.display_results()
68
69    except MdoError as e: