1 /* A few tests for the maxmin library */
3 /* Copyright (c) 2007-2017. 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 "simgrid/msg.h"
10 #include "src/surf/surf_interface.hpp"
11 #include "surf/maxmin.hpp"
13 #include "xbt/module.h"
14 #include "xbt/sysdep.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, "Messages specific for surf example");
19 #define PRINT_VAR(var) XBT_DEBUG(#var " = %g",lmm_variable_getvalue(var))
20 #define SHOW_EXPR(expr) XBT_DEBUG(#expr " = %g",expr)
23 /* ==l1== L2 ==L3== */
32 static double dichotomy(double func(double), double min, double max, double min_error)
34 double overall_error = 2 * min_error;
36 double min_func = func(min);
37 double max_func = func(max);
39 if ((min_func > 0 && max_func > 0))
41 if ((min_func < 0 && max_func < 0))
43 if ((min_func > 0 && max_func < 0))
48 while (overall_error > min_error) {
49 SHOW_EXPR(overall_error);
50 xbt_assert(min_func <= 0 || max_func <= 0);
51 xbt_assert(min_func >= 0 || max_func >= 0);
52 xbt_assert(min_func <= 0 || max_func >= 0);
59 double middle = (max + min) / 2.0;
60 if (fabs(min - middle) < 1e-12 || fabs(max - middle) < 1e-12) {
63 double middle_func = func(middle);
65 SHOW_EXPR(middle_func);
67 if (middle_func < 0) {
69 min_func = middle_func;
70 overall_error = max_func - middle_func;
71 } else if (middle_func > 0) {
73 max_func = middle_func;
74 overall_error = middle_func - min_func;
79 return ((min + max) / 2.0);
84 static double diff_lagrange_test_1(double x)
86 return -(3 / (1 + 3 * x * x / 2) - 3 / (2 * (3 * (a_test_1 - x) * (a_test_1 - x) / 2 + 1)) +
87 3 / (2 * (3 * (b_test_1 - a_test_1 + x) * (b_test_1 - a_test_1 + x) / 2 + 1)));
90 static void test1(method_t method)
95 if (method == LAGRANGE_VEGAS)
96 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
97 else if (method == LAGRANGE_RENO)
98 lmm_set_default_protocol_function(func_reno_f, func_reno_fpi, func_reno_fpi);
100 lmm_system_t Sys = lmm_system_new(1);
101 lmm_constraint_t L1 = lmm_constraint_new(Sys, nullptr, a);
102 lmm_constraint_t L2 = lmm_constraint_new(Sys, nullptr, b);
103 lmm_constraint_t L3 = lmm_constraint_new(Sys, nullptr, a);
105 lmm_variable_t R_1_2_3 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 3);
106 lmm_variable_t R_1 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
107 lmm_variable_t R_2 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
108 lmm_variable_t R_3 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
110 lmm_update_variable_weight(Sys, R_1_2_3, 1.0);
111 lmm_update_variable_weight(Sys, R_1, 1.0);
112 lmm_update_variable_weight(Sys, R_2, 1.0);
113 lmm_update_variable_weight(Sys, R_3, 1.0);
115 lmm_expand(Sys, L1, R_1_2_3, 1.0);
116 lmm_expand(Sys, L2, R_1_2_3, 1.0);
117 lmm_expand(Sys, L3, R_1_2_3, 1.0);
119 lmm_expand(Sys, L1, R_1, 1.0);
120 lmm_expand(Sys, L2, R_2, 1.0);
121 lmm_expand(Sys, L3, R_3, 1.0);
123 if (method == MAXMIN) {
127 if (method == LAGRANGE_VEGAS) {
128 x = 3 * a / 4 - 3 * b / 8 + sqrt(9 * b * b + 4 * a * a - 4 * a * b) / 8;
129 /* Computed with mupad and D_f=1.0 */
136 } else if (method == LAGRANGE_RENO) {
139 x = dichotomy(diff_lagrange_test_1, 0, a, 1e-13);
146 xbt_die( "Invalid method");
151 double max_deviation = 0.0;
152 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_1) - x));
153 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_3) - x));
154 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_2) - (b - a + x)));
155 max_deviation = MAX(max_deviation, fabs(lmm_variable_getvalue(R_1_2_3) - (a - x)));
157 if (max_deviation > 0.00001) { // Legacy value used in lagrange.c
158 XBT_WARN("Max Deviation from optimal solution : %g", max_deviation);
159 XBT_WARN("Found x = %1.20f", x);
160 XBT_WARN("Deviation from optimal solution (R_1 = %g): %1.20f", x, lmm_variable_getvalue(R_1) - x);
161 XBT_WARN("Deviation from optimal solution (R_2 = %g): %1.20f", b - a + x,
162 lmm_variable_getvalue(R_2) - (b - a + x));
163 XBT_WARN("Deviation from optimal solution (R_3 = %g): %1.20f", x, lmm_variable_getvalue(R_3) - x);
164 XBT_WARN("Deviation from optimal solution (R_1_2_3 = %g): %1.20f", a - x,
165 lmm_variable_getvalue(R_1_2_3) - (a - x));
174 lmm_variable_free(Sys, R_1_2_3);
175 lmm_variable_free(Sys, R_1);
176 lmm_variable_free(Sys, R_2);
177 lmm_variable_free(Sys, R_3);
178 lmm_system_free(Sys);
181 static void test2(method_t method)
183 if (method == LAGRANGE_VEGAS)
184 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
185 if (method == LAGRANGE_RENO)
186 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
188 lmm_system_t Sys = lmm_system_new(1);
189 lmm_constraint_t CPU1 = lmm_constraint_new(Sys, nullptr, 200.0);
190 lmm_constraint_t CPU2 = lmm_constraint_new(Sys, nullptr, 100.0);
192 lmm_variable_t T1 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
193 lmm_variable_t T2 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
195 lmm_update_variable_weight(Sys, T1, 1.0);
196 lmm_update_variable_weight(Sys, T2, 1.0);
198 lmm_expand(Sys, CPU1, T1, 1.0);
199 lmm_expand(Sys, CPU2, T2, 1.0);
201 if (method == MAXMIN) {
203 } else if (method == LAGRANGE_VEGAS || method == LAGRANGE_RENO) {
206 xbt_die("Invalid method");
212 lmm_variable_free(Sys, T1);
213 lmm_variable_free(Sys, T2);
214 lmm_system_free(Sys);
217 static void test3(method_t method)
222 double **A = xbt_new0(double *, links + 5);
223 /* array to add 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 for (int i = 0; i < links + 5; i++) {
227 A[i] = xbt_new0(double, flows + 5);
228 for (int j = 0; j < flows + 5; j++) {
231 if (i >= links || j >= flows) {
237 /*matrix that store the constraints/topology */
238 A[0][1] = A[0][7] = 1.0;
239 A[1][1] = A[1][7] = A[1][8] = 1.0;
240 A[2][1] = A[2][8] = 1.0;
242 A[4][0] = A[4][3] = A[4][9] = 1.0;
243 A[5][0] = A[5][3] = A[5][4] = A[5][9] = 1.0;
244 A[6][0] = A[6][4] = A[6][9] = A[6][10] = 1.0;
245 A[7][2] = A[7][4] = A[7][6] = A[7][9] = A[7][10] = 1.0;
246 A[8][2] = A[8][10] = 1.0;
247 A[9][5] = A[9][6] = A[9][9] = 1.0;
254 if (method == LAGRANGE_VEGAS)
255 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
256 if (method == LAGRANGE_RENO)
257 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
259 lmm_system_t Sys = lmm_system_new(1);
261 /* Creates the constraints */
262 lmm_constraint_t *tmp_cnst = xbt_new0(lmm_constraint_t, 15);
263 for (int i = 0; i < 15; i++)
264 tmp_cnst[i] = lmm_constraint_new(Sys, nullptr, B[i]);
266 /* Creates the variables */
267 lmm_variable_t *tmp_var = xbt_new0(lmm_variable_t, 16);
268 for (int j = 0; j < 16; j++) {
269 tmp_var[j] = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 15);
270 lmm_update_variable_weight(Sys, tmp_var[j], 1.0);
273 /* Link constraints and variables */
274 for (int i = 0; i < 15; i++)
275 for (int j = 0; j < 16; j++)
277 lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0);
279 if (method == MAXMIN) {
281 } else if (method == LAGRANGE_VEGAS) {
283 } else if (method == LAGRANGE_RENO) {
286 xbt_die("Invalid method");
289 for (int j = 0; j < 16; j++)
290 PRINT_VAR(tmp_var[j]);
292 for (int j = 0; j < 16; j++)
293 lmm_variable_free(Sys, tmp_var[j]);
296 lmm_system_free(Sys);
297 for (int i = 0; i < links + 5; i++)
302 int main(int argc, char** argv)
304 MSG_init(&argc, argv);
305 XBT_INFO("***** Test 1 (Max-Min)");
307 XBT_INFO("***** Test 1 (Lagrange - Vegas)");
308 test1(LAGRANGE_VEGAS);
309 XBT_INFO("***** Test 1 (Lagrange - Reno)");
310 test1(LAGRANGE_RENO);
312 XBT_INFO("***** Test 2 (Max-Min)");
314 XBT_INFO("***** Test 2 (Lagrange - Vegas)");
315 test2(LAGRANGE_VEGAS);
316 XBT_INFO("***** Test 2 (Lagrange - Reno)");
317 test2(LAGRANGE_RENO);
319 XBT_INFO("***** Test 3 (Max-Min)");
321 XBT_INFO("***** Test 3 (Lagrange - Vegas)");
322 test3(LAGRANGE_VEGAS);
323 XBT_INFO("***** Test 3 (Lagrange - Reno)");
324 test3(LAGRANGE_RENO);