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= 10;
47 double epsilon_min_error = 1e-10;
48 double overall_error = 1;
52 * Variables to manipulate the data structure proposed to model the maxmin
53 * fairness. See docummentation for more details.
55 xbt_swag_t elem_list = NULL;
56 lmm_element_t elem = NULL;
59 xbt_swag_t cnst_list = NULL;
60 lmm_constraint_t cnst = NULL;
62 xbt_swag_t var_list = NULL;
63 lmm_variable_t var = NULL;
74 DEBUG0("Iterative method configuration snapshot =====>");
75 DEBUG1("#### Maximum number of iterations : %d", max_iterations);
76 DEBUG1("#### Minimum error tolerated : %e", epsilon_min_error);
79 if ( !(sys->modified))
83 * Initialize the var list variable with only the active variables.
84 * Associate an index in the swag variables. Initialize mu.
86 var_list = &(sys->variable_set);
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);
96 DEBUG2("#### var(%d)->mu : %e", i, var->mu);
97 DEBUG2("#### var(%d)->weight: %e", i, var->weight);
104 cnst_list=&(sys->active_constraint_set);
105 xbt_swag_foreach(cnst, cnst_list) {
107 cnst->new_lambda = 2.0;
108 DEBUG2("#### cnst(%p)->lambda : %e", cnst, cnst->lambda);
112 * While doesn't reach a minimun error or a number maximum of iterations.
114 while(overall_error > epsilon_min_error && iteration < max_iterations){
119 * Compute the value of mu_i
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;
132 * Compute the value of lambda_i
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;
142 * Now computes the values of each variable (\rho) based on
143 * the values of \lambda and \mu.
146 DEBUG1("Iteration %d ", iteration);
147 xbt_swag_foreach(var, var_list) {
152 for(i=0; i<var->cnsts_number; i++){
153 tmp += (var->cnsts[i].constraint)->lambda;
158 //computes de overall_error
159 if(overall_error < fabs(var->value - 1.0/tmp)){
160 overall_error = fabs(var->value - 1.0/tmp);
163 var->value = 1.0 / tmp;
167 DEBUG2("======> value of var (%p) = %e", var, var->value);
172 //verify the KKT property for each link
173 xbt_swag_foreach(cnst, cnst_list){
175 elem_list = &(cnst->element_set);
176 xbt_swag_foreach(elem, elem_list) {
177 var = elem->variable;
178 if(var->weight<=0) continue;
182 tmp = tmp - cnst->bound;
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);
192 //verify the KKT property of each flow
193 xbt_swag_foreach(var, var_list){
194 if(var->bound <= 0 || var->weight <= 0) continue;
196 tmp = (var->value - var->bound);
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);
206 if(overall_error <= epsilon_min_error){
207 DEBUG1("The method converge in %d iterations.", iteration);
209 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;
236 DEBUG0("STARTING DICOTOMI... Debuggin, format used [min, max], [D(min),D(max)]");
238 while(overall_error > min_error){
239 DEBUG4("====> [%e, %e] , [%e,%e]", min, max, diff(min, var_cnst), diff(max, var_cnst));
241 if( diff(min, var_cnst) > 0 && diff(max, var_cnst) > 0 ){
247 }else if( diff(min, var_cnst) < 0 && diff(max, var_cnst) < 0 ){
253 }else if( diff(min, var_cnst) < 0 && diff(max, var_cnst) > 0 ){
254 middle = (max + min)/2.0;
256 if( diff(middle, var_cnst) < 0 ){
258 }else if( diff(middle, var_cnst) > 0 ){
261 WARN0("Found an optimal solution with 0 error!");
264 overall_error = fabs(min - max);
266 WARN0("The impossible happened, partial_diff(min) >0 && partial_diff(max) < 0");
270 return ((min+max)/2.0);
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;
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;
282 mu_partial = (-1.0/mu_partial) + var->bound;
288 double partial_diff_lambda(double lambda, void *param_cnst){
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;
299 elem_list = &(cnst->element_set);
301 xbt_swag_foreach(elem, elem_list) {
302 var = elem->variable;
303 if(var->weight<=0) continue;
306 for(i=0; i<var->cnsts_number; i++){
307 tmp += (var->cnsts[i].constraint)->lambda;
313 tmp = tmp - cnst->lambda + lambda;
315 //avoid a disaster value of lambda
316 if(tmp==0) lambda_partial = 10e-8;
318 lambda_partial += (-1.0 /tmp);
321 lambda_partial += cnst->bound;
323 return lambda_partial;