switching transport tests to new MQ-based transport API
[oweals/gnunet.git] / src / ats / plugin_ats_ril.c
old mode 100755 (executable)
new mode 100644 (file)
index 944e4a5..eac62c4
@@ -1,6 +1,6 @@
 /*
  This file is part of GNUnet.
- (C) 2011 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2011-2014 GNUnet e.V.
 
  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
  */
 
 /**
  * @author Fabian Oehlmann
  * @author Matthias Wachs
  */
-#include "plugin_ats_ril.h"
+#include "platform.h"
+#include <float.h>
+#include <math.h>
+#include "gnunet_ats_plugin.h"
+#include "gnunet-service-ats_addresses.h"
+
+
 
 #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                      1024 * 1000//GNUNET_ATS_MaxBandwidth TODO! revert
+#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           100
-
-#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_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
@@ -67,7 +74,7 @@
 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,
@@ -98,7 +105,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 +146,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 +176,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
@@ -199,19 +221,9 @@ struct RIL_Address_Wrapped
    * The address
    */
   struct ATS_Address *address_naked;
-
-  /**
-   * Last inbound bandwidth used
-   */
-  unsigned long long bw_in;
-
-  /**
-   * Last outbound bandwidth used
-   */
-  unsigned long long bw_out;
-
 };
 
+
 struct RIL_Peer_Agent
 {
   /**
@@ -262,7 +274,7 @@ struct RIL_Peer_Agent
   /**
    * Last perceived state feature vector
    */
-  double * s_old;
+  double *s_old;
 
   /**
    * Last chosen action
@@ -282,27 +294,27 @@ struct RIL_Peer_Agent
   /**
    * Address in use
    */
-  struct ATS_Address * address_inuse;
+  struct ATS_Address *address_inuse;
 
   /**
    * Head of addresses DLL
    */
-  struct RIL_Address_Wrapped * addresses_head;
+  struct RIL_Address_Wrapped *addresses_head;
 
   /**
    * Tail of addresses DLL
    */
-  struct RIL_Address_Wrapped * addresses_tail;
+  struct RIL_Address_Wrapped *addresses_tail;
 
   /**
    * Inbound bandwidth assigned by the agent
    */
-  unsigned long long bw_in;
+  uint32_t bw_in;
 
   /**
    * Outbound bandwidth assigned by the agent
    */
-  unsigned long long bw_out;
+  uint32_t bw_out;
 
   /**
    * Flag whether a suggestion has to be issued
@@ -312,7 +324,7 @@ struct RIL_Peer_Agent
   /**
    * The address which has to be issued
    */
-  struct ATS_Address * suggestion_address;
+  struct ATS_Address *suggestion_address;
 
   /**
    * The agent's last objective value
@@ -335,22 +347,22 @@ struct RIL_Scope
   /**
    * Total available inbound bandwidth
    */
-  unsigned long long bw_in_available;
+  uint32_t bw_in_available;
 
   /**
    * Bandwidth inbound assigned in network after last step
    */
-  unsigned long long bw_in_assigned;
+  uint32_t bw_in_assigned;
 
   /**
    * Bandwidth inbound actually utilized in the network
    */
-  unsigned long long bw_in_utilized;
+  uint32_t bw_in_utilized;
 
   /**
    * Total available outbound bandwidth
    */
-  unsigned long long bw_out_available;
+  uint32_t bw_out_available;
 
   /**
    * Bandwidth outbound assigned in network after last step
@@ -365,7 +377,7 @@ struct RIL_Scope
   /**
    * Number of active agents in scope
    */
-  unsigned int agent_count;
+  unsigned int active_agent_count;
 
   /**
    * The social welfare achieved in the scope
@@ -381,12 +393,7 @@ struct GAS_RIL_Handle
   /**
    * The solver-plugin environment of the solver-plugin API
    */
-  struct GNUNET_ATS_PluginEnvironment *plugin_envi;
-
-  /**
-   * Statistics handle
-   */
-  struct GNUNET_STATISTICS_Handle *stats;
+  struct GNUNET_ATS_PluginEnvironment *env;
 
   /**
    * Number of performed steps
@@ -401,7 +408,7 @@ struct GAS_RIL_Handle
   /**
    * Task identifier of the next time-step to be executed
    */
-  GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
+  struct GNUNET_SCHEDULER_Task * step_next_task_id;
 
   /**
    * Variable discount factor, dependent on time between steps
@@ -469,23 +476,21 @@ struct GAS_RIL_Handle
  * @return estimation value
  */
 static double
-agent_q (struct RIL_Peer_Agent *agent, double *state, int action)
+agent_q (struct RIL_Peer_Agent *agent,
+         const double *state,
+         int action)
 {
-  int i;
-  double result = 0;
+  unsigned int i;
+  double result = 0.0;
 
   for (i = 0; i < agent->m; i++)
-  {
     result += state[i] * agent->W[action][i];
-  }
-
-  GNUNET_assert(!isnan(result));
 
-  //prevent crash when learning diverges
+  /* prevent crashes if learning diverges */
+  if (isnan(result))
+      return isnan(result) * UINT32_MAX;
   if (isinf(result))
-  {
     return isinf(result) * UINT32_MAX;
-  }
   return result;
 }
 
@@ -552,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;
@@ -566,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;
@@ -668,6 +673,7 @@ agent_get_action_random (struct RIL_Peer_Agent *agent)
   }
 
   GNUNET_assert(GNUNET_NO);
