fixing resource leaks
[oweals/gnunet.git] / src / ats / plugin_ats_ril.c
old mode 100755 (executable)
new mode 100644 (file)
index d29767a..2bf715e
 
 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
 
-#define RIL_MIN_BW ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)
-#define RIL_MAX_BW GNUNET_ATS_MaxBandwidth
-
-#define RIL_ACTION_INVALID -1
-#define RIL_INTERVAL_EXPONENT 10
-#define RIL_UTILITY_MAX (double) RIL_MAX_BW
-
-#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_Q
-#define RIL_DEFAULT_SELECT              RIL_SELECT_EGREEDY
-#define RIL_DEFAULT_WELFARE             RIL_WELFARE_EGALITARIAN
-#define RIL_DEFAULT_DISCOUNT_BETA       1.0
+#define RIL_MIN_BW                      (5 * ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__))
+#define RIL_MAX_BW                      GNUNET_ATS_MaxBandwidth
+
+#define RIL_ACTION_INVALID              -1
+#define RIL_INTERVAL_EXPONENT           10
+#define RIL_UTILITY_DELAY_MAX           1000
+
+#define RIL_DEFAULT_STEP_TIME_MIN       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200)
+#define RIL_DEFAULT_STEP_TIME_MAX       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 2000)
+#define RIL_DEFAULT_ALGORITHM           RIL_ALGO_SARSA
+#define RIL_DEFAULT_SELECT              RIL_SELECT_SOFTMAX
+#define RIL_DEFAULT_WELFARE             RIL_WELFARE_NASH
+#define RIL_DEFAULT_DISCOUNT_BETA       0.6
 #define RIL_DEFAULT_DISCOUNT_GAMMA      0.5
-#define RIL_DEFAULT_GRADIENT_STEP_SIZE  0.1
+#define RIL_DEFAULT_GRADIENT_STEP_SIZE  0.01
 #define RIL_DEFAULT_TRACE_DECAY         0.5
-#define RIL_DEFAULT_EXPLORE_RATIO       0.1
-#define RIL_DEFAULT_RBF_DIVISOR         10
-#define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5
-#define RIL_DEFAULT_TEMPERATURE         1.0
+#define RIL_DEFAULT_EXPLORE_RATIO       1
+#define RIL_DEFAULT_EXPLORE_DECAY       0.95
+#define RIL_DEFAULT_RBF_DIVISOR         50
+#define RIL_DEFAULT_TEMPERATURE         0.1
+#define RIL_DEFAULT_TEMPERATURE_DECAY   1
 
 #define RIL_INC_DEC_STEP_SIZE           1
-#define RIL_NOP_BONUS                   0.5
+#define RIL_NOP_DECAY                   0.5
 
 /**
  * ATS reinforcement learning solver
 enum RIL_Action_Type
 {
   RIL_ACTION_NOTHING = 0,
-  RIL_ACTION_BW_IN_DBL = -2, //TODO! put actions back
+  RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions
   RIL_ACTION_BW_IN_HLV = -3,
   RIL_ACTION_BW_IN_INC = 1,
   RIL_ACTION_BW_IN_DEC = 2,
   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 = 3
+  RIL_ACTION_BW_OUT_INC = 3,
+  RIL_ACTION_BW_OUT_DEC = 4,
+  RIL_ACTION_TYPE_NUM = 5
 };
 
 enum RIL_Algorithm
@@ -98,7 +99,7 @@ enum RIL_Welfare
 
 enum RIL_E_Modification
 {
-  RIL_E_DISCOUNT,
+  RIL_E_DECAY,
   RIL_E_ZERO,
   RIL_E_ACCUMULATE,
   RIL_E_REPLACE
@@ -139,11 +140,21 @@ struct RIL_Learning_Parameters
    */
   enum RIL_E_Modification eligibility_trace_mode;
 
+  /**
+   * Initial softmax action-selection temperature
+   */
+  double temperature_init;
+
   /**
    * Softmax action-selection temperature
    */
   double temperature;
 
+  /**
+   * Decay factor of the temperature value
+   */
+  double temperature_decay;
+
   /**
    * Which measure of social welfare should be used
    */
@@ -159,15 +170,20 @@ struct RIL_Learning_Parameters
    */
   enum RIL_Select select;
 
+  /**
+   * Initial exploration ratio value
+   */
+  double epsilon_init;
+
   /**
    * Ratio, with what probability an agent should explore in the e-greed policy
    */
-  double explore_ratio;
+  double epsilon;
 
   /**
-   * How big the share of the global part of the reward signal is
+   * Decay factor of the explore ratio
    */
