-need to do mkdir before testing UNIXPATH, not sure how this worked before
[oweals/gnunet.git] / src / ats / libgnunet_plugin_ats_ril.c
index 6d2bb5e31c484a1207fa10ff96dc6acdb73afc1d..2a35007f3b175161afdd3a894d2a677f0b549544 100755 (executable)
@@ -151,7 +151,7 @@ struct RIL_Peer_Agent
   /**
    * Whether the agent is active or not
    */
-  int active; //TODO? rename into something better. It reflects the state whether get_preferred_address() has been called for the according peer or not
+  int active;
 
   /**
    * Number of performed time-steps
@@ -242,39 +242,6 @@ struct RIL_Network
   unsigned long long bw_out_assigned;
 };
 
-struct RIL_Callbacks
-{
-  /**
-   * Bandwidth changed callback
-   */
-  GAS_bandwidth_changed_cb bw_changed;
-
-  /**
-   * Bandwidth changed callback cls
-   */
-  void *bw_changed_cls;
-
-  /**
-   * ATS function to get preferences for a peer
-   */
-  GAS_get_preferences get_preferences;
-
-  /**
-   * Closure for ATS function to get preferences
-   */
-  void *get_preferences_cls;
-
-  /**
-   * ATS function to get properties of an address
-   */
-  GAS_get_properties get_properties;
-
-  /**
-   * Closure for ATS function to get properties
-   */
-  void *get_properties_cls;
-};
-
 /**
  * A handle for the reinforcement learning solver
  */
@@ -290,16 +257,6 @@ struct GAS_RIL_Handle
    */
   struct GNUNET_STATISTICS_Handle *stats;
 
-  /**
-   * Hashmap containing all valid addresses
-   */
-  const struct GNUNET_CONTAINER_MultiPeerMap *addresses;
-
-  /**
-   * Callbacks for the solver
-   */
-  struct RIL_Callbacks *callbacks;
-
   /**
    * Number of performed time-steps
    */
@@ -344,6 +301,7 @@ struct GAS_RIL_Handle
 
 /**
  * Estimate the current action-value for state s and action a
+ *
  * @param agent agent performing the estimation
  * @param state s
  * @param action a
@@ -366,6 +324,7 @@ agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
 /**
  * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
  * currently estimated best action) in the current step
+ *
  * @param agent agent performing the step
  * @return yes, if exploring
  */
@@ -379,19 +338,20 @@ 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;
 }
 
-/**
- * Get the index of the address in the agent's list.
- * @param agent agent handle
- * @param address address handle
- * @return the index, starting with zero
- */
+  /**
+   * Get the index of the address in the agent's list.
+   *
+   * @param agent agent handle
+   * @param address address handle
+   * @return the index, starting with zero
+   */
 static int
 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
 {
@@ -413,6 +373,7 @@ agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *addre
 
 /**
  * Gets the wrapped address from the agent's list
+ *
  * @param agent agent handle
  * @param address address handle
  * @return wrapped address
@@ -436,6 +397,7 @@ agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
 /**
  * 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
@@ -465,6 +427,7 @@ agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
 
 /**
  * Gets any action, to explore the action space from that state
+ *
  * @param agent agent performing the calculation
  * @param state the state from which to take the action
  * @return any action
@@ -478,6 +441,7 @@ agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
 
 /**
  * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
+ *
  * @param agent the agent performing the update
  * @param reward the reward received for the last action
  * @param s_next the new state, the last step got the agent into
@@ -504,8 +468,9 @@ agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_nex
  * RIL_E_REPLACE - resets each component to 1 as in replacing traces
  * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
  * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
- * @param agent
- * @param mod
+ *
+ * @param agent the agent handle
+ * @param mod the kind of modification
  */
 static void
 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
@@ -537,7 +502,8 @@ agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification
 
 /**
  * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
- * notify ATS of its new decision.
+ * notify ATS of its new decision
+ *
  * @param solver solver handle
  * @param agent agent handle
  * @param new_address the address which is to be used
@@ -555,7 +521,7 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
 {
   int notify = GNUNET_NO;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "set_active_suggestion()\n");
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "set_active_suggestion()\n");
 
   //address change
   if (agent->address_inuse != new_address)
@@ -568,7 +534,7 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
     }
     if (NULL != new_address)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "set address active: %s\n", agent->active ? "yes" : "no");
+      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);
@@ -603,16 +569,18 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
   {
     if (new_address)
     {
-      solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, new_address);
+      solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
+          new_address);
     }
-    else
+    else if (agent->address_inuse)
     {
-      GNUNET_assert (0 == ntohl(agent->address_inuse->assigned_bw_in.value__));
-      GNUNET_assert (0 == ntohl(agent->address_inuse->assigned_bw_out.value__));
+      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);
+      solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
+          agent->address_inuse);
     }
   }
   agent->address_inuse = new_address;
@@ -620,6 +588,7 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
 
 /**
  * Allocates a state vector and fills it with the features present
+ *
  * @param solver the solver handle
  * @return pointer to the state vector
  */
@@ -649,7 +618,8 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
     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);
