fix warning
[oweals/gnunet.git] / src / ats / gnunet-service-ats-solver_ril.c
index 9aecd1747a5b4ee0bae845646c99f272c56ec615..5121846270fb9dfc328fa8657aebf38a9e4df6ba 100755 (executable)
 #include "gnunet-service-ats_addresses.h"
 #include "gnunet_statistics_service.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
+
+#define RIL_ACTION_INVALID -1
+#define RIL_FEATURES_ADDRESS_COUNT (3 + GNUNET_ATS_QualityPropertiesCount)
+#define RIL_FEATURES_NETWORK_COUNT 4
+
 #define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
 #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
  * General description
  */
 
+/**
+ * TODO! implement reward calculation 1 and 2 (i.e. meeting preferences and taking scores)
+ */
+
 enum RIL_Action_Type
 {
   RIL_ACTION_NOTHING = 0,
@@ -56,16 +66,19 @@ enum RIL_Action_Type
   RIL_ACTION_BW_OUT_DEC = 8,
   RIL_ACTION_TYPE_NUM = 9
 };
-//TODO! add the rest of the actions
 
 enum RIL_Algorithm
 {
-  RIL_ALGO_SARSA = 0, RIL_ALGO_Q = 1
+  RIL_ALGO_SARSA = 0,
+  RIL_ALGO_Q = 1
 };
 
 enum RIL_E_Modification
 {
-  RIL_E_SET, RIL_E_ZERO, RIL_E_ACCUMULATE, RIL_E_REPLACE
+  RIL_E_SET,
+  RIL_E_ZERO,
+  RIL_E_ACCUMULATE,
+  RIL_E_REPLACE
 };
 
 /**
@@ -94,6 +107,27 @@ struct RIL_Learning_Parameters
   float lambda;
 };
 
+/**
+ * Wrapper for addresses to store them in agent's linked list
+ */
+struct RIL_Address_Wrapped
+{
+  /**
+   * Next in DLL
+   */
+  struct RIL_Address_Wrapped *next;
+
+  /**
+   * Previous in DLL
+   */
+  struct RIL_Address_Wrapped *prev;
+
+  /**
+   * The address
+   */
+  struct ATS_Address *address_naked;
+};
+
 struct RIL_Peer_Agent
 {
   /**
@@ -119,7 +153,7 @@ struct RIL_Peer_Agent
   /**
    * Whether the agent is active or not
    */
-  int active;
+  int active; //TODO? rename into "requested", since it rather depicts whether there is a request pending for it
 
   /**
    * Number of performed time-steps
@@ -134,12 +168,12 @@ struct RIL_Peer_Agent
   /**
    * Number of rows of W / Number of state-vector features
    */
-  int m;
+  unsigned int m;
 
   /**
    * Number of columns of W / Number of actions
    */
-  int n;
+  unsigned int n;
 
   /**
    * Last perceived state feature vector
@@ -159,7 +193,17 @@ struct RIL_Peer_Agent
   /**
    * Address in use
    */
-  struct ATS_Address * address;
+  struct ATS_Address * address_inuse;
+
+  /**
+   * Head of addresses DLL
+   */
+  struct RIL_Address_Wrapped * addresses_head;
+
+  /**
+   * Tail of addresses DLL
+   */
+  struct RIL_Address_Wrapped * addresses_tail;
 
   /**
    * Inbound bandwidth assigned by the agent
@@ -246,7 +290,7 @@ struct GAS_RIL_Handle
   /**
    * Hashmap containing all valid addresses
    */
-  const struct GNUNET_CONTAINER_MultiHashMap *addresses;
+  const struct GNUNET_CONTAINER_MultiPeerMap *addresses;
 
   /**
    * Callbacks for the solver
@@ -338,13 +382,48 @@ agent_decide_exploration (struct RIL_Peer_Agent *agent)
   double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
       UINT32_MAX) / (double) UINT32_MAX;
 
-  if (r < RIL_EXPLORE_RATIO)
+if  (r < RIL_EXPLORE_RATIO)
   {
     return GNUNET_YES;
   }
   return GNUNET_NO;
 }
 
+static int
+agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
+{
+  int i;
+  struct RIL_Address_Wrapped *cur;
+
+  i = -1;
+  for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
+  {
+    i++;
+    if (cur->address_naked == address)
+    {
+      return i;
+    }
+  }
+
+  return i;
+}
+
+static struct RIL_Address_Wrapped *
+agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
+{
+  struct RIL_Address_Wrapped *cur;
+
+  for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
+  {
+    if (cur->address_naked == address)
+    {
+      return cur;
+    }
+  }
+
+  return NULL ;
+}
+
 /**
  * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
  * most reward in the future)
@@ -356,7 +435,7 @@ static int
 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
 {
   int i;
-  int max_i = -1;
+  int max_i = RIL_ACTION_INVALID;
   double cur_q;
   double max_q = -DBL_MAX;
 
@@ -370,7 +449,7 @@ agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
     }
   }
 
-  GNUNET_assert(-1 != max_i);
+  GNUNET_assert(RIL_ACTION_INVALID != max_i);
 
   return max_i;
 }
@@ -395,10 +474,7 @@ agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
  * @param a_prime the new
  */
 static void
-agent_update_weights (struct RIL_Peer_Agent *agent,
-    double reward,
-    double *s_next,
-    int a_prime)
+agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
 {
   int i;
   double delta;
@@ -422,8 +498,7 @@ agent_update_weights (struct RIL_Peer_Agent *agent,
  * @param mod
  */
 static void
-agent_modify_eligibility (struct RIL_Peer_Agent *agent,
-    enum RIL_E_Modification mod)
+agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
 {
   int i;
   double *e = agent->e;
@@ -451,41 +526,76 @@ agent_modify_eligibility (struct RIL_Peer_Agent *agent,
 }
 
 static void
-envi_change_active_address (struct GAS_RIL_Handle *solver,
+envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
     struct RIL_Peer_Agent *agent,
     struct ATS_Address *new_address,
     unsigned long long new_bw_in,
-    unsigned long long new_bw_out)
+    unsigned long long new_bw_out,
+    int silent)
 {
   int notify = GNUNET_NO;
 
-  if (agent->address != new_address)
-  {
-    agent->address->active = GNUNET_NO;
-    agent->address = new_address;
-    agent->address->active = GNUNET_YES;
-    agent->address->assigned_bw_in.value__ = htonl (agent->bw_in);
-    agent->address->assigned_bw_out.value__ = htonl (agent->bw_out);
-    notify |= GNUNET_YES;
-  }
-  if (agent->bw_in != new_bw_in)
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "set_active_suggestion()\n");
+
+  //address change
+  if (agent->address_inuse != new_address)
   {
-    agent->bw_in = new_bw_in;
-    agent->address->assigned_bw_in.value__ = htonl (new_bw_out);
+    if (NULL != agent->address_inuse)
+    {
+      agent->address_inuse->active = GNUNET_NO;
+      agent->address_inuse->assigned_bw_in.value__ = htonl (0);
+      agent->address_inuse->assigned_bw_out.value__ = htonl (0);
+    }
+    if (NULL != new_address)
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "set address active: %s\n", agent->active ? "yes" : "no");
+      new_address->active = agent->active;
+      new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
+      new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
+    }
     notify |= GNUNET_YES;
   }
-  if (agent->bw_out != new_bw_out)
+
+  if (new_address)
   {
-    agent->bw_out = new_bw_out;
-    agent->address->assigned_bw_out.value__ = htonl (new_bw_out);
-    notify |= GNUNET_YES;
+    //activity change
+    if (new_address->active != agent->active)
+    {
+      new_address->active = agent->active;
+    }
+
+    //bw change
+    if (agent->bw_in != new_bw_in)
+    {
+      agent->bw_in = new_bw_in;
+      new_address->assigned_bw_in.value__ = htonl (new_bw_out);
+      notify |= GNUNET_YES;
+    }
+    if (agent->bw_out != new_bw_out)
+    {
+      agent->bw_out = new_bw_out;
+      new_address->assigned_bw_out.value__ = htonl (new_bw_out);
+      notify |= GNUNET_YES;
+    }
   }
 
-  if (notify)
+  if (notify && agent->active && (GNUNET_NO == silent))
   {
-    solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls,
-        agent->address);
+    if (new_address)
+    {
+      solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, new_address);
+    }
+    else
+    {
+      GNUNET_assert (0 == ntohl(agent->address_inuse->assigned_bw_in.value__));
+      GNUNET_assert (0 == ntohl(agent->address_inuse->assigned_bw_out.value__));
+      agent->bw_in = 0;
+      agent->bw_out = 0;
+      //disconnect
+      solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, agent->address_inuse);
+    }
   }