-  double reward_global_share;
+  double epsilon_decay;
 
   /**
    * Minimal interval time between steps in milliseconds
@@ -263,6 +279,11 @@ struct RIL_Peer_Agent
    */
   double ** E;
 
+  /**
+   * Whether to reset the eligibility traces to 0 after a Q-exploration step
+   */
+  int eligibility_reset;
+
   /**
    * Address in use
    */
@@ -326,20 +347,30 @@ struct RIL_Scope
    */
   unsigned long long bw_in_assigned;
 
+  /**
+   * Bandwidth inbound actually utilized in the network
+   */
+  unsigned long long bw_in_utilized;
+
   /**
    * Total available outbound bandwidth
    */
   unsigned long long bw_out_available;
 
   /**
-   * Bandwidth outbound assigned in network after last step
+   * Bandwidth outbound assigned in network after last step
    */
   unsigned long long bw_out_assigned;
 
+  /**
+   * Bandwidth outbound actually utilized in the network
+   */
+  unsigned long long bw_out_utilized;
+
   /**
    * Number of active agents in scope
    */
-  unsigned int agent_count;
+  unsigned int active_agent_count;
 
   /**
    * The social welfare achieved in the scope
@@ -430,7 +461,7 @@ struct GAS_RIL_Handle
 };
 
 /*
- *  Private functions
+ *  "Private" functions
  *  ---------------------------
  */
 
@@ -443,7 +474,7 @@ struct GAS_RIL_Handle
  * @return estimation value
  */
 static double
-agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
+agent_q (struct RIL_Peer_Agent *agent, double *state, int action)
 {
   int i;
   double result = 0;
@@ -496,7 +527,7 @@ agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *addre
  * @return wrapped address
  */
 static struct RIL_Address_Wrapped *
-agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
+agent_address_get_wrapped (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
 {
   struct RIL_Address_Wrapped *cur;
 
@@ -526,7 +557,7 @@ agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
     break;
   case RIL_ACTION_BW_IN_DEC:
   case RIL_ACTION_BW_IN_HLV:
-    if (agent->bw_in <= RIL_MIN_BW)
+    if (agent->bw_in <= 0)
       return GNUNET_NO;
     else
       return GNUNET_YES;
@@ -540,7 +571,7 @@ agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
     break;
   case RIL_ACTION_BW_OUT_DEC:
   case RIL_ACTION_BW_OUT_HLV:
-    if (agent->bw_out <= RIL_MIN_BW)
+    if (agent->bw_out <= 0)
       return GNUNET_NO;
     else
       return GNUNET_YES;
@@ -587,7 +618,7 @@ agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
   {
     if (agent_action_is_possible(agent, i))
     {
-      cur_q = agent_estimate_q (agent, state, i);
+      cur_q = agent_q (agent, state, i);
       if (cur_q > max_q)
       {
         max_q = cur_q;
@@ -601,7 +632,12 @@ agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
   return max_i;
 }
 
-
+/**
+ * Chooses a random action from the set of possible ones
+ *
+ * @param agent the agent performing the action
+ * @return the action index
+ */
 static int
 agent_get_action_random (struct RIL_Peer_Agent *agent)
 {
@@ -637,6 +673,7 @@ agent_get_action_random (struct RIL_Peer_Agent *agent)
   }
 
   GNUNET_assert(GNUNET_NO);
+  return RIL_ACTION_INVALID;
 }
 
 
@@ -649,33 +686,37 @@ agent_get_action_random (struct RIL_Peer_Agent *agent)
  * @param a_prime the new
  */
 static void
-agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
+agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
 {
   int i;
+  int k;
   double delta;
-  double *theta = agent->W[agent->a_old];
+  double **theta = agent->W;
 
   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
+  delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
+  delta -= agent_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_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),
+//      agent_q (agent, s_next, a_prime),
 //      delta);
 
-  for (i = 0; i < agent->m; i++)
+  for (k = 0; k < agent->n; k++)
   {
-//    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[a_prime][i];
+    for (i = 0; i < agent->m; i++)
+    {
+  //    LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f   delta = %f   e[%d] = %f\n",
+  //        agent->envi->parameters.alpha,
+  //        delta,
+  //        i,
+  //        agent->e[i]);
+      theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
+    }
   }
 }
 
@@ -684,12 +725,13 @@ 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 @a feature to each component as in accumulating eligibility traces
  * #RIL_E_REPLACE - resets each component to @a feature  as in replacing traces
- * #RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
+ * #RIL_E_DECAY - 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 the agent handle
  * @param mod the kind of modification
  * @param feature the feature vector
+ * @param action the action to take
  */
 static void
 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
@@ -708,10 +750,13 @@ agent_modify_eligibility (struct RIL_Peer_Agent *agent,
       agent->E[action][i] += feature[i];
       break;
     case RIL_E_REPLACE:
-      agent->E[action][i] =  (agent->envi->global_discount_variable * agent->envi->parameters.lambda * agent->E[action][i]) > feature[i] ? agent->E[action][i] : feature[i];
+      agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
       break;
-    case RIL_E_DISCOUNT:
-      agent->E[action][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
+    case RIL_E_DECAY:
+      for (k = 0; k < agent->n; k++)
+      {
+        agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
+      }
       break;
     case RIL_E_ZERO:
       for (k = 0; k < agent->n; k++)
@@ -723,16 +768,32 @@ agent_modify_eligibility (struct RIL_Peer_Agent *agent,
   }
 }
 
-
+/**
+ * Informs the environment about the status of the solver
+ *
+ * @param solver
+ * @param op
+ * @param stat
+ */
 static void
 ril_inform (struct GAS_RIL_Handle *solver,
-    enum GAS_Solver_Operation op,
-    enum GAS_Solver_Status stat)
+            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);
 }
 
+/**
+ * Calculates the maximum bandwidth an agent can assign in a network scope
+ *
+ * @param net
+ */
+static unsigned long long
+ril_get_max_bw (struct RIL_Scope *net)
+{
+  return GNUNET_MIN(2 * GNUNET_MAX(net->bw_in_available, net->bw_out_available), GNUNET_ATS_MaxBandwidth);
+}
 
 /**
  * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
@@ -824,31 +885,6 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
 }
 
 
-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_Scope *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;
-}
-
 /**
  * Allocates a state vector and fills it with the features present
  * @param solver the solver handle
@@ -871,7 +907,7 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 
   state = GNUNET_malloc (sizeof(double) * agent->m);
 
-  max_bw = RIL_MAX_BW;
+  max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
 
   y[0] = (double) agent->bw_out;
   y[1] = (double) agent->bw_in;
@@ -885,7 +921,7 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
       x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
       d[0] = x[0]-y[0];
       d[1] = x[1]-y[1];
-      sigma = (((double) max_bw / (double) solver->parameters.rbf_divisor) / 2.0) * M_SQRT2;
+      sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
       f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
       state[m++] = f;
     }
@@ -894,120 +930,68 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
   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_Scope *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_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;
-//}
+/**
+ * Retrieves an ATS information value of an address
+ *
+ * @param address the address in question
+ * @param type the ATS information type
+ * @return the value
+ */
+static unsigned 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 GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
 
+  for (c1 = 0; c1 < address->atsi_count; c1++)
+  {
+    if (ntohl (address->atsi[c1].type) == type)
+      return ntohl (address->atsi[c1].value);
+  }
+  return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
+}
+
+/**
+ * Returns the utility value of the connection an agent manages
+ *
+ * @param agent the agent in question
+ * @return the utility value
+ */
 static double
 agent_get_utility (struct RIL_Peer_Agent *agent)
 {
-  return (double) (agent->bw_in/RIL_MIN_BW);
+  const double *preferences;
+  double delay_atsi;
+  double delay_norm;
+  double pref_match;
+
+  preferences = agent->envi->plugin_envi->get_preferences (agent->envi->plugin_envi->get_preference_cls,
+      &agent->peer);
+
+  delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
+  delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
+
+  pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
+  pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
+      sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
+//      sqrt((double) (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)/RIL_MIN_BW) * (double) (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)/RIL_MIN_BW));
+
+//  return (double) (agent->bw_in/RIL_MIN_BW);
 //  return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
+  return pref_match;
 }
 
+/**
+ * Calculates the social welfare within a network scope according to what social
+ * welfare measure is set in the configuration.
+ *
+ * @param solver the solver handle
+ * @param scope the network scope in question
+ * @return the social welfare value
+ */
 static double
 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
 {
@@ -1017,7 +1001,7 @@ ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope
   switch (solver->parameters.social_welfare)
   {
   case RIL_WELFARE_EGALITARIAN:
-    result = RIL_UTILITY_MAX;
+    result = DBL_MAX;
     for (cur = solver->agents_head; NULL != cur; cur = cur->next)
     {
       if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
@@ -1033,15 +1017,46 @@ ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope
     {
       if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
       {
-        result *= agent_get_utility(cur);
+        result *= pow(agent_get_utility(cur), 1.0 / (double) scope->active_agent_count);
       }
     }
-    return pow(result, 1.0 / (double) scope->agent_count);
+    return result;
   }
   GNUNET_assert(GNUNET_NO);
   return 1;
 }
 
+static double
+envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
+{
+  struct RIL_Scope *net;
+  unsigned long long over_max;
+  unsigned long long over_in = 0;
+  unsigned long long over_out = 0;
+
+  net = agent->address_inuse->solver_information;
+
+  if (net->bw_in_utilized > net->bw_in_available)
+  {
+    over_in = net->bw_in_utilized - net->bw_in_available;
+    if (RIL_ACTION_BW_IN_INC == agent->a_old)
+    {
+      over_in *= 2;
+    }
+  }
+  if (net->bw_out_utilized > net->bw_out_available)
+  {
+    over_out = net->bw_out_utilized - net->bw_out_available;
+    if (RIL_ACTION_BW_OUT_INC == agent->a_old)
+    {
+      over_out *= 2;
+    }
+  }
+  over_max = (over_in + over_out) / RIL_MIN_BW;
+
+  return -1.0 * (double) over_max;
+}
+
 /**
  * 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
@@ -1055,42 +1070,32 @@ static double
 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 {
   struct RIL_Scope *net;
-  unsigned long long overutilization;
-  unsigned long long over_in = 0;
-  unsigned long long over_out = 0;
   double objective;
   double delta;
   double steady;
+  double penalty;
+  double reward;
 
   net = agent->address_inuse->solver_information;
 
-  if (net->bw_in_assigned > net->bw_in_available)
-    over_in = net->bw_in_assigned - net->bw_in_available;
-  if (net->bw_out_assigned > net->bw_out_available)
-    over_out = net->bw_out_assigned - net->bw_out_available;
-  overutilization = GNUNET_MAX(over_in, over_out) / RIL_MIN_BW;
-
-  objective = agent_get_utility (agent) + net->social_welfare;
+  penalty = envi_get_penalty(solver, agent);
+  objective = (agent_get_utility (agent) + net->social_welfare) / 2;
   delta = objective - agent->objective_old;
   agent->objective_old = objective;
 
-  if (delta != 0)
+  if (delta != 0 && penalty == 0)
   {
-    agent->nop_bonus = 0;
-  }
-
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "utility: %f, welfare: %f, objective, overutilization: %d\n", agent_get_utility (agent), net->social_welfare, objective, overutilization);
-
-  steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
-
-  if (0 != overutilization)
-  {
-    return -1.0 * (double) overutilization;
+    agent->nop_bonus = delta * RIL_NOP_DECAY;
   }
   else
   {
-    return delta + steady;
+    agent->nop_bonus *= RIL_NOP_DECAY;
   }
+
+  steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
+
+  reward = delta + steady;
+  return reward + penalty;
 }
 
 /**
@@ -1106,20 +1111,23 @@ envi_action_bw_double (struct GAS_RIL_Handle *solver,
     int direction_in)
 {
   unsigned long long new_bw;
+  unsigned long long max_bw;
+
+  max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
 
   if (direction_in)
   {
     new_bw = agent->bw_in * 2;
-    if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
-      new_bw = RIL_MAX_BW;
+    if (new_bw < agent->bw_in || new_bw > max_bw)
+      new_bw = max_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 < agent->bw_out || new_bw > RIL_MAX_BW)
-      new_bw = RIL_MAX_BW;
+    if (new_bw < agent->bw_out || new_bw > max_bw)
+      new_bw = max_bw;
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
         new_bw, GNUNET_NO);
   }
@@ -1144,16 +1152,16 @@ envi_action_bw_halven (struct GAS_RIL_Handle *solver,
   if (direction_in)
   {
     new_bw = agent->bw_in / 2;
-    if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
-      new_bw = RIL_MIN_BW;
+    if (new_bw <= 0 || new_bw > agent->bw_in)
+      new_bw = 0;
     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 < RIL_MIN_BW || new_bw > agent->bw_out)
-      new_bw = RIL_MIN_BW;
+    if (new_bw <= 0 || new_bw > agent->bw_out)
+      new_bw = 0;
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
         GNUNET_NO);
   }
@@ -1171,20 +1179,23 @@ static void
 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
 {
   unsigned long long new_bw;
+  unsigned long long max_bw;
+
+  max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
 
   if (direction_in)
   {
     new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
-    if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
-      new_bw = RIL_MAX_BW;
+    if (new_bw < agent->bw_in || new_bw > max_bw)
+      new_bw = max_bw;
     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 * RIL_MIN_BW);
-    if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
-      new_bw = RIL_MAX_BW;
+    if (new_bw < agent->bw_out || new_bw > max_bw)
+      new_bw = max_bw;
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
         new_bw, GNUNET_NO);
   }
@@ -1207,16 +1218,16 @@ envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
   if (direction_in)
   {
     new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
-    if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
-      new_bw = RIL_MIN_BW;
+    if (new_bw <= 0 || new_bw > agent->bw_in)
+      new_bw = 0;
     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 * RIL_MIN_BW);
-    if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
-      new_bw = RIL_MIN_BW;
+    if (new_bw <= 0 || new_bw > agent->bw_out)
+      new_bw = 0;
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
         GNUNET_NO);
   }
@@ -1237,6 +1248,8 @@ envi_action_address_switch (struct GAS_RIL_Handle *solver,
   struct RIL_Address_Wrapped *cur;
   int i = 0;
 
+  cur = agent_address_get_wrapped(agent, agent->address_inuse);
+
   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
   {
     if (i == address_index)
@@ -1310,6 +1323,17 @@ envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int
   }
 }
 
+/**
+ * Selects the next action using the e-greedy strategy. I.e. with a probability
+ * of (1-e) the action with the maximum expected return will be chosen
+ * (=> exploitation) and with probability (e) a random action will be chosen.
+ * In case the Q-learning rule is set, the function also resets the eligibility
+ * traces in the exploration case (after Watkin's Q-learning).
+ *
+ * @param agent the agent selecting an action
+ * @param state the current state-feature vector
+ * @return the action index
+ */
 static int
 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
 {
@@ -1317,22 +1341,19 @@ agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
   double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
         UINT32_MAX) / (double) UINT32_MAX;
 
-  if (r < agent->envi->parameters.explore_ratio) //explore
+  if (r < agent->envi->parameters.epsilon) //explore
   {
     action = agent_get_action_random(agent);
     if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
     {
-      agent_modify_eligibility(agent, RIL_E_ZERO, NULL, action);
+      agent->eligibility_reset = GNUNET_YES;
     }
+    agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
     return action;
   }
   else //exploit
   {
     action = agent_get_action_max(agent, state);
-    if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
-    {
-      agent_modify_eligibility(agent, RIL_E_DISCOUNT, NULL, action);
-    }
     return action;
   }
 }
@@ -1363,7 +1384,7 @@ agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
   {
     if (agent_action_is_possible(agent, i))
     {
-      eqt[i] = exp(agent_estimate_q(agent,state,i) / agent->envi->parameters.temperature);
+      eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
       sum += eqt[i];
     }
   }
@@ -1385,20 +1406,28 @@ agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
   {
     if (sum + p[i] > r)
     {
-      if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
+      if (i != a_max)
       {
-        if (i == a_max)
-          agent_modify_eligibility(agent, RIL_E_DISCOUNT, NULL, i);
-        else
-          agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
+        if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
+          agent->eligibility_reset = GNUNET_YES;
+        agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay;
       }
       return i;
     }
     sum += p[i];
   }
   GNUNET_assert(GNUNET_NO);
+  return RIL_ACTION_INVALID;
 }
 
