fix eligibility traces
[oweals/gnunet.git] / src / ats / libgnunet_plugin_ats_ril.c
index 2a35007f3b175161afdd3a894d2a677f0b549544..7dcc29efd8a33cea69ab4e582431774436e4918d 100755 (executable)
 #define RIL_ACTION_INVALID -1
 #define RIL_FEATURES_ADDRESS_COUNT (3 + GNUNET_ATS_QualityPropertiesCount)
 #define RIL_FEATURES_NETWORK_COUNT 4
+#define RIL_INTERVAL_EXPONENT 10
 
-#define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
-#define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
-#define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
+#define RIL_DEFAULT_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 0.7
 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
 #define RIL_DEFAULT_TRACE_DECAY 0.6
-#define RIL_EXPLORE_RATIO 0.1
+#define RIL_DEFAULT_EXPLORE_RATIO 0.1
 
 /**
  * ATS reinforcement learning solver
@@ -92,17 +94,32 @@ struct RIL_Learning_Parameters
   /**
    * Learning discount factor in the TD-update
    */
-  float gamma;
+  double beta;
 
   /**
    * Gradient-descent step-size
    */
-  float alpha;
+  double alpha;
 
   /**
    * Trace-decay factor for eligibility traces
    */
-  float lambda;
+  double lambda;
+
+  /**
+   *
+   */
+  double explore_ratio;
+
+  /**
+   * Minimal interval time between steps in milliseconds
+   */
+  struct GNUNET_TIME_Relative step_time_min;
+
+  /**
+   * Maximum interval time between steps in milliseconds
+   */
+  struct GNUNET_TIME_Relative step_time_max;
 };
 
 /**
@@ -151,7 +168,7 @@ struct RIL_Peer_Agent
   /**
    * Whether the agent is active or not
    */
-  int active;
+  int is_active;
 
   /**
    * Number of performed time-steps
@@ -248,7 +265,7 @@ struct RIL_Network
 struct GAS_RIL_Handle
 {
   /**
-   *
+   * The solver-plugin environment of the solver-plugin API
    */
   struct GNUNET_ATS_PluginEnvironment *plugin_envi;
 
@@ -258,19 +275,44 @@ struct GAS_RIL_Handle
   struct GNUNET_STATISTICS_Handle *stats;
 
   /**
-   * Number of performed time-steps
+   * Number of performed steps
    */
   unsigned long long step_count;
 
   /**
-   * Interval time between steps in milliseconds //TODO? Future Work: Heterogeneous stepping among agents
+   * Timestamp for the last time-step
    */
-  struct GNUNET_TIME_Relative step_time;
+  struct GNUNET_TIME_Absolute step_time_last;
 
   /**
    * Task identifier of the next time-step to be executed
    */
-  GNUNET_SCHEDULER_TaskIdentifier next_step;
+  GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
+
+  /**
+   * Whether a step is already scheduled
+   */
+  int task_pending;
+
+  /**
+   * Variable discount factor, dependent on time between steps
+   */
+  double discount_variable;
+
+  /**
+   * Integrated variable discount factor, dependent on time between steps
+   */
+  double discount_integrated;
+
+  /**
+   * Lock for bulk operations
+   */
+  int bulk_lock;
+
+  /**
+   * Number of changes during a lock
+   */
+  int bulk_changes;
 
   /**
    * Learning parameters
@@ -332,13 +374,10 @@ static int
 agent_decide_exploration (struct RIL_Peer_Agent *agent)
 {
   //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
-  /*
-   * An e-greedy replacement could be based on the accuracy of the prediction of the Q-value
-   */
   double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
       UINT32_MAX) / (double) UINT32_MAX;
 
-if  (r < RIL_EXPLORE_RATIO)
+  if (r < agent->envi->parameters.explore_ratio)
   {
     return GNUNET_YES;
   }
@@ -454,11 +493,12 @@ agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_nex
   double delta;
   double *theta = agent->W[agent->a_old];
 
-  delta = reward + agent_estimate_q (agent, s_next, a_prime)
-      - agent_estimate_q (agent, agent->s_old, agent->a_old);
+  delta = agent->envi->discount_integrated * reward; //reward
+  delta += agent->envi->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
   for (i = 0; i < agent->m; i++)
   {
-    theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
+    theta[i] += agent->envi->parameters.alpha * delta * agent->e[i];
   }
 }
 
@@ -466,7 +506,7 @@ agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_nex
  * Changes the eligibility trace vector e in various manners:
  * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
  * RIL_E_REPLACE - resets each component to 1 as in replacing traces
- * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
+ * RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
  * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
  *
  * @param agent the agent handle
