-revert of address_bw addition
[oweals/gnunet.git] / src / ats / plugin_ats_ril.c
index 88120d517f51e845772787b38350b24d69676f94..2177c91ac60142cb98bc528e25d57d5b8a19a3e0 100755 (executable)
 
 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
 
-#define MIN_BW ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)
-
-#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_MIN_BW                      (1 * 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, 1000)
+#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_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_RBF_DIVISOR         50
 #define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5
+#define RIL_DEFAULT_TEMPERATURE         1.0
 
-#define RIL_INC_DEC_STEP_SIZE 1
+#define RIL_INC_DEC_STEP_SIZE           1
+#define RIL_NOP_DECAY                   0.5
 
 /**
  * ATS reinforcement learning solver
  */
 enum RIL_Action_Type
 {
-  RIL_ACTION_NOTHING = -1,
-  RIL_ACTION_BW_IN_DBL = -2, //TODO! put actions back
+  RIL_ACTION_NOTHING = 0,
+  RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions
   RIL_ACTION_BW_IN_HLV = -3,
-  RIL_ACTION_BW_IN_INC = 0,
-  RIL_ACTION_BW_IN_DEC = 1,
+  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 = 1
+  RIL_ACTION_BW_OUT_INC = 3,
+  RIL_ACTION_BW_OUT_DEC = 4,
+  RIL_ACTION_TYPE_NUM = 5
 };
 
 enum RIL_Algorithm
@@ -79,9 +84,21 @@ enum RIL_Algorithm
   RIL_ALGO_Q = 1
 };
 
+enum RIL_Select
+{
+  RIL_SELECT_SOFTMAX = 0,
+  RIL_SELECT_EGREEDY = 1
+};
+
+enum RIL_Welfare
+{
+  RIL_WELFARE_NASH,
+  RIL_WELFARE_EGALITARIAN
+};
+
 enum RIL_E_Modification
 {
-  RIL_E_SET,
+  RIL_E_DISCOUNT,
   RIL_E_ZERO,
   RIL_E_ACCUMULATE,
   RIL_E_REPLACE
@@ -117,6 +134,31 @@ struct RIL_Learning_Parameters
    */
   double lambda;
 
+  /**
+   * Whether to accumulate or replace eligibility traces
+   */
+  enum RIL_E_Modification eligibility_trace_mode;
+
+  /**
+   * Softmax action-selection temperature
+   */
+  double temperature;
+
+  /**
+   * Which measure of social welfare should be used
+   */
+  enum RIL_Welfare social_welfare;
+
+  /**
+   * State space divisor
+   */
+  unsigned long long rbf_divisor;
+
+  /**
+   * Action selection strategy;
+   */
+  enum RIL_Select select;
+
   /**
    * Ratio, with what probability an agent should explore in the e-greed policy
    */
@@ -217,9 +259,14 @@ struct RIL_Peer_Agent
   int a_old;
 
   /**
-   * Eligibility trace vector
+   * Eligibility traces
+   */
+  double ** E;
+
+  /**
+   * Whether to reset the eligibility traces to 0 after a Q-exploration step
    */
-  double * e;
+  int eligibility_reset;
 
   /**
    * Address in use
@@ -255,9 +302,19 @@ struct RIL_Peer_Agent
    * The address which has to be issued
    */
   struct ATS_Address * suggestion_address;
+
+  /**
+   * The agent's last objective value
+   */
+  double objective_old;
+
+  /**
+   * NOP bonus
+   */
+  double nop_bonus;
 };
 
-struct RIL_Network
+struct RIL_Scope
 {
   /**
    * ATS network type
@@ -274,15 +331,35 @@ struct RIL_Network
    */
   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;
+
+  /**
+   * The social welfare achieved in the scope
+   */
+  double social_welfare;
 };
 
 /**
@@ -325,11 +402,6 @@ struct GAS_RIL_Handle
    */
   double global_discount_integrated;
 
-  /**
-   * State vector for networks for the current step
-   */
-  double *global_state_networks;
-
   /**
    * Lock for bulk operations
    */
@@ -348,7 +420,7 @@ struct GAS_RIL_Handle
   /**
    * Array of networks with global assignment state
    */
-  struct RIL_Network * network_entries;
+  struct RIL_Scope * network_entries;
 
   /**
    * Networks count
@@ -373,13 +445,10 @@ struct GAS_RIL_Handle
 };
 
 /*
- *  Private functions
+ *  "Private" functions
  *  ---------------------------
  */
 
