2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/libgnunet_plugin_ats_ril.c
23 * @brief ATS reinforcement learning solver
24 * @author Fabian Oehlmann
25 * @author Matthias Wachs
27 #include "libgnunet_plugin_ats_ril.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
31 #define RIL_ACTION_INVALID -1
32 #define RIL_FEATURES_ADDRESS_COUNT (3 + GNUNET_ATS_QualityPropertiesCount)
33 #define RIL_FEATURES_NETWORK_COUNT 4
35 #define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
36 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
37 #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
38 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
39 #define RIL_DEFAULT_TRACE_DECAY 0.6
40 #define RIL_EXPLORE_RATIO 0.1
43 * ATS reinforcement learning solver
49 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
50 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
51 * to a particular address. The action of switching to address with index i is depicted by the
52 * number (RIL_ACTION_TYPE_NUM + i).
56 RIL_ACTION_NOTHING = 0,
57 RIL_ACTION_BW_IN_DBL = 1,
58 RIL_ACTION_BW_IN_HLV = 2,
59 RIL_ACTION_BW_IN_INC = 3,
60 RIL_ACTION_BW_IN_DEC = 4,
61 RIL_ACTION_BW_OUT_DBL = 5,
62 RIL_ACTION_BW_OUT_HLV = 6,
63 RIL_ACTION_BW_OUT_INC = 7,
64 RIL_ACTION_BW_OUT_DEC = 8,
65 RIL_ACTION_TYPE_NUM = 9
74 enum RIL_E_Modification
83 * Global learning parameters
85 struct RIL_Learning_Parameters
88 * The TD-algorithm to use
90 enum RIL_Algorithm algorithm;
93 * Learning discount factor in the TD-update
98 * Gradient-descent step-size
103 * Trace-decay factor for eligibility traces
109 * Wrapper for addresses to store them in agent's linked list
111 struct RIL_Address_Wrapped
116 struct RIL_Address_Wrapped *next;
121 struct RIL_Address_Wrapped *prev;
126 struct ATS_Address *address_naked;
129 struct RIL_Peer_Agent
132 * Next agent in solver's linked list
134 struct RIL_Peer_Agent *next;
137 * Previous agent in solver's linked list
139 struct RIL_Peer_Agent *prev;
144 struct GAS_RIL_Handle *envi;
149 struct GNUNET_PeerIdentity peer;
152 * Whether the agent is active or not
154 int active; //TODO? rename into something better. It reflects the state whether get_preferred_address() has been called for the according peer or not
157 * Number of performed time-steps
159 unsigned long long step_count;
162 * Experience matrix W
167 * Number of rows of W / Number of state-vector features
172 * Number of columns of W / Number of actions
177 * Last perceived state feature vector
187 * Eligibility trace vector
194 struct ATS_Address * address_inuse;
197 * Head of addresses DLL
199 struct RIL_Address_Wrapped * addresses_head;
202 * Tail of addresses DLL
204 struct RIL_Address_Wrapped * addresses_tail;
207 * Inbound bandwidth assigned by the agent
209 unsigned long long bw_in;
212 * Outbound bandwidth assigned by the agent
214 unsigned long long bw_out;
222 enum GNUNET_ATS_Network_Type type;
225 * Total available inbound bandwidth
227 unsigned long long bw_in_available;
230 * Total assigned outbound bandwidth
232 unsigned long long bw_in_assigned;
235 * Total available outbound bandwidth
237 unsigned long long bw_out_available;
240 * Total assigned outbound bandwidth
242 unsigned long long bw_out_assigned;
246 * A handle for the reinforcement learning solver
248 struct GAS_RIL_Handle
253 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
258 struct GNUNET_STATISTICS_Handle *stats;
261 * Number of performed time-steps
263 unsigned long long step_count;
266 * Interval time between steps in milliseconds //TODO? Future Work: Heterogeneous stepping among agents
268 struct GNUNET_TIME_Relative step_time;
271 * Task identifier of the next time-step to be executed
273 GNUNET_SCHEDULER_TaskIdentifier next_step;
276 * Learning parameters
278 struct RIL_Learning_Parameters parameters;
281 * Array of networks with global assignment state
283 struct RIL_Network * network_entries;
288 unsigned int networks_count;
291 * List of active peer-agents
293 struct RIL_Peer_Agent * agents_head;
294 struct RIL_Peer_Agent * agents_tail;
299 * ---------------------------
303 * Estimate the current action-value for state s and action a
304 * @param agent agent performing the estimation
307 * @return estimation value
310 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
315 for (i = 0; i < agent->m; i++)
317 result += state[i] * agent->W[action][i];
324 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
325 * currently estimated best action) in the current step
326 * @param agent agent performing the step
327 * @return yes, if exploring
330 agent_decide_exploration (struct RIL_Peer_Agent *agent)
332 //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
334 * An e-greedy replacement could be based on the accuracy of the prediction of the Q-value
336 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
337 UINT32_MAX) / (double) UINT32_MAX;
339 if (r < RIL_EXPLORE_RATIO)
347 * Get the index of the address in the agent's list.
348 * @param agent agent handle
349 * @param address address handle
350 * @return the index, starting with zero
353 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
356 struct RIL_Address_Wrapped *cur;
359 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
362 if (cur->address_naked == address)
372 * Gets the wrapped address from the agent's list
373 * @param agent agent handle
374 * @param address address handle
375 * @return wrapped address
377 static struct RIL_Address_Wrapped *
378 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
380 struct RIL_Address_Wrapped *cur;
382 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
384 if (cur->address_naked == address)
394 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
395 * most reward in the future)
396 * @param agent agent performing the calculation
397 * @param state the state from which to take the action
398 * @return the action promising most future reward
401 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
404 int max_i = RIL_ACTION_INVALID;
406 double max_q = -DBL_MAX;
408 for (i = 0; i < agent->n; i++)
410 cur_q = agent_estimate_q (agent, state, i);
418 GNUNET_assert(RIL_ACTION_INVALID != max_i);
424 * Gets any action, to explore the action space from that state
425 * @param agent agent performing the calculation
426 * @param state the state from which to take the action
430 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
432 // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
433 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
437 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
438 * @param agent the agent performing the update
439 * @param reward the reward received for the last action
440 * @param s_next the new state, the last step got the agent into
441 * @param a_prime the new
444 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
448 double *theta = agent->W[agent->a_old];
450 delta = reward + agent_estimate_q (agent, s_next, a_prime)
451 - agent_estimate_q (agent, agent->s_old, agent->a_old);
452 for (i = 0; i < agent->m; i++)
454 theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
459 * Changes the eligibility trace vector e in various manners:
460 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
461 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
462 * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
463 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
468 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
471 double *e = agent->e;
472 double gamma = agent->envi->parameters.gamma;
473 double lambda = agent->envi->parameters.lambda;
475 for (i = 0; i < agent->m; i++)
479 case RIL_E_ACCUMULATE:
486 e[i] = gamma * lambda;
496 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
497 * notify ATS of its new decision.
498 * @param solver solver handle
499 * @param agent agent handle
500 * @param new_address the address which is to be used
501 * @param new_bw_in the new amount of inbound bandwidth set for this address
502 * @param new_bw_out the new amount of outbound bandwidth set for this address
503 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
506 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
507 struct RIL_Peer_Agent *agent,
508 struct ATS_Address *new_address,
509 unsigned long long new_bw_in,
510 unsigned long long new_bw_out,
513 int notify = GNUNET_NO;
515 LOG(GNUNET_ERROR_TYPE_DEBUG, "set_active_suggestion()\n");
518 if (agent->address_inuse != new_address)
520 if (NULL != agent->address_inuse)
522 agent->address_inuse->active = GNUNET_NO;
523 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
524 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
526 if (NULL != new_address)
528 LOG(GNUNET_ERROR_TYPE_DEBUG, "set address active: %s\n", agent->active ? "yes" : "no");
529 new_address->active = agent->active;
530 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
531 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
533 notify |= GNUNET_YES;
539 if (new_address->active != agent->active)
541 new_address->active = agent->active;
545 if (agent->bw_in != new_bw_in)
547 agent->bw_in = new_bw_in;
548 new_address->assigned_bw_in.value__ = htonl (new_bw_out);
549 notify |= GNUNET_YES;
551 if (agent->bw_out != new_bw_out)
553 agent->bw_out = new_bw_out;
554 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
555 notify |= GNUNET_YES;
559 if (notify && agent->active && (GNUNET_NO == silent))
563 solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
568 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
569 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
573 solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
574 agent->address_inuse);
577 agent->address_inuse = new_address;
581 * Allocates a state vector and fills it with the features present
582 * @param solver the solver handle
583 * @return pointer to the state vector
586 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
590 struct RIL_Network *net;
591 double *state = GNUNET_malloc (sizeof (double) * agent->m);
592 struct RIL_Address_Wrapped *cur_address;
593 const double *properties;
595 for (i = 0; i < solver->networks_count; i++)
597 net = &solver->network_entries[i];
598 state[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) net->bw_in_assigned;
599 state[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net->bw_in_available;
600 state[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) net->bw_out_assigned;
601 state[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net->bw_out_available;
604 i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
606 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
608 state[i++] = cur_address->address_naked->active;
609 state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
610 state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
611 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
612 cur_address->address_naked);
613 for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
615 state[i++] = properties[k];
623 * For all networks a peer has an address in, this gets the maximum bandwidth which could
624 * theoretically be available in one of the networks. This is used for bandwidth normalization.
625 * @param solver the solver handle
626 * @param agent the agent handle
627 * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
629 static long long unsigned
630 ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
633 * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
634 * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
637 struct RIL_Address_Wrapped *cur;
638 struct RIL_Network *net;
640 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
642 net = cur->address_naked->solver_information;
645 if (net->bw_in_available > max)
647 max = net->bw_in_available;
652 if (net->bw_out_available > max)
654 max = net->bw_out_available;
662 * Get the index of the quality-property in question
663 * @param type the quality property type
667 ril_find_property_index (uint32_t type)
669 int existing_types[] = GNUNET_ATS_QualityProperties;
671 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
672 if (existing_types[c] == type)
674 return GNUNET_SYSERR;
678 * Gets the reward of the last performed step
679 * @param solver solver handle
683 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
686 * Match the preferences of the peer with the current assignment.
688 const double *preferences;
689 const double *properties;
690 double pref_match = 0;
692 struct RIL_Network *net;
695 preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
697 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
698 agent->address_inuse);
699 prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
700 pref_match += preferences[GNUNET_ATS_PREFERENCE_LATENCY] * properties[prop_index];
701 bw_norm = GNUNET_MAX(2, (((
702 ((double) agent->bw_in / (double) ril_get_max_bw(agent, GNUNET_YES)) +
703 ((double) agent->bw_out / (double) ril_get_max_bw(agent, GNUNET_NO))
706 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm;
708 net = agent->address_inuse->solver_information;
709 if ((net->bw_in_assigned > net->bw_in_available) || net->bw_out_assigned > net->bw_out_available)
718 * Doubles the bandwidth for the active address
719 * @param solver solver handle
720 * @param agent agent handle
721 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
724 envi_action_bw_double (struct GAS_RIL_Handle *solver,
725 struct RIL_Peer_Agent *agent,
730 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2,
731 agent->bw_out, GNUNET_NO);
735 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
736 agent->bw_out * 2, GNUNET_NO);
741 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
742 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
743 * @param solver solver handle
744 * @param agent agent handle
745 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
749 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
750 struct RIL_Peer_Agent *agent,
753 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
754 unsigned long long new_bw;
758 new_bw = agent->bw_in / 2;
761 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
766 new_bw = agent->bw_out / 2;
769 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
775 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
776 * @param solver solver handle
777 * @param agent agent handle
778 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
782 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
784 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
788 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
789 agent->bw_out, GNUNET_NO);
793 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
794 agent->bw_out + (5 * min_bw), GNUNET_NO);
799 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
800 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
801 * @param solver solver handle
802 * @param agent agent handle
803 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
807 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
809 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
810 unsigned long long new_bw;
814 new_bw = agent->bw_in - (5 * min_bw);
817 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
822 new_bw = agent->bw_out - (5 * min_bw);
825 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
831 * Switches to the address given by its index
832 * @param solver solver handle
833 * @param agent agent handle
834 * @param address_index index of the address as it is saved in the agent's list, starting with zero
837 envi_action_address_switch (struct GAS_RIL_Handle *solver,
838 struct RIL_Peer_Agent *agent,
839 unsigned int address_index)
841 struct RIL_Address_Wrapped *cur;
844 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
846 if (i == address_index)
848 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
856 //no address with address_index exists, in this case this action should not be callable
857 GNUNET_assert(GNUNET_NO);
861 * Puts the action into effect by calling the according function
862 * @param solver solver handle
863 * @param action action to perform by the solver
866 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
868 unsigned int address_index;
872 case RIL_ACTION_NOTHING:
874 case RIL_ACTION_BW_IN_DBL:
875 envi_action_bw_double (solver, agent, GNUNET_YES);
877 case RIL_ACTION_BW_IN_HLV:
878 envi_action_bw_halven (solver, agent, GNUNET_YES);
880 case RIL_ACTION_BW_IN_INC:
881 envi_action_bw_inc (solver, agent, GNUNET_YES);
883 case RIL_ACTION_BW_IN_DEC:
884 envi_action_bw_dec (solver, agent, GNUNET_YES);
886 case RIL_ACTION_BW_OUT_DBL:
887 envi_action_bw_double (solver, agent, GNUNET_NO);
889 case RIL_ACTION_BW_OUT_HLV:
890 envi_action_bw_halven (solver, agent, GNUNET_NO);
892 case RIL_ACTION_BW_OUT_INC:
893 envi_action_bw_inc (solver, agent, GNUNET_NO);
895 case RIL_ACTION_BW_OUT_DEC:
896 envi_action_bw_dec (solver, agent, GNUNET_NO);
899 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
901 address_index = action - RIL_ACTION_TYPE_NUM;
903 GNUNET_assert(address_index >= 0);
905 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
907 envi_action_address_switch (solver, agent, address_index);
910 // error - action does not exist
911 GNUNET_assert(GNUNET_NO);
916 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
917 * after having done the last action a_old. It observes the new state s_next and the reward
918 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
919 * next action is put into effect.
920 * @param agent the agent performing the step
923 agent_step (struct RIL_Peer_Agent *agent)
925 int a_next = RIL_ACTION_INVALID;
929 s_next = envi_get_state (agent->envi, agent);
930 reward = envi_get_reward (agent->envi, agent);
932 LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
933 agent->envi->parameters.algorithm ? "Q" : "SARSA");
935 switch (agent->envi->parameters.algorithm)
938 agent_modify_eligibility (agent, RIL_E_SET);
939 if (agent_decide_exploration (agent))
941 a_next = agent_get_action_explore (agent, s_next);
945 a_next = agent_get_action_best (agent, s_next);
947 if (RIL_ACTION_INVALID != agent->a_old)
949 //updates weights with selected action (on-policy), if not first step
950 agent_update_weights (agent, reward, s_next, a_next);
955 a_next = agent_get_action_best (agent, s_next);
956 if (RIL_ACTION_INVALID != agent->a_old)
958 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
959 agent_update_weights (agent, reward, s_next, a_next);
961 if (agent_decide_exploration (agent))
963 a_next = agent_get_action_explore (agent, s_next);
964 agent_modify_eligibility (agent, RIL_E_ZERO);
968 a_next = agent_get_action_best (agent, s_next);
969 agent_modify_eligibility (agent, RIL_E_SET);
974 GNUNET_assert(RIL_ACTION_INVALID != a_next);
976 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
978 envi_do_action (agent->envi, agent, a_next);
980 GNUNET_free(agent->s_old);
981 agent->s_old = s_next;
982 agent->a_old = a_next;
984 agent->step_count += 1;
988 * Cycles through all agents and lets the active ones do a step. Schedules the next step.
989 * @param solver the solver handle
990 * @param tc task context for the scheduler
993 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
995 struct GAS_RIL_Handle *solver = cls;
996 struct RIL_Peer_Agent *cur;
998 LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
1000 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1002 if (cur->active && cur->address_inuse)
1008 solver->step_count += 1;
1009 solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, &ril_periodic_step, solver);
1013 * Initialize an agent without addresses and its knowledge base
1014 * @param s ril solver
1015 * @param peer the one in question
1016 * @return handle to the new agent
1018 static struct RIL_Peer_Agent *
1019 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1022 struct GAS_RIL_Handle * solver = s;
1023 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
1025 agent->envi = solver;
1026 agent->peer = *peer;
1027 agent->step_count = 0;
1028 agent->active = GNUNET_NO;
1029 agent->n = RIL_ACTION_TYPE_NUM;
1030 agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
1031 agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
1032 for (i = 0; i < agent->n; i++)
1034 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1036 agent->a_old = RIL_ACTION_INVALID;
1037 agent->s_old = envi_get_state (solver, agent);
1038 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1039 agent_modify_eligibility (agent, RIL_E_ZERO);
1041 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
1048 * @param s solver handle
1049 * @param agent the agent to retire
1052 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1056 for (i = 0; i < agent->n; i++)
1058 GNUNET_free(agent->W[i]);
1060 GNUNET_free(agent->W);
1061 GNUNET_free(agent->e);
1062 GNUNET_free(agent->s_old);
1067 * Returns the agent for a peer
1068 * @param s solver handle
1069 * @param peer identity of the peer
1070 * @param create whether to create an agent if none is allocated yet
1073 static struct RIL_Peer_Agent *
1074 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1076 struct RIL_Peer_Agent *cur;
1078 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1080 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1088 return agent_init (solver, peer);
1094 * Lookup network struct by type
1096 * @param s the solver handle
1097 * @param type the network type
1098 * @return the network struct
1100 static struct RIL_Network *
1101 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1105 for (i = 0; i < s->networks_count; i++)
1107 if (s->network_entries[i].type == type)
1109 return &s->network_entries[i];
1116 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1117 * considered inactive and not used. Addresses in an inactive network are ignored.
1118 * @param solver solver handle
1119 * @param network the network type
1123 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1125 struct RIL_Network *net;
1126 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1128 net = ril_get_network (solver, network);
1129 if (net->bw_out_available < min_bw)
1135 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1136 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1137 * frees the memory of the old vector, and redirects the pointer to the new one.
1138 * @param old pointer to the pointer to the first element of the vector
1139 * @param element_size byte size of the vector elements
1140 * @param hole_start the first element to cut out
1141 * @param hole_length the number of elements to cut out
1142 * @param old_length the length of the old vector
1145 ril_cut_from_vector (void **old,
1146 size_t element_size,
1147 unsigned int hole_start,
1148 unsigned int hole_length,
1149 unsigned int old_length)
1152 char *oldptr = (char *) *old;
1154 unsigned int bytes_before;
1155 unsigned int bytes_hole;
1156 unsigned int bytes_after;
1158 GNUNET_assert(old_length > hole_length);
1159 GNUNET_assert(old_length >= (hole_start + hole_length));
1161 size = element_size * (old_length - hole_length);
1163 bytes_before = element_size * hole_start;
1164 bytes_hole = element_size * hole_length;
1165 bytes_after = element_size * (old_length - hole_start - hole_length);
1173 tmpptr = GNUNET_malloc (size);
1174 memcpy (tmpptr, oldptr, bytes_before);
1175 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1181 *old = (void *) tmpptr;
1185 * Solver API functions
1186 * ---------------------------
1190 * Changes the preferences for a peer in the problem
1192 * @param solver the solver handle
1193 * @param peer the peer to change the preference for
1194 * @param kind the kind to change the preference
1195 * @param pref_rel the normalized preference value for this kind over all clients
1198 GAS_ril_address_change_preference (void *s,
1199 const struct GNUNET_PeerIdentity *peer,
1200 enum GNUNET_ATS_PreferenceKind kind,
1203 LOG(GNUNET_ERROR_TYPE_DEBUG,
1204 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1205 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1207 * Nothing to do here. Preferences are considered during reward calculation.
1213 libgnunet_plugin_ats_ril_init (void *cls)
1215 struct GNUNET_ATS_PluginEnvironment *env = cls;
1216 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1217 struct RIL_Network * cur;
1219 unsigned long long tmp;
1222 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1224 GNUNET_assert(NULL != env);
1225 GNUNET_assert(NULL != env->cfg);
1226 GNUNET_assert(NULL != env->stats);
1227 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1228 GNUNET_assert(NULL != env->get_preferences);
1229 GNUNET_assert(NULL != env->get_property);
1232 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
1234 solver->step_time = RIL_DEFAULT_STEP_TIME;
1236 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string)
1237 && NULL != string && 0 == strcmp (string, "SARSA"))
1239 solver->parameters.algorithm = RIL_ALGO_SARSA;
1243 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1246 == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
1248 solver->parameters.gamma = (double) tmp / 100;
1252 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
1255 == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
1257 solver->parameters.alpha = (double) tmp / 100;
1261 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1263 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_TRACE_DECAY", &tmp))
1265 solver->parameters.lambda = (double) tmp / 100;
1269 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1272 env->sf.s_add = &GAS_ril_address_add;
1273 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1274 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1275 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
1276 env->sf.s_address_update_network = &GAS_ril_address_change_network;
1277 env->sf.s_get = &GAS_ril_get_preferred_address;
1278 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
1279 env->sf.s_pref = &GAS_ril_address_change_preference;
1280 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
1281 env->sf.s_del = &GAS_ril_address_delete;
1282 env->sf.s_bulk_start = &GAS_ril_bulk_start;
1283 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
1285 solver->plugin_envi = env;
1286 solver->networks_count = env->network_count;
1287 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
1288 solver->step_count = 0;
1290 for (c = 0; c < env->network_count; c++)
1292 cur = &solver->network_entries[c];
1293 cur->type = env->networks[c];
1294 cur->bw_in_available = env->in_quota[c];
1295 cur->bw_in_assigned = 0;
1296 cur->bw_out_available = env->out_quota[c];
1297 cur->bw_out_assigned = 0;
1300 solver->next_step = GNUNET_SCHEDULER_add_delayed (
1301 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1302 &ril_periodic_step, solver);
1309 libgnunet_plugin_ats_ril_done (void *cls)
1311 struct GAS_RIL_Handle *s = cls;
1312 struct RIL_Peer_Agent *cur_agent;
1313 struct RIL_Peer_Agent *next_agent;
1315 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1317 cur_agent = s->agents_head;
1318 while (NULL != cur_agent)
1320 next_agent = cur_agent->next;
1321 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1322 agent_die (s, cur_agent);
1323 cur_agent = next_agent;
1326 GNUNET_SCHEDULER_cancel (s->next_step);
1327 GNUNET_free(s->network_entries);
1334 * Add a single address within a network to the solver
1336 * @param solver the solver Handle
1337 * @param address the address to add
1338 * @param network network type of this address
1341 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1343 struct GAS_RIL_Handle *s = solver;
1344 struct RIL_Peer_Agent *agent;
1345 struct RIL_Address_Wrapped *address_wrapped;
1346 struct RIL_Network *net;
1353 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1355 net = ril_get_network (s, network);
1356 address->solver_information = net;
1358 if (!ril_network_is_active (s, network))
1360 LOG(GNUNET_ERROR_TYPE_DEBUG,
1361 "API_address_add() Did not add %s address %p for peer '%s', network does not have enough bandwidth\n",
1362 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1366 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1369 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1370 address_wrapped->address_naked = address;
1371 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1373 //increase size of W
1374 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
1376 n_new = agent->n + 1;
1379 GNUNET_array_grow(agent->W, agent->n, n_new);
1380 for (i = 0; i < n_new; i++)
1385 GNUNET_array_grow(agent->W[i], agent->m, m_new);
1390 GNUNET_array_grow(agent->W[i], zero, m_new);
1394 //increase size of old state vector
1396 GNUNET_array_grow(agent->s_old, agent->m, m_new); //TODO random instead of zero-initialization of state features
1399 GNUNET_array_grow(agent->e, agent->m, m_new);
1401 if (NULL == agent->address_inuse)
1403 net->bw_in_assigned += min_bw;
1404 net->bw_out_assigned += min_bw;
1405 envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
1408 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %p for peer '%s'\n",
1409 address->active ? "active" : "inactive", address->plugin, address->addr,
1410 GNUNET_i2s (&address->peer));
1414 * Remove an address from the solver
1416 * @param solver the solver handle
1417 * @param address the address to remove
1418 * @param session_only delete only session not whole address
1421 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1423 struct GAS_RIL_Handle *s = solver;
1424 struct RIL_Peer_Agent *agent;
1425 struct RIL_Address_Wrapped *address_wrapped;
1426 int address_was_used = address->active;
1431 struct RIL_Network *net;
1432 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1434 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
1435 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
1436 address->addr, GNUNET_i2s (&address->peer));
1438 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1441 net = address->solver_information;
1442 GNUNET_assert(!ril_network_is_active (s, net->type));
1443 LOG(GNUNET_ERROR_TYPE_DEBUG,
1444 "No agent allocated for peer yet, since address was in inactive network\n");
1448 address_index = agent_address_get_index (agent, address);
1449 address_wrapped = agent_address_get (agent, address);
1451 if (NULL == address_wrapped)
1453 net = address->solver_information;
1454 GNUNET_assert(!ril_network_is_active (s, net->type));
1455 LOG(GNUNET_ERROR_TYPE_DEBUG,
1456 "Address not considered by agent, address was in inactive network\n");
1460 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1461 GNUNET_free(address_wrapped);
1464 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
1465 n_new = agent->n - 1;
1467 for (i = 0; i < agent->n; i++)
1469 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
1470 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1471 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1473 GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
1474 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
1476 //correct last action
1477 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1481 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1483 agent->a_old = RIL_ACTION_INVALID;
1485 //decrease old state vector and eligibility vector
1486 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
1487 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1488 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1489 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
1490 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1491 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1495 LOG(GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
1497 if (address_was_used)
1499 net = address->solver_information;
1500 net->bw_in_assigned -= agent->bw_in;
1501 net->bw_out_assigned -= agent->bw_out;
1503 if (NULL != agent->addresses_head) //if peer has an address left, use it
1505 LOG(GNUNET_ERROR_TYPE_DEBUG, "address left: %p\n",
1506 agent->addresses_head->address_naked->addr);
1507 //TODO? check if network/bandwidth update can be done more clever/elegant at different function
1508 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw,
1510 net = agent->addresses_head->address_naked->solver_information;
1511 net->bw_in_assigned -= min_bw;
1512 net->bw_out_assigned -= min_bw;
1516 LOG(GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
1518 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
1522 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
1526 * Transport properties for this address have changed
1528 * @param solver solver handle
1529 * @param address the address
1530 * @param type the ATSI type in HBO
1531 * @param abs_value the absolute value of the property
1532 * @param rel_value the normalized value
1535 GAS_ril_address_property_changed (void *solver,
1536 struct ATS_Address *address,
1541 LOG(GNUNET_ERROR_TYPE_DEBUG,
1542 "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1543 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1544 address->addr, rel_value);
1546 * Nothing to do here, properties are considered in every reward calculation
1551 * Transport session for this address has changed
1553 * NOTE: values in addresses are already updated
1555 * @param solver solver handle
1556 * @param address the address
1557 * @param cur_session the current session
1558 * @param new_session the new session
1561 GAS_ril_address_session_changed (void *solver,
1562 struct ATS_Address *address,
1563 uint32_t cur_session,
1564 uint32_t new_session)
1567 * TODO? Future Work: Potentially add session activity as a feature in state vector
1569 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1573 * Usage for this address has changed
1575 * NOTE: values in addresses are already updated
1577 * @param solver solver handle
1578 * @param address the address
1579 * @param in_use usage state
1582 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1584 /* Nothing to do here */
1585 LOG(GNUNET_ERROR_TYPE_DEBUG,
1586 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1587 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1591 * Network scope for this address has changed
1593 * NOTE: values in addresses are already updated
1595 * @param solver solver handle
1596 * @param address the address
1597 * @param current_network the current network
1598 * @param new_network the new network
1601 GAS_ril_address_change_network (void *solver,
1602 struct ATS_Address *address,
1603 uint32_t current_network,
1604 uint32_t new_network)
1606 struct GAS_RIL_Handle *s = solver;
1607 struct RIL_Peer_Agent *agent;
1608 struct RIL_Network *net;
1609 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1611 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
1612 "%s address of peer %s from '%s' to '%s'\n",
1613 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
1614 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
1616 if (address->active && !ril_network_is_active (solver, new_network))
1618 GAS_ril_address_delete (solver, address, GNUNET_NO);
1622 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1625 GNUNET_assert(!ril_network_is_active (solver, current_network));
1627 GAS_ril_address_add (s, address, new_network);
1631 net = ril_get_network (s, current_network);
1632 net->bw_in_assigned -= agent->bw_in;
1633 net->bw_out_assigned -= agent->bw_out;
1635 net = ril_get_network (s, new_network);
1636 net->bw_in_assigned -= min_bw;
1637 net->bw_out_assigned -= min_bw;
1638 address->solver_information = net;
1642 * Get application feedback for a peer
1644 * @param solver the solver handle
1645 * @param application the application
1646 * @param peer the peer to change the preference for
1647 * @param scope the time interval for this feedback: [now - scope .. now]
1648 * @param kind the kind to change the preference
1649 * @param score the score
1652 GAS_ril_address_preference_feedback (void *solver,
1654 const struct GNUNET_PeerIdentity *peer,
1655 const struct GNUNET_TIME_Relative scope,
1656 enum GNUNET_ATS_PreferenceKind kind,
1659 //TODO! collect feedback
1660 LOG(GNUNET_ERROR_TYPE_DEBUG,
1661 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1662 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1663 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1667 * Start a bulk operation
1669 * @param solver the solver
1672 GAS_ril_bulk_start (void *solver)
1675 * Since new calculations of the assignment are not triggered by a change of preferences, as it
1676 * happens in the proportional and the mlp solver, there is no need to block this solver.
1681 * Bulk operation done
1684 GAS_ril_bulk_stop (void *solver)
1687 * Since new calculations of the assignment are not triggered by a change of preferences, as it
1688 * happens in the proportional and the mlp solver, there is no need to block this solver.
1693 * Get the preferred address for a specific peer
1695 * @param solver the solver handle
1696 * @param peer the identity of the peer
1698 const struct ATS_Address *
1699 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1702 * activate agent, return currently chosen address
1704 struct GAS_RIL_Handle *s = solver;
1705 struct RIL_Peer_Agent *agent;
1707 agent = ril_get_agent (s, peer, GNUNET_YES);
1709 agent->active = GNUNET_YES;
1711 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
1714 if (agent->address_inuse)
1716 LOG(GNUNET_ERROR_TYPE_DEBUG,
1717 "API_get_preferred_address() Activated agent for peer '%s' with %s address %p\n",
1718 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
1722 LOG(GNUNET_ERROR_TYPE_DEBUG,
1723 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
1727 return agent->address_inuse;
1731 * Stop notifying about address and bandwidth changes for this peer
1733 * @param solver the solver handle
1734 * @param peer the peer
1737 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1739 struct GAS_RIL_Handle *s = solver;
1740 struct RIL_Peer_Agent *agent;
1742 agent = ril_get_agent (s, peer, GNUNET_NO);
1749 if (GNUNET_NO == agent->active)
1755 agent->active = GNUNET_NO;
1756 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
1759 LOG(GNUNET_ERROR_TYPE_DEBUG,
1760 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1761 GNUNET_i2s (peer), agent->address_inuse->plugin);
1764 /* end of libgnunet_plugin_ats_ril.c */