1 /* A few tests for the maxmin library */
3 /* Copyright (c) 2007-2015. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include "xbt/sysdep.h"
10 #include "surf/maxmin.h"
12 #include "xbt/module.h"
14 #include "src/surf/surf_interface.hpp"
16 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, "Messages specific for surf example");
18 #define PRINT_VAR(var) XBT_DEBUG(#var " = %g",lmm_variable_getvalue(var))
19 #define SHOW_EXPR(expr) XBT_DEBUG(#expr " = %g",expr)
22 /* ==l1== L2 ==L3== */
31 static double dichotomy(double func(double), double min, double max, double min_error)
33 double overall_error = 2 * min_error;
35 double min_func = func(min);
36 double max_func = func(max);
38 if ((min_func > 0 && max_func > 0))
40 if ((min_func < 0 && max_func < 0))
42 if ((min_func > 0 && max_func < 0))
47 while (overall_error > min_error) {
48 SHOW_EXPR(overall_error);
49 if ((min_func > 0 && max_func > 0) || (min_func < 0 && max_func < 0) || (min_func > 0 && max_func < 0)) {
51 } //TODO replace by xbt_assert
58 double middle = (max + min) / 2.0;
59 if (fabs(min - middle) < 1e-12 || fabs(max - middle) < 1e-12) {
62 double middle_func = func(middle);
64 SHOW_EXPR(middle_func);
66 if (middle_func < 0) {
68 min_func = middle_func;
69 overall_error = max_func - middle_func;
70 } else if (middle_func > 0) {
72 max_func = middle_func;
73 overall_error = middle_func - min_func;
78 return ((min + max) / 2.0);
83 static double diff_lagrange_test_1(double x)
85 return -(3 / (1 + 3 * x * x / 2) - 3 / (2 * (3 * (a_test_1 - x) * (a_test_1 - x) / 2 + 1)) +
86 3 / (2 * (3 * (b_test_1 - a_test_1 + x) * (b_test_1 - a_test_1 + x) / 2 + 1)));
89 static void test1(method_t method)
91 lmm_system_t Sys = nullptr;
92 lmm_constraint_t L1 = nullptr;
93 lmm_constraint_t L2 = nullptr;
94 lmm_constraint_t L3 = nullptr;
96 lmm_variable_t R_1_2_3 = nullptr;
97 lmm_variable_t R_1 = nullptr;
98 lmm_variable_t R_2 = nullptr;
99 lmm_variable_t R_3 = nullptr;
104 double max_deviation = 0.0;
106 if (method == LAGRANGE_VEGAS)
107 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
108 else if (method == LAGRANGE_RENO)
109 lmm_set_default_protocol_function(func_reno_f, func_reno_fpi, func_reno_fpi);
111 Sys = lmm_system_new(1);
112 L1 = lmm_constraint_new(Sys, static_cast<void *>(const_cast<char*>("L1")), a);
113 L2 = lmm_constraint_new(Sys, static_cast<void *>(const_cast<char*>("L2")), b);
114 L3 = lmm_constraint_new(Sys, static_cast<void *>(const_cast<char*>("L3")), a);
116 R_1_2_3 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "R 1->2->3")), 1.0, -1.0, 3);
117 R_1 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "R 1")), 1.0, -1.0, 1);
118 R_2 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "R 2")), 1.0, -1.0, 1);
119 R_3 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "R 3")), 1.0, -1.0, 1);
121 lmm_update_variable_weight(Sys, R_1_2_3, 1.0);
122 lmm_update_variable_weight(Sys, R_1, 1.0);
123 lmm_update_variable_weight(Sys, R_2, 1.0);
124 lmm_update_variable_weight(Sys, R_3, 1.0);
126 lmm_expand(Sys, L1, R_1_2_3, 1.0);
127 lmm_expand(Sys, L2, R_1_2_3, 1.0);
128 lmm_expand(Sys, L3, R_1_2_3, 1.0);
130 lmm_expand(Sys, L1, R_1, 1.0);
131 lmm_expand(Sys, L2, R_2, 1.0);
132 lmm_expand(Sys, L3, R_3, 1.0);
134 if (method == MAXMIN) {
137 if (method == LAGRANGE_VEGAS) {
138 x = 3 * a / 4 - 3 * b / 8 + sqrt(9 * b * b + 4 * a * a - 4 * a * b) / 8;
139 /* Computed with mupad and D_f=1.0 */
146 } else if (method == LAGRANGE_RENO) {
149 x = dichotomy(diff_lagrange_test_1, 0, a, 1e-13);
156 xbt_die( "Invalid method");
161 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_1) - x));
162 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_3) - x));
163 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_2) - (b - a + x)));
164 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_1_2_3) - (a - x)));
166 if (max_deviation > 0.00001) { // Legacy value used in lagrange.c
167 XBT_WARN("Max Deviation from optimal solution : %g", max_deviation);
168 XBT_WARN("Found x = %1.20f", x);
169 XBT_WARN("Deviation from optimal solution (R_1 = %g): %1.20f", x, lmm_variable_getvalue(R_1) - x);
170 XBT_WARN("Deviation from optimal solution (R_2 = %g): %1.20f", b - a + x,
171 lmm_variable_getvalue(R_2) - (b - a + x));
172 XBT_WARN("Deviation from optimal solution (R_3 = %g): %1.20f", x, lmm_variable_getvalue(R_3) - x);
173 XBT_WARN("Deviation from optimal solution (R_1_2_3 = %g): %1.20f", a - x,
174 lmm_variable_getvalue(R_1_2_3) - (a - x));
183 lmm_variable_free(Sys, R_1_2_3);
184 lmm_variable_free(Sys, R_1);
185 lmm_variable_free(Sys, R_2);
186 lmm_variable_free(Sys, R_3);
187 lmm_system_free(Sys);
190 static void test2(method_t method)
192 lmm_system_t Sys = nullptr;
193 lmm_constraint_t CPU1 = nullptr;
194 lmm_constraint_t CPU2 = nullptr;
196 lmm_variable_t T1 = nullptr;
197 lmm_variable_t T2 = nullptr;
199 if (method == LAGRANGE_VEGAS)
200 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
201 if (method == LAGRANGE_RENO)
202 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
204 Sys = lmm_system_new(1);
205 CPU1 = lmm_constraint_new(Sys, static_cast<void *>(const_cast<char*>( "CPU1")), 200.0);
206 CPU2 = lmm_constraint_new(Sys, static_cast<void *>(const_cast<char*>( "CPU2")), 100.0);
208 T1 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "T1")), 1.0, -1.0, 1);
209 T2 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "T2")), 1.0, -1.0, 1);
211 lmm_update_variable_weight(Sys, T1, 1.0);
212 lmm_update_variable_weight(Sys, T2, 1.0);
214 lmm_expand(Sys, CPU1, T1, 1.0);
215 lmm_expand(Sys, CPU2, T2, 1.0);
217 if (method == MAXMIN) {
219 } else if (method == LAGRANGE_VEGAS || method == LAGRANGE_RENO) {
222 xbt_die("Invalid method");
228 lmm_variable_free(Sys, T1);
229 lmm_variable_free(Sys, T2);
230 lmm_system_free(Sys);
233 static void test3(method_t method)
243 lmm_system_t Sys = nullptr;
244 lmm_constraint_t *tmp_cnst = nullptr;
245 lmm_variable_t *tmp_var = nullptr;
248 /*array to add the the constraints of fictitious variables */
249 double B[15] = { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 1 };
251 A = xbt_new0(double *, links + 5);
253 for (i = 0; i < links + 5; i++) {
254 A[i] = xbt_new0(double, flows + 5);
255 for (j = 0; j < flows + 5; j++) {
258 if (i >= links || j >= flows) {
264 /*matrix that store the constraints/topology */
300 if (method == LAGRANGE_VEGAS)
301 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
302 if (method == LAGRANGE_RENO)
303 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
305 Sys = lmm_system_new(1);
307 tmp_name = xbt_new0(char *, 31);
309 /* Creates the constraints */
310 tmp_cnst = xbt_new0(lmm_constraint_t, 15);
311 for (i = 0; i < 15; i++) {
312 tmp_name[i] = bprintf("C_%03d", i);
313 tmp_cnst[i] = lmm_constraint_new(Sys, static_cast<void *>(tmp_name[i]), B[i]);
316 /* Creates the variables */
317 tmp_var = xbt_new0(lmm_variable_t, 16);
318 for (j = 0; j < 16; j++) {
319 tmp_name[i + j] = bprintf("X_%03d", j);
320 tmp_var[j] = lmm_variable_new(Sys, static_cast<void *>(tmp_name[i + j]), 1.0, -1.0, 15);
321 lmm_update_variable_weight(Sys, tmp_var[j], 1.0);
324 /* Link constraints and variables */
325 for (i = 0; i < 15; i++) {
326 for (j = 0; j < 16; j++) {
328 lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0);
333 if (method == MAXMIN) {
335 } else if (method == LAGRANGE_VEGAS) {
337 } else if (method == LAGRANGE_RENO) {
340 xbt_die("Invalid method");
343 for (j = 0; j < 16; j++) {
344 PRINT_VAR(tmp_var[j]);
347 for (j = 0; j < 16; j++)
348 lmm_variable_free(Sys, tmp_var[j]);
351 for (i = 0; i < 31; i++)
352 xbt_free(tmp_name[i]);
354 lmm_system_free(Sys);
355 for (i = 0; i < links + 5; i++)
360 int main(int argc, char **argv)
362 surf_init(&argc, argv);
364 XBT_INFO("***** Test 1 (Max-Min)");
366 XBT_INFO("***** Test 1 (Lagrange - Vegas)");
367 test1(LAGRANGE_VEGAS);
368 XBT_INFO("***** Test 1 (Lagrange - Reno)");
369 test1(LAGRANGE_RENO);
371 XBT_INFO("***** Test 2 (Max-Min)");
373 XBT_INFO("***** Test 2 (Lagrange - Vegas)");
374 test2(LAGRANGE_VEGAS);
375 XBT_INFO("***** Test 2 (Lagrange - Reno)");
376 test2(LAGRANGE_RENO);
378 XBT_INFO("***** Test 3 (Max-Min)");
380 XBT_INFO("***** Test 3 (Lagrange - Vegas)");
381 test3(LAGRANGE_VEGAS);
382 XBT_INFO("***** Test 3 (Lagrange - Reno)");
383 test3(LAGRANGE_RENO);