5.2.4. Python 的MILP建模与优化

在本节中,我们将使用 MindOpt Python 语言的 API 来建模以及求解 混合整数线性规划问题示例 中的问题。

5.2.4.1. 按行输入: mdo_milo_ex1

首先,引入 Python 包:

26from mindoptpy import *

并创建优化模型:

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

接下来,我们通过 mindoptpy.MdoModel.set_int_attr() 将目标函数设置为 最小化 ,并调用 mindoptpy.MdoModel.add_var() 来添加四个优化变量,定义其下界、上界、名称和类型(有关 mindoptpy.MdoModel.set_int_attr()mindoptpy.MdoModel.add_var() 的详细使用方式,请参考 Python 接口函数):

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

Note

在函数 mindoptpy.MdoModel.add_var() 中,最后一个参数位是 is_integer ,设置为 True 代表这个变量是整形变量。

接着,我们开始添加线性约束:

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

问题输入完成后,再调用 mindoptpy.MdoModel.solve_prob() 求解优化问题,并用 mindoptpy.MdoModel.display_results() 来查看优化结果:

53        # Step 3. Solve the problem and populate the result.
54        model.solve_prob()
55        model.display_results()

最后,我们调用 mindoptpy.MdoModel.free_mdl() 来释放内存:

65        # Step 4. Free the model.
66        model.free_mdl()

mdo_milo_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 *  Subject To
14 *   c1 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
15 *   c2 : 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 x1 x2  
23 *  End
24 */
25"""
26from mindoptpy import *
27
28
29if __name__ == "__main__":
30
31    MDO_INFINITY = MdoModel.get_infinity()
32
33    # Step 1. Create a model and change the parameters.
34    model = MdoModel()
35
36    try:
37        # Step 2. Input model.
38        # Change to minimization problem.
39        model.set_int_attr(MDO_INT_ATTR.MIN_SENSE, 1)
40        
41        # Add variables.
42        x = []
43        x.append(model.add_var(0.0,         10.0, 1.0, None, "x0", True))
44        x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x1", True))
45        x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x2", True))
46        x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x3", False))
47
48        # Add constraints.
49        # Note that the nonzero elements are inputted in a row-wise order here.
50        model.add_cons(1.0, MDO_INFINITY, 1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3], "c0")
51        model.add_cons(1.0,          1.0, 1.0 * x[0]              - 1.0 * x[2] + 6.0 * x[3], "c1")
52
53        # Step 3. Solve the problem and populate the result.
54        model.solve_prob()
55        model.display_results()
56
57    except MdoError as e:
58        print("Received Mindopt exception.")
59        print(" - Code          : {}".format(e.code))
60        print(" - Reason        : {}".format(e.message))
61    except Exception as e:
62        print("Received exception.")
63        print(" - Reason        : {}".format(e))
64    finally:
65        # Step 4. Free the model.
66        model.free_mdl()