2 /* Copyright (c) 2007 Arnaud Legrand, Pedro Velho. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 * Modelling the proportional fairness using the Lagrange Optimization
7 * Approach. For a detailed description see:
8 * "ssh://username@scm.gforge.inria.fr/svn/memo/people/pvelho/lagrange/ppf.ps".
11 #include "xbt/sysdep.h"
12 #include "xbt/mallocator.h"
13 #include "maxmin_private.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_lagrange, surf,
21 "Logging specific to SURF (lagrange)");
22 XBT_LOG_NEW_SUBCATEGORY(surf_lagrange_dichotomy, surf_lagrange,
23 "Logging specific to SURF (lagrange dichotomy)");
25 #define SHOW_EXPR(expr) CDEBUG1(surf_lagrange,#expr " = %g",expr);
27 double (* func_f_def ) (lmm_variable_t , double);
28 double (* func_fp_def ) (lmm_variable_t , double);
29 double (* func_fpi_def )(lmm_variable_t , double);
32 * Local prototypes to implement the lagrangian optimization with optimal step, also called dichotomy.
34 //solves the proportional fairness using a lagrange optimizition with dichotomy step
35 void lagrange_solve(lmm_system_t sys);
36 //computes the value of the dichotomy using a initial values, init, with a specific variable or constraint
37 static double dichotomy(double init, double diff(double, void *), void *var_cnst,
39 //computes the value of the differential of variable param_var applied to mu
40 static double partial_diff_mu(double mu, void *param_var);
41 //computes the value of the differential of constraint param_cnst applied to lambda
42 static double partial_diff_lambda(double lambda, void *param_cnst);
44 static int __check_feasible(xbt_swag_t cnst_list, xbt_swag_t var_list, int warn)
46 xbt_swag_t elem_list = NULL;
47 lmm_element_t elem = NULL;
48 lmm_constraint_t cnst = NULL;
49 lmm_variable_t var = NULL;
53 xbt_swag_foreach(cnst, cnst_list) {
55 elem_list = &(cnst->element_set);
56 xbt_swag_foreach(elem, elem_list) {
63 if (double_positive(tmp - cnst->bound)) {
66 ("The link (%p) is over-used. Expected less than %f and got %f",
67 cnst, cnst->bound, tmp);
70 DEBUG3("Checking feasability for constraint (%p): sat = %f, lambda = %f ",
71 cnst, tmp - cnst->bound, cnst->lambda);
74 xbt_swag_foreach(var, var_list) {
75 if (var->bound < 0 || var->weight <= 0)
77 DEBUG3("Checking feasability for variable (%p): sat = %f mu = %f", var,
78 var->value - var->bound, var->mu);
80 if (double_positive(var->value - var->bound)) {
83 ("The variable (%p) is too large. Expected less than %f and got %f",
84 var, var->bound, var->value);
91 static double new_value(lmm_variable_t var)
96 for (i = 0; i < var->cnsts_number; i++) {
97 tmp += (var->cnsts[i].constraint)->lambda;
101 DEBUG3("\t Working on var (%p). cost = %e; Df = %e", var, tmp,
103 //uses the partial differential inverse function
104 return var->func_fpi(var, tmp);
107 static double new_mu(lmm_variable_t var)
110 double sigma_i = 0.0;
113 for (j = 0; j < var->cnsts_number; j++) {
114 sigma_i += (var->cnsts[j].constraint)->lambda;
116 mu_i = var->func_fp(var,var->bound)-sigma_i;
117 if(mu_i<0.0) return 0.0;
121 static double dual_objective(xbt_swag_t var_list, xbt_swag_t cnst_list)
123 lmm_constraint_t cnst = NULL;
124 lmm_variable_t var = NULL;
128 xbt_swag_foreach(var, var_list) {
132 for (j = 0; j < var->cnsts_number; j++)
133 sigma_i += (var->cnsts[j].constraint)->lambda;
138 DEBUG2("var %p : sigma_i = %1.20f",var,sigma_i);
140 obj += var->func_f(var,var->func_fpi(var,sigma_i)) -
141 sigma_i*var->func_fpi(var,sigma_i);
144 obj += var->mu*var->bound;
147 xbt_swag_foreach(cnst, cnst_list)
148 obj += cnst->lambda*cnst->bound;
153 void lagrange_solve(lmm_system_t sys)
156 * Lagrange Variables.
158 int max_iterations = 100;
159 double epsilon_min_error = MAXMIN_PRECISION;
160 double dichotomy_min_error = 1e-14;
161 double overall_modification = 1;
164 * Variables to manipulate the data structure proposed to model the maxmin
165 * fairness. See docummentation for more details.
167 xbt_swag_t cnst_list = NULL;
168 lmm_constraint_t cnst = NULL;
170 xbt_swag_t var_list = NULL;
171 lmm_variable_t var = NULL;
174 * Auxiliar variables.
181 DEBUG0("Iterative method configuration snapshot =====>");
182 DEBUG1("#### Maximum number of iterations : %d", max_iterations);
183 DEBUG1("#### Minimum error tolerated : %e",
185 DEBUG1("#### Minimum error tolerated (dichotomy) : %e",
186 dichotomy_min_error);
188 if (!(sys->modified))
195 cnst_list = &(sys->active_constraint_set);
196 xbt_swag_foreach(cnst, cnst_list) {
198 cnst->new_lambda = 2.0;
199 DEBUG2("#### cnst(%p)->lambda : %e", cnst, cnst->lambda);
203 * Initialize the var list variable with only the active variables.
204 * Associate an index in the swag variables. Initialize mu.
206 var_list = &(sys->variable_set);
208 xbt_swag_foreach(var, var_list) {
209 if ((var->bound < 0.0) || (var->weight <= 0.0)) {
210 DEBUG1("#### NOTE var(%d) is a boundless (or inactive) variable", i);
213 var->value = new_value(var);
219 var->value = new_value(var);
221 DEBUG3("#### var(%d) %p ->mu : %e", i, var, var->mu);
222 DEBUG3("#### var(%d) %p ->weight: %e", i, var, var->weight);
223 DEBUG3("#### var(%d) %p ->bound: %e", i, var, var->bound);
228 * Compute dual objective.
230 obj = dual_objective(var_list,cnst_list);
233 * While doesn't reach a minimun error or a number maximum of iterations.
235 while (overall_modification > epsilon_min_error && iteration < max_iterations) {
239 DEBUG1("************** ITERATION %d **************", iteration);
240 DEBUG0("-------------- Gradient Descent ----------");
243 * Improve the value of mu_i
245 xbt_swag_foreach(var, var_list) {
246 if ((var->bound >= 0) && (var->weight > 0)) {
247 DEBUG1("Working on var (%p)", var);
248 var->new_mu = new_mu(var);
249 dual_updated += (fabs(var->new_mu-var->mu)>dichotomy_min_error);
250 DEBUG2("dual_updated (%d) : %1.20f",dual_updated,fabs(var->new_mu-var->mu));
251 DEBUG3("Updating mu : var->mu (%p) : %1.20f -> %1.20f", var, var->mu, var->new_mu);
252 var->mu = var->new_mu;
254 new_obj=dual_objective(var_list,cnst_list);
255 DEBUG3("Improvement for Objective (%g -> %g) : %g", obj, new_obj,
257 xbt_assert1(obj-new_obj>=-epsilon_min_error,"Our gradient sucks! (%1.20f)",obj-new_obj);
263 * Improve the value of lambda_i
265 xbt_swag_foreach(cnst, cnst_list) {
266 DEBUG1("Working on cnst (%p)", cnst);
268 dichotomy(cnst->lambda, partial_diff_lambda, cnst,
269 dichotomy_min_error);
270 dual_updated += (fabs(cnst->new_lambda-cnst->lambda)>dichotomy_min_error);
271 DEBUG2("dual_updated (%d) : %1.20f",dual_updated,fabs(cnst->new_lambda-cnst->lambda));
272 DEBUG3("Updating lambda : cnst->lambda (%p) : %1.20f -> %1.20f", cnst, cnst->lambda, cnst->new_lambda);
273 cnst->lambda = cnst->new_lambda;
275 new_obj=dual_objective(var_list,cnst_list);
276 DEBUG3("Improvement for Objective (%g -> %g) : %g", obj, new_obj,
278 xbt_assert1(obj-new_obj>=-epsilon_min_error,"Our gradient sucks! (%1.20f)",obj-new_obj);
283 * Now computes the values of each variable (\rho) based on
284 * the values of \lambda and \mu.
286 DEBUG0("-------------- Check convergence ----------");
287 overall_modification = 0;
288 xbt_swag_foreach(var, var_list) {
289 if (var->weight <= 0)
292 tmp = new_value(var);
294 if (overall_modification < (fabs(var->value - tmp)/tmp)) {
295 overall_modification = (fabs(var->value - tmp)/tmp);
299 DEBUG3("New value of var (%p) = %e, overall_modification = %e", var,
300 var->value, overall_modification);
304 DEBUG0("-------------- Check feasability ----------");
305 if (!__check_feasible(cnst_list, var_list, 0))
306 overall_modification = 1.0;
307 DEBUG2("Iteration %d: overall_modification : %f", iteration, overall_modification);
309 WARN1("Could not improve the convergence at iteration %d. Drop it!",iteration);
314 * Compute dual objective.
319 __check_feasible(cnst_list, var_list, 1);
321 if (overall_modification <= epsilon_min_error) {
322 DEBUG1("The method converges in %d iterations.", iteration);
324 if (iteration >= max_iterations) {
326 ("Method reach %d iterations, which is the maximum number of iterations allowed.",
329 /* INFO1("Method converged after %d iterations", iteration); */
331 if (XBT_LOG_ISENABLED(surf_lagrange, xbt_log_priority_debug)) {
337 * Returns a double value corresponding to the result of a dichotomy proccess with
338 * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
339 * case of a constraint) and a initial value init.
341 * @param init initial value for \mu or \lambda
342 * @param diff a function that computes the differential of with respect a \mu or \lambda
343 * @param var_cnst a pointer to a variable or constraint
344 * @param min_erro a minimun error tolerated
346 * @return a double correponding to the result of the dichotomyal process
348 static double dichotomy(double init, double diff(double, void *), void *var_cnst,
352 double overall_error;
354 double min_diff, max_diff, middle_diff;
364 min_diff = max_diff = middle_diff = 0.0;
367 if ((diff_0 = diff(1e-16, var_cnst)) >= 0) {
368 CDEBUG1(surf_lagrange_dichotomy, "returning 0.0 (diff = %e)",
374 min_diff = diff(min, var_cnst);
375 max_diff = diff(max, var_cnst);
377 while (overall_error > min_error) {
378 CDEBUG4(surf_lagrange_dichotomy,
379 "[min, max] = [%1.20f, %1.20f] || diffmin, diffmax = %1.20f, %1.20f", min, max,
382 if (min_diff > 0 && max_diff > 0) {
384 CDEBUG0(surf_lagrange_dichotomy, "Decreasing min");
386 min_diff = diff(min, var_cnst);
388 CDEBUG0(surf_lagrange_dichotomy, "Decreasing max");
392 } else if (min_diff < 0 && max_diff < 0) {
394 CDEBUG0(surf_lagrange_dichotomy, "Increasing max");
396 max_diff = diff(max, var_cnst);
398 CDEBUG0(surf_lagrange_dichotomy, "Increasing min");
402 } else if (min_diff < 0 && max_diff > 0) {
403 middle = (max + min) / 2.0;
404 CDEBUG1(surf_lagrange_dichotomy, "Trying (max+min)/2 : %1.20f",middle);
406 if((min==middle) || (max==middle)) {
407 CWARN4(surf_lagrange_dichotomy,"Cannot improve the convergence! min=max=middle=%1.20f, diff = %1.20f."
408 " Reaching the 'double' limits. Maybe scaling your function would help ([%1.20f,%1.20f]).",
409 min, max-min, min_diff,max_diff);
412 middle_diff = diff(middle, var_cnst);
414 if (middle_diff < 0) {
415 CDEBUG0(surf_lagrange_dichotomy, "Increasing min");
417 overall_error = max_diff-middle_diff;
418 min_diff = middle_diff;
419 /* SHOW_EXPR(overall_error); */
420 } else if (middle_diff > 0) {
421 CDEBUG0(surf_lagrange_dichotomy, "Decreasing max");
423 overall_error = max_diff-middle_diff;
424 max_diff = middle_diff;
425 /* SHOW_EXPR(overall_error); */
428 /* SHOW_EXPR(overall_error); */
430 } else if (min_diff == 0) {
433 /* SHOW_EXPR(overall_error); */
434 } else if (max_diff == 0) {
437 /* SHOW_EXPR(overall_error); */
438 } else if (min_diff > 0 && max_diff < 0) {
439 CWARN0(surf_lagrange_dichotomy,
440 "The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
443 CWARN2(surf_lagrange_dichotomy,
444 "diffmin (%1.20f) or diffmax (%1.20f) are something I don't know, taking no action.",
450 CDEBUG1(surf_lagrange_dichotomy, "returning %e", (min + max) / 2.0);
452 return ((min + max) / 2.0);
455 static double partial_diff_lambda(double lambda, void *param_cnst)
459 xbt_swag_t elem_list = NULL;
460 lmm_element_t elem = NULL;
461 lmm_variable_t var = NULL;
462 lmm_constraint_t cnst = (lmm_constraint_t) param_cnst;
464 double sigma_i = 0.0;
467 elem_list = &(cnst->element_set);
469 CDEBUG1(surf_lagrange_dichotomy,"Computing diff of cnst (%p)", cnst);
471 xbt_swag_foreach(elem, elem_list) {
472 var = elem->variable;
473 if (var->weight <= 0)
476 CDEBUG1(surf_lagrange_dichotomy,"Computing sigma_i for var (%p)", var);
477 // Initialize the summation variable
481 for (j = 0; j < var->cnsts_number; j++) {
482 sigma_i += (var->cnsts[j].constraint)->lambda;
485 //add mu_i if this flow has a RTT constraint associated
489 //replace value of cnst->lambda by the value of parameter lambda
490 sigma_i = (sigma_i - cnst->lambda) + lambda;
492 diff += -var->func_fpi(var, sigma_i);
498 CDEBUG3(surf_lagrange_dichotomy,"d D/d lambda for cnst (%p) at %1.20f = %1.20f",
504 /** \brief Attribute the value bound to var->bound.
506 * \param func_fpi inverse of the partial differential of f (f prime inverse, (f')^{-1})
508 * Set default functions to the ones passed as parameters. This is a polimorfism in C pure, enjoy the roots of programming.
511 void lmm_set_default_protocol_function(double (* func_f) (lmm_variable_t var, double x),
512 double (* func_fp) (lmm_variable_t var, double x),
513 double (* func_fpi) (lmm_variable_t var, double x))
516 func_fp_def = func_fp;
517 func_fpi_def = func_fpi;
521 /**************** Vegas and Reno functions *************************/
523 * NOTE for Reno: all functions consider the network
524 * coeficient (alpha) equal to 1.
528 * For Vegas: $f(x) = \alpha D_f\ln(x)$
529 * Therefore: $fp(x) = \frac{\alpha D_f}{x}$
530 * Therefore: $fpi(x) = \frac{\alpha D_f}{x}$
532 #define VEGAS_SCALING 1000.0
534 double func_vegas_f(lmm_variable_t var, double x){
535 xbt_assert0(x>0.0,"Don't call me with stupid values!");
536 return VEGAS_SCALING*var->df*log(x);
539 double func_vegas_fp(lmm_variable_t var, double x){
540 xbt_assert0(x>0.0,"Don't call me with stupid values!");
541 return VEGAS_SCALING*var->df/x;
544 double func_vegas_fpi(lmm_variable_t var, double x){
545 xbt_assert0(x>0.0,"Don't call me with stupid values!");
546 return var->df/(x/VEGAS_SCALING);
550 * For Reno: $f(x) = \frac{\sqrt{3/2}}{D_f} atan(\sqrt{3/2}D_f x)$
551 * Therefore: $fp(x) = \frac{3}{3 D_f^2 x^2+2}$
552 * Therefore: $fpi(x) = \sqrt{\frac{1}{{D_f}^2 x} - \frac{2}{3{D_f}^2}}$
554 #define RENO_SCALING 1.0
555 double func_reno_f(lmm_variable_t var, double x){
556 xbt_assert0(var->df>0.0,"Don't call me with stupid values!");
558 return RENO_SCALING*sqrt(3.0/2.0)/var->df*atan(sqrt(3.0/2.0)*var->df*x);
561 double func_reno_fp(lmm_variable_t var, double x){
562 return RENO_SCALING*3.0/(3.0*var->df*var->df*x*x +2.0);
565 double func_reno_fpi(lmm_variable_t var, double x){
568 xbt_assert0(var->df>0.0,"Don't call me with stupid values!");
569 xbt_assert0(x>0.0,"Don't call me with stupid values!");
571 res_fpi = 1.0/(var->df*var->df*(x/RENO_SCALING)) - 2.0/(3.0*var->df*var->df);
572 if(res_fpi<=0.0) return 0.0;
573 /* xbt_assert0(res_fpi>0.0,"Don't call me with stupid values!"); */
574 return sqrt(res_fpi);