Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding more debuging info.
[simgrid.git] / src / surf / lagrange.c
1 /*      $Id$     */
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. */
5 /*
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".
9  */
10 #include "xbt/log.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/mallocator.h"
13 #include "maxmin_private.h"
14
15 #include <stdlib.h>
16 #ifndef MATH
17 #include <math.h>
18 #endif
19
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_lagrange, surf, "Logging specific to SURF (lagrange)");
22
23 /*
24  * Local prototypes to implement the lagrangian optimization with optimal step, also called dicotomi.
25  */
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
35
36 void lagrange_solve(lmm_system_t sys)
37 {
38   /*
39    * Lagrange Variables.
40    */
41   int max_iterations= 10000;
42   double epsilon_min_error = 1e-4;
43   double dicotomi_min_error = 1e-8;
44   double overall_error = 1;
45
46   /*
47    * Variables to manipulate the data structure proposed to model the maxmin
48    * fairness. See docummentation for more details.
49    */
50   xbt_swag_t elem_list  = NULL;
51   lmm_element_t elem    = NULL;
52
53   xbt_swag_t cnst_list  = NULL;
54   lmm_constraint_t cnst = NULL;
55   
56   xbt_swag_t var_list   = NULL;
57   lmm_variable_t var    = NULL;
58
59   /*
60    * Auxiliar variables.
61    */
62   int iteration=0;
63   double tmp=0;
64   int i;
65    
66
67   DEBUG0("Iterative method configuration snapshot =====>");
68   DEBUG1("#### Maximum number of iterations       : %d", max_iterations);
69   DEBUG1("#### Minimum error tolerated            : %e", epsilon_min_error);  
70   DEBUG1("#### Minimum error tolerated (dicotomi) : %e", dicotomi_min_error);
71
72   if ( !(sys->modified))
73     return;
74
75   /* 
76    * Initialize the var list variable with only the active variables. 
77    * Associate an index in the swag variables. Initialize mu.
78    */
79   var_list = &(sys->variable_set);
80   i=0;
81   xbt_swag_foreach(var, var_list) {    
82     if((var->bound > 0.0) || (var->weight <= 0.0)){
83       DEBUG1("#### NOTE var(%d) is a boundless variable", i);
84       var->mu = -1.0;
85     } else{ 
86       var->mu =   1.0;
87       var->new_mu = 2.0;
88     }
89     DEBUG2("#### var(%d)->mu :  %e", i, var->mu);
90     DEBUG2("#### var(%d)->weight: %e", i, var->weight);
91     i++;
92   }
93
94   /* 
95    * Initialize lambda.
96    */
97   cnst_list=&(sys->active_constraint_set); 
98   xbt_swag_foreach(cnst, cnst_list){
99     cnst->lambda = 1.0;
100     cnst->new_lambda = 2.0;
101     DEBUG2("#### cnst(%p)->lambda :  %e", cnst, cnst->lambda);
102   }
103   
104   /*
105    * While doesn't reach a minimun error or a number maximum of iterations.
106    */
107   while(overall_error > epsilon_min_error && iteration < max_iterations){
108    
109     iteration++;
110     DEBUG1("************** ITERATION %d **************", iteration);    
111
112     /*                       
113      * Compute the value of mu_i
114      */
115     //forall mu_i in mu_1, mu_2, ..., mu_n
116     xbt_swag_foreach(var, var_list) {
117       if((var->bound >= 0) && (var->weight > 0) ){
118         var->new_mu = dicotomi(var->mu, partial_diff_mu, var, dicotomi_min_error);
119         if(var->new_mu < 0) var->new_mu = 0;
120         var->mu = var->new_mu;
121       }
122     }
123
124     /*
125      * Compute the value of lambda_i
126      */
127     //forall lambda_i in lambda_1, lambda_2, ..., lambda_n
128     xbt_swag_foreach(cnst, cnst_list) {
129       cnst->new_lambda = dicotomi(cnst->lambda, partial_diff_lambda, cnst, dicotomi_min_error);
130       DEBUG2("====> cnst->lambda (%p) = %e", cnst, cnst->new_lambda);      
131       cnst->lambda = cnst->new_lambda;
132     }
133
134
135 /*     /\*                        */
136 /*      * Update values of mu and lambda */
137 /*      *\/ */
138 /*     //forall mu_i in mu_1, mu_2, ..., mu_n */
139 /*     xbt_swag_foreach(var, var_list) { */
140 /*       var->mu = var->new_mu ; */
141 /*     } */
142   
143 /*     //forall lambda_i in lambda_1, lambda_2, ..., lambda_n */
144 /*     xbt_swag_foreach(cnst, cnst_list) { */
145 /*       cnst->lambda = cnst->new_lambda; */
146 /*     } */
147
148     /*
149      * Now computes the values of each variable (\rho) based on
150      * the values of \lambda and \mu.
151      */
152     overall_error=0;   
153     xbt_swag_foreach(var, var_list) {
154       if(var->weight <=0) 
155         var->value = 0.0;
156       else {
157         tmp = 0;
158         for(i=0; i<var->cnsts_number; i++){
159           tmp += (var->cnsts[i].constraint)->lambda;
160           if(var->bound > 0) 
161             tmp+=var->mu;
162         }
163
164         if(tmp == 0.0)
165           WARN0("CAUTION: division by 0.0");
166
167         //computes de overall_error
168         if(overall_error < fabs(var->value - 1.0/tmp)){
169           overall_error = fabs(var->value - 1.0/tmp);
170         }
171         var->value = 1.0 / tmp;
172       }
173       DEBUG4("======> value of var %s (%p)  = %e, overall_error = %e", (char *)var->id, var, var->value, overall_error);       
174     }
175   }
176
177
178   //verify the KKT property for each link
179   xbt_swag_foreach(cnst, cnst_list){
180     tmp = 0;
181     elem_list = &(cnst->element_set);
182     xbt_swag_foreach(elem, elem_list) {
183       var = elem->variable;
184       if(var->weight<=0) continue;
185       tmp += var->value;
186     }
187   
188     tmp = tmp - cnst->bound;
189
190     if(tmp > epsilon_min_error){
191       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);
192     }
193   
194   }
195   
196   //verify the KKT property of each flow
197   xbt_swag_foreach(var, var_list){
198     if(var->bound <= 0 || var->weight <= 0) continue;
199     tmp = 0;
200     tmp = (var->value - var->bound);
201
202     
203     if(tmp != 0 ||  var->mu != 0){
204       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);
205     }
206
207   }
208
209   if(overall_error <= epsilon_min_error){
210     DEBUG1("The method converge in %d iterations.", iteration);
211   }else{
212     WARN1("Method reach %d iterations, which is the maxmimun number of iterations allowed.", iteration);
213   }
214 }
215
216 /*
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. 
220  *
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
225  *
226  * @return a double correponding to the result of the dicotomial process
227  */
228 double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error){
229   double min, max;
230   double overall_error;
231   double middle;
232   double min_diff, max_diff, middle_diff;
233   
234   min = max = init;
235
236   if(init == 0){
237     min = max = 1;
238   }
239
240   min_diff = max_diff = middle_diff = 0.0;
241   overall_error = 1;
242
243   if(diff(0.0, var_cnst) > 0){
244     DEBUG1("====> returning 0.0 (diff = %e)", diff(0.0, var_cnst));
245     return 0.0;
246   }
247
248   DEBUG0("====> not detected positive diff in 0");
249
250   while(overall_error > min_error){
251
252     min_diff = diff(min, var_cnst);
253     max_diff = diff(max, var_cnst);
254
255     DEBUG2("DICOTOMI ===> min = %e , max = %e", min, max);
256     DEBUG2("DICOTOMI ===> diffmin = %e , diffmax = %e", min_diff, max_diff);
257
258     if( min_diff > 0 && max_diff > 0 ){
259       if(min == max){
260         min = min / 2.0;
261       }else{
262         max = min;
263       }
264     }else if( min_diff < 0 && max_diff < 0 ){
265       if(min == max){
266         max = max * 2.0;
267       }else{
268         min = max;
269       }
270     }else if( min_diff < 0 && max_diff > 0 ){
271       middle = (max + min)/2.0;
272       middle_diff = diff(middle, var_cnst);
273       overall_error = fabs(min - max);
274
275       if( middle_diff < 0 ){
276         min = middle;
277       }else if( middle_diff > 0 ){
278         max = middle;
279       }else{
280         WARN0("Found an optimal solution with 0 error!");
281         overall_error = 0;
282         return middle;
283       }
284
285     }else if(min_diff == 0){
286       return min;
287     }else if(max_diff == 0){
288       return max;
289     }else if(min_diff > 0 && max_diff < 0){
290       WARN0("The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
291     }
292   }
293
294
295   DEBUG1("====> returning %e", (min+max)/2.0);
296   return ((min+max)/2.0);
297 }
298
299 /*
300  *
301  */
302 double partial_diff_mu(double mu, void *param_var){
303   double mu_partial=0.0;
304   lmm_variable_t var = (lmm_variable_t)param_var;
305   int i;
306
307   //for each link with capacity cnsts[i] that uses flow of variable var do
308   for(i=0; i<var->cnsts_number; i++)
309     mu_partial += (var->cnsts[i].constraint)->lambda + mu;
310   
311   mu_partial = (-1.0/mu_partial) + var->bound;
312
313   return mu_partial;
314 }
315
316 /*
317  *
318  */
319 double partial_diff_lambda(double lambda, void *param_cnst){
320
321   double tmp=0.0;
322   int i;
323   xbt_swag_t elem_list = NULL;
324   lmm_element_t elem = NULL;
325   lmm_variable_t var = NULL;
326   lmm_constraint_t cnst= (lmm_constraint_t) param_cnst;
327   double lambda_partial=0.0;
328
329
330   elem_list = &(cnst->element_set);
331
332
333   DEBUG2("Computting diff of cnst (%p) %s", cnst, (char *)cnst->id);
334   
335   xbt_swag_foreach(elem, elem_list) {
336     var = elem->variable;
337     if(var->weight<=0) continue;
338     
339     tmp = 0;
340
341     //DEBUG2("===> Variable (%p) %s", var, (char *)var->id);
342
343     for(i=0; i<var->cnsts_number; i++){
344       tmp += (var->cnsts[i].constraint)->lambda;
345       //DEBUG1("======> lambda %e + ", (var->cnsts[i].constraint)->lambda);
346     }
347         
348     if(var->bound > 0)
349       tmp += var->mu;
350     
351
352     //DEBUG2("======> lambda - %e + %e ", cnst->lambda, lambda);
353
354     tmp = tmp - cnst->lambda + lambda;
355     
356     //avoid a disaster value of lambda
357     //if(tmp==0) tmp = 10e-8;
358     
359     lambda_partial += (-1.0/tmp);
360
361     //DEBUG1("======> %e ", (-1.0/tmp));
362   }
363
364   lambda_partial += cnst->bound;
365
366   //DEBUG1("===> %e ", lambda_partial);
367
368   return lambda_partial;
369 }
370   
371