added simulation
[oweals/gnunet.git] / src / ats / libgnunet_plugin_ats_ril.c
index b829f335782ee3075922eec9fc451a54172bc0a0..603e93a4d7863e7ad744ad933839eae94e8b9830 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 MIN_BW ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)
 
-#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
-#define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
-#define RIL_DEFAULT_TRACE_DECAY 0.6
-#define RIL_EXPLORE_RATIO 0.1
+#define RIL_ACTION_INVALID -1
+#define RIL_FEATURES_ADDRESS_COUNT (0)// + GNUNET_ATS_QualityPropertiesCount)
+#define RIL_FEATURES_NETWORK_COUNT 2
+#define RIL_INTERVAL_EXPONENT 10
+
+#define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
+#define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
+#define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
+#define RIL_DEFAULT_DISCOUNT_BETA 1
+#define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
+#define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.1
+#define RIL_DEFAULT_TRACE_DECAY 0.5
+#define RIL_DEFAULT_EXPLORE_RATIO 0.1
+#define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5
+
+#define RIL_INC_DEC_STEP_SIZE 1
 
 /**
  * ATS reinforcement learning solver
  */
 enum RIL_Action_Type
 {
-  RIL_ACTION_NOTHING = 0,
-  RIL_ACTION_BW_IN_DBL = 1,
-  RIL_ACTION_BW_IN_HLV = 2,
-  RIL_ACTION_BW_IN_INC = 3,
-  RIL_ACTION_BW_IN_DEC = 4,
-  RIL_ACTION_BW_OUT_DBL = 5,
-  RIL_ACTION_BW_OUT_HLV = 6,
-  RIL_ACTION_BW_OUT_INC = 7,
-  RIL_ACTION_BW_OUT_DEC = 8,
-  RIL_ACTION_TYPE_NUM = 9
+  RIL_ACTION_NOTHING = -1,
+  RIL_ACTION_BW_IN_DBL = -2, //TODO! put actions back
+  RIL_ACTION_BW_IN_HLV = -3,
+  RIL_ACTION_BW_IN_INC = 0,
+  RIL_ACTION_BW_IN_DEC = 1,
+  RIL_ACTION_BW_OUT_DBL = -4,
+  RIL_ACTION_BW_OUT_HLV = -5,
+  RIL_ACTION_BW_OUT_INC = -6,
+  RIL_ACTION_BW_OUT_DEC = -7,
+  RIL_ACTION_TYPE_NUM = 1
 };
 
 enum RIL_Algorithm
@@ -90,19 +98,44 @@ struct RIL_Learning_Parameters
   enum RIL_Algorithm algorithm;
 
   /**
-   * Learning discount factor in the TD-update
+   * Gradient-descent step-size
    */
-  float gamma;
+  double alpha;
 
   /**
-   * Gradient-descent step-size
+   * Learning discount variable in the TD-update for semi-MDPs
    */
-  float alpha;
+  double beta;
+
+  /**
+   * Learning discount factor in the TD-update for MDPs
+   */
+  double gamma;
 
   /**
    * Trace-decay factor for eligibility traces
    */
-  float lambda;
+  double lambda;
+
+  /**
+   * Ratio, with what probability an agent should explore in the e-greed policy
+   */
+  double explore_ratio;
+
+  /**
+   * How big the share of the global part of the reward signal is
+   */
+  double reward_global_share;
+
+  /**
+   * Minimal interval time between steps in milliseconds
+   */
+  struct GNUNET_TIME_Relative step_time_min;
+
+  /**
+   * Maximum interval time between steps in milliseconds
+   */
+  struct GNUNET_TIME_Relative step_time_max;
 };
 
 /**
@@ -151,7 +184,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 is_active;
 
   /**
    * Number of performed time-steps
@@ -212,6 +245,16 @@ struct RIL_Peer_Agent
    * Outbound bandwidth assigned by the agent
    */
   unsigned long long bw_out;
+
+  /**
+   * Flag whether a suggestion has to be issued
+   */
+  int suggestion_issue;
+
+  /**
+   * The address which has to be issued
+   */
+  struct ATS_Address * suggestion_address;
 };
 
 struct RIL_Network
@@ -227,7 +270,7 @@ struct RIL_Network
   unsigned long long bw_in_available;
 
   /**
-   * Total assigned outbound bandwidth
+   * Bandwidth inbound assigned in network after last step
    */
   unsigned long long bw_in_assigned;
 
@@ -237,83 +280,65 @@ struct RIL_Network
   unsigned long long bw_out_available;
 
   /**
-   * Total assigned outbound bandwidth
+   * * Bandwidth outbound assigned in network after last step
    */
   unsigned long long bw_out_assigned;
 };
 
-struct RIL_Callbacks
+/**
+ * A handle for the reinforcement learning solver
+ */
+struct GAS_RIL_Handle
 {
   /**
-   * Bandwidth changed callback
+   * The solver-plugin environment of the solver-plugin API
    */
-  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;
+  struct GNUNET_ATS_PluginEnvironment *plugin_envi;
 
   /**
-   * ATS function to get properties of an address
+   * Statistics handle
    */
-  GAS_get_properties get_properties;
+  struct GNUNET_STATISTICS_Handle *stats;
 
   /**
-   * Closure for ATS function to get properties
+   * Number of performed steps
    */
-  void *get_properties_cls;
-};
+  unsigned long long step_count;
 