+/**
+ * Select the next action of an agent either according to the e-greedy strategy
+ * or the softmax strategy.
+ *
+ * @param agent the agent in question
+ * @param state the current state-feature vector
+ * @return the action index
+ */
 static int
 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
 {
@@ -1435,6 +1464,20 @@ agent_step (struct RIL_Peer_Agent *agent)
   s_next = envi_get_state (agent->envi, agent);
   reward = envi_get_reward (agent->envi, agent);
 
+  if (agent->eligibility_reset)
+  {
+    agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
+    agent->eligibility_reset = GNUNET_NO;
+  }
+  else
+  {
+    agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
+  }
+  if (RIL_ACTION_INVALID != agent->a_old)
+  {
+    agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
+  }
+
   switch (agent->envi->parameters.algorithm)
   {
   case RIL_ALGO_SARSA:
@@ -1442,9 +1485,8 @@ agent_step (struct RIL_Peer_Agent *agent)
     if (RIL_ACTION_INVALID != agent->a_old)
     {
       //updates weights with selected action (on-policy), if not first step
-      agent_update_weights (agent, reward, s_next, a_next);
+      agent_update (agent, reward, s_next, a_next);
     }
-    agent_modify_eligibility (agent, RIL_E_DISCOUNT, s_next, a_next);
     break;
 
   case RIL_ALGO_Q:
@@ -1452,7 +1494,7 @@ agent_step (struct RIL_Peer_Agent *agent)
     if (RIL_ACTION_INVALID != agent->a_old)
     {
       //updates weights with best action, disregarding actually selected action (off-policy), if not first step
-      agent_update_weights (agent, reward, s_next, a_max);
+      agent_update (agent, reward, s_next, a_max);
     }
     a_next = agent_select_action (agent, s_next);
     break;
@@ -1460,8 +1502,6 @@ agent_step (struct RIL_Peer_Agent *agent)
 
   GNUNET_assert(RIL_ACTION_INVALID != a_next);
 
-  agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, s_next, a_next);
-
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step()  Step# %llu  R: %f  IN %llu  OUT %llu  A: %d\n",
         agent->step_count,
         reward,
@@ -1478,6 +1518,11 @@ agent_step (struct RIL_Peer_Agent *agent)
   agent->step_count += 1;
 }
 