+  return RIL_ACTION_INVALID;
 }
 
 
@@ -683,8 +689,9 @@ static void
 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_q (agent, s_next, a_prime); //discounted future value
@@ -699,14 +706,17 @@ agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a
 //      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->E[agent->a_old][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];
+    }
   }
 }
 
@@ -715,12 +725,13 @@ agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a
  * 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_DISCOUNT - 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,
@@ -739,9 +750,9 @@ 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:
+    case RIL_E_DECAY:
       for (k = 0; k < agent->n; k++)
       {
         agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
@@ -769,10 +780,22 @@ ril_inform (struct GAS_RIL_Handle *solver,
             enum GAS_Solver_Operation op,
             enum GAS_Solver_Status stat)
 {
-  if (NULL != solver->plugin_envi->info_cb)
-    solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
+  solver->env->info_cb (solver->env->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
@@ -783,7 +806,7 @@ ril_inform (struct GAS_RIL_Handle *solver,
  * @param new_address the address which is to be used
  * @param new_bw_in the new amount of inbound bandwidth set for this address
  * @param new_bw_out the new amount of outbound bandwidth set for this address
- * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
+ * @param silent disables invocation of the bw_changed callback, if #GNUNET_YES
  */
 static void
 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
@@ -795,7 +818,9 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
 {
   int notify = GNUNET_NO;
 
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "    set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
+      "    set_active_suggestion() for peer '%s'\n",
+      GNUNET_i2s (&agent->peer));
 
   //address change
   if (agent->address_inuse != new_address)
@@ -803,15 +828,15 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
     if (NULL != agent->address_inuse)
     {
       agent->address_inuse->active = GNUNET_NO;
-      agent->address_inuse->assigned_bw_in.value__ = htonl (0);
-      agent->address_inuse->assigned_bw_out.value__ = htonl (0);
+      agent->address_inuse->assigned_bw_in = 0;
+      agent->address_inuse->assigned_bw_out = 0;
     }
     if (NULL != new_address)
     {
       LOG(GNUNET_ERROR_TYPE_DEBUG, "    set address active: %s\n", agent->is_active ? "yes" : "no");
       new_address->active = agent->is_active;
-      new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
-      new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
+      new_address->assigned_bw_in = agent->bw_in;
+      new_address->assigned_bw_out = agent->bw_out;
     }
     notify |= GNUNET_YES;
   }
@@ -829,13 +854,13 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
     if (agent->bw_in != new_bw_in)
     {
       agent->bw_in = new_bw_in;
-      new_address->assigned_bw_in.value__ = htonl (new_bw_in);
+      new_address->assigned_bw_in = new_bw_in;
       notify |= GNUNET_YES;
     }
     if (agent->bw_out != new_bw_out)
     {
       agent->bw_out = new_bw_out;
-      new_address->assigned_bw_out.value__ = htonl (new_bw_out);
+      new_address->assigned_bw_out = new_bw_out;
       notify |= GNUNET_YES;
     }
   }
@@ -850,9 +875,9 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
     }
     else if (agent->address_inuse)
     {
-      //disconnect case, no new address
-      GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
-      GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
+      /* disconnect case, no new address */
+      GNUNET_assert(0 ==  agent->address_inuse->assigned_bw_in);
+      GNUNET_assert(0 == agent->address_inuse->assigned_bw_out);
       agent->bw_in = 0;
       agent->bw_out = 0;
 
@@ -886,7 +911,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;
@@ -909,29 +934,6 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
   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++)
-  {
-    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
@@ -947,18 +949,15 @@ agent_get_utility (struct RIL_Peer_Agent *agent)
   double delay_norm;
   double pref_match;
 
-  preferences = agent->envi->plugin_envi->get_preferences (agent->envi->plugin_envi->get_preference_cls,
-      &agent->peer);
+  preferences = agent->envi->env->get_preferences (agent->envi->env->cls,
+                                                   &agent->peer);
 
-  delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
+  delay_atsi = agent->address_inuse->norm_delay.norm;
   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] * (double) (agent->bw_in/RIL_MIN_BW));
-  pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * (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));
+  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 pref_match;
 }
 
@@ -995,7 +994,7 @@ 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 *= pow(agent_get_utility(cur), 1.0 / (double) scope->agent_count);
+        result *= pow(agent_get_utility(cur), 1.0 / (double) scope->active_agent_count);
       }
     }
     return result;
@@ -1004,24 +1003,6 @@ ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope
   return 1;
 }
 
-static double
-envi_penalty_share (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
-{
-  struct RIL_Scope *net;
-  double util_ratio_in;
-  double util_ratio_out;
-  double util_ratio_max;
-  double sigmoid_x;
-
-  net = agent->address_inuse->solver_information;
-
-  util_ratio_in = (double) net->bw_in_utilized / (double) net->bw_in_available;
-  util_ratio_out = (double) net->bw_out_utilized / (double) net->bw_out_available;
-  util_ratio_max = GNUNET_MAX (util_ratio_in, util_ratio_out);
-  sigmoid_x = util_ratio_max - 1;
-  return 1 - (1 / (1 + exp(5 * sigmoid_x)));
-}
-
 static double
 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 {
@@ -1032,13 +1013,13 @@ envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 
   net = agent->address_inuse->solver_information;
 
-  //TODO make sure in tests to have utilization property updated
   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;
+      /* increase quadratically */
+      over_in *= over_in;
     }
   }
   if (net->bw_out_utilized > net->bw_out_available)