-/**
- * A handle for the reinforcement learning solver
- */
-struct GAS_RIL_Handle
-{
   /**
-   *
+   * Timestamp for the last time-step
    */
-  struct GNUNET_ATS_PluginEnvironment *plugin_envi;
+  struct GNUNET_TIME_Absolute step_time_last;
 
   /**
-   * Statistics handle
+   * Task identifier of the next time-step to be executed
    */
-  struct GNUNET_STATISTICS_Handle *stats;
+  GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
 
   /**
-   * Hashmap containing all valid addresses
+   * Variable discount factor, dependent on time between steps
    */
-  const struct GNUNET_CONTAINER_MultiPeerMap *addresses;
+  double global_discount_variable;
 
   /**
-   * Callbacks for the solver
+   * Integrated variable discount factor, dependent on time between steps
    */
-  struct RIL_Callbacks *callbacks;
+  double global_discount_integrated;
 
   /**
-   * Number of performed time-steps
+   * State vector for networks for the current step
    */
-  unsigned long long step_count;
+  double *global_state_networks;
 
   /**
-   * Interval time between steps in milliseconds //TODO? Future Work: Heterogeneous stepping among agents
+   * Lock for bulk operations
    */
-  struct GNUNET_TIME_Relative step_time;
+  int bulk_lock;
 
   /**
-   * Task identifier of the next time-step to be executed
+   * Number of changes during a lock
    */
-  GNUNET_SCHEDULER_TaskIdentifier next_step;
+  int bulk_changes;
 
   /**
    * Learning parameters
@@ -335,6 +360,16 @@ struct GAS_RIL_Handle
    */
   struct RIL_Peer_Agent * agents_head;
   struct RIL_Peer_Agent * agents_tail;
+
+  /**
+   * Shutdown
+   */
+  int done;
+
+  /**
+   * Simulate steps, i.e. schedule steps immediately
+   */
+  unsigned long long simulate;
 };
 
 /*
@@ -342,8 +377,12 @@ struct GAS_RIL_Handle
  *  ---------------------------
  */
 
+static int
+ril_count_agents(struct GAS_RIL_Handle * solver);
+
 /**
  * Estimate the current action-value for state s and action a
+ *
  * @param agent agent performing the estimation
  * @param state s
  * @param action a
@@ -360,12 +399,20 @@ agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
     result += state[i] * agent->W[action][i];
   }
 
+  GNUNET_assert(!isnan(result));
+
+  if (isinf(result))
+  {
+    return isinf(result) * UINT32_MAX; //TODO! fix
+  }
+
   return result;
 }
 
 /**
  * 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
  */
@@ -373,25 +420,23 @@ static int
 agent_decide_exploration (struct RIL_Peer_Agent *agent)
 {
   //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
-  /*
-   * An e-greedy replacement could be based on the accuracy of the prediction of the Q-value
-   */
   double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
       UINT32_MAX) / (double) UINT32_MAX;
 
-  if  (r < RIL_EXPLORE_RATIO)
+  if (r < agent->envi->parameters.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 +458,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 +482,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 +512,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 +526,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
@@ -490,11 +539,27 @@ agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_nex
   double delta;
   double *theta = agent->W[agent->a_old];
 
-  delta = reward + agent_estimate_q (agent, s_next, a_prime)
-      - agent_estimate_q (agent, agent->s_old, agent->a_old);
+  delta = agent->envi->global_discount_integrated * reward; //reward
+  delta += agent->envi->global_discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
+  delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
+
+  LOG(GNUNET_ERROR_TYPE_INFO, "update()   Step# %llu  Q(s,a): %f  a: %f  r: %f  y: %f  Q(s+1,a+1) = %f  delta: %f\n",
+      agent->step_count,
+      agent_estimate_q (agent, agent->s_old, agent->a_old),
+      agent->envi->parameters.alpha,
+      reward,
+      agent->envi->global_discount_variable,
+      agent_estimate_q (agent, s_next, a_prime),
+      delta);
+
   for (i = 0; i < agent->m; i++)
   {
-    theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
+//    LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f   delta = %f   e[%d] = %f\n",
+//        agent->envi->parameters.alpha,
+//        delta,
+//        i,
+//        agent->e[i]);
+    theta[i] += agent->envi->parameters.alpha * delta * agent->s_old[i];// * agent->e[i];
   }
 }
 
@@ -502,31 +567,30 @@ agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_nex
  * Changes the eligibility trace vector e in various manners:
  * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
  * 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_SET - multiplies e with discount factor 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)
+agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod, double *f)
 {
   int i;
   double *e = agent->e;
-  double gamma = agent->envi->parameters.gamma;
-  double lambda = agent->envi->parameters.lambda;
 
   for (i = 0; i < agent->m; i++)
   {
     switch (mod)
     {
     case RIL_E_ACCUMULATE:
-      e[i] += 1;
+      e[i] += f[i];
       break;
     case RIL_E_REPLACE:
-      e[i] = 1;
+      e[i] = f[i];
       break;
     case RIL_E_SET:
-      e[i] = gamma * lambda;
+      e[i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
       break;
     case RIL_E_ZERO:
       e[i] = 0;
@@ -535,9 +599,19 @@ agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification
   }
 }
 
+static void
+ril_inform (struct GAS_RIL_Handle *solver,
+    enum GAS_Solver_Operation op,
+    enum GAS_Solver_Status stat)
+{
+  if (NULL != solver->plugin_envi->info_cb)
+    solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
+}
+
 /**
  * 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 +629,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() for peer '%s'\n", GNUNET_i2s (&agent->peer));
 
   //address change
   if (agent->address_inuse != new_address)
@@ -568,8 +642,8 @@ 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");
-      new_address->active = agent->active;
+      LOG(GNUNET_ERROR_TYPE_DEBUG, "    set address active: %s\n", agent->is_active ? "yes" : "no");
+      new_address->active = agent->is_active;
       new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
       new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
     }
@@ -579,16 +653,17 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
   if (new_address)
   {
     //activity change
-    if (new_address->active != agent->active)
+    if (new_address->active != agent->is_active)
     {
-      new_address->active = agent->active;
+      new_address->active = agent->is_active;
+      notify |= GNUNET_YES;
     }
 
     //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);
+      new_address->assigned_bw_in.value__ = htonl (new_bw_in);
       notify |= GNUNET_YES;
     }
     if (agent->bw_out != new_bw_out)
@@ -599,162 +674,333 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
     }
   }
 
-  if (notify && agent->active && (GNUNET_NO == silent))
+  if (notify && agent->is_active && (GNUNET_NO == silent))
   {
     if (new_address)
     {
-      solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, new_address);
+      LOG(GNUNET_ERROR_TYPE_DEBUG, "    envi_set_active_suggestion() notify\n");
+      agent->suggestion_issue = GNUNET_YES;
+      agent->suggestion_address = 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__));
+      //disconnect case, no new address
+      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->suggestion_issue = GNUNET_YES;
+      agent->suggestion_address = agent->address_inuse;
     }
   }
   agent->address_inuse = new_address;
 }
 
+static unsigned long long
+ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
+{
+  struct RIL_Peer_Agent *cur;
+  struct RIL_Network *net;
+  unsigned long long sum = 0;
+
+  for (cur = solver->agents_head; NULL != cur; cur = cur->next)
+  {
+    if (cur->is_active && cur->address_inuse)
+    {
+      net = cur->address_inuse->solver_information;
+      if (net->type == type)
+      {
+        if (direction_in)
+          sum += cur->bw_in;
+        else
+          sum += cur->bw_out;
+      }
+    }
+  }
+
+  return sum;
+}
+
+//static void
+//envi_state_networks (struct GAS_RIL_Handle *solver)
+//{
+//  int i;
+//  struct RIL_Network net;
+//  int overutilized_in;
+//  int overutilized_out;
+//
+//  for (i = 0; i < solver->networks_count; i++)
+//  {
+//    net = solver->network_entries[i];
+//
+//    overutilized_in = net.bw_in_assigned > net.bw_in_available;
+//    overutilized_out = net.bw_out_assigned > net.bw_out_available;
+//
+//    solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 0] = ((double) net.bw_in_assigned / (double) net.bw_in_available)*10;
+//    solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) overutilized_in;
+//    solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 2] = ((double) net.bw_out_assigned / (double) net.bw_out_available)*10;
+//    solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) overutilized_out;
+//  }
+//}
+
 /**
  * Allocates a state vector and fills it with the features present
  * @param solver the solver handle
+ * @param agent the agent handle
  * @return pointer to the state vector
  */
 static double *
 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 {
   int i;
-  int k;
-  struct RIL_Network *net;
+//  int k;
   double *state = GNUNET_malloc (sizeof (double) * agent->m);
   struct RIL_Address_Wrapped *cur_address;
-  const double *properties;
+//  const double *preferences;
+//  const double *properties;
+  struct RIL_Network *net;
 
-  for (i = 0; i < solver->networks_count; i++)
+  //copy global networks state
+  for (i = 0; i < solver->networks_count * RIL_FEATURES_NETWORK_COUNT; i++)
   {
-    net = &solver->network_entries[i];
-    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;
+//    state[i] = solver->global_state_networks[i];
   }
+  net = agent->address_inuse->solver_information;
 
-  i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
-
-  for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
+  state[0] = (double) net->bw_in_assigned / 1024; //(double) net->bw_in_available;
+  if (net->bw_in_assigned > net->bw_in_available)
   {
-    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];
-    }
+    state[1] = 1;// net->bw_in_available;
   }
+  else
+  {
+    state[1] = 0;
+  }
+  LOG(GNUNET_ERROR_TYPE_INFO, "get_state()  state[0] = %f\n", state[0]);
+  LOG(GNUNET_ERROR_TYPE_INFO, "get_state()  state[1] = %f\n", state[1]);
 
-  return state;
-}
+  LOG(GNUNET_ERROR_TYPE_INFO, "get_state()  W / %08.3f %08.3f \\ \n", agent->W[0][0], agent->W[1][0]);
+  LOG(GNUNET_ERROR_TYPE_INFO, "get_state()  W \\ %08.3f %08.3f / \n", agent->W[0][1], agent->W[1][1]);
 
-/**
- * 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)
-{
-  /*
-   * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
-   * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
-   */
-  int max = 0;
-  struct RIL_Address_Wrapped *cur;
-  struct RIL_Network *net;
 
-  for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
+  //get peer features
+//  preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
+//        &agent->peer);
+//  for (k = 0; k < GNUNET_ATS_PreferenceCount; k++)
+//  {
+//    state[i++] = preferences[k];
+//  }
+
+  //get address specific features
+  for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
   {
-    net = cur->address_naked->solver_information;
-    if (direction_in)
-    {
-      if (net->bw_in_available > max)
-      {
-        max = net->bw_in_available;
-      }
-    }
-    else
-    {
-      if (net->bw_out_available > max)
-      {
-        max = net->bw_out_available;
-      }
-    }
+//    //when changing the number of address specific state features, change RIL_FEATURES_ADDRESS_COUNT macro
+//    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->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];
+//    }
   }
-  return max;
-}
 
-/**
- * Get the index of the quality-property in question
- * @param type the quality property type
- * @return the index
- */
-static int
-ril_find_property_index (uint32_t type)
-{
-  int existing_types[] = GNUNET_ATS_QualityProperties;
-  int c;
-  for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
-    if (existing_types[c] == type)
-      return c;
-  return GNUNET_SYSERR;
+  return state;
 }
 