@@ -477,8 +517,6 @@ agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification
 {
   int i;
   double *e = agent->e;
-  double gamma = agent->envi->parameters.gamma;
-  double lambda = agent->envi->parameters.lambda;
 
   for (i = 0; i < agent->m; i++)
   {
@@ -491,7 +529,7 @@ agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification
       e[i] = 1;
       break;
     case RIL_E_SET:
-      e[i] = gamma * lambda;
+      e[i] *= agent->envi->discount_variable * agent->envi->parameters.lambda;
       break;
     case RIL_E_ZERO:
       e[i] = 0;
@@ -500,6 +538,15 @@ agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification
   }
 }
 
+static void
+ril_inform (struct GAS_RIL_Handle *solver,
+    enum GAS_Solver_Operation op,
+    enum GAS_Solver_Status stat)
+{
+  if (NULL != solver->plugin_envi->info_cb)
+    solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
+}
+
 /**
  * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
  * notify ATS of its new decision
@@ -534,8 +581,8 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
     }
     if (NULL != new_address)
     {
-      LOG(GNUNET_ERROR_TYPE_DEBUG, "set address active: %s\n", agent->active ? "yes" : "no");
-      new_address->active = agent->active;
+      LOG(GNUNET_ERROR_TYPE_DEBUG, "set address active: %s\n", agent->is_active ? "yes" : "no");
+      new_address->active = agent->is_active;
       new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
       new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
     }
@@ -545,9 +592,9 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
   if (new_address)
   {
     //activity change
-    if (new_address->active != agent->active)
+    if (new_address->active != agent->is_active)
     {
-      new_address->active = agent->active;
+      new_address->active = agent->is_active;
     }
 
     //bw change
@@ -565,7 +612,7 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
     }
   }
 
-  if (notify && agent->active && (GNUNET_NO == silent))
+  if (notify && agent->is_active && (GNUNET_NO == silent))
   {
     if (new_address)
     {
@@ -588,8 +635,8 @@ envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
 
 /**
  * Allocates a state vector and fills it with the features present
- *
  * @param solver the solver handle
+ * @param agent the agent handle
  * @return pointer to the state vector
  */
 static double *
@@ -633,7 +680,6 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
  * For all networks a peer has an address in, this gets the maximum bandwidth which could
  * theoretically be available in one of the networks. This is used for bandwidth normalization.
  *
- * @param solver the solver handle
  * @param agent the agent handle
  * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
  */
@@ -686,46 +732,87 @@ ril_find_property_index (uint32_t type)
   return GNUNET_SYSERR;
 }
 
-/**
- * Gets the reward for the last performed step
- *
- * @param solver solver handle
- * @return the reward
- */
 static double
-envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
+envi_reward_global (struct GAS_RIL_Handle *solver)
 {
-  /*
-   * - Match the preferences of the peer with the current assignment
-   * - Validity of the solution
-   */
+  int i;
+  unsigned int in_available = 0;
+  unsigned int out_available = 0;
+  unsigned int in_assigned = 0;
+  unsigned int out_assigned = 0;
+  double ratio_in;
+  double ratio_out;
+
+  for (i = 0; i < solver->networks_count; i++)
+  {
+    in_available += solver->network_entries[i].bw_in_available;
+    in_assigned += solver->network_entries[i].bw_in_assigned;
+    out_available += solver->network_entries[i].bw_out_available;
+    out_assigned += solver->network_entries[i].bw_out_assigned;
+  }
+
+  ratio_in = ((double) in_assigned) / ((double) in_available);
+  ratio_out = ((double) out_assigned) / ((double) out_available);
+
+  return ((ratio_in + ratio_out) * 0.5) + 1;
+}
+
+static double
+envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
+{
+  //TODO! add utilization
+
   const double *preferences;
   const double *properties;
+  int prop_index;
   double pref_match = 0;
   double bw_norm;
-  struct RIL_Network *net;
-  int prop_index;
 
   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);
+
+  //preference matching from latency and bandwidth
   prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
-  pref_match += preferences[GNUNET_ATS_PREFERENCE_LATENCY] * (3 - properties[prop_index]); //invert property as we want to maximize for lower latencies
+  pref_match += 1 - (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * (3 - properties[prop_index])); //invert property as we want to maximize for lower latencies
   bw_norm = GNUNET_MAX(2, (((
                   ((double) agent->bw_in / (double) ril_get_max_bw(agent, GNUNET_YES)) +
                   ((double) agent->bw_out / (double) ril_get_max_bw(agent, GNUNET_NO))
               ) / 2
           ) + 1));
