wip
[oweals/gnunet.git] / src / transport / gnunet-service-transport.c
index 7a2d1a647dc3a05b1d5ca9d69209aa0ad536580a..49f9531df6e69e6caffd83b3c564ab08036c9ee9 100644 (file)
 
 #define VERY_BIG_DOUBLE_VALUE 100000000000LL
 
+#define ATS_NEW 0
+#define ATS_Q_UPDATED 1
+#define ATS_C_UPDATED 2
+#define ATS_QC_UPDATED 3
+#define ATS_UNMODIFIED 4
+
 /**
  * List of addresses of other peers
  */
@@ -875,19 +881,54 @@ struct ATS_peer
        int     t;
 };
 
-struct ATS_result
+struct ATS_stat
 {
+       /**
+        * result of last GLPK run
+        * 5 == OPTIMAL
+        */
+       int solution;
+
+       /**
+        * is problem currently valid and can it be solved
+        */
+       int valid;
+
+       /**
+        * Number of transport mechanisms in the problem
+        */
        int c_mechs;
+
+       /**
+        * Number of transport mechanisms in the problem
+        */
        int c_peers;
 
+       /**
+        * row index where quality related rows start
+        */
        int begin_qm;
+
+       /**
+        * row index where quality related rows end
+        */
        int end_qm;
 
+       /**
+        * row index where ressource cost related rows start
+        */
        int begin_cr;
+
+       /**
+        * row index where ressource cost related rows end
+        */
        int end_cr;
 
-       int solution;
-       int valid;
+       int col_d;
+       int col_u;
+       int col_r;
+       int col_qm;
+       int col_cr;
 };
 
 struct ATS_ressource_entry
@@ -955,49 +996,118 @@ struct ATS_info
        /**
         * Regular intervall when execution is triggered
         */
-       struct GNUNET_TIME_Relative exec_intervall;
+       struct GNUNET_TIME_Relative exec_interval;
        /**
         * Maximum execution time per calculation
         */
        struct GNUNET_TIME_Relative max_exec_duration;
+
+#if HAVE_LIBGLPK
+       /**
+        * GLPK (MLP) problem object
+        */
+       glp_prob *prob;
+#endif
+
+       /**
+        * task to recalculate the bandwidth assignment
+        */
+       GNUNET_SCHEDULER_TaskIdentifier ats_task;
+
+       /**
+        * Current state of the GLPK problem
+        */
+       struct ATS_stat stat;
+
+       /**
+        * mechanisms used in current problem
+        * needed for problem modification
+        */
+       struct ATS_mechanism * mechanisms;
+
+       /**
+        * peers used in current problem
+        * needed for problem modification
+        */
+       struct ATS_peer * peers;
+
        /**
         * Maximum number of LP iterations per calculation
         */
        int max_iterations;
 
+       /**
+        * Dump problem to a file?
+        */
        int save_mlp;
+
+       /**
+        * Dump solution to a file
+        */
        int save_solution;
 
        /**
-        * Ressource costs or quality metrics changed, update matrix
+        * Dump solution when minimum peers:
+        */
+       int dump_min_peers;
+
+       /**
+        * Dump solution when minimum addresses:
+        */
+       int dump_min_addr;
+
+       /**
+        * Dump solution overwrite file:
+        */
+       int dump_overwrite;
+
+       /**
+        * Ressource costs or quality metrics changed
+        * update problem before solving
         */
        int modified_resources;
 
        /**
         * Ressource costs or quality metrics changed, update matrix
+        * update problem before solving
         */
        int modified_quality;
 
        /**
-        * Peers have connected or disconnected, problem has to be recreated
+        * Peers have connected or disconnected
+        * problem has to be recreated
         */
        int modified_addr;
 
+       /**
+        * Was the available basis invalid and we needed to rerun simplex?
+        */
+       int simplex_rerun_required;
+
+       /**
+        * Diversity weight
+        */
        double D;
+
+       /**
+        * Utility weight
+        */
        double U;
-       double R;
-       int v_b_min;
-       int v_n_min;
 
-       GNUNET_SCHEDULER_TaskIdentifier ats_task;
+       /**
+        * Relativity weight
+        */
+       double R;
 
-       struct ATS_result res;
-#if HAVE_LIBGLPK
-       glp_prob *prob;
-#endif
+       /**
+        * Minimum bandwidth per peer
+        */
+       int v_b_min;
 
-       struct ATS_mechanism * mechanisms;
-       struct ATS_peer * peers;
+       /**
+        * Minimum number of connections per peer
+        */
+       int v_n_min;
 };
 
 
