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 xbt_assert(min_func <= 0 || max_func <= 0);
50 xbt_assert(min_func >= 0 || max_func >= 0);
51 xbt_assert(min_func <= 0 || max_func >= 0);
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)
94 if (method == LAGRANGE_VEGAS)
95 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
96 else if (method == LAGRANGE_RENO)
97 lmm_set_default_protocol_function(func_reno_f, func_reno_fpi, func_reno_fpi);
99 lmm_system_t Sys = lmm_system_new(1);
100 lmm_constraint_t L1 = lmm_constraint_new(Sys, static_cast<void *>(const_cast<char*>("L1")), a);
101 lmm_constraint_t L2 = lmm_constraint_new(Sys, static_cast<void *>(const_cast<char*>("L2")), b);
102 lmm_constraint_t L3 = lmm_constraint_new(Sys, static_cast<void *>(const_cast<char*>("L3")), a);
104 lmm_variable_t R_1_2_3 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "R 1->2->3")), 1.0, -1.0, 3);
105 lmm_variable_t R_1 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "R 1")), 1.0, -1.0, 1);
106 lmm_variable_t R_2 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "R 2")), 1.0, -1.0, 1);
107 lmm_variable_t R_3 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "R 3")), 1.0, -1.0, 1);
109 lmm_update_variable_weight(Sys, R_1_2_3, 1.0);
110 lmm_update_variable_weight(Sys, R_1, 1.0);
111 lmm_update_variable_weight(Sys, R_2, 1.0);
112 lmm_update_variable_weight(Sys, R_3, 1.0);
114 lmm_expand(Sys, L1, R_1_2_3, 1.0);
115 lmm_expand(Sys, L2, R_1_2_3, 1.0);
116 lmm_expand(Sys, L3, R_1_2_3, 1.0);
118 lmm_expand(Sys, L1, R_1, 1.0);
119 lmm_expand(Sys, L2, R_2, 1.0);
120 lmm_expand(Sys, L3, R_3, 1.0);
122 if (method == MAXMIN) {
126 if (method == LAGRANGE_VEGAS) {
127 x = 3 * a / 4 - 3 * b / 8 + sqrt(9 * b * b + 4 * a * a - 4 * a * b) / 8;
128 /* Computed with mupad and D_f=1.0 */
135 } else if (method == LAGRANGE_RENO) {
138 x = dichotomy(diff_lagrange_test_1, 0, a, 1e-13);
145 xbt_die( "Invalid method");
150 double max_deviation = 0.0;
151 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_1) - x));
152 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_3) - x));
153 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_2) - (b - a + x)));
154 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_1_2_3) - (a - x)));
156 if (max_deviation > 0.00001) { // Legacy value used in lagrange.c
157 XBT_WARN("Max Deviation from optimal solution : %g", max_deviation);
158 XBT_WARN("Found x = %1.20f", x);
159 XBT_WARN("Deviation from optimal solution (R_1 = %g): %1.20f", x, lmm_variable_getvalue(R_1) - x);
160 XBT_WARN("Deviation from optimal solution (R_2 = %g): %1.20f", b - a + x,
161 lmm_variable_getvalue(R_2) - (b - a + x));
162 XBT_WARN("Deviation from optimal solution (R_3 = %g): %1.20f", x, lmm_variable_getvalue(R_3) - x);
163 XBT_WARN("Deviation from optimal solution (R_1_2_3 = %g): %1.20f", a - x,
164 lmm_variable_getvalue(R_1_2_3) - (a - x));
173 lmm_variable_free(Sys, R_1_2_3);
174 lmm_variable_free(Sys, R_1);
175 lmm_variable_free(Sys, R_2);
176 lmm_variable_free(Sys, R_3);
177 lmm_system_free(Sys);
180 static void test2(method_t method)
182 if (method == LAGRANGE_VEGAS)
183 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
184 if (method == LAGRANGE_RENO)
185 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
187 lmm_system_t Sys = lmm_system_new(1);
188 lmm_constraint_t CPU1 = lmm_constraint_new(Sys, static_cast<void *>(const_cast<char*>( "CPU1")), 200.0);
189 lmm_constraint_t CPU2 = lmm_constraint_new(Sys, static_cast<void *>(const_cast<char*>( "CPU2")), 100.0);
191 lmm_variable_t T1 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "T1")), 1.0, -1.0, 1);
192 lmm_variable_t T2 = lmm_variable_new(Sys, static_cast<void *>(const_cast<char*>( "T2")), 1.0, -1.0, 1);
194 lmm_update_variable_weight(Sys, T1, 1.0);
195 lmm_update_variable_weight(Sys, T2, 1.0);
197 lmm_expand(Sys, CPU1, T1, 1.0);
198 lmm_expand(Sys, CPU2, T2, 1.0);
200 if (method == MAXMIN) {
202 } else if (method == LAGRANGE_VEGAS || method == LAGRANGE_RENO) {
205 xbt_die("Invalid method");
211 lmm_variable_free(Sys, T1);
212 lmm_variable_free(Sys, T2);
213 lmm_system_free(Sys);
216 static void test3(method_t method)
223 /*array to add the the constraints of fictitious variables */
224 double B[15] = { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 1 };
226 A = xbt_new0(double *, links + 5);
228 for (int i = 0; i < links + 5; i++) {
229 A[i] = xbt_new0(double, flows + 5);
230 for (int j = 0; j < flows + 5; j++) {
233 if (i >= links || j >= flows) {
239 /*matrix that store the constraints/topology */
275 if (method == LAGRANGE_VEGAS)
276 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
277 if (method == LAGRANGE_RENO)
278 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
280 lmm_system_t Sys = lmm_system_new(1);
282 char **tmp_name = xbt_new0(char *, 31);
284 /* Creates the constraints */
285 lmm_constraint_t *tmp_cnst = xbt_new0(lmm_constraint_t, 15);
286 for (int i = 0; i < 15; i++) {
287 tmp_name[i] = bprintf("C_%03d", i);
288 tmp_cnst[i] = lmm_constraint_new(Sys, static_cast<void *>(tmp_name[i]), B[i]);
291 /* Creates the variables */
292 lmm_variable_t *tmp_var = xbt_new0(lmm_variable_t, 16);
293 for (int j = 0; j < 16; j++) {
294 tmp_name[15 + j] = bprintf("X_%03d", j);
295 tmp_var[j] = lmm_variable_new(Sys, static_cast<void *>(tmp_name[15 + j]), 1.0, -1.0, 15);
296 lmm_update_variable_weight(Sys, tmp_var[j], 1.0);
299 /* Link constraints and variables */
300 for (int i = 0; i < 15; i++) {
301 for (int j = 0; j < 16; j++) {
303 lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0);
308 if (method == MAXMIN) {
310 } else if (method == LAGRANGE_VEGAS) {
312 } else if (method == LAGRANGE_RENO) {
315 xbt_die("Invalid method");
318 for (int j = 0; j < 16; j++) {
319 PRINT_VAR(tmp_var[j]);
322 for (int j = 0; j < 16; j++)
323 lmm_variable_free(Sys, tmp_var[j]);
326 for (int i = 0; i < 31; i++)
327 xbt_free(tmp_name[i]);
329 lmm_system_free(Sys);
330 for (int i = 0; i < links + 5; i++)
335 int main(int argc, char **argv)
337 surf_init(&argc, argv);
339 XBT_INFO("***** Test 1 (Max-Min)");
341 XBT_INFO("***** Test 1 (Lagrange - Vegas)");
342 test1(LAGRANGE_VEGAS);
343 XBT_INFO("***** Test 1 (Lagrange - Reno)");
344 test1(LAGRANGE_RENO);
346 XBT_INFO("***** Test 2 (Max-Min)");
348 XBT_INFO("***** Test 2 (Lagrange - Vegas)");
349 test2(LAGRANGE_VEGAS);
350 XBT_INFO("***** Test 2 (Lagrange - Reno)");
351 test2(LAGRANGE_RENO);
353 XBT_INFO("***** Test 3 (Max-Min)");
355 XBT_INFO("***** Test 3 (Lagrange - Vegas)");
356 test3(LAGRANGE_VEGAS);
357 XBT_INFO("***** Test 3 (Lagrange - Reno)");
358 test3(LAGRANGE_RENO);