fix warning
[oweals/gnunet.git] / src / ats / gnunet-service-ats-solver_ril.c
index c80b85604d4ce76bbaee1199be9f70b559375e99..5121846270fb9dfc328fa8657aebf38a9e4df6ba 100755 (executable)
 
 #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
@@ -46,7 +50,6 @@
  */
 
 /**
- * TODO! implement address administration
  * TODO! implement reward calculation 1 and 2 (i.e. meeting preferences and taking scores)
  */
 
@@ -63,7 +66,6 @@ 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
 {
@@ -151,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
@@ -288,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
@@ -393,17 +395,17 @@ agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *addre
   int i;
   struct RIL_Address_Wrapped *cur;
 
-  i = 0;
+  i = -1;
   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
   {
+    i++;
     if (cur->address_naked == address)
     {
       return i;
     }
-    i++;
   }
 
-  return -1;
+  return i;
 }
 
 static struct RIL_Address_Wrapped *
@@ -419,21 +421,21 @@ agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
     }
   }
 
-  return NULL;
+  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)
  * @param agent agent performing the calculation
  * @param state the state from which to take the action
  * @return the action promising most future reward
  */
+/**
+ * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
+ * most reward in the future)
+ * @param agent agent performing the calculation
+ * @param state the state from which to take the action
+ * @return the action promising most future reward
+ */
 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;
 
@@ -447,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;
 }
@@ -528,39 +530,72 @@ 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;
 
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "set_active_suggestion()\n");
+
+  //address change
   if (agent->address_inuse != new_address)
   {
     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);
     }
-    agent->address_inuse = new_address;
-    agent->address_inuse->active = GNUNET_YES;
-    agent->address_inuse->assigned_bw_in.value__ = htonl (agent->bw_in);
-    agent->address_inuse->assigned_bw_out.value__ = htonl (agent->bw_out);
-    notify |= GNUNET_YES;
-  }
-  if (agent->bw_in != new_bw_in)
-  {
-    agent->bw_in = new_bw_in;
-    agent->address_inuse->assigned_bw_in.value__ = htonl (new_bw_out);
     notify |= GNUNET_YES;
   }