@@ -1616,7 +1726,7 @@ transmit_to_client (struct TransportClient *client,
   GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
                                     client->message_queue_tail,
                                     client->message_queue_tail,
-                                    q);                                
+                                    q);
   client->message_count++;
   if (client->th == NULL)
     {
@@ -2197,7 +2307,7 @@ expire_address_task (void *cls,
  *        expired
  */
 static void
-update_addresses (struct TransportPlugin *plugin, 
+update_addresses (struct TransportPlugin *plugin,
                  int fresh)
 {
   static struct GNUNET_TIME_Absolute last_update;
@@ -2331,7 +2441,7 @@ try_fast_reconnect (struct TransportPlugin *p,
      Furthermore, the same mechanism (or small variation) could be used
      to switch to a better-performing plugin (ATS).
 
-     Finally, this needs to be tested throughly... */                                                          
+     Finally, this needs to be tested throughly... */
 
   /*
    * GNUNET_NO in the call below makes transport disconnect the peer,
@@ -2414,6 +2524,8 @@ plugin_env_session_end  (void *cls,
       GNUNET_SCHEDULER_cancel (pos->revalidate_task);
       pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
     }
+  GNUNET_free_non_null(pos->ressources);
+  GNUNET_free_non_null(pos->quality);
   GNUNET_free (pos);
   if (nl->received_pong == GNUNET_NO)
     return; /* nothing to do, never connected... */
@@ -2473,9 +2585,9 @@ plugin_env_notify_address (void *cls,
   al = p->addresses;
   while (al != NULL)
     {
-      if ( (addrlen == al->addrlen) && 
+      if ( (addrlen == al->addrlen) &&
           (0 == memcmp (addr, &al[1], addrlen)) )
-        {            
+        {
          al->expires = abex;
          update_addresses (p, GNUNET_NO);
           return;
@@ -3352,7 +3464,7 @@ do_blacklist_check (void *cls,
   if (bl == NULL)
     {
       bc->cont (bc->cont_cls,
-               setup_new_neighbour (&bc->peer, bc->do_hello));         
+               setup_new_neighbour (&bc->peer, bc->do_hello));
       GNUNET_free (bc);
       return;
     }
@@ -3564,7 +3676,7 @@ send_periodic_ping (void *cls,
   size_t tsize;
 
   peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
-  if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
+  if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
     return;
   tp = peer_address->ready_list->plugin;
   neighbour = peer_address->ready_list->neighbour;
@@ -3730,7 +3842,7 @@ schedule_next_ping (struct ForeignAddressList *fal)
     {
       delay = GNUNET_TIME_UNIT_ZERO;
       fal->estimated = GNUNET_YES;
-    }                          
+    }
   if (GNUNET_YES == fal->connected)
     {
       delay = GNUNET_TIME_relative_min (delay,
@@ -3792,14 +3904,14 @@ handle_payload_message (const struct GNUNET_MessageHeader *message,
     {
       n->quota_violation_count++;
 #if DEBUG_TRANSPORT
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,                     
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
                  n->in_tracker.available_bytes_per_s__,
                  n->quota_violation_count);
 #endif
       /* Discount 32k per violation */
       GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
-                                       - 32 * 1024);           
+                                       - 32 * 1024);
     }
   else
     {
@@ -3971,7 +4083,7 @@ check_pending_validation (void *cls,
                      a2s (ve->transport_name,
                           &addr[slen],
                           alen));
-         return GNUNET_NO;     
+         return GNUNET_NO;
        }
       if (GNUNET_OK !=
          GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
@@ -4443,7 +4555,7 @@ check_hello_validated (void *cls,
          GNUNET_CONTAINER_DLL_remove (chvc_head,
                                       chvc_tail,
                                       chvc);
-         GNUNET_free (chvc);   
+         GNUNET_free (chvc);
        }
       return;
     }
@@ -4734,8 +4846,10 @@ disconnect_neighbour (struct NeighbourList *n, int check)
              peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
            }
                  GNUNET_free(peer_pos->ressources);
+                 peer_pos->ressources = NULL;
                  GNUNET_free(peer_pos->quality);
-          GNUNET_free(peer_pos);
+                 peer_pos->ressources = NULL;
+                 GNUNET_free(peer_pos);
         }
       GNUNET_free (rpos);
     }
@@ -4962,7 +5076,7 @@ handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
          GNUNET_assert (GNUNET_OK ==
                         GNUNET_CRYPTO_rsa_sign (my_private_key,
                                                 &pong->purpose,
-                                                &oal->pong_signature));        
+                                                &oal->pong_signature));
          memcpy (&pong->signature,
                  &oal->pong_signature,
                  sizeof (struct GNUNET_CRYPTO_RsaSignature));
@@ -4974,7 +5088,7 @@ handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
          GNUNET_assert (GNUNET_OK ==
                         GNUNET_CRYPTO_rsa_sign (my_private_key,
                                                 &pong->purpose,
-                                                &pong->signature));    
+                                                &pong->signature));
        }
       else
        {
@@ -5103,9 +5217,11 @@ plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
                  distance = ntohl(ats_data[c].value);
          }
   }
+
   /* notify ATS about incoming data */
   //ats_notify_ats_data(peer, ats_data);
 
+
   if (message != NULL)
     {
       if ( (session != NULL) ||
@@ -5132,6 +5248,7 @@ plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
        }
       /* update traffic received amount ... */
       msize = ntohs (message->size);
+
       GNUNET_STATISTICS_update (stats,
                                gettext_noop ("# bytes received from other peers"),
                                msize,
@@ -5159,6 +5276,27 @@ plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
                                    GNUNET_NO);
          return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
        }
+    if ((ntohs(message->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ATS) &&
+       (ntohs(message->size) == (sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t))))
+    {
+       uint32_t value =  ntohl(*((uint32_t *) &message[1]));
+       //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GNUNET_MESSAGE_TYPE_TRANSPORT_ATS: %i \n", value);
+       /* Force ressource and quality update */
+       if (value == 4)
+       {
+               ats->modified_resources = GNUNET_YES;
+               ats->modified_quality = GNUNET_YES;
+       }
+       /* Force cost update */
+       if (value == 3)
+               ats->modified_resources = GNUNET_YES;
+       /* Force quality update */
+       if (value == 2)
+               ats->modified_quality = GNUNET_YES;
+       /* Force full rebuild */
+       if (value == 1)
+               ats->modified_addr = GNUNET_YES;
+    }
 
 #if DEBUG_PING_PONG
           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -5182,6 +5320,8 @@ plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
        case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
          handle_pong (plugin, message, peer, sender_address, sender_address_len);
          break;
+       case GNUNET_MESSAGE_TYPE_TRANSPORT_ATS:
+         break;
        default:
          handle_payload_message (message, n);
          break;
@@ -5689,7 +5829,7 @@ client_disconnect_notification (void *cls,
                  if (bc->th != NULL)
                    {
                      GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
-                     bc->th = NULL;            
+                     bc->th = NULL;
                    }
                  if (bc->task == GNUNET_SCHEDULER_NO_TASK)
                    bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
@@ -5848,11 +5988,11 @@ static int ats_evaluate_results (int result, int solution, char * problem)
                GNUNET_log (error_kind, "%s , Search terminated by application ", problem);
                break;
        case GLP_EITLIM :    /* iteration limit exceeded */
-               GNUNET_log (error_kind, "%s Iteration limit exceeded ", problem);
+               GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s Iteration limit exceeded ", problem);
                break;
        break;
        case GLP_ETMLIM :    /* time limit exceeded */
-               GNUNET_log (error_kind, "%s Time limit exceeded ", problem);
+               GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s Time limit exceeded ", problem);
        break;
        case GLP_ENOPFS :    /* no primal feasible solution */
        case GLP_ENODFS :    /* no dual feasible solution */
@@ -5901,68 +6041,94 @@ static int ats_evaluate_results (int result, int solution, char * problem)
 return cont;
 }
 
-
-static void ats_solve_problem (unsigned int max_it, unsigned int  max_dur, unsigned int c_peers, unsigned int  c_mechs, struct ATS_result *res)
+static void ats_solve_problem (unsigned int max_it, unsigned int  max_dur, unsigned int c_peers, unsigned int  c_mechs, struct ATS_stat *stat)
 {
        int result;
-       int solution;
+       int lp_solution;
+       int mlp_solution;
 
        // Solving simplex
-       glp_prob *prob = ats->prob;
 
        glp_smcp opt_lp;
        glp_init_smcp(&opt_lp);
-
 #if VERBOSE_ATS
        opt_lp.msg_lev = GLP_MSG_ALL;
 #else
        opt_lp.msg_lev = GLP_MSG_OFF;
 #endif
-       //opt_lp.presolve = GLP_ON;
-       result = glp_simplex(prob, &opt_lp);
-       solution =  glp_get_status (prob);
 
-       if (GNUNET_YES == ats_evaluate_results(result, solution, "LP"))
+       // setting iteration limit
+       opt_lp.it_lim = max_it;
+       // maximum duration
+       opt_lp.tm_lim = max_dur;
+
+       if (ats->modified_addr == GNUNET_YES)
+               opt_lp.presolve = GLP_ON;
+       result = glp_simplex(ats->prob, &opt_lp);
+       lp_solution =  glp_get_status (ats->prob);
+
+       if ((result == GLP_ETMLIM) || (result == GLP_ETMLIM))
+       {
+               ats->stat.valid = GNUNET_NO;
+               GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS exceeded time or iteration limit!\n");
+               return;
+       }
+
+       if (ats_evaluate_results(result, lp_solution, "LP") == GNUNET_YES)
        {
-               /* Solving mlp */
-               glp_iocp opt_mlp;
-               glp_init_iocp(&opt_mlp);
-               /* maximum duration */
-               //opt_mlp.presolve = GLP_ON;
-               opt_mlp.tm_lim = max_dur;
-               /* output level */
+                       stat->valid = GNUNET_YES;
+       }
+       else
+       {
+               ats->simplex_rerun_required = GNUNET_YES;
+               opt_lp.presolve = GLP_ON;
+               result = glp_simplex(ats->prob, &opt_lp);
+               lp_solution =  glp_get_status (ats->prob);
+
+               // TODO: Remove if this does not appear until release
+               GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "EXECUTED SIMPLEX WITH PRESOLVER! %i", lp_solution);
+
+               if (ats_evaluate_results(result, lp_solution, "LP") != GNUNET_YES)
+               {
+                       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "After execution simplex with presolver: STILL INVALID!\n");
+                       char * filename;
+                       GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%llu.mlp",ats->stat.c_peers, ats->stat.c_mechs, GNUNET_TIME_absolute_get().abs_value);
+                       glp_write_lp (ats->prob, NULL, filename);
+                       GNUNET_free (filename);
+                       stat->valid = GNUNET_NO;
+                       return;
+               }
+               stat->valid = GNUNET_YES;
+       }
+
+       // Solving mlp
+       glp_iocp opt_mlp;
+       glp_init_iocp(&opt_mlp);
+       // maximum duration
+       opt_mlp.tm_lim = max_dur;
+       // output level
 #if VERBOSE_ATS