@@ -1046,10 +1027,11 @@ envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
     over_out = net->bw_out_utilized - net->bw_out_available;
     if (RIL_ACTION_BW_OUT_INC == agent->a_old)
     {
-      over_out *= 2;
+      /* increase quadratically */
+      over_out *= over_out;
     }
   }
-  over_max = GNUNET_MAX (over_in , over_out) / RIL_MIN_BW;
+  over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
 
   return -1.0 * (double) over_max;
 }
@@ -1070,28 +1052,29 @@ envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
   double objective;
   double delta;
   double steady;
-  double pen_share;
   double penalty;
   double reward;
 
   net = agent->address_inuse->solver_information;
 
+  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 = delta * RIL_NOP_DECAY;
+  }
+  else
   {
-    agent->nop_bonus = delta * 0.5;
+    agent->nop_bonus *= RIL_NOP_DECAY;
   }
 
   steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
 
-  pen_share = envi_penalty_share(solver, agent);
-  penalty = envi_get_penalty(solver, agent);
-
   reward = delta + steady;
-  return ((1 - pen_share) * reward) + (pen_share * penalty);
+  return reward + penalty;
 }
 
 /**
@@ -1107,20 +1090,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);
   }
@@ -1145,16 +1131,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);
   }
@@ -1172,20 +1158,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);
   }
@@ -1208,16 +1197,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);
   }
@@ -1238,15 +1227,13 @@ 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);
-  cur->bw_in = agent->bw_in;
-  cur->bw_out = agent->bw_out;
+  //cur = agent_address_get_wrapped(agent, agent->address_inuse);
 
   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
   {
     if (i == address_index)
     {
-      envi_set_active_suggestion (solver, agent, cur->address_naked, cur->bw_in, cur->bw_out,
+      envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
           GNUNET_NO);
       return;
     }
@@ -1333,13 +1320,14 @@ 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->eligibility_reset = GNUNET_YES;
     }
+    agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
     return action;
   }
   else //exploit
@@ -1376,6 +1364,8 @@ agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
     if (agent_action_is_possible(agent, i))
     {
       eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
+      if (isinf (eqt[i]))
+        eqt[i] = isinf(eqt[i]) * UINT32_MAX;
       sum += eqt[i];
     }
   }
@@ -1397,16 +1387,18 @@ 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)
+        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;
 }
 
 /**
@@ -1460,7 +1452,7 @@ agent_step (struct RIL_Peer_Agent *agent)
   }
   else
   {
-    agent_modify_eligibility (agent, RIL_E_DISCOUNT, NULL, -1);
+    agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
   }
   if (RIL_ACTION_INVALID != agent->a_old)
   {
@@ -1491,7 +1483,7 @@ agent_step (struct RIL_Peer_Agent *agent)
 
   GNUNET_assert(RIL_ACTION_INVALID != a_next);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step()  Step# %llu  R: %f  IN %llu  OUT %llu  A: %d\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "step()  Step# %llu  R: %f  IN %llu  OUT %llu  A: %d\n",
         agent->step_count,
         reward,
         agent->bw_in/1024,
@@ -1515,19 +1507,19 @@ agent_step (struct RIL_Peer_Agent *agent)
 static void
 ril_step (struct GAS_RIL_Handle *solver);
 
+
 /**
  * Task for the scheduler, which performs one step and lets the solver know that
  * no further step is scheduled.
  *
  * @param cls the solver handle
- * @param tc the task context for the scheduler
  */
 static void
-ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+ril_step_scheduler_task (void *cls)
 {
   struct GAS_RIL_Handle *solver = cls;
 
-  solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
+  solver->step_next_task_id = NULL;
   ril_step (solver);
 }
 
@@ -1553,8 +1545,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;
     }
@@ -1568,7 +1560,7 @@ ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
     ratio = 0;
   }
 
