5.3.4. Java 的QP建模和优化¶
在本节中,我们将使用 MindOpt JAVA API,以按行输入的形式来建模以及求解 二次规划问题示例 中的问题。
首先,引入头文件:
25import com.alibaba.damo.mindopt.*;
并创建优化模型:
31 MDOEnv env = new MDOEnv();
32 MDOModel model = new MDOModel(env);
33 model.set(MDO.StringAttr.ModelName, "QP_01");
接下来,我们通过 MDOModel.set
将目标函数设置为 最小化,并调用 MDOModel.addVar
来添加四个优化变量,定义其下界、上界、名称和类型(有关 MDOModel.set
和 MDOModel.addVar
的详细使用方式,请参考 JAVA API):
36 // Change to minimization problem.
37 model.set(MDO.IntAttr.ModelSense, MDO.MINIMIZE);
38
39 // Add variables.
40 MDOVar[] x = new MDOVar[4];
41 x[0] = model.addVar(0.0, 10.0, 0.0, 'C', "x0");
42 x[1] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x1");
43 x[2] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x2");
44 x[3] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x3");
接着,我们开始添加线性约束:
64 // Add constraints.
65 double[][] consV = new double[][] {
66 { 1.0, 1.0, 2.0, 3.0 },
67 { 1.0, 0, -1.0, 6.0 }
68 };
69
70 MDOLinExpr tempLinExpr1 = new MDOLinExpr();
71 tempLinExpr1.addTerms(consV[0], x);
72 model.addConstr(tempLinExpr1, MDO.GREATER_EQUAL, 1.0, "c0");
73
74 MDOLinExpr tempLinExpr2 = new MDOLinExpr();
75 tempLinExpr2.addTerms(consV[1], x);
76 model.addConstr(tempLinExpr2, MDO.EQUAL, 1.0, "c1");
然后,我们开始设置二次目标函数。我们创建一个二次表达式 MDOQuadExpr
, 再调用 MDOQuadExpr.addTerms
来设置目标函数线性部分。这里
obj_idx
表示线性部分的索引,obj_val
表示与 obj_idx
中的索引相对应的非零系数值。
49 // Add objective linear term: 1 x0 + 1 x1 + 1 x2 + 1 x3
50 int obj_nnz = 4;
51 MDOVar[] obj_idx = new MDOVar[] { x[0], x[1], x[2], x[3] };
52 double[] obj_val = new double[] { 1.0, 1.0, 1.0, 1.0 };
53 obj.addTerms(obj_val, obj_idx);
然后,调用 MDOQuadExpr.addTerms
来设置目标的二次项系数 \(Q\)。
其中,qo_values 表示要添加的二次项的系数,qo_col1
和 qo_col2
表示与qo_values相对应的二次项的第一个变量和第二个变量。
55 // Add quadratic part in objective: 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1]
56 int qo_nnz = 5;
57 MDOVar[] qo_col1 = new MDOVar[] { x[0], x[1], x[2], x[3], x[0] };
58 MDOVar[] qo_col2 = new MDOVar[] { x[0], x[1], x[2], x[3], x[1] };
59 double[] qo_values = new double[] { 0.5, 0.5, 0.5, 0.5, 0.5 };
60 obj.addTerms(qo_values, qo_col1, qo_col2);
最后,我们调用 MDOModel.setObjective
设定优化目标与方向。
62 model.setObjective(obj, MDO.MINIMIZE);
问题输入完成后,调用 MDOModel.optimize
求解优化问题:
79 model.optimize();
示例 MdoQoEx1.java 提供了完整源代码:
1/**
2 * Description
3 * -----------
4 *
5 * Quadratic optimization (row-wise input).
6 *
7 * Formulation
8
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 * c0 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
16 * c1 : 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
25import com.alibaba.damo.mindopt.*;
26import java.util.*;
27
28public class MdoQoEx1 {
29 public static void main(String[] args) throws MDOException {
30 // Create model
31 MDOEnv env = new MDOEnv();
32 MDOModel model = new MDOModel(env);
33 model.set(MDO.StringAttr.ModelName, "QP_01");
34
35 try {
36 // Change to minimization problem.
37 model.set(MDO.IntAttr.ModelSense, MDO.MINIMIZE);
38
39 // Add variables.
40 MDOVar[] x = new MDOVar[4];
41 x[0] = model.addVar(0.0, 10.0, 0.0, 'C', "x0");
42 x[1] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x1");
43 x[2] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x2");
44 x[3] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x3");
45
46 // Create a QuadExpr for quadratic objective
47 MDOQuadExpr obj = new MDOQuadExpr();
48
49 // Add objective linear term: 1 x0 + 1 x1 + 1 x2 + 1 x3
50 int obj_nnz = 4;
51 MDOVar[] obj_idx = new MDOVar[] { x[0], x[1], x[2], x[3] };
52 double[] obj_val = new double[] { 1.0, 1.0, 1.0, 1.0 };
53 obj.addTerms(obj_val, obj_idx);
54
55 // Add quadratic part in objective: 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1]
56 int qo_nnz = 5;
57 MDOVar[] qo_col1 = new MDOVar[] { x[0], x[1], x[2], x[3], x[0] };
58 MDOVar[] qo_col2 = new MDOVar[] { x[0], x[1], x[2], x[3], x[1] };
59 double[] qo_values = new double[] { 0.5, 0.5, 0.5, 0.5, 0.5 };
60 obj.addTerms(qo_values, qo_col1, qo_col2);
61
62 model.setObjective(obj, MDO.MINIMIZE);
63
64 // Add constraints.
65 double[][] consV = new double[][] {
66 { 1.0, 1.0, 2.0, 3.0 },
67 { 1.0, 0, -1.0, 6.0 }
68 };
69
70 MDOLinExpr tempLinExpr1 = new MDOLinExpr();
71 tempLinExpr1.addTerms(consV[0], x);
72 model.addConstr(tempLinExpr1, MDO.GREATER_EQUAL, 1.0, "c0");
73
74 MDOLinExpr tempLinExpr2 = new MDOLinExpr();
75 tempLinExpr2.addTerms(consV[1], x);
76 model.addConstr(tempLinExpr2, MDO.EQUAL, 1.0, "c1");
77
78 // Solve the problem and populate optimization result.
79 model.optimize();
80
81 if (model.get(MDO.IntAttr.Status) == MDO.OPTIMAL) {
82 System.out.println("Optimal objective value is: " + model.get(MDO.DoubleAttr.ObjVal));
83 System.out.println("Decision variables: ");
84 for (int i = 0; i < 4; i++) {
85 System.out.println( "x[" + i + "] = " + x[i].get(MDO.DoubleAttr.X));
86 }
87 }
88 else {
89 System.out.println("No feasible solution.");
90 }
91 } catch (Exception e) {
92 System.out.println("Exception during optimization");
93 e.printStackTrace();
94 } finally {
95 model.dispose();
96 env.dispose();
97 }
98 }
99}