5.1.4. LP Modeling and Optimization in JAVAΒΆ
In this section, we will utilize MindOpt JAVA API to model and solve the linear optimization problem in Example of Linear Programming.
First of all, import MindOpt JAVA package:
22import com.alibaba.damo.mindopt.*;
Create an optimization model model
:
28 MDOEnv env = new MDOEnv();
29 MDOModel model = new MDOModel(env);
30 model.set(MDO.StringAttr.ModelName, "LP_01");
Next, we set the optimization sense to minimization via MDOModel.set
and add four decision variables using MDOModel.addVar
to specify their lower and upper bounds, variable types and names (please refer to Attributes for the detailed usage of model attributes, and JAVA API for other JAVA API information):
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");
Next, we input the linear constraints into the model model
:
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");
Once the model is constructed, we call MDOModel.optimize
to solve the problem:
57 // Step 3. Solve the problem and populate optimization result.
58 model.optimize();
We can retrieive the optimal objective value using MDOModel.get
and model attribute ObjVal, as well as optimal solutions using MDOVar.get
and attribute X. For other attributes please check Attributes.
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 }
Complete example codes are provided in 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}