+/**
+ * Prototype of the ril_step() procedure
+ *
+ * @param solver the solver handle
+ */
 static void
 ril_step (struct GAS_RIL_Handle *solver);
 
@@ -1497,6 +1542,14 @@ ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *t
   ril_step (solver);
 }
 
+/**
+ * Determines how much of the available bandwidth is assigned. If more is
+ * assigned than available it returns 1. The function is used to determine the
+ * step size of the adaptive stepping.
+ *
+ * @param solver the solver handle
+ * @return the ratio
+ */
 static double
 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
 {
@@ -1511,8 +1564,8 @@ ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
     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_assigned += net.bw_in_utilized;
+      sum_assigned += net.bw_out_utilized;
       sum_available += net.bw_in_available;
       sum_available += net.bw_out_available;
     }
@@ -1526,7 +1579,7 @@ ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
     ratio = 0;
   }
 
-  return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
+  return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
 }
 
 /**
@@ -1551,6 +1604,15 @@ ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
   return NULL ;
 }
 
+/**
+ * Determines whether more connections are allocated in a network scope, than
+ * they would theoretically fit. This is used as a heuristic to determine,
+ * whether a new connection can be allocated or not.
+ *
+ * @param solver the solver handle
+ * @param network the network scope in question
+ * @return GNUNET_YES if there are theoretically enough resources left
+ */
 static int
 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
 {
@@ -1574,11 +1636,23 @@ ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_
   return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
 }
 