+///**
+// * 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 agent the agent handle
+// * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
+// */
+//static unsigned long long
+//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'
+//   * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
+//   */
+//  unsigned long long max = 0;
+//  struct RIL_Address_Wrapped *cur;
+//  struct RIL_Network *net;
+//
+//  for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
+//  {
+//    net = cur->address_naked->solver_information;
+//    if (direction_in)
+//    {
+//      if (net->bw_in_available > max)
+//      {
+//        max = net->bw_in_available;
+//      }
+//    }
+//    else
+//    {
+//      if (net->bw_out_available > max)
+//      {
+//        max = net->bw_out_available;
+//      }
+//    }
+//  }
+//  return max;
+//}
+
+///**
+// * Get the index of the quality-property in question
+// *
+// * @param type the quality property type
+// * @return the index
+// */
+//static int
+//ril_find_property_index (uint32_t type)
+//{
+//  int existing_types[] = GNUNET_ATS_QualityProperties;
+//  int c;
+//  for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
+//    if (existing_types[c] == type)
+//      return c;
+//  return GNUNET_SYSERR;
+//}
+
+//static int
+//ril_get_atsi (struct ATS_Address *address, uint32_t type)
+//{
+//  int c1;
+//  GNUNET_assert(NULL != address);
+//
+//  if ((NULL == address->atsi) || (0 == address->atsi_count))
+//    return 0;
+//
+//  for (c1 = 0; c1 < address->atsi_count; c1++)
+//  {
+//    if (ntohl (address->atsi[c1].type) == type)
+//      return ntohl (address->atsi[c1].value);
+//  }
+//  return 0;
+//}
+
+//static double
+//envi_reward_global (struct GAS_RIL_Handle *solver)
+//{
+//  int i;
+//  struct RIL_Network net;
+//  unsigned int sum_in_available = 0;
+//  unsigned int sum_out_available = 0;
+//  unsigned int sum_in_assigned = 0;
+//  unsigned int sum_out_assigned = 0;
+//  double ratio_in;
+//  double ratio_out;
+//
+//  for (i = 0; i < solver->networks_count; i++)
+//  {
+//    net = solver->network_entries[i];
+//    sum_in_available += net.bw_in_available;
+//    sum_in_assigned += net.bw_in_assigned;
+//    sum_out_available += net.bw_out_available;
+//    sum_out_assigned += net.bw_out_assigned;
+//  }
+//
+//  ratio_in = ((double) sum_in_assigned) / ((double) sum_in_available);
+//  ratio_out = ((double) sum_out_assigned) / ((double) sum_out_available);
+//
+//  // global reward in [1,2]
+//  return ratio_in +1;
+//  return ((ratio_in + ratio_out) / 2) + 1;
+//}
+
+//static double
+//envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
+//{
+//  const double *preferences;
+//  const double *properties;
+//  int prop_index;
+//  double pref_match = 0;
+//  double bw_norm;
+//  double dl_norm;
+//
+//  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);
+//
+//  // delay in [0,1]
+//  prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
+//  dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
+//
+//  // utilization in [0,1]
+//  bw_norm = (((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
+//      / (double) ril_get_max_bw (agent, GNUNET_YES))
+//      + ((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
+//          / (double) ril_get_max_bw (agent, GNUNET_NO))) / 2;
+//
+//  // preference matching in [0,4]
+//  pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
+//  pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
+//
+//  // local reward in [1,2]
+//  return (pref_match / 4) +1;
+//}
+
 /**
- * Gets the reward of the last performed step
- * @param solver solver handle
+ * Gets the reward for the last performed step, which is calculated in equal
+ * parts from the local (the peer specific) and the global (for all peers
+ * identical) reward.
+ *
+ * @param solver the solver handle
+ * @param agent the agent handle
  * @return the reward
  */
 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.
-   */
-  const double *preferences;
-  const double *properties;
-  double pref_match = 0;
-  double bw_norm;
   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,
-      agent->address_inuse);
-  prop_index = ril_find_property_index(GNUNET_ATS_QUALITY_NET_DELAY);
-  pref_match += preferences[GNUNET_ATS_PREFERENCE_LATENCY] * properties[prop_index];
-  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));
-  pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm;
-
+//  double reward = 0;
+  long long overutilized_in = 0;
+//  long long overutilized_out;
+  long long assigned_in = 0;
+//  long long assigned_out = 0;
+//  long long unused;
+
+  //punish overutilization
   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)
   {
-    return -1;
+    overutilized_in = (net->bw_in_assigned - net->bw_in_available);
+    assigned_in = net->bw_in_available;
   }