-                       opt_mlp.msg_lev = GLP_MSG_ALL;
+       opt_mlp.msg_lev = GLP_MSG_ALL;
 #else
-                       opt_mlp.msg_lev = GLP_MSG_OFF;
+       opt_mlp.msg_lev = GLP_MSG_OFF;
 #endif
-               result = glp_intopt (prob, &opt_mlp);
-               solution =  glp_mip_status (prob);
-               res->solution = solution;
-               res->valid = GNUNET_NO;
-               if (ats_evaluate_results(result, solution, "MLP") == GNUNET_YES)
-                       res->valid = GNUNET_YES;
-       /* done */
-       }
 
-       if ((ats->save_mlp == GNUNET_YES) && (c_peers > 1))
+       result = glp_intopt (ats->prob, &opt_mlp);
+       mlp_solution =  glp_mip_status (ats->prob);
+       stat->solution = mlp_solution;
+
+       if (ats_evaluate_results(result, mlp_solution, "MLP") == GNUNET_YES)
        {
-               char * filename;
-               GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%llu.mlp",c_peers, c_mechs, GNUNET_TIME_absolute_get().abs_value);
-               //if (GNUNET_NO == GNUNET_DISK_file_test(filename))
-                       glp_write_lp (prob, NULL, filename);
-               GNUNET_free (filename);
+               stat->valid = GNUNET_YES;
        }
-       if ((ats->save_solution == GNUNET_YES) && (c_peers > 1))
+       else
        {
-               char * filename;
-               GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%llu.sol",c_peers, c_mechs, GNUNET_TIME_absolute_get().abs_value);
-               //if (GNUNET_NO == GNUNET_DISK_file_test(filename))
-                       glp_print_sol (prob, filename);
-               GNUNET_free (filename);
+               // TODO: Remove if this does not appear until release
+               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,  "MLP SOLUTION INVALID: %i\n", lp_solution);
+               stat->valid = GNUNET_NO;
        }
 
-       /*
+/*
        int check;
        int error = GNUNET_NO;
        double bw;
@@ -5994,16 +6160,16 @@ static void ats_solve_problem (unsigned int max_it, unsigned int  max_dur, unsig
        }*/
 
 #if VERBOSE_ATS
