- improved configuration and statistics handling
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses_mlp.c
index 74869e94c3224e2cba662692f4888109187f0ef2..175d4f41a79eacf49df43ecdf9d9716191753af1 100644 (file)
 #include "gnunet-service-ats_addresses_mlp.h"
 #include "gnunet_statistics_service.h"
 #include "glpk.h"
-#include "float.h"
 
 #define WRITE_MLP GNUNET_NO
-#define DEBUG_ATS GNUNET_YES
-/* A very big value */
-#define M DBL_MAX
+#define DEBUG_ATS GNUNET_NO
+#define VERBOSE_GLPK GNUNET_NO
 
+#define ENABLE_C8 GNUNET_YES
+#define ENABLE_C9 GNUNET_YES
 /**
  * Translate glpk solver error codes to text
  * @param retcode return code
@@ -149,9 +149,57 @@ mlp_status_to_string (int retcode)
   return "unknown error";
 }
 
+/**
+ * Translate ATS properties to text
+ * Just intended for debugging
+ *
+ * @param ats_index the ATS index
+ * @return string with result
+ */
+const char *
+mlp_ats_to_string (int ats_index)
+{
+  switch (ats_index) {
+    case GNUNET_ATS_ARRAY_TERMINATOR:
+      return "GNUNET_ATS_ARRAY_TERMINATOR";
+      break;
+    case GNUNET_ATS_UTILIZATION_UP:
+      return "GNUNET_ATS_UTILIZATION_UP";
+      break;
+    case GNUNET_ATS_UTILIZATION_DOWN:
+      return "GNUNET_ATS_UTILIZATION_DOWN";
+      break;
+    case GNUNET_ATS_COST_LAN:
+      return "GNUNET_ATS_COST_LAN";
+      break;
+    case GNUNET_ATS_COST_WAN:
+      return "GNUNET_ATS_COST_LAN";
+      break;
+    case GNUNET_ATS_COST_WLAN:
+      return "GNUNET_ATS_COST_WLAN";
+      break;
+    case GNUNET_ATS_NETWORK_TYPE:
+      return "GNUNET_ATS_NETWORK_TYPE";
+      break;
+    case GNUNET_ATS_QUALITY_NET_DELAY:
+      return "GNUNET_ATS_QUALITY_NET_DELAY";
+      break;
+    case GNUNET_ATS_QUALITY_NET_DISTANCE:
+      return "GNUNET_ATS_QUALITY_NET_DISTANCE";
+      break;
+    default:
+      return "unknown";
+      break;
+  }
+  GNUNET_break (0);
+  return "unknown error";
+}
+
 /**
  * Find a peer in the DLL
- * @param the peer to find
+ *
+ * @param mlp the mlp handle
+ * @param peer the peer to find
  * @return the peer struct
  */
 static struct ATS_Peer *
@@ -177,7 +225,7 @@ static int
 mlp_term_hook (void *info, const char *s)
 {
   /* Not needed atm struct MLP_information *mlp = info; */
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", s);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s", s);
   return 1;
 }
 
@@ -236,6 +284,7 @@ create_constraint_it (void *cls, const GNUNET_HashCode * key, void *value)
   struct ATS_Address *address = value;
   struct MLP_information *mlpi;
   unsigned int row_index;
+  char *name;
 
   GNUNET_assert (address->mlp_information != NULL);
   mlpi = (struct MLP_information *) address->mlp_information;
@@ -245,9 +294,12 @@ create_constraint_it (void *cls, const GNUNET_HashCode * key, void *value)
    */
   row_index = glp_add_rows (mlp->prob, 1);
   mlpi->r_c1 = row_index;
+  /* set row name */
+  GNUNET_asprintf(&name, "c1_%s_%s", GNUNET_i2s(&address->peer), address->plugin);
+  glp_set_row_name (mlp->prob, row_index, name);
+  GNUNET_free (name);
   /* set row bounds: <= 0 */
   glp_set_row_bnds (mlp->prob, row_index, GLP_UP, 0.0, 0.0);
-
   mlp->ia[mlp->ci] = row_index;
   mlp->ja[mlp->ci] = mlpi->c_b;
   mlp->ar[mlp->ci] = 1;
@@ -255,14 +307,18 @@ create_constraint_it (void *cls, const GNUNET_HashCode * key, void *value)
 
   mlp->ia[mlp->ci] = row_index;
   mlp->ja[mlp->ci] = mlpi->c_n;
-  mlp->ar[mlp->ci] = -M;
+  mlp->ar[mlp->ci] = -mlp->BIG_M;
   mlp->ci++;
-#if 0
+
   /* c 3) minimum bandwidth
    * b_t + (-n_t * b_min) >= 0
    */
 
   row_index = glp_add_rows (mlp->prob, 1);
+  /* set row name */
+  GNUNET_asprintf(&name, "c3_%s_%s", GNUNET_i2s(&address->peer), address->plugin);
+  glp_set_row_name (mlp->prob, row_index, name);
+  GNUNET_free (name);
   mlpi->r_c3 = row_index;
   /* set row bounds: >= 0 */
   glp_set_row_bnds (mlp->prob, row_index, GLP_LO, 0.0, 0.0);
@@ -274,7 +330,7 @@ create_constraint_it (void *cls, const GNUNET_HashCode * key, void *value)
 
   mlp->ia[mlp->ci] = row_index;
   mlp->ja[mlp->ci] = mlpi->c_n;