-  return ratio > 1 ? 1 : ratio; //overassignment is possible, cap at 1
+  return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
 }
 
 /**
@@ -1640,6 +1632,8 @@ ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *age
 {
   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)
   {
@@ -1647,7 +1641,11 @@ 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;
     }
   }
@@ -1667,7 +1665,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;
@@ -1773,9 +1771,9 @@ ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network
       if (net->type == type)
       {
         if (direction_in)
-          sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
+          sum += cur->address_inuse->norm_utilization_in.norm;
         else
-          sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
+          sum += cur->address_inuse->norm_utilization_out.norm;
       }
     }
   }
@@ -1802,7 +1800,7 @@ ril_networks_update_state (struct GAS_RIL_Handle *solver)
     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->active_agent_count = ril_network_count_active_agents(solver, net);
     net->social_welfare = ril_network_get_social_welfare(solver, net);
   }
 }
@@ -1839,18 +1837,18 @@ ril_step_schedule_next (struct GAS_RIL_Handle *solver)
 
   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);
+//  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;
   }
 
-  if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
+  if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done))
   {
     solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
           solver);
@@ -1903,7 +1901,7 @@ ril_step (struct GAS_RIL_Handle *solver)
 
   ril_networks_update_state (solver);
 
-  solver->step_count += 1;
+  solver->step_count++;
   ril_step_schedule_next (solver);
 
   ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
@@ -1912,7 +1910,8 @@ ril_step (struct GAS_RIL_Handle *solver)
   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
   {
     if (cur->suggestion_issue) {
-      solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
+      solver->env->bandwidth_changed_cb (solver->env->cls,
+                                         cur->suggestion_address);
       cur->suggestion_issue = GNUNET_NO;
     }
   }
@@ -2090,8 +2089,8 @@ ril_cut_from_vector (void **old,
   else
   {
     tmpptr = GNUNET_malloc (size);
-    memcpy (tmpptr, oldptr, bytes_before);
-    memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
+    GNUNET_memcpy (tmpptr, oldptr, bytes_before);
+    GNUNET_memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
   }
   if (NULL != *old)
   {
@@ -2113,567 +2112,255 @@ ril_cut_from_vector (void **old,
  * @param kind the kind to change the preference
  * @param pref_rel the normalized preference value for this kind over all clients
  */
-void
+static void
 GAS_ril_address_change_preference (void *solver,
-    const struct GNUNET_PeerIdentity *peer,
-    enum GNUNET_ATS_PreferenceKind kind,
-    double pref_rel)
+                                  const struct GNUNET_PeerIdentity *peer,
+                                  enum GNUNET_ATS_PreferenceKind kind,
+                                  double pref_rel)
 {
   LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
 
-  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);
 }
 
+
 /**
- * Entry point for the plugin
+ * Add a new address for a peer to the solver
  *
- * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
+ * The address is already contained in the addresses hashmap!
+ *
+ * @param solver the solver Handle
+ * @param address the address to add
+ * @param network network type of this address
  */
-void *
-libgnunet_plugin_ats_ril_init (void *cls)
+static void
+GAS_ril_address_add (void *solver,
+                    struct ATS_Address *address,
+                    uint32_t network)
 {
-  struct GNUNET_ATS_PluginEnvironment *env = cls;
-  struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
-  struct RIL_Scope * cur;
-  int c;
-  char *string;
+  struct GAS_RIL_Handle *s = solver;
+  struct RIL_Peer_Agent *agent;
+  struct RIL_Address_Wrapped *address_wrapped;
+  struct RIL_Scope *net;
+  unsigned int m_new;
+  unsigned int m_old;
+  unsigned int n_new;
+  unsigned int n_old;
+  int i;
+  unsigned int zero;
 
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "API_address_add()\n");
 
-  GNUNET_assert(NULL != env);
-  GNUNET_assert(NULL != env->cfg);
-  GNUNET_assert(NULL != env->stats);
-  GNUNET_assert(NULL != env->bandwidth_changed_cb);
-  GNUNET_assert(NULL != env->get_preferences);
-  GNUNET_assert(NULL != env->get_property);
+  net = ril_get_network (s, network);
+  address->solver_information = net;
 
-  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))
-  {
-    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))
+  if (!ril_network_is_active (s, network))
   {
-    solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
+    LOG(GNUNET_ERROR_TYPE_DEBUG,
+        "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
+        address->plugin, address->addr, GNUNET_i2s (&address->peer));
+    return;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
+
+  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
+  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 + ((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++)
   {
-    solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
-    GNUNET_free (string);
+    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);
+    }
   }
-  else
+
+  //increase size of old state vector
+  agent->m = m_old;
+  GNUNET_array_grow(agent->s_old, agent->m, m_new);
+
+  ril_try_unblock_agent(s, agent, GNUNET_NO);
+
+  ril_step (s);
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
+      address->active ? "active" : "inactive", address->plugin, address->addr,
+      GNUNET_i2s (&address->peer));
+}
+
+/**
+ * Delete an address in the solver
+ *
+ * The address is not contained in the address hashmap anymore!
+ *
+ * @param solver the solver handle
+ * @param address the address to remove
+ */
+static void
+GAS_ril_address_delete (void *solver,
+                       struct ATS_Address *address)
+{
+  struct GAS_RIL_Handle *s = solver;
+  struct RIL_Peer_Agent *agent;
+  struct RIL_Address_Wrapped *address_wrapped;
+  int address_index;
+  unsigned int m_new;
+  unsigned int n_new;
+  int i;
+  struct RIL_Scope *net;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "API_address_delete() Delete %s %s address %s for peer '%s'\n",
+       address->active ? "active" : "inactive",
+       address->plugin,
+       address->addr,
+       GNUNET_i2s (&address->peer));
+
+  agent = ril_get_agent (s, &address->peer, GNUNET_NO);
+  if (NULL == agent)
   {
-    solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
+    net = address->solver_information;
+    GNUNET_assert(! ril_network_is_active (s, net->type));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "No agent allocated for peer yet, since address was in inactive network\n");
+    return;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
+
+  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_wrapped (agent, address);
+
+  if (NULL == address_wrapped)
   {
-    solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
-    GNUNET_free (string);
+    net = address->solver_information;
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Address not considered by agent, address was in inactive network\n");
+    return;
   }