+    properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
+        cur_address->address_naked);
     for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
     {
       state[i++] = properties[k];
@@ -662,12 +632,13 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 /**
  * For all networks a peer has an address in, this gets the maximum bandwidth which could
  * theoretically be available in one of the networks. This is used for bandwidth normalization.
+ *
  * @param solver the solver handle
  * @param agent the agent handle
  * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
  */
 static long long unsigned
-ril_get_max_bw(struct RIL_Peer_Agent *agent, int direction_in)
+ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
 {
   /*
    * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
@@ -700,6 +671,7 @@ ril_get_max_bw(struct RIL_Peer_Agent *agent, int direction_in)
 
 /**
  * Get the index of the quality-property in question
+ *
  * @param type the quality property type
  * @return the index
  */
@@ -715,7 +687,8 @@ ril_find_property_index (uint32_t type)
 }
 
 /**
- * Gets the reward of the last performed step
+ * Gets the reward for the last performed step
+ *
  * @param solver solver handle
  * @return the reward
  */
@@ -723,7 +696,8 @@ static double
 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 {
   /*
-   * Match the preferences of the peer with the current assignment.
+   * - Match the preferences of the peer with the current assignment
+   * - Validity of the solution
    */
   const double *preferences;
   const double *properties;
@@ -732,20 +706,21 @@ envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
   struct RIL_Network *net;
   int prop_index;
 
-  preferences = solver->callbacks->get_preferences (solver->callbacks->get_preferences_cls, &agent->peer);
-  properties = solver->callbacks->get_properties (solver->callbacks->get_properties_cls,
+  preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
+      &agent->peer);
+  properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
       agent->address_inuse);
-  prop_index = ril_find_property_index(GNUNET_ATS_QUALITY_NET_DELAY);
-  pref_match += preferences[GNUNET_ATS_PREFERENCE_LATENCY] * properties[prop_index];
+  prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
+  pref_match += preferences[GNUNET_ATS_PREFERENCE_LATENCY] * (3 - properties[prop_index]); //invert property as we want to maximize for lower latencies
   bw_norm = GNUNET_MAX(2, (((
-      ((double) agent->bw_in / (double) ril_get_max_bw(agent, GNUNET_YES)) +
-      ((double) agent->bw_out / (double) ril_get_max_bw(agent, GNUNET_NO))
-      ) / 2
-      ) + 1));
+                  ((double) agent->bw_in / (double) ril_get_max_bw(agent, GNUNET_YES)) +
+                  ((double) agent->bw_out / (double) ril_get_max_bw(agent, GNUNET_NO))
+              ) / 2
+          ) + 1));
   pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm;
 
   net = agent->address_inuse->solver_information;
-  if ((net->bw_in_assigned > net->bw_in_available) || net->bw_out_assigned > net->bw_out_available)
+  if ((net->bw_in_assigned > net->bw_in_available) || (net->bw_out_assigned > net->bw_out_available))
   {
     return -1;
   }