-  mlp->ar[mlp->ci] = -mlp->b_min;
+  mlp->ar[mlp->ci] = - (double) mlp->b_min;
   mlp->ci++;
 
   /* c 4) minimum connections
@@ -292,16 +348,71 @@ create_constraint_it (void *cls, const GNUNET_HashCode * key, void *value)
   mlp->ja[mlp->ci] = mlpi->c_n;
   mlp->ar[mlp->ci] = 1;
   mlp->ci++;
-#endif
+
+  /* c 10) obey network specific quotas
+   * (1)*b_1 + ... + (1)*b_m <= quota_n
+   */
+
+  int cur_row = 0;
+  int c;
+  for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
+    {
+    if (mlp->quota_index[c] == address->atsp_network_type)
+    {
+      cur_row = mlp->r_quota[c];
+      break;
+    }
+  }
+
+  if (cur_row != 0)
+  {
+    mlp->ia[mlp->ci] = cur_row;
+    mlp->ja[mlp->ci] = mlpi->c_b;
+    mlp->ar[mlp->ci] = 1;
+    mlp->ci++;
+  }
+  else
+  {
+    GNUNET_break (0);
+  }
 
   return GNUNET_OK;
 }
 
+/**
+ * Find the required ATS information for an address
+ *
+ * @param addr the address
+ * @param ats_index the desired ATS index
+ *
+ * @return the index on success, otherwise GNUNET_SYSERR
+ */
+
+static int
+mlp_lookup_ats (struct ATS_Address *addr, int ats_index)
+{
+  struct GNUNET_ATS_Information * ats = addr->ats;
+  int c = 0;
+  int found = GNUNET_NO;
+  for (c = 0; c < addr->ats_count; c++)
+  {
+    if (ats[c].type == ats_index)
+    {
+      found = GNUNET_YES;
+      break;
+    }
+  }
+  if (found == GNUNET_YES)
+    return c;
+  else
+    return GNUNET_SYSERR;
+}
 
 /**
  * Adds the problem constraints for all addresses
  * Required for problem recreation after address deletion
  *
+ * @param mlp the mlp handle
  * @param addresses all addresses
  */
 
@@ -309,8 +420,8 @@ static void
 mlp_add_constraints_all_addresses (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses)
 {
   unsigned int n_addresses;
-  int row_index;
-  //int c;
+  int c;
+  char *name;
 
   /* Problem matrix*/
   n_addresses = GNUNET_CONTAINER_multihashmap_size(addresses);
@@ -339,6 +450,10 @@ mlp_add_constraints_all_addresses (struct GAS_MLP_Handle *mlp, struct GNUNET_CON
    * #rows: |ressources|
    * #indices: |n_addresses|
    *
+   * c 10) obey network specific quota
+   * #rows: |network types
+   * #indices: |n_addresses|
+   *
    * Sum for feasibility constraints:
    * #rows: 3 * |n_addresses| +  |ressources| + |peers| + 1
    * #indices: 7 * |n_addresses|
@@ -351,12 +466,21 @@ mlp_add_constraints_all_addresses (struct GAS_MLP_Handle *mlp, struct GNUNET_CON
    *
    * c 7) quality
    * #rows: |quality properties|
-   * #indices:|quality properties| + |n_addresses|
+   * #indices: |n_addresses| + |quality properties|
+   *
+   * c 8) utilization
+   * #rows: 1
+   * #indices: |n_addresses| + 1
+   *
+   * c 9) relativity
+   * #rows: |peers|
+   * #indices: |n_addresses| + |peers|
    * */
 
-  int pi = ((7 * n_addresses) /*+ (2 * n_addresses +  mlp->m_q + 1)*/);
+  /* last +1 caused by glpk index starting with one: [1..pi]*/
+  int pi = ((7 * n_addresses) + (5 * n_addresses +  mlp->m_q + mlp->c_p + 2) + 1);
   mlp->cm_size = pi;
-  mlp->ci = 0;
+  mlp->ci = 1;
 
   /* row index */
   int *ia = GNUNET_malloc (pi * sizeof (int));
@@ -378,17 +502,19 @@ mlp_add_constraints_all_addresses (struct GAS_MLP_Handle *mlp, struct GNUNET_CON
    * c 3) minimum bandwidth
    * c 4) minimum number of connections
    * c 6) maximize diversity
+   * c 10) obey network specific quota
    */
 
   int min = mlp->n_min;
   if (mlp->n_min > mlp->c_p)
     min = mlp->c_p;
-/*
+
   mlp->r_c4 = glp_add_rows (mlp->prob, 1);
-  glp_set_row_bnds (mlp->prob, mlp->r_c4, GLP_FX, min, min);
-*/
+  glp_set_row_name (mlp->prob, mlp->r_c4, "c4");
+  glp_set_row_bnds (mlp->prob, mlp->r_c4, GLP_LO, min, min);
+
   /* Add row for c6) */
-#if 0
+
   mlp->r_c6 = glp_add_rows (mlp->prob, 1);
   /* Set type type to fix */
   glp_set_row_bnds (mlp->prob, mlp->r_c6, GLP_FX, 0.0, 0.0);
