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);
36 void lagrange_solve(lmm_system_t sys)
41 int max_iterations= 10000;
42 double epsilon_min_error = 1e-4;
43 double dicotomi_min_error = 1e-8;
44 double overall_error = 1;
47 * Variables to manipulate the data structure proposed to model the maxmin
48 * fairness. See docummentation for more details.
50 xbt_swag_t elem_list = NULL;
51 lmm_element_t elem = NULL;
53 xbt_swag_t cnst_list = NULL;
54 lmm_constraint_t cnst = NULL;
56 xbt_swag_t var_list = NULL;
57 lmm_variable_t var = NULL;
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);
72 if ( !(sys->modified))
76 * Initialize the var list variable with only the active variables.
77 * Associate an index in the swag variables. Initialize mu.
79 var_list = &(sys->variable_set);
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);
89 DEBUG2("#### var(%d)->mu : %e", i, var->mu);
90 DEBUG2("#### var(%d)->weight: %e", i, var->weight);
97 cnst_list=&(sys->active_constraint_set);
98 xbt_swag_foreach(cnst, cnst_list){
100 cnst->new_lambda = 2.0;
101 DEBUG2("#### cnst(%p)->lambda : %e", cnst, cnst->lambda);
105 * While doesn't reach a minimun error or a number maximum of iterations.
107 while(overall_error > epsilon_min_error && iteration < max_iterations){
110 DEBUG1("************** ITERATION %d **************", iteration);
113 * Compute the value of mu_i
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;
125 * Compute the value of lambda_i
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;
136 /* * Update values of mu and lambda */
138 /* //forall mu_i in mu_1, mu_2, ..., mu_n */
139 /* xbt_swag_foreach(var, var_list) { */
140 /* var->mu = var->new_mu ; */
143 /* //forall lambda_i in lambda_1, lambda_2, ..., lambda_n */
144 /* xbt_swag_foreach(cnst, cnst_list) { */
145 /* cnst->lambda = cnst->new_lambda; */
149 * Now computes the values of each variable (\rho) based on
150 * the values of \lambda and \mu.
153 xbt_swag_foreach(var, var_list) {
158 for(i=0; i<var->cnsts_number; i++){
159 tmp += (var->cnsts[i].constraint)->lambda;
165 WARN0("CAUTION: division by 0.0");
167 //computes de overall_error
168 if(overall_error < fabs(var->value - 1.0/tmp)){
169 overall_error = fabs(var->value - 1.0/tmp);
171 var->value = 1.0 / tmp;
173 DEBUG4("======> value of var %s (%p) = %e, overall_error = %e", (char *)var->id, var, var->value, overall_error);
178 //verify the KKT property for each link
179 xbt_swag_foreach(cnst, cnst_list){
181 elem_list = &(cnst->element_set);
182 xbt_swag_foreach(elem, elem_list) {
183 var = elem->variable;
184 if(var->weight<=0) continue;
188 tmp = tmp - cnst->bound;
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);
196 //verify the KKT property of each flow
197 xbt_swag_foreach(var, var_list){
198 if(var->bound <= 0 || var->weight <= 0) continue;
200 tmp = (var->value - var->bound);
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);
209 if(overall_error <= epsilon_min_error){
210 DEBUG1("The method converge 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);
273 overall_error = fabs(min - max);
275 if( middle_diff < 0 ){
277 }else if( middle_diff > 0 ){
280 WARN0("Found an optimal solution with 0 error!");
285 }else if(min_diff == 0){
287 }else if(max_diff == 0){
289 }else if(min_diff > 0 && max_diff < 0){
290 WARN0("The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
295 DEBUG1("====> returning %e", (min+max)/2.0);
296 return ((min+max)/2.0);
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;
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;
311 mu_partial = (-1.0/mu_partial) + var->bound;
319 double partial_diff_lambda(double lambda, void *param_cnst){
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;
330 elem_list = &(cnst->element_set);
333 DEBUG2("Computting diff of cnst (%p) %s", cnst, (char *)cnst->id);
335 xbt_swag_foreach(elem, elem_list) {
336 var = elem->variable;
337 if(var->weight<=0) continue;
341 //DEBUG2("===> Variable (%p) %s", var, (char *)var->id);
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);
352 //DEBUG2("======> lambda - %e + %e ", cnst->lambda, lambda);
354 tmp = tmp - cnst->lambda + lambda;
356 //avoid a disaster value of lambda
357 //if(tmp==0) tmp = 10e-8;
359 lambda_partial += (-1.0/tmp);
361 //DEBUG1("======> %e ", (-1.0/tmp));
364 lambda_partial += cnst->bound;
366 //DEBUG1("===> %e ", lambda_partial);
368 return lambda_partial;