-
-  return pref_match;
+  else
+  {
+    assigned_in = net->bw_in_assigned;
+  }
+//  if (net->bw_out_assigned > net->bw_out_available)
+//  {
+//    overutilized_out = (net->bw_out_assigned - net->bw_out_available);
+//    assigned_out = net->bw_out_available;
+//  }
+//  else
+//  {
+//    assigned_out = net->bw_out_assigned;
+//  }
+
+//  unused = net->bw_in_available - net->bw_in_assigned;
+//  unused = unused < 0 ? unused : -unused;
+
+  return (double) (assigned_in - overutilized_in) / 1024;
+
+//  reward += envi_reward_global (solver) * (solver->parameters.reward_global_share);
+//  reward += envi_reward_local (solver, agent) * (1 - solver->parameters.reward_global_share);
+//
+//  return (reward - 1.) * 100;
 }
 
 /**
  * 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
@@ -764,21 +1010,30 @@ envi_action_bw_double (struct GAS_RIL_Handle *solver,
     struct RIL_Peer_Agent *agent,
     int direction_in)
 {
+  unsigned long long new_bw;
+
   if (direction_in)
   {
-    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2,
+    new_bw = agent->bw_in * 2;
+    if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
+      new_bw = GNUNET_ATS_MaxBandwidth;
+    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 < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth)
+      new_bw = GNUNET_ATS_MaxBandwidth;
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
-        agent->bw_out * 2, GNUNET_NO);
+        new_bw, GNUNET_NO);
   }
 }
 
 /**
  * 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
@@ -789,27 +1044,29 @@ envi_action_bw_halven (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;
 
   if (direction_in)
   {
     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);
+    if (new_bw < MIN_BW || new_bw > agent->bw_in)
+      new_bw = MIN_BW;
+    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);
+    if (new_bw < MIN_BW || new_bw > agent->bw_out)
+      new_bw = MIN_BW;
+    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
@@ -818,23 +1075,30 @@ envi_action_bw_halven (struct GAS_RIL_Handle *solver,
 static void
 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__);
+  unsigned long long new_bw;
 
   if (direction_in)
   {
-    envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
+    new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * MIN_BW);
+    if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
+      new_bw = GNUNET_ATS_MaxBandwidth;
+    envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
         agent->bw_out, GNUNET_NO);
   }
   else
   {
+    new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * MIN_BW);
+    if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth)
+      new_bw = GNUNET_ATS_MaxBandwidth;
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
-        agent->bw_out + (5 * min_bw), GNUNET_NO);
+        new_bw, GNUNET_NO);
   }
 }
 
 /**
  * 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
@@ -843,27 +1107,29 @@ envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
 static void
 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;
 
   if (direction_in)
   {
-    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);
+    new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * MIN_BW);
+    if (new_bw < MIN_BW || new_bw > agent->bw_in)
+      new_bw = MIN_BW;
+    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);
+    new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * MIN_BW);
+    if (new_bw < MIN_BW || new_bw > agent->bw_out)
+      new_bw = MIN_BW;
+    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 +1146,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 +1160,15 @@ 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
+ *
+ * @param solver the solver handle
+ * @param agent the action handle
+ * @param action the 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,26 +1220,29 @@ 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
 agent_step (struct RIL_Peer_Agent *agent)
 {
   int a_next = RIL_ACTION_INVALID;
+  int explore;
   double *s_next;
   double reward;
 
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "    agent_step() Peer '%s', algorithm %s\n",
+      GNUNET_i2s (&agent->peer),
+      agent->envi->parameters.algorithm ? "Q" : "SARSA");
+
   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",
-      agent->envi->parameters.algorithm ? "Q" : "SARSA");
+  explore = agent_decide_exploration (agent);
 
   switch (agent->envi->parameters.algorithm)
   {
   case RIL_ALGO_SARSA:
-    agent_modify_eligibility (agent, RIL_E_SET);
-    if (agent_decide_exploration (agent))
+    if (explore)
     {
       a_next = agent_get_action_explore (agent, s_next);
     }
@@ -982,6 +1254,7 @@ agent_step (struct RIL_Peer_Agent *agent)
     {
       //updates weights with selected action (on-policy), if not first step
       agent_update_weights (agent, reward, s_next, a_next);
+      agent_modify_eligibility (agent, RIL_E_SET, s_next);
     }
     break;
 
@@ -992,22 +1265,29 @@ agent_step (struct RIL_Peer_Agent *agent)
       //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))
+    if (explore)
     {
       a_next = agent_get_action_explore (agent, s_next);
-      agent_modify_eligibility (agent, RIL_E_ZERO);
+      agent_modify_eligibility (agent, RIL_E_ZERO, NULL);
     }
     else
     {
       a_next = agent_get_action_best (agent, s_next);
-      agent_modify_eligibility (agent, RIL_E_SET);
+      agent_modify_eligibility (agent, RIL_E_SET, s_next);
     }
     break;
   }
 
   GNUNET_assert(RIL_ACTION_INVALID != a_next);
 
-  agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
+  agent_modify_eligibility (agent, RIL_E_ACCUMULATE, s_next);
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step()  Step# %llu  R: %f  IN %llu  OUT %llu  A: %d\n",
+        agent->step_count,
+        reward,
+        agent->bw_in/1024,
+        agent->bw_out/1024,
+        a_next);
 
   envi_do_action (agent->envi, agent, a_next);
 
@@ -1018,33 +1298,318 @@ agent_step (struct RIL_Peer_Agent *agent)
   agent->step_count += 1;
 }
 
+static void
+ril_step (struct GAS_RIL_Handle *solver);
+
 /**
- * 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
+ * Task for the scheduler, which performs one step and lets the solver know that
+ * no further step is scheduled.
+ *
+ * @param cls the solver handle
+ * @param tc the task context for the scheduler
  */
 static void
-ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GAS_RIL_Handle *solver = cls;
+
+  solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
+  ril_step (solver);
+}
+
+static double
+ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
+{
+  int i;
+  struct RIL_Network net;
+  unsigned long long sum_assigned = 0;
+  unsigned long long sum_available = 0;
+  double ratio;
+
+  for (i = 0; i < solver->networks_count; i++)
+  {
+    net = solver->network_entries[i];
+    if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
+    {
+      sum_assigned += net.bw_in_assigned;
+      sum_assigned += net.bw_out_assigned;
+      sum_available += net.bw_in_available;
+      sum_available += net.bw_out_available;
+    }
+  }
+  if (sum_available > 0)
+  {
+    ratio = ((double) sum_assigned) / ((double) sum_available);
+  }
+  else
+  {
+    ratio = 0;
+  }
+
+  return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
+}
+
+/**
+ * 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 (s->network_entries[i].type == type)
+    {
+      return &s->network_entries[i];
+    }
+  }
+  return NULL ;
+}
+
+static int
+ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
+{
+  struct RIL_Network *net;
+  struct RIL_Peer_Agent *agent;
+  unsigned long long address_count = 0;
+
+  for (agent = solver->agents_head; NULL != agent; agent = agent->next)
+  {
+    if (agent->address_inuse && agent->is_active)
+    {
+      net = agent->address_inuse->solver_information;
+      if (net->type == network)
+      {
+        address_count++;
+      }
+    }
+  }
+
+  net = ril_get_network (solver, network);
+  return (net->bw_in_available > MIN_BW * address_count) && (net->bw_out_available > MIN_BW * address_count);
+}
+
+static void
+ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
+{
+  struct RIL_Address_Wrapped *addr_wrap;
+  struct RIL_Network *net;
+
+  for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
+  {
+    net = addr_wrap->address_naked->solver_information;
+    if (ril_network_is_not_full(solver, net->type))
+    {
+      if (NULL == agent->address_inuse)
+        envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, MIN_BW, MIN_BW, silent);
+      return;
+    }
+  }
+  agent->address_inuse = NULL;
+}
+
+static void
+ril_calculate_discount (struct GAS_RIL_Handle *solver)
+{
+  struct GNUNET_TIME_Absolute time_now;
+  struct GNUNET_TIME_Relative time_delta;
+  double tau;
+
+  // MDP case - remove when debugged
+  if (solver->simulate)
+  {
+    solver->global_discount_variable = solver->parameters.gamma;
+    solver->global_discount_integrated = 1;
+    return;
+  }
+
+  // semi-MDP case
+
+  //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
+  time_now = GNUNET_TIME_absolute_get ();
+  time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
+  solver->step_time_last = time_now;
+  tau = (double) time_delta.rel_value_us
+      / (double) solver->parameters.step_time_min.rel_value_us;
+
+  //calculate reward discounts (once per step for all agents)
+  solver->global_discount_variable = pow (M_E, ((-1.) * ((double) solver->parameters.beta) * tau));
+  solver->global_discount_integrated = (1. - solver->global_discount_variable)
+      / (double) solver->parameters.beta;
+}
+
+static void
+ril_calculate_assigned_bwnet (struct GAS_RIL_Handle *solver)
+{
+  int c;
+  struct RIL_Network *net;
+
+  for (c = 0; c < solver->networks_count; c++)
+  {
+    net = &solver->network_entries[c];
+    net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
+    net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
+  }
+}
+
+/**
+ * Schedules the next global step in an adaptive way. The more resources are
+ * left, the earlier the next step is scheduled. This serves the reactivity of
+ * the solver to changed inputs.
+ *
+ * @param solver the solver handle
+ */
+static void
+ril_step_schedule_next (struct GAS_RIL_Handle *solver)
+{
+  double used_ratio;
+  double factor;
+  double y;
+  double offset;
+  struct GNUNET_TIME_Relative time_next;
+
+  used_ratio = ril_get_used_resource_ratio (solver);
+
+  GNUNET_assert(
+      solver->parameters.step_time_min.rel_value_us
+          <= solver->parameters.step_time_max.rel_value_us);
+
+  factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
+      solver->parameters.step_time_min).rel_value_us;
+  offset = (double) solver->parameters.step_time_min.rel_value_us;
+  y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
+
+  GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
+  GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
+
+  time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
+
+  if (solver->simulate)
+  {
+    time_next = GNUNET_TIME_UNIT_ZERO;
+  }
+
+  if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
+  {
+    solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
+          solver);
+  }
+}
+
+/**
+ * Triggers one step per agent
+ * @param solver
+ */
+static void
+ril_step (struct GAS_RIL_Handle *solver)
+{
   struct RIL_Peer_Agent *cur;
 
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
+  if (GNUNET_YES == solver->bulk_lock)
+  {
+    solver->bulk_changes++;
+    return;
+  }
+
+  ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "    RIL step number %d\n", solver->step_count);
 
+  if (0 == solver->step_count)
+  {
+    solver->step_time_last = GNUNET_TIME_absolute_get ();
+  }
+
+  ril_calculate_discount (solver);
+  ril_calculate_assigned_bwnet (solver);
+
+  //calculate network state vector
+//  envi_state_networks(solver);
+
+  //trigger one step per active, unblocked agent
   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
   {
-    if (cur->active && cur->address_inuse)
+    if (cur->is_active)
     {
-      agent_step (cur);
+      if (NULL == cur->address_inuse)
+      {
+        ril_try_unblock_agent(solver, cur, GNUNET_NO);
+      }
+      if (cur->address_inuse)
+      {
+        agent_step (cur);
+      }
     }
   }
 
+  ril_calculate_assigned_bwnet (solver);
+
   solver->step_count += 1;
-  solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, &ril_periodic_step, solver);
+  ril_step_schedule_next (solver);
+
+  ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
+
+  ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
+  for (cur = solver->agents_head; NULL != cur; cur = cur->next)
+  {
+    if (cur->suggestion_issue) {
+      solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
+      cur->suggestion_issue = GNUNET_NO;
+    }
+  }
+  ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
+}
+
+static int
+ril_count_agents (struct GAS_RIL_Handle *solver)
+{
+  int c = 0;
+  struct RIL_Peer_Agent *cur_agent;
+
+  for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
+  {
+    c++;
+  }
+  return c;
+}
+
+static void
+agent_w_start (struct RIL_Peer_Agent *agent)
+{
+  int count;
+  struct RIL_Peer_Agent *other;
+  int i;
+  int k;
+
+  count = ril_count_agents(agent->envi);
+
+  for (i = 0; i < agent->n; i++)
+  {
+    for (k = 0; k < agent->m; k++)
+    {
+      if (0 == count) {
+        agent->W[i][k] = 1;//.1 - ((double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX/5)/(double)UINT32_MAX);
+      }
+      else {
+        for (other = agent->envi->agents_head; NULL != other; other = other->next)
+        {
+          agent->W[i][k] += (other->W[i][k] / (double) count);
+        }
+      }
+
+      GNUNET_assert(!isinf(agent->W[i][k]));
+    }
+  }
 }
 
 /**
  * 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
@@ -1059,27 +1624,30 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
   agent->envi = solver;
   agent->peer = *peer;
   agent->step_count = 0;
-  agent->active = GNUNET_NO;
+  agent->is_active = GNUNET_NO;
+  agent->bw_in = MIN_BW;
+  agent->bw_out = MIN_BW;
+  agent->suggestion_issue = 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->m = (RIL_FEATURES_NETWORK_COUNT);// + GNUNET_ATS_PreferenceCount;
+  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_w_start(agent);
   agent->a_old = RIL_ACTION_INVALID;
-  agent->s_old = envi_get_state (solver, agent);
+  agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
   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);
+  agent_modify_eligibility (agent, RIL_E_ZERO, NULL);
 
   return agent;
 }
 
 /**
  * Deallocate agent
- * @param s solver handle
+ *
+ * @param solver the solver handle
  * @param agent the agent to retire
  */
 static void