+  agent->address_inuse = new_address;
 }
 
 /**
@@ -494,19 +604,36 @@ envi_change_active_address (struct GAS_RIL_Handle *solver,
  * @return pointer to the state vector
  */
 static double *
-envi_get_state (struct GAS_RIL_Handle *solver)
+envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 {
   int i;
+  int k;
   struct RIL_Network *net;
-  double *state = GNUNET_malloc (sizeof (double) * solver->networks_count * 4);
+  double *state = GNUNET_malloc (sizeof (double) * agent->m);
+  struct RIL_Address_Wrapped *cur_address;
+  const double *properties;
 
   for (i = 0; i < solver->networks_count; i++)
   {
     net = &solver->network_entries[i];
-    state[i * 4 + 0] = (double) net->bw_in_assigned;
-    state[i * 4 + 1] = (double) net->bw_in_available;
-    state[i * 4 + 2] = (double) net->bw_out_assigned;
-    state[i * 4 + 3] = (double) net->bw_out_available;
+    state[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) net->bw_in_assigned;
+    state[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net->bw_in_available;
+    state[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) net->bw_out_assigned;
+    state[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net->bw_out_available;
+  }
+
+  i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
+
+  for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
+  {
+    state[i++] = cur_address->address_naked->active;
+    state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
+    state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
+    properties = solver->callbacks->get_properties (solver->callbacks->get_properties_cls, cur_address->address_naked);
+    for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
+    {
+      state[i++] = properties[k];
+    }
   }
 
   return state;
@@ -522,8 +649,8 @@ envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 {
   //TODO! implement reward calculation
 
-  return (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
-      UINT32_MAX) / (double) UINT32_MAX;
+  return (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX)
+      / (double) UINT32_MAX;
 }
 
 static void
@@ -533,13 +660,13 @@ envi_action_bw_double (struct GAS_RIL_Handle *solver,
 {
   if (direction_in)
   {
-    envi_change_active_address (solver, agent, agent->address, agent->bw_in * 2,
-        agent->bw_out);
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2,
+        agent->bw_out, GNUNET_NO);
   }
   else
   {
-    envi_change_active_address (solver, agent, agent->address, agent->bw_in,
-        agent->bw_out * 2);
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
+        agent->bw_out * 2, GNUNET_NO);
   }
 }
 
@@ -556,42 +683,36 @@ envi_action_bw_halven (struct GAS_RIL_Handle *solver,
     new_bw = agent->bw_in / 2;
     if (new_bw < min_bw)
       new_bw = min_bw;
-    envi_change_active_address (solver, agent, agent->address, new_bw,
-        agent->bw_out);
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out, GNUNET_NO);
   }
   else
   {
     new_bw = agent->bw_out / 2;
     if (new_bw < min_bw)
       new_bw = min_bw;
-    envi_change_active_address (solver, agent, agent->address, agent->bw_in,
-        new_bw);
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw, GNUNET_NO);
   }
 }
 
 static void