-  else
+  GNUNET_CONTAINER_DLL_remove (agent->addresses_head,
+                               agent->addresses_tail,
+                               address_wrapped);
+  GNUNET_free (address_wrapped);
+
+  //decrease W
+  m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
+  n_new = agent->n - 1;
+
+  for (i = 0; i < agent->n; i++)
   {
-    solver->parameters.select = RIL_DEFAULT_SELECT;
+    ril_cut_from_vector ((void **) &agent->W[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);
+    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);
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
+  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))
   {
-    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;
-    }
+    agent->a_old -= 1;
   }
-  else
+  else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
   {
-    solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
+    agent->a_old = RIL_ACTION_INVALID;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
+  //decrease old state vector
+  ril_cut_from_vector ((void **) &agent->s_old, 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);
+  agent->m = m_new;
+  agent->n = n_new;
+
+  if (agent->address_inuse == address)
   {
-    solver->parameters.gamma = strtod (string, NULL);
-    GNUNET_free (string);
-    if (!(solver->parameters.gamma < 1) || (solver->parameters.gamma < 0))
+    if (NULL != agent->addresses_head) //if peer has an address left, use it
     {
-      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;
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Active address died, suggesting alternative!\n");
+      envi_set_active_suggestion (s,
+                                  agent,
+                                  agent->addresses_head->address_naked,
+                                  agent->bw_in,
+                                  agent->bw_out,
+                                  GNUNET_YES);
     }
-  }
-  else
-  {
-    solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
-  }
-  if (GNUNET_OK
-      == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
-  {
-    solver->parameters.alpha = strtod (string, NULL);
-    GNUNET_free (string);
-    if (!(solver->parameters.alpha > 0) || solver->parameters.alpha > 1)
+    else
     {
-      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;
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Active address died, suggesting disconnect!\n");
+      envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
     }
   }
-  else
+  ril_step (solver);
+  if (agent->suggestion_address == address)
   {
-    solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
+    agent->suggestion_issue = GNUNET_NO;
+    agent->suggestion_address = NULL;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
-  {
-    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
-  {
-    solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
-  }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
-  {
-    solver->parameters.explore_ratio = strtod (string, NULL);
-    GNUNET_free (string);
-    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
-  {
-    solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
-  }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
-  {
-    solver->parameters.reward_global_share = strtod (string, NULL);
-    GNUNET_free (string);
-    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;
-  env->sf.s_address_update_session = &GAS_ril_address_session_changed;
-  env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
-  env->sf.s_address_update_network = &GAS_ril_address_change_network;
-  env->sf.s_get = &GAS_ril_get_preferred_address;
-  env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
-  env->sf.s_pref = &GAS_ril_address_change_preference;
-  env->sf.s_feedback = &GAS_ril_address_preference_feedback;
-  env->sf.s_del = &GAS_ril_address_delete;
-  env->sf.s_bulk_start = &GAS_ril_bulk_start;
-  env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
-
-  solver->plugin_envi = env;
-  solver->networks_count = env->network_count;
-  solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
-  solver->step_count = 0;
-  solver->done = GNUNET_NO;
-
-  for (c = 0; c < env->network_count; c++)
-  {
-    cur = &solver->network_entries[c];
-    cur->type = env->networks[c];
-    cur->bw_in_available = env->in_quota[c];
-    cur->bw_out_available = env->out_quota[c];
-    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_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_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;
-}
-
-/**
- * Exit point for the plugin
- *
- * @param cls the solver handle
- */
-void *
-libgnunet_plugin_ats_ril_done (void *cls)
-{
-  struct GAS_RIL_Handle *s = cls;
-  struct RIL_Peer_Agent *cur_agent;
-  struct RIL_Peer_Agent *next_agent;
-
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
-
-  s->done = GNUNET_YES;
-
-  cur_agent = s->agents_head;
-  while (NULL != cur_agent)
-  {
-    next_agent = cur_agent->next;
-    GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
-    agent_die (s, cur_agent);
-    cur_agent = next_agent;
-  }
-
-  if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
-  {
-    GNUNET_SCHEDULER_cancel (s->step_next_task_id);
-  }
-  GNUNET_free(s->network_entries);
-  GNUNET_free(s);
-
-  return NULL;
-}
-
-/**
- * Add a new address for a peer to the solver
- *
- * The address is already contained in the addresses hashmap!
- *
- * @param solver the solver Handle
- * @param address the address to add
- * @param network network type of this address
- */
-void
-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_Scope *net;
-  unsigned int m_new;
-  unsigned int m_old;
-  unsigned int n_new;
-  unsigned int n_old;
-  int i;
-  unsigned int zero;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
-
-  net = ril_get_network (s, network);
-  address->solver_information = net;
-
-  if (!ril_network_is_active (s, network))
-  {
-    LOG(GNUNET_ERROR_TYPE_DEBUG,
-        "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
-        address->plugin, address->addr, GNUNET_i2s (&address->peer));
-    return;
-  }
-
-  agent = ril_get_agent (s, &address->peer, GNUNET_YES);
-
-  //add address
-  address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
-  address_wrapped->address_naked = address;
-  address_wrapped->bw_in = RIL_MIN_BW;
-  address_wrapped->bw_out = RIL_MIN_BW;
-  GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
-
-  //increase size of W
-  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);
-    }
-  }
-
-  //increase size of old state vector
-  agent->m = m_old;
-  GNUNET_array_grow(agent->s_old, agent->m, m_new);
-
-  ril_try_unblock_agent(s, agent, GNUNET_NO);
-
-  ril_step (s);
-
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
-      address->active ? "active" : "inactive", address->plugin, address->addr,
-      GNUNET_i2s (&address->peer));
-}
-
-/**
- * Delete an address in the solver
- *
- * The address is not contained in the address hashmap anymore!
- *
- * @param solver the solver handle
- * @param address the address to remove
- * @param session_only delete only session not whole address
- */
-void
-GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
-{
-  struct GAS_RIL_Handle *s = solver;
-  struct RIL_Peer_Agent *agent;
-  struct RIL_Address_Wrapped *address_wrapped;
-  int address_was_used = address->active;
-  int address_index;
-  unsigned int m_new;
-  unsigned int n_new;
-  int i;
-  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,
-      address->addr, GNUNET_i2s (&address->peer));
-
-  agent = ril_get_agent (s, &address->peer, GNUNET_NO);
-  if (NULL == agent)
-  {
-    net = address->solver_information;
-    GNUNET_assert(!ril_network_is_active (s, net->type));
-    LOG(GNUNET_ERROR_TYPE_DEBUG,
-        "No agent allocated for peer yet, since address was in inactive network\n");
-    return;
-  }
-
-  address_index = agent_address_get_index (agent, address);
-  address_wrapped = agent_address_get_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;
-  }
-
-  GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
-  GNUNET_free(address_wrapped);
-
-  //decrease W
-  m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
-  n_new = agent->n - 1;
-
-  for (i = 0; i < agent->n; i++)
-  {
-    ril_cut_from_vector ((void **) &agent->W[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);
-    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))
-  {
-    agent->a_old -= 1;
-  }
-  else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
-  {
-    agent->a_old = RIL_ACTION_INVALID;
-  }
-  //decrease old state vector
-  ril_cut_from_vector ((void **) &agent->s_old, 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);
-  agent->m = m_new;
-  agent->n = n_new;
-
-  if (address_was_used)
-  {
-    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);
-    }
-    else
-    {
-      envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
-    }
-  }
-
-  ril_step (solver);
-}
-
-/**
- * Update the properties of an address in the solver
- *
- * @param solver solver handle
- * @param address the address
- * @param type the ATSI type in HBO
- * @param abs_value the absolute value of the property
- * @param rel_value the normalized value
- */
-void
-GAS_ril_address_property_changed (void *solver,
-    struct ATS_Address *address,
-    uint32_t type,
-    uint32_t abs_value,
-    double rel_value)
-{
-  LOG(GNUNET_ERROR_TYPE_DEBUG,
-      "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
-          "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
-      address->addr, rel_value);
-
-  ril_step (solver);
-}
-
-/**
- * Update the session of an address in the solver
- *
- * NOTE: values in addresses are already updated
- *
- * @param solver solver handle
- * @param address the address
- * @param cur_session the current session
- * @param new_session the new session
- */
-void
-GAS_ril_address_session_changed (void *solver,
-    struct ATS_Address *address,
-    uint32_t cur_session,
-    uint32_t new_session)
-{
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
+  GNUNET_assert (agent->address_inuse != address);
 }
 
