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"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_lagrange, surf, "Logging specific to SURF (lagrange)");
24 * Local prototypes to implement the lagrangian optimization with optimal step, also called dicotomi.
26 //solves the proportional fairness using a lagrange optimizition with dicotomi step
27 void lagrange_solve (lmm_system_t sys);
28 //computes the value of the dicotomi using a initial values, init, with a specific variable or constraint
29 double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error);
30 //computes the value of the differential of variable param_var applied to mu
31 double partial_diff_mu (double mu, void * param_var);
32 //computes the value of the differential of constraint param_cnst applied to lambda
33 double partial_diff_lambda (double lambda, void * param_cnst);
34 //auxiliar function to compute the partial_diff
35 double diff_aux(lmm_variable_t var, double x);
38 void lagrange_solve(lmm_system_t sys)
43 int max_iterations= 10000;
44 double epsilon_min_error = 1e-4;
45 double dicotomi_min_error = 1e-8;
46 double overall_error = 1;
49 * Variables to manipulate the data structure proposed to model the maxmin
50 * fairness. See docummentation for more details.
52 xbt_swag_t elem_list = NULL;
53 lmm_element_t elem = NULL;
55 xbt_swag_t cnst_list = NULL;
56 lmm_constraint_t cnst = NULL;
58 xbt_swag_t var_list = NULL;
59 lmm_variable_t var = NULL;
69 DEBUG0("Iterative method configuration snapshot =====>");
70 DEBUG1("#### Maximum number of iterations : %d", max_iterations);
71 DEBUG1("#### Minimum error tolerated : %e", epsilon_min_error);
72 DEBUG1("#### Minimum error tolerated (dicotomi) : %e", dicotomi_min_error);
74 if ( !(sys->modified))
78 * Initialize the var list variable with only the active variables.
79 * Associate an index in the swag variables. Initialize mu.
81 var_list = &(sys->variable_set);
83 xbt_swag_foreach(var, var_list) {
84 if((var->bound > 0.0) || (var->weight <= 0.0)){
85 DEBUG1("#### NOTE var(%d) is a boundless variable", i);
91 DEBUG2("#### var(%d)->mu : %e", i, var->mu);
92 DEBUG2("#### var(%d)->weight: %e", i, var->weight);
99 cnst_list=&(sys->active_constraint_set);
100 xbt_swag_foreach(cnst, cnst_list){
102 cnst->new_lambda = 2.0;
103 DEBUG2("#### cnst(%p)->lambda : %e", cnst, cnst->lambda);
107 * While doesn't reach a minimun error or a number maximum of iterations.
109 while(overall_error > epsilon_min_error && iteration < max_iterations){
112 DEBUG1("************** ITERATION %d **************", iteration);
115 * Compute the value of mu_i
117 //forall mu_i in mu_1, mu_2, ..., mu_n
118 xbt_swag_foreach(var, var_list) {
119 if((var->bound >= 0) && (var->weight > 0) ){
120 var->new_mu = dicotomi(var->mu, partial_diff_mu, var, dicotomi_min_error);
121 if(var->new_mu < 0) var->new_mu = 0;
122 var->mu = var->new_mu;
127 * Compute the value of lambda_i
129 //forall lambda_i in lambda_1, lambda_2, ..., lambda_n
130 xbt_swag_foreach(cnst, cnst_list) {
131 cnst->new_lambda = dicotomi(cnst->lambda, partial_diff_lambda, cnst, dicotomi_min_error);
132 DEBUG2("====> cnst->lambda (%p) = %e", cnst, cnst->new_lambda);
133 cnst->lambda = cnst->new_lambda;
137 * Now computes the values of each variable (\rho) based on
138 * the values of \lambda and \mu.
141 xbt_swag_foreach(var, var_list) {
145 //compute sigma_i + mu_i
147 for(i=0; i<var->cnsts_number; i++){
148 tmp += (var->cnsts[i].constraint)->lambda;
153 //uses the partial differential inverse function
154 tmp = var->func_fpi(var, tmp);
156 //computes de overall_error
157 if(overall_error < fabs(var->value - tmp)){
158 overall_error = fabs(var->value - tmp);
163 DEBUG4("======> value of var %s (%p) = %e, overall_error = %e", (char *)var->id, var, var->value, overall_error);
168 //verify the KKT property for each link
169 xbt_swag_foreach(cnst, cnst_list){
171 elem_list = &(cnst->element_set);
172 xbt_swag_foreach(elem, elem_list) {
173 var = elem->variable;
174 if(var->weight<=0) continue;
178 tmp = tmp - cnst->bound;
180 if(tmp > epsilon_min_error){
181 WARN4("The link %s(%p) doesn't match the KKT property, expected less than %e and got %e", (char *)cnst->id, cnst, epsilon_min_error, tmp);
186 //verify the KKT property of each flow
187 xbt_swag_foreach(var, var_list){
188 if(var->bound <= 0 || var->weight <= 0) continue;
190 tmp = (var->value - var->bound);
193 if(tmp != 0 || var->mu != 0){
194 WARN4("The flow %s(%p) doesn't match the KKT property, value expected (=0) got (lambda=%e) (sum_rho=%e)", (char *)var->id, var, var->mu, tmp);
199 if(overall_error <= epsilon_min_error){
200 DEBUG1("The method converge in %d iterations.", iteration);
202 WARN1("Method reach %d iterations, which is the maxmimun number of iterations allowed.", iteration);
207 * Returns a double value corresponding to the result of a dicotomi proccess with
208 * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
209 * case of a constraint) and a initial value init.
211 * @param init initial value for \mu or \lambda
212 * @param diff a function that computes the differential of with respect a \mu or \lambda
213 * @param var_cnst a pointer to a variable or constraint
214 * @param min_erro a minimun error tolerated
216 * @return a double correponding to the result of the dicotomial process
218 double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error){
220 double overall_error;
222 double min_diff, max_diff, middle_diff;
230 min_diff = max_diff = middle_diff = 0.0;
233 if(diff(0.0, var_cnst) > 0){
234 DEBUG1("====> returning 0.0 (diff = %e)", diff(0.0, var_cnst));
238 DEBUG0("====> not detected positive diff in 0");
240 while(overall_error > min_error){
242 min_diff = diff(min, var_cnst);
243 max_diff = diff(max, var_cnst);
245 DEBUG2("DICOTOMI ===> min = %e , max = %e", min, max);
246 DEBUG2("DICOTOMI ===> diffmin = %e , diffmax = %e", min_diff, max_diff);
248 if( min_diff > 0 && max_diff > 0 ){
254 }else if( min_diff < 0 && max_diff < 0 ){
260 }else if( min_diff < 0 && max_diff > 0 ){
261 middle = (max + min)/2.0;
262 middle_diff = diff(middle, var_cnst);
263 overall_error = fabs(min - max);
265 if( middle_diff < 0 ){
267 }else if( middle_diff > 0 ){
270 WARN0("Found an optimal solution with 0 error!");
275 }else if(min_diff == 0){
277 }else if(max_diff == 0){
279 }else if(min_diff > 0 && max_diff < 0){
280 WARN0("The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
285 DEBUG1("====> returning %e", (min+max)/2.0);
286 return ((min+max)/2.0);
292 double partial_diff_mu(double mu, void *param_var){
293 double mu_partial=0.0;
295 lmm_variable_t var = (lmm_variable_t)param_var;
299 for(i=0; i<var->cnsts_number; i++)
300 sigma_mu += (var->cnsts[i].constraint)->lambda;
302 //compute sigma_i + mu_i
305 //use auxiliar function passing (sigma_i + mu_i)
306 mu_partial = diff_aux(var, sigma_mu) ;
309 mu_partial += var->bound;
317 double partial_diff_lambda(double lambda, void *param_cnst){
320 xbt_swag_t elem_list = NULL;
321 lmm_element_t elem = NULL;
322 lmm_variable_t var = NULL;
323 lmm_constraint_t cnst= (lmm_constraint_t) param_cnst;
324 double lambda_partial=0.0;
327 elem_list = &(cnst->element_set);
329 DEBUG2("Computting diff of cnst (%p) %s", cnst, (char *)cnst->id);
331 xbt_swag_foreach(elem, elem_list) {
332 var = elem->variable;
333 if(var->weight<=0) continue;
335 //initilize de sumation variable
338 //compute sigma_i of variable var
339 for(i=0; i<var->cnsts_number; i++){
340 sigma_mu += (var->cnsts[i].constraint)->lambda;
343 //add mu_i if this flow has a RTT constraint associated
344 if(var->bound > 0) sigma_mu += var->mu;
346 //replace value of cnst->lambda by the value of parameter lambda
347 sigma_mu = (sigma_mu - cnst->lambda) + lambda;
349 //use the auxiliar function passing (\sigma_i + \mu_i)
350 lambda_partial += diff_aux(var, sigma_mu);
353 lambda_partial += cnst->bound;
355 return lambda_partial;
359 double diff_aux(lmm_variable_t var, double x){
360 double tmp_fp, tmp_fpi, tmp_fpip, result;
362 xbt_assert0(var->func_fp, "Initialize the protocol functions first create variables before.");
364 tmp_fp = var->func_fp(var, x);
365 tmp_fpi = var->func_fpi(var, x);
366 tmp_fpip = var->func_fpip(var, x);
368 result = tmp_fpip*(var->func_fp(var, tmp_fpi));
370 result = result - tmp_fpi;
372 result = result - (tmp_fpip * x);