-       if (glp_get_col_prim(prob,2*c_mechs+1) != 1)
+       if (glp_get_col_prim(ats->prob,2*c_mechs+1) != 1)
        {
        int c;
        for (c=1; c<= available_quality_metrics; c++ )
        {
-               GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+3+c), glp_get_col_prim(prob,2*c_mechs+3+c));
+               GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(ats->prob,2*c_mechs+3+c), glp_get_col_prim(ats->prob,2*c_mechs+3+c));
        }
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+1), glp_get_col_prim(prob,2*c_mechs+1));
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+2), glp_get_col_prim(prob,2*c_mechs+2));
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+3), glp_get_col_prim(prob,2*c_mechs+3));
+       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(ats->prob,2*c_mechs+1), glp_get_col_prim(ats->prob,2*c_mechs+1));
+       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(ats->prob,2*c_mechs+2), glp_get_col_prim(ats->prob,2*c_mechs+2));
+       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(ats->prob,2*c_mechs+3), glp_get_col_prim(ats->prob,2*c_mechs+3));
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "objective value:  %f\n", glp_mip_obj_val(ats->prob));
        }
 #endif
@@ -6013,7 +6179,7 @@ static void ats_delete_problem ()
 {
        int c;
 
-       for (c=0; c< (ats->res).c_mechs; c++)
+       for (c=0; c< (ats->stat).c_mechs; c++)
                GNUNET_free_non_null (ats->mechanisms[c].rc);
 
        if (ats->mechanisms!=NULL)
@@ -6033,16 +6199,17 @@ static void ats_delete_problem ()
                ats->prob = NULL;
        }
 
-       ats->res.begin_cr = GNUNET_SYSERR;
-       ats->res.begin_qm = GNUNET_SYSERR;
-       ats->res.c_mechs = 0;
-       ats->res.c_peers = 0;
-       ats->res.end_cr = GNUNET_SYSERR;
-       ats->res.end_qm = GNUNET_SYSERR;
-       ats->res.solution = GNUNET_SYSERR;
-       ats->res.valid = GNUNET_SYSERR;
+       ats->stat.begin_cr = GNUNET_SYSERR;
+       ats->stat.begin_qm = GNUNET_SYSERR;
+       ats->stat.c_mechs = 0;
+       ats->stat.c_peers = 0;
+       ats->stat.end_cr = GNUNET_SYSERR;
+       ats->stat.end_qm = GNUNET_SYSERR;
+       ats->stat.solution = GNUNET_SYSERR;
+       ats->stat.valid = GNUNET_SYSERR;
 }
 