-envi_action_bw_inc (struct GAS_RIL_Handle *solver,
-    struct RIL_Peer_Agent *agent,
-    int direction_in)
+envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
 {
   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
 
   if (direction_in)
   {
-    envi_change_active_address (solver, agent, agent->address,
-        agent->bw_in + (5 * min_bw), agent->bw_out);
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
+        agent->bw_out, GNUNET_NO);
   }
   else
   {
-    envi_change_active_address (solver, agent, agent->address, agent->bw_in,
-        agent->bw_out + (5 * min_bw));
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
+        agent->bw_out + (5 * min_bw), GNUNET_NO);
   }
 }
 
 static void
-envi_action_bw_dec (struct GAS_RIL_Handle *solver,
-    struct RIL_Peer_Agent *agent,
-    int direction_in)
+envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
 {
   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
   unsigned long long new_bw;
@@ -601,17 +722,38 @@ envi_action_bw_dec (struct GAS_RIL_Handle *solver,
     new_bw = agent->bw_in - (5 * min_bw);
     if (new_bw < min_bw)
       new_bw = min_bw;
-    envi_change_active_address (solver, agent, agent->address, new_bw,
-        agent->bw_out);
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out, GNUNET_NO);
   }
   else
   {
     new_bw = agent->bw_out - (5 * min_bw);
     if (new_bw < min_bw)
       new_bw = min_bw;
-    envi_change_active_address (solver, agent, agent->address, agent->bw_in,
-        new_bw);
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw, GNUNET_NO);
+  }
+}
+
+static void
+envi_action_address_switch (struct GAS_RIL_Handle *solver,
+    struct RIL_Peer_Agent *agent,
+    unsigned int address_index)
+{
+  struct RIL_Address_Wrapped *cur;
+  int i = 0;
+
+  for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
+  {
+    if (i == address_index)
+    {
+      envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out, GNUNET_NO);
+      return;
+    }
+
+    i++;
   }
+
+  //no address with address_index exists
+  GNUNET_assert(GNUNET_NO);
 }
 
 /**
@@ -620,10 +762,10 @@ envi_action_bw_dec (struct GAS_RIL_Handle *solver,
  * @param action action to perform by the solver
  */
 static void
