1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * Modelling the proportional fairness using the Lagrange Optimization
9 * Approach. For a detailed description see:
10 * "ssh://username@scm.gforge.inria.fr/svn/memo/people/pvelho/lagrange/ppf.ps".
13 #include "xbt/sysdep.h"
14 //#include "maxmin_private.h"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_lagrange, surf,
23 "Logging specific to SURF (lagrange)");
24 XBT_LOG_NEW_SUBCATEGORY(surf_lagrange_dichotomy, surf_lagrange,
25 "Logging specific to SURF (lagrange dichotomy)");
27 #define SHOW_EXPR(expr) XBT_CDEBUG(surf_lagrange,#expr " = %g",expr);
29 double (*func_f_def) (lmm_variable_t, double);
30 double (*func_fp_def) (lmm_variable_t, double);
31 double (*func_fpi_def) (lmm_variable_t, double);
34 * Local prototypes to implement the lagrangian optimization with optimal step, also called dichotomy.
36 //solves the proportional fairness using a lagrange optimizition with dichotomy step
37 void lagrange_solve(lmm_system_t sys);
38 //computes the value of the dichotomy using a initial values, init, with a specific variable or constraint
39 static double dichotomy(double init, double diff(double, void *),
40 void *var_cnst, double min_error);
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(std::vector<ConstraintPtr> *cnstList, std::vector<VariablePtr> *varList,
47 std::vector<ElementPtr> *elemList = NULL;
48 lmm_element_t elem = NULL;
49 lmm_constraint_t cnst = NULL;
50 lmm_variable_t var = NULL;
51 std::vector<VariablePtr>::iterator varIt;
52 std::vector<ElementPtr>::iterator elemIt;
53 std::vector<ConstraintPtr>::iterator cnstIt;
57 for (cnstIt=cnstList->begin(); cnstIt!=cnstList->end(); ++cnstIt) {
60 elemList = &(cnst->m_elementSet);
61 for (elemIt=elemList->begin(); elemIt!=elemList->end(); ++elemIt) {
62 var = (*elemIt)->p_variable;
63 if (var->m_weight <= 0)
68 if (double_positive(tmp - cnst->m_bound)) {
71 ("The link (%p) is over-used. Expected less than %f and got %f",
72 cnst, cnst->m_bound, tmp);
76 ("Checking feasability for constraint (%p): sat = %f, lambda = %f ",
77 cnst, tmp - cnst->m_bound, cnst->m_lambda);
80 for (varIt=varList->begin(); varIt!=varList->end(); ++varIt) {
85 XBT_DEBUG("Checking feasability for variable (%p): sat = %f mu = %f", var,
86 var->m_value - var->m_bound, var->m_mu);
88 if (double_positive(var->m_value - var->m_bound)) {
91 ("The variable (%p) is too large. Expected less than %f and got %f",
92 var, var->m_bound, var->m_value);
99 static double new_value(lmm_variable_t var)
103 std::vector<ElementPtr>::iterator elemIt;
105 for (elemIt=var->m_cnsts.begin(); elemIt!=var->m_cnsts.end(); ++elemIt) {
106 tmp += ((*elemIt)->p_constraint)->m_lambda;
108 if (var->m_bound > 0)
110 XBT_DEBUG("\t Working on var (%p). cost = %e; Weight = %e", var, tmp,
112 //uses the partial differential inverse function
113 return var->p_funcFPI(var, tmp);
116 static double new_mu(lmm_variable_t var)
119 double sigma_i = 0.0;
121 std::vector<ElementPtr>::iterator elemIt;
123 for (elemIt=var->m_cnsts.begin(); elemIt!=var->m_cnsts.end(); ++elemIt) {
124 sigma_i += ((*elemIt)->p_constraint)->m_lambda;
126 mu_i = var->p_funcFP(var, var->m_bound) - sigma_i;
132 static double dual_objective(std::vector<VariablePtr> *varList, std::vector<ConstraintPtr> *cnstList)
134 lmm_constraint_t cnst = NULL;
135 lmm_variable_t var = NULL;
138 std::vector<VariablePtr>::iterator varIt;
139 std::vector<ElementPtr>::iterator elemIt;
140 std::vector<ConstraintPtr>::iterator cnstIt;
142 for (varIt=varList->begin(); varIt!=varList->end(); ++varIt) {
144 double sigma_i = 0.0;
150 for (elemIt=var->m_cnsts.begin(); elemIt!=var->m_cnsts.end(); ++elemIt)
151 sigma_i += ((*elemIt)->p_constraint)->m_lambda;
153 if (var->m_bound > 0)
154 sigma_i += var->m_mu;
156 XBT_DEBUG("var %p : sigma_i = %1.20f", var, sigma_i);
158 obj += var->p_funcF(var, var->p_funcFPI(var, sigma_i)) -
159 sigma_i * var->p_funcFPI(var, sigma_i);
161 if (var->m_bound > 0)
162 obj += var->m_mu * var->m_bound;
165 for (cnstIt=cnstList->begin(); cnstIt!=cnstList->end(); ++cnstIt)
166 obj += (*cnstIt)->m_lambda * (*cnstIt)->m_bound;
171 void lagrange_solve(lmm_system_t sys)
174 * Lagrange Variables.
176 int max_iterations = 100;
177 double epsilon_min_error = MAXMIN_PRECISION;
178 double dichotomy_min_error = 1e-14;
179 double overall_modification = 1;
182 * Variables to manipulate the data structure proposed to model the maxmin
183 * fairness. See docummentation for more details.
185 std::vector<ConstraintPtr> *cnstList = NULL;
186 std::vector<ConstraintPtr>::iterator cnstIt;
187 lmm_constraint_t cnst = NULL;
189 std::vector<VariablePtr> *varList = NULL;
190 std::vector<VariablePtr>::iterator varIt;
191 lmm_variable_t var = NULL;
193 std::vector<ElementPtr>::iterator elemIt;
196 * Auxiliar variables.
203 XBT_DEBUG("Iterative method configuration snapshot =====>");
204 XBT_DEBUG("#### Maximum number of iterations : %d", max_iterations);
205 XBT_DEBUG("#### Minimum error tolerated : %e",
207 XBT_DEBUG("#### Minimum error tolerated (dichotomy) : %e",
208 dichotomy_min_error);
210 if (XBT_LOG_ISENABLED(surf_lagrange, xbt_log_priority_debug)) {
214 if (!(sys->m_modified))
220 cnstList = &(sys->m_activeConstraintSet);
221 for (cnstIt=cnstList->begin(); cnstIt!=cnstList->end(); ++cnstIt) {
223 cnst->m_lambda = 1.0;
224 cnst->m_newLambda = 2.0;
225 XBT_DEBUG("#### cnst(%p)->lambda : %e", cnst, cnst->m_lambda);
229 * Initialize the var list variable with only the active variables.
230 * Associate an index in the swag variables. Initialize mu.
232 varList = &(sys->m_variableSet);
234 for (varIt=varList->begin(); varIt!=varList->end(); ++varIt) {
240 if (var->m_bound < 0.0) {
241 XBT_DEBUG("#### NOTE var(%d) is a boundless variable", i);
243 var->m_value = new_value(var);
247 var->m_value = new_value(var);
249 XBT_DEBUG("#### var(%p) ->weight : %e", var, var->m_weight);
250 XBT_DEBUG("#### var(%p) ->mu : %e", var, var->m_mu);
251 XBT_DEBUG("#### var(%p) ->weight: %e", var, var->m_weight);
252 XBT_DEBUG("#### var(%p) ->bound: %e", var, var->m_bound);
253 for (elemIt=var->m_cnsts.begin(); elemIt!=var->m_cnsts.end(); ++elemIt) {
254 if ((*elemIt)->m_value == 0.0)
257 if (nb == var->m_cnsts.size())
263 * Compute dual objective.
265 obj = dual_objective(varList, cnstList);
268 * While doesn't reach a minimun error or a number maximum of iterations.
270 while (overall_modification > epsilon_min_error
271 && iteration < max_iterations) {
272 /* int dual_updated=0; */
275 XBT_DEBUG("************** ITERATION %d **************", iteration);
276 XBT_DEBUG("-------------- Gradient Descent ----------");
279 * Improve the value of mu_i
281 for (varIt=varList->begin(); varIt!=varList->end(); ++varIt) {
285 if (var->m_bound >= 0) {
286 XBT_DEBUG("Working on var (%p)", var);
287 var->m_newMu = new_mu(var);
288 /* dual_updated += (fabs(var->new_mu-var->mu)>dichotomy_min_error); */
289 /* XBT_DEBUG("dual_updated (%d) : %1.20f",dual_updated,fabs(var->new_mu-var->mu)); */
290 XBT_DEBUG("Updating mu : var->mu (%p) : %1.20f -> %1.20f", var,
291 var->m_mu, var->m_newMu);
292 var->m_mu = var->m_newMu;
294 new_obj = dual_objective(varList, cnstList);
295 XBT_DEBUG("Improvement for Objective (%g -> %g) : %g", obj, new_obj,
297 xbt_assert(obj - new_obj >= -epsilon_min_error,
298 "Our gradient sucks! (%1.20f)", obj - new_obj);
304 * Improve the value of lambda_i
306 for (cnstIt=cnstList->begin(); cnstIt!=cnstList->end(); ++cnstIt) {
308 XBT_DEBUG("Working on cnst (%p)", cnst);
310 dichotomy(cnst->m_lambda, partial_diff_lambda, cnst,
311 dichotomy_min_error);
312 /* dual_updated += (fabs(cnst->new_lambda-cnst->lambda)>dichotomy_min_error); */
313 /* XBT_DEBUG("dual_updated (%d) : %1.20f",dual_updated,fabs(cnst->new_lambda-cnst->lambda)); */
314 XBT_DEBUG("Updating lambda : cnst->lambda (%p) : %1.20f -> %1.20f",
315 cnst, cnst->m_lambda, cnst->m_newLambda);
316 cnst->m_lambda = cnst->m_newLambda;
318 new_obj = dual_objective(varList, cnstList);
319 XBT_DEBUG("Improvement for Objective (%g -> %g) : %g", obj, new_obj,
321 xbt_assert(obj - new_obj >= -epsilon_min_error,
322 "Our gradient sucks! (%1.20f)", obj - new_obj);
327 * Now computes the values of each variable (\rho) based on
328 * the values of \lambda and \mu.
330 XBT_DEBUG("-------------- Check convergence ----------");
331 overall_modification = 0;
332 for (varIt=varList->begin(); varIt!=varList->end(); ++varIt) {
334 if (var->m_weight <= 0)
337 tmp = new_value(var);
339 overall_modification =
340 MAX(overall_modification, fabs(var->m_value - tmp));
343 XBT_DEBUG("New value of var (%p) = %e, overall_modification = %e",
344 var, var->m_value, overall_modification);
348 XBT_DEBUG("-------------- Check feasability ----------");
349 if (!__check_feasible(cnstList, varList, 0))
350 overall_modification = 1.0;
351 XBT_DEBUG("Iteration %d: overall_modification : %f", iteration,
352 overall_modification);
353 /* if(!dual_updated) { */
354 /* XBT_WARN("Could not improve the convergence at iteration %d. Drop it!",iteration); */
358 __check_feasible(cnstList, varList, 1);
360 if (overall_modification <= epsilon_min_error) {
361 XBT_DEBUG("The method converges in %d iterations.", iteration);
363 if (iteration >= max_iterations) {
365 ("Method reach %d iterations, which is the maximum number of iterations allowed.",
368 /* XBT_INFO("Method converged after %d iterations", iteration); */
370 if (XBT_LOG_ISENABLED(surf_lagrange, xbt_log_priority_debug)) {
376 * Returns a double value corresponding to the result of a dichotomy proccess with
377 * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
378 * case of a constraint) and a initial value init.
380 * @param init initial value for \mu or \lambda
381 * @param diff a function that computes the differential of with respect a \mu or \lambda
382 * @param var_cnst a pointer to a variable or constraint
383 * @param min_erro a minimun error tolerated
385 * @return a double correponding to the result of the dichotomyal process
387 static double dichotomy(double init, double diff(double, void *),
388 void *var_cnst, double min_error)
392 double overall_error;
394 double min_diff, max_diff, middle_diff;
404 min_diff = max_diff = middle_diff = 0.0;
407 if ((diff_0 = diff(1e-16, var_cnst)) >= 0) {
408 XBT_CDEBUG(surf_lagrange_dichotomy, "returning 0.0 (diff = %e)", diff_0);
413 min_diff = diff(min, var_cnst);
414 max_diff = diff(max, var_cnst);
416 while (overall_error > min_error) {
417 XBT_CDEBUG(surf_lagrange_dichotomy,
418 "[min, max] = [%1.20f, %1.20f] || diffmin, diffmax = %1.20f, %1.20f",
419 min, max, min_diff, max_diff);
421 if (min_diff > 0 && max_diff > 0) {
423 XBT_CDEBUG(surf_lagrange_dichotomy, "Decreasing min");
425 min_diff = diff(min, var_cnst);
427 XBT_CDEBUG(surf_lagrange_dichotomy, "Decreasing max");
431 } else if (min_diff < 0 && max_diff < 0) {
433 XBT_CDEBUG(surf_lagrange_dichotomy, "Increasing max");
435 max_diff = diff(max, var_cnst);
437 XBT_CDEBUG(surf_lagrange_dichotomy, "Increasing min");
441 } else if (min_diff < 0 && max_diff > 0) {
442 middle = (max + min) / 2.0;
443 XBT_CDEBUG(surf_lagrange_dichotomy, "Trying (max+min)/2 : %1.20f",
446 if ((min == middle) || (max == middle)) {
447 XBT_CWARN(surf_lagrange_dichotomy,
448 "Cannot improve the convergence! min=max=middle=%1.20f, diff = %1.20f."
449 " Reaching the 'double' limits. Maybe scaling your function would help ([%1.20f,%1.20f]).",
450 min, max - min, min_diff, max_diff);
453 middle_diff = diff(middle, var_cnst);
455 if (middle_diff < 0) {
456 XBT_CDEBUG(surf_lagrange_dichotomy, "Increasing min");
458 overall_error = max_diff - middle_diff;
459 min_diff = middle_diff;
460 /* SHOW_EXPR(overall_error); */
461 } else if (middle_diff > 0) {
462 XBT_CDEBUG(surf_lagrange_dichotomy, "Decreasing max");
464 overall_error = max_diff - middle_diff;
465 max_diff = middle_diff;
466 /* SHOW_EXPR(overall_error); */
469 /* SHOW_EXPR(overall_error); */
471 } else if (min_diff == 0) {
474 /* SHOW_EXPR(overall_error); */
475 } else if (max_diff == 0) {
478 /* SHOW_EXPR(overall_error); */
479 } else if (min_diff > 0 && max_diff < 0) {
480 XBT_CWARN(surf_lagrange_dichotomy,
481 "The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
484 XBT_CWARN(surf_lagrange_dichotomy,
485 "diffmin (%1.20f) or diffmax (%1.20f) are something I don't know, taking no action.",
491 XBT_CDEBUG(surf_lagrange_dichotomy, "returning %e", (min + max) / 2.0);
493 return ((min + max) / 2.0);
497 static double partial_diff_lambda(double lambda, void *param_cnst)
501 xbt_swag_t elem_list = NULL;
502 lmm_element_t elem = NULL;
503 lmm_variable_t var = NULL;
504 lmm_constraint_t cnst = (lmm_constraint_t) param_cnst;
506 double sigma_i = 0.0;
509 elem_list = &(cnst->element_set);
511 XBT_CDEBUG(surf_lagrange_dichotomy, "Computing diff of cnst (%p)", cnst);
513 xbt_swag_foreach(elem, elem_list) {
514 var = elem->variable;
515 if (var->weight <= 0)
518 XBT_CDEBUG(surf_lagrange_dichotomy, "Computing sigma_i for var (%p)",
520 // Initialize the summation variable
524 for (j = 0; j < var->cnsts_number; j++) {
525 sigma_i += (var->cnsts[j].constraint)->lambda;
528 //add mu_i if this flow has a RTT constraint associated
532 //replace value of cnst->lambda by the value of parameter lambda
533 sigma_i = (sigma_i - cnst->lambda) + lambda;
535 diff += -var->func_fpi(var, sigma_i);
541 XBT_CDEBUG(surf_lagrange_dichotomy,
542 "d D/d lambda for cnst (%p) at %1.20f = %1.20f", cnst, lambda,
549 /** \brief Attribute the value bound to var->bound.
551 * \param func_fpi inverse of the partial differential of f (f prime inverse, (f')^{-1})
553 * Set default functions to the ones passed as parameters. This is a polimorfism in C pure, enjoy the roots of programming.
556 void lmm_set_default_protocol_function(double (*func_f) (lmm_variable_t var, double x),
557 double (*func_fp) (lmm_variable_t var, double x),
558 double (*func_fpi) (lmm_variable_t var, double x))
561 func_fp_def = func_fp;
562 func_fpi_def = func_fpi;
566 /**************** Vegas and Reno functions *************************/
568 * NOTE for Reno: all functions consider the network
569 * coeficient (alpha) equal to 1.
573 * For Vegas: $f(x) = \alpha D_f\ln(x)$
574 * Therefore: $fp(x) = \frac{\alpha D_f}{x}$
575 * Therefore: $fpi(x) = \frac{\alpha D_f}{x}$
577 #define VEGAS_SCALING 1000.0
579 double func_vegas_f(lmm_variable_t var, double x)
582 xbt_assert(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
583 return VEGAS_SCALING * var->weight * log(x);
587 double func_vegas_fp(lmm_variable_t var, double x)
590 xbt_assert(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
591 return VEGAS_SCALING * var->weight / x;
595 double func_vegas_fpi(lmm_variable_t var, double x)
598 xbt_assert(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
599 return var->weight / (x / VEGAS_SCALING);
604 * For Reno: $f(x) = \frac{\sqrt{3/2}}{D_f} atan(\sqrt{3/2}D_f x)$
605 * Therefore: $fp(x) = \frac{3}{3 D_f^2 x^2+2}$
606 * Therefore: $fpi(x) = \sqrt{\frac{1}{{D_f}^2 x} - \frac{2}{3{D_f}^2}}$
608 #define RENO_SCALING 1.0
609 double func_reno_f(lmm_variable_t var, double x)
611 xbt_assert(var->m_weight > 0.0, "Don't call me with stupid values!");
613 return RENO_SCALING * sqrt(3.0 / 2.0) / var->m_weight *
614 atan(sqrt(3.0 / 2.0) * var->m_weight * x);
617 double func_reno_fp(lmm_variable_t var, double x)
619 return RENO_SCALING * 3.0 / (3.0 * var->m_weight * var->m_weight * x * x +
623 double func_reno_fpi(lmm_variable_t var, double x)
627 xbt_assert(var->m_weight > 0.0, "Don't call me with stupid values!");
628 xbt_assert(x > 0.0, "Don't call me with stupid values!");
631 1.0 / (var->m_weight * var->m_weight * (x / RENO_SCALING)) -
632 2.0 / (3.0 * var->m_weight * var->m_weight);
635 // xbt_assert(res_fpi>0.0,"Don't call me with stupid values!");
636 return sqrt(res_fpi);
640 /* Implementing new Reno-2
641 * For Reno-2: $f(x) = U_f(x_f) = \frac{{2}{D_f}}*ln(2+x*D_f)$
642 * Therefore: $fp(x) = 2/(Weight*x + 2)
643 * Therefore: $fpi(x) = (2*Weight)/x - 4
645 #define RENO2_SCALING 1.0
646 double func_reno2_f(lmm_variable_t var, double x)
648 xbt_assert(var->m_weight > 0.0, "Don't call me with stupid values!");
649 return RENO2_SCALING * (1.0 / var->m_weight) * log((x * var->m_weight) /
650 (2.0 * x * var->m_weight +
654 double func_reno2_fp(lmm_variable_t var, double x)
656 return RENO2_SCALING * 3.0 / (var->m_weight * x *
657 (2.0 * var->m_weight * x + 3.0));
660 double func_reno2_fpi(lmm_variable_t var, double x)
665 xbt_assert(x > 0.0, "Don't call me with stupid values!");
666 tmp = x * var->m_weight * var->m_weight;
667 res_fpi = tmp * (9.0 * x + 24.0);
672 res_fpi = RENO2_SCALING * (-3.0 * tmp + sqrt(res_fpi)) / (4.0 * tmp);