# Java Interface

This chapter walks through a simple Java example to illustrate the use of the COPT Java interface. In short words, the example creates an environment, builds a model, add variables and constraints, optimizes it, and then outputs the optimal objective value.

The example solves the following linear problem:

(4)$\begin{split}\text{Maximize: } & \\ & 1.2 x + 1.8 y + 2.1 z \\ \text{Subject to: } & \\ & 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{Bounds: } & \\ & 0.1 \leq x \leq 0.6 \\ & 0.2 \leq y \leq 1.5 \\ & 0.3 \leq z \leq 2.8 \\\end{split}$

Note that this is the same problem that was modelled and optimized in chapter of C Interface.

## Example details

Below is the source code solving the above problem using COPT Java interface.

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


Let’s now walk through the example, line by line, to understand how it achieves the desired result of optimizing the model.

### Import COPT class

To use the Java interface of COPT, users need to import the Java interface class of COPT first.

import copt.*;


### Creating environment and model

Essentially, any Java application using Cardinal Optimizer should start with a COPT environment, where user could add one or more models. Note that each model encapsulates a problem and corresponding data.

Furthermore, to create multiple problems, one can load them one by one in the same model, besides the naive option of creating multiple models in the environment.

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


The above call instantiates a COPT environment and a model with name “COPT Java Example”.

The next step in our example is to add variables to the model. Variables are added through addVar() or addVars() method on the model object. A variable is always associated with a particular model.

      // Add variables
//
// 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");


The first and second arguments to the addVar() call are the variable lower and upper bounds, respectively. The third argument is the linear objective coefficient. The fourth argument is the variable type. Our variables are all continuous in this example. The final argument is the name of the variable.

The addVar() method has been overloaded to accept several different argument lists. Please refer to Java Modeling Classes of Java API reference for further details.

The next step in the example is to add the linear constraints. As with variables, constraints are always associated with a specific model. They are created using addConstr() or addConstrs() methods on the model object.

      // Add new 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);


Two constraints here are created by building linear expressions incrementally. That is, an expression can be built by constructor of a variable and its coefficient, and then by addTerm() method.

### Setting parameters and attributes

The next step in the example is to set parameters and attributes of the problem before optimization.

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


The setDblParam() call here with copt.DblParam.TimeLimit argument sets solver to optimize up to 10 seconds. The setObjSense() call with copt.Consts.MAXIMIZE argument sets objective sense as maximization.

### Solving problem

Now that the model has been built, the next step is to optimize it:

      // Solve problem
model.solve();


This routine performs the optimization and populates several internal model attributes (including the status of the optimization, the solution, etc.).

### Outputting solution

After solving the problem, one can query the values of the attributes for various of purposes.

      // 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 var = vars.getVar(i);
System.out.println("  " + var.getName() + " = "
+ var.get(copt.DblInfo.Value));
}
System.out.println("Obj = " + model.getDblAttr(copt.DblAttr.LpObjVal));
}

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


Spcifically, one can query the copt.IntAttr.HasLpSol attribute on the model to know whether we have optimal LP solution; query the copt.DblInfo.Value attribute of a variable to obtain its solution value; query the copt.DblAttr.LpObjVal attribute on the model to obtain the objective value for the current solution.

The names and types of all model, variable, and constraint attributes can be found in Constants of Java API reference.

### Error handling

Errors in the COPT Java interface are handled through the Java exception mechanism. In the example, all COPT statements are enclosed inside a try block, and any associated errors would be caught by the catch block.

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


## Build and Run

To build and run java example, users may refer to files under $COPT_HOME/examples/java. Specifically, We provide an example file in java and a script file to build. This single example runs on all platforms that support Java. First of all, download and install Java 8 or above on your platform. ### Java example detail In the java example folder $COPT_HOME/examples/java, the easiest way to run the example is to enter the java example folder in console or terminal and then execute command 'sh run.sh'.

This java project has dependency on COPT java package copt_java.jar, which defines all java classes of COPT solver. In addition, copt_java.jar loads two shared libraries, that is, coptjniwrap.dll and copt_cpp.dll on Windows, libcoptjniwrap.so and libcopt_cpp.so on Linux, libcoptjniwrap.dylib and libcopt_cpp.dylib on Mac respectively. Note that coptjniwrap library is a JNI swig wrapper and acts as a bridge between COPT java package and native library copt_cpp, which declares and implements COPT constants, interfaces and methods. So users should make sure they are installed properly on runtime search paths.

In summary, to run java example, users should have COPT installed properly. Specifically, it requires two related COPT shared libaries existing on runtime search paths, and valid license files to run. Please refer to Install Guide for Cardinal Optimizer for further details.