-envi_do_action (struct GAS_RIL_Handle *solver,
-    struct RIL_Peer_Agent *agent,
-    int action)
+envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
 {
+  unsigned int address_index;
+
   switch (action)
   {
   case RIL_ACTION_NOTHING:
@@ -653,6 +795,17 @@ envi_do_action (struct GAS_RIL_Handle *solver,
     envi_action_bw_dec (solver, agent, GNUNET_NO);
     break;
   default:
+    if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n))
+    {
+      address_index = agent->n - RIL_ACTION_TYPE_NUM;
+
+      GNUNET_assert(address_index >= 0);
+      GNUNET_assert(
+          address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
+
+      envi_action_address_switch (solver, agent, address_index);
+      break;
+    }
     // error - action does not exist
     GNUNET_assert(GNUNET_NO);
   }
@@ -668,14 +821,14 @@ envi_do_action (struct GAS_RIL_Handle *solver,
 static void
 agent_step (struct RIL_Peer_Agent *agent)
 {
-  int a_next = -1;
+  int a_next = RIL_ACTION_INVALID;
   double *s_next;
   double reward;
 
-  s_next = envi_get_state (agent->envi);
+  s_next = envi_get_state (agent->envi, agent);
   reward = envi_get_reward (agent->envi, agent);
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
       agent->envi->parameters.algorithm ? "Q" : "SARSA");
 
   switch (agent->envi->parameters.algorithm)
@@ -690,16 +843,20 @@ agent_step (struct RIL_Peer_Agent *agent)
     {
       a_next = agent_get_action_best (agent, s_next);
     }
-    //updates weights with selected action (on-policy), if not first step
-    if (-1 != agent->a_old)
+    if (RIL_ACTION_INVALID != agent->a_old)
+    {
+      //updates weights with selected action (on-policy), if not first step
       agent_update_weights (agent, reward, s_next, a_next);
+    }
     break;
 
   case RIL_ALGO_Q:
-    //updates weights with best action, disregarding actually selected action (off-policy), if not first step
     a_next = agent_get_action_best (agent, s_next);
-    if (-1 != agent->a_old)
+    if (RIL_ACTION_INVALID != agent->a_old)
+    {
+      //updates weights with best action, disregarding actually selected action (off-policy), if not first step
       agent_update_weights (agent, reward, s_next, a_next);
+    }
     if (agent_decide_exploration (agent))
     {
       a_next = agent_get_action_explore (agent, s_next);
@@ -713,7 +870,7 @@ agent_step (struct RIL_Peer_Agent *agent)
     break;
   }
 
-  GNUNET_assert(-1 != a_next);
+  GNUNET_assert(RIL_ACTION_INVALID != a_next);
 
   agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
 
@@ -737,20 +894,18 @@ ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct GAS_RIL_Handle *solver = cls;
   struct RIL_Peer_Agent *cur;
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n",
-      solver->step_count);
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
 
   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
   {
-    if (cur->active)
+    if (cur->active && cur->address_inuse)
     {
       agent_step (cur);
     }
   }
 
   solver->step_count += 1;
-  solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time,
-      &ril_periodic_step, solver);
+  solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, &ril_periodic_step, solver);
 }
 
 /**
@@ -770,20 +925,19 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
   agent->peer = *peer;
   agent->step_count = 0;
   agent->active = GNUNET_NO;
-  agent->s_old = envi_get_state (solver);
   agent->n = RIL_ACTION_TYPE_NUM;
-  agent->m = solver->networks_count * 4;
+  agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
   agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
   for (i = 0; i < agent->n; i++)
   {
     agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
   }
-  agent->a_old = -1;
+  agent->a_old = RIL_ACTION_INVALID;
+  agent->s_old = envi_get_state (solver, agent);
   agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
   agent_modify_eligibility (agent, RIL_E_ZERO);
 
-  GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail,
-      agent);
+  GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
 
   return agent;
 }
@@ -805,52 +959,9 @@ agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
   GNUNET_free(agent->W);
   GNUNET_free(agent->e);
   GNUNET_free(agent->s_old);
-}
-
-static void
-ril_remove_agent (struct GAS_RIL_Handle *s, struct RIL_Peer_Agent *agent)
-{
-  struct RIL_Peer_Agent *cur_agent;
-  struct RIL_Peer_Agent *next_agent;
-
-  cur_agent = s->agents_head;
-  while (NULL != cur_agent)
-  {
-    next_agent = cur_agent->next;
-
-    if (agent == cur_agent)
-    {
-      GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
-      agent_die (s, cur_agent);
-    }
-
-    cur_agent = next_agent;
-  }
+  GNUNET_free(agent);
 }
 
-/**
- * Counts the (active) agents
- * @param solver solver handle
- * @param active_only whether only active agents should be counted
- * @return number of agents
- */
-//static int
-//ril_count_agents (struct GAS_RIL_Handle *solver, int active_only)
-//{
-//  int c;
-//  struct RIL_Peer_Agent *cur;
-//
-//  c = 0;
-//  for (cur = solver->agents_head; NULL != cur; cur = cur->next)
-//  {
-//    if ((!active_only) || (active_only && cur->active))
-//    {
-//      c += 1;
-//    }
-//  }
-//  return c;
-//}
-
 /**
  * Returns the agent for a peer
  * @param s solver handle
@@ -859,112 +970,106 @@ ril_remove_agent (struct GAS_RIL_Handle *s, struct RIL_Peer_Agent *agent)
  * @return agent
  */
 static struct RIL_Peer_Agent *
-ril_get_agent (struct GAS_RIL_Handle *solver,
-    const struct GNUNET_PeerIdentity *peer,
-    int create)
+ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
 {
   struct RIL_Peer_Agent *cur;
 
   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
   {
-    if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey))
+    if (0 == memcmp (peer, 
+                    &cur->peer,
+                    sizeof (struct GNUNET_PeerIdentity)))
     {
       return cur;
     }
   }
 
   if (create)
+  {
     return agent_init (solver, peer);
+  }
   return NULL ;
 }
 
-static int
-ril_network_is_active (struct RIL_Network *network)
-{
-  uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
-
-  if (network->bw_out_available < min_bw)
-    return GNUNET_NO;
-  return GNUNET_YES;
-}
-
 /**
- * Iterator, which allocates one agent per peer
+ * Lookup network struct by type
  *
- * @param cls solver
- * @param key peer identity
- * @param value address
- * @return whether iterator should continue
+ * @param s the solver handle
+ * @param type the network type
+ * @return the network struct
  */