-  pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm;
 
+  pref_match += 1 - (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
+
+  return pref_match * 0.5;
+}
+
+/**
+ * Gets the reward for the last performed step, which is calculated in equal
+ * parts from the local (the peer specific) and the global (for all peers
+ * identical) reward.
+ *
+ * @param solver the solver handle
+ * @param agent the agent handle
+ * @return the reward
+ */
+static double
+envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
+{
+  struct RIL_Network *net;
+  double reward = 0;
+
+  //punish overutilization
   net = agent->address_inuse->solver_information;
-  if ((net->bw_in_assigned > net->bw_in_available) || (net->bw_out_assigned > net->bw_out_available))
+  if ((net->bw_in_assigned > net->bw_in_available)
+      || (net->bw_out_assigned > net->bw_out_available))
   {
     return -1;
   }
 
-  return pref_match;
+  reward += envi_reward_global (solver);
+  reward += envi_reward_local (solver, agent);
+  return reward * 0.5;
 }
 
 /**
@@ -879,8 +966,9 @@ envi_action_address_switch (struct GAS_RIL_Handle *solver,
 /**
  * Puts the action into effect by calling the according function
  *
- * @param solver solver handle
- * @param action action to perform by the solver
+ * @param solver the solver handle
+ * @param agent the action handle
+ * @param action the action to perform by the solver
  */
 static void
 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
@@ -1005,30 +1093,156 @@ agent_step (struct RIL_Peer_Agent *agent)
   agent->step_count += 1;
 }
 
+static int
+ril_step (struct GAS_RIL_Handle *solver);
+
 /**
- * Cycles through all agents and lets the active ones do a step. Schedules the next step.
+ * Task for the scheduler, which performs one step and lets the solver know that
+ * no further step is scheduled.
  *
- * @param solver the solver handle
- * @param tc task context for the scheduler
+ * @param cls the solver handle
+ * @param tc the task context for the scheduler
  */
 static void
-ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GAS_RIL_Handle *solver = cls;
+
+  solver->task_pending = GNUNET_NO;
+  ril_step (solver);
+}
+
+static double
+ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
+{
+  int i;
+  struct RIL_Network net;
+  unsigned long long sum_assigned = 0;
+  unsigned long long sum_available = 0;
+  double ratio;
+
+  for (i = 0; i < solver->networks_count; i++)
+  {
+    net = solver->network_entries[i];
+    if (net.bw_in_assigned > 0) //only consider scopes with an active address
+    {
+      sum_assigned += net.bw_in_assigned;
+      sum_assigned += net.bw_out_assigned;
+      sum_available += net.bw_in_available;
+      sum_available += net.bw_out_available;
+    }
+  }
+  if (sum_available > 0)
+  {
+    ratio = ((double) sum_assigned) / ((double) sum_available);
+  }
+  else
+  {
+    ratio = 0;
+  }
+
+  return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
+}
+
+/**
+ * Schedules the next global step in an adaptive way. The more resources are
+ * left, the earlier the next step is scheduled. This serves the reactivity of
+ * the solver to changed inputs.
+ *
+ * @param solver the solver handle
+ */
+static void
+ril_step_schedule_next (struct GAS_RIL_Handle *solver)
+{
+  double used_ratio;
+  double factor;
+  double y;
+  double offset;
+  struct GNUNET_TIME_Relative time_next;
+
+  if (solver->task_pending)
+  {
+    GNUNET_SCHEDULER_cancel (solver->step_next_task_id);
+  }
+
+  used_ratio = ril_get_used_resource_ratio (solver);
+
+  GNUNET_assert(
+      solver->parameters.step_time_min.rel_value_us
+          < solver->parameters.step_time_max.rel_value_us);
+
+  factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
+      solver->parameters.step_time_min).rel_value_us;
+  offset = (double) solver->parameters.step_time_min.rel_value_us;
+  y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
+
+  //LOG (GNUNET_ERROR_TYPE_INFO, "used = %f, min = %f, max = %f\n", used_ratio, (double)solver->parameters.step_time_min.rel_value_us, (double)solver->parameters.step_time_max.rel_value_us);
+  //LOG (GNUNET_ERROR_TYPE_INFO, "factor = %f, offset = %f, y = %f\n", factor, offset, y);
+
+  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 int) y);
+
+  solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
+      solver);
+  solver->task_pending = GNUNET_YES;
+}
+
+/**
+ * Triggers one step per agent
+ * @param solver
+ */
+static int
+ril_step (struct GAS_RIL_Handle *solver)
+{
   struct RIL_Peer_Agent *cur;
+  struct GNUNET_TIME_Absolute time_now;
+  struct GNUNET_TIME_Relative time_delta;
+  double tau;
+
+  if (GNUNET_YES == solver->bulk_lock)
+  {
+    solver->bulk_changes++;
+    return GNUNET_NO;
+  }
+
+  ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
 
   LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
 
+  if (0 == solver->step_count)
+  {
+    solver->step_time_last = GNUNET_TIME_absolute_get ();
+  }
+
+  //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
+  time_now = GNUNET_TIME_absolute_get ();
+  time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
+  tau = ((double) time_delta.rel_value_us)
+      / ((double) solver->parameters.step_time_min.rel_value_us);
+  solver->step_time_last = time_now;
+
+  //calculate reward discounts (once per step for all agents)
+  solver->discount_variable = pow (M_E, ((-1.) * ((double) solver->parameters.beta) * tau));
+  solver->discount_integrated = (1 - solver->discount_variable)
+      / ((double) solver->parameters.beta);
+
+  //trigger one step per active agent
   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
   {
-    if (cur->active && cur->address_inuse)
+    if (cur->is_active && cur->address_inuse)
     {
       agent_step (cur);
     }
   }
 
   solver->step_count += 1;
-  solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, &ril_periodic_step, solver);
+  ril_step_schedule_next (solver);
+
+  ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
+
+  return GNUNET_YES;
 }
 
 /**
@@ -1048,7 +1262,7 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
   agent->envi = solver;
   agent->peer = *peer;
   agent->step_count = 0;
-  agent->active = GNUNET_NO;
+  agent->is_active = GNUNET_NO;
   agent->n = RIL_ACTION_TYPE_NUM;
   agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
   agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
@@ -1069,7 +1283,7 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
 /**
  * Deallocate agent
  *
- * @param s solver handle
+ * @param solver the solver handle
  * @param agent the agent to retire
  */
 static void