@@ -397,7 +523,19 @@ mlp_add_constraints_all_addresses (struct GAS_MLP_Handle *mlp, struct GNUNET_CON
   ja[mlp->ci] = mlp->c_d;
   ar[mlp->ci] = -1;
   mlp->ci++;
-#endif
+
+  /* Add rows for c 10) */
+  for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
+  {
+    mlp->r_quota[c] = glp_add_rows (mlp->prob, 1);
+    char * text;
+    GNUNET_asprintf(&text, "quota_ats_%i", mlp->quota_index[c]);
+    glp_set_row_name (mlp->prob, mlp->r_quota[c], text);
+    GNUNET_free (text);
+    /* Set bounds to 0 <= x <= quota_out */
+    glp_set_row_bnds (mlp->prob, mlp->r_quota[c], GLP_UP, 0.0, mlp->quota_out[c]);
+  }
+
   GNUNET_CONTAINER_multihashmap_iterate (addresses, create_constraint_it, mlp);
 
   /* Adding constraint rows
@@ -406,49 +544,123 @@ mlp_add_constraints_all_addresses (struct GAS_MLP_Handle *mlp, struct GNUNET_CON
    *
    * c 2) 1 address per peer
    * sum (n_p1_1 + ... + n_p1_n) = 1
-   */
-return;
-  /* Adding rows for c 2) */
-  row_index = glp_add_rows (mlp->prob, mlp->c_p);
+   *
+   * c 8) utilization
+   * sum (f_p * b_p1_1 + ... + f_p * b_p1_n) - u = 0
+   *
+   * c 9) relativity
+   * V p : sum (bt_1 + ... +bt_n) - f_p * r = 0
+   * */
+
+  /* Adding rows for c 8) */
+  mlp->r_c8 = glp_add_rows (mlp->prob, mlp->c_p);
+  glp_set_row_name (mlp->prob, mlp->r_c8, "c8");
+  /* Set row bound == 0 */
+  glp_set_row_bnds (mlp->prob, mlp->r_c8, GLP_FX, 0.0, 0.0);
+  /* -u */
+
+  ia[mlp->ci] = mlp->r_c8;
+  ja[mlp->ci] = mlp->c_u;
+  ar[mlp->ci] = -1;
+  mlp->ci++;
 
   struct ATS_Peer * peer = mlp->peer_head;
   while (peer != NULL)
   {
     struct ATS_Address *addr = peer->head;
-    struct MLP_information *mlpi = (struct MLP_information *) addr->mlp_information;
-    /* Adding row for c 2) */
+    struct MLP_information *mlpi = NULL;
+
+    /* Adding rows for c 2) */
+    peer->r_c2 = glp_add_rows (mlp->prob, 1);
+    GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&peer->id));
+    glp_set_row_name (mlp->prob, peer->r_c2, name);
+    GNUNET_free (name);
     /* Set row bound == 1 */
-    glp_set_row_bnds (mlp->prob, row_index, GLP_FX, 1.0, 1.0);
+    glp_set_row_bnds (mlp->prob, peer->r_c2, GLP_FX, 1.0, 1.0);
+
+    /* Adding rows for c 9) */
+#if ENABLE_C9
+    peer->r_c9 = glp_add_rows (mlp->prob, 1);
+    GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&peer->id));
+    glp_set_row_name (mlp->prob, peer->r_c9, name);
+    GNUNET_free (name);
+    /* Set row bound == 0 */
+    glp_set_row_bnds (mlp->prob, peer->r_c9, GLP_LO, 0.0, 0.0);
+
+    /* Set -r */
+    ia[mlp->ci] = peer->r_c9;
+    ja[mlp->ci] = mlp->c_r;
+    ar[mlp->ci] = -1;
+    mlp->ci++;
+#endif
 
     while (addr != NULL)
     {
-      ia[mlp->ci] = row_index;
+      mlpi = (struct MLP_information *) addr->mlp_information;
+
+      /* coefficient for c 2) */
+
+      ia[mlp->ci] = peer->r_c2;
       ja[mlp->ci] = mlpi->c_n;
       ar[mlp->ci] = 1;
       mlp->ci++;
 
+      /* coefficient for c 8) */
+      ia[mlp->ci] = mlp->r_c8;
+      ja[mlp->ci] = mlpi->c_b;
+      ar[mlp->ci] = peer->f;
+      mlp->ci++;
+
+#if ENABLE_C9
+      /* coefficient for c 9) */
+      ia[mlp->ci] = peer->r_c9;
+      ja[mlp->ci] = mlpi->c_b;
+      ar[mlp->ci] = 1;
+      mlp->ci++;
+#endif
+
       addr = addr->next;
     }
     peer = peer->next;
   }
 
+  /* c 7) For all quality metrics */
+
 
-  /* For all quality metrics */
-/*
   for (c = 0; c < mlp->m_q; c++)
   {
-    struct ATS_Peer *p = mlp->peer_head;
-    while (p != NULL)
-    {
-      ia[mlp->ci] = row_index;
-      ja[mlp->ci] = mlp->c_q[c];
-      ar[mlp->ci] = mlp->;
-      mlp->ci++;
-
-      p = p->next;
-    }
+    struct ATS_Peer *tp;
+    struct ATS_Address *ta;
+    struct MLP_information * mlpi;
+    double value = 1.0;
+
+    /* Adding rows for c 7) */
+    mlp->r_q[c] = glp_add_rows (mlp->prob, 1);
+    GNUNET_asprintf(&name, "c7_q%i_%s", c, mlp_ats_to_string(mlp->q[c]));
+    glp_set_row_name (mlp->prob, mlp->r_q[c], name);
+    GNUNET_free (name);
+    /* Set row bound == 0 */
+    glp_set_row_bnds (mlp->prob, mlp->r_q[c], GLP_LO, 0.0, 0.0);
+
+    ia[mlp->ci] = mlp->r_q[c];
+    ja[mlp->ci] = mlp->c_q[c];
+    ar[mlp->ci] = -1;
+    mlp->ci++;
+
+    for (tp = mlp->peer_head; tp != NULL; tp = tp->next)
+      for (ta = tp->head; ta != NULL; ta = ta->next)
+        {
+          mlpi = ta->mlp_information;
+          value = mlpi->q_averaged[c];
+
+          mlpi->r_q[c] = mlp->r_q[c];
+
+          ia[mlp->ci] = mlp->r_q[c];
+          ja[mlp->ci] = mlpi->c_b;
+          ar[mlp->ci] = tp->f * value;
+          mlp->ci++;
+        }
   }