-static int
-ril_count_agents(struct GAS_RIL_Handle * solver);
-
 /**
  * Estimate the current action-value for state s and action a
  *
@@ -389,7 +458,7 @@ ril_count_agents(struct GAS_RIL_Handle * solver);
  * @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;
@@ -401,43 +470,23 @@ agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
 
   GNUNET_assert(!isnan(result));
 
+  //prevent crash when learning diverges
   if (isinf(result))
   {
-    return isinf(result) * UINT32_MAX; //TODO! fix
+    return isinf(result) * UINT32_MAX;
   }
-
   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
+ * Get the index of the address in the agent's list.
  *
- * @param agent agent performing the step
- * @return yes, if exploring
+ * @param agent agent handle
+ * @param address address handle
+ * @return the index, starting with zero
  */
 static int
-agent_decide_exploration (struct RIL_Peer_Agent *agent)
-{
-  //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
-  double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
-      UINT32_MAX) / (double) UINT32_MAX;
-
-  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
-   */
-static int
 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
 {
   int i;
@@ -448,14 +497,12 @@ agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *addre
   {
     i++;
     if (cur->address_naked == address)
-    {
       return i;
-    }
   }
-
   return i;
 }
 
+
 /**
  * Gets the wrapped address from the agent's list
  *
@@ -464,21 +511,77 @@ 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;
 
   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
-  {
     if (cur->address_naked == address)
-    {
       return cur;
+  return NULL;
+}
+
+
+static int
+agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
+{
+  int address_index;
+
+  switch (action)
+  {
+  case RIL_ACTION_NOTHING:
+    return GNUNET_YES;
+    break;
+  case RIL_ACTION_BW_IN_INC:
+  case RIL_ACTION_BW_IN_DBL:
+    if (agent->bw_in >= RIL_MAX_BW)
+      return GNUNET_NO;
+    else
+      return GNUNET_YES;
+    break;
+  case RIL_ACTION_BW_IN_DEC:
+  case RIL_ACTION_BW_IN_HLV:
+    if (agent->bw_in <= RIL_MIN_BW)
+      return GNUNET_NO;
+    else
+      return GNUNET_YES;
+    break;
+  case RIL_ACTION_BW_OUT_INC:
+  case RIL_ACTION_BW_OUT_DBL:
+    if (agent->bw_out >= RIL_MAX_BW)
+      return GNUNET_NO;
+    else
+      return GNUNET_YES;
+    break;
+  case RIL_ACTION_BW_OUT_DEC:
+  case RIL_ACTION_BW_OUT_HLV:
+    if (agent->bw_out <= RIL_MIN_BW)
+      return GNUNET_NO;
+    else
+      return GNUNET_YES;
+    break;
+  default:
+    if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
+    {
+      address_index = action - RIL_ACTION_TYPE_NUM;
+
+      GNUNET_assert(address_index >= 0);
+      GNUNET_assert(
+          address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
+
+      if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
+          agent->address_inuse->active)
+        return GNUNET_NO;
+      else
+        return GNUNET_YES;
+      break;
     }
+    // error - action does not exist
+    GNUNET_assert(GNUNET_NO);
   }
-
-  return NULL ;
 }
 
+
 /**
  * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
  * most reward in the future)
@@ -488,7 +591,7 @@ agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
  * @return the action promising most future reward
  */
 static int
-agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
+agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
 {
   int i;
   int max_i = RIL_ACTION_INVALID;
@@ -497,11 +600,14 @@ agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
 
   for (i = 0; i < agent->n; i++)
   {
-    cur_q = agent_estimate_q (agent, state, i);
-    if (cur_q > max_q)
+    if (agent_action_is_possible(agent, i))
     {
-      max_q = cur_q;
-      max_i = i;
+      cur_q = agent_q (agent, state, i);
+      if (cur_q > max_q)
+      {
+        max_q = cur_q;
+        max_i = i;
+      }
     }
   }
 
@@ -511,19 +617,49 @@ agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
 }
 
 /**
- * Gets any action, to explore the action space from that state
+ * Chooses a random action from the set of possible ones
  *
- * @param agent agent performing the calculation
- * @param state the state from which to take the action
- * @return any action
+ * @param agent the agent performing the action
+ * @return the action index
  */
 static int
-agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
+agent_get_action_random (struct RIL_Peer_Agent *agent)
 {
-  // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
-  return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
+  int i;
+  int is_possible[agent->n];
+  int sum = 0;
+  int r;
+
+  for (i = 0; i<agent->n; i++)
+  {
+    if (agent_action_is_possible(agent, i))
+    {
+      is_possible[i] = GNUNET_YES;
+      sum++;
+    }
+    else
+    {
+      is_possible[i] = GNUNET_NO;
+    }
+  }
+
+  r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
+
+  sum = -1;
+  for (i = 0; i<agent->n; i++)
+  {
+    if (is_possible[i])
+    {
+      sum++;
+      if (sum == r)
+        return i;
+    }
+  }
+
+  GNUNET_assert(GNUNET_NO);
 }
 
+
 /**
  * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
  *
@@ -533,83 +669,104 @@ agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
  * @param a_prime the new
  */
 static void
-agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
+agent_update (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
-
-  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);
+  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
 
-  for (i = 0; i < agent->m; i++)
+//  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_q (agent, agent->s_old, agent->a_old),
+//      agent->envi->parameters.alpha,
+//      reward,
+//      agent->envi->global_discount_variable,
+//      agent_q (agent, s_next, a_prime),
+//      delta);
+
+  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[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];
+    }
   }
 }
 
+
 /**
  * Changes the eligibility trace vector e in various manners:
- * #RIL_E_ACCUMULATE - adds @a f to each component as in accumulating eligibility traces
- * #RIL_E_REPLACE - resets each component to @f  as in replacing traces
- * #RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
+ * #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_DISCOUNT - 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 f how much to change
+ * @param feature the feature vector
  */
 static void
-agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod, double *f)
+agent_modify_eligibility (struct RIL_Peer_Agent *agent,
+                          enum RIL_E_Modification mod,
+                          double *feature,
+                          int action)
 {
   int i;
-  double *e = agent->e;
+  int k;
 
   for (i = 0; i < agent->m; i++)
   {
     switch (mod)
     {
     case RIL_E_ACCUMULATE:
-      e[i] += f[i];
+      agent->E[action][i] += feature[i];
       break;
     case RIL_E_REPLACE:
-      e[i] = f[i];
+      agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
       break;
-    case RIL_E_SET:
-      e[i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
+    case RIL_E_DISCOUNT:
+      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:
-      e[i] = 0;
+      for (k = 0; k < agent->n; k++)
+      {
+        agent->E[k][i] = 0;
+      }
       break;
     }
   }
 }
 
-
+/**
+ * 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);
 }
 
+
 /**
  * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
  * notify ATS of its new decision
@@ -699,52 +856,6 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
   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
@@ -755,198 +866,168 @@ ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network
 static double *
 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 {
+  double *state;
+  double y[2];
+  double x[2];
+  double d[2];
+  double sigma;
+  double f;
+  int m;
   int i;
-//  int k;
-  double *state = GNUNET_malloc (sizeof (double) * agent->m);
-  struct RIL_Address_Wrapped *cur_address;
-//  const double *preferences;
-//  const double *properties;
-  struct RIL_Network *net;
+  int k;
+  unsigned long long max_bw;
+
+  state = GNUNET_malloc (sizeof(double) * agent->m);
+
+  max_bw = RIL_MAX_BW;
+
+  y[0] = (double) agent->bw_out;
+  y[1] = (double) agent->bw_in;
 
-  //copy global networks state
-  for (i = 0; i < solver->networks_count * RIL_FEATURES_NETWORK_COUNT; i++)
+  m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
+  for (i = 0; i <= solver->parameters.rbf_divisor; i++)
   {
-//    state[i] = solver->global_state_networks[i];
+    for (k = 0; k <= solver->parameters.rbf_divisor; k++)
+    {
+      x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
+      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 + 1)) * 0.5);
+      f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
+      state[m++] = f;
+    }
   }
-  net = agent->address_inuse->solver_information;
 
-  state[0] = (double) net->bw_in_assigned / 1024; //(double) net->bw_in_available;
-  if (net->bw_in_assigned > net->bw_in_available)
+  return state;
+}
+
+/**
+ * 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++)
   {
-    state[1] = 1;// net->bw_in_available;
+    if (ntohl (address->atsi[c1].type) == type)
+      return ntohl (address->atsi[c1].value);
   }
-  else
+  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)
+{
+  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));
+
+//  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)
+{
+  struct RIL_Peer_Agent *cur;
+  double result;
+
+  switch (solver->parameters.social_welfare)
   {
-    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]);
+  case RIL_WELFARE_EGALITARIAN:
+    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))
+      {
+        result = GNUNET_MIN(result, agent_get_utility(cur));
+      }
+    }
+    return result;
 
-  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]);
+  case RIL_WELFARE_NASH:
+    result = 0;
+    for (cur = solver->agents_head; NULL != cur; cur = cur->next)
+    {
+      if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
+      {
+        result *= pow(agent_get_utility(cur), 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;
 
-  //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];
-//  }
+  net = agent->address_inuse->solver_information;
 
-  //get address specific features
-  for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
+  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)
   {
-//    //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];
-//    }
+    over_out = net->bw_out_utilized - net->bw_out_available;
+    if (RIL_ACTION_BW_OUT_INC == agent->a_old)
+    {
+      over_out *= 2;
+    }
   }
+  over_max = GNUNET_MAX (over_in , over_out) / RIL_MIN_BW;
 
-  return state;
+  return -1.0 * (double) over_max;
 }
 
-///**
-// * 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 for the last performed step, which is calculated in equal
  * parts from the local (the peer specific) and the global (for all peers
@@ -959,45 +1040,36 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 static double
 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 {
-  struct RIL_Network *net;
-//  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;
+  struct RIL_Scope *net;
+  double objective;
+  double delta;
+  double steady;
+  double pen_share;
+  double penalty;
+  double reward;
 
-  //punish overutilization
   net = agent->address_inuse->solver_information;
 
-  if (net->bw_in_assigned > net->bw_in_available)
+  objective = (agent_get_utility (agent) + net->social_welfare) / 2;
+  delta = objective - agent->objective_old;
+  agent->objective_old = objective;
+
+  if (delta != 0)
   {
-    overutilized_in = (net->bw_in_assigned - net->bw_in_available);
-    assigned_in = net->bw_in_available;
+    agent->nop_bonus = delta * RIL_NOP_DECAY;
   }
   else
   {
-    assigned_in = net->bw_in_assigned;
+    agent->nop_bonus *= RIL_NOP_DECAY;
   }
-//  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;
+  steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
 
-  return (double) (assigned_in - overutilized_in) / 1024;
+  pen_share = 0.5;
+  penalty = envi_get_penalty(solver, agent);
 
-//  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;
+  reward = delta + steady;
+  return ((1 - pen_share) * reward) + (pen_share * penalty);
 }
 
 /**
@@ -1017,16 +1089,16 @@ envi_action_bw_double (struct GAS_RIL_Handle *solver,
   if (direction_in)
   {
     new_bw = agent->bw_in * 2;
-    if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
-      new_bw = GNUNET_ATS_MaxBandwidth;
+    if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
+      new_bw = RIL_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 > GNUNET_ATS_MaxBandwidth)
-      new_bw = GNUNET_ATS_MaxBandwidth;
+    if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
+      new_bw = RIL_MAX_BW;
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
         new_bw, GNUNET_NO);
   }
@@ -1051,16 +1123,16 @@ envi_action_bw_halven (struct GAS_RIL_Handle *solver,
   if (direction_in)
   {
     new_bw = agent->bw_in / 2;
-    if (new_bw < MIN_BW || new_bw > agent->bw_in)
-      new_bw = MIN_BW;
+    if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
+      new_bw = RIL_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 > agent->bw_out)
-      new_bw = MIN_BW;
+    if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
+      new_bw = RIL_MIN_BW;
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
         GNUNET_NO);
   }
@@ -1081,17 +1153,17 @@ envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
 
   if (direction_in)
   {
-    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;
+    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;
     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;
+    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;
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
         new_bw, GNUNET_NO);
   }
@@ -1113,17 +1185,17 @@ 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 * MIN_BW);
-    if (new_bw < MIN_BW || new_bw > agent->bw_in)
-      new_bw = MIN_BW;
+    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;
     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 < MIN_BW || new_bw > agent->bw_out)
-      new_bw = MIN_BW;
+    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;
     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
         GNUNET_NO);
   }
@@ -1144,6 +1216,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)
@@ -1217,6 +1291,121 @@ 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)
+{
+  int action;
+  double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+        UINT32_MAX) / (double) UINT32_MAX;
+
+  if (r < agent->envi->parameters.explore_ratio) //explore
+  {
+    action = agent_get_action_random(agent);
+    if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
+    {
+      agent->eligibility_reset = GNUNET_YES;
+    }
+    return action;
+  }
+  else //exploit
+  {
+    action = agent_get_action_max(agent, state);
+    return action;
+  }
+}
+
+/**
+ * Selects the next action with a probability corresponding to its value. The
+ * probability is calculated using a Boltzmann distribution with a temperature
+ * value. The higher the temperature, the more are the action selection
+ * probabilities the same. With a temperature of 0, the selection is greedy,
+ * i.e. always the action with the highest value is chosen.
+ * @param agent
+ * @param state
+ * @return
+ */
+static int
+agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
+{
+  int i;
+  int a_max;
+  double eqt[agent->n];
+  double p[agent->n];
+  double sum = 0;
+  double r;
+
+  a_max = agent_get_action_max(agent, state);
+
+  for (i=0; i<agent->n; i++)
+  {
+    if (agent_action_is_possible(agent, i))
+    {
+      eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
+      sum += eqt[i];
+    }
+  }
+  for (i=0; i<agent->n; i++)
+  {
+    if (agent_action_is_possible(agent, i))
+    {
+      p[i] = eqt[i]/sum;
+    }
+    else
+    {
+      p[i] = 0;
+    }
+  }
+  r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+      UINT32_MAX) / (double) UINT32_MAX;
+  sum = 0;
+  for (i=0; i<agent->n; i++)
+  {
+    if (sum + p[i] > r)
+    {
+      if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
+      {
+        if (i != a_max)
+          agent->eligibility_reset = GNUNET_YES;
+      }
+      return i;
+    }
+    sum += p[i];
+  }
+  GNUNET_assert(GNUNET_NO);
+}
+
+/**
+ * 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)
+{
+  if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
+  {
+    return agent_select_egreedy(agent, state);
+  }
+  else
+  {
+    return agent_select_softmax(agent, state);
+  }
+}
+
 /**
  * Performs one step of the Markov Decision Process. Other than in the literature the step starts
  * after having done the last action a_old. It observes the new state s_next and the reward
@@ -1229,7 +1418,7 @@ static void
 agent_step (struct RIL_Peer_Agent *agent)
 {
   int a_next = RIL_ACTION_INVALID;
-  int explore;
+  int a_max;
   double *s_next;
   double reward;
 
@@ -1239,51 +1428,45 @@ agent_step (struct RIL_Peer_Agent *agent)
 
   s_next = envi_get_state (agent->envi, agent);
   reward = envi_get_reward (agent->envi, agent);
-  explore = agent_decide_exploration (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_DISCOUNT, 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:
-    if (explore)
-    {
-      a_next = agent_get_action_explore (agent, s_next);
-    }
-    else
-    {
-      a_next = agent_get_action_best (agent, s_next);
-    }
+    a_next = agent_select_action (agent, s_next);
     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_modify_eligibility (agent, RIL_E_SET, s_next);
+      agent_update (agent, reward, s_next, a_next);
     }
     break;
 
   case RIL_ALGO_Q:
-    a_next = agent_get_action_best (agent, s_next);
+    a_max = agent_get_action_max (agent, s_next);
     if (RIL_ACTION_INVALID != agent->a_old)
     {
       //updates weights with best action, disregarding actually selected action (off-policy), if not first step
-      agent_update_weights (agent, reward, s_next, a_next);
-    }
-    if (explore)
-    {
-      a_next = agent_get_action_explore (agent, s_next);
-      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, s_next);
+      agent_update (agent, reward, s_next, a_max);
     }
+    a_next = agent_select_action (agent, s_next);
     break;
   }
 
   GNUNET_assert(RIL_ACTION_INVALID != a_next);
 
-  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,
@@ -1300,6 +1483,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);
 
@@ -1319,11 +1507,19 @@ 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)
 {
   int i;
-  struct RIL_Network net;
+  struct RIL_Scope net;
   unsigned long long sum_assigned = 0;
   unsigned long long sum_available = 0;
   double ratio;
@@ -1333,8 +1529,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;
     }
@@ -1348,7 +1544,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
 }
 
 /**
@@ -1358,7 +1554,7 @@ ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
  * @param type the network type
  * @return the network struct
  */