-static int
-ril_init_agents_it (void *cls, const struct GNUNET_HashCode *key, void *value)
+static struct RIL_Network *
+ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
 {
-  struct GAS_RIL_Handle *solver = cls;
-  struct ATS_Address *address = value;
-  struct RIL_Peer_Agent *agent = NULL;
-  uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
+  int i;
 
-  if (ril_network_is_active (address->solver_information))
+  for (i = 0; i < s->networks_count; i++)
   {
-    agent = ril_get_agent (solver, &address->peer, GNUNET_YES);
-
-    GNUNET_assert(NULL != agent);
-
-    if (NULL == agent->address)
+    if (s->network_entries[i].type == type)
     {
-      agent->address = address;
-      agent->address->active = GNUNET_YES;
-      agent->bw_in = min_bw;
-      agent->address->assigned_bw_in.value__ = htonl (min_bw);
-      agent->bw_out = min_bw;
-      agent->address->assigned_bw_out.value__ = htonl (min_bw);
+      return &s->network_entries[i];
     }
   }
+  return NULL ;
+}
+
+static int
+ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
+{
+  struct RIL_Network *net;
+  uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
+
+  net = ril_get_network (solver, network);
+  if (net->bw_out_available < min_bw)
+    return GNUNET_NO;
   return GNUNET_YES;
 }
 
 static void
-ril_get_new_address_or_delete (struct GAS_RIL_Handle *solver,
-    struct RIL_Peer_Agent *agent)
+ril_cut_from_vector (void **old,
+    size_t element_size,
+    unsigned int hole_start,
+    unsigned int hole_length,
+    unsigned int old_length)
 {
-  uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
-  //get new address for agent or delete agent
+  char *tmpptr;
+  char *oldptr = (char *) *old;
+  size_t size;
+  unsigned int bytes_before;
+  unsigned int bytes_hole;
+  unsigned int bytes_after;
 
-  agent->address = NULL; //forget current address
-  GNUNET_CONTAINER_multihashmap_iterate (solver->addresses, &ril_init_agents_it,
-      solver); //put another address
 
-  if (NULL == agent->address) //no other address available
+  GNUNET_assert(old_length > hole_length);
+  GNUNET_assert(old_length >= (hole_start + hole_length));
+
+  size = element_size * (old_length - hole_length);
+
+  bytes_before = element_size * hole_start;
+  bytes_hole = element_size * hole_length;
+  bytes_after = element_size * (old_length - hole_start - hole_length);
+
+  if (0 == size)
   {
-    agent->active = GNUNET_NO;
-    ril_remove_agent (solver, agent);
+    tmpptr = NULL;
   }
   else
   {
-    envi_change_active_address (solver, agent, agent->address, min_bw, min_bw);
+//    LOG(GNUNET_ERROR_TYPE_DEBUG, "hole_start = %d, hole_length = %d, old_length = %d\n", hole_start, hole_length, old_length);
+//    LOG(GNUNET_ERROR_TYPE_DEBUG, "bytes_before = %d, bytes_hole = %d, bytes_after = %d\n", bytes_before, bytes_hole, bytes_after);
+//    LOG(GNUNET_ERROR_TYPE_DEBUG, "element_size = %d, bytes_old = %d, bytes_new = %d\n", element_size, old_length * element_size, size);
+
+    tmpptr = GNUNET_malloc (size);
+//    LOG(GNUNET_ERROR_TYPE_DEBUG, "first\n");
+    memcpy (tmpptr, oldptr, bytes_before);
+//    LOG(GNUNET_ERROR_TYPE_DEBUG, "second\n");
+    memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
   }
-}
-
-/**
- * Lookup network struct by type
- *
- * @param s the solver handle
- * @param type the network type
- * @return the network struct
- */
-static struct RIL_Network *
-ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
-{
-  int i;
-
-  for (i = 0; i < s->networks_count; i++)
+  if (NULL != *old)
   {
-    if (s->network_entries[i].type == type)
-    {
-      return &s->network_entries[i];
-    }
+    GNUNET_free(*old);
   }
-  return NULL ;
+  *old = (void *) tmpptr;
 }
 
 /**
@@ -986,7 +1091,7 @@ GAS_ril_address_change_preference (void *s,
     enum GNUNET_ATS_PreferenceKind kind,
     double pref_rel)
 {
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
   /*
@@ -1026,7 +1131,7 @@ GAS_ril_address_change_preference (void *s,
 void *
 GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
     const struct GNUNET_STATISTICS_Handle *stats,
-    const struct GNUNET_CONTAINER_MultiHashMap *addresses,
+    const struct GNUNET_CONTAINER_MultiPeerMap *addresses,
     int *network,
     unsigned long long *out_quota,
     unsigned long long *in_quota,
@@ -1042,9 +1147,9 @@ GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   unsigned long long tmp;
   char *string;
   struct RIL_Network * cur;
-  struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle));
+  struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
 
   GNUNET_assert(NULL != cfg);
   GNUNET_assert(NULL != stats);
@@ -1054,14 +1159,12 @@ GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   GNUNET_assert(NULL != get_properties);
 
   if (GNUNET_OK
-      != GNUNET_CONFIGURATION_get_value_time (cfg, "ats", "RIL_STEP_TIME",
-          &solver->step_time))
+      != GNUNET_CONFIGURATION_get_value_time (cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
   {
     solver->step_time = RIL_DEFAULT_STEP_TIME;
   }
-  if (GNUNET_OK
-      == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "RIL_ALGORITHM",
-          &string) && NULL != string && 0 == strcmp (string, "SARSA"))
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "RIL_ALGORITHM", &string)
+      && NULL != string && 0 == strcmp (string, "SARSA"))
   {
     solver->parameters.algorithm = RIL_ALGO_SARSA;
   }
@@ -1069,9 +1172,7 @@ GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   {
     solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
   }
-  if (GNUNET_OK
-      == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_DISCOUNT_FACTOR",
-          &tmp))
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
   {
     solver->parameters.gamma = (double) tmp / 100;
   }
@@ -1079,9 +1180,7 @@ GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   {
     solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
   }
-  if (GNUNET_OK
-      == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
-          "RIL_GRADIENT_STEP_SIZE", &tmp))
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
   {
     solver->parameters.alpha = (double) tmp / 100;
   }
@@ -1089,9 +1188,7 @@ GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   {
     solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
   }
-  if (GNUNET_OK
-      == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_TRACE_DECAY",
-          &tmp))
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_TRACE_DECAY", &tmp))
   {
     solver->parameters.lambda = (double) tmp / 100;
   }
@@ -1109,8 +1206,7 @@ GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   solver->callbacks->get_properties = get_properties;
   solver->callbacks->get_properties_cls = get_properties_cls;
   solver->networks_count = dest_length;
-  solver->network_entries =
-      GNUNET_malloc (dest_length * sizeof (struct RIL_Network));
+  solver->network_entries = GNUNET_malloc (dest_length * sizeof (struct RIL_Network));
   solver->bulk_lock = GNUNET_NO;
   solver->addresses = addresses;
   solver->step_count = 0;
@@ -1126,8 +1222,8 @@ GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   }
 
   solver->next_step = GNUNET_SCHEDULER_add_delayed (
-      GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (),
-          1000), &ril_periodic_step, solver);
+      GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
+      &ril_periodic_step, solver);
 
   return solver;
 }
@@ -1144,7 +1240,7 @@ GAS_ril_done (void * solver)
   struct RIL_Peer_Agent *cur_agent;
   struct RIL_Peer_Agent *next_agent;
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
 
   cur_agent = s->agents_head;
   while (NULL != cur_agent)
@@ -1169,32 +1265,72 @@ GAS_ril_done (void * solver)
  * @param network network type of this address
  */
 void
