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== */
26 enum method_t { MAXMIN, LAGRANGE_RENO, LAGRANGE_VEGAS };
28 static double dichotomy(double func(double), double min, double max, double min_error)
30 double overall_error = 2 * min_error;
32 double min_func = func(min);
33 double max_func = func(max);
35 if ((min_func > 0 && max_func > 0))
37 if ((min_func < 0 && max_func < 0))
39 if ((min_func > 0 && max_func < 0))
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);
55 double middle = (max + min) / 2.0;
56 if (fabs(min - middle) < 1e-12 || fabs(max - middle) < 1e-12) {
59 double middle_func = func(middle);
61 SHOW_EXPR(middle_func);
63 if (middle_func < 0) {
65 min_func = middle_func;
66 overall_error = max_func - middle_func;
67 } else if (middle_func > 0) {
69 max_func = middle_func;
70 overall_error = middle_func - min_func;
75 return ((min + max) / 2.0);
80 static double diff_lagrange_test_1(double x)
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)));
86 static void test1(method_t method)
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);
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);
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);
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);
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);
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);
119 if (method == MAXMIN) {
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 */
132 } else if (method == LAGRANGE_RENO) {
135 x = dichotomy(diff_lagrange_test_1, 0, a, 1e-13);
142 xbt_die( "Invalid method");
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)));
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));
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);
177 static void test2(method_t method)
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);
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);
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);
191 lmm_update_variable_weight(Sys, T1, 1.0);
192 lmm_update_variable_weight(Sys, T2, 1.0);
194 lmm_expand(Sys, CPU1, T1, 1.0);
195 lmm_expand(Sys, CPU2, T2, 1.0);
197 if (method == MAXMIN) {
199 } else if (method == LAGRANGE_VEGAS || method == LAGRANGE_RENO) {
202 xbt_die("Invalid method");
208 lmm_variable_free(Sys, T1);
209 lmm_variable_free(Sys, T2);
210 lmm_system_free(Sys);
213 static void test3(method_t method)
218 double **A = xbt_new0(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 };
222 for (int i = 0; i < links + 5; i++) {
223 A[i] = xbt_new0(double, flows + 5);
224 for (int j = 0; j < flows + 5; j++) {
227 if (i >= links || j >= flows) {
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;
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;
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);
255 lmm_system_t Sys = lmm_system_new(1);
257 /* Creates the constraints */
258 lmm_constraint_t *tmp_cnst = xbt_new0(lmm_constraint_t, 15);
259 for (int i = 0; i < 15; i++)
260 tmp_cnst[i] = lmm_constraint_new(Sys, nullptr, B[i]);
262 /* Creates the variables */
263 lmm_variable_t *tmp_var = xbt_new0(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);
269 /* Link constraints and variables */
270 for (int i = 0; i < 15; i++)
271 for (int j = 0; j < 16; j++)
273 lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0);
275 if (method == MAXMIN) {
277 } else if (method == LAGRANGE_VEGAS) {
279 } else if (method == LAGRANGE_RENO) {
282 xbt_die("Invalid method");
285 for (int j = 0; j < 16; j++)
286 PRINT_VAR(tmp_var[j]);
288 for (int j = 0; j < 16; j++)
289 lmm_variable_free(Sys, tmp_var[j]);
292 lmm_system_free(Sys);
293 for (int i = 0; i < links + 5; i++)
298 int main(int argc, char** argv)
300 MSG_init(&argc, argv);
301 XBT_INFO("***** Test 1 (Max-Min)");
303 XBT_INFO("***** Test 1 (Lagrange - Vegas)");
304 test1(LAGRANGE_VEGAS);
305 XBT_INFO("***** Test 1 (Lagrange - Reno)");
306 test1(LAGRANGE_RENO);
308 XBT_INFO("***** Test 2 (Max-Min)");
310 XBT_INFO("***** Test 2 (Lagrange - Vegas)");
311 test2(LAGRANGE_VEGAS);
312 XBT_INFO("***** Test 2 (Lagrange - Reno)");
313 test2(LAGRANGE_RENO);
315 XBT_INFO("***** Test 3 (Max-Min)");
317 XBT_INFO("***** Test 3 (Lagrange - Vegas)");
318 test3(LAGRANGE_VEGAS);
319 XBT_INFO("***** Test 3 (Lagrange - Reno)");
320 test3(LAGRANGE_RENO);