-  if (agent->bw_out != new_bw_out)
+
+  if (new_address)
   {
-    agent->bw_out = new_bw_out;
-    agent->address_inuse->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 && agent->active)
+  if (notify && agent->active && (GNUNET_NO == silent))
   {
-    solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, agent->address_inuse);
+    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;
 }
 
 /**
@@ -569,19 +604,36 @@ envi_set_active_suggestion (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;
@@ -608,11 +660,13 @@ envi_action_bw_double (struct GAS_RIL_Handle *solver,
 {
   if (direction_in)
   {
-    envi_set_active_suggestion (solver, agent, agent->address_inuse, 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_set_active_suggestion (solver, agent, agent->address_inuse, 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);
   }
 }
 
@@ -629,14 +683,14 @@ 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_set_active_suggestion (solver, agent, agent->address_inuse, 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_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw);
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw, GNUNET_NO);
   }
 }
 
@@ -648,12 +702,12 @@ envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
   if (direction_in)
   {
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
-        agent->bw_out);
+        agent->bw_out, GNUNET_NO);
   }
   else
   {
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
-        agent->bw_out + (5 * min_bw));
+        agent->bw_out + (5 * min_bw), GNUNET_NO);
   }
 }
 
@@ -668,15 +722,38 @@ envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
     new_bw = agent->bw_in - (5 * min_bw);
     if (new_bw < min_bw)
       new_bw = min_bw;
-    envi_set_active_suggestion (solver, agent, agent->address_inuse, 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_set_active_suggestion (solver, agent, agent->address_inuse, 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);
 }
 
 /**
@@ -687,6 +764,8 @@ envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
 static void
 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:
@@ -716,6 +795,17 @@ envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int
     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);
   }
@@ -731,11 +821,11 @@ envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int
 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);
 
   LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
@@ -753,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);
@@ -776,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);
 
@@ -804,7 +898,7 @@ ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
   {
-    if (cur->active)
+    if (cur->active && cur->address_inuse)
     {
       agent_step (cur);
     }
@@ -831,15 +925,15 @@ 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);
 
@@ -865,6 +959,7 @@ 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);
+  GNUNET_free(agent);
 }
 
 /**
@@ -881,14 +976,18 @@ ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *
 
   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 ;
 }
 
@@ -920,14 +1019,18 @@ ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Ty
   struct RIL_Network *net;
   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
 
-  net = ril_get_network(solver, network);
+  net = ril_get_network (solver, network);
   if (net->bw_out_available < min_bw)
     return GNUNET_NO;
   return GNUNET_YES;
 }
 
 static void
-ril_cut_from_vector (void **old, size_t element_size, unsigned int hole_start, unsigned int hole_length, unsigned int old_length)
+ril_cut_from_vector (void **old,
+    size_t element_size,
+    unsigned int hole_start,
+    unsigned int hole_length,
+    unsigned int old_length)
 {
   char *tmpptr;
   char *oldptr = (char *) *old;
@@ -936,14 +1039,15 @@ ril_cut_from_vector (void **old, size_t element_size, unsigned int hole_start, u
   unsigned int bytes_hole;
   unsigned int bytes_after;
 
+
   GNUNET_assert(old_length > hole_length);
-  GNUNET_assert(old_length > (hole_start + hole_length - 1));
+  GNUNET_assert(old_length >= (hole_start + hole_length));
 
-  size = (old_length - hole_length) * element_size;
+  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);
+  bytes_hole = element_size * hole_length;
+  bytes_after = element_size * (old_length - hole_start - hole_length);
 
   if (0 == size)
   {
@@ -951,8 +1055,14 @@ ril_cut_from_vector (void **old, size_t element_size, unsigned int hole_start, u
   }
   else
   {
+//    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);
   }
   if (NULL != *old)
@@ -1021,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,
@@ -1037,7 +1147,7 @@ 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);
 
   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
 
@@ -1170,14 +1280,15 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
 
   address->solver_information = ril_get_network (s, network);
 
-  if (!ril_network_is_active(s, network))
+  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",
+    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);
+  agent = ril_get_agent (s, &address->peer, GNUNET_YES);
 
   //add address
   address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
@@ -1185,12 +1296,12 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
   GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
 
   //increase size of W
-  m_new = agent->m + 5; //TODO! make size of features from address variable (Note to self: ctrl+f for "5" or I kill you!)
+  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);
+  GNUNET_array_grow(agent->W, agent->n, n_new);
   for (i = 0; i < n_new; i++)
   {
     if (i < n_old)
@@ -1205,22 +1316,20 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
     }
   }
 
-  //increase size of old state vector if there is one
-  if (-1 != agent->a_old)
-  {
-    agent->m = m_old;
-    GNUNET_array_grow(agent->s_old, agent->m, m_new); //TODO initialize new state features?
-  }
+  //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);
+    envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
   }
 
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s address %p for peer '%s'\n",
+  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));
 }
 
@@ -1246,26 +1355,49 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
   struct RIL_Network *net;
   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
 
-  agent = ril_get_agent(s, &address->peer, GNUNET_NO);
+  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)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "No agent allocated for peer\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;
   }
 
-  address_index = agent_address_get_index(agent, address);
-  address_wrapped = agent_address_get(agent, address);
+  address_index = agent_address_get_index (agent, address);
+  address_wrapped = agent_address_get (agent, address);
+
+  if (NULL == address_wrapped)
+  {
+    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_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
+  GNUNET_free(address_wrapped);
 
   //decrease W
-  m_new = agent->m - 5;
+  m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
   n_new = agent->n - 1;
 
   for (i = 0; i < agent->n; i++)
   {
-    ril_cut_from_vector((void **) &agent->W[i], sizeof (double), ((s->networks_count * 4) + (i * 5)), 5, agent->m);
+//    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);
   }
-  ril_cut_from_vector((void **) &agent->W, sizeof (double *), RIL_ACTION_TYPE_NUM + address_index, 1, agent->n);
+//  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))
   {
@@ -1273,29 +1405,44 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
   }
   else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
   {
-    agent->a_old = -1; //TODO! macro for invalid action
+    agent->a_old = RIL_ACTION_INVALID;
   }
   //decrease old state vector and eligibility vector
-  ril_cut_from_vector((void **) &agent->s_old, sizeof (double), ((s->networks_count * 4) + (i * 5)), 5, agent->m);
-  ril_cut_from_vector((void **) &agent->e,     sizeof (double), ((s->networks_count * 4) + (i * 5)), 5, agent->m);
+//  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;
 
-    //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);
-    net = agent->addresses_head->address_naked->solver_information;
-    net->bw_in_assigned -= min_bw;
-    net->bw_out_assigned -= min_bw;
+    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, "API_address_delete() Deleted %s%s address for peer '%s'\n",
-      session_only ? "session for " : "", address->plugin, GNUNET_i2s (&address->peer));
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
 }
 
 /**
@@ -1316,8 +1463,8 @@ GAS_ril_address_property_changed (void *solver,
 {
   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
    */
@@ -1393,18 +1540,18 @@ GAS_ril_address_change_network (void *solver,
       (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 && !ril_network_is_active(solver, new_network))
+  if (address->active && !ril_network_is_active (solver, new_network))
   {
-    GAS_ril_address_delete(solver, address, GNUNET_NO);
+    GAS_ril_address_delete (solver, address, GNUNET_NO);
     return;
   }
 
-  agent = ril_get_agent(s, &address->peer, GNUNET_NO);
+  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);
+    GAS_ril_address_add (s, address, new_network);
     return;
   }
 
@@ -1490,23 +1637,23 @@ GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *p
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
 
-  agent = ril_get_agent (s, peer, GNUNET_NO);
-
-  if (NULL == agent)
-  {
-    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_inuse);
+  envi_set_active_suggestion(s, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
 
-  LOG(GNUNET_ERROR_TYPE_DEBUG,
-      "API_get_preferred_address() Activated agent for peer '%s' with %s address\n",
-      GNUNET_i2s (peer), agent->address_inuse->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_inuse;
 }
@@ -1524,7 +1671,20 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
   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);
 
   LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",