-static struct RIL_Network *
+static struct RIL_Scope *
 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
 {
   int i;
@@ -1373,10 +1569,19 @@ 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)
 {
-  struct RIL_Network *net;
+  struct RIL_Scope *net;
   struct RIL_Peer_Agent *agent;
   unsigned long long address_count = 0;
 
@@ -1393,14 +1598,24 @@ ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_
   }
 
   net = ril_get_network (solver, network);
-  return (net->bw_in_available > MIN_BW * address_count) && (net->bw_out_available > MIN_BW * address_count);
+  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_Network *net;
+  struct RIL_Scope *net;
 
   for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
   {
@@ -1408,13 +1623,19 @@ 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, MIN_BW, MIN_BW, silent);
+        envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, agent->bw_in, agent->bw_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)
 {
@@ -1422,7 +1643,7 @@ ril_calculate_discount (struct GAS_RIL_Handle *solver)
   struct GNUNET_TIME_Relative time_delta;
   double tau;
 
-  // MDP case - remove when debugged
+  // MDP case - TODO! remove when debugged and test SMDP case
   if (solver->simulate)
   {
     solver->global_discount_variable = solver->parameters.gamma;
@@ -1440,22 +1661,125 @@ ril_calculate_discount (struct GAS_RIL_Handle *solver)
       / (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)
+  solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
+  solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
       / (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)
+{
+  int c = 0;
+  struct RIL_Peer_Agent *cur_agent;
+
+  for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
+  {
+    if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
+    {
+      c++;
+    }
+  }
+  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_calculate_assigned_bwnet (struct GAS_RIL_Handle *solver)
+ril_networks_update_state (struct GAS_RIL_Handle *solver)
 {
   int c;
-  struct RIL_Network *net;
+  struct RIL_Scope *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_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->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
+    net->agent_count = ril_network_count_active_agents(solver, net);
+    net->social_welfare = ril_network_get_social_welfare(solver, net);
   }
 }
 
@@ -1486,11 +1810,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;
@@ -1505,6 +1835,7 @@ ril_step_schedule_next (struct GAS_RIL_Handle *solver)
 
 /**
  * Triggers one step per agent
+ *
  * @param solver
  */
 static void
@@ -1528,10 +1859,7 @@ ril_step (struct GAS_RIL_Handle *solver)
   }
 
   ril_calculate_discount (solver);
-  ril_calculate_assigned_bwnet (solver);
-
-  //calculate network state vector
-//  envi_state_networks(solver);
+  ril_networks_update_state (solver);
 
   //trigger one step per active, unblocked agent
   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
@@ -1549,7 +1877,7 @@ ril_step (struct GAS_RIL_Handle *solver)
     }
   }
 
