5.3.4. QP Modeling and Optimization in JAVAΒΆ

In this chapter, we will use MindOpt JAVA API to model and solve the problem in Example of Quadratic Programming.

Include the header file:

25import com.alibaba.damo.mindopt.*;

Create an optimization model model:

31        MDOEnv env = new MDOEnv(); 
32        MDOModel model = new MDOModel(env); 
33        model.set(MDO.StringAttr.ModelName, "QP_01");

Next, we set the optimization sense to minimization via MDOModel.set. Then, we call MDOModel.addVar to add four variables, which define upper bounds, lower bounds, names and types. (for more details on MDOModel.set and MDOModel.addVar , please refer to 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");

We input the linear constraints into model:

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");    

Then, we create a quadratic expression MDOQuadExpr and call MDOQuadExpr.addTerms to set the linear part of the objective function. Here obj_idx represents the indices of the linear terms, obj_val represents the corresponding non-zero coefficient values in obj_idx.

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);

We call MDOQuadExpr.addTerms to set the quadratic terms of the objective. Here, qo_values represents the coefficients of all the non-zero quadratic terms, while qo_col1 and qo_col2 respectively represent its row and column indices.

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);

Lastly, we call MDOModel.setObjective to set the objective and the direction to be optimized.

62            model.setObjective(obj, MDO.MINIMIZE);

Once the model is constructed, we call MDOModel.optimize to solve the problem:

79            model.optimize();

The complete example code is shown in 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}