Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
xbt_new0/free => new/delete[]
[simgrid.git] / teshsuite / surf / lmm_usage / lmm_usage.cpp
1 /* A few tests for the maxmin library                                       */
2
3 /* Copyright (c) 2007-2017. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "simgrid/msg.h"
10 #include "src/surf/surf_interface.hpp"
11 #include "surf/maxmin.hpp"
12 #include "xbt/log.h"
13 #include "xbt/module.h"
14 #include "xbt/sysdep.h"
15 #include <cmath>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, "Messages specific for surf example");
18
19 #define PRINT_VAR(var) XBT_DEBUG(#var " = %g",lmm_variable_getvalue(var))
20 #define SHOW_EXPR(expr) XBT_DEBUG(#expr " = %g",expr)
21
22 /*        ______                 */
23 /*  ==l1==  L2  ==L3==           */
24 /*        ------                 */
25
26 enum method_t { MAXMIN, LAGRANGE_RENO, LAGRANGE_VEGAS };
27
28 static double dichotomy(double func(double), double min, double max, double min_error)
29 {
30   double overall_error = 2 * min_error;
31
32   double min_func = func(min);
33   double max_func = func(max);
34
35   if ((min_func > 0 && max_func > 0))
36     return min - 1.0;
37   if ((min_func < 0 && max_func < 0))
38     return max + 1.0;
39   if ((min_func > 0 && max_func < 0))
40     abort();
41
42   SHOW_EXPR(min_error);
43
44   while (overall_error > min_error) {
45     SHOW_EXPR(overall_error);
46     xbt_assert(min_func <= 0 || max_func <= 0);
47     xbt_assert(min_func >= 0 || max_func >= 0);
48     xbt_assert(min_func <= 0 || max_func >= 0);
49
50     SHOW_EXPR(min);
51     SHOW_EXPR(min_func);
52     SHOW_EXPR(max);
53     SHOW_EXPR(max_func);
54
55     double middle = (max + min) / 2.0;
56     if (fabs(min - middle) < 1e-12 || fabs(max - middle) < 1e-12) {
57       break;
58     }
59     double middle_func = func(middle);
60     SHOW_EXPR(middle);
61     SHOW_EXPR(middle_func);
62
63     if (middle_func < 0) {
64       min = middle;
65       min_func = middle_func;
66       overall_error = max_func - middle_func;
67     } else if (middle_func > 0) {
68       max = middle;
69       max_func = middle_func;
70       overall_error = middle_func - min_func;
71     } else {
72       overall_error = 0;
73     }
74   }
75   return ((min + max) / 2.0);
76 }
77
78 double a_test_1 = 0;
79 double b_test_1 = 0;
80 static double diff_lagrange_test_1(double x)
81 {
82   return -(3 / (1 + 3 * x * x / 2) - 3 / (2 * (3 * (a_test_1 - x) * (a_test_1 - x) / 2 + 1)) +
83            3 / (2 * (3 * (b_test_1 - a_test_1 + x) * (b_test_1 - a_test_1 + x) / 2 + 1)));
84 }
85
86 static void test1(method_t method)
87 {
88   double a = 1.0;
89   double b = 10.0;
90
91   if (method == LAGRANGE_VEGAS)
92     lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
93   else if (method == LAGRANGE_RENO)
94     lmm_set_default_protocol_function(func_reno_f, func_reno_fpi, func_reno_fpi);
95
96   lmm_system_t Sys = lmm_system_new(1);
97   lmm_constraint_t L1 = lmm_constraint_new(Sys, nullptr, a);
98   lmm_constraint_t L2 = lmm_constraint_new(Sys, nullptr, b);
99   lmm_constraint_t L3 = lmm_constraint_new(Sys, nullptr, a);
100
101   lmm_variable_t R_1_2_3 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 3);
102   lmm_variable_t R_1 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
103   lmm_variable_t R_2 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
104   lmm_variable_t R_3 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
105
106   lmm_update_variable_weight(Sys, R_1_2_3, 1.0);
107   lmm_update_variable_weight(Sys, R_1, 1.0);
108   lmm_update_variable_weight(Sys, R_2, 1.0);
109   lmm_update_variable_weight(Sys, R_3, 1.0);
110
111   lmm_expand(Sys, L1, R_1_2_3, 1.0);
112   lmm_expand(Sys, L2, R_1_2_3, 1.0);
113   lmm_expand(Sys, L3, R_1_2_3, 1.0);
114
115   lmm_expand(Sys, L1, R_1, 1.0);
116   lmm_expand(Sys, L2, R_2, 1.0);
117   lmm_expand(Sys, L3, R_3, 1.0);
118
119   if (method == MAXMIN) {
120     lmm_solve(Sys);
121   } else {
122     double x;
123     if (method == LAGRANGE_VEGAS) {
124       x = 3 * a / 4 - 3 * b / 8 + sqrt(9 * b * b + 4 * a * a - 4 * a * b) / 8;
125       /* Computed with mupad and D_f=1.0 */
126       if (x > a) {
127         x = a;
128       }
129       if (x < 0) {
130         x = 0;
131       }
132     } else if (method == LAGRANGE_RENO) {
133       a_test_1 = a;
134       b_test_1 = b;
135       x = dichotomy(diff_lagrange_test_1, 0, a, 1e-13);
136
137       if (x < 0)
138         x = 0;
139       if (x > a)
140         x = a;
141     } else {
142       xbt_die( "Invalid method");
143     }
144
145     lagrange_solve(Sys);
146
147     double max_deviation = 0.0;
148     max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_1) - x));
149     max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_3) - x));
150     max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_2) - (b - a + x)));
151     max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_1_2_3) - (a - x)));
152
153     if (max_deviation > 0.00001) { // Legacy value used in lagrange.c
154       XBT_WARN("Max Deviation from optimal solution : %g", max_deviation);
155       XBT_WARN("Found x = %1.20f", x);
156       XBT_WARN("Deviation from optimal solution (R_1 = %g): %1.20f", x, lmm_variable_getvalue(R_1) - x);
157       XBT_WARN("Deviation from optimal solution (R_2 = %g): %1.20f", b - a + x,
158                lmm_variable_getvalue(R_2) - (b - a + x));
159       XBT_WARN("Deviation from optimal solution (R_3 = %g): %1.20f", x, lmm_variable_getvalue(R_3) - x);
160       XBT_WARN("Deviation from optimal solution (R_1_2_3 = %g): %1.20f", a - x,
161                lmm_variable_getvalue(R_1_2_3) - (a - x));
162     }
163   }
164
165   PRINT_VAR(R_1_2_3);
166   PRINT_VAR(R_1);
167   PRINT_VAR(R_2);
168   PRINT_VAR(R_3);
169
170   lmm_variable_free(Sys, R_1_2_3);
171   lmm_variable_free(Sys, R_1);
172   lmm_variable_free(Sys, R_2);
173   lmm_variable_free(Sys, R_3);
174   lmm_system_free(Sys);
175 }
176
177 static void test2(method_t method)
178 {
179   if (method == LAGRANGE_VEGAS)
180     lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
181   if (method == LAGRANGE_RENO)
182     lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
183
184   lmm_system_t Sys = lmm_system_new(1);
185   lmm_constraint_t CPU1 = lmm_constraint_new(Sys, nullptr, 200.0);
186   lmm_constraint_t CPU2 = lmm_constraint_new(Sys, nullptr, 100.0);
187
188   lmm_variable_t T1 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
189   lmm_variable_t T2 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
190
191   lmm_update_variable_weight(Sys, T1, 1.0);
192   lmm_update_variable_weight(Sys, T2, 1.0);
193
194   lmm_expand(Sys, CPU1, T1, 1.0);
195   lmm_expand(Sys, CPU2, T2, 1.0);
196
197   if (method == MAXMIN) {
198     lmm_solve(Sys);
199   } else if (method == LAGRANGE_VEGAS || method == LAGRANGE_RENO) {
200     lagrange_solve(Sys);
201   } else {
202     xbt_die("Invalid method");
203   }
204
205   PRINT_VAR(T1);
206   PRINT_VAR(T2);
207
208   lmm_variable_free(Sys, T1);
209   lmm_variable_free(Sys, T2);
210   lmm_system_free(Sys);
211 }
212
213 static void test3(method_t method)
214 {
215   int flows = 11;
216   int links = 10;
217
218   double** A = new double*[links + 5];
219   /* array to add the constraints of fictitious variables */
220   double B[15] = { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 1 };
221
222   for (int i = 0; i < links + 5; i++) {
223     A[i] = new double[flows + 5];
224     for (int j = 0; j < flows + 5; j++) {
225       A[i][j] = 0.0;
226
227       if (i >= links || j >= flows) {
228         A[i][j] = 0.0;
229       }
230     }
231   }
232
233   /*matrix that store the constraints/topology */
234   A[0][1] = A[0][7] =                                1.0;
235   A[1][1] = A[1][7] = A[1][8] =                      1.0;
236   A[2][1] = A[2][8] =                                1.0;
237   A[3][8] =                                          1.0;
238   A[4][0] = A[4][3] = A[4][9] =                      1.0;
239   A[5][0] = A[5][3] = A[5][4] = A[5][9] =            1.0;
240   A[6][0] = A[6][4] = A[6][9] = A[6][10] =           1.0;
241   A[7][2] = A[7][4] = A[7][6] = A[7][9] = A[7][10] = 1.0;
242   A[8][2] = A[8][10] =                               1.0;
243   A[9][5] = A[9][6] = A[9][9] =                      1.0;
244   A[10][11] =                                        1.0;
245   A[11][12] =                                        1.0;
246   A[12][13] =                                        1.0;
247   A[13][14] =                                        1.0;
248   A[14][15] =                                        1.0;
249
250   if (method == LAGRANGE_VEGAS)
251     lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
252   if (method == LAGRANGE_RENO)
253     lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
254
255   lmm_system_t Sys = lmm_system_new(1);
256
257   /* Creates the constraints */
258   lmm_constraint_t* tmp_cnst = new lmm_constraint_t[15];
259   for (int i = 0; i < 15; i++)
260     tmp_cnst[i] = lmm_constraint_new(Sys, nullptr, B[i]);
261
262   /* Creates the variables */
263   lmm_variable_t* tmp_var = new lmm_variable_t[16];
264   for (int j = 0; j < 16; j++) {
265     tmp_var[j] = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 15);
266     lmm_update_variable_weight(Sys, tmp_var[j], 1.0);
267   }
268
269   /* Link constraints and variables */
270   for (int i = 0; i < 15; i++)
271     for (int j = 0; j < 16; j++)
272       if (A[i][j])
273         lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0);
274
275   if (method == MAXMIN) {
276     lmm_solve(Sys);
277   } else if (method == LAGRANGE_VEGAS) {
278     lagrange_solve(Sys);
279   } else if (method == LAGRANGE_RENO) {
280     lagrange_solve(Sys);
281   } else {
282     xbt_die("Invalid method");
283   }
284
285   for (int j = 0; j < 16; j++)
286     PRINT_VAR(tmp_var[j]);
287
288   for (int j = 0; j < 16; j++)
289     lmm_variable_free(Sys, tmp_var[j]);
290   delete[] tmp_var;
291   delete[] tmp_cnst;
292   lmm_system_free(Sys);
293   for (int i = 0; i < links + 5; i++)
294     delete[] A[i];
295   delete[] A;
296 }
297
298 int main(int argc, char** argv)
299 {
300   MSG_init(&argc, argv);
301   XBT_INFO("***** Test 1 (Max-Min)");
302   test1(MAXMIN);
303   XBT_INFO("***** Test 1 (Lagrange - Vegas)");
304   test1(LAGRANGE_VEGAS);
305   XBT_INFO("***** Test 1 (Lagrange - Reno)");
306   test1(LAGRANGE_RENO);
307
308   XBT_INFO("***** Test 2 (Max-Min)");
309   test2(MAXMIN);
310   XBT_INFO("***** Test 2 (Lagrange - Vegas)");
311   test2(LAGRANGE_VEGAS);
312   XBT_INFO("***** Test 2 (Lagrange - Reno)");
313   test2(LAGRANGE_RENO);
314
315   XBT_INFO("***** Test 3 (Max-Min)");
316   test3(MAXMIN);
317   XBT_INFO("***** Test 3 (Lagrange - Vegas)");
318   test3(LAGRANGE_VEGAS);
319   XBT_INFO("***** Test 3 (Lagrange - Reno)");
320   test3(LAGRANGE_RENO);
321
322   return 0;
323 }