+
 static void ats_update_problem_qm ()
 {
        int array_index;
@@ -6050,11 +6217,12 @@ static void ats_update_problem_qm ()
        int c, c2;
        int c_q_metrics = available_quality_metrics;
 
-       int *ja    = GNUNET_malloc ((1 + ats->res.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
-       double *ar = GNUNET_malloc ((1 + ats->res.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
-       if (ats->res.c_mechs>10)
+       int *ja    = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
+       double *ar = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
+#if DEBUG_ATS
                GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics\n");
-       row_index = ats->res.begin_qm;
+#endif
+       row_index = ats->stat.begin_qm;
 
        for (c=1; c <= c_q_metrics; c++)
        {
@@ -6065,7 +6233,7 @@ static void ats_update_problem_qm ()
 #endif
 
                glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
-               for (c2=1; c2<=ats->res.c_mechs; c2++)
+               for (c2=1; c2<=ats->stat.c_mechs; c2++)
                {
                        ja[array_index] = c2;
 
@@ -6102,7 +6270,7 @@ static void ats_update_problem_qm ()
 #endif
                        array_index++;
                }
-               ja[array_index] = (2*ats->res.c_mechs) + 3 +c;
+               ja[array_index] = ats->stat.col_qm + c - 1;
                ar[array_index] = -1;
 
 #if VERBOSE_ATS
@@ -6119,7 +6287,6 @@ static void ats_update_problem_qm ()
 }
 
 
-
 static void ats_update_problem_cr ()
 {
 
@@ -6128,11 +6295,11 @@ static void ats_update_problem_cr ()
        int c, c2;
        double ct_max, ct_min;
 
-       int *ja    = GNUNET_malloc ((1 + ats->res.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
-       double *ar = GNUNET_malloc ((1 + ats->res.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
+       int *ja    = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
+       double *ar = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
 
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics\n");
-       row_index = ats->res.begin_cr;
+       row_index = ats->stat.begin_cr;
 
        for (c=0; c<available_ressources; c++)
        {
@@ -6143,14 +6310,14 @@ static void ats_update_problem_cr ()
 #endif
                glp_set_row_bnds(ats->prob, row_index, GLP_DB, ct_min, ct_max);
 
-               for (c2=1; c2<=ats->res.c_mechs; c2++)
+               for (c2=1; c2<=ats->stat.c_mechs; c2++)
                {
                        double value = 0;
                        ja[array_index] = c2;
                        value = ats->mechanisms[c2].addr->ressources[c].c;
                        ar[array_index] = value;
 #if VERBOSE_ATS
-                       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, rowrow_indexs, ja[array_index], ar[array_index]);
+                       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, row_index, ja[array_index], ar[array_index]);
 #endif
                        array_index++;
                }
@@ -6165,6 +6332,60 @@ static void ats_update_problem_cr ()
 }
 
 
+#if 0
+static void ats_update_problem_qm_TEST ()
+{
+       int row_index;
+       int c, c2;
+
+       int old_ja[ats->stat.c_mechs + 2];
+       double old_ar[ats->stat.c_mechs + 2];
+       int c_old;
+       int changed = 0;
+
+       int *ja    = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
+       double *ar = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
+#if DEBUG_ATS
+       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics TEST\n");
+#endif
+       if (ats->stat.begin_qm >0)
+               row_index = ats->stat.begin_qm;
+       else
+               return;
+
+
+       for (c=0; c<available_quality_metrics; c++)
+       {
+
+               c_old = glp_get_mat_row (ats->prob, row_index, old_ja, old_ar);
+
+               glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
+
+               for (c2=1; c2<=c_old; c2++)
+               {
+                       ja[c2] = old_ja[c2];
+                       if ((changed < 3) && (c2>2) && (old_ar[c2] != -1))
+                       {
+                               ar[c2] = old_ar[c2] + 5 - changed;
+                               changed ++;
+                       }
+                       else
+                               ar[c2] = old_ar[c2];
+#if VERBOSE_ATS
+                       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: old [%i,%i]=%f  new [%i,%i]=%f\n",c2, row_index, old_ja[c2], old_ar[c2], row_index, ja[c2], ar[c2]);
+#endif
+               }
+               glp_set_mat_row (ats->prob, row_index, c_old, ja, ar);
+
+               row_index ++;
+       }
+
+       GNUNET_free_non_null (ja);
+       GNUNET_free_non_null (ar);
+}
+#endif
+
+
 /** solve the bandwidth distribution problem
  * @param max_it maximum iterations
  * @param max_dur maximum duration in ms
@@ -6173,10 +6394,10 @@ static void ats_update_problem_cr ()
  * @param R weight for relativity
  * @param v_b_min minimal bandwidth per peer
  * @param v_n_min minimum number of connections
- * @param res result struct
+ * @param stat result struct
  * @return GNUNET_SYSERR if glpk is not available, number of mechanisms used
  */
-static int ats_create_problem (double D, double U, double R, int v_b_min, int v_n_min, struct ATS_result *res)
+static int ats_create_problem (double D, double U, double R, int v_b_min, int v_n_min, struct ATS_stat *stat)
 {
        if (ats->prob != NULL)
                glp_delete_prob(ats->prob);
@@ -6220,9 +6441,9 @@ static int ats_create_problem (double D, double U, double R, int v_b_min, int v_
 #if DEBUG_ATS
                GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No addresses for bw distribution available\n", c_peers);
 #endif
-               res->valid = GNUNET_NO;
-               res->c_peers = 0;
-               res->c_mechs = 0;
+               stat->valid = GNUNET_NO;
+               stat->c_peers = 0;
+               stat->c_mechs = 0;
                return GNUNET_SYSERR;
        }
 
@@ -6393,7 +6614,7 @@ static int ats_create_problem (double D, double U, double R, int v_b_min, int v_
        double ct_max = VERY_BIG_DOUBLE_VALUE;
        double ct_min = 0.0;
 
-       res->begin_cr = array_index;
+       stat->begin_cr = array_index;
 
        for (c=0; c<available_ressources; c++)
        {
@@ -6418,7 +6639,7 @@ static int ats_create_problem (double D, double U, double R, int v_b_min, int v_
                }
                row_index ++;
        }
-       res->end_cr = array_index--;
+       stat->end_cr = array_index--;
 
        /* Constraint 5: min number of connections*/
        glp_add_rows(ats->prob, 1);
@@ -6445,10 +6666,13 @@ static int ats_create_problem (double D, double U, double R, int v_b_min, int v_
        // adding columns
 
        // Constraint 6: optimize for diversity
-       glp_add_cols(ats->prob, 1);
-       glp_set_col_name(ats->prob, (2*c_mechs) + 1, "d");
-       glp_set_obj_coef(ats->prob, (2*c_mechs) + 1, D);
-       glp_set_col_bnds(ats->prob, (2*c_mechs) + 1, GLP_LO, 0.0, 0.0);
+       int col_d;
+       col_d = glp_add_cols(ats->prob, 1);
+       stat->col_d = col_d;
+       //GNUNET_assert (col_d == (2*c_mechs) + 1);
+       glp_set_col_name(ats->prob, col_d, "d");
+       glp_set_obj_coef(ats->prob, col_d, D);
+       glp_set_col_bnds(ats->prob, col_d, GLP_LO, 0.0, 0.0);
        glp_add_rows(ats->prob, 1);
 #if VERBOSE_ATS
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
@@ -6466,89 +6690,30 @@ static int ats_create_problem (double D, double U, double R, int v_b_min, int v_
                array_index++;
        }
        ia[array_index] = row_index;
-       ja[array_index] = (2*c_mechs) + 1;
-       ar[array_index] = -1;
-#if VERBOSE_ATS
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
-#endif
-       array_index++;
-       row_index ++;
-
-       // Constraint 8: optimize bandwidth utility
-       glp_add_cols(ats->prob, 1);
-       glp_set_col_name(ats->prob, (2*c_mechs) + 2, "u");
-       glp_set_obj_coef(ats->prob, (2*c_mechs) + 2, U);
-       glp_set_col_bnds(ats->prob, (2*c_mechs) + 2, GLP_LO, 0.0, 0.0);
-       glp_add_rows(ats->prob, 1);
-#if VERBOSE_ATS
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
-#endif
-       glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
-       for (c=1; c<=c_mechs; c++)
-       {
-               ia[array_index] = row_index;
-               ja[array_index] = c;
-               ar[array_index] = mechanisms[c].peer->f;
-#if VERBOSE_ATS
-               GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
-#endif
-               array_index++;
-       }
-       ia[array_index] = row_index;
-       ja[array_index] = (2*c_mechs) + 2;
+       ja[array_index] = col_d;
        ar[array_index] = -1;
 #if VERBOSE_ATS
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
 #endif
-
        array_index++;
        row_index ++;
 
-       // Constraint 9: optimize relativity
-       glp_add_cols(ats->prob, 1);
-       glp_set_col_name(ats->prob, (2*c_mechs) + 3, "r");
-       glp_set_obj_coef(ats->prob, (2*c_mechs) + 3, R);
-       glp_set_col_bnds(ats->prob, (2*c_mechs) + 3, GLP_LO, 0.0, 0.0);
-       glp_add_rows(ats->prob, c_peers);
-       for (c=1; c<=c_peers; c++)
-       {
-               glp_set_row_bnds(ats->prob, row_index, GLP_LO, 0.0, 0.0);
-
-               struct ATS_mechanism *m = peers[c].m_head;
-               while (m!=NULL)
-               {
-                       ia[array_index] = row_index;
-                       ja[array_index] = m->col_index;
-                       ar[array_index] = 1 / mechanisms[c].peer->f;
-#if VERBOSE_ATS
-                       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
-#endif
-                       array_index++;
-                       m = m->next;
-               }
-               ia[array_index] = row_index;
-               ja[array_index] = (2*c_mechs) + 3;
-               ar[array_index] = -1;
-#if VERBOSE_ATS
-               GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
-#endif
-               array_index++;
-
-               row_index++;
-       }
 
        // Constraint 7: optimize for quality
-       glp_add_cols(ats->prob, c_q_metrics);
-       for (c=1; c<= c_q_metrics; c++)
+       int col_qm;
+       col_qm = glp_add_cols(ats->prob, c_q_metrics);
+       stat->col_qm = col_qm;
+       //GNUNET_assert (col_qm == (2*c_mechs) + 3 + 1);
+       for (c=0; c< c_q_metrics; c++)
        {
-               GNUNET_asprintf(&name, "Q_%s",qm[c-1].name);
-               glp_set_col_name(ats->prob, (2*c_mechs) + 3 + c, name);
-               glp_set_col_bnds(ats->prob, (2*c_mechs) + 3 + c, GLP_LO, 0.0, 0.0);
+               GNUNET_asprintf(&name, "Q_%s",qm[c].name);
+               glp_set_col_name(ats->prob, col_qm + c, name);
+               glp_set_col_bnds(ats->prob, col_qm + c, GLP_LO, 0.0, 0.0);
                GNUNET_free (name);
-               glp_set_obj_coef(ats->prob, (2*c_mechs) + 3 + c, Q[c]);
+               glp_set_obj_coef(ats->prob, col_qm + c, Q[c]);
        }
     glp_add_rows(ats->prob, available_quality_metrics);
-       res->begin_qm = row_index;
+       stat->begin_qm = row_index;
        for (c=1; c <= c_q_metrics; c++)
        {
 #if VERBOSE_ATS
@@ -6596,7 +6761,7 @@ static int ats_create_problem (double D, double U, double R, int v_b_min, int v_
                }
 
                ia[array_index] = row_index;
-               ja[array_index] = (2*c_mechs) + 3 +c;
+               ja[array_index] = col_qm + c - 1;
                ar[array_index] = -1;
 #if VERBOSE_ATS
                GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
@@ -6604,15 +6769,84 @@ static int ats_create_problem (double D, double U, double R, int v_b_min, int v_
                array_index++;
                row_index++;
        }
-       res->end_qm = row_index-1;
+       stat->end_qm = row_index-1;
+
+       // Constraint 8: optimize bandwidth utility
+       int col_u;
+       col_u = glp_add_cols(ats->prob, 1);
+       stat->col_u = col_u;
+       //GNUNET_assert (col_u == (2*c_mechs) + 2);
+       glp_set_col_name(ats->prob, col_u, "u");
+       glp_set_obj_coef(ats->prob, col_u, U);
+       glp_set_col_bnds(ats->prob, col_u, GLP_LO, 0.0, 0.0);
+       glp_add_rows(ats->prob, 1);
+#if VERBOSE_ATS
+       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
+#endif
+       glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
+       for (c=1; c<=c_mechs; c++)
+       {
+               ia[array_index] = row_index;
+               ja[array_index] = c;
+               ar[array_index] = mechanisms[c].peer->f;
+#if VERBOSE_ATS
+               GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
+#endif
+               array_index++;
+       }
+       ia[array_index] = row_index;
+       ja[array_index] = col_u;
+       ar[array_index] = -1;
+#if VERBOSE_ATS
+       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
+#endif
+
+       array_index++;
+       row_index ++;
+
+       // Constraint 9: optimize relativity
+       int col_r;
+       col_r = glp_add_cols(ats->prob, 1);
+       stat->col_r = col_r;
+       //GNUNET_assert (col_r == (2*c_mechs) + 3);
+       glp_set_col_name(ats->prob, col_r, "r");
+       glp_set_obj_coef(ats->prob, col_r, R);
+       glp_set_col_bnds(ats->prob, col_r, GLP_LO, 0.0, 0.0);
+       glp_add_rows(ats->prob, c_peers);
+       for (c=1; c<=c_peers; c++)
+       {
+               glp_set_row_bnds(ats->prob, row_index, GLP_LO, 0.0, 0.0);
+
+               struct ATS_mechanism *m = peers[c].m_head;
+               while (m!=NULL)
+               {
+                       ia[array_index] = row_index;
+                       ja[array_index] = m->col_index;
+                       ar[array_index] = 1 / mechanisms[c].peer->f;
+#if VERBOSE_ATS
+                       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
+#endif
+                       array_index++;
+                       m = m->next;
+               }
+               ia[array_index] = row_index;
+               ja[array_index] = col_r;
+               ar[array_index] = -1;
+#if VERBOSE_ATS
+               GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
+#endif
+               array_index++;
+
+               row_index++;
+       }
 
        /* Loading the matrix */
        glp_load_matrix(ats->prob, array_index-1, ia, ja, ar);
 
-       res->c_mechs = c_mechs;
-       res->c_peers = c_peers;
-       res->solution = 0;
-       res->valid = GNUNET_YES;
+       stat->c_mechs = c_mechs;
+       stat->c_peers = c_peers;
+       stat->solution = 0;
+       stat->valid = GNUNET_YES;
 
        /* clean up */
 
@@ -6623,7 +6857,7 @@ static int ats_create_problem (double D, double U, double R, int v_b_min, int v_
        return GNUNET_OK;
 
 }
-#if 0
+
 void ats_notify_ats_data (
                const struct GNUNET_PeerIdentity *peer,
                const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
@@ -6631,19 +6865,20 @@ void ats_notify_ats_data (
 #if DEBUG_ATS
        GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ATS_notify_ats_data: %s\n",GNUNET_i2s(peer));
 #endif
-       ats_calculate_bandwidth_distribution(ats);
+       ats_calculate_bandwidth_distribution();
 }
-#endif
-#endif
+#endif //END: HAVE_LIBGLPK
 
 static void
 ats_calculate_bandwidth_distribution ()
 {
 #if HAVE_LIBGLPK
        struct GNUNET_TIME_Absolute start;
-       struct GNUNET_TIME_Relative duration;
+       struct GNUNET_TIME_Relative creation;
+       struct GNUNET_TIME_Relative solving;
+       char *text = "unmodified";
 
-       struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(ats->last,GNUNET_TIME_absolute_get());
+       struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference (ats->last, GNUNET_TIME_absolute_get());
        if (delta.rel_value < ats->min_delta.rel_value)
        {
 #if DEBUG_ATS
@@ -6657,76 +6892,122 @@ ats_calculate_bandwidth_distribution ()
                dur = INT_MAX;
        else
                dur = (int) ats->max_exec_duration.rel_value;
+       ats->simplex_rerun_required = GNUNET_NO;
 
        start = GNUNET_TIME_absolute_get();
        if ((ats->modified_addr == GNUNET_YES) || (ats->prob==NULL))
        {
-               ats->modified_addr = GNUNET_NO;
+               text = "new";
+               ats->modified_addr = GNUNET_YES;
                ats_delete_problem ();
-               ats_create_problem (ats->D, ats->U, ats->R, ats->v_b_min, ats->v_n_min, &ats->res);
+               ats_create_problem (ats->D, ats->U, ats->R, ats->v_b_min, ats->v_n_min, &ats->stat);
 #if DEBUG_ATS
-               GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers/Addresses were modified... new problem: %i peer, %i mechs\n", ats->res.c_peers, ats->res.c_mechs);
+               GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers/Addresses were modified... new problem: %i peer, %i mechs\n", ats->stat.c_peers, ats->stat.c_mechs);
 #endif
        }
-       else if (ats->modified_resources == GNUNET_YES)
+       else if ((ats->modified_addr == GNUNET_NO) && (ats->modified_resources == GNUNET_YES) && (ats->stat.valid == GNUNET_YES))
        {
                ats_update_problem_cr();
+               text = "modified resources";
        }
-       else if (ats->modified_quality == GNUNET_YES)
+       else if ((ats->modified_addr == GNUNET_NO) && (ats->modified_quality == GNUNET_YES) && (ats->stat.valid == GNUNET_YES))
        {
                ats_update_problem_qm();
+               //ats_update_problem_qm_TEST ();
+               text = "modified quality";
        }
 #if DEBUG_ATS
        else GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Problem is unmodified\n");
 #endif
 
-       if (ats->res.valid == GNUNET_YES)
+       creation = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
+       start = GNUNET_TIME_absolute_get();
+
+       if (ats->stat.valid == GNUNET_YES)
        {
-               ats->res.solution = GNUNET_SYSERR;
-               ats_solve_problem(ats->max_iterations, ats->max_exec_duration.rel_value, ats->res.c_peers, ats->res.c_mechs, &ats->res);
-               if (ats->res.solution != 5)
-                       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Problem solution is not optimal: %i\n", ats->res.solution);
+               ats->stat.solution = GNUNET_SYSERR;
+               ats_solve_problem(ats->max_iterations, ats->max_exec_duration.rel_value, ats->stat.c_peers, ats->stat.c_mechs, &ats->stat);
        }
+       solving = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
 
-       duration = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
-
-       if (ats->res.valid == GNUNET_YES)
+       if (ats->stat.valid == GNUNET_YES)
        {
 #if DEBUG_ATS
+                       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP %s: creation time in [ms] %llu execution time in [ms] %llu for %i mechanisms: simplex rerun: %s\n",
+                                       text, creation.rel_value, solving.rel_value,
+                                       ats->stat.c_mechs,
+                                       (ats->simplex_rerun_required == GNUNET_NO) ? " NO" : "YES");
 #endif
-               if (ats->res.c_mechs > 8)
-                       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP execution time in [ms] for %i mechanisms: %llu\n", ats->res.c_mechs, duration.rel_value);
-
-               GNUNET_STATISTICS_set (stats, "ATS duration", duration.rel_value, GNUNET_NO);
-               GNUNET_STATISTICS_set (stats, "ATS mechanisms", ats->res.c_mechs, GNUNET_NO);
-               GNUNET_STATISTICS_set (stats, "ATS peers", ats->res.c_peers, GNUNET_NO);
-               GNUNET_STATISTICS_set (stats, "ATS solution", ats->res.solution, GNUNET_NO);
+               GNUNET_STATISTICS_set (stats, "ATS duration", solving.rel_value + creation.rel_value, GNUNET_NO);
+               GNUNET_STATISTICS_set (stats, "ATS mechanisms", ats->stat.c_mechs, GNUNET_NO);
+               GNUNET_STATISTICS_set (stats, "ATS peers", ats->stat.c_peers, GNUNET_NO);
+               GNUNET_STATISTICS_set (stats, "ATS solution", ats->stat.solution, GNUNET_NO);
                GNUNET_STATISTICS_set (stats, "ATS timestamp", start.abs_value, GNUNET_NO);
+
+               if ((ats->modified_addr == GNUNET_YES) || (ats->prob==NULL))
+                       GNUNET_STATISTICS_set (stats, "ATS state",ATS_NEW, GNUNET_NO);
+               else if ((ats->modified_resources == GNUNET_YES) &&
+                               (ats->modified_quality == GNUNET_NO))
+                       GNUNET_STATISTICS_set (stats, "ATS state", ATS_C_UPDATED, GNUNET_NO);
+               else if ((ats->modified_resources == GNUNET_NO) &&
+                               (ats->modified_quality == GNUNET_YES) &&
+                               (ats->simplex_rerun_required == GNUNET_NO))
+                       GNUNET_STATISTICS_set (stats, "ATS state", ATS_Q_UPDATED, GNUNET_NO);
+               else if ((ats->modified_resources == GNUNET_YES) &&
+                               (ats->modified_quality == GNUNET_YES) &&
+                               (ats->simplex_rerun_required == GNUNET_NO))
+                       GNUNET_STATISTICS_set (stats, "ATS state", ATS_QC_UPDATED, GNUNET_NO);
+               else if (ats->simplex_rerun_required == GNUNET_NO)
+                       GNUNET_STATISTICS_set (stats, "ATS state", ATS_UNMODIFIED, GNUNET_NO);
        }
-#if DEBUG_ATS
-       else if (ats->res.valid == GNUNET_NO)
+
+       if ((ats->save_mlp == GNUNET_YES) && (ats->stat.c_mechs >= ats->dump_min_peers) && (ats->stat.c_mechs >= ats->dump_min_addr))
+       {
+               char * filename;
+               if (ats->dump_overwrite == GNUNET_NO)
+               {
+                       GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%s_%llu.mlp",
+                       ats->stat.c_peers, ats->stat.c_mechs, text, GNUNET_TIME_absolute_get().abs_value);
+                       glp_write_lp (ats->prob, NULL, filename);
+               }
+               else
+               {
+                       GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i.mlp",
+                       ats->stat.c_peers, ats->stat.c_mechs );
+                       glp_write_lp (ats->prob, NULL, filename);
+               }
+               GNUNET_free (filename);
+       }
+       if ((ats->save_solution == GNUNET_YES) && (ats->stat.c_mechs >= ats->dump_min_peers) && (ats->stat.c_mechs >= ats->dump_min_addr))
        {
-                GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP not executed: no addresses\n");
+               char * filename;
+               if (ats->dump_overwrite == GNUNET_NO)
+               {
+                       GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%s_%llu.sol",
+                       ats->stat.c_peers, ats->stat.c_mechs, text, GNUNET_TIME_absolute_get().abs_value);
+                       glp_print_sol (ats->prob, filename);
+               }
+               else
+               {
+                       GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i.sol",
+                       ats->stat.c_peers, ats->stat.c_mechs);
+                       glp_print_sol (ats->prob, filename);
+               }
+               GNUNET_free (filename);
        }
-#endif
        ats->last = GNUNET_TIME_absolute_get();
-
        ats->modified_addr = GNUNET_NO;
        ats->modified_resources = GNUNET_NO;
        ats->modified_quality = GNUNET_NO;
-
 #endif
 }
 
-
-
 static void
 ats_schedule_calculation (void *cls,
                          const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
        struct ATS_info *ats = (struct ATS_info *) cls;
-       if (ats==NULL)
-               return;
+       if (ats==NULL) return;
 
        ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
        if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
@@ -6738,7 +7019,7 @@ ats_schedule_calculation (void *cls,
 
        ats_calculate_bandwidth_distribution (ats);
 
-       ats->ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_intervall,
+       ats->ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_interval,
                                        &ats_schedule_calculation, ats);
 }
 
@@ -6751,13 +7032,13 @@ void ats_init ()
        ats = GNUNET_malloc(sizeof (struct ATS_info));
 
        ats->min_delta = ATS_MIN_INTERVAL;
-       ats->exec_intervall = ATS_EXEC_INTERVAL;
+       ats->exec_interval = ATS_EXEC_INTERVAL;
        ats->max_exec_duration = ATS_MAX_EXEC_DURATION;
        ats->max_iterations = ATS_MAX_ITERATIONS;
        ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
 
 #if !HAVE_LIBGLPK
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed, ATS not active");
+       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "GLPK not installed, ATS not active\n");
        return;
 #endif
 
@@ -6766,6 +7047,10 @@ void ats_init ()
        ats->R = 1.0;
        ats->v_b_min = 64000;
        ats->v_n_min = 10;
+       ats->dump_min_peers = 1;
+       ats->dump_min_addr = 1;
+       ats->dump_overwrite = GNUNET_NO;
+
 #if HAVE_LIBGLPK
        ats->prob = NULL;
 #endif
@@ -6804,6 +7089,40 @@ void ats_init ()
 
        if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_SOLUTION"))
                ats->save_solution = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_SOLUTION");
+       if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_OVERWRITE"))
+               ats->dump_overwrite = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_OVERWRITE");
+       if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MIN_PEERS"))
+       {
+               GNUNET_CONFIGURATION_get_value_number(cfg, "transport","DUMP_MIN_PEERS", &value);
+               ats->dump_min_peers= value;
+       }
+       if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MIN_ADDRS"))
+       {
+               GNUNET_CONFIGURATION_get_value_number(cfg, "transport","DUMP_MIN_ADDRS", &value);
+               ats->dump_min_addr= value;
+       }
+       if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_OVERWRITE"))
+       {
+               GNUNET_CONFIGURATION_get_value_number(cfg, "transport","DUMP_OVERWRITE", &value);
+               ats->min_delta.rel_value = value;
+       }
+
+       if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "ATS_MIN_INTERVAL"))
+       {
+               GNUNET_CONFIGURATION_get_value_number(cfg, "transport","ATS_MIN_INTERVAL", &value);
+               ats->min_delta.rel_value = value;
+       }
+
+       if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "ATS_EXEC_INTERVAL"))
+       {
+               GNUNET_CONFIGURATION_get_value_number(cfg, "transport","ATS_EXEC_INTERVAL", &value);
+               ats->exec_interval.rel_value = value;
+       }
+       if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "ATS_MIN_INTERVAL"))
+       {
+               GNUNET_CONFIGURATION_get_value_number(cfg, "transport","ATS_MIN_INTERVAL", &value);
+               ats->min_delta.rel_value = value;
+       }
 
        ats->ats_task = GNUNET_SCHEDULER_add_now(&ats_schedule_calculation, ats);
 }
@@ -6938,7 +7257,7 @@ run (void *cls,
   if (peerinfo == NULL)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 _("Could not access PEERINFO service.  Exiting.\n")); 
+                 _("Could not access PEERINFO service.  Exiting.\n"));
       GNUNET_SCHEDULER_shutdown ();
       if (stats != NULL)
        {