+/**
+ * Unblocks an agent for which a connection request is there, that could not
+ * be satisfied. Iterates over the addresses of the agent, if one of its
+ * addresses can now be allocated in its scope the agent is unblocked,
+ * otherwise it remains unchanged.
+ *
+ * @param solver the solver handle
+ * @param agent the agent in question
+ * @param silent
+ */
 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_Scope *net;
+  unsigned long long start_in;
+  unsigned long long start_out;
 
   for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
   {
@@ -1586,13 +1660,23 @@ ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *age
     if (ril_network_is_not_full(solver, net->type))
     {
       if (NULL == agent->address_inuse)
-        envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, RIL_MIN_BW, RIL_MIN_BW, silent);
+      {
+        start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
+        start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
+        envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, start_in, start_out, silent);
+      }
       return;
     }
   }
   agent->address_inuse = NULL;
 }
 
+/**
+ * Determines how much the reward needs to be discounted depending on the amount
+ * of time, which has passed since the last time-step.
+ *
+ * @param solver the solver handle
+ */
 static void
 ril_calculate_discount (struct GAS_RIL_Handle *solver)
 {
@@ -1600,7 +1684,7 @@ ril_calculate_discount (struct GAS_RIL_Handle *solver)
   struct GNUNET_TIME_Relative time_delta;
   double tau;
 
-  // MDP case - TODO remove when debugged
+  // MDP case only for debugging purposes
   if (solver->simulate)
   {
     solver->global_discount_variable = solver->parameters.gamma;
@@ -1623,6 +1707,13 @@ ril_calculate_discount (struct GAS_RIL_Handle *solver)
       / (double) solver->parameters.beta;
 }
 
+/**
+ * Count the number of active agents/connections in a network scope
+ *
+ * @param solver the solver handle
+ * @param scope the network scope in question
+ * @return the number of allocated connections
+ */
 static int
 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
 {
@@ -1639,6 +1730,82 @@ ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope
   return c;
 }
 
+/**
+ * Calculates how much bandwidth is assigned in sum in a network scope, either
+ * in the inbound or in the outbound direction.
+ *
+ * @param solver the solver handle
+ * @param type the type of the network scope in question
+ * @param direction_in GNUNET_YES if the inbound direction should be summed up,
+ *   otherwise the outbound direction will be summed up
+ * @return the sum of the assigned bandwidths
+ */
+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_Scope *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;
+}
+
+/**
+ * Calculates how much bandwidth is actually utilized in sum in a network scope,
+ * either in the inbound or in the outbound direction.
+ *
+ * @param solver the solver handle
+ * @param type the type of the network scope in question
+ * @param direction_in GNUNET_YES if the inbound direction should be summed up,
+ *   otherwise the outbound direction will be summed up
+ * @return the sum of the utilized bandwidths (in bytes/second)
+ */
+static unsigned long long
+ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
+{
+  struct RIL_Peer_Agent *cur;
+  struct RIL_Scope *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 += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
+        else
+          sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
+      }
+    }
+  }
+
+  return sum;
+}
+
+/**
+ * Retrieves the state of the network scope, so that its attributes are up-to-
+ * date.
+ *
+ * @param solver the solver handle
+ */
 static void
 ril_networks_update_state (struct GAS_RIL_Handle *solver)
 {
@@ -1649,8 +1816,10 @@ ril_networks_update_state (struct GAS_RIL_Handle *solver)
   {
     net = &solver->network_entries[c];
     net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
+    net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
     net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
-    net->agent_count = ril_network_count_active_agents(solver, net);
+    net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
+    net->active_agent_count = ril_network_count_active_agents(solver, net);
     net->social_welfare = ril_network_get_social_welfare(solver, net);
   }
 }