-*/
 }
 
 
@@ -478,9 +690,6 @@ create_columns_it (void *cls, const GNUNET_HashCode * key, void *value)
   mlpi->c_b = col;
   mlpi->c_n = col + 1;
 
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
-      "ats-mlp",
-      "Culoumn %i %i\n", mlpi->c_b, mlpi->c_n);
 
   GNUNET_asprintf (&name, "b_%s_%s", GNUNET_i2s (&address->peer), address->plugin);
   glp_set_col_name (mlp->prob, mlpi->c_b , name);
@@ -502,7 +711,7 @@ create_columns_it (void *cls, const GNUNET_HashCode * key, void *value)
   /* Integer value*/
   glp_set_col_kind (mlp->prob, mlpi->c_n, GLP_IV);
   /* Objective function coefficient == 0 */
-  glp_set_obj_coef (mlp->prob, mlpi->c_n, 1);
+  glp_set_obj_coef (mlp->prob, mlpi->c_n, 0);
 
   return GNUNET_OK;
 }
@@ -513,17 +722,17 @@ create_columns_it (void *cls, const GNUNET_HashCode * key, void *value)
  * Create the MLP problem
  *
  * @param mlp the MLP handle
+ * @param addresses the hashmap containing all adresses
  * @return GNUNET_OK or GNUNET_SYSERR
  */
 static int
 mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses)
 {
   int res = GNUNET_OK;
-/*
   int col;
   int c;
   char *name;
-*/
+
   GNUNET_assert (mlp->prob == NULL);
 
   /* create the glpk problem */
@@ -538,7 +747,7 @@ mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHas
   /* Adding invariant columns */
 
   /* Diversity d column  */
-#if 0
+
   col = glp_add_cols (mlp->prob, 1);
   mlp->c_d = col;
   /* Column name */
@@ -559,6 +768,7 @@ mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHas
   /* Column lower bound = 0.0 */
   glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);
 
+#if ENABLE_C9
   /* Relativity r column  */
   col = glp_add_cols (mlp->prob, 1);
   mlp->c_r = col;
@@ -568,6 +778,7 @@ mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHas
   glp_set_obj_coef (mlp->prob, col, mlp->co_R);
   /* Column lower bound = 0.0 */
   glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);
+#endif
 
   /* Quality metric columns */
   col = glp_add_cols(mlp->prob, mlp->m_q);
@@ -582,7 +793,7 @@ mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHas
     /* Coefficient == Qm */
     glp_set_obj_coef (mlp->prob, col + c, mlp->co_Q[c]);
   }
-#endif
+
   /* Add columns for addresses */
   GNUNET_CONTAINER_multihashmap_iterate (addresses, create_columns_it, mlp);
 
@@ -590,7 +801,7 @@ mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHas
   mlp_add_constraints_all_addresses (mlp, addresses);
 
   /* Load the matrix */
-  glp_load_matrix(mlp->prob, (mlp->ci - 1), mlp->ia, mlp->ja, mlp->ar);
+  glp_load_matrix(mlp->prob, (mlp->ci-1), mlp->ia, mlp->ja, mlp->ar);
 
   return res;
 }
@@ -645,7 +856,7 @@ lp_solv:
     else
     {
       /* Problem was ill-defined, no way to handle that */
-      GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
+      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
           "ats-mlp",
           "Solving LP problem failed: %i %s\n", res, mlp_solve_to_string(res));
       return GNUNET_SYSERR;
@@ -674,7 +885,7 @@ lp_solv:
 
     /* Problem was ill-defined, no way to handle that */
     default:
-      GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
+      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
           "ats-mlp",
           "Solving LP problem failed, no solution: %s\n", mlp_status_to_string(res));
       return GNUNET_SYSERR;
@@ -722,7 +933,7 @@ mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
   else
   {
     /* Problem was ill-defined, no way to handle that */
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
         "ats-mlp",
         "Solving MLP problem failed:  %s\n", mlp_solve_to_string(res));
     return GNUNET_SYSERR;
@@ -749,7 +960,7 @@ mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
 
     /* Problem was ill-defined, no way to handle that */
     default:
-      GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
+      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
           "ats-mlp",
           "Solving MLP problem failed, %s\n\n", mlp_status_to_string(res));
       return GNUNET_SYSERR;
@@ -759,7 +970,7 @@ mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
   return GNUNET_OK;
 }
 
-int mlp_solve_problem (struct GAS_MLP_Handle *mlp);
+int GAS_mlp_solve_problem (struct GAS_MLP_Handle *mlp);
 
 static void
 mlp_scheduler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
