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, nullptr, a);
101 lmm_constraint_t L2 = lmm_constraint_new(Sys, nullptr, b);
102 lmm_constraint_t L3 = lmm_constraint_new(Sys, nullptr, a);
104 lmm_variable_t R_1_2_3 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 3);
105 lmm_variable_t R_1 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
106 lmm_variable_t R_2 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
107 lmm_variable_t R_3 = lmm_variable_new(Sys, nullptr, 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, nullptr, 200.0);
189 lmm_constraint_t CPU2 = lmm_constraint_new(Sys, nullptr, 100.0);
191 lmm_variable_t T1 = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 1);
192 lmm_variable_t T2 = lmm_variable_new(Sys, nullptr, 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)
221 double **A = xbt_new0(double *, links + 5);
222 /* array to add the constraints of fictitious variables */
223 double B[15] = { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 1 };
225 for (int i = 0; i < links + 5; i++) {
226 A[i] = xbt_new0(double, flows + 5);
227 for (int j = 0; j < flows + 5; j++) {
230 if (i >= links || j >= flows) {
236 /*matrix that store the constraints/topology */
237 A[0][1] = A[0][7] = 1.0;
238 A[1][1] = A[1][7] = A[1][8] = 1.0;
239 A[2][1] = A[2][8] = 1.0;
241 A[4][0] = A[4][3] = A[4][9] = 1.0;
242 A[5][0] = A[5][3] = A[5][4] = A[5][9] = 1.0;
243 A[6][0] = A[6][4] = A[6][9] = A[6][10] = 1.0;
244 A[7][2] = A[7][4] = A[7][6] = A[7][9] = A[7][10] = 1.0;
245 A[8][2] = A[8][10] = 1.0;
246 A[9][5] = A[9][6] = A[9][9] = 1.0;
253 if (method == LAGRANGE_VEGAS)
254 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
255 if (method == LAGRANGE_RENO)
256 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
258 lmm_system_t Sys = lmm_system_new(1);
260 /* Creates the constraints */
261 lmm_constraint_t *tmp_cnst = xbt_new0(lmm_constraint_t, 15);
262 for (int i = 0; i < 15; i++)
263 tmp_cnst[i] = lmm_constraint_new(Sys, nullptr, B[i]);
265 /* Creates the variables */
266 lmm_variable_t *tmp_var = xbt_new0(lmm_variable_t, 16);
267 for (int j = 0; j < 16; j++) {
268 tmp_var[j] = lmm_variable_new(Sys, nullptr, 1.0, -1.0, 15);
269 lmm_update_variable_weight(Sys, tmp_var[j], 1.0);
272 /* Link constraints and variables */
273 for (int i = 0; i < 15; i++)
274 for (int j = 0; j < 16; j++)
276 lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0);
278 if (method == MAXMIN) {
280 } else if (method == LAGRANGE_VEGAS) {
282 } else if (method == LAGRANGE_RENO) {
285 xbt_die("Invalid method");
288 for (int j = 0; j < 16; j++)
289 PRINT_VAR(tmp_var[j]);
291 for (int j = 0; j < 16; j++)
292 lmm_variable_free(Sys, tmp_var[j]);
295 lmm_system_free(Sys);
296 for (int i = 0; i < links + 5; i++)
301 int main(int argc, char **argv)
303 surf_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);