# Java接口

(4)$\begin{split}\text{最大化： } & \\ & 1.2 x + 1.8 y + 2.1 z \\ \text{约束： } & \\ & 1.5 x + 1.2 y + 1.8 z \leq 2.6 \\ & 0.8 x + 0.6 y + 0.9 z \geq 1.2 \\ \text{变量范围： } & \\ & 0.1 \leq x \leq 0.6 \\ & 0.2 \leq y \leq 1.5 \\ & 0.3 \leq z \leq 2.8 \\\end{split}$

## 示例解析

 1/*
3 */
4import copt.*;
5
6/*
7 * This Java example solves the following LP model:
8 *
9 * Maximize:
10 *  1.2 x + 1.8 y + 2.1 z
11 *
12 * Subject to:
13 *  1.5 x + 1.2 y + 1.8 z <= 2.6
14 *  0.8 x + 0.6 y + 0.9 z >= 1.2
15 *
16 * where:
17 *  0.1 <= x <= 0.6
18 *  0.2 <= y <= 1.5
19 *  0.3 <= z <= 2.8
20 */
21public class Lp_ex1 {
22  public static void main(final String argv[]) {
23    try {
24      Envr env = new Envr();
25      Model model = env.createModel("lp_ex1");
26
27      /*
28       * Add variables x, y, z
29       *
30       * obj: 1.2 x + 1.8 y + 2.1 z
31       *
32       * var:
33       *  0.1 <= x <= 0.6
34       *  0.2 <= y <= 1.5
35       *  0.3 <= z <= 2.8
36       */
37      Var x = model.addVar(0.1, 0.6, 1.2, copt.Consts.CONTINUOUS, "x");
38      Var y = model.addVar(0.2, 1.5, 1.8, copt.Consts.CONTINUOUS, "y");
39      Var z = model.addVar(0.3, 2.8, 2.1, copt.Consts.CONTINUOUS, "z");
40
41      /*
42       * Add two constraints using linear expression
43       *
44       * r0: 1.5 x + 1.2 y + 1.8 z <= 2.6
45       * r1: 0.8 x + 0.6 y + 0.9 z >= 1.2
46       */
47      Expr e0 = new Expr(x, 1.5);
51
52      Expr e1 = new Expr(x, 0.8);
56
57      // Set parameters and attributes
58      model.setDblParam(copt.DblParam.TimeLimit, 10);
59      model.setObjSense(copt.Consts.MAXIMIZE);
60
61      // Solve problem
62      model.solve();
63
64      // Output solution
65      if (model.getIntAttr(copt.IntAttr.HasLpSol) != 0) {
66        System.out.println("\nFound optimal solution:");
67        VarArray vars = model.getVars();
68        for (int i = 0; i < vars.size(); i++) {
69          Var x = vars.getVar(i);
70          System.out.println("  " + x.getName() + " = " + x.get(copt.DblInfo.Value));
71        }
72        System.out.println("Obj = " + model.getDblAttr(copt.DblAttr.LpObjVal));
73      }
74
75      System.out.println("\nDone");
76    } catch (CoptException e) {
77      System.out.println("Error Code = " + e.getCode());
78      System.out.println(e.getMessage());
79    }
80  }
81}


### 导入COPT的Java类

import copt.*;


### 创建求解环境和建模

      Envr env = new Envr();
Model model = env.createModel("lp_ex1");


### 添加变量

      /*
* Add variables x, y, z
*
* obj: 1.2 x + 1.8 y + 2.1 z
*
* var:
*  0.1 <= x <= 0.6
*  0.2 <= y <= 1.5
*  0.3 <= z <= 2.8
*/
Var x = model.addVar(0.1, 0.6, 1.2, copt.Consts.CONTINUOUS, "x");
Var y = model.addVar(0.2, 1.5, 1.8, copt.Consts.CONTINUOUS, "y");
Var z = model.addVar(0.3, 2.8, 2.1, copt.Consts.CONTINUOUS, "z");


### 添加约束

      /*
* Add two constraints using linear expression
*
* r0: 1.5 x + 1.2 y + 1.8 z <= 2.6
* r1: 0.8 x + 0.6 y + 0.9 z >= 1.2
*/
Expr e0 = new Expr(x, 1.5);

Expr e1 = new Expr(x, 0.8);


### 设置参数和属性

      // Set parameters and attributes
model.setDblParam(copt.DblParam.TimeLimit, 10);
model.setObjSense(copt.Consts.MAXIMIZE);


### 求解问题

      // Solve problem
model.solve();


### 输出结果

      // Output solution
if (model.getIntAttr(copt.IntAttr.HasLpSol) != 0) {
System.out.println("\nFound optimal solution:");
VarArray vars = model.getVars();
for (int i = 0; i < vars.size(); i++) {
Var x = vars.getVar(i);
System.out.println("  " + x.getName() + " = " + x.get(copt.DblInfo.Value));
}
System.out.println("Obj = " + model.getDblAttr(copt.DblAttr.LpObjVal));
}

System.out.println("\nDone");


### 错误处理

    } catch (CoptException e) {
System.out.println("Error Code = " + e.getCode());
System.out.println(e.getMessage());
}