Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c70af9e1f9d9e1666cd772a271fb59294eccd6e6
[simgrid.git] / src / surf / lagrange.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Pedro Velho. All rights reserved.     */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 /*
9  * Modelling the proportional fairness using the Lagrange Optimization 
10  * Approach. For a detailed description see:
11  * "ssh://username@scm.gforge.inria.fr/svn/memo/people/pvelho/lagrange/ppf.ps".
12  */
13 #include "xbt/log.h"
14 #include "xbt/sysdep.h"
15 #include "xbt/mallocator.h"
16 #include "maxmin_private.h"
17
18 #include <stdlib.h>
19 #ifndef MATH
20 #include <math.h>
21 #endif
22
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_lagrange, surf, "Logging specific to SURF (lagrange)");
25
26
27 /*
28  * Local prototypes to implement the lagrangian optimization with optimal step, also called dicotomi.
29  */
30 //solves the proportional fairness using a lagrange optimizition with dicotomi step
31 void   lagrange_solve       (lmm_system_t sys);
32 //computes the value of the dicotomi using a initial values, init, with a specific variable or constraint
33 double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error);
34 //computes the value of the differential of variable param_var applied to mu  
35 double partial_diff_mu      (double mu, void * param_var);
36 //computes the value of the differential of constraint param_cnst applied to lambda  
37 double partial_diff_lambda  (double lambda, void * param_cnst);
38
39
40
41 void lagrange_solve(lmm_system_t sys)
42 {
43   /*
44    * Lagrange Variables.
45    */
46   int max_iterations= 10;
47   double epsilon_min_error = 1e-10;
48   double overall_error = 1;
49
50
51   /*
52    * Variables to manipulate the data structure proposed to model the maxmin
53    * fairness. See docummentation for more details.
54    */
55   xbt_swag_t elem_list  = NULL;
56   lmm_element_t elem    = NULL;
57
58
59   xbt_swag_t cnst_list  = NULL;
60   lmm_constraint_t cnst = NULL;
61   
62   xbt_swag_t var_list   = NULL;
63   lmm_variable_t var    = NULL;
64
65
66   /*
67    * Auxiliar variables.
68    */
69   int iteration=0;
70   double tmp=0;
71   int i;
72    
73
74   DEBUG0("Iterative method configuration snapshot =====>");
75   DEBUG1("#### Maximum number of iterations : %d", max_iterations);
76   DEBUG1("#### Minimum error tolerated      : %e", epsilon_min_error);
77
78
79   if ( !(sys->modified))
80     return;
81
82   /* 
83    * Initialize the var list variable with only the active variables. 
84    * Associate an index in the swag variables. Initialize mu.
85    */
86   var_list = &(sys->variable_set);
87   i=0;
88   xbt_swag_foreach(var, var_list) {
89     if((var->bound > 0.0) || (var->weight <= 0.0)){
90       DEBUG1("#### NOTE var(%d) is a boundless variable", i);
91       var->mu = -1.0;
92     } else{ 
93       var->mu =   1.0;
94       var->new_mu = 2.0;
95     }
96     DEBUG2("#### var(%d)->mu :  %e", i, var->mu);
97     DEBUG2("#### var(%d)->weight: %e", i, var->weight);
98     i++;
99   }
100
101   /* 
102    * Initialize lambda.
103    */
104   cnst_list=&(sys->active_constraint_set); 
105   xbt_swag_foreach(cnst, cnst_list) {
106     cnst->lambda = 1.0;
107     cnst->new_lambda = 2.0;
108     DEBUG2("#### cnst(%p)->lambda :  %e", cnst, cnst->lambda);
109   }
110   
111   /*
112    * While doesn't reach a minimun error or a number maximum of iterations.
113    */
114   while(overall_error > epsilon_min_error && iteration < max_iterations){
115    
116     iteration++;
117     
118     /*                       
119      * Compute the value of mu_i
120      */
121     var_list = &(sys->variable_set);
122     //forall mu_i in mu_1, mu_2, ..., mu_n
123     xbt_swag_foreach(var, var_list) {
124       if((var->bound >= 0) && (var->weight > 0) ){
125         var->mu = dicotomi(var->mu, partial_diff_mu, var, epsilon_min_error);
126         if(var->mu < 0) var->mu = 0;
127       }
128     }
129     
130
131     /*
132      * Compute the value of lambda_i
133      */
134     //forall lambda_i in lambda_1, lambda_2, ..., lambda_n
135     xbt_swag_foreach(cnst, cnst_list) {
136       cnst->lambda = dicotomi(cnst->lambda, partial_diff_lambda, cnst, epsilon_min_error);
137       if(cnst->lambda < 0) cnst->lambda = 0;
138     }
139
140
141     /*
142      * Now computes the values of each variable (\rho) based on
143      * the values of \lambda and \mu.
144      */
145     overall_error=0;
146     DEBUG1("Iteration %d ", iteration);
147     xbt_swag_foreach(var, var_list) {
148       if(var->weight <=0) 
149         var->value = 0.0;
150       else {
151         tmp = 0;
152         for(i=0; i<var->cnsts_number; i++){
153           tmp += (var->cnsts[i].constraint)->lambda;
154           if(var->bound > 0) 
155             tmp+=var->mu;
156         }
157         
158         //computes de overall_error
159         if(overall_error < fabs(var->value - 1.0/tmp)){
160           overall_error = fabs(var->value - 1.0/tmp);
161         }
162
163         var->value = 1.0 / tmp;
164       }
165
166
167       DEBUG2("======> value of var (%p)  = %e", var, var->value);      
168     }
169   }
170
171
172   //verify the KKT property for each link
173   xbt_swag_foreach(cnst, cnst_list){
174     tmp = 0;
175     elem_list = &(cnst->element_set);
176     xbt_swag_foreach(elem, elem_list) {
177       var = elem->variable;
178       if(var->weight<=0) continue;
179       tmp += var->value;
180     }
181   
182     tmp = tmp - cnst->bound;
183   
184   
185     if(tmp != 0 ||  cnst->lambda != 0){
186       WARN4("The link %s(%p) doesn't match the KKT property, value expected (=0) got (lambda=%e) (sum_rho=%e)", (char *)cnst->id, cnst, cnst->lambda, tmp);
187     }
188   
189   }
190
191   
192   //verify the KKT property of each flow
193   xbt_swag_foreach(var, var_list){
194     if(var->bound <= 0 || var->weight <= 0) continue;
195     tmp = 0;
196     tmp = (var->value - var->bound);
197
198     
199     if(tmp != 0 ||  var->mu != 0){
200       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);
201     }
202
203   }
204
205
206   if(overall_error <= epsilon_min_error){
207     DEBUG1("The method converge in %d iterations.", iteration);
208   }else{
209     WARN1("Method reach %d iterations, which is the maxmimun number of iterations allowed.", iteration);
210   }
211
212
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
233   min = max = init;
234   overall_error = 1;
235
236   DEBUG0("STARTING DICOTOMI... Debuggin, format used [min, max], [D(min),D(max)]");
237
238   while(overall_error > min_error){
239     DEBUG4("====> [%e, %e] , [%e,%e]", min, max, diff(min, var_cnst), diff(max, var_cnst));
240
241     if( diff(min, var_cnst) > 0 && diff(max, var_cnst) > 0 ){
242       if(min == max){
243         min = min / 2.0;
244       }else{
245         max = min;
246       }
247     }else if( diff(min, var_cnst) < 0 && diff(max, var_cnst) < 0 ){
248       if(min == max){
249         max = max * 2.0;
250       }else{
251         min = max;
252       }
253     }else if( diff(min, var_cnst) < 0 && diff(max, var_cnst) > 0 ){
254       middle = (max + min)/2.0;
255       
256       if( diff(middle, var_cnst) < 0 ){
257         min = middle;
258       }else if( diff(middle, var_cnst) > 0 ){
259         max = middle;
260       }else{
261         WARN0("Found an optimal solution with 0 error!");
262         overall_error = 0;
263       }
264       overall_error = fabs(min - max);
265     }else{
266       WARN0("The impossible happened, partial_diff(min) >0 && partial_diff(max) < 0");
267     }
268   }
269
270   return ((min+max)/2.0);
271 }
272
273 double partial_diff_mu(double mu, void *param_var){
274   double mu_partial=0.0;
275   lmm_variable_t var = (lmm_variable_t)param_var;
276   int i;
277
278   //for each link with capacity cnsts[i] that uses flow of variable var do
279   for(i=0; i<var->cnsts_number; i++)
280     mu_partial += (var->cnsts[i].constraint)->lambda + mu;
281   
282   mu_partial = (-1.0/mu_partial) + var->bound;
283
284   return mu_partial;
285 }
286
287
288 double partial_diff_lambda(double lambda, void *param_cnst){
289
290   double tmp=0.0;
291   int i;
292   xbt_swag_t elem_list = NULL;
293   lmm_element_t elem = NULL;
294   lmm_variable_t var = NULL;
295   lmm_constraint_t cnst= (lmm_constraint_t) param_cnst;
296   double lambda_partial=0.0;
297
298
299   elem_list = &(cnst->element_set);
300   
301   xbt_swag_foreach(elem, elem_list) {
302     var = elem->variable;
303     if(var->weight<=0) continue;
304     
305     tmp = 0;
306     for(i=0; i<var->cnsts_number; i++){
307       tmp += (var->cnsts[i].constraint)->lambda;
308     }
309         
310     if(var->bound > 0)
311       tmp += var->mu;
312     
313     tmp = tmp - cnst->lambda + lambda;
314     
315     //avoid a disaster value of lambda
316     if(tmp==0) lambda_partial = 10e-8;
317     
318     lambda_partial += (-1.0 /tmp);
319   }
320
321   lambda_partial += cnst->bound;
322
323   return lambda_partial;
324 }
325