+
+/*
+ * Returns a double value corresponding to the result of a dicotomi proccess with
+ * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
+ * case of a constraint) and a initial value init.
+ *
+ * @param init initial value for \mu or \lambda
+ * @param diff a function that computes the differential of with respect a \mu or \lambda
+ * @param var_cnst a pointer to a variable or constraint
+ * @param min_erro a minimun error tolerated
+ *
+ * @return a double correponding to the result of the dicotomial process
+ */
+double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error){
+ double min, max;
+ double overall_error;
+ double middle;
+ double min_diff, max_diff, middle_diff;
+
+ min = max = init;
+
+ if(init == 0){
+ min = max = 1;
+ }
+
+ min_diff = max_diff = middle_diff = 0.0;
+ overall_error = 1;
+
+ if(diff(0.0, var_cnst) > 0){
+ DEBUG1("====> returning 0.0 (diff = %e)", diff(0.0, var_cnst));
+ return 0.0;
+ }
+
+ DEBUG0("====> not detected positive diff in 0");
+
+ while(overall_error > min_error){
+
+ min_diff = diff(min, var_cnst);
+ max_diff = diff(max, var_cnst);
+
+ DEBUG2("DICOTOMI ===> min = %e , max = %e", min, max);
+ DEBUG2("DICOTOMI ===> diffmin = %e , diffmax = %e", min_diff, max_diff);
+
+ if( min_diff > 0 && max_diff > 0 ){
+ if(min == max){
+ min = min / 2.0;
+ }else{
+ max = min;
+ }
+ }else if( min_diff < 0 && max_diff < 0 ){
+ if(min == max){
+ max = max * 2.0;
+ }else{
+ min = max;
+ }
+ }else if( min_diff < 0 && max_diff > 0 ){
+ middle = (max + min)/2.0;
+ middle_diff = diff(middle, var_cnst);
+ overall_error = fabs(min - max);
+
+ if( middle_diff < 0 ){
+ min = middle;
+ }else if( middle_diff > 0 ){
+ max = middle;
+ }else{
+ WARN0("Found an optimal solution with 0 error!");
+ overall_error = 0;
+ return middle;
+ }
+
+ }else if(min_diff == 0){
+ return min;
+ }else if(max_diff == 0){
+ return max;
+ }else if(min_diff > 0 && max_diff < 0){
+ WARN0("The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
+ }
+ }
+
+
+ DEBUG1("====> returning %e", (min+max)/2.0);
+ return ((min+max)/2.0);
+}
+
+/*
+ *
+ */
+double partial_diff_mu(double mu, void *param_var){
+ double mu_partial=0.0;
+ lmm_variable_t var = (lmm_variable_t)param_var;
+ int i;
+
+ //for each link with capacity cnsts[i] that uses flow of variable var do
+ for(i=0; i<var->cnsts_number; i++)
+ mu_partial += (var->cnsts[i].constraint)->lambda + mu;
+
+ mu_partial = (-1.0/mu_partial) + var->bound;
+
+ return mu_partial;
+}
+
+/*
+ *
+ */
+double partial_diff_lambda(double lambda, void *param_cnst){
+
+ double tmp=0.0;
+ int i;
+ xbt_swag_t elem_list = NULL;
+ lmm_element_t elem = NULL;
+ lmm_variable_t var = NULL;
+ lmm_constraint_t cnst= (lmm_constraint_t) param_cnst;
+ double lambda_partial=0.0;
+
+
+ elem_list = &(cnst->element_set);
+
+
+ DEBUG2("Computting diff of cnst (%p) %s", cnst, (char *)cnst->id);
+
+ xbt_swag_foreach(elem, elem_list) {
+ var = elem->variable;
+ if(var->weight<=0) continue;
+
+ tmp = 0;
+
+ //DEBUG2("===> Variable (%p) %s", var, (char *)var->id);
+
+ for(i=0; i<var->cnsts_number; i++){
+ tmp += (var->cnsts[i].constraint)->lambda;
+ //DEBUG1("======> lambda %e + ", (var->cnsts[i].constraint)->lambda);
+ }
+
+ if(var->bound > 0)
+ tmp += var->mu;
+
+
+ //DEBUG2("======> lambda - %e + %e ", cnst->lambda, lambda);
+
+ tmp = tmp - cnst->lambda + lambda;
+
+ //avoid a disaster value of lambda
+ //if(tmp==0) tmp = 10e-8;
+
+ lambda_partial += (-1.0/tmp);
+
+ //DEBUG1("======> %e ", (-1.0/tmp));
+ }
+
+ lambda_partial += cnst->bound;
+
+ //DEBUG1("===> %e ", lambda_partial);
+
+ return lambda_partial;
+}
+
+