@@ -755,6 +730,7 @@ envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 
 /**
  * Doubles the bandwidth for the active address
+ *
  * @param solver solver handle
  * @param agent agent handle
  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
@@ -779,6 +755,7 @@ envi_action_bw_double (struct GAS_RIL_Handle *solver,
 /**
  * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
  * the minimum bandwidth for a peer, in order to not invoke a disconnect.
+ *
  * @param solver solver handle
  * @param agent agent handle
  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
@@ -797,19 +774,22 @@ 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, GNUNET_NO);
+    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, GNUNET_NO);
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
+        GNUNET_NO);
   }
 }
 
 /**
  * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
+ *
  * @param solver solver handle
  * @param agent agent handle
  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
@@ -835,6 +815,7 @@ envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
 /**
  * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
  * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
+ *
  * @param solver solver handle
  * @param agent agent handle
  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
@@ -851,19 +832,22 @@ 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, GNUNET_NO);
+    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, GNUNET_NO);
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
+        GNUNET_NO);
   }
 }
 
 /**
  * Switches to the address given by its index
+ *
  * @param solver solver handle
  * @param agent agent handle
  * @param address_index index of the address as it is saved in the agent's list, starting with zero
@@ -880,7 +864,8 @@ envi_action_address_switch (struct GAS_RIL_Handle *solver,
   {
     if (i == address_index)
     {
-      envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out, GNUNET_NO);
+      envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
+          GNUNET_NO);
       return;
     }
 
@@ -893,13 +878,14 @@ envi_action_address_switch (struct GAS_RIL_Handle *solver,
 
 /**
  * Puts the action into effect by calling the according function
+ *
  * @param solver solver handle
  * @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)
 {
-  unsigned int address_index;
+  int address_index;
 
   switch (action)
   {
@@ -951,6 +937,7 @@ envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int
  * after having done the last action a_old. It observes the new state s_next and the reward
  * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
  * next action is put into effect.
+ *
  * @param agent the agent performing the step
  */
 static void
@@ -1020,6 +1007,7 @@ agent_step (struct RIL_Peer_Agent *agent)
 
 /**
  * Cycles through all agents and lets the active ones do a step. Schedules the next step.
+ *
  * @param solver the solver handle
  * @param tc task context for the scheduler
  */
@@ -1045,6 +1033,7 @@ ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 /**
  * Initialize an agent without addresses and its knowledge base
+ *
  * @param s ril solver
  * @param peer the one in question
  * @return handle to the new agent
@@ -1062,7 +1051,7 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
   agent->active = GNUNET_NO;
   agent->n = RIL_ACTION_TYPE_NUM;
   agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
-  agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
+  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);
@@ -1079,6 +1068,7 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
 
 /**
  * Deallocate agent
+ *
  * @param s solver handle
  * @param agent the agent to retire
  */
@@ -1099,6 +1089,7 @@ agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 
 /**
  * Returns the agent for a peer
+ *
  * @param s solver handle
  * @param peer identity of the peer
  * @param create whether to create an agent if none is allocated yet
@@ -1111,9 +1102,7 @@ ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *
 
   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
   {
-    if (0 == memcmp (peer,
-                    &cur->peer,
-                    sizeof (struct GNUNET_PeerIdentity)))
+    if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
     {
       return cur;
     }
@@ -1151,9 +1140,10 @@ ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
 /**
  * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
  * considered inactive and not used. Addresses in an inactive network are ignored.
+ *
  * @param solver solver handle
  * @param network the network type
- * @return
+ * @return whether or not the network is considered active
  */
 static int
 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
@@ -1171,6 +1161,7 @@ ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Ty
  * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
  * the reward function approximation. It copies the memory, which is not cut, to the new vector,
  * frees the memory of the old vector, and redirects the pointer to the new one.
+ *
  * @param old pointer to the pointer to the first element of the vector
  * @param element_size byte size of the vector elements
  * @param hole_start the first element to cut out
@@ -1223,7 +1214,7 @@ ril_cut_from_vector (void **old,
  */
 
 /**
- * Changes the preferences for a peer in the problem
+ * Change relative preference for quality in solver
  *
  * @param solver the solver handle
  * @param peer the peer to change the preference for
@@ -1244,12 +1235,16 @@ GAS_ril_address_change_preference (void *s,
    */
 }
 
-//TODO doxygen
+/**
+ * Entry point for the plugin
+ *
+ * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
+ */
 void *
 libgnunet_plugin_ats_ril_init (void *cls)
 {
   struct GNUNET_ATS_PluginEnvironment *env = cls;
-  struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);;
+  struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
   struct RIL_Network * cur;
   int c;
   unsigned long long tmp;
