Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
*** empty log message ***
[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= 100;
47   double epsilon_min_error = 1e-4;
48   double dicotomi_min_error = 1e-8;
49   double overall_error = 1;
50
51
52   /*
53    * Variables to manipulate the data structure proposed to model the maxmin
54    * fairness. See docummentation for more details.
55    */
56   xbt_swag_t elem_list  = NULL;
57   lmm_element_t elem    = NULL;
58
59
60   xbt_swag_t cnst_list  = NULL;
61   lmm_constraint_t cnst = NULL;
62   
63   xbt_swag_t var_list   = NULL;
64   lmm_variable_t var    = NULL;
65
66
67   /*
68    * Auxiliar variables.
69    */
70   int iteration=0;
71   double tmp=0;
72   int i;
73    
74
75   DEBUG0("Iterative method configuration snapshot =====>");
76   DEBUG1("#### Maximum number of iterations       : %d", max_iterations);
77   DEBUG1("#### Minimum error tolerated            : %e", epsilon_min_error);  
78   DEBUG1("#### Minimum error tolerated (dicotomi) : %e", dicotomi_min_error);
79
80
81   if ( !(sys->modified))
82     return;
83
84   /* 
85    * Initialize the var list variable with only the active variables. 
86    * Associate an index in the swag variables. Initialize mu.
87    */
88   var_list = &(sys->variable_set);
89   i=0;
90   xbt_swag_foreach(var, var_list) {    if((var->bound > 0.0) || (var->weight <= 0.0)){
91     DEBUG1("#### NOTE var(%d) is a boundless variable", i);
92     var->mu = -1.0;
93   } else{ 
94     var->mu =   1.0;
95     var->new_mu = 2.0;
96   }
97   DEBUG2("#### var(%d)->mu :  %e", i, var->mu);
98   DEBUG2("#### var(%d)->weight: %e", i, var->weight);
99   i++;
100   }
101
102   /* 
103    * Initialize lambda.
104    */
105   cnst_list=&(sys->active_constraint_set); 
106   xbt_swag_foreach(cnst, cnst_list) {
107     cnst->lambda = 1.0;
108     cnst->new_lambda = 2.0;
109     DEBUG2("#### cnst(%p)->lambda :  %e", cnst, cnst->lambda);
110   }
111   
112   /*
113    * While doesn't reach a minimun error or a number maximum of iterations.
114    */
115   while(overall_error > epsilon_min_error && iteration < max_iterations){
116    
117     iteration++;
118     DEBUG1("************** ITERATION %d **************", iteration);    
119
120
121     /*                       
122      * Compute the value of mu_i
123      */
124     //forall mu_i in mu_1, mu_2, ..., mu_n
125     xbt_swag_foreach(var, var_list) {
126       if((var->bound >= 0) && (var->weight > 0) ){
127         var->new_mu = dicotomi(var->mu, partial_diff_mu, var, dicotomi_min_error);
128         if(var->new_mu < 0) var->new_mu = 0;
129       }
130     }
131
132     /*
133      * Compute the value of lambda_i
134      */
135     //forall lambda_i in lambda_1, lambda_2, ..., lambda_n
136     xbt_swag_foreach(cnst, cnst_list) {
137       cnst->new_lambda = dicotomi(cnst->lambda, partial_diff_lambda, cnst, dicotomi_min_error);
138       if(cnst->new_lambda < 0) cnst->new_lambda = 0;
139     }
140
141
142     /*                       
143      * Update values of mu and lambda
144      */
145     //forall mu_i in mu_1, mu_2, ..., mu_n
146     xbt_swag_foreach(var, var_list) {
147       var->mu = var->new_mu ;
148     }
149   
150     //forall lambda_i in lambda_1, lambda_2, ..., lambda_n
151     xbt_swag_foreach(cnst, cnst_list) {
152       cnst->lambda = cnst->new_lambda;
153     }
154
155
156     /*
157      * Now computes the values of each variable (\rho) based on
158      * the values of \lambda and \mu.
159      */
160     overall_error=0;   
161     xbt_swag_foreach(var, var_list) {
162       if(var->weight <=0) 
163         var->value = 0.0;
164       else {
165         tmp = 0;
166         for(i=0; i<var->cnsts_number; i++){
167           tmp += (var->cnsts[i].constraint)->lambda;
168           if(var->bound > 0) 
169             tmp+=var->mu;
170         }
171         //computes de overall_error
172         if(overall_error < fabs(var->value - 1.0/tmp)){
173           overall_error = fabs(var->value - 1.0/tmp);
174         }
175         var->value = 1.0 / tmp;
176       }
177       DEBUG4("======> value of var %s (%p)  = %e, overall_error = %e", (char *)var->id, var, var->value, overall_error);       
178     }
179   }
180
181
182   //verify the KKT property for each link
183   xbt_swag_foreach(cnst, cnst_list){
184     tmp = 0;
185     elem_list = &(cnst->element_set);
186     xbt_swag_foreach(elem, elem_list) {
187       var = elem->variable;
188       if(var->weight<=0) continue;
189       tmp += var->value;
190     }
191   
192     tmp = tmp - cnst->bound;
193
194     if(tmp > epsilon_min_error){
195       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);
196     }
197   
198   }
199
200   
201   //verify the KKT property of each flow
202   xbt_swag_foreach(var, var_list){
203     if(var->bound <= 0 || var->weight <= 0) continue;
204     tmp = 0;
205     tmp = (var->value - var->bound);
206
207     
208     if(tmp != 0 ||  var->mu != 0){
209       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);
210     }
211
212   }
213
214
215   if(overall_error <= epsilon_min_error){
216     DEBUG1("The method converge in %d iterations.", iteration);
217   }else{
218     WARN1("Method reach %d iterations, which is the maxmimun number of iterations allowed.", iteration);
219   }
220 }
221
222 /*
223  * Returns a double value corresponding to the result of a dicotomi proccess with
224  * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
225  * case of a constraint) and a initial value init. 
226  *
227  * @param init initial value for \mu or \lambda
228  * @param diff a function that computes the differential of with respect a \mu or \lambda
229  * @param var_cnst a pointer to a variable or constraint 
230  * @param min_erro a minimun error tolerated
231  *
232  * @return a double correponding to the result of the dicotomial process
233  */
234 double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error){
235   double min, max;
236   double overall_error;
237   double middle;
238
239   min = max = init;
240   overall_error = 1;
241
242   //DEBUG0("STARTING DICOTOMI... Debugging, format used [min, max], [D(min),D(max)]");
243
244   while(overall_error > min_error){
245     //DEBUG4("====> [%e, %e] , [%e,%e]", min, max, diff(min, var_cnst), diff(max, var_cnst));
246
247     if( diff(min, var_cnst) > 0 && diff(max, var_cnst) > 0 ){
248       if(min == max){
249         min = min / 2.0;
250       }else{
251         max = min;
252       }
253     }else if( diff(min, var_cnst) < 0 && diff(max, var_cnst) < 0 ){
254       if(min == max){
255         max = max * 2.0;
256       }else{
257         min = max;
258       }
259     }else if( diff(min, var_cnst) < 0 && diff(max, var_cnst) > 0 ){
260       middle = (max + min)/2.0;
261       
262       if( diff(middle, var_cnst) < 0 ){
263         min = middle;
264       }else if( diff(middle, var_cnst) > 0 ){
265         max = middle;
266       }else{
267         WARN0("Found an optimal solution with 0 error!");
268         overall_error = 0;
269       }
270       overall_error = fabs(min - max);
271     }else{
272       WARN0("The impossible happened, partial_diff(min) >0 && partial_diff(max) < 0");
273     }
274   }
275
276   return ((min+max)/2.0);
277 }
278
279 double partial_diff_mu(double mu, void *param_var){
280   double mu_partial=0.0;
281   lmm_variable_t var = (lmm_variable_t)param_var;
282   int i;
283
284   //for each link with capacity cnsts[i] that uses flow of variable var do
285   for(i=0; i<var->cnsts_number; i++)
286     mu_partial += (var->cnsts[i].constraint)->lambda + mu;
287   
288   mu_partial = (-1.0/mu_partial) + var->bound;
289
290   return mu_partial;
291 }
292
293
294 double partial_diff_lambda(double lambda, void *param_cnst){
295
296   double tmp=0.0;
297   int i;
298   xbt_swag_t elem_list = NULL;
299   lmm_element_t elem = NULL;
300   lmm_variable_t var = NULL;
301   lmm_constraint_t cnst= (lmm_constraint_t) param_cnst;
302   double lambda_partial=0.0;
303
304
305   elem_list = &(cnst->element_set);
306   
307   xbt_swag_foreach(elem, elem_list) {
308     var = elem->variable;
309     if(var->weight<=0) continue;
310     
311     tmp = 0;
312     for(i=0; i<var->cnsts_number; i++){
313       tmp += (var->cnsts[i].constraint)->lambda;
314     }
315         
316     if(var->bound > 0)
317       tmp += var->mu;
318     
319     tmp = tmp - cnst->lambda + lambda;
320     
321     //avoid a disaster value of lambda
322     if(tmp==0) lambda_partial = 10e-8;
323     
324     lambda_partial += (-1.0 /tmp);
325   }
326
327   lambda_partial += cnst->bound;
328
329   return lambda_partial;
330 }
331