@@ -1682,11 +1851,17 @@ ril_step_schedule_next (struct GAS_RIL_Handle *solver)
   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);
+  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);
 
+//  LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
+//      used_ratio,
+//      factor,
+//      offset,
+//      y);
+
   if (solver->simulate)
   {
     time_next = GNUNET_TIME_UNIT_ZERO;
@@ -1776,7 +1951,7 @@ agent_w_init (struct RIL_Peer_Agent *agent)
   {
     for (k = 0; k < agent->m; k++)
     {
-      agent->W[i][k] = agent->envi->parameters.alpha * (1.0 - 2.0*((double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX)/(double)UINT32_MAX));
+      agent->W[i][k] = agent->envi->parameters.alpha * (1.0 - 2.0 * ((double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX)/(double) UINT32_MAX));
     }
   }
 }
@@ -1812,10 +1987,12 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
     agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
   }
   agent_w_init(agent);
+  agent->eligibility_reset = GNUNET_NO;
   agent->a_old = RIL_ACTION_INVALID;
   agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
   agent->address_inuse = NULL;
   agent->objective_old = 0;
+  agent->nop_bonus = 0;
 
   return agent;
 }
@@ -1963,7 +2140,11 @@ GAS_ril_address_change_preference (void *solver,
       "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
 
-  ril_step (solver);
+  struct GAS_RIL_Handle *s = solver;
+
+  s->parameters.temperature = s->parameters.temperature_init;
+  s->parameters.epsilon = s->parameters.epsilon_init;
+  ril_step (s);
 }
 
 /**
@@ -1993,18 +2174,21 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
   }
+
   if (GNUNET_OK
       != 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->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
   }
+
   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
   {
     solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
@@ -2014,6 +2198,7 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
   }
+
   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
   {
     solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
@@ -2023,6 +2208,7 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.select = RIL_DEFAULT_SELECT;
   }
+
   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
   {
     solver->parameters.beta = strtod (string, NULL);
@@ -2037,6 +2223,7 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     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 = strtod (string, NULL);
@@ -2051,6 +2238,7 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
   }
+
   if (GNUNET_OK
       == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
   {
@@ -2066,6 +2254,7 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
   }
+
   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
   {
     solver->parameters.lambda = strtod (string, NULL);
@@ -2080,52 +2269,72 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     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);
+    solver->parameters.epsilon_init = strtod (string, NULL);
     GNUNET_free (string);
-    if (solver->parameters.explore_ratio < 0 || solver->parameters.explore_ratio > 1)
+    if (solver->parameters.epsilon_init < 0 || solver->parameters.epsilon_init > 1)
     {
       LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_EXPLORE_RATIO not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_EXPLORE_RATIO);
-      solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
+      solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
     }
   }
   else
   {
-    solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
+    solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
+
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_DECAY", &string))
   {
-    solver->parameters.reward_global_share = strtod (string, NULL);
+    solver->parameters.epsilon_decay = strtod (string, NULL);
     GNUNET_free (string);
-    if (solver->parameters.reward_global_share < 0 || solver->parameters.reward_global_share > 1)
+    if (solver->parameters.epsilon_decay < 0 || solver->parameters.epsilon_decay > 1)
     {
-      LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_GLOBAL_REWARD_SHARE not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_GLOBAL_REWARD_SHARE);
-      solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
+      LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_EXPLORE_RATIO not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_EXPLORE_DECAY);
+      solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
     }
   }
   else
   {
-    solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
+    solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
   }
+
   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE", &string))
   {
-    solver->parameters.temperature = strtod (string, NULL);
+    solver->parameters.temperature_init = strtod (string, NULL);
     GNUNET_free (string);
-    if (solver->parameters.temperature <= 0)
+    if (solver->parameters.temperature_init <= 0)
     {
       LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE not positive. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE);
-      solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
+      solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
     }
   }
   else
   {
-    solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
+    solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
   }
+
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE_DECAY", &string))
+  {
+    solver->parameters.temperature_decay = strtod (string, NULL);
+    GNUNET_free (string);
+    if (solver->parameters.temperature_decay <= 0 || solver->parameters.temperature_decay > 1)
+    {
+      LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE_DECAY not in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE_DECAY);
+      solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
+    }
+  }
+  else
+  {
+  solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
+  }
+
   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
   {
     solver->simulate = GNUNET_NO;
   }
+
   if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
   {
     solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
@@ -2134,6 +2343,7 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
   }
+
   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
   {
     solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
@@ -2178,11 +2388,10 @@ libgnunet_plugin_ats_ril_init (void *cls)
       solver->parameters.alpha,
       solver->parameters.beta,
       solver->parameters.lambda);
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  exploration_ratio = %f, temperature = %f, ActionSelection = %s, global_share = %f\n",
-      solver->parameters.explore_ratio,
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
+      solver->parameters.epsilon,
       solver->parameters.temperature,
-      solver->parameters.select ? "EGREEDY" : "SOFTMAX",
-      solver->parameters.reward_global_share);
+      solver->parameters.select ? "EGREEDY" : "SOFTMAX");
   LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  RBF_DIVISOR = %llu\n",
       solver->parameters.rbf_divisor);
 
@@ -2260,6 +2469,9 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
     return;
   }
 
+  s->parameters.temperature = s->parameters.temperature_init;
+  s->parameters.epsilon = s->parameters.epsilon_init;
+
   agent = ril_get_agent (s, &address->peer, GNUNET_YES);
 
   //add address
@@ -2343,13 +2555,15 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
     return;
   }
 
+  s->parameters.temperature = s->parameters.temperature_init;
+  s->parameters.epsilon = s->parameters.epsilon_init;
+
   address_index = agent_address_get_index (agent, address);
-  address_wrapped = agent_address_get (agent, address);
+  address_wrapped = agent_address_get_wrapped (agent, address);
 
   if (NULL == address_wrapped)
   {
     net = address->solver_information;
-    GNUNET_assert(!ril_network_is_active (s, net->type));
     LOG(GNUNET_ERROR_TYPE_DEBUG,
         "Address not considered by agent, address was in inactive network\n");
     return;
@@ -2397,8 +2611,8 @@ 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
     {
-      envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, RIL_MIN_BW, RIL_MIN_BW,
-          GNUNET_NO);
+      envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, agent->bw_in, agent->bw_out,
+          GNUNET_YES);
     }
     else
     {
@@ -2430,7 +2644,11 @@ GAS_ril_address_property_changed (void *solver,
           "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
       address->addr, rel_value);
 
-  ril_step (solver);
+  struct GAS_RIL_Handle *s = solver;
+
+  s->parameters.temperature = s->parameters.temperature_init;
+  s->parameters.epsilon = s->parameters.epsilon_init;
+  ril_step (s);
 }
 
 /**
@@ -2494,6 +2712,9 @@ GAS_ril_address_change_network (void *solver,
       (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
       GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
 
+  s->parameters.temperature = s->parameters.temperature_init;
+  s->parameters.epsilon = s->parameters.epsilon_init;
+
   if (address->active && !ril_network_is_active (solver, new_network))
   {
     GAS_ril_address_delete (solver, address, GNUNET_NO);
@@ -2590,9 +2811,6 @@ GAS_ril_bulk_stop (void *solver)
 const struct ATS_Address *
 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
 {
-  /*
-   * activate agent, return currently chosen address
-   */
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
 
@@ -2616,8 +2834,9 @@ GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *p
     LOG(GNUNET_ERROR_TYPE_DEBUG,
         "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
         GNUNET_i2s (peer));
+    s->parameters.temperature = s->parameters.temperature_init;
+    s->parameters.epsilon = s->parameters.epsilon_init;
   }
-
   return agent->address_inuse;
 }
 
@@ -2651,6 +2870,9 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
     return;
   }
 
+  s->parameters.temperature = s->parameters.temperature_init;
+  s->parameters.epsilon = s->parameters.epsilon_init;
+
   agent->is_active = GNUNET_NO;
 
   envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,