@@ -771,11 +982,11 @@ mlp_scheduler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
 
-#if DEBUG_ATS
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scheduled problem solving\n");
-#endif
+
   if (mlp->addr_in_problem != 0)
-    mlp_solve_problem(mlp);
+    GAS_mlp_solve_problem(mlp);
 }
 
 
@@ -786,13 +997,13 @@ mlp_scheduler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
  */
 int
-mlp_solve_problem (struct GAS_MLP_Handle *mlp)
+GAS_mlp_solve_problem (struct GAS_MLP_Handle *mlp)
 {
   int res;
   mlp->last_execution = GNUNET_TIME_absolute_get ();
-#if DEBUG_ATS
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Problem solving\n");
-#endif
+
 
 #if WRITE_MLP
   char * name;
@@ -813,9 +1024,9 @@ mlp_solve_problem (struct GAS_MLP_Handle *mlp)
 
   if (res != GNUNET_OK)
   {
-#if DEBUG_ATS
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "LP Problem solving failed\n");
-#endif
+
     return GNUNET_SYSERR;
   }
 
@@ -826,31 +1037,44 @@ mlp_solve_problem (struct GAS_MLP_Handle *mlp)
   glp_print_mip (mlp->prob, name);
   GNUNET_free (name);
 # endif
-
-
   if (res != GNUNET_OK)
   {
-#if DEBUG_ATS
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP Problem solving failed\n");
-#endif
+
     return GNUNET_SYSERR;
   }
 
-  res = glp_mip_status(mlp->prob);
 
-  if (res != GNUNET_OK)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Problem solved\n");
+  /* Process result */
+  struct ATS_Peer *p = NULL;
+  struct ATS_Address *a = NULL;
+  struct MLP_information *mlpi = NULL;
+
+  for (p = mlp->peer_head; p != NULL; p = p->next)
   {
-#if DEBUG_ATS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "LP Problem solving failed\n");
-#endif
-    return GNUNET_SYSERR;
-  }
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s'\n", GNUNET_i2s (&p->id));
+    for (a = p->head; a != NULL; a = a->next)
+    {
+      double b = 0.0;
+      double n = 0.0;
 
-#if DEBUG_ATS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Problem solved: %i %s\n", res, mlp_status_to_string(res));
-#endif
+      mlpi = a->mlp_information;
 
-  /* Process result */
+      b = glp_mip_col_val(mlp->prob, mlpi->c_b);
+      mlpi->b = b;
+
+      n = glp_mip_col_val(mlp->prob, mlpi->c_n);
+      if (n == 1.0)
+        mlpi->n = GNUNET_YES;
+      else
+        mlpi->n = GNUNET_NO;
+
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tAddress %s %f\n",
+          (n == 1.0) ? "[x]" : "[ ]", b);
+    }
+  }
 
   if (mlp->mlp_task != GNUNET_SCHEDULER_NO_TASK)
   {
@@ -864,6 +1088,7 @@ mlp_solve_problem (struct GAS_MLP_Handle *mlp)
 /**
  * Init the MLP problem solving component
  *
+ * @param cfg the GNUNET_CONFIGURATION_Handle handle
  * @param stats the GNUNET_STATISTICS handle
  * @param max_duration maximum numbers of iterations for the LP/MLP Solver
  * @param max_iterations maximum time limit for the LP/MLP Solver
@@ -884,6 +1109,9 @@ GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   unsigned int b_min;
   unsigned int n_min;
   struct GNUNET_TIME_Relative i_exec;
+  int c;
+  char * quota_out_str;
+  char * quota_in_str;
 
   /* Init GLPK environment */
   GNUNET_assert (glp_init_env() == 0);
@@ -892,6 +1120,8 @@ GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   mlp->prob = glp_create_prob();
   GNUNET_assert (mlp->prob != NULL);
 
+  mlp->BIG_M = (double) BIG_M_VALUE;
+
   /* Get diversity coefficient from configuration */
   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
                                                       "COEFFICIENT_D",
@@ -920,7 +1150,6 @@ GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   int i_delay = -1;
   int i_distance = -1;
   int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
-  int c;
   for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
   {
     /* initialize quality coefficients with default value 1.0 */
@@ -954,7 +1183,9 @@ GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                                       &tmp))
     b_min = tmp;
   else
-    b_min = 64000;
+  {
+    b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
+  }
 
   /* Get minimum number of connections from configuration */
   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
@@ -964,6 +1195,99 @@ GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   else
     n_min = 4;
 