-/**
- * Notify the solver that an address is (not) actively used by transport
- * to communicate with a remote peer
- *
- * NOTE: values in addresses are already updated
- *
- * @param solver solver handle
- * @param address the address
- * @param in_use usage state
- */
-void
-GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
-{
-  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");
-}
 
 /**
- * Notify solver that the network an address is located in has changed
- *
- * NOTE: values in addresses are already updated
+ * Update the properties of an address in the solver
  *
  * @param solver solver handle
  * @param address the address
- * @param current_network the current network
- * @param new_network the new network
  */
-void
-GAS_ril_address_change_network (void *solver,
-    struct ATS_Address *address,
-    uint32_t current_network,
-    uint32_t new_network)
+static void
+GAS_ril_address_property_changed (void *solver,
+                                 struct ATS_Address *address)
 {
   struct GAS_RIL_Handle *s = solver;
-  struct RIL_Peer_Agent *agent;
 
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
-      "%s address of peer %s from '%s' to '%s'\n",
-      (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
-      GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
-
-  if (address->active && !ril_network_is_active (solver, new_network))
-  {
-    GAS_ril_address_delete (solver, address, GNUNET_NO);
-    return;
-  }
-
-  agent = ril_get_agent (s, &address->peer, GNUNET_NO);
-  if (NULL == agent)
-  {
-    GNUNET_assert(!ril_network_is_active (solver, current_network));
-
-    GAS_ril_address_add (s, address, new_network);
-    return;
-  }
-
-  address->solver_information = ril_get_network(solver, new_network);
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
+      "Properties for peer '%s' address changed\n",
+      GNUNET_i2s (&address->peer));
+  s->parameters.temperature = s->parameters.temperature_init;
+  s->parameters.epsilon = s->parameters.epsilon_init;
+  ril_step (s);
 }
 
