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/gnunet-service-ats-solver_ril.c
23 * @brief ATS reinforcement learning solver
24 * @author Fabian Oehlmann
25 * @author Matthias Wachs
29 #include "gnunet_util_lib.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet_statistics_service.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
35 #define RIL_ACTION_INVALID -1
36 #define RIL_FEATURES_ADDRESS_COUNT (3 + GNUNET_ATS_QualityPropertiesCount)
37 #define RIL_FEATURES_NETWORK_COUNT 4
39 #define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
40 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
41 #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
42 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
43 #define RIL_DEFAULT_TRACE_DECAY 0.6
44 #define RIL_EXPLORE_RATIO 0.1
47 * ATS reinforcement learning solver
53 * TODO! implement reward calculation 1 and 2 (i.e. meeting preferences and taking scores)
58 RIL_ACTION_NOTHING = 0,
59 RIL_ACTION_BW_IN_DBL = 1,
60 RIL_ACTION_BW_IN_HLV = 2,
61 RIL_ACTION_BW_IN_INC = 3,
62 RIL_ACTION_BW_IN_DEC = 4,
63 RIL_ACTION_BW_OUT_DBL = 5,
64 RIL_ACTION_BW_OUT_HLV = 6,
65 RIL_ACTION_BW_OUT_INC = 7,
66 RIL_ACTION_BW_OUT_DEC = 8,
67 RIL_ACTION_TYPE_NUM = 9
76 enum RIL_E_Modification
85 * Global learning parameters
87 struct RIL_Learning_Parameters
90 * The TD-algorithm to use
92 enum RIL_Algorithm algorithm;
95 * Learning discount factor in the TD-update
100 * Gradient-descent step-size
105 * Trace-decay factor for eligibility traces
111 * Wrapper for addresses to store them in agent's linked list
113 struct RIL_Address_Wrapped
118 struct RIL_Address_Wrapped *next;
123 struct RIL_Address_Wrapped *prev;
128 struct ATS_Address *address_naked;
131 struct RIL_Peer_Agent
134 * Next agent in solver's linked list
136 struct RIL_Peer_Agent *next;
139 * Previous agent in solver's linked list
141 struct RIL_Peer_Agent *prev;
146 struct GAS_RIL_Handle *envi;
151 struct GNUNET_PeerIdentity peer;
154 * Whether the agent is active or not
159 * Number of performed time-steps
161 unsigned long long step_count;
164 * Experience matrix W
169 * Number of rows of W / Number of state-vector features
174 * Number of columns of W / Number of actions
179 * Last perceived state feature vector
189 * Eligibility trace vector
196 struct ATS_Address * address_inuse;
199 * Head of addresses DLL
201 struct RIL_Address_Wrapped * addresses_head;
204 * Tail of addresses DLL
206 struct RIL_Address_Wrapped * addresses_tail;
209 * Inbound bandwidth assigned by the agent
211 unsigned long long bw_in;
214 * Outbound bandwidth assigned by the agent
216 unsigned long long bw_out;
224 enum GNUNET_ATS_Network_Type type;
227 * Total available inbound bandwidth
229 unsigned long long bw_in_available;
232 * Total assigned outbound bandwidth
234 unsigned long long bw_in_assigned;
237 * Total available outbound bandwidth
239 unsigned long long bw_out_available;
242 * Total assigned outbound bandwidth
244 unsigned long long bw_out_assigned;
250 * Bandwidth changed callback
252 GAS_bandwidth_changed_cb bw_changed;
255 * Bandwidth changed callback cls
257 void *bw_changed_cls;
260 * ATS function to get preferences
262 GAS_get_preferences get_preferences;
265 * Closure for ATS function to get preferences
267 void *get_preferences_cls;
270 * ATS function to get properties
272 GAS_get_properties get_properties;
275 * Closure for ATS function to get properties
277 void *get_properties_cls;
281 * A handle for the reinforcement learning solver
283 struct GAS_RIL_Handle
288 struct GNUNET_STATISTICS_Handle *stats;
291 * Hashmap containing all valid addresses
293 const struct GNUNET_CONTAINER_MultiHashMap *addresses;
296 * Callbacks for the solver
298 struct RIL_Callbacks *callbacks;
306 * Number of changes while solver was locked
311 * Number of performed time-steps
313 unsigned long long step_count;
316 * Interval time between steps in milliseconds //TODO? put in agent
318 struct GNUNET_TIME_Relative step_time;
321 * Task identifier of the next time-step to be executed //TODO? put in agent
323 GNUNET_SCHEDULER_TaskIdentifier next_step;
326 * Learning parameters
328 struct RIL_Learning_Parameters parameters;
331 * Array of networks with global assignment state
333 struct RIL_Network * network_entries;
338 unsigned int networks_count;
341 * List of active peer-agents
343 struct RIL_Peer_Agent * agents_head;
344 struct RIL_Peer_Agent * agents_tail;
349 * ---------------------------
353 * Estimate the current action-value for state s and action a
354 * @param agent agent performing the estimation
357 * @return estimation value
360 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
365 for (i = 0; i < agent->m; i++)
367 result += state[i] * agent->W[action][i];
374 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
375 * currently estimated best action) in the current step
376 * @param agent agent performing the step
377 * @return yes, if exploring
380 agent_decide_exploration (struct RIL_Peer_Agent *agent)
382 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
383 UINT32_MAX) / (double) UINT32_MAX;
385 if (r < RIL_EXPLORE_RATIO)
393 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
396 struct RIL_Address_Wrapped *cur;
399 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
401 if (cur->address_naked == address)
411 static struct RIL_Address_Wrapped *
412 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
414 struct RIL_Address_Wrapped *cur;
416 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
418 if (cur->address_naked == address)
428 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
429 * most reward in the future)
430 * @param agent agent performing the calculation
431 * @param state the state from which to take the action
432 * @return the action promising most future reward
435 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
438 int max_i = RIL_ACTION_INVALID;
440 double max_q = -DBL_MAX;
442 for (i = 0; i < agent->n; i++)
444 cur_q = agent_estimate_q (agent, state, i);
452 GNUNET_assert(RIL_ACTION_INVALID != max_i);
458 * Gets any action, to explore the action space from that state
459 * @param agent agent performing the calculation
460 * @param state the state from which to take the action
464 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
466 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
470 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
471 * @param agent the agent performing the update
472 * @param reward the reward received for the last action
473 * @param s_next the new state, the last step got the agent into
474 * @param a_prime the new
477 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
481 double *theta = agent->W[agent->a_old];
483 delta = reward + agent_estimate_q (agent, s_next, a_prime)
484 - agent_estimate_q (agent, agent->s_old, agent->a_old);
485 for (i = 0; i < agent->m; i++)
487 theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
492 * Changes the eligibility trace vector e in various manners:
493 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
494 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
495 * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
496 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
501 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
504 double *e = agent->e;
505 double gamma = agent->envi->parameters.gamma;
506 double lambda = agent->envi->parameters.lambda;
508 for (i = 0; i < agent->m; i++)
512 case RIL_E_ACCUMULATE:
519 e[i] = gamma * lambda;
529 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
530 struct RIL_Peer_Agent *agent,
531 struct ATS_Address *new_address,
532 unsigned long long new_bw_in,
533 unsigned long long new_bw_out)
535 int notify = GNUNET_NO;
537 if (agent->address_inuse != new_address)
539 if (NULL != agent->address_inuse)
541 agent->address_inuse->active = GNUNET_NO;
543 agent->address_inuse = new_address;
546 agent->address_inuse->active = GNUNET_YES;
548 agent->address_inuse->assigned_bw_in.value__ = htonl (agent->bw_in);
549 agent->address_inuse->assigned_bw_out.value__ = htonl (agent->bw_out);
550 notify |= GNUNET_YES;
553 if (agent->bw_in != new_bw_in)
555 agent->bw_in = new_bw_in;
556 agent->address_inuse->assigned_bw_in.value__ = htonl (new_bw_out);
557 notify |= GNUNET_YES;
559 if (agent->bw_out != new_bw_out)
561 agent->bw_out = new_bw_out;
562 agent->address_inuse->assigned_bw_out.value__ = htonl (new_bw_out);
563 notify |= GNUNET_YES;
566 if (notify && agent->active)
568 solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, agent->address_inuse);
573 * Allocates a state vector and fills it with the features present
574 * @param solver the solver handle
575 * @return pointer to the state vector
578 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
582 struct RIL_Network *net;
583 double *state = GNUNET_malloc (sizeof (double) * agent->m);
584 struct RIL_Address_Wrapped *cur_address;
585 const double *properties;
587 for (i = 0; i < solver->networks_count; i++)
589 net = &solver->network_entries[i];
590 state[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) net->bw_in_assigned;
591 state[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net->bw_in_available;
592 state[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) net->bw_out_assigned;
593 state[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net->bw_out_available;
596 i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
598 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
600 state[i++] = cur_address->address_naked->active;
601 state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
602 state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
603 properties = solver->callbacks->get_properties (solver->callbacks->get_properties_cls, cur_address->address_naked);
604 for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
606 state[i++] = properties[k];
614 * Gets the reward of the last performed step
615 * @param solver solver handle
619 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
621 //TODO! implement reward calculation
623 return (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX)
624 / (double) UINT32_MAX;
628 envi_action_bw_double (struct GAS_RIL_Handle *solver,
629 struct RIL_Peer_Agent *agent,
634 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2,
639 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
645 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
646 struct RIL_Peer_Agent *agent,
649 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
650 unsigned long long new_bw;
654 new_bw = agent->bw_in / 2;
657 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out);
661 new_bw = agent->bw_out / 2;
664 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw);
669 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
671 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
675 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
680 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
681 agent->bw_out + (5 * min_bw));
686 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
688 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
689 unsigned long long new_bw;
693 new_bw = agent->bw_in - (5 * min_bw);
696 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out);
700 new_bw = agent->bw_out - (5 * min_bw);
703 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw);
708 envi_action_address_switch (struct GAS_RIL_Handle *solver,
709 struct RIL_Peer_Agent *agent,
710 unsigned int address_index)
712 struct RIL_Address_Wrapped *cur;
715 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
717 if (i == address_index)
719 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out);
726 //no address with address_index exists
727 GNUNET_assert(GNUNET_NO);
731 * Puts the action into effect
732 * @param solver solver handle
733 * @param action action to perform by the solver
736 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
738 unsigned int address_index;
742 case RIL_ACTION_NOTHING:
744 case RIL_ACTION_BW_IN_DBL:
745 envi_action_bw_double (solver, agent, GNUNET_YES);
747 case RIL_ACTION_BW_IN_HLV:
748 envi_action_bw_halven (solver, agent, GNUNET_YES);
750 case RIL_ACTION_BW_IN_INC:
751 envi_action_bw_inc (solver, agent, GNUNET_YES);
753 case RIL_ACTION_BW_IN_DEC:
754 envi_action_bw_dec (solver, agent, GNUNET_YES);
756 case RIL_ACTION_BW_OUT_DBL:
757 envi_action_bw_double (solver, agent, GNUNET_NO);
759 case RIL_ACTION_BW_OUT_HLV:
760 envi_action_bw_halven (solver, agent, GNUNET_NO);
762 case RIL_ACTION_BW_OUT_INC:
763 envi_action_bw_inc (solver, agent, GNUNET_NO);
765 case RIL_ACTION_BW_OUT_DEC:
766 envi_action_bw_dec (solver, agent, GNUNET_NO);
769 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n))
771 address_index = agent->n - RIL_ACTION_TYPE_NUM;
773 GNUNET_assert(address_index >= 0);
775 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
777 envi_action_address_switch (solver, agent, address_index);
780 // error - action does not exist
781 GNUNET_assert(GNUNET_NO);
786 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
787 * after having done the last action a_old. It observes the new state s_next and the reward
788 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
789 * next action is put into effect.
790 * @param agent the agent performing the step
793 agent_step (struct RIL_Peer_Agent *agent)
795 int a_next = RIL_ACTION_INVALID;
799 s_next = envi_get_state (agent->envi, agent);
800 reward = envi_get_reward (agent->envi, agent);
802 LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
803 agent->envi->parameters.algorithm ? "Q" : "SARSA");
805 switch (agent->envi->parameters.algorithm)
808 agent_modify_eligibility (agent, RIL_E_SET);
809 if (agent_decide_exploration (agent))
811 a_next = agent_get_action_explore (agent, s_next);
815 a_next = agent_get_action_best (agent, s_next);
817 if (RIL_ACTION_INVALID != agent->a_old)
819 //updates weights with selected action (on-policy), if not first step
820 agent_update_weights (agent, reward, s_next, a_next);
825 a_next = agent_get_action_best (agent, s_next);
826 if (RIL_ACTION_INVALID != agent->a_old)
828 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
829 agent_update_weights (agent, reward, s_next, a_next);
831 if (agent_decide_exploration (agent))
833 a_next = agent_get_action_explore (agent, s_next);
834 agent_modify_eligibility (agent, RIL_E_ZERO);
838 a_next = agent_get_action_best (agent, s_next);
839 agent_modify_eligibility (agent, RIL_E_SET);
844 GNUNET_assert(RIL_ACTION_INVALID != a_next);
846 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
848 envi_do_action (agent->envi, agent, a_next);
850 GNUNET_free(agent->s_old);
851 agent->s_old = s_next;
852 agent->a_old = a_next;
854 agent->step_count += 1;
858 * Cycles through all agents and lets the active ones do a step. Schedules the next step.
859 * @param solver the solver handle
860 * @param tc task context for the scheduler
863 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
865 struct GAS_RIL_Handle *solver = cls;
866 struct RIL_Peer_Agent *cur;
868 LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
870 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
878 solver->step_count += 1;
879 solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, &ril_periodic_step, solver);
883 * Initialize an agent without addresses and its knowledge base
884 * @param s ril solver
885 * @param peer the one in question
886 * @return handle to the new agent
888 static struct RIL_Peer_Agent *
889 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
892 struct GAS_RIL_Handle * solver = s;
893 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
895 agent->envi = solver;
897 agent->step_count = 0;
898 agent->active = GNUNET_NO;
899 agent->n = RIL_ACTION_TYPE_NUM;
900 agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
901 agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
902 for (i = 0; i < agent->n; i++)
904 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
906 agent->a_old = RIL_ACTION_INVALID;
907 agent->s_old = envi_get_state (solver, agent);
908 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
909 agent_modify_eligibility (agent, RIL_E_ZERO);
911 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
918 * @param s solver handle
919 * @param agent the agent to retire
922 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
926 for (i = 0; i < agent->n; i++)
928 GNUNET_free(agent->W[i]);
930 GNUNET_free(agent->W);
931 GNUNET_free(agent->e);
932 GNUNET_free(agent->s_old);
937 * Returns the agent for a peer
938 * @param s solver handle
939 * @param peer identity of the peer
940 * @param create whether to create an agent if none is allocated yet
943 static struct RIL_Peer_Agent *
944 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
946 struct RIL_Peer_Agent *cur;
948 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
950 if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey))
958 return agent_init (solver, peer);
964 * Lookup network struct by type
966 * @param s the solver handle
967 * @param type the network type
968 * @return the network struct
970 static struct RIL_Network *
971 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
975 for (i = 0; i < s->networks_count; i++)
977 if (s->network_entries[i].type == type)
979 return &s->network_entries[i];
986 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
988 struct RIL_Network *net;
989 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
991 net = ril_get_network (solver, network);
992 if (net->bw_out_available < min_bw)
998 ril_cut_from_vector (void **old,
1000 unsigned int hole_start,
1001 unsigned int hole_length,
1002 unsigned int old_length)
1005 char *oldptr = (char *) *old;
1007 unsigned int bytes_before;
1008 unsigned int bytes_hole;
1009 unsigned int bytes_after;
1012 GNUNET_assert(old_length > hole_length);
1013 GNUNET_assert(old_length >= (hole_start + hole_length));
1015 size = element_size * (old_length - hole_length);
1017 bytes_before = element_size * hole_start;
1018 bytes_hole = element_size * hole_length;
1019 bytes_after = element_size * (old_length - hole_start - hole_length);
1027 // LOG(GNUNET_ERROR_TYPE_DEBUG, "hole_start = %d, hole_length = %d, old_length = %d\n", hole_start, hole_length, old_length);
1028 // LOG(GNUNET_ERROR_TYPE_DEBUG, "bytes_before = %d, bytes_hole = %d, bytes_after = %d\n", bytes_before, bytes_hole, bytes_after);
1029 // LOG(GNUNET_ERROR_TYPE_DEBUG, "element_size = %d, bytes_old = %d, bytes_new = %d\n", element_size, old_length * element_size, size);
1031 tmpptr = GNUNET_malloc (size);
1032 // LOG(GNUNET_ERROR_TYPE_DEBUG, "first\n");
1033 memcpy (tmpptr, oldptr, bytes_before);
1034 // LOG(GNUNET_ERROR_TYPE_DEBUG, "second\n");
1035 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1041 *old = (void *) tmpptr;
1045 * Solver API functions
1046 * ---------------------------
1050 * Changes the preferences for a peer in the problem
1052 * @param solver the solver handle
1053 * @param peer the peer to change the preference for
1054 * @param kind the kind to change the preference
1055 * @param pref_rel the normalized preference value for this kind over all clients
1058 GAS_ril_address_change_preference (void *s,
1059 const struct GNUNET_PeerIdentity *peer,
1060 enum GNUNET_ATS_PreferenceKind kind,
1063 LOG(GNUNET_ERROR_TYPE_DEBUG,
1064 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1065 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1067 * Nothing to do here. Preferences are considered during reward calculation.
1072 * Init the reinforcement learning problem solver
1075 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1076 * out_quota[i] contains outbound quota for network type i
1077 * in_quota[i] contains inbound quota for network type i
1080 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1081 * network[2] == GNUNET_ATS_NET_LAN
1082 * out_quota[2] == 65353
1083 * in_quota[2] == 65353
1085 * @param cfg configuration handle
1086 * @param stats the GNUNET_STATISTICS handle
1087 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1088 * @param addresses hashmap containing all addresses
1089 * @param out_quota array of outbound quotas
1090 * @param in_quota array of outbound quota
1091 * @param dest_length array length for quota arrays
1092 * @param bw_changed_cb callback for changed bandwidth amounts
1093 * @param bw_changed_cb_cls cls for callback
1094 * @param get_preference callback to get relative preferences for a peer
1095 * @param get_preference_cls cls for callback to get relative preferences
1096 * @param get_properties_cls for callback to get relative properties
1097 * @param get_properties_cls cls for callback to get relative properties
1098 * @return handle for the solver on success, NULL on fail
1101 GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1102 const struct GNUNET_STATISTICS_Handle *stats,
1103 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1105 unsigned long long *out_quota,
1106 unsigned long long *in_quota,
1108 GAS_bandwidth_changed_cb bw_changed_cb,
1109 void *bw_changed_cb_cls,
1110 GAS_get_preferences get_preference,
1111 void *get_preference_cls,
1112 GAS_get_properties get_properties,
1113 void *get_properties_cls)
1116 unsigned long long tmp;
1118 struct RIL_Network * cur;
1119 struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle));
1121 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1123 GNUNET_assert(NULL != cfg);
1124 GNUNET_assert(NULL != stats);
1125 GNUNET_assert(NULL != network);
1126 GNUNET_assert(NULL != bw_changed_cb);
1127 GNUNET_assert(NULL != get_preference);
1128 GNUNET_assert(NULL != get_properties);
1131 != GNUNET_CONFIGURATION_get_value_time (cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
1133 solver->step_time = RIL_DEFAULT_STEP_TIME;
1135 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "RIL_ALGORITHM", &string)
1136 && NULL != string && 0 == strcmp (string, "SARSA"))
1138 solver->parameters.algorithm = RIL_ALGO_SARSA;
1142 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1144 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
1146 solver->parameters.gamma = (double) tmp / 100;
1150 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
1152 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
1154 solver->parameters.alpha = (double) tmp / 100;
1158 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1160 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_TRACE_DECAY", &tmp))
1162 solver->parameters.lambda = (double) tmp / 100;
1166 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1169 solver->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1170 solver->callbacks = GNUNET_malloc (sizeof (struct RIL_Callbacks));
1171 solver->callbacks->bw_changed = bw_changed_cb;
1172 solver->callbacks->bw_changed_cls = bw_changed_cb_cls;
1173 solver->callbacks->get_preferences = get_preference;
1174 solver->callbacks->get_preferences_cls = get_preference_cls;
1175 solver->callbacks->get_properties = get_properties;
1176 solver->callbacks->get_properties_cls = get_properties_cls;
1177 solver->networks_count = dest_length;
1178 solver->network_entries = GNUNET_malloc (dest_length * sizeof (struct RIL_Network));
1179 solver->bulk_lock = GNUNET_NO;
1180 solver->addresses = addresses;
1181 solver->step_count = 0;
1183 for (c = 0; c < dest_length; c++)
1185 cur = &solver->network_entries[c];
1186 cur->type = network[c];
1187 cur->bw_in_available = in_quota[c];
1188 cur->bw_in_assigned = 0;
1189 cur->bw_out_available = out_quota[c];
1190 cur->bw_out_assigned = 0;
1193 solver->next_step = GNUNET_SCHEDULER_add_delayed (
1194 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1195 &ril_periodic_step, solver);
1201 * Shutdown the reinforcement learning problem solver
1203 * @param solver the respective handle to shutdown
1206 GAS_ril_done (void * solver)
1208 struct GAS_RIL_Handle *s = solver;
1209 struct RIL_Peer_Agent *cur_agent;
1210 struct RIL_Peer_Agent *next_agent;
1212 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1214 cur_agent = s->agents_head;
1215 while (NULL != cur_agent)
1217 next_agent = cur_agent->next;
1218 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1219 agent_die (s, cur_agent);
1220 cur_agent = next_agent;
1223 GNUNET_SCHEDULER_cancel (s->next_step);
1224 GNUNET_free(s->callbacks);
1225 GNUNET_free(s->network_entries);
1230 * Add a single address within a network to the solver
1232 * @param solver the solver Handle
1233 * @param address the address to add
1234 * @param network network type of this address
1237 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1239 struct GAS_RIL_Handle *s = solver;
1240 struct RIL_Peer_Agent *agent;
1241 struct RIL_Address_Wrapped *address_wrapped;
1248 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1250 address->solver_information = ril_get_network (s, network);
1252 if (!ril_network_is_active (s, network))
1254 LOG(GNUNET_ERROR_TYPE_DEBUG,
1255 "API_address_add() Did not add %s address %p for peer '%s', network does not have enough bandwidth\n",
1256 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1260 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1263 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1264 address_wrapped->address_naked = address;
1265 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1267 //increase size of W
1268 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
1270 n_new = agent->n + 1;
1273 GNUNET_array_grow(agent->W, agent->n, n_new);
1274 for (i = 0; i < n_new; i++)
1279 GNUNET_array_grow(agent->W[i], agent->m, m_new);
1284 GNUNET_array_grow(agent->W[i], zero, m_new);
1288 //increase size of old state vector
1290 GNUNET_array_grow(agent->s_old, agent->m, m_new); //TODO initialize new state features?
1293 GNUNET_array_grow(agent->e, agent->m, m_new);
1295 if (NULL == agent->address_inuse)
1297 envi_set_active_suggestion (s, agent, address, min_bw, min_bw);
1300 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s address %p for peer '%s'\n",
1301 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1305 * Remove an address from the solver
1307 * @param solver the solver handle
1308 * @param address the address to remove
1309 * @param session_only delete only session not whole address
1312 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1314 //TODO! delete session only
1315 struct GAS_RIL_Handle *s = solver;
1316 struct RIL_Peer_Agent *agent;
1317 struct RIL_Address_Wrapped *address_wrapped;
1318 int address_was_used = address->active;
1323 struct RIL_Network *net;
1324 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1326 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
1327 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
1328 address->addr, GNUNET_i2s (&address->peer));
1330 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1333 net = address->solver_information;
1334 GNUNET_assert(!ril_network_is_active (s, net->type));
1335 LOG(GNUNET_ERROR_TYPE_DEBUG,
1336 "No agent allocated for peer yet, since address was in inactive network\n");
1340 address_index = agent_address_get_index (agent, address);
1341 address_wrapped = agent_address_get (agent, address);
1343 if (NULL == address_wrapped)
1345 net = address->solver_information;
1346 GNUNET_assert(!ril_network_is_active (s, net->type));
1347 LOG(GNUNET_ERROR_TYPE_DEBUG,
1348 "Address not considered by agent, address was in inactive network\n");
1352 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1353 GNUNET_free(address_wrapped);
1356 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
1357 n_new = agent->n - 1;
1359 for (i = 0; i < agent->n; i++)
1361 // LOG (GNUNET_ERROR_TYPE_DEBUG, "first - cut vectors in W\n");
1362 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
1363 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT) + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1365 // LOG (GNUNET_ERROR_TYPE_DEBUG, "second - cut action vector out of W\n");
1366 GNUNET_free (agent->W[RIL_ACTION_TYPE_NUM + address_index]);
1367 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
1369 //correct last action
1370 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1374 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1376 agent->a_old = RIL_ACTION_INVALID;
1378 //decrease old state vector and eligibility vector
1379 // LOG (GNUNET_ERROR_TYPE_DEBUG, "third - cut state vector\n");
1380 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
1381 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT) + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1382 // LOG (GNUNET_ERROR_TYPE_DEBUG, "fourth - cut eligibility vector\n");
1383 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
1384 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT) + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1388 if (address_was_used)
1390 net = address->solver_information;
1391 net->bw_in_assigned -= agent->bw_in;
1392 net->bw_out_assigned -= agent->bw_out;
1394 if (NULL != agent->addresses_head) //if peer has an address left, use it
1396 //TODO? check if network/bandwidth update can be done more clever/elegant at different function
1397 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw);
1398 net = agent->addresses_head->address_naked->solver_information;
1399 net->bw_in_assigned -= min_bw;
1400 net->bw_out_assigned -= min_bw;
1404 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
1408 * Transport properties for this address have changed
1410 * @param solver solver handle
1411 * @param address the address
1412 * @param type the ATSI type in HBO
1413 * @param abs_value the absolute value of the property
1414 * @param rel_value the normalized value
1417 GAS_ril_address_property_changed (void *solver,
1418 struct ATS_Address *address,
1423 LOG(GNUNET_ERROR_TYPE_DEBUG,
1424 "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1425 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1426 address->addr, rel_value);
1428 * Nothing to do here, properties are considered in every reward calculation
1433 * Transport session for this address has changed
1435 * NOTE: values in addresses are already updated
1437 * @param solver solver handle
1438 * @param address the address
1439 * @param cur_session the current session
1440 * @param new_session the new session
1443 GAS_ril_address_session_changed (void *solver,
1444 struct ATS_Address *address,
1445 uint32_t cur_session,
1446 uint32_t new_session)
1448 //TODO? consider session changed in solver behaviour
1450 * Potentially add session activity as a feature in state vector
1452 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1456 * Usage for this address has changed
1458 * NOTE: values in addresses are already updated
1460 * @param solver solver handle
1461 * @param address the address
1462 * @param in_use usage state
1465 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1467 //TODO! consider address_inuse_changed according to matthias' email
1469 * See matthias' email
1471 LOG(GNUNET_ERROR_TYPE_DEBUG,
1472 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1473 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1477 * Network scope for this address has changed
1479 * NOTE: values in addresses are already updated
1481 * @param solver solver handle
1482 * @param address the address
1483 * @param current_network the current network
1484 * @param new_network the new network
1487 GAS_ril_address_change_network (void *solver,
1488 struct ATS_Address *address,
1489 uint32_t current_network,
1490 uint32_t new_network)
1492 struct GAS_RIL_Handle *s = solver;
1493 struct RIL_Peer_Agent *agent;
1494 struct RIL_Network *net;
1495 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1497 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
1498 "%s address of peer %s from '%s' to '%s'\n",
1499 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
1500 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
1502 if (address->active && !ril_network_is_active (solver, new_network))
1504 GAS_ril_address_delete (solver, address, GNUNET_NO);
1508 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1511 //no agent there yet, so add as if address is new
1512 address->solver_information = ril_get_network (s, new_network);
1513 GAS_ril_address_add (s, address, new_network);
1517 net = ril_get_network (s, current_network);
1518 net->bw_in_assigned -= agent->bw_in;
1519 net->bw_out_assigned -= agent->bw_out;
1521 net = ril_get_network (s, new_network);
1522 net->bw_in_assigned -= min_bw;
1523 net->bw_out_assigned -= min_bw;
1524 address->solver_information = net;
1528 * Get application feedback for a peer
1530 * @param solver the solver handle
1531 * @param application the application
1532 * @param peer the peer to change the preference for
1533 * @param scope the time interval for this feedback: [now - scope .. now]
1534 * @param kind the kind to change the preference
1535 * @param score the score
1538 GAS_ril_address_preference_feedback (void *solver,
1540 const struct GNUNET_PeerIdentity *peer,
1541 const struct GNUNET_TIME_Relative scope,
1542 enum GNUNET_ATS_PreferenceKind kind,
1545 //TODO! collect reward until next reward calculation
1546 LOG(GNUNET_ERROR_TYPE_DEBUG,
1547 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1548 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1549 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1553 * Start a bulk operation
1555 * @param solver the solver
1558 GAS_ril_bulk_start (void *solver)
1560 //TODO? consideration: keep bulk counter and stop agents during bulk
1562 * bulk counter up, but not really relevant, because there is no complete calculation of the
1563 * bandwidth assignment triggered anyway. Therefore, changes to addresses can come and go as
1564 * they want. Consideration: Step-pause during bulk-start-stop period...
1567 //LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n");
1571 * Bulk operation done
1574 GAS_ril_bulk_stop (void *solver)
1576 //TODO? consideration: keep bulk counter and stop agents during bulk
1578 * bulk counter down, see bulk_start()
1581 //LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n");
1585 * Get the preferred address for a specific peer
1587 * @param solver the solver handle
1588 * @param peer the identity of the peer
1590 const struct ATS_Address *
1591 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1594 * activate agent, return currently chosen address
1596 struct GAS_RIL_Handle *s = solver;
1597 struct RIL_Peer_Agent *agent;
1599 agent = ril_get_agent (s, peer, GNUNET_NO);
1603 LOG(GNUNET_ERROR_TYPE_DEBUG,
1604 "API_get_preferred_address() No agent for peer '%s' do not suggest address\n",
1609 agent->active = GNUNET_YES;
1611 GNUNET_assert(NULL != agent->address_inuse);
1613 LOG(GNUNET_ERROR_TYPE_DEBUG,
1614 "API_get_preferred_address() Activated agent for peer '%s' with %s address\n",
1615 GNUNET_i2s (peer), agent->address_inuse->plugin);
1617 return agent->address_inuse;
1621 * Stop notifying about address and bandwidth changes for this peer
1623 * @param solver the solver handle
1624 * @param peer the peer
1627 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1629 struct GAS_RIL_Handle *s = solver;
1630 struct RIL_Peer_Agent *agent;
1632 agent = ril_get_agent (s, peer, GNUNET_NO);
1633 agent->active = GNUNET_NO;
1635 LOG(GNUNET_ERROR_TYPE_DEBUG,
1636 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1637 GNUNET_i2s (peer), agent->address_inuse->plugin);
1640 /* end of gnunet-service-ats-solver_ril.c */