-GAS_ril_address_add (void *solver,
-    struct ATS_Address *address,
-    uint32_t network)
+GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
 {
   struct GAS_RIL_Handle *s = solver;
-  //TODO! implement solver address add
-  /*
-   * if (new peer)
-   *     initialize new agent
-   * Add address
-   * increase state vector
-   * knowledge matrix
-   * and action vector
-   */
+  struct RIL_Peer_Agent *agent;
+  struct RIL_Address_Wrapped *address_wrapped;
+  unsigned int m_new;
+  unsigned int m_old;
+  unsigned int n_new;
+  unsigned int n_old;
+  int i;
+  unsigned int zero;
+  uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
 
   address->solver_information = ril_get_network (s, network);
 
-  /*
-   * reiterate all addresses, create new agent if necessary and give the agent the address
-   */
-  GNUNET_CONTAINER_multihashmap_iterate (s->addresses, &ril_init_agents_it,
-      solver);
+  if (!ril_network_is_active (s, network))
+  {
+    LOG(GNUNET_ERROR_TYPE_DEBUG,
+        "API_address_add() Did not add %s address %p for peer '%s', network does not have enough bandwidth\n",
+        address->plugin, address->addr, GNUNET_i2s (&address->peer));
+    return;
+  }
+
+  agent = ril_get_agent (s, &address->peer, GNUNET_YES);
+
+  //add address
+  address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
+  address_wrapped->address_naked = address;
+  GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-      "API_address_add() Added %s address for peer '%s'\n", address->plugin,
-      GNUNET_i2s (&address->peer));
+  //increase size of W
+  m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
+  m_old = agent->m;
+  n_new = agent->n + 1;
+  n_old = agent->n;
+
+  GNUNET_array_grow(agent->W, agent->n, n_new);
+  for (i = 0; i < n_new; i++)
+  {
+    if (i < n_old)
+    {
+      agent->m = m_old;
+      GNUNET_array_grow(agent->W[i], agent->m, m_new);
+    }
+    else
+    {
+      zero = 0;
+      GNUNET_array_grow(agent->W[i], zero, m_new);
+    }
+  }
+
+  //increase size of old state vector
+  agent->m = m_old;
+  GNUNET_array_grow(agent->s_old, agent->m, m_new); //TODO initialize new state features?
+
+  agent->m = m_old;
+  GNUNET_array_grow(agent->e, agent->m, m_new);
+
+  if (NULL == agent->address_inuse)
+  {
+    envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
+  }
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %p for peer '%s'\n",
+      address->active ? "active" : "inactive",
+      address->plugin, address->addr, GNUNET_i2s (&address->peer));
 }
 
 /**
@@ -1205,48 +1341,108 @@ GAS_ril_address_add (void *solver,
  * @param session_only delete only session not whole address
  */
 void
-GAS_ril_address_delete (void *solver,
-    struct ATS_Address *address,
-    int session_only)
+GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
 {
-  //TODO! implement solver address delete
   //TODO! delete session only
-  /*
-   * remove address
-   * if (last address of peer)
-   *     remove agent
-   * else
-   *     decrease state vector
-   *     decrease knowledge matrix
-   *     decrease action vector
-   */
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
+  struct RIL_Address_Wrapped *address_wrapped;
+  int address_was_used = address->active;
+  int address_index;
+  unsigned int m_new;
+  unsigned int n_new;
+  int i;
+  struct RIL_Network *net;
+  uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-      "API_address_delete() deleting %s address %p for peer '%s'\n",
-      address->active ? "active" : "inactive", address,
-      GNUNET_i2s (&address->peer));
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
+      session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
+      address->addr, GNUNET_i2s (&address->peer));
 
   agent = ril_get_agent (s, &address->peer, GNUNET_NO);
