5.5.5. MIQP Modeling and Optimization in JAVAΒΆ
In this chapter, we will use MindOpt JAVA API to model and solve the problem in Example of Mixed Integer Quadratic Programming.
Include the header file:
25import java.util.*;
Create an optimization model model
:
31 MDOModel model = new MDOModel(env);
32 model.set(MDO.StringAttr.ModelName, "MIQP_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 model.set(MDO.IntAttr.ModelSense, MDO.MINIMIZE);
37
38 // Add variables.
39 MDOVar[] x = new MDOVar[4];
40 x[0] = model.addVar(0.0, 10.0, 0.0, 'I', "x0");
41 x[1] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x1");
42 x[2] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x2");
43 x[3] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x3");
We input the linear constraints into model
:
65 { 1.0, 1.0, 2.0, 3.0 },
66 { 1.0, 0, -1.0, 6.0 }
67 };
68
69 MDOLinExpr tempLinExpr1 = new MDOLinExpr();
70 tempLinExpr1.addTerms(consV[0], x);
71 model.addConstr(tempLinExpr1, MDO.GREATER_EQUAL, 1.0, "c0");
72
73 MDOLinExpr tempLinExpr2 = new MDOLinExpr();
74 tempLinExpr2.addTerms(consV[1], x);
75 model.addConstr(tempLinExpr2, MDO.EQUAL, 1.0, "c1");
76
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 // Add objective linear term: 1 x0 + 1 x1 + 1 x2 + 1 x3
48 int obj_nnz = 4;
49 MDOVar[] obj_idx = new MDOVar[] { x[0], x[1], x[2], x[3] };
50 double[] obj_val = new double[] { 1.0, 1.0, 1.0, 1.0 };
51 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 MDOVar[] qo_col1 = new MDOVar[] { x[0], x[1], x[2], x[3], x[0] };
57 MDOVar[] qo_col2 = new MDOVar[] { x[0], x[1], x[2], x[3], x[1] };
58 double[] qo_values = new double[] { 0.5, 0.5, 0.5, 0.5, 0.5 };
59 obj.addTerms(qo_values, qo_col1, qo_col2);
Lastly, we call MDOModel.setObjective
to set the objective and the direction to be optimized.
62
Once the model is constructed, we call MDOModel.optimize
to solve the problem:
79
The complete example code is shown in MdoMIQPEx1.java :
1/**
2 * Description
3 * -----------
4 *
5 * Mixed IntegerQuadratic optimization (row-wise input).
6 *
7 * Formulation
8 * -----------
9 * Minimize
10 * obj: 1 x0 + 1 x1 + 1 x2 + 1 x3
11 * + 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1]
12 * Subject To
13 * c0 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
14 * c1 : 1 x0 - 1 x2 + 6 x3 = 1
15 * Bounds
16 * 0 <= x0 <= 10
17 * 0 <= x1
18 * 0 <= x2
19 * 0 <= x3
20 * x0 integer
21 * End
22 */
23
24import com.alibaba.damo.mindopt.*;
25import java.util.*;
26
27public class MdoMIQPEx1 {
28 public static void main(String[] args) throws MDOException {
29 // Create model
30 MDOEnv env = new MDOEnv();
31 MDOModel model = new MDOModel(env);
32 model.set(MDO.StringAttr.ModelName, "MIQP_01");
33
34 try {
35 // Change to minimization problem.
36 model.set(MDO.IntAttr.ModelSense, MDO.MINIMIZE);
37
38 // Add variables.
39 MDOVar[] x = new MDOVar[4];
40 x[0] = model.addVar(0.0, 10.0, 0.0, 'I', "x0");
41 x[1] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x1");
42 x[2] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x2");
43 x[3] = model.addVar(0.0, MDO.INFINITY, 0.0, 'C', "x3");
44
45 // Create a QuadExpr for quadratic objective
46 MDOQuadExpr obj = new MDOQuadExpr();
47
48 // Add objective linear term: 1 x0 + 1 x1 + 1 x2 + 1 x3
49 int obj_nnz = 4;
50 MDOVar[] obj_idx = new MDOVar[] { x[0], x[1], x[2], x[3] };
51 double[] obj_val = new double[] { 1.0, 1.0, 1.0, 1.0 };
52 obj.addTerms(obj_val, obj_idx);
53
54 // Add quadratic part in objective: 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1]
55 int qo_nnz = 5;
56 MDOVar[] qo_col1 = new MDOVar[] { x[0], x[1], x[2], x[3], x[0] };
57 MDOVar[] qo_col2 = new MDOVar[] { x[0], x[1], x[2], x[3], x[1] };
58 double[] qo_values = new double[] { 0.5, 0.5, 0.5, 0.5, 0.5 };
59 obj.addTerms(qo_values, qo_col1, qo_col2);
60
61 model.setObjective(obj, MDO.MINIMIZE);
62
63 // Add constraints.
64 double[][] consV = new double[][] {
65 { 1.0, 1.0, 2.0, 3.0 },
66 { 1.0, 0, -1.0, 6.0 }
67 };
68
69 MDOLinExpr tempLinExpr1 = new MDOLinExpr();
70 tempLinExpr1.addTerms(consV[0], x);
71 model.addConstr(tempLinExpr1, MDO.GREATER_EQUAL, 1.0, "c0");
72
73 MDOLinExpr tempLinExpr2 = new MDOLinExpr();
74 tempLinExpr2.addTerms(consV[1], x);
75 model.addConstr(tempLinExpr2, MDO.EQUAL, 1.0, "c1");
76
77 // Solve the problem and populate optimization result.
78 model.optimize();
79
80 if (model.get(MDO.IntAttr.Status) == MDO.OPTIMAL) {
81 System.out.println("Optimal objective value is: " + model.get(MDO.DoubleAttr.ObjVal));
82 System.out.println("Decision variables: ");
83 for (int i = 0; i < 4; i++) {
84 System.out.println( "x[" + i + "] = " + x[i].get(MDO.DoubleAttr.X));
85 }
86 }
87 else {
88 System.out.println("No feasible solution.");
89 }
90 } catch (Exception e) {
91 System.out.println("Exception during optimization");
92 e.printStackTrace();
93 } finally {
94 model.dispose();
95 env.dispose();
96 }
97 }
98}