+
 /**
  * Give feedback about the current assignment
  *
@@ -2684,46 +2371,54 @@ GAS_ril_address_change_network (void *solver,
  * @param kind the kind to change the preference
  * @param score the score
  */
-void
+static void
 GAS_ril_address_preference_feedback (void *solver,
-    void *application,
-    const struct GNUNET_PeerIdentity *peer,
-    const struct GNUNET_TIME_Relative scope,
-    enum GNUNET_ATS_PreferenceKind kind,
-    double score)
-{
-  LOG(GNUNET_ERROR_TYPE_DEBUG,
-      "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
-          "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
-      GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
+                                     struct GNUNET_SERVER_Client *application,
+                                     const struct GNUNET_PeerIdentity *peer,
+                                     const struct GNUNET_TIME_Relative scope,
+                                     enum GNUNET_ATS_PreferenceKind kind,
+                                     double score)
+{
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
+       "preference %s for %d seconds\n",
+       GNUNET_i2s (peer),
+       "UNKNOWN",
+       GNUNET_ATS_print_preference_type (kind),
+       scope.rel_value_us / 1000000);
 }
 
+
 /**
  * Start a bulk operation
  *
  * @param solver the solver
  */
-void
+static void
 GAS_ril_bulk_start (void *solver)
 {
   struct GAS_RIL_Handle *s = solver;
 
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "API_bulk_start() lock: %d\n", s->bulk_lock+1);
 
   s->bulk_lock++;
 }
 
+
 /**
  * Bulk operation done
  *
  * @param solver the solver handle
  */
-void
+static void
 GAS_ril_bulk_stop (void *solver)
 {
   struct GAS_RIL_Handle *s = solver;
 
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
+      "API_bulk_stop() lock: %d\n",
+      s->bulk_lock - 1);
 
   if (s->bulk_lock < 1)
   {
@@ -2739,6 +2434,7 @@ GAS_ril_bulk_stop (void *solver)
   }
 }
 
+
 /**
  * Tell solver to notify ATS if the address to use changes for a specific
  * peer using the bandwidth changed callback
@@ -2749,12 +2445,10 @@ GAS_ril_bulk_stop (void *solver)
  * @param solver the solver handle
  * @param peer the identity of the peer
  */
-const struct ATS_Address *
-GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
+static void
+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;
 
@@ -2778,11 +2472,15 @@ 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;
+  if (NULL != agent->address_inuse)
+    s->env->bandwidth_changed_cb (s->env->cls,
+                                  agent->address_inuse);
 }
 
+
 /**
  * Tell solver stop notifying ATS about changes for this peers
  *
@@ -2792,13 +2490,15 @@ GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *p
  * @param solver the solver handle
  * @param peer the peer
  */
-void
-GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
+static void
+GAS_ril_stop_get_preferred_address (void *solver,
+                                   const struct GNUNET_PeerIdentity *peer)
 {
   struct GAS_RIL_Handle *s = solver;
   struct RIL_Peer_Agent *agent;
 
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "API_stop_get_preferred_address()");
 
   agent = ril_get_agent (s, peer, GNUNET_NO);
 