+  /* Init network quotas */
+  int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
+  for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
+  {
+    mlp->quota_index[c] = quotas[c];
+    static char * entry_in = NULL;
+    static char * entry_out = NULL;
+    unsigned long long quota_in = 0;
+    unsigned long long quota_out = 0;
+
+    switch (quotas[c]) {
+      case GNUNET_ATS_NET_UNSPECIFIED:
+        entry_out = "UNSPECIFIED_QUOTA_OUT";
+        entry_in = "UNSPECIFIED_QUOTA_IN";
+        break;
+      case GNUNET_ATS_NET_LOOPBACK:
+        entry_out = "LOOPBACK_QUOTA_OUT";
+        entry_in = "LOOPBACK_QUOTA_IN";
+        break;
+      case GNUNET_ATS_NET_LAN:
+        entry_out = "LAN_QUOTA_OUT";
+        entry_in = "LAN_QUOTA_IN";
+        break;
+      case GNUNET_ATS_NET_WAN:
+        entry_out = "WAN_QUOTA_OUT";
+        entry_in = "WAN_QUOTA_IN";
+        break;
+      case GNUNET_ATS_NET_WLAN:
+        entry_out = "WLAN_QUOTA_OUT";
+        entry_in = "WLAN_QUOTA_IN";
+        break;
+      default:
+        break;
+    }
+
+    if ((entry_in == NULL) || (entry_out == NULL))
+      continue;
+
+    if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
+    {
+      if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
+          (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &quota_out)))
+        quota_out = mlp->BIG_M;
+
+      GNUNET_free (quota_out_str);
+      quota_out_str = NULL;
+    }
+    else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
+    {
+      quota_out = 0;
+    }
+    else
+    {
+      quota_out = mlp->BIG_M;
+    }
+
+    if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
+    {
+      if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
+          (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &quota_in)))
+        quota_in = mlp->BIG_M;
+
+      GNUNET_free (quota_in_str);
+      quota_in_str = NULL;
+    }
+    else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
+    {
+      quota_in = 0;
+    }
+    else
+    {
+      quota_in = mlp->BIG_M;
+    }
+
+    /* Check if defined quota could make problem unsolvable */
+    if (((n_min * b_min) > quota_out) && (GNUNET_ATS_NET_UNSPECIFIED != quotas[c]))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Inconsistent quota configuration value `%s': " \
+          "outbound quota (%u Bps) too small for combination of minimum connections and minimum bandwidth per peer (%u * %u Bps = %u)\n", entry_out, quota_out, n_min, b_min, n_min * b_min);
+
+      GAS_mlp_done(mlp);
+      mlp = NULL;
+      return NULL;
+    }
+
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' quota %llu and `%s' quota %llu\n",
+                entry_out, quota_out, entry_in, quota_in);
+    GNUNET_STATISTICS_update ((struct GNUNET_STATISTICS_Handle *) stats, entry_out, quota_out, GNUNET_NO);
+    GNUNET_STATISTICS_update ((struct GNUNET_STATISTICS_Handle *) stats, entry_in, quota_in, GNUNET_NO);
+    mlp->quota_out[c] = quota_out;
+    mlp->quota_in[c] = quota_in;
+  }
+
   /* Get minimum number of connections from configuration */
   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_time (cfg, "ats",
                                                         "ATS_EXEC_INTERVAL",
@@ -975,26 +1299,28 @@ GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   mlp->stats = (struct GNUNET_STATISTICS_Handle *) stats;
   mlp->max_iterations = max_iterations;
   mlp->max_exec_duration = max_duration;
+  mlp->auto_solve = GNUNET_YES;
 
   /* Redirect GLPK output to GNUnet logging */
   glp_error_hook((void *) mlp, &mlp_term_hook);
 
   /* Init LP solving parameters */
   glp_init_smcp(&mlp->control_param_lp);
-#if DEBUG_ATS
-  mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
-#else
+
   mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
+#if VERBOSE_GLPK
+  mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
 #endif
+
   mlp->control_param_lp.it_lim = max_iterations;
   mlp->control_param_lp.tm_lim = max_duration.rel_value;
 
   /* Init MLP solving parameters */
   glp_init_iocp(&mlp->control_param_mlp);
-#if DEBUG_ATS
-  mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
-#else
+
   mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
+#if VERBOSE_GLPK
+  mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
 #endif
   mlp->control_param_mlp.tm_lim = max_duration.rel_value;
 
@@ -1010,6 +1336,160 @@ GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   return mlp;
 }
 