@@ -1090,10 +1304,10 @@ agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
 /**
  * Returns the agent for a peer
  *
- * @param s solver handle
- * @param peer identity of the peer
- * @param create whether to create an agent if none is allocated yet
- * @return agent
+ * @param solver the solver handle
+ * @param peer the identity of the peer
+ * @param create whether or not to create an agent, if none is allocated yet
+ * @return the agent
  */
 static struct RIL_Peer_Agent *
 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
@@ -1222,7 +1436,7 @@ ril_cut_from_vector (void **old,
  * @param pref_rel the normalized preference value for this kind over all clients
  */
 void
-GAS_ril_address_change_preference (void *s,
+GAS_ril_address_change_preference (void *solver,
     const struct GNUNET_PeerIdentity *peer,
     enum GNUNET_ATS_PreferenceKind kind,
     double pref_rel)
@@ -1230,9 +1444,8 @@ GAS_ril_address_change_preference (void *s,
   LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
-  /*
-   * Nothing to do here. Preferences are considered during reward calculation.
-   */
+
+  ril_step (solver);
 }
 
 /**
@@ -1247,7 +1460,6 @@ libgnunet_plugin_ats_ril_init (void *cls)
   struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
   struct RIL_Network * cur;
   int c;
-  unsigned long long tmp;
   char *string;
 
   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
@@ -1260,45 +1472,63 @@ libgnunet_plugin_ats_ril_init (void *cls)
   GNUNET_assert(NULL != env->get_property);
 
   if (GNUNET_OK
-      != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
+      != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
+          &solver->parameters.step_time_min))
+  {
+    solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
+  }
+  if (GNUNET_OK
+      != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
+          &solver->parameters.step_time_max))
   {
-    solver->step_time = RIL_DEFAULT_STEP_TIME;
+    solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string)
-      && NULL != string && 0 == strcmp (string, "SARSA"))
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
   {
-    solver->parameters.algorithm = RIL_ALGO_SARSA;
+    solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
+    GNUNET_free (string);
   }
   else
   {
     solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
   }
-  if (GNUNET_OK
-      == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
   {
-    solver->parameters.gamma = (double) tmp / 100;
+    solver->parameters.beta = strtod (string, NULL);
+    GNUNET_free (string);
   }
   else
   {
-    solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
+    solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
   }
   if (GNUNET_OK
-      == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
+      == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
   {
-    solver->parameters.alpha = (double) tmp / 100;
+    solver->parameters.alpha = strtod (string, NULL);
+    GNUNET_free (string);
   }
   else
   {
     solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
   }
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_TRACE_DECAY", &tmp))
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
   {
-    solver->parameters.lambda = (double) tmp / 100;
+    solver->parameters.lambda = strtod (string, NULL);
+    GNUNET_free (string);
   }
   else
   {
     solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
   }
+  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
+  {
+    solver->parameters.explore_ratio = strtod (string, NULL);
+    GNUNET_free (string);
+  }
+  else
+  {
+    solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
+  }
 
   env->sf.s_add = &GAS_ril_address_add;
   env->sf.s_address_update_property = &GAS_ril_address_property_changed;
@@ -1328,9 +1558,10 @@ libgnunet_plugin_ats_ril_init (void *cls)
     cur->bw_out_assigned = 0;
   }
 
-  solver->next_step = GNUNET_SCHEDULER_add_delayed (
+  solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (
       GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
-      &ril_periodic_step, solver);
+      &ril_step_scheduler_task, solver);
+  solver->task_pending = GNUNET_YES;
 
   return solver;
 }
@@ -1358,7 +1589,10 @@ libgnunet_plugin_ats_ril_done (void *cls)
     cur_agent = next_agent;
   }
 
-  GNUNET_SCHEDULER_cancel (s->next_step);
+  if (s->task_pending)
+  {
+    GNUNET_SCHEDULER_cancel (s->step_next_task_id);
+  }
   GNUNET_free(s->network_entries);
   GNUNET_free(s);
 
@@ -1442,6 +1676,8 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
     envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
   }
 
+  ril_step (s);
+
   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %p for peer '%s'\n",
       address->active ? "active" : "inactive", address->plugin, address->addr,
       GNUNET_i2s (&address->peer));
@@ -1558,6 +1794,8 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
     }
   }
 
+  ril_step (solver);
+
   LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
 }
 
@@ -1581,9 +1819,8 @@ GAS_ril_address_property_changed (void *solver,
       "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
           "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
       address->addr, rel_value);
-  /*
-   * Nothing to do here, properties are considered in every reward calculation
-   */
+
+  ril_step (solver);
 }
 
 /**
@@ -1622,7 +1859,7 @@ void
 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
 {
   /* Nothing to do here.
-   * Possible TODO? Future Work: Use usage as state vector
+   * Possible 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",
@@ -1707,29 +1944,42 @@ GAS_ril_address_preference_feedback (void *solver,
 /**
  * Start a bulk operation
  *
- * Since new calculations of the assignment are not triggered by a change of preferences, as it
- * happens in the proportional and the mlp solver, there is no need to block this solver.
- *
  * @param solver the solver
  */
 void
 GAS_ril_bulk_start (void *solver)
 {
-  /* nop */
+  struct GAS_RIL_Handle *s = solver;
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() Locking solver for bulk operation ...\n");
+
+  s->bulk_lock++;
 }
 
 /**
  * Bulk operation done
  *
- * Since new calculations of the assignment are not triggered by a change of preferences, as it
- * happens in the proportional and the mlp solver, there is no need to block this solver.
- *
  * @param solver the solver handle
  */
 void
 GAS_ril_bulk_stop (void *solver)
 {
-  /* nop */
+  struct GAS_RIL_Handle *s = solver;
+
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() Releasing solver from bulk operation ...\n");
+
+  if (s->bulk_lock < 1)
+  {
+    GNUNET_break(0);
+    return;
+  }
+  s->bulk_lock--;
+
+  if (0 < s->bulk_changes)
+  {
+    ril_step (solver);
+    s->bulk_changes = 0;
+  }
 }
 
 /**
@@ -1754,7 +2004,7 @@ GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *p
 
   agent = ril_get_agent (s, peer, GNUNET_YES);
 
-  agent->active = GNUNET_YES;
+  agent->is_active = GNUNET_YES;
 
   if (agent->address_inuse)
   {
@@ -1778,6 +2028,8 @@ GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *p
         GNUNET_i2s (peer));
   }
 
+  ril_step (s);
+
   return agent->address_inuse;
 }
 
@@ -1804,13 +2056,13 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
     GNUNET_break(0);
     return;
   }
-  if (GNUNET_NO == agent->active)
+  if (GNUNET_NO == agent->is_active)
   {
     GNUNET_break(0);
     return;
   }
 
-  agent->active = GNUNET_NO;
+  agent->is_active = GNUNET_NO;
   if (agent->address_inuse)
   {
     net = agent->address_inuse->solver_information;
@@ -1820,6 +2072,8 @@ GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdenti
   envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
       GNUNET_YES);
 
+  ril_step (s);
+
   LOG(GNUNET_ERROR_TYPE_DEBUG,
       "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
       GNUNET_i2s (peer), agent->address_inuse->plugin);