@@ -1099,10 +1667,11 @@ 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
- * @return agent
+ *
+ * @param solver the solver handle
+ * @param peer the identity of the peer
+ * @param create whether or not to create an agent, if none is allocated yet
+ * @return the agent
  */
 static struct RIL_Peer_Agent *
 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
@@ -1111,9 +1680,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;
     }
@@ -1121,29 +1688,9 @@ ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *
 
   if (create)
   {
-    return agent_init (solver, peer);
-  }
-  return NULL ;
-}
-
-/**
- * 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 (s->network_entries[i].type == type)
-    {
-      return &s->network_entries[i];
-    }
+    cur = agent_init (solver, peer);
+    GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
+    return cur;
   }
   return NULL ;
 }
@@ -1151,26 +1698,25 @@ 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)
 {
   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;
+  return net->bw_out_available >= MIN_BW;
 }
 
 /**
  * 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 +1769,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
@@ -1231,7 +1777,7 @@ ril_cut_from_vector (void **old,
  * @param pref_rel the normalized preference value for this kind over all clients
  */
 void
-GAS_ril_address_change_preference (void *s,
+GAS_ril_address_change_preference (void *solver,
     const struct GNUNET_PeerIdentity *peer,
     enum GNUNET_ATS_PreferenceKind kind,
     double pref_rel)
@@ -1239,20 +1785,22 @@ GAS_ril_address_change_preference (void *s,
   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);
-  /*
-   * Nothing to do here. Preferences are considered during reward calculation.
-   */
+
+  ril_step (solver);
 }
 
-//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;
   char *string;
 
   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
@@ -1261,47 +1809,89 @@ libgnunet_plugin_ats_ril_init (void *cls)
   GNUNET_assert(NULL != env->cfg);
   GNUNET_assert(NULL != env->stats);
   GNUNET_assert(NULL != env->bandwidth_changed_cb);
-  GNUNET_assert(NULL != env->get_preferences_cb);
-  GNUNET_assert(NULL != env->get_property_cb);
+  GNUNET_assert(NULL != env->get_preferences);
+  GNUNET_assert(NULL != env->get_property);
 
   if (GNUNET_OK
-      != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
+      != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
+          &solver->parameters.step_time_min))
+  {
+    solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
+  }
+  if (GNUNET_OK
+      != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
+          &solver->parameters.step_time_max))
   {
-    solver->step_time = RIL_DEFAULT_STEP_TIME;
+    solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string)
-      && NULL != string && 0 == strcmp (string, "SARSA"))
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
   {
-    solver->parameters.algorithm = RIL_ALGO_SARSA;
+    solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
+    GNUNET_free (string);
   }
   else
   {
     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_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
+  {
+    solver->parameters.beta = strtod (string, NULL);
+    GNUNET_free (string);
+  }
+  else
+  {
+    solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
+  }
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
   {
-    solver->parameters.gamma = (double) tmp / 100;
+    solver->parameters.gamma = strtod (string, NULL);
+    GNUNET_free (string);
   }
   else
   {
-    solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
+    solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
+  if (GNUNET_OK
+      == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
   {
-    solver->parameters.alpha = (double) tmp / 100;
+    solver->parameters.alpha = strtod (string, NULL);
+    GNUNET_free (string);
   }
   else
   {
     solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_TRACE_DECAY", &tmp))
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
   {
-    solver->parameters.lambda = (double) tmp / 100;
+    solver->parameters.lambda = strtod (string, NULL);
+    GNUNET_free (string);
   }
   else
   {
     solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
   }
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
+  {
+    solver->parameters.explore_ratio = strtod (string, NULL);
+    GNUNET_free (string);
+  }
+  else
+  {
+    solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
+  }
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
+  {
+    solver->parameters.reward_global_share = strtod (string, NULL);
+    GNUNET_free (string);
+  }
+  else
+  {
+    solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
+  }
+  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
+  {
+    solver->simulate = GNUNET_NO;
+  }
 
   env->sf.s_add = &GAS_ril_address_add;
   env->sf.s_address_update_property = &GAS_ril_address_property_changed;
@@ -1317,37 +1907,39 @@ 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_cb;
-  solver->callbacks->get_preferences_cls = env->get_preference_cls;
-  solver->callbacks->get_properties = env->get_property_cb;
-  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;
+  solver->global_state_networks = GNUNET_malloc (solver->networks_count * RIL_FEATURES_NETWORK_COUNT * sizeof (double));
+  solver->done = GNUNET_NO;
 
   for (c = 0; c < env->network_count; c++)
   {
     cur = &solver->network_entries[c];
     cur->type = env->networks[c];
     cur->bw_in_available = env->in_quota[c];
-    cur->bw_in_assigned = 0;
     cur->bw_out_available = env->out_quota[c];
-    cur->bw_out_assigned = 0;
+    LOG(GNUNET_ERROR_TYPE_INFO, "init()  Quotas for %s network:  IN %llu - OUT %llu\n", GNUNET_ATS_print_network_type(cur->type), cur->bw_in_available/1024, cur->bw_out_available/1024);
   }
 
-  solver->next_step = GNUNET_SCHEDULER_add_delayed (
-      GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
-      &ril_periodic_step, solver);
+  LOG(GNUNET_ERROR_TYPE_INFO, "init()  Parameters:\n");
+  LOG(GNUNET_ERROR_TYPE_INFO, "init()  Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
+      solver->parameters.algorithm ? "Q" : "SARSA",
+      solver->parameters.alpha,
+      solver->parameters.beta,
+      solver->parameters.lambda);
+  LOG(GNUNET_ERROR_TYPE_INFO, "init()  explore = %f, global_share = %f\n",
+      solver->parameters.explore_ratio,
+      solver->parameters.reward_global_share);
 
   return solver;
 }
 
-//TODO doxygen
+/**
+ * Exit point for the plugin
+ *
+ * @param cls the solver handle
+ */
 void *
 libgnunet_plugin_ats_ril_done (void *cls)
 {
@@ -1357,6 +1949,8 @@ libgnunet_plugin_ats_ril_done (void *cls)
 
   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
 
+  s->done = GNUNET_YES;
+
   cur_agent = s->agents_head;
   while (NULL != cur_agent)
   {
@@ -1366,16 +1960,21 @@ libgnunet_plugin_ats_ril_done (void *cls)
     cur_agent = next_agent;
   }
 
-  GNUNET_SCHEDULER_cancel (s->next_step);
-  GNUNET_free(s->callbacks);
+  if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
+  {
+    GNUNET_SCHEDULER_cancel (s->step_next_task_id);
+  }
   GNUNET_free(s->network_entries);
+  GNUNET_free(s->global_state_networks);
   GNUNET_free(s);
 
   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
@@ -1394,7 +1993,8 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
   unsigned int n_old;
   int i;
   unsigned int zero;
-  uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
 
   net = ril_get_network (s, network);
   address->solver_information = net;
@@ -1402,7 +2002,7 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t 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",
+        "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
         address->plugin, address->addr, GNUNET_i2s (&address->peer));
     return;
   }
@@ -1437,25 +2037,24 @@ 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);
 
-  if (NULL == agent->address_inuse)
-  {
-    net->bw_in_assigned += min_bw;
-    net->bw_out_assigned += min_bw;
-    envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
-  }
+  ril_try_unblock_agent(s, agent, 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));
+  ril_step (s);
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
+      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
@@ -1473,9 +2072,8 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
   unsigned int n_new;
   int i;
   struct RIL_Network *net;