+static void
+update_quality (struct GAS_MLP_Handle *mlp, struct ATS_Address * address)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating quality metrics for peer `%s'\n",
+      GNUNET_i2s (&address->peer));
+
+  struct MLP_information *mlpi = address->mlp_information;
+  struct GNUNET_ATS_Information *ats = address->ats;
+  GNUNET_assert (mlpi != NULL);
+
+  int c;
+
+  for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
+  {
+    int index = mlp_lookup_ats(address, mlp->q[c]);
+
+    if (index == GNUNET_SYSERR)
+      continue;
+
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' value `%s': %f\n",
+        GNUNET_i2s (&address->peer),
+        mlp_ats_to_string(mlp->q[c]),
+        (double) ats[index].value);
+
+    int i = mlpi->q_avg_i[c];
+    double * qp = mlpi->q[c];
+    qp[i] = (double) ats[index].value;
+
+    int t;
+    for (t = 0; t < MLP_AVERAGING_QUEUE_LENGTH; t++)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' queue[%u]: %f\n",
+        GNUNET_i2s (&address->peer),
+        mlp_ats_to_string(mlp->q[c]),
+        t,
+        qp[t]);
+    }
+
+    if (mlpi->q_avg_i[c] + 1 < (MLP_AVERAGING_QUEUE_LENGTH))
+      mlpi->q_avg_i[c] ++;
+    else
+      mlpi->q_avg_i[c] = 0;
+
+
+    int c2;
+    int c3;
+    double avg = 0.0;
+    switch (mlp->q[c])
+    {
+      case GNUNET_ATS_QUALITY_NET_DELAY:
+        c3 = 0;
+        for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
+        {
+          if (mlpi->q[c][c2] != -1)
+          {
+            double * t2 = mlpi->q[c] ;
+            avg += t2[c2];
+            c3 ++;
+          }
+        }
+        if (c3 > 0)
+          /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/
+          mlpi->q_averaged[c] = (double) c3 / avg;
+        else
+          mlpi->q_averaged[c] = 0.0;
+
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' average sum: %f, average: %f, weight: %f\n",
+          GNUNET_i2s (&address->peer),
+          mlp_ats_to_string(mlp->q[c]),
+          avg,
+          avg / (double) c3,
+          mlpi->q_averaged[c]);
+
+        break;
+      case GNUNET_ATS_QUALITY_NET_DISTANCE:
+        c3 = 0;
+        for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
+        {
+          if (mlpi->q[c][c2] != -1)
+          {
+            double * t2 = mlpi->q[c] ;
+            avg += t2[c2];
+            c3 ++;
+          }
+        }
+        if (c3 > 0)
+          /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/
+          mlpi->q_averaged[c] = (double) c3 / avg;
+        else
+          mlpi->q_averaged[c] = 0.0;
+
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' average sum: %f, average: %f, weight: %f\n",
+          GNUNET_i2s (&address->peer),
+          mlp_ats_to_string(mlp->q[c]),
+          avg,
+          avg / (double) c3,
+          mlpi->q_averaged[c]);
+
+        break;
+      default:
+        break;
+    }
+
+    if ((mlpi->c_b != 0) && (mlpi->r_q[c] != 0))
+    {
+
+      /* Get current number of columns */
+      int found = GNUNET_NO;
+      int cols = glp_get_num_cols(mlp->prob);
+      int *ind = GNUNET_malloc (cols * sizeof (int) + 1);
+      double *val = GNUNET_malloc (cols * sizeof (double) + 1);
+
+      /* Get the matrix row of quality */
+      int length = glp_get_mat_row(mlp->prob, mlp->r_q[c], ind, val);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "cols %i, length %i c_b %i\n", cols, length, mlpi->c_b);
+      int c4;
+      /* Get the index if matrix row of quality */
+      for (c4 = 1; c4 <= length; c4++ )
+      {
+        if (mlpi->c_b == ind[c4])
+        {
+          /* Update the value */
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating quality `%s' column `%s' row `%s' : %f -> %f\n",
+              mlp_ats_to_string(mlp->q[c]),
+              glp_get_col_name (mlp->prob, ind[c4]),
+              glp_get_row_name (mlp->prob, mlp->r_q[c]),
+              val[c4],
+              mlpi->q_averaged[c]);
+          val[c4] = mlpi->q_averaged[c];
+          found = GNUNET_YES;
+          break;
+        }
+      }
+
+      if (found == GNUNET_NO)
+        {
+
+          ind[length+1] = mlpi->c_b;
+          val[length+1] = mlpi->q_averaged[c];
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%i ind[%i] val[%i]:  %i %f\n", length+1,  length+1, length+1, mlpi->c_b, mlpi->q_averaged[c]);
+          glp_set_mat_row (mlp->prob, mlpi->r_q[c], length+1, ind, val);
+        }
+      else
+        {
+        /* Get the index if matrix row of quality */
+        glp_set_mat_row (mlp->prob, mlpi->r_q[c], length, ind, val);
+        }
+
+      GNUNET_free (ind);
+      GNUNET_free (val);
+    }
+  }
+}
+
 /**
  * Updates a single address in the MLP problem
  *
@@ -1029,7 +1509,6 @@ GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
 {
   int new;
   struct MLP_information *mlpi;
-  int c;
 
   GNUNET_STATISTICS_update (mlp->stats,"# LP address updates", 1, GNUNET_NO);
 
@@ -1043,6 +1522,18 @@ GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
   if (new == GNUNET_YES)
   {
     mlpi = GNUNET_malloc (sizeof (struct MLP_information));
+
+    int c;
+    for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
+    {
+      int c2;
+      mlpi->r_q[c] = 0;
+      for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
+        mlpi->q[c][c2] = -1.0; /* -1.0: invalid value */
+      mlpi->q_avg_i[c] = 0;
+      mlpi->q_averaged[c] = 0.0;
+    }
+
     address->mlp_information = mlpi;
     mlp->addr_in_problem ++;
 
@@ -1050,17 +1541,20 @@ GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
     struct ATS_Peer *peer = mlp_find_peer (mlp, &address->peer);
     if (peer == NULL)
     {
-#if DEBUG_ATS
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding new peer `%s'\n", GNUNET_i2s (&address->peer));
-#endif
+
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer `%s'\n",
+          GNUNET_i2s (&address->peer));
+
       peer = GNUNET_malloc (sizeof (struct ATS_Peer));
       peer->head = NULL;
       peer->tail = NULL;
 
+      int c;
       for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
       {
         peer->f_q[c] = 1.0;
       }
+      peer->f = 1.0;
 
       memcpy (&peer->id, &address->peer, sizeof (struct GNUNET_PeerIdentity));
       GNUNET_assert(address->prev == NULL);
@@ -1071,27 +1565,34 @@ GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
     }
     else
     {
-#if DEBUG_ATS
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding address to peer `%s'\n", GNUNET_i2s (&address->peer));
-#endif
+
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding address to peer `%s'\n",
+          GNUNET_i2s (&address->peer));
+
       GNUNET_CONTAINER_DLL_insert (peer->head, peer->tail, address);
     }