-
   if (NULL == agent)
   {
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-        "API_address_delete() deleting address for unallocated agent\n");
+    net = address->solver_information;
+    GNUNET_assert(!ril_network_is_active (s, net->type));
+    LOG(GNUNET_ERROR_TYPE_DEBUG,
+        "No agent allocated for peer yet, since address was in inactive network\n");
     return;
   }
 
-  if (address == agent->address) //if used address deleted
+  address_index = agent_address_get_index (agent, address);
+  address_wrapped = agent_address_get (agent, address);
+
+  if (NULL == address_wrapped)
   {
-    address->active = GNUNET_NO;
-    ril_get_new_address_or_delete (s, agent);
+    net = address->solver_information;
+    GNUNET_assert(!ril_network_is_active (s, net->type));
+    LOG(GNUNET_ERROR_TYPE_DEBUG,
+        "Address not considered by agent, address was in inactive network\n");
+    return;
   }
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-      "API_address_delete() Deleted %s%s address for peer '%s'\n",
-      session_only ? "session for " : "", address->plugin,
-      GNUNET_i2s (&address->peer));
+  GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
+  GNUNET_free(address_wrapped);
+
+  //decrease W
+  m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
+  n_new = agent->n - 1;
+
+  for (i = 0; i < agent->n; i++)
+  {
+//    LOG (GNUNET_ERROR_TYPE_DEBUG, "first - cut vectors in W\n");
+    ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
+        ((s->networks_count * RIL_FEATURES_NETWORK_COUNT) + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
+  }
+//  LOG (GNUNET_ERROR_TYPE_DEBUG, "second - cut action vector out of W\n");
+  GNUNET_free (agent->W[RIL_ACTION_TYPE_NUM + address_index]);
+  ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
+      1, agent->n);
+  //correct last action
+  if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
+  {
+    agent->a_old -= 1;
+  }
+  else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
+  {
+    agent->a_old = RIL_ACTION_INVALID;
+  }
+  //decrease old state vector and eligibility vector
+//  LOG (GNUNET_ERROR_TYPE_DEBUG, "third - cut state vector\n");
+  ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
+      ((s->networks_count * RIL_FEATURES_NETWORK_COUNT) + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
+//  LOG (GNUNET_ERROR_TYPE_DEBUG, "fourth - cut eligibility vector\n");
+  ril_cut_from_vector ((void **) &agent->e, sizeof(double),
+      ((s->networks_count * RIL_FEATURES_NETWORK_COUNT) + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
+  agent->m = m_new;
+  agent->n = n_new;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
+
+  if (address_was_used)
+  {
+    net = address->solver_information;
+    net->bw_in_assigned -= agent->bw_in;
+    net->bw_out_assigned -= agent->bw_out;
+
+    if (NULL != agent->addresses_head) //if peer has an address left, use it
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "address left: %p\n", agent->addresses_head->address_naked->addr);
+      //TODO? check if network/bandwidth update can be done more clever/elegant at different function
+      envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw, GNUNET_NO);
+      net = agent->addresses_head->address_naked->solver_information;
+      net->bw_in_assigned -= min_bw;
+      net->bw_out_assigned -= min_bw;
+    }
+    else
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
+
+      envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
+    }
+  }
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
 }
 
 /**
@@ -1265,10 +1461,10 @@ GAS_ril_address_property_changed (void *solver,
     uint32_t abs_value,
     double rel_value)
 {
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
-          "to %.2f \n", GNUNET_ATS_print_property_type (type),
-      GNUNET_i2s (&address->peer), address, rel_value);
+          "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
+      address->addr, rel_value);
   /*
    * Nothing to do here, properties are considered in every reward calculation
    */
@@ -1294,7 +1490,7 @@ GAS_ril_address_session_changed (void *solver,
   /*
    * Potentially add session activity as a feature in state vector
    */
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
 }
 
 /**
@@ -1307,18 +1503,15 @@ GAS_ril_address_session_changed (void *solver,
  * @param in_use usage state
  */
 void