-  uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
 
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
       session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
       address->addr, GNUNET_i2s (&address->peer));
 
@@ -1508,12 +2106,17 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
   m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
   n_new = agent->n - 1;
 
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "first\n");
+
   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)
+        ((RIL_FEATURES_NETWORK_COUNT) //TODO! replace, when adding more networks
+            + (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]);
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "second\n");
   ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
       1, agent->n);
   //correct last action
@@ -1526,43 +2129,36 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
     agent->a_old = RIL_ACTION_INVALID;
   }
   //decrease old state vector and eligibility vector
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "third\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);
+      //((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
+      ((RIL_FEATURES_NETWORK_COUNT) //TODO! replace when adding more networks
+          + (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)
+      ((RIL_FEATURES_NETWORK_COUNT) //TODO! replace when adding more networks
+          + (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;
+      envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, MIN_BW, MIN_BW,
+          GNUNET_NO);
     }
     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");
+  ril_step (solver);
 }
 
 /**
- * Transport properties for this address have changed
+ * Update the properties of an address in the solver
  *
  * @param solver solver handle
  * @param address the address
@@ -1578,16 +2174,15 @@ GAS_ril_address_property_changed (void *solver,
     double rel_value)
 {
   LOG(GNUNET_ERROR_TYPE_DEBUG,
-      "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
+      "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
           "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
-   */
+
+  ril_step (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 +2204,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 +2216,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 */
+  /*
+   * TODO? Future Work: Potentially add usage variable to 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
  *
@@ -1644,8 +2242,6 @@ 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__);
 
   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
       "%s address of peer %s from '%s' to '%s'\n",
@@ -1667,18 +2263,11 @@ GAS_ril_address_change_network (void *solver,
     return;
   }
 
-  net = ril_get_network (s, current_network);
-  net->bw_in_assigned -= agent->bw_in;
-  net->bw_out_assigned -= agent->bw_out;
-
-  net = ril_get_network (s, new_network);
-  net->bw_in_assigned -= min_bw;
-  net->bw_out_assigned -= min_bw;
-  address->solver_information = net;
+  address->solver_information = ril_get_network(solver, new_network);
 }
 
 /**
- * Get application feedback for a peer
+ * Give feedback about the current assignment
  *
  * @param solver the solver handle
  * @param application the application
@@ -1695,7 +2284,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",
@@ -1710,26 +2298,45 @@ GAS_ril_address_preference_feedback (void *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.
-   */
+  struct GAS_RIL_Handle *s = solver;
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
+
+  s->bulk_lock++;
 }
 
 /**
  * Bulk operation done
+ *
+ * @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.
-   */
+  struct GAS_RIL_Handle *s = solver;
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
+
+  if (s->bulk_lock < 1)
+  {
+    GNUNET_break(0);
+    return;
+  }
+  s->bulk_lock--;
+
+  if (0 < s->bulk_changes)
+  {
+    ril_step (solver);
+    s->bulk_changes = 0;
+  }
 }
 
 /**
- * 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
@@ -1743,29 +2350,36 @@ GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *p
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
 
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
+
   agent = ril_get_agent (s, peer, GNUNET_YES);
 
-  agent->active = GNUNET_YES;
+  agent->is_active = GNUNET_YES;
+  envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
 
-  envi_set_active_suggestion(s, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
+  ril_try_unblock_agent(solver, agent, GNUNET_YES);
 
   if (agent->address_inuse)
   {
     LOG(GNUNET_ERROR_TYPE_DEBUG,
-        "API_get_preferred_address() Activated agent for peer '%s' with %s address %p\n",
+        "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\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));
+        "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
@@ -1776,6 +2390,8 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
 
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
+
   agent = ril_get_agent (s, peer, GNUNET_NO);
 
   if (NULL == agent)
@@ -1783,14 +2399,18 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
     GNUNET_break(0);
     return;
   }
-  if (GNUNET_NO == agent->active)
+  if (GNUNET_NO == agent->is_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);
+  agent->is_active = GNUNET_NO;
+
+  envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
+      GNUNET_YES);
+
+  ril_step (s);
 
   LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",