+
+    update_quality (mlp, address);
   }
   else
   {
-#if DEBUG_ATS
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating existing address to peer `%s'\n", GNUNET_i2s (&address->peer));
-#endif
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating existing address to peer `%s'\n",
+        GNUNET_i2s (&address->peer));
+
+    update_quality (mlp, address);
   }
 
   /* Recalculate */
   if (new == GNUNET_YES)
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Recreating problem: new address\n");
+
     mlp_delete_problem (mlp);
     mlp_create_problem (mlp, addresses);
     mlp->presolver_required = GNUNET_YES;
   }
-  mlp_solve_problem (mlp);
+  if (mlp->auto_solve == GNUNET_YES)
+    GAS_mlp_solve_problem (mlp);
 }
 
 /**
@@ -1121,43 +1622,89 @@ GAS_mlp_address_delete (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
   /* Remove from peer list */
   struct ATS_Peer *head = mlp_find_peer (mlp, &address->peer);
   GNUNET_assert (head != NULL);
-#if DEBUG_ATS
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Deleting address for `%s'\n", GNUNET_i2s (&address->peer));
-#endif
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting address for `%s'\n", GNUNET_i2s (&address->peer));
+
   GNUNET_CONTAINER_DLL_remove (head->head, head->tail, address);
   if ((head->head == NULL) && (head->tail == NULL))
   {
     /* No address for peer left, remove peer */
-#if DEBUG_ATS
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Deleting peer `%s'\n", GNUNET_i2s (&address->peer));
-#endif
+
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%s'\n", GNUNET_i2s (&address->peer));
+
     GNUNET_CONTAINER_DLL_remove (mlp->peer_head, mlp->peer_tail, head);
     GNUNET_free (head);
     mlp->c_p --;
   }
 
   /* Update problem */
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Recreating problem: new address\n");
+
   mlp_delete_problem (mlp);
   if ((GNUNET_CONTAINER_multihashmap_size (addresses) > 0) && (mlp->c_p > 0))
   {
-#if DEBUG_ATS
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "mlp_create_problem %i\n",__LINE__);
-#endif
     mlp_create_problem (mlp, addresses);
 
     /* Recalculate */
     mlp->presolver_required = GNUNET_YES;
-    mlp_solve_problem (mlp);
+    if (mlp->auto_solve == GNUNET_YES)
+      GAS_mlp_solve_problem (mlp);
+  }
+}
+
+static int
+mlp_get_preferred_address_it (void *cls, const GNUNET_HashCode * key, void *value)
+{
+
+  struct ATS_PreferedAddress *aa = (struct ATS_PreferedAddress *) cls;
+  struct ATS_Address *addr = value;
+  struct MLP_information *mlpi = addr->mlp_information;
+  if (mlpi == NULL)
+    return GNUNET_YES;
+  if (mlpi->n == GNUNET_YES)
+  {
+    aa->address = addr;
+    if (mlpi->b > (double) UINT32_MAX)
+      aa->bandwidth_out = UINT32_MAX;
+    else
+      aa->bandwidth_out = (uint32_t) mlpi->b;
+    aa->bandwidth_in = 0;
+    return GNUNET_NO;
   }
+  return GNUNET_YES;
+}
+
+
+/**
+ * Get the preferred address for a specific peer
+ *
+ * @param mlp the MLP Handle
+ * @param addresses address hashmap
+ * @param peer the peer
+ * @return suggested address
+ */
+struct ATS_PreferedAddress *
+GAS_mlp_get_preferred_address (struct GAS_MLP_Handle *mlp,
+                               struct GNUNET_CONTAINER_MultiHashMap * addresses,
+                               const struct GNUNET_PeerIdentity *peer)
+{
+  struct ATS_PreferedAddress * aa = GNUNET_malloc (sizeof (struct ATS_PreferedAddress));
+  aa->address = NULL;
+  aa->bandwidth_in = 0;
+  aa->bandwidth_out = 0;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Getting preferred address for `%s'\n", GNUNET_i2s (peer));
+  GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, mlp_get_preferred_address_it, aa);
+  return aa;
 }
 
+
 /**
  * Changes the preferences for a peer in the MLP problem
  *
  * @param mlp the MLP Handle
  * @param peer the peer
  * @param kind the kind to change the preference
- * @param float the score
+ * @param score the score
  */
 void
 GAS_mlp_address_change_preference (struct GAS_MLP_Handle *mlp,
@@ -1182,6 +1729,8 @@ GAS_mlp_done (struct GAS_MLP_Handle *mlp)
   struct ATS_Peer * peer;
   struct ATS_Peer * tmp;
 
+  GNUNET_assert (mlp != NULL);
+
   if (mlp->mlp_task != GNUNET_SCHEDULER_NO_TASK)
   {
     GNUNET_SCHEDULER_cancel(mlp->mlp_task);
@@ -1189,16 +1738,13 @@ GAS_mlp_done (struct GAS_MLP_Handle *mlp)
   }
 
   /* clean up peer list */
-  if (mlp != NULL)
+  peer = mlp->peer_head;
+  while (peer != NULL)
   {
-    peer = mlp->peer_head;
-    while (peer != NULL)
-    {
-      GNUNET_CONTAINER_DLL_remove(mlp->peer_head, mlp->peer_tail, peer);
-      tmp = peer->next;
-      GNUNET_free (peer);
-      peer = tmp;
-    }
+    GNUNET_CONTAINER_DLL_remove(mlp->peer_head, mlp->peer_tail, peer);
+    tmp = peer->next;
+    GNUNET_free (peer);
+    peer = tmp;
   }
   mlp_delete_problem (mlp);