1 /* Test MIP_Problem Java test class of the Parma Polyhedra Library Java
2    interface.
3    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
4    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
5 
6 This file is part of the Parma Polyhedra Library (PPL).
7 
8 The PPL is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The PPL is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
21 
22 For the most up-to-date information see the Parma Polyhedra Library
23 site: http://bugseng.com/products/ppl/ . */
24 
25 import parma_polyhedra_library.*;
26 
27 public class MIP_Problem_test1 {
28     static {
29         try {
30             System.loadLibrary("ppl_java");
31         }
32         catch (UnsatisfiedLinkError  e) {
33             System.out.println("Unable to load the library");
34             System.out.println(e.getMessage());
35             System.exit(-1);
36         }
37     }
38 
39     // This code tests the MIP_Problem methods.
test01()40     public static boolean test01() {
41         Variable A = new Variable(0);
42         Variable B = new Variable(1);
43         Variable C = new Variable(2);
44         Linear_Expression_Variable le_b = new Linear_Expression_Variable(B);
45         Linear_Expression_Variable le_c = new Linear_Expression_Variable(C);
46         Linear_Expression_Variable le_a = new Linear_Expression_Variable(A);
47         Coefficient coeff_1 = new Coefficient(1);
48         Coefficient coeff_3 = new Coefficient(3);
49         Coefficient coeff_5 = new Coefficient(5);
50         Linear_Expression le_1 = new Linear_Expression_Coefficient(coeff_1);
51         Linear_Expression le_3 = new Linear_Expression_Coefficient(coeff_3);
52         Linear_Expression le_5 = new Linear_Expression_Coefficient(coeff_5);
53 
54         // Constraint declarations.
55         Constraint c_a_geq_1
56           = new Constraint(le_a, Relation_Symbol.GREATER_OR_EQUAL, le_1);
57         Constraint c_a_leq_5
58           = new Constraint(le_a, Relation_Symbol.LESS_OR_EQUAL, le_5);
59         Constraint c_b_geq_3
60           = new Constraint(le_b, Relation_Symbol.GREATER_OR_EQUAL, le_3);
61         Constraint constraint1 = c_a_geq_1;
62         Constraint constraint2 = c_b_geq_3;
63         Constraint_System constraints1 = new Constraint_System();
64         constraints1.add(constraint1);
65         C_Polyhedron ph1 = new C_Polyhedron(3, Degenerate_Element.UNIVERSE);
66         ph1.add_constraints(constraints1);
67         C_Polyhedron ph2 = new C_Polyhedron(4, Degenerate_Element.UNIVERSE);
68         ph2.add_constraints(constraints1);
69         ph2.add_constraint(constraint2);
70 
71         MIP_Problem mip1
72           = new MIP_Problem(3, constraints1, le_a,
73                            Optimization_Mode.MAXIMIZATION);
74         Constraint_System mip1_constraints = mip1.constraints();
75         long mip1_dim = mip1.space_dimension();
76         Linear_Expression mip1_obj = mip1.objective_function();
77         Optimization_Mode mip1_opt = mip1.optimization_mode();
78 
79         MIP_Problem mip2 = new MIP_Problem(mip1_dim);
80         mip2.add_constraints(mip1_constraints);
81         mip2.set_objective_function(mip1_obj);
82         mip2.set_optimization_mode(mip1_opt);
83 
84         boolean ok = (mip2.space_dimension() == 3)
85            && (mip2.optimization_mode() == Optimization_Mode.MAXIMIZATION);
86         C_Polyhedron mip2_ph = new C_Polyhedron(3,
87                                                 Degenerate_Element.UNIVERSE);
88         mip2_ph.add_constraints(mip1_constraints);
89         ok = ok && new Boolean(mip2_ph.equals(ph1));
90         if (!ok)
91           return false;
92 
93         MIP_Problem mip3 = new MIP_Problem(3);
94         mip3.add_constraints(constraints1);
95         mip3.add_space_dimensions_and_embed(1);
96         mip3.set_objective_function(le_b);
97         mip3.add_constraint(constraint2);
98         mip3.set_optimization_mode(Optimization_Mode.MINIMIZATION);
99         ok = ok && (mip3.space_dimension() == 4)
100            && (mip3.optimization_mode() == Optimization_Mode.MINIMIZATION);
101         Constraint_System mip3_constraints = mip3.constraints();
102         C_Polyhedron mip3_ph = new C_Polyhedron(4,
103                                                 Degenerate_Element.UNIVERSE);
104         mip3_ph.add_constraints(mip3_constraints);
105         ok = ok && mip3_ph.equals(ph2);
106 
107         return ok;
108     }
109 
110     // This code tests more MIP_Problem methods.
test02()111     public static boolean test02() {
112         Variable A = new Variable(0);
113         Linear_Expression_Variable le_a = new Linear_Expression_Variable(A);
114         Coefficient coeff_0 = new Coefficient(0);
115         Coefficient coeff_1 = new Coefficient(1);
116         Coefficient coeff_5 = new Coefficient(5);
117         Coefficient coeff_8 = new Coefficient(8);
118         Linear_Expression le_1 = new Linear_Expression_Coefficient(coeff_1);
119         Linear_Expression le_5 = new Linear_Expression_Coefficient(coeff_5);
120         Linear_Expression le_8 = new Linear_Expression_Coefficient(coeff_8);
121 
122         // Constraint declarations.
123         Constraint c_a_geq_1
124           = new Constraint(le_a, Relation_Symbol.GREATER_OR_EQUAL, le_1);
125         Constraint c_a_leq_5
126           = new Constraint(le_a, Relation_Symbol.LESS_OR_EQUAL, le_5);
127         Constraint c_a_eq_8
128           = new Constraint(le_a, Relation_Symbol.EQUAL, le_8);
129         Constraint constraint1 = c_a_geq_1;
130         Constraint_System constraints1 = new Constraint_System();
131         constraints1.add(constraint1);
132 
133         Variables_Set var_set_A = new Variables_Set();
134         var_set_A.add(A);
135 
136         MIP_Problem mip1
137           = new MIP_Problem(1, constraints1, le_a,
138                            Optimization_Mode.MAXIMIZATION);
139         Constraint_System mip1_constraints = mip1.constraints();
140         long mip1_dim = mip1.space_dimension();
141         Linear_Expression mip1_obj = mip1.objective_function();
142         Optimization_Mode mip1_opt = mip1.optimization_mode();
143 
144         Variables_Set var_set = mip1.integer_space_dimensions();
145         boolean ok = var_set.isEmpty();
146         mip1.add_to_integer_space_dimensions(var_set_A);
147         Variables_Set var_set1 = mip1.integer_space_dimensions();
148         ok = ok && (var_set1.contains(A));
149         if (!ok)
150           return false;
151 
152         ok = mip1.is_satisfiable();
153         if (!ok)
154           return false;
155 
156         MIP_Problem_Status mip1_status;
157         mip1_status = mip1.solve();
158         ok = ok && (mip1_status == MIP_Problem_Status.UNBOUNDED_MIP_PROBLEM);
159 
160         MIP_Problem_Status mip2_status;
161         mip1.add_constraint(c_a_leq_5);
162         mip2_status = mip1.solve();
163         ok = ok && (mip2_status == MIP_Problem_Status.OPTIMIZED_MIP_PROBLEM);
164         if (!ok)
165           return false;
166 
167         MIP_Problem mip3
168           = new MIP_Problem(1, constraints1, le_a,
169                            Optimization_Mode.MAXIMIZATION);
170         MIP_Problem_Status mip3_status;
171         mip3.add_constraint(c_a_leq_5);
172         mip3.add_constraint(c_a_eq_8);
173 
174         Constraint_System cs = mip3.constraints();
175         mip3_status = mip3.solve();
176         ok = !mip3.is_satisfiable();
177         ok = ok && (mip3_status == MIP_Problem_Status.UNFEASIBLE_MIP_PROBLEM);
178         if (!ok)
179           return false;
180 
181         Generator g1 = Generator.point(le_a, coeff_1);
182         Coefficient num = coeff_1;
183         Coefficient den = coeff_1;
184         mip1.evaluate_objective_function(g1, num, den);
185         ok = (num == coeff_1 && den == coeff_1);
186         if (!ok)
187           return false;
188 
189         Linear_Expression le_5a = le_a.times(coeff_5);
190 
191         Generator f_point = mip1.feasible_point();
192         C_Polyhedron f_ph = new C_Polyhedron(1, Degenerate_Element.EMPTY);
193         f_ph.add_generator(f_point);
194         Generator expected_f_point = Generator.point(le_5a, coeff_1);
195         C_Polyhedron expected_f_ph
196           = new C_Polyhedron(1, Degenerate_Element.EMPTY);
197         expected_f_ph.add_generator(expected_f_point);
198         ok = f_ph.equals(expected_f_ph);
199 
200         Generator o_point = mip1.optimizing_point();
201         C_Polyhedron o_ph = new C_Polyhedron(1, Degenerate_Element.EMPTY);
202         o_ph.add_generator(o_point);
203         Generator expected_o_point = Generator.point(le_5a, coeff_1);
204         C_Polyhedron expected_o_ph
205           = new C_Polyhedron(1, Degenerate_Element.EMPTY);
206         expected_o_ph.add_generator(expected_o_point);
207         ok = o_ph.equals(expected_o_ph);
208 
209         Coefficient ov_num = new Coefficient(0);
210         Coefficient ov_den = new Coefficient(0);
211         mip1.optimal_value(ov_num, ov_den);
212         Linear_Expression le_ov_num
213           = new Linear_Expression_Coefficient(ov_num);
214         Linear_Expression le_ov_den
215           = new Linear_Expression_Coefficient(ov_den);
216         // ok = (le_ov_num == le_5 && le_ov_den == le_1);
217         C_Polyhedron ov_ph
218           = new C_Polyhedron(1, Degenerate_Element.EMPTY);
219         Constraint c_a_leq_ov_num
220           = new Constraint(le_a, Relation_Symbol.LESS_OR_EQUAL, le_ov_num);
221         Constraint c_a_geq_ov_num
222           = new Constraint(le_a, Relation_Symbol.GREATER_OR_EQUAL, le_ov_den);
223         ov_ph.add_constraint(c_a_leq_ov_num);
224         C_Polyhedron expected_ov_ph
225           = new C_Polyhedron(1, Degenerate_Element.EMPTY);
226         expected_ov_ph.add_constraint(c_a_leq_5);
227         expected_ov_ph.add_constraint(c_a_geq_1);
228         ok = (ov_ph.equals(expected_ov_ph));
229 
230         PPL_Test.println_if_noisy("Testing toString() and wrap_string(): ");
231         PPL_Test.println_if_noisy(IO.wrap_string(mip1.toString(), 4, 64, 60));
232         PPL_Test.println_if_noisy();
233 
234         PPL_Test.print_if_noisy("Testing max_space_dimension(): ");
235         long max_space_dim = mip1.max_space_dimension();
236         PPL_Test.println_if_noisy(max_space_dim);
237 
238         Control_Parameter_Value cp_value
239           = mip1.get_control_parameter(Control_Parameter_Name.PRICING);
240         mip1.set_control_parameter(
241           Control_Parameter_Value.PRICING_STEEPEST_EDGE_FLOAT);
242         Control_Parameter_Value cp_value1
243           = mip1.get_control_parameter(Control_Parameter_Name.PRICING);
244         ok = ok
245           && (cp_value1
246                 == Control_Parameter_Value.PRICING_STEEPEST_EDGE_FLOAT);
247         mip1.set_control_parameter(
248           Control_Parameter_Value.PRICING_STEEPEST_EDGE_EXACT);
249         Control_Parameter_Value cp_value2
250           = mip1.get_control_parameter(Control_Parameter_Name.PRICING);
251         ok = ok
252           && (cp_value2
253                 == Control_Parameter_Value.PRICING_STEEPEST_EDGE_EXACT);
254         mip1.set_control_parameter(
255           Control_Parameter_Value.PRICING_TEXTBOOK);
256         Control_Parameter_Value cp_value3
257           = mip1.get_control_parameter(Control_Parameter_Name.PRICING);
258         ok = ok
259           && (cp_value3
260                 == Control_Parameter_Value.PRICING_TEXTBOOK);
261 
262         return ok && mip1.OK();
263     }
264 
main(String[] args)265     public static void main(String[] args) {
266         Parma_Polyhedra_Library.initialize_library();
267         boolean test_result_ok =
268             Test_Executor.executeTests(MIP_Problem_test1.class);
269         Parma_Polyhedra_Library.finalize_library();
270         if (!test_result_ok)
271             System.exit(1);
272         System.exit(0);
273     }
274 }
275