@@ -1278,7 +1273,8 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
+  if (GNUNET_OK
+      == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
   {
     solver->parameters.gamma = (double) tmp / 100;
   }
@@ -1286,7 +1282,8 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
+  if (GNUNET_OK
+      == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
   {
     solver->parameters.alpha = (double) tmp / 100;
   }
@@ -1317,17 +1314,8 @@ libgnunet_plugin_ats_ril_init (void *cls)
   env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
 
   solver->plugin_envi = env;
-  solver->stats = (struct GNUNET_STATISTICS_Handle *) env->stats;
-  solver->callbacks = GNUNET_malloc (sizeof (struct RIL_Callbacks));
-  solver->callbacks->bw_changed = env->bandwidth_changed_cb;
-  solver->callbacks->bw_changed_cls = env->bw_changed_cb_cls;
-  solver->callbacks->get_preferences = env->get_preferences;
-  solver->callbacks->get_preferences_cls = env->get_preference_cls;
-  solver->callbacks->get_properties = env->get_property;
-  solver->callbacks->get_properties_cls = env->get_property_cls;
   solver->networks_count = env->network_count;
   solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
-  solver->addresses = env->addresses;
   solver->step_count = 0;
 
   for (c = 0; c < env->network_count; c++)
@@ -1347,7 +1335,11 @@ libgnunet_plugin_ats_ril_init (void *cls)
   return solver;
 }
 
-//TODO doxygen
+/**
+ * Exit point for the plugin
+ *
+ * @param cls the solver handle
+ */
 void *
 libgnunet_plugin_ats_ril_done (void *cls)
 {
@@ -1367,15 +1359,16 @@ libgnunet_plugin_ats_ril_done (void *cls)
   }
 
   GNUNET_SCHEDULER_cancel (s->next_step);
-  GNUNET_free(s->callbacks);
   GNUNET_free(s->network_entries);
   GNUNET_free(s);
 
-  return NULL;
+  return NULL ;
 }
 
 /**
- * Add a single address within a network to the solver
+ * Add a new address for a peer to the solver
+ *
+ * The address is already contained in the addresses hashmap!
  *
  * @param solver the solver Handle
  * @param address the address to add
@@ -1437,7 +1430,7 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
 
   //increase size of old state vector
   agent->m = m_old;
-  GNUNET_array_grow(agent->s_old, agent->m, m_new); //TODO random instead of zero-initialization of state features
+  GNUNET_array_grow(agent->s_old, agent->m, m_new);
 
   agent->m = m_old;
   GNUNET_array_grow(agent->e, agent->m, m_new);
@@ -1450,12 +1443,14 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
   }
 
   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));
+      address->active ? "active" : "inactive", address->plugin, address->addr,
+      GNUNET_i2s (&address->peer));
 }
 
 /**
- * Remove an address from the solver
+ * Delete an address in the solver
+ *
+ * The address is not contained in the address hashmap anymore!
  *
  * @param solver the solver handle
  * @param address the address to remove
@@ -1511,9 +1506,10 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
   for (i = 0; i < agent->n; i++)
   {
     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);
+        ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
+            + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
   }
-  GNUNET_free (agent->W[RIL_ACTION_TYPE_NUM + address_index]);
+  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
@@ -1527,13 +1523,15 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
   }
   //decrease old state vector and eligibility vector
   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);
+      ((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->e, sizeof(double),
-      ((s->networks_count * RIL_FEATURES_NETWORK_COUNT) + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
+      ((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");
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
 
   if (address_was_used)
   {
@@ -1543,16 +1541,18 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
 
     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);
+      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);
+      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");
+      LOG(GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
 
       envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
     }
@@ -1562,7 +1562,7 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
 }
 
 /**
- * Transport properties for this address have changed
+ * Update the properties of an address in the solver
  *
  * @param solver solver handle
  * @param address the address
@@ -1587,7 +1587,7 @@ GAS_ril_address_property_changed (void *solver,
 }
 
 /**
- * Transport session for this address has changed
+ * Update the session of an address in the solver
  *
  * NOTE: values in addresses are already updated
  *
@@ -1609,7 +1609,8 @@ GAS_ril_address_session_changed (void *solver,
 }
 
 /**
- * Usage for this address has changed
+ * Notify the solver that an address is (not) actively used by transport
+ * to communicate with a remote peer
  *
  * NOTE: values in addresses are already updated
  *
@@ -1620,14 +1621,16 @@ GAS_ril_address_session_changed (void *solver,
 void
 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
 {
-  /* Nothing to do here */
+  /* Nothing to do here.
+   * Possible TODO? Future Work: Use usage as state vector
+   */
   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");
 }
 
 /**
- * Network scope for this address has changed
+ * Notify solver that the network an address is located in has changed
  *
  * NOTE: values in addresses are already updated
  *
@@ -1678,7 +1681,7 @@ GAS_ril_address_change_network (void *solver,
 }
 
 /**
- * Get application feedback for a peer
+ * Give feedback about the current assignment
  *
  * @param solver the solver handle
  * @param application the application
@@ -1695,7 +1698,6 @@ GAS_ril_address_preference_feedback (void *solver,
     enum GNUNET_ATS_PreferenceKind kind,
     double score)
 {
-  //TODO! collect feedback
   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",
@@ -1705,31 +1707,37 @@ GAS_ril_address_preference_feedback (void *solver,
 /**
  * Start a bulk operation
  *
+ * Since new calculations of the assignment are not triggered by a change of preferences, as it
+ * happens in the proportional and the mlp solver, there is no need to block this solver.
+ *
  * @param solver the solver
  */
 void
 GAS_ril_bulk_start (void *solver)
 {
-  /*
-   * Since new calculations of the assignment are not triggered by a change of preferences, as it
-   * happens in the proportional and the mlp solver, there is no need to block this solver.
-   */
+  /* nop */
 }
 
 /**
  * Bulk operation done
+ *
+ * Since new calculations of the assignment are not triggered by a change of preferences, as it
+ * happens in the proportional and the mlp solver, there is no need to block this solver.
+ *
+ * @param solver the solver handle
  */
 void
 GAS_ril_bulk_stop (void *solver)
 {
-  /*
-   * Since new calculations of the assignment are not triggered by a change of preferences, as it
-   * happens in the proportional and the mlp solver, there is no need to block this solver.
-   */
+  /* nop */
 }
 
 /**
- * Get the preferred address for a specific peer
+ * Tell solver to notify ATS if the address to use changes for a specific
+ * peer using the bandwidth changed callback
+ *
+ * The solver must only notify about changes for peers with pending address
+ * requests!
  *
  * @param solver the solver handle
  * @param peer the identity of the peer
@@ -1742,12 +1750,20 @@ GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *p
    */
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
+  struct RIL_Network *net;
 
   agent = ril_get_agent (s, peer, GNUNET_YES);
 
   agent->active = GNUNET_YES;
 
