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-6;
45 double dicotomi_min_error = 1e-6;
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 (or inactive) variable", i);
91 DEBUG3("#### var(%d) %p ->mu : %e", i, var, var->mu);
92 DEBUG3("#### var(%d) %p ->weight: %e", i, var, var->weight);
93 DEBUG3("#### var(%d) %p ->bound: %e", i, var, var->bound);
100 cnst_list=&(sys->active_constraint_set);
101 xbt_swag_foreach(cnst, cnst_list){
103 cnst->new_lambda = 2.0;
104 DEBUG2("#### cnst(%p)->lambda : %e", cnst, cnst->lambda);
108 * While doesn't reach a minimun error or a number maximum of iterations.
110 while(overall_error > epsilon_min_error && iteration < max_iterations){
113 DEBUG1("************** ITERATION %d **************", iteration);
116 * Compute the value of mu_i
118 //forall mu_i in mu_1, mu_2, ..., mu_n
119 xbt_swag_foreach(var, var_list) {
120 if((var->bound >= 0) && (var->weight > 0) ){
121 var->new_mu = dicotomi(var->mu, partial_diff_mu, var, dicotomi_min_error);
122 if(var->new_mu < 0) var->new_mu = 0;
123 DEBUG2("====> var->mu (%p) = %e", var, var->new_mu);
124 var->mu = var->new_mu;
129 * Compute the value of lambda_i
131 //forall lambda_i in lambda_1, lambda_2, ..., lambda_n
132 xbt_swag_foreach(cnst, cnst_list) {
133 cnst->new_lambda = dicotomi(cnst->lambda, partial_diff_lambda, cnst, dicotomi_min_error);
134 DEBUG2("====> cnst->lambda (%p) = %e", cnst, cnst->new_lambda);
135 cnst->lambda = cnst->new_lambda;
139 * Now computes the values of each variable (\rho) based on
140 * the values of \lambda and \mu.
143 xbt_swag_foreach(var, var_list) {
147 //compute sigma_i + mu_i
149 for(i=0; i<var->cnsts_number; i++){
150 tmp += (var->cnsts[i].constraint)->lambda;
154 DEBUG3("\t Working on var (%p). cost = %e; Df = %e", var, tmp, var->df);
156 //uses the partial differential inverse function
157 tmp = var->func_fpi(var, tmp);
159 //computes de overall_error using normalized value
160 if(overall_error < (fabs(var->value - tmp)/tmp) ){
161 overall_error = (fabs(var->value - tmp)/tmp);
166 DEBUG3("======> value of var (%p) = %e, overall_error = %e", var, var->value, overall_error);
171 //verify the KKT property for each link
172 xbt_swag_foreach(cnst, cnst_list){
174 elem_list = &(cnst->element_set);
175 xbt_swag_foreach(elem, elem_list) {
176 var = elem->variable;
177 if(var->weight<=0) continue;
181 if(tmp - cnst->bound > epsilon_min_error) {
182 WARN3("The link (%p) is over-used. Expected less than %e and got %e", cnst, cnst->bound, tmp);
184 if(!((fabs(tmp - cnst->bound)<epsilon_min_error && cnst->lambda>=epsilon_min_error) ||
185 (fabs(tmp - cnst->bound)>=epsilon_min_error && cnst->lambda<epsilon_min_error))) {
186 WARN1("The KKT condition is not verified for cnst %p...", cnst);
191 //verify the KKT property of each flow
192 xbt_swag_foreach(var, var_list){
193 if(var->bound < 0 || var->weight <= 0) continue;
195 INFO2("Checking KKT: sat = %e mu = %e",var->value - var->bound,var->mu);
196 if(!((fabs(var->value - var->bound)<epsilon_min_error && var->mu>=epsilon_min_error) ||
197 (fabs(var->value - var->bound)>=epsilon_min_error && var->mu<epsilon_min_error))) {
198 WARN1("The KKT condition is not verified for var %p...",var);
203 /* tmp = (var->value - var->bound); */
204 /* if(tmp != 0.0 || var->mu != 0.0){ */
205 /* WARN3("The flow (%p) doesn't match the KKT property, value expected (=0) got (lambda=%e) (sum_rho=%e)", var, var->mu, tmp); */
209 if(overall_error <= epsilon_min_error){
210 DEBUG1("The method converges in %d iterations.", iteration);
212 WARN1("Method reach %d iterations, which is the maxmimun number of iterations allowed.", iteration);
217 * Returns a double value corresponding to the result of a dicotomi proccess with
218 * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
219 * case of a constraint) and a initial value init.
221 * @param init initial value for \mu or \lambda
222 * @param diff a function that computes the differential of with respect a \mu or \lambda
223 * @param var_cnst a pointer to a variable or constraint
224 * @param min_erro a minimun error tolerated
226 * @return a double correponding to the result of the dicotomial process
228 double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error){
230 double overall_error;
232 double min_diff, max_diff, middle_diff;
240 min_diff = max_diff = middle_diff = 0.0;
243 if(diff(0.0, var_cnst) > 0){
244 DEBUG1("====> returning 0.0 (diff = %e)", diff(0.0, var_cnst));
248 DEBUG0("====> not detected positive diff in 0");
250 while(overall_error > min_error){
252 min_diff = diff(min, var_cnst);
253 max_diff = diff(max, var_cnst);
255 DEBUG2("DICOTOMI ===> min = %e , max = %e", min, max);
256 DEBUG2("DICOTOMI ===> diffmin = %e , diffmax = %e", min_diff, max_diff);
258 if( min_diff > 0 && max_diff > 0 ){
264 }else if( min_diff < 0 && max_diff < 0 ){
270 }else if( min_diff < 0 && max_diff > 0 ){
271 middle = (max + min)/2.0;
272 middle_diff = diff(middle, var_cnst);
274 if(max != 0.0 && min != 0.0){
275 overall_error = fabs(min - max)/max;
278 if( middle_diff < 0 ){
280 }else if( middle_diff > 0 ){
283 WARN0("Found an optimal solution with 0 error!");
288 }else if(min_diff == 0){
290 }else if(max_diff == 0){
292 }else if(min_diff > 0 && max_diff < 0){
293 WARN0("The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
298 DEBUG1("====> returning %e", (min+max)/2.0);
299 return ((min+max)/2.0);
305 double partial_diff_mu(double mu, void *param_var){
306 double mu_partial=0.0;
308 lmm_variable_t var = (lmm_variable_t)param_var;
312 for(i=0; i<var->cnsts_number; i++)
313 sigma_mu += (var->cnsts[i].constraint)->lambda;
315 //compute sigma_i + mu_i
318 //use auxiliar function passing (sigma_i + mu_i)
319 mu_partial = diff_aux(var, sigma_mu) ;
322 mu_partial += var->bound;
330 double partial_diff_lambda(double lambda, void *param_cnst){
333 xbt_swag_t elem_list = NULL;
334 lmm_element_t elem = NULL;
335 lmm_variable_t var = NULL;
336 lmm_constraint_t cnst= (lmm_constraint_t) param_cnst;
337 double lambda_partial=0.0;
340 elem_list = &(cnst->element_set);
342 DEBUG1("Computting diff of cnst (%p)", cnst);
344 xbt_swag_foreach(elem, elem_list) {
345 var = elem->variable;
346 if(var->weight<=0) continue;
348 //initilize de sumation variable
351 //compute sigma_i of variable var
352 for(i=0; i<var->cnsts_number; i++){
353 sigma_i += (var->cnsts[i].constraint)->lambda;
356 //add mu_i if this flow has a RTT constraint associated
357 if(var->bound > 0) sigma_i += var->mu;
359 //replace value of cnst->lambda by the value of parameter lambda
360 sigma_i = (sigma_i - cnst->lambda) + lambda;
362 //use the auxiliar function passing (\sigma_i + \mu_i)
363 lambda_partial += diff_aux(var, sigma_i);
366 lambda_partial += cnst->bound;
368 return lambda_partial;
372 double diff_aux(lmm_variable_t var, double x){
373 double tmp_fp, tmp_fpi, tmp_fpip, result;
375 xbt_assert0(var->func_fp, "Initialize the protocol functions first create variables before.");
377 tmp_fp = var->func_fp(var, x);
378 tmp_fpi = var->func_fpi(var, x);
379 tmp_fpip = var->func_fpip(var, x);
381 result = tmp_fpip*(var->func_fp(var, tmp_fpi));
383 result = result - tmp_fpi;
385 result = result - (tmp_fpip * x);