#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
/**
* 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;
};
/**
struct GAS_RIL_Handle
{
/**
- *
+ * The solver-plugin environment of the solver-plugin API
*/
struct GNUNET_ATS_PluginEnvironment *plugin_envi;
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
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;
}
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];
}
}
* 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
{
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++)
{
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;
}
}
+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
/**
* 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 *
* For all networks a peer has an address in, this gets the maximum bandwidth which could
* theoretically be available in one of the networks. This is used for bandwidth normalization.
*
- * @param solver the solver handle
* @param agent the agent handle
* @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
*/
static 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;
prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
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));
+ ((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 += 1 - (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
* parts from the local (the peer specific) and the global (for all peers
* identical) reward.
*
- * @param solver solver handle
+ * @param solver the solver handle
+ * @param agent the agent handle
* @return the reward
*/
static double
//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;
}
/**
* 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)
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->is_active && cur->address_inuse)
}
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;
}
/**
/**
* Deallocate agent
*
- * @param s solver handle
+ * @param solver the solver handle
* @param agent the agent to retire
*/
static void
/**
* 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)
* @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)
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);
}
/**
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");
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;
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;
}
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);
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));
}
}
+ ril_step (solver);
+
LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
}
"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);
}
/**
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",
/**
* 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;
+ }
}
/**
GNUNET_i2s (peer));
}
+ ril_step (s);
+
return agent->address_inuse;
}
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);