-  envi_set_active_suggestion(s, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
+  if (agent->address_inuse)
+  {
+    net = agent->address_inuse->solver_information;
+    net->bw_in_assigned += agent->bw_in;
+    net->bw_out_assigned += agent->bw_out;
+  }
+  envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
+      GNUNET_YES);
 
   if (agent->address_inuse)
   {
@@ -1758,14 +1774,18 @@ GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *p
   else
   {
     LOG(GNUNET_ERROR_TYPE_DEBUG,
-            "API_get_preferred_address() Activated agent for peer '%s', but no address available\n", GNUNET_i2s (peer));
+        "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
+        GNUNET_i2s (peer));
   }
 
   return agent->address_inuse;
 }
 
 /**
- * Stop notifying about address and bandwidth changes for this peer
+ * Tell solver stop notifying ATS about changes for this peers
+ *
+ * The solver must only notify about changes for peers with pending address
+ * requests!
  *
  * @param solver the solver handle
  * @param peer the peer
@@ -1775,6 +1795,7 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
 {
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
+  struct RIL_Network *net;
 
   agent = ril_get_agent (s, peer, GNUNET_NO);
 
@@ -1790,7 +1811,14 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
   }
 
   agent->active = GNUNET_NO;
-  envi_set_active_suggestion(s, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
+  if (agent->address_inuse)
+  {
+    net = agent->address_inuse->solver_information;
+    net->bw_in_assigned -= agent->bw_in;
+    net->bw_out_assigned -= agent->bw_out;
+  }
+  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",