@@ -2813,6 +2513,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,
@@ -2821,8 +2524,327 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
   ril_step (s);
 
   LOG(GNUNET_ERROR_TYPE_DEBUG,
-      "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
-      GNUNET_i2s (peer), agent->address_inuse->plugin);
+      "API_stop_get_preferred_address() Paused agent for peer '%s'\n",
+      GNUNET_i2s (peer));
+}
+
+
+/**
+ * Entry point for the plugin
+ *
+ * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
+ */
+void *
+libgnunet_plugin_ats_ril_init (void *cls)
+{
+  static struct GNUNET_ATS_SolverFunctions sf;
+  struct GNUNET_ATS_PluginEnvironment *env = cls;
+  struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
+  struct RIL_Scope * cur;
+  int c;
+  char *string;
+  float f_tmp;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "API_init() Initializing RIL solver\n");
+
+  GNUNET_assert (NULL != env);
+  GNUNET_assert (NULL != env->cfg);
+  GNUNET_assert (NULL != env->stats);
+  GNUNET_assert (NULL != env->bandwidth_changed_cb);
+  GNUNET_assert (NULL != env->get_preferences);
+
+  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))
+  {
+    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))
+  {
+    GNUNET_STRINGS_utf8_toupper (string, string);
+    if (0 == strcmp (string, "SARSA"))
+    {
+        solver->parameters.algorithm = RIL_ALGO_SARSA;
+    }
+    if (0 == strcmp (string, "Q-LEARNING"))
+    {
+        solver->parameters.algorithm = RIL_ALGO_Q;
+    }
+
+    GNUNET_free (string);
+  }
+  else
+  {
+    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;
+  }
+
+
+  solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
+  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
+      "RIL_DISCOUNT_BETA", &f_tmp))
+  {
+    if (f_tmp < 0.0)
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
+          "RIL_DISCOUNT_BETA", f_tmp);
+    }
+    else
+    {
+      solver->parameters.beta = f_tmp;
+      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
+          "RIL_DISCOUNT_BETA", f_tmp);
+    }
+  }
+
+  solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
+  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
+      "RIL_DISCOUNT_GAMMA", &f_tmp))
+  {
+    if ((f_tmp < 0.0) || (f_tmp > 1.0))
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
+          "RIL_DISCOUNT_GAMMA", f_tmp);
+    }
+    else
+    {
+      solver->parameters.gamma = f_tmp;
+      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
+          "RIL_DISCOUNT_GAMMA", f_tmp);
+    }
+  }
+
+  solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
+  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
+      "RIL_GRADIENT_STEP_SIZE", &f_tmp))
+  {
+    if ((f_tmp < 0.0) || (f_tmp > 1.0))
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
+          "RIL_GRADIENT_STEP_SIZE", f_tmp);
+    }
+    else
+    {
+      solver->parameters.alpha = f_tmp;
+      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
+          "RIL_GRADIENT_STEP_SIZE", f_tmp);
+    }
+  }
+
+  solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
+  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
+      "RIL_TRACE_DECAY", &f_tmp))
+  {
+    if ((f_tmp < 0.0) || (f_tmp > 1.0))
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
+          "RIL_TRACE_DECAY", f_tmp);
+    }
+    else
+    {
+      solver->parameters.lambda = f_tmp;
+      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
+          "RIL_TRACE_DECAY", f_tmp);
+    }
+  }
+
+  solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
+  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
+      "RIL_EXPLORE_RATIO", &f_tmp))
+  {
+    if ((f_tmp < 0.0) || (f_tmp > 1.0))
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
+          "RIL_EXPLORE_RATIO", f_tmp);
+    }
+    else
+    {
+      solver->parameters.epsilon_init = f_tmp;
+      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
+          "RIL_EXPLORE_RATIO", f_tmp);
+    }
+  }
+
+  solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
+  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
+      "RIL_EXPLORE_DECAY", &f_tmp))
+  {
+    if ((f_tmp < 0.0) || (f_tmp > 1.0))
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
+          "RIL_EXPLORE_DECAY", f_tmp);
+    }
+    else
+    {
+      solver->parameters.epsilon_decay = f_tmp;
+      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
+          "RIL_EXPLORE_DECAY", f_tmp);
+    }
+  }
+
+  solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
+  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
+      "RIL_TEMPERATURE", &f_tmp))
+  {
+    if (f_tmp <= 0.0)
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
+          "RIL_TEMPERATURE", f_tmp);
+    }
+    else
+    {
+      solver->parameters.temperature_init = f_tmp;
+      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
+          "RIL_TEMPERATURE", f_tmp);
+    }
+  }
+
+  solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
+  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
+      "RIL_TEMPERATURE_DECAY", &f_tmp))
+  {
+    if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1)
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
+          "RIL_TEMPERATURE_DECAY", f_tmp);
+    }
+    else
+    {
+      solver->parameters.temperature_decay = f_tmp;
+      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
+          "RIL_TEMPERATURE_DECAY", f_tmp);
+    }
+  }
+
+  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;
+  }
+
+  solver->env = env;
+  sf.cls = solver;
+  sf.s_add = &GAS_ril_address_add;
+  sf.s_address_update_property = &GAS_ril_address_property_changed;
+  sf.s_get = &GAS_ril_get_preferred_address;
+  sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
+  sf.s_pref = &GAS_ril_address_change_preference;
+  sf.s_feedback = &GAS_ril_address_preference_feedback;
+  sf.s_del = &GAS_ril_address_delete;
+  sf.s_bulk_start = &GAS_ril_bulk_start;
+  sf.s_bulk_stop = &GAS_ril_bulk_stop;
+
+  solver->networks_count = env->network_count;
+  solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
+  solver->step_count = 0;
+  solver->done = GNUNET_NO;
+
+  for (c = 0; c < env->network_count; c++)
+  {
+    cur = &solver->network_entries[c];
+    cur->type = c;
+    cur->bw_in_available = env->in_quota[c];
+    cur->bw_out_available = env->out_quota[c];
+    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_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_DEBUG, "init()  exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
+      solver->parameters.epsilon,
+      solver->parameters.temperature,
+      solver->parameters.select ? "EGREEDY" : "SOFTMAX");
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  RBF_DIVISOR = %llu\n",
+      solver->parameters.rbf_divisor);
+
+  return &sf;
+}
+
+
+/**
+ * Exit point for the plugin
+ *
+ * @param cls the solver handle
+ */
+void *
+libgnunet_plugin_ats_ril_done (void *cls)
+{
+  struct GNUNET_ATS_SolverFunctions *sf = cls;
+  struct GAS_RIL_Handle *s = sf->cls;
+  struct RIL_Peer_Agent *cur_agent;
+  struct RIL_Peer_Agent *next_agent;
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
+
+  s->done = GNUNET_YES;
+
+  cur_agent = s->agents_head;
+  while (NULL != cur_agent)
+  {
+    next_agent = cur_agent->next;
+    GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
+    agent_die (s, cur_agent);
+    cur_agent = next_agent;
+  }
+
+  if (NULL != s->step_next_task_id)
+  {
+    GNUNET_SCHEDULER_cancel (s->step_next_task_id);
+  }
+  GNUNET_free(s->network_entries);
+  GNUNET_free(s);
+
+  return NULL;
 }
 
+
 /* end of plugin_ats_ril.c */