5.1.4. Java 的LP建模与优化¶
在本节中,我们将使用 MindOpt JAVA API,以按行输入的形式来建模以及求解 线性规划问题示例 中的问题。
首先,引入 Java 包:
22import com.alibaba.damo.mindopt.*;
并创建优化模型:
28 MDOEnv env = new MDOEnv();
29 MDOModel model = new MDOModel(env);
30 model.set(MDO.StringAttr.ModelName, "LP_01");
接下来,我们通过 MDOModel.set
设置模型属性 ModelSense,将目标函数设置为 最小化,并调用 MDOModel.addVar
来添加四个优化变量,定义其下界、上界、类型和名称(有关模型属性内容及其设置可参考 属性, 其他API请参考 JAVA API):
33 // Change to minimization problem.
34 model.set(MDO.IntAttr.ModelSense, MDO.MINIMIZE);
35
36 // Add variables.
37 MDOVar[] x = new MDOVar[4];
38 x[0] = model.addVar(0.0, 10.0, 1.0, 'C', "x0");
39 x[1] = model.addVar(0.0, MDO.INFINITY, 2.0, 'C', "x1");
40 x[2] = model.addVar(0.0, MDO.INFINITY, 1.0, 'C', "x2");
41 x[3] = model.addVar(0.0, MDO.INFINITY, 1.0, 'C', "x3");
接着,我们开始添加线性约束:
43 // Add constraints.
44 double[][] consV = new double[][] {
45 { 1.0, 1.0, 2.0, 3.0},
46 { 1.0, 0, -1.0, 6.0}
47 };
48
49 MDOLinExpr tempLinExpr1 = new MDOLinExpr();
50 tempLinExpr1.addTerms(consV[0], x);
51 model.addConstr(tempLinExpr1, MDO.GREATER_EQUAL, 1.0, "c0");
52
53 MDOLinExpr tempLinExpr2 = new MDOLinExpr();
54 tempLinExpr2.addTerms(consV[1], x);
55 model.addConstr(tempLinExpr2, MDO.EQUAL, 1.0, "c1");
问题输入完成后,再调用 MDOModel.optimize
求解优化问题:
57 // Step 3. Solve the problem and populate optimization result.
58 model.optimize();
求解完成后,用 MDOModel.get
和模型属性值 ObjVal 来查看优化结果和最优目标值,以及 MDOVar.get
和变量属性值 X 来查看优化解的目标值。 其他的属性值请查看 属性 章节。
60 if (model.get(MDO.IntAttr.Status) == MDO.OPTIMAL) {
61 System.out.println("Optimal objective value is: " + model.get(MDO.DoubleAttr.ObjVal));
62 System.out.println("Decision variables: ");
63 for (int i = 0; i < 4; i++) {
64 System.out.println( "x[" + i + "] = " + x[i].get(MDO.DoubleAttr.X));
65 }
66 }
67 else {
68 System.out.println("No feasible solution.");
69 }
示例 MdoLoEx1.java 提供了完整源代码:
1/*
2 * Description
3 * -----------
4 *
5 * Linear optimization (row-wise input).
6 *
7 * Formulation
8 * -----------
9 *
10 * Minimize
11 * obj: 1 x0 + 2 x1 + 1 x2 + 1 x3
12 * Subject To
13 * c1 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
14 * c2 : 1 x0 - 1 x2 + 6 x3 = 1
15 * Bounds
16 * 0 <= x0 <= 10
17 * 0 <= x1
18 * 0 <= x2
19 * 0 <= x3
20 * End
21 */
22import com.alibaba.damo.mindopt.*;
23import java.util.*;
24
25public class MdoLoEx1 {
26 public static void main(String[] args) throws MDOException {
27 // Create model
28 MDOEnv env = new MDOEnv();
29 MDOModel model = new MDOModel(env);
30 model.set(MDO.StringAttr.ModelName, "LP_01");
31
32 try {
33 // Change to minimization problem.
34 model.set(MDO.IntAttr.ModelSense, MDO.MINIMIZE);
35
36 // Add variables.
37 MDOVar[] x = new MDOVar[4];
38 x[0] = model.addVar(0.0, 10.0, 1.0, 'C', "x0");
39 x[1] = model.addVar(0.0, MDO.INFINITY, 2.0, 'C', "x1");
40 x[2] = model.addVar(0.0, MDO.INFINITY, 1.0, 'C', "x2");
41 x[3] = model.addVar(0.0, MDO.INFINITY, 1.0, 'C', "x3");
42
43 // Add constraints.
44 double[][] consV = new double[][] {
45 { 1.0, 1.0, 2.0, 3.0},
46 { 1.0, 0, -1.0, 6.0}
47 };
48
49 MDOLinExpr tempLinExpr1 = new MDOLinExpr();
50 tempLinExpr1.addTerms(consV[0], x);
51 model.addConstr(tempLinExpr1, MDO.GREATER_EQUAL, 1.0, "c0");
52
53 MDOLinExpr tempLinExpr2 = new MDOLinExpr();
54 tempLinExpr2.addTerms(consV[1], x);
55 model.addConstr(tempLinExpr2, MDO.EQUAL, 1.0, "c1");
56
57 // Step 3. Solve the problem and populate optimization result.
58 model.optimize();
59
60 if (model.get(MDO.IntAttr.Status) == MDO.OPTIMAL) {
61 System.out.println("Optimal objective value is: " + model.get(MDO.DoubleAttr.ObjVal));
62 System.out.println("Decision variables: ");
63 for (int i = 0; i < 4; i++) {
64 System.out.println( "x[" + i + "] = " + x[i].get(MDO.DoubleAttr.X));
65 }
66 }
67 else {
68 System.out.println("No feasible solution.");
69 }
70 } catch (Exception e) {
71 System.out.println("Exception during optimization");
72 e.printStackTrace();
73 } finally {
74 model.dispose();
75 env.dispose();
76 }
77 }
78}