-GAS_ril_address_inuse_changed (void *solver,
-    struct ATS_Address *address,
-    int in_use)
+GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
 {
   //TODO! consider address_inuse_changed according to matthias' email
   /**
    * See matthias' email
    */
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
-      address->plugin, GNUNET_i2s (&address->peer),
-      (GNUNET_YES == in_use) ? "USED" : "UNUSED");
+      address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
 }
 
 /**
@@ -1340,54 +1533,36 @@ GAS_ril_address_change_network (void *solver,
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
   struct RIL_Network *net;
+  uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-      "API_address_change_network() Network type changed, moving "
-          "%s address of peer %s from '%s' to '%s'\n",
-      (GNUNET_YES == address->active) ? "active" : "inactive",
-      GNUNET_i2s (&address->peer),
-      GNUNET_ATS_print_network_type (current_network),
-      GNUNET_ATS_print_network_type (new_network));
-
-  address->solver_information = ril_get_network (solver, new_network);
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
+      "%s address of peer %s from '%s' to '%s'\n",
+      (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
+      GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
 
-  if (address->active)
+  if (address->active && !ril_network_is_active (solver, new_network))
   {
-    agent = ril_get_agent (solver, &address->peer, GNUNET_NO);
-
-    //remove from old network
-    net = ril_get_network (s, current_network);
-    net->bw_in_assigned -= agent->bw_in;
-    net->bw_out_assigned -= agent->bw_out;
-
-    if (ril_network_is_active (ril_get_network (s, new_network)))
-    {
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New network is active\n");
-      //add to new network
-      net = ril_get_network (s, new_network);
-      net->bw_in_assigned += agent->bw_in;
-      net->bw_out_assigned += agent->bw_out;
-
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-          "API_address_change_network() Moved %d inbound and %d "
-              "outbound\n", agent->bw_in, agent->bw_out);
-    }
-    else //new network for this address is not active => address must not be considered
-    {
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New network is not active\n");
+    GAS_ril_address_delete (solver, address, GNUNET_NO);
+    return;
+  }
 
-      net = agent->address->solver_information;
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Before: active address %p is %s\n",
-          agent->address, GNUNET_ATS_print_network_type (net->type));
+  agent = ril_get_agent (s, &address->peer, GNUNET_NO);
+  if (NULL == agent)
+  {
+    //no agent there yet, so add as if address is new
+    address->solver_information = ril_get_network (s, new_network);
+    GAS_ril_address_add (s, address, new_network);
+    return;
+  }
 
-      address->active = GNUNET_NO;
-      ril_get_new_address_or_delete (s, agent);
+  net = ril_get_network (s, current_network);
+  net->bw_in_assigned -= agent->bw_in;
+  net->bw_out_assigned -= agent->bw_out;
 
-      net = agent->address->solver_information;
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "After: active address %p is %s\n",
-          agent->address, GNUNET_ATS_print_network_type (net->type));
-    }
-  }
+  net = ril_get_network (s, new_network);
+  net->bw_in_assigned -= min_bw;
+  net->bw_out_assigned -= min_bw;
+  address->solver_information = net;
 }
 
 /**
@@ -1409,7 +1584,7 @@ GAS_ril_address_preference_feedback (void *solver,
     double score)
 {
   //TODO! collect reward until next reward calculation
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
           "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
       GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
@@ -1430,7 +1605,7 @@ GAS_ril_bulk_start (void *solver)
    * they want. Consideration: Step-pause during bulk-start-stop period...
    */
 
-  //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n");
+  //LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n");
 }
 
 /**
@@ -1444,7 +1619,7 @@ GAS_ril_bulk_stop (void *solver)
    * bulk counter down, see bulk_start()
    */
 
-  //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n");
+  //LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n");
 }
 
 /**
@@ -1454,8 +1629,7 @@ GAS_ril_bulk_stop (void *solver)
  * @param peer the identity of the peer
  */
 const struct ATS_Address *
-GAS_ril_get_preferred_address (void *solver,
-    const struct GNUNET_PeerIdentity *peer)
+GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
 {
   /*
    * activate agent, return currently chosen address
@@ -1463,25 +1637,25 @@ GAS_ril_get_preferred_address (void *solver,
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
 
-  agent = ril_get_agent (s, peer, GNUNET_NO);
-
-  if (NULL == agent)
-  {
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-        "API_get_preferred_address() No agent for peer '%s' do not suggest address\n",
-        GNUNET_i2s (peer));
-    return NULL ;
-  }
+  agent = ril_get_agent (s, peer, GNUNET_YES);
 
   agent->active = GNUNET_YES;
 
-  GNUNET_assert(NULL != agent->address);
+  envi_set_active_suggestion(s, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-      "API_get_preferred_address() Activated agent for peer '%s' with %s address\n",
-      GNUNET_i2s (peer), agent->address->plugin);
+  if (agent->address_inuse)
+  {
+    LOG(GNUNET_ERROR_TYPE_DEBUG,
+        "API_get_preferred_address() Activated agent for peer '%s' with %s address %p\n",
+        GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
+  }
+  else
+  {
+    LOG(GNUNET_ERROR_TYPE_DEBUG,
+            "API_get_preferred_address() Activated agent for peer '%s', but no address available\n", GNUNET_i2s (peer));
+  }
 
-  return agent->address;
+  return agent->address_inuse;
 }
 
 /**
@@ -1491,18 +1665,30 @@ GAS_ril_get_preferred_address (void *solver,
  * @param peer the peer
  */
 void
-GAS_ril_stop_get_preferred_address (void *solver,
-    const struct GNUNET_PeerIdentity *peer)
+GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
 {
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
 
   agent = ril_get_agent (s, peer, GNUNET_NO);
+
+  if (NULL == agent)
+  {
+    GNUNET_break(0);
+    return;
+  }
+  if (GNUNET_NO == agent->active)
+  {
+    GNUNET_break(0);
+    return;
+  }
+
   agent->active = GNUNET_NO;
+  envi_set_active_suggestion(s, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
-      GNUNET_i2s (peer), agent->address->plugin);
+      GNUNET_i2s (peer), agent->address_inuse->plugin);
 }
 
 /* end of gnunet-service-ats-solver_ril.c */