5.2.5. MILP 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 Mixed-Integer Linear Programming.

First of all, include JAVA package:

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

Create an optimization model model:

30        MDOEnv env = new MDOEnv(); 
31        MDOModel model = new MDOModel(env); 
32        model.set(MDO.StringAttr.ModelName, "MILP_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):

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, 1.0, 'I', "x0");
41            x[1] = model.addVar(0.0, MDO.INFINITY, 2.0, 'I', "x1");
42            x[2] = model.addVar(0.0, MDO.INFINITY, 1.0, 'I', "x2");
43            x[3] = model.addVar(0.0, MDO.INFINITY, 1.0, 'C', "x3");

Next, We input the linear constraints:

45            // Add constraints.
46            double[][] consV = new double[][]{
47                { 1.0, 1.0, 2.0, 3.0},
48                { 1.0, 0,  -1.0, 6.0} 
49            };
50
51            MDOLinExpr tempLinExpr1 = new MDOLinExpr();
52            tempLinExpr1.addTerms(consV[0], x);
53            model.addConstr(tempLinExpr1, MDO.GREATER_EQUAL, 1.0, "c0");
54
55            MDOLinExpr tempLinExpr2 = new MDOLinExpr();
56            tempLinExpr2.addTerms(consV[1], x);
57            model.addConstr(tempLinExpr2, MDO.EQUAL, 1.0, "c1");

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

60            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.

62            if (model.get(MDO.IntAttr.Status) == MDO.OPTIMAL) {
63                System.out.println("Optimal objective value is: " + model.get(MDO.DoubleAttr.ObjVal));
64                System.out.println("Decision variables: ");
65                for(int i = 0; i < 4; i++) {
66                    System.out.println( "x[" + i + "] = " + x[i].get(MDO.DoubleAttr.X));
67                }
68            }
69            else {
70                System.out.println("No feasible solution.");
71            }

Complete example codes are provided in MdoMiloEx1.java.

 1/*
 2 *  Description
 3 *  -----------
 4 *
 5 *  Mixed Integer 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 *  Integers
21 *    x0 x1 x2
22 *  End
23 */
24import com.alibaba.damo.mindopt.*;
25import java.util.*;
26
27public class MdoMiloEx1 {
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, "MILP_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, 1.0, 'I', "x0");
41            x[1] = model.addVar(0.0, MDO.INFINITY, 2.0, 'I', "x1");
42            x[2] = model.addVar(0.0, MDO.INFINITY, 1.0, 'I', "x2");
43            x[3] = model.addVar(0.0, MDO.INFINITY, 1.0, 'C', "x3");
44
45            // Add constraints.
46            double[][] consV = new double[][]{
47                { 1.0, 1.0, 2.0, 3.0},
48                { 1.0, 0,  -1.0, 6.0} 
49            };
50
51            MDOLinExpr tempLinExpr1 = new MDOLinExpr();
52            tempLinExpr1.addTerms(consV[0], x);
53            model.addConstr(tempLinExpr1, MDO.GREATER_EQUAL, 1.0, "c0");
54
55            MDOLinExpr tempLinExpr2 = new MDOLinExpr();
56            tempLinExpr2.addTerms(consV[1], x);
57            model.addConstr(tempLinExpr2, MDO.EQUAL, 1.0, "c1");
58    
59            // Step 3. Solve the problem and populate the result.
60            model.optimize();
61
62            if (model.get(MDO.IntAttr.Status) == MDO.OPTIMAL) {
63                System.out.println("Optimal objective value is: " + model.get(MDO.DoubleAttr.ObjVal));
64                System.out.println("Decision variables: ");
65                for(int i = 0; i < 4; i++) {
66                    System.out.println( "x[" + i + "] = " + x[i].get(MDO.DoubleAttr.X));
67                }
68            }
69            else {
70                System.out.println("No feasible solution.");
71            }
72        } catch (Exception e) { 
73            System.out.println("Exception during optimization");
74            e.printStackTrace();
75        } finally { 
76            model.dispose();
77            env.dispose();
78        }
79    }
80}