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_lagrangedico, surf,
25 "Logging specific to SURF (lagrange)");
28 void lagrange_dicotomi_solve(lmm_system_t sys);
30 double partial_diff_mu(double mu, lmm_variable_t var1);
31 double partial_diff_lambda(double lambda, lmm_constraint_t cnst1);
33 void lagrange_dicotomi_solve(lmm_system_t sys)
38 int max_iterations= 1000000;
39 double epsilon_min_error = 0.00001;
40 double overall_error = 1;
41 double min, max, middle;
45 * Variables to manipulate the data structure proposed to model the maxmin
46 * fairness. See docummentation for more details.
48 xbt_swag_t elem_list = NULL;
49 lmm_element_t elem1 = NULL;
52 xbt_swag_t cnst_list = NULL;
53 lmm_constraint_t cnst1 = NULL;
55 xbt_swag_t var_list = NULL;
56 lmm_variable_t var1 = 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);
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(var1, var_list) {
82 if((var1->bound > 0.0) || (var1->weight <= 0.0)){
83 DEBUG1("#### NOTE var1(%d) is a boundless variable", i);
89 DEBUG2("#### var1(%d)->mu : %e", i, var1->mu);
90 DEBUG2("#### var1(%d)->weight: %e", i, var1->weight);
97 cnst_list=&(sys->active_constraint_set);
98 xbt_swag_foreach(cnst1, cnst_list) {
100 cnst1->new_lambda = 2.0;
101 DEBUG2("#### cnst1(%p)->lambda : %e", cnst1, cnst1->lambda);
107 * While doesn't reach a minimun error or a number maximum of iterations.
109 while(overall_error > epsilon_min_error && iteration < max_iterations){
115 * Compute the value of ----------- (\lambda^k, \mu^k) this portion
117 * of code depends on function f(x).
119 var_list = &(sys->variable_set);
120 //forall mu_i in mu_1, mu_2, ..., mu_n
121 xbt_swag_foreach(var1, var_list) {
122 if((var1->bound >= 0) && (var1->weight > 0) ){
123 //for each link with capacity cnsts[i] that uses flow of variable var1 do
127 while(overall_error < epsilon_min_error){
128 if( partial_diff_mu(min, var1)>0 && partial_diff_mu(max, var1)>0 ){
134 }else if( partial_diff_mu(min, var1)<0 && partial_diff_mu(max, var1)<0 ){
140 }else if( partial_diff_mu(min,var1)<0 && partial_diff_mu(max,var1) > 0 ){
142 middle = partial_diff_mu((fabs(min - max)/2), var1);
144 max = (fabs(min - max)/2);
145 }else if( middle < 0 ){
146 min = (fabs(min - max)/2);
148 WARN0("Found an optimal solution with 0 error!");
151 overall_error = fabs(min - max);
154 WARN0("The impossible happened, partial_diff(min) >0 && partial_diff(max) < 0");
160 if(var1->new_mu < 0){
168 * Compute the value of ------------- (\lambda^k, \mu^k) this portion
170 * of code depends on function f(x).
172 xbt_swag_foreach(cnst1, cnst_list) {
177 while(overall_error < epsilon_min_error){
178 if( partial_diff_lambda(min, cnst1) > 0 && partial_diff_lambda(max, cnst1) > 0 ){
184 }else if( partial_diff_lambda(min, cnst1) < 0 && partial_diff_lambda(max, cnst1) < 0 ){
190 }else if( partial_diff_lambda(min,cnst1) < 0 && partial_diff_lambda(max,cnst1) > 0 ){
192 middle = partial_diff_lambda((fabs(min - max)/2), cnst1);
194 max = (fabs(min - max)/2);
195 }else if( middle < 0 ){
196 min = (fabs(min - max)/2);
198 WARN0("Found an optimal solution with 0 error!");
201 overall_error = fabs(min - max);
204 WARN0("The impossible happened, partial_diff(min) >0 && partial_diff(max) < 0");
207 cnst1->new_lambda = cnst1->lambda;
209 if(cnst1->new_lambda < 0){
210 cnst1->new_lambda = 0;
216 * Now computes the values of each variable (\rho) based on
217 * the values of \lambda and \mu.
220 xbt_swag_foreach(var1, var_list) {
225 for(i=0; i<var1->cnsts_number; i++){
226 tmp += (var1->cnsts[i].constraint)->lambda;
231 //computes de overall_error
232 if(overall_error < fabs(var1->value - 1.0/tmp)){
233 overall_error = fabs(var1->value - 1.0/tmp);
236 var1->value = 1.0 / tmp;
242 /* Updating lambda's and mu's */
243 xbt_swag_foreach(var1, var_list)
244 if(!((var1->bound > 0.0) || (var1->weight <= 0.0)))
245 var1->mu = var1->new_mu;
248 xbt_swag_foreach(cnst1, cnst_list)
249 cnst1->lambda = cnst1->new_lambda;
253 //verify the KKT property
254 xbt_swag_foreach(cnst1, cnst_list){
256 elem_list = &(cnst1->element_set);
257 xbt_swag_foreach(elem1, elem_list) {
258 var1 = elem1->variable;
259 if(var1->weight<=0) continue;
263 tmp = tmp - cnst1->bound;
266 if(tmp != 0 || cnst1->lambda != 0){
267 WARN4("The link %s(%p) doesn't match the KKT property, value expected (=0) got (lambda=%e) (sum_rho=%e)", (char *)cnst1->id, cnst1, cnst1->lambda, tmp);
273 xbt_swag_foreach(var1, var_list){
274 if(var1->bound <= 0 || var1->weight <= 0) continue;
276 tmp = (var1->value - var1->bound);
279 if(tmp != 0 || var1->mu != 0){
280 WARN4("The flow %s(%p) doesn't match the KKT property, value expected (=0) got (lambda=%e) (sum_rho=%e)", (char *)var1->id, var1, var1->mu, tmp);
286 if(overall_error <= epsilon_min_error){
287 DEBUG1("The method converge in %d iterations.", iteration);
289 WARN1("Method reach %d iterations, which is the maxmimun number of iterations allowed.", iteration);
298 double partial_diff_mu(double mu, lmm_variable_t var1){
299 double mu_partial=0.0;
302 //for each link with capacity cnsts[i] that uses flow of variable var1 do
303 for(i=0; i<var1->cnsts_number; i++)
304 mu_partial += (var1->cnsts[i].constraint)->lambda + mu;
306 mu_partial = (-1.0/mu_partial) + var1->bound;
312 double partial_diff_lambda(double lambda, lmm_constraint_t cnst1){
316 double lambda_partial=0.0;
317 xbt_swag_t elem_list = NULL;
318 lmm_element_t elem1 = NULL;
319 lmm_variable_t var1 = NULL;
322 elem_list = &(cnst1->element_set);
324 xbt_swag_foreach(elem1, elem_list) {
325 var1 = elem1->variable;
326 if(var1->weight<=0) continue;
329 for(i=0; i<var1->cnsts_number; i++){
330 tmp += (var1->cnsts[i].constraint)->lambda;
336 tmp = tmp - cnst1->lambda + lambda;
338 //un peux du bricolage pour evite la catastrophe
339 if(tmp==0) lambda_partial = 10e-8;
341 lambda_partial += (-1.0 /tmp);
344 return lambda_partial;