-  ril_calculate_assigned_bwnet (solver);
+  ril_networks_update_state (solver);
 
   solver->step_count += 1;
   ril_step_schedule_next (solver);
@@ -1567,44 +1895,22 @@ ril_step (struct GAS_RIL_Handle *solver)
   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;
-}
-
+/**
+ * Initializes the matrix W of parameter vectors theta with small random numbers.
+ *
+ * @param agent The respective agent
+ */
 static void
-agent_w_start (struct RIL_Peer_Agent *agent)
+agent_w_init (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]));
+      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));
     }
   }
 }
@@ -1621,27 +1927,31 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
 {
   int i;
   struct GAS_RIL_Handle * solver = s;
-  struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
+  struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
 
   agent->envi = solver;
   agent->peer = *peer;
   agent->step_count = 0;
   agent->is_active = GNUNET_NO;
-  agent->bw_in = MIN_BW;
-  agent->bw_out = MIN_BW;
+  agent->bw_in = RIL_MIN_BW;
+  agent->bw_out = RIL_MIN_BW;
   agent->suggestion_issue = GNUNET_NO;
   agent->n = RIL_ACTION_TYPE_NUM;
-  agent->m = (RIL_FEATURES_NETWORK_COUNT);// + GNUNET_ATS_PreferenceCount;
+  agent->m = 0;
   agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
+  agent->E = (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->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
   }
-  agent_w_start(agent);
+  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->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
-  agent_modify_eligibility (agent, RIL_E_ZERO, NULL);
+  agent->address_inuse = NULL;
+  agent->objective_old = 0;
+  agent->nop_bonus = 0;
 
   return agent;
 }
@@ -1659,11 +1969,12 @@ agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 
   for (i = 0; i < agent->n; i++)
   {
-    GNUNET_free(agent->W[i]);
+    GNUNET_free_non_null(agent->W[i]);
+    GNUNET_free_non_null(agent->E[i]);
   }
-  GNUNET_free(agent->W);
-  GNUNET_free(agent->e);
-  GNUNET_free(agent->s_old);
+  GNUNET_free_non_null(agent->W);
+  GNUNET_free_non_null(agent->E);
+  GNUNET_free_non_null(agent->s_old);
   GNUNET_free(agent);
 }
 
@@ -1708,10 +2019,10 @@ ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *
 static int
 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
 {
-  struct RIL_Network *net;
+  struct RIL_Scope *net;
 
   net = ril_get_network (solver, network);
-  return net->bw_out_available >= MIN_BW;
+  return net->bw_out_available >= RIL_MIN_BW;
 }
 
 /**
@@ -1739,7 +2050,7 @@ ril_cut_from_vector (void **old,
   unsigned int bytes_hole;
   unsigned int bytes_after;
 
-  GNUNET_assert(old_length > hole_length);
+  GNUNET_assert(old_length >= hole_length);
   GNUNET_assert(old_length >= (hole_start + hole_length));
 
   size = element_size * (old_length - hole_length);
@@ -1801,7 +2112,7 @@ 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 RIL_Network * cur;
+  struct RIL_Scope * cur;
   int c;
   char *string;
 
@@ -1814,6 +2125,10 @@ libgnunet_plugin_ats_ril_init (void *cls)
   GNUNET_assert(NULL != env->get_preferences);
   GNUNET_assert(NULL != env->get_property);
 
+  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
+  {
+    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))
@@ -1835,10 +2150,24 @@ 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;
+    GNUNET_free (string);
+  }
+  else
+  {
+    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);
     GNUNET_free (string);
+    if (!(solver->parameters.beta > 0))
+    {
+      LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_DISCOUNT_BETA not configured as positive number. Set to default value of %f instead.\n", RIL_DEFAULT_DISCOUNT_BETA);
+      solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
+    }
   }
   else
   {
@@ -1848,6 +2177,11 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.gamma = strtod (string, NULL);
     GNUNET_free (string);
+    if (!(solver->parameters.gamma < 1) || (solver->parameters.gamma < 0))
+    {
+      LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_DISCOUNT_GAMMA not configured in range [0,1[. Set to default value of %f instead.\n", RIL_DEFAULT_DISCOUNT_GAMMA);
+      solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
+    }
   }
   else
   {
@@ -1858,6 +2192,11 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.alpha = strtod (string, NULL);
     GNUNET_free (string);
+    if (!(solver->parameters.alpha > 0) || solver->parameters.alpha > 1)
+    {
+      LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_GRADIENT_STEP_SIZE not configured in range ]0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_GRADIENT_STEP_SIZE);
+      solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
+    }
   }
   else
   {
@@ -1867,6 +2206,11 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.lambda = strtod (string, NULL);
     GNUNET_free (string);
+    if (solver->parameters.lambda < 0 || solver->parameters.lambda > 1)
+    {
+      LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TRACE_DECAY not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_TRACE_DECAY);
+      solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
+    }
   }
   else
   {
@@ -1876,6 +2220,11 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.explore_ratio = strtod (string, NULL);
     GNUNET_free (string);
+    if (solver->parameters.explore_ratio < 0 || solver->parameters.explore_ratio > 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;
+    }
   }
   else
   {
@@ -1885,15 +2234,51 @@ libgnunet_plugin_ats_ril_init (void *cls)
   {
     solver->parameters.reward_global_share = strtod (string, NULL);
     GNUNET_free (string);
+    if (solver->parameters.reward_global_share < 0 || solver->parameters.reward_global_share > 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;
+    }
   }
   else
   {
     solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
   }
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE", &string))
+  {
+    solver->parameters.temperature = strtod (string, NULL);
+    GNUNET_free (string);
+    if (solver->parameters.temperature <= 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;
+    }
+  }
+  else
+  {
+    solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
+  }
   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;
+  }
+  else
+  {
+    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;
+    GNUNET_free (string);
+  }
+  else
+  {
+    solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
+  }
 
   env->sf.s_add = &GAS_ril_address_add;
   env->sf.s_address_update_property = &GAS_ril_address_property_changed;
@@ -1910,9 +2295,8 @@ libgnunet_plugin_ats_ril_init (void *cls)
 
   solver->plugin_envi = env;
   solver->networks_count = env->network_count;
-  solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
+  solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
   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++)
@@ -1921,18 +2305,22 @@ libgnunet_plugin_ats_ril_init (void *cls)
     cur->type = env->networks[c];
     cur->bw_in_available = env->in_quota[c];
     cur->bw_out_available = env->out_quota[c];
-    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);
+    LOG(GNUNET_ERROR_TYPE_DEBUG, "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);
   }
 
-  LOG(GNUNET_ERROR_TYPE_INFO, "init()  Parameters:\n");
-  LOG(GNUNET_ERROR_TYPE_INFO, "init()  Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  Parameters:\n");
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "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",
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  exploration_ratio = %f, temperature = %f, ActionSelection = %s, global_share = %f\n",
       solver->parameters.explore_ratio,
+      solver->parameters.temperature,
+      solver->parameters.select ? "EGREEDY" : "SOFTMAX",
       solver->parameters.reward_global_share);
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  RBF_DIVISOR = %llu\n",
+      solver->parameters.rbf_divisor);
 
   return solver;
 }
@@ -1967,7 +2355,6 @@ libgnunet_plugin_ats_ril_done (void *cls)
     GNUNET_SCHEDULER_cancel (s->step_next_task_id);
   }
   GNUNET_free(s->network_entries);
-  GNUNET_free(s->global_state_networks);
   GNUNET_free(s);
 
   return NULL;
@@ -1988,7 +2375,7 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
   struct RIL_Address_Wrapped *address_wrapped;
-  struct RIL_Network *net;
+  struct RIL_Scope *net;
   unsigned int m_new;
   unsigned int m_old;
   unsigned int n_new;
@@ -2012,28 +2399,34 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
   agent = ril_get_agent (s, &address->peer, GNUNET_YES);
 
   //add address
-  address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
+  address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
   address_wrapped->address_naked = address;
   GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
 
   //increase size of W
-  m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
+  m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
   m_old = agent->m;
   n_new = agent->n + 1;
   n_old = agent->n;
 
   GNUNET_array_grow(agent->W, agent->n, n_new);
+  agent->n = n_old;
+  GNUNET_array_grow(agent->E, agent->n, n_new);
   for (i = 0; i < n_new; i++)
   {
     if (i < n_old)
     {
       agent->m = m_old;
       GNUNET_array_grow(agent->W[i], agent->m, m_new);
+      agent->m = m_old;
+      GNUNET_array_grow(agent->E[i], agent->m, m_new);
     }
     else
     {
       zero = 0;
       GNUNET_array_grow(agent->W[i], zero, m_new);
+      zero = 0;
+      GNUNET_array_grow(agent->E[i], zero, m_new);
     }
   }
 
@@ -2041,9 +2434,6 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
   agent->m = m_old;
   GNUNET_array_grow(agent->s_old, agent->m, m_new);
 
-  agent->m = m_old;
-  GNUNET_array_grow(agent->e, agent->m, m_new);
-
   ril_try_unblock_agent(s, agent, GNUNET_NO);
 
   ril_step (s);
@@ -2073,7 +2463,7 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
   unsigned int m_new;
   unsigned int n_new;
   int i;
-  struct RIL_Network *net;
+  struct RIL_Scope *net;
 
   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,
@@ -2090,12 +2480,11 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
   }
 
   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;
@@ -2105,22 +2494,24 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
   GNUNET_free(address_wrapped);
 
   //decrease W
-  m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
+  m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
   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)
-        ((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]);
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "second\n");
+        address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
+        ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
+    ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
+        address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
+        ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
+  }
+  GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
+  GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
   ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
       1, agent->n);
+  ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
+      1, agent->n);
   //correct last action
   if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
   {
@@ -2130,16 +2521,10 @@ 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");
+  //decrease old state vector
   ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
-      //((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)
-      ((RIL_FEATURES_NETWORK_COUNT) //TODO! replace when adding more networks
-          + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
+      address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
+      ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
   agent->m = m_new;
   agent->n = n_new;
 
@@ -2147,8 +2532,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, MIN_BW, MIN_BW,
-          GNUNET_NO);
+      envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, agent->bw_in, agent->bw_out,
+          GNUNET_YES);
     }
     else
     {
@@ -2199,9 +2584,6 @@ GAS_ril_address_session_changed (void *solver,
     uint32_t cur_session,
     uint32_t new_session)
 {
-  /*
-   * TODO? Future Work: Potentially add session activity as a feature in state vector
-   */
   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
 }
 
@@ -2218,9 +2600,6 @@ GAS_ril_address_session_changed (void *solver,
 void
 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
 {
-  /*
-   * 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");
@@ -2373,7 +2752,6 @@ GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *p
         "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
         GNUNET_i2s (peer));
   }
-
   return agent->address_inuse;
 }