3 /* Copyright (c) 2007 Arnaud Legrand, Pedro Velho. All rights reserved. */
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. */
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".
14 #include "xbt/sysdep.h"
15 #include "xbt/mallocator.h"
16 #include "maxmin_private.h"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_lagrange, surf, "Logging specific to SURF (lagrange)");
28 * Local prototypes to implement the lagrangian optimization with optimal step, also called dicotomi.
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);
41 void lagrange_solve(lmm_system_t sys)
46 int max_iterations= 100;
47 double epsilon_min_error = 1e-4;
48 double dicotomi_min_error = 1e-8;
49 double overall_error = 1;
53 * Variables to manipulate the data structure proposed to model the maxmin
54 * fairness. See docummentation for more details.
56 xbt_swag_t elem_list = NULL;
57 lmm_element_t elem = NULL;
60 xbt_swag_t cnst_list = NULL;
61 lmm_constraint_t cnst = NULL;
63 xbt_swag_t var_list = NULL;
64 lmm_variable_t var = NULL;
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);
81 if ( !(sys->modified))
85 * Initialize the var list variable with only the active variables.
86 * Associate an index in the swag variables. Initialize mu.
88 var_list = &(sys->variable_set);
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);
97 DEBUG2("#### var(%d)->mu : %e", i, var->mu);
98 DEBUG2("#### var(%d)->weight: %e", i, var->weight);
105 cnst_list=&(sys->active_constraint_set);
106 xbt_swag_foreach(cnst, cnst_list) {
108 cnst->new_lambda = 2.0;
109 DEBUG2("#### cnst(%p)->lambda : %e", cnst, cnst->lambda);
113 * While doesn't reach a minimun error or a number maximum of iterations.
115 while(overall_error > epsilon_min_error && iteration < max_iterations){
118 DEBUG1("************** ITERATION %d **************", iteration);
122 * Compute the value of mu_i
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;
133 * Compute the value of lambda_i
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;
143 * Update values of mu and lambda
145 //forall mu_i in mu_1, mu_2, ..., mu_n
146 xbt_swag_foreach(var, var_list) {
147 var->mu = var->new_mu ;
150 //forall lambda_i in lambda_1, lambda_2, ..., lambda_n
151 xbt_swag_foreach(cnst, cnst_list) {
152 cnst->lambda = cnst->new_lambda;
157 * Now computes the values of each variable (\rho) based on
158 * the values of \lambda and \mu.
161 xbt_swag_foreach(var, var_list) {
166 for(i=0; i<var->cnsts_number; i++){
167 tmp += (var->cnsts[i].constraint)->lambda;
171 //computes de overall_error
172 if(overall_error < fabs(var->value - 1.0/tmp)){
173 overall_error = fabs(var->value - 1.0/tmp);
175 var->value = 1.0 / tmp;
177 DEBUG4("======> value of var %s (%p) = %e, overall_error = %e", (char *)var->id, var, var->value, overall_error);
182 //verify the KKT property for each link
183 xbt_swag_foreach(cnst, cnst_list){
185 elem_list = &(cnst->element_set);
186 xbt_swag_foreach(elem, elem_list) {
187 var = elem->variable;
188 if(var->weight<=0) continue;
192 tmp = tmp - cnst->bound;
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);
201 //verify the KKT property of each flow
202 xbt_swag_foreach(var, var_list){
203 if(var->bound <= 0 || var->weight <= 0) continue;
205 tmp = (var->value - var->bound);
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);
215 if(overall_error <= epsilon_min_error){
216 DEBUG1("The method converge in %d iterations.", iteration);
218 WARN1("Method reach %d iterations, which is the maxmimun number of iterations allowed.", iteration);
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.
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
232 * @return a double correponding to the result of the dicotomial process
234 double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error){
236 double overall_error;
242 //DEBUG0("STARTING DICOTOMI... Debugging, format used [min, max], [D(min),D(max)]");
244 while(overall_error > min_error){
245 //DEBUG4("====> [%e, %e] , [%e,%e]", min, max, diff(min, var_cnst), diff(max, var_cnst));
247 if( diff(min, var_cnst) > 0 && diff(max, var_cnst) > 0 ){
253 }else if( diff(min, var_cnst) < 0 && diff(max, var_cnst) < 0 ){
259 }else if( diff(min, var_cnst) < 0 && diff(max, var_cnst) > 0 ){
260 middle = (max + min)/2.0;
262 if( diff(middle, var_cnst) < 0 ){
264 }else if( diff(middle, var_cnst) > 0 ){
267 WARN0("Found an optimal solution with 0 error!");
270 overall_error = fabs(min - max);
272 WARN0("The impossible happened, partial_diff(min) >0 && partial_diff(max) < 0");
276 return ((min+max)/2.0);
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;
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;
288 mu_partial = (-1.0/mu_partial) + var->bound;
294 double partial_diff_lambda(double lambda, void *param_cnst){
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;
305 elem_list = &(cnst->element_set);
307 xbt_swag_foreach(elem, elem_list) {
308 var = elem->variable;
309 if(var->weight<=0) continue;
312 for(i=0; i<var->cnsts_number; i++){
313 tmp += (var->cnsts[i].constraint)->lambda;
319 tmp = tmp - cnst->lambda + lambda;
321 //avoid a disaster value of lambda
322 if(tmp==0) lambda_partial = 10e-8;
324 lambda_partial += (-1.0 /tmp);
327 lambda_partial += cnst->bound;
329 return lambda_partial;