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
37 #define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
38 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
39 #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
40 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
41 #define RIL_DEFAULT_TRACE_DECAY 0.6
42 #define RIL_EXPLORE_RATIO 0.1
45 * ATS reinforcement learning solver
51 * TODO! implement reward calculation 1 and 2 (i.e. meeting preferences and taking scores)
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
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;
248 * Bandwidth changed callback
250 GAS_bandwidth_changed_cb bw_changed;
253 * Bandwidth changed callback cls
255 void *bw_changed_cls;
258 * ATS function to get preferences
260 GAS_get_preferences get_preferences;
263 * Closure for ATS function to get preferences
265 void *get_preferences_cls;
268 * ATS function to get properties
270 GAS_get_properties get_properties;
273 * Closure for ATS function to get properties
275 void *get_properties_cls;
279 * A handle for the reinforcement learning solver
281 struct GAS_RIL_Handle
286 struct GNUNET_STATISTICS_Handle *stats;
289 * Hashmap containing all valid addresses
291 const struct GNUNET_CONTAINER_MultiHashMap *addresses;
294 * Callbacks for the solver
296 struct RIL_Callbacks *callbacks;
304 * Number of changes while solver was locked
309 * Number of performed time-steps
311 unsigned long long step_count;
314 * Interval time between steps in milliseconds //TODO? put in agent
316 struct GNUNET_TIME_Relative step_time;
319 * Task identifier of the next time-step to be executed //TODO? put in agent
321 GNUNET_SCHEDULER_TaskIdentifier next_step;
324 * Learning parameters
326 struct RIL_Learning_Parameters parameters;
329 * Array of networks with global assignment state
331 struct RIL_Network * network_entries;
336 unsigned int networks_count;
339 * List of active peer-agents
341 struct RIL_Peer_Agent * agents_head;
342 struct RIL_Peer_Agent * agents_tail;
347 * ---------------------------
351 * Estimate the current action-value for state s and action a
352 * @param agent agent performing the estimation
355 * @return estimation value
358 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
363 for (i = 0; i < agent->m; i++)
365 result += state[i] * agent->W[action][i];
372 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
373 * currently estimated best action) in the current step
374 * @param agent agent performing the step
375 * @return yes, if exploring
378 agent_decide_exploration (struct RIL_Peer_Agent *agent)
380 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
381 UINT32_MAX) / (double) UINT32_MAX;
383 if (r < RIL_EXPLORE_RATIO)
391 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
394 struct RIL_Address_Wrapped *cur;
397 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
399 if (cur->address_naked == address)
409 static struct RIL_Address_Wrapped *
410 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
412 struct RIL_Address_Wrapped *cur;
414 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
416 if (cur->address_naked == address)
426 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
427 * most reward in the future)
428 * @param agent agent performing the calculation
429 * @param state the state from which to take the action
430 * @return the action promising most future reward
433 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
436 int max_i = RIL_ACTION_INVALID;
438 double max_q = -DBL_MAX;
440 for (i = 0; i < agent->n; i++)
442 cur_q = agent_estimate_q (agent, state, i);
450 GNUNET_assert(RIL_ACTION_INVALID != max_i);
456 * Gets any action, to explore the action space from that state
457 * @param agent agent performing the calculation
458 * @param state the state from which to take the action
462 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
464 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
468 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
469 * @param agent the agent performing the update
470 * @param reward the reward received for the last action
471 * @param s_next the new state, the last step got the agent into
472 * @param a_prime the new
475 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
479 double *theta = agent->W[agent->a_old];
481 delta = reward + agent_estimate_q (agent, s_next, a_prime)
482 - agent_estimate_q (agent, agent->s_old, agent->a_old);
483 for (i = 0; i < agent->m; i++)
485 theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
490 * Changes the eligibility trace vector e in various manners:
491 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
492 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
493 * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
494 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
499 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
502 double *e = agent->e;
503 double gamma = agent->envi->parameters.gamma;
504 double lambda = agent->envi->parameters.lambda;
506 for (i = 0; i < agent->m; i++)
510 case RIL_E_ACCUMULATE:
517 e[i] = gamma * lambda;
527 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
528 struct RIL_Peer_Agent *agent,
529 struct ATS_Address *new_address,
530 unsigned long long new_bw_in,
531 unsigned long long new_bw_out)
533 int notify = GNUNET_NO;
535 if (agent->address_inuse != new_address)
537 if (NULL != agent->address_inuse)
539 agent->address_inuse->active = GNUNET_NO;
541 agent->address_inuse = new_address;
542 agent->address_inuse->active = GNUNET_YES;
543 agent->address_inuse->assigned_bw_in.value__ = htonl (agent->bw_in);
544 agent->address_inuse->assigned_bw_out.value__ = htonl (agent->bw_out);
545 notify |= GNUNET_YES;
548 if (agent->bw_in != new_bw_in)
550 agent->bw_in = new_bw_in;
551 agent->address_inuse->assigned_bw_in.value__ = htonl (new_bw_out);
552 notify |= GNUNET_YES;
554 if (agent->bw_out != new_bw_out)
556 agent->bw_out = new_bw_out;
557 agent->address_inuse->assigned_bw_out.value__ = htonl (new_bw_out);
558 notify |= GNUNET_YES;
561 if (notify && agent->active)
563 solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, agent->address_inuse);
568 * Allocates a state vector and fills it with the features present
569 * @param solver the solver handle
570 * @return pointer to the state vector
573 envi_get_state (struct GAS_RIL_Handle *solver)
576 struct RIL_Network *net;
577 double *state = GNUNET_malloc (sizeof (double) * solver->networks_count * 4);
579 for (i = 0; i < solver->networks_count; i++)
581 net = &solver->network_entries[i];
582 state[i * 4 + 0] = (double) net->bw_in_assigned;
583 state[i * 4 + 1] = (double) net->bw_in_available;
584 state[i * 4 + 2] = (double) net->bw_out_assigned;
585 state[i * 4 + 3] = (double) net->bw_out_available;
592 * Gets the reward of the last performed step
593 * @param solver solver handle
597 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
599 //TODO! implement reward calculation
601 return (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX)
602 / (double) UINT32_MAX;
606 envi_action_bw_double (struct GAS_RIL_Handle *solver,
607 struct RIL_Peer_Agent *agent,
612 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2, agent->bw_out);
616 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out * 2);
621 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
622 struct RIL_Peer_Agent *agent,
625 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
626 unsigned long long new_bw;
630 new_bw = agent->bw_in / 2;
633 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out);
637 new_bw = agent->bw_out / 2;
640 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw);
645 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
647 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
651 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
656 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
657 agent->bw_out + (5 * min_bw));
662 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
664 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
665 unsigned long long new_bw;
669 new_bw = agent->bw_in - (5 * min_bw);
672 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out);
676 new_bw = agent->bw_out - (5 * min_bw);
679 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw);
684 envi_action_address_switch (struct GAS_RIL_Handle *solver,
685 struct RIL_Peer_Agent *agent,
686 unsigned int address_index)
688 struct RIL_Address_Wrapped *cur;
691 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
693 if (i == address_index) {
694 envi_set_active_suggestion(solver, agent, cur->address_naked, agent->bw_in, agent->bw_out);
701 //no address with address_index exists
702 GNUNET_assert (GNUNET_NO);
706 * Puts the action into effect
707 * @param solver solver handle
708 * @param action action to perform by the solver
711 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
713 unsigned int address_index;
717 case RIL_ACTION_NOTHING:
719 case RIL_ACTION_BW_IN_DBL:
720 envi_action_bw_double (solver, agent, GNUNET_YES);
722 case RIL_ACTION_BW_IN_HLV:
723 envi_action_bw_halven (solver, agent, GNUNET_YES);
725 case RIL_ACTION_BW_IN_INC:
726 envi_action_bw_inc (solver, agent, GNUNET_YES);
728 case RIL_ACTION_BW_IN_DEC:
729 envi_action_bw_dec (solver, agent, GNUNET_YES);
731 case RIL_ACTION_BW_OUT_DBL:
732 envi_action_bw_double (solver, agent, GNUNET_NO);
734 case RIL_ACTION_BW_OUT_HLV:
735 envi_action_bw_halven (solver, agent, GNUNET_NO);
737 case RIL_ACTION_BW_OUT_INC:
738 envi_action_bw_inc (solver, agent, GNUNET_NO);
740 case RIL_ACTION_BW_OUT_DEC:
741 envi_action_bw_dec (solver, agent, GNUNET_NO);
744 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n))
746 address_index = agent->n - RIL_ACTION_TYPE_NUM;
748 GNUNET_assert (address_index >= 0);
749 GNUNET_assert (address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
751 envi_action_address_switch (solver, agent, address_index);
754 // error - action does not exist
755 GNUNET_assert(GNUNET_NO);
760 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
761 * after having done the last action a_old. It observes the new state s_next and the reward
762 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
763 * next action is put into effect.
764 * @param agent the agent performing the step
767 agent_step (struct RIL_Peer_Agent *agent)
769 int a_next = RIL_ACTION_INVALID;
773 s_next = envi_get_state (agent->envi);
774 reward = envi_get_reward (agent->envi, agent);
776 LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
777 agent->envi->parameters.algorithm ? "Q" : "SARSA");
779 switch (agent->envi->parameters.algorithm)
782 agent_modify_eligibility (agent, RIL_E_SET);
783 if (agent_decide_exploration (agent))
785 a_next = agent_get_action_explore (agent, s_next);
789 a_next = agent_get_action_best (agent, s_next);
791 if (RIL_ACTION_INVALID != agent->a_old)
793 //updates weights with selected action (on-policy), if not first step
794 agent_update_weights (agent, reward, s_next, a_next);
799 a_next = agent_get_action_best (agent, s_next);
800 if (RIL_ACTION_INVALID != agent->a_old)
802 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
803 agent_update_weights (agent, reward, s_next, a_next);
805 if (agent_decide_exploration (agent))
807 a_next = agent_get_action_explore (agent, s_next);
808 agent_modify_eligibility (agent, RIL_E_ZERO);
812 a_next = agent_get_action_best (agent, s_next);
813 agent_modify_eligibility (agent, RIL_E_SET);
818 GNUNET_assert(RIL_ACTION_INVALID != a_next);
820 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
822 envi_do_action (agent->envi, agent, a_next);
824 GNUNET_free(agent->s_old);
825 agent->s_old = s_next;
826 agent->a_old = a_next;
828 agent->step_count += 1;
832 * Cycles through all agents and lets the active ones do a step. Schedules the next step.
833 * @param solver the solver handle
834 * @param tc task context for the scheduler
837 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
839 struct GAS_RIL_Handle *solver = cls;
840 struct RIL_Peer_Agent *cur;
842 LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
844 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
852 solver->step_count += 1;
853 solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, &ril_periodic_step, solver);
857 * Initialize an agent without addresses and its knowledge base
858 * @param s ril solver
859 * @param peer the one in question
860 * @return handle to the new agent
862 static struct RIL_Peer_Agent *
863 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
866 struct GAS_RIL_Handle * solver = s;
867 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
869 agent->envi = solver;
871 agent->step_count = 0;
872 agent->active = GNUNET_NO;
873 agent->s_old = envi_get_state (solver);
874 agent->n = RIL_ACTION_TYPE_NUM;
875 agent->m = solver->networks_count * 4;
876 agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
877 for (i = 0; i < agent->n; i++)
879 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
881 agent->a_old = RIL_ACTION_INVALID;
882 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
883 agent_modify_eligibility (agent, RIL_E_ZERO);
885 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
892 * @param s solver handle
893 * @param agent the agent to retire
896 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
900 for (i = 0; i < agent->n; i++)
902 GNUNET_free(agent->W[i]);
904 GNUNET_free(agent->W);
905 GNUNET_free(agent->e);
906 GNUNET_free(agent->s_old);
910 * Returns the agent for a peer
911 * @param s solver handle
912 * @param peer identity of the peer
913 * @param create whether to create an agent if none is allocated yet
916 static struct RIL_Peer_Agent *
917 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
919 struct RIL_Peer_Agent *cur;
921 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
923 if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey))
930 return agent_init (solver, peer);
935 * Lookup network struct by type
937 * @param s the solver handle
938 * @param type the network type
939 * @return the network struct
941 static struct RIL_Network *
942 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
946 for (i = 0; i < s->networks_count; i++)
948 if (s->network_entries[i].type == type)
950 return &s->network_entries[i];
957 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
959 struct RIL_Network *net;
960 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
962 net = ril_get_network(solver, network);
963 if (net->bw_out_available < min_bw)
969 ril_cut_from_vector (void **old, size_t element_size, unsigned int hole_start, unsigned int hole_length, unsigned int old_length)
972 char *oldptr = (char *) *old;
974 unsigned int bytes_before;
975 unsigned int bytes_hole;
976 unsigned int bytes_after;
978 // LOG(GNUNET_ERROR_TYPE_DEBUG, "hole_start = %d, hole_length = %d, old_length = %d\n", hole_start, hole_length, old_length);
979 GNUNET_assert(old_length > hole_length);
980 GNUNET_assert(old_length >= (hole_start + hole_length));
982 size = (old_length - hole_length) * element_size;
984 bytes_before = element_size * hole_start;
985 bytes_hole = element_size * hole_length;
986 bytes_after = element_size * (old_length - hole_start - hole_length);
994 tmpptr = GNUNET_malloc (size);
995 memcpy (tmpptr, oldptr, bytes_before);
996 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1002 *old = (void *) tmpptr;
1006 * Solver API functions
1007 * ---------------------------
1011 * Changes the preferences for a peer in the problem
1013 * @param solver the solver handle
1014 * @param peer the peer to change the preference for
1015 * @param kind the kind to change the preference
1016 * @param pref_rel the normalized preference value for this kind over all clients
1019 GAS_ril_address_change_preference (void *s,
1020 const struct GNUNET_PeerIdentity *peer,
1021 enum GNUNET_ATS_PreferenceKind kind,
1024 LOG(GNUNET_ERROR_TYPE_DEBUG,
1025 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1026 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1028 * Nothing to do here. Preferences are considered during reward calculation.
1033 * Init the reinforcement learning problem solver
1036 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1037 * out_quota[i] contains outbound quota for network type i
1038 * in_quota[i] contains inbound quota for network type i
1041 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1042 * network[2] == GNUNET_ATS_NET_LAN
1043 * out_quota[2] == 65353
1044 * in_quota[2] == 65353
1046 * @param cfg configuration handle
1047 * @param stats the GNUNET_STATISTICS handle
1048 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1049 * @param addresses hashmap containing all addresses
1050 * @param out_quota array of outbound quotas
1051 * @param in_quota array of outbound quota
1052 * @param dest_length array length for quota arrays
1053 * @param bw_changed_cb callback for changed bandwidth amounts
1054 * @param bw_changed_cb_cls cls for callback
1055 * @param get_preference callback to get relative preferences for a peer
1056 * @param get_preference_cls cls for callback to get relative preferences
1057 * @param get_properties_cls for callback to get relative properties
1058 * @param get_properties_cls cls for callback to get relative properties
1059 * @return handle for the solver on success, NULL on fail
1062 GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1063 const struct GNUNET_STATISTICS_Handle *stats,
1064 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1066 unsigned long long *out_quota,
1067 unsigned long long *in_quota,
1069 GAS_bandwidth_changed_cb bw_changed_cb,
1070 void *bw_changed_cb_cls,
1071 GAS_get_preferences get_preference,
1072 void *get_preference_cls,
1073 GAS_get_properties get_properties,
1074 void *get_properties_cls)
1077 unsigned long long tmp;
1079 struct RIL_Network * cur;
1080 struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle));
1082 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1084 GNUNET_assert(NULL != cfg);
1085 GNUNET_assert(NULL != stats);
1086 GNUNET_assert(NULL != network);
1087 GNUNET_assert(NULL != bw_changed_cb);
1088 GNUNET_assert(NULL != get_preference);
1089 GNUNET_assert(NULL != get_properties);
1092 != GNUNET_CONFIGURATION_get_value_time (cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
1094 solver->step_time = RIL_DEFAULT_STEP_TIME;
1096 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "RIL_ALGORITHM", &string)
1097 && NULL != string && 0 == strcmp (string, "SARSA"))
1099 solver->parameters.algorithm = RIL_ALGO_SARSA;
1103 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1105 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
1107 solver->parameters.gamma = (double) tmp / 100;
1111 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
1113 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
1115 solver->parameters.alpha = (double) tmp / 100;
1119 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1121 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_TRACE_DECAY", &tmp))
1123 solver->parameters.lambda = (double) tmp / 100;
1127 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1130 solver->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1131 solver->callbacks = GNUNET_malloc (sizeof (struct RIL_Callbacks));
1132 solver->callbacks->bw_changed = bw_changed_cb;
1133 solver->callbacks->bw_changed_cls = bw_changed_cb_cls;
1134 solver->callbacks->get_preferences = get_preference;
1135 solver->callbacks->get_preferences_cls = get_preference_cls;
1136 solver->callbacks->get_properties = get_properties;
1137 solver->callbacks->get_properties_cls = get_properties_cls;
1138 solver->networks_count = dest_length;
1139 solver->network_entries = GNUNET_malloc (dest_length * sizeof (struct RIL_Network));
1140 solver->bulk_lock = GNUNET_NO;
1141 solver->addresses = addresses;
1142 solver->step_count = 0;
1144 for (c = 0; c < dest_length; c++)
1146 cur = &solver->network_entries[c];
1147 cur->type = network[c];
1148 cur->bw_in_available = in_quota[c];
1149 cur->bw_in_assigned = 0;
1150 cur->bw_out_available = out_quota[c];
1151 cur->bw_out_assigned = 0;
1154 solver->next_step = GNUNET_SCHEDULER_add_delayed (
1155 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1156 &ril_periodic_step, solver);
1162 * Shutdown the reinforcement learning problem solver
1164 * @param solver the respective handle to shutdown
1167 GAS_ril_done (void * solver)
1169 struct GAS_RIL_Handle *s = solver;
1170 struct RIL_Peer_Agent *cur_agent;
1171 struct RIL_Peer_Agent *next_agent;
1173 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1175 cur_agent = s->agents_head;
1176 while (NULL != cur_agent)
1178 next_agent = cur_agent->next;
1179 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1180 agent_die (s, cur_agent);
1181 cur_agent = next_agent;
1184 GNUNET_SCHEDULER_cancel (s->next_step);
1185 GNUNET_free(s->callbacks);
1186 GNUNET_free(s->network_entries);
1191 * Add a single address within a network to the solver
1193 * @param solver the solver Handle
1194 * @param address the address to add
1195 * @param network network type of this address
1198 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1200 struct GAS_RIL_Handle *s = solver;
1201 struct RIL_Peer_Agent *agent;
1202 struct RIL_Address_Wrapped *address_wrapped;
1209 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1211 address->solver_information = ril_get_network (s, network);
1213 if (!ril_network_is_active(s, network))
1215 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Did not add %s address %p for peer '%s', network does not have enough bandwidth\n",
1216 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1220 agent = ril_get_agent(s, &address->peer, GNUNET_YES);
1223 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1224 address_wrapped->address_naked = address;
1225 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1227 //increase size of W
1228 m_new = agent->m + 5; //TODO! make size of features from address variable (Note to self: ctrl+f for "5" or I kill you!)
1230 n_new = agent->n + 1;
1233 GNUNET_array_grow (agent->W, agent->n, n_new);
1234 for (i = 0; i < n_new; i++)
1239 GNUNET_array_grow(agent->W[i], agent->m, m_new);
1244 GNUNET_array_grow(agent->W[i], zero, m_new);
1248 //increase size of old state vector if there is one
1249 if (RIL_ACTION_INVALID != agent->a_old)
1252 GNUNET_array_grow(agent->s_old, agent->m, m_new); //TODO initialize new state features?
1256 GNUNET_array_grow(agent->e, agent->m, m_new);
1258 if (NULL == agent->address_inuse)
1260 envi_set_active_suggestion(s, agent, address, min_bw, min_bw);
1263 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s address %p for peer '%s'\n",
1264 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1268 * Remove an address from the solver
1270 * @param solver the solver handle
1271 * @param address the address to remove
1272 * @param session_only delete only session not whole address
1275 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1277 //TODO! delete session only
1278 struct GAS_RIL_Handle *s = solver;
1279 struct RIL_Peer_Agent *agent;
1280 struct RIL_Address_Wrapped *address_wrapped;
1281 int address_was_used = address->active;
1286 struct RIL_Network *net;
1287 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1289 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
1290 session_only ? "session for " : "",
1291 address->active ? "active" : "inactive",
1294 GNUNET_i2s (&address->peer));
1296 agent = ril_get_agent(s, &address->peer, GNUNET_NO);
1299 net = address->solver_information;
1300 GNUNET_assert (!ril_network_is_active(s, net->type));
1301 LOG (GNUNET_ERROR_TYPE_DEBUG, "No agent allocated for peer yet, since address was in inactive network\n");
1305 address_index = agent_address_get_index(agent, address);
1306 address_wrapped = agent_address_get(agent, address);
1308 if (NULL == address_wrapped)
1310 net = address->solver_information;
1311 GNUNET_assert (!ril_network_is_active(s, net->type));
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, "Address not considered by agent, address was in inactive network\n");
1316 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1319 m_new = agent->m - 5;
1320 n_new = agent->n - 1;
1322 for (i = 0; i < agent->n; i++)
1324 // LOG (GNUNET_ERROR_TYPE_DEBUG, "first - cut vectors in W\n");
1325 ril_cut_from_vector((void **) &agent->W[i], sizeof (double), ((s->networks_count * 4) + (address_index * 5)), 5, agent->m);
1327 // LOG (GNUNET_ERROR_TYPE_DEBUG, "second - cut action vector out of W\n");
1328 ril_cut_from_vector((void **) &agent->W, sizeof (double *), RIL_ACTION_TYPE_NUM + address_index, 1, agent->n);
1329 //correct last action
1330 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1334 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1336 agent->a_old = RIL_ACTION_INVALID;
1338 //decrease old state vector and eligibility vector
1339 // LOG (GNUNET_ERROR_TYPE_DEBUG, "third - cut state vector\n");
1340 ril_cut_from_vector((void **) &agent->s_old, sizeof (double), ((s->networks_count * 4) + (address_index * 5)), 5, agent->m);
1341 // LOG (GNUNET_ERROR_TYPE_DEBUG, "fourth - cut eligibility vector\n");
1342 ril_cut_from_vector((void **) &agent->e, sizeof (double), ((s->networks_count * 4) + (address_index * 5)), 5, agent->m);
1346 if (address_was_used)
1348 net = address->solver_information;
1349 net->bw_in_assigned -= agent->bw_in;
1350 net->bw_out_assigned -= agent->bw_out;
1352 if (NULL != agent->addresses_head) //if peer has an address left, use it
1354 //TODO? check if network/bandwidth update can be done more clever/elegant at different function
1355 envi_set_active_suggestion(s, agent, agent->addresses_head->address_naked, min_bw, min_bw);
1356 net = agent->addresses_head->address_naked->solver_information;
1357 net->bw_in_assigned -= min_bw;
1358 net->bw_out_assigned -= min_bw;
1362 LOG (GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
1366 * Transport properties for this address have changed
1368 * @param solver solver handle
1369 * @param address the address
1370 * @param type the ATSI type in HBO
1371 * @param abs_value the absolute value of the property
1372 * @param rel_value the normalized value
1375 GAS_ril_address_property_changed (void *solver,
1376 struct ATS_Address *address,
1381 LOG(GNUNET_ERROR_TYPE_DEBUG,
1382 "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1383 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer), address->addr,
1386 * Nothing to do here, properties are considered in every reward calculation
1391 * Transport session for this address has changed
1393 * NOTE: values in addresses are already updated
1395 * @param solver solver handle
1396 * @param address the address
1397 * @param cur_session the current session
1398 * @param new_session the new session
1401 GAS_ril_address_session_changed (void *solver,
1402 struct ATS_Address *address,
1403 uint32_t cur_session,
1404 uint32_t new_session)
1406 //TODO? consider session changed in solver behaviour
1408 * Potentially add session activity as a feature in state vector
1410 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1414 * Usage for this address has changed
1416 * NOTE: values in addresses are already updated
1418 * @param solver solver handle
1419 * @param address the address
1420 * @param in_use usage state
1423 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1425 //TODO! consider address_inuse_changed according to matthias' email
1427 * See matthias' email
1429 LOG(GNUNET_ERROR_TYPE_DEBUG,
1430 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1431 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1435 * Network scope for this address has changed
1437 * NOTE: values in addresses are already updated
1439 * @param solver solver handle
1440 * @param address the address
1441 * @param current_network the current network
1442 * @param new_network the new network
1445 GAS_ril_address_change_network (void *solver,
1446 struct ATS_Address *address,
1447 uint32_t current_network,
1448 uint32_t new_network)
1450 struct GAS_RIL_Handle *s = solver;
1451 struct RIL_Peer_Agent *agent;
1452 struct RIL_Network *net;
1453 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1455 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
1456 "%s address of peer %s from '%s' to '%s'\n",
1457 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
1458 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
1460 if (address->active && !ril_network_is_active(solver, new_network))
1462 GAS_ril_address_delete(solver, address, GNUNET_NO);
1466 agent = ril_get_agent(s, &address->peer, GNUNET_NO);
1469 //no agent there yet, so add as if address is new
1470 address->solver_information = ril_get_network (s, new_network);
1471 GAS_ril_address_add(s,address,new_network);
1475 net = ril_get_network (s, current_network);
1476 net->bw_in_assigned -= agent->bw_in;
1477 net->bw_out_assigned -= agent->bw_out;
1479 net = ril_get_network (s, new_network);
1480 net->bw_in_assigned -= min_bw;
1481 net->bw_out_assigned -= min_bw;
1482 address->solver_information = net;
1486 * Get application feedback for a peer
1488 * @param solver the solver handle
1489 * @param application the application
1490 * @param peer the peer to change the preference for
1491 * @param scope the time interval for this feedback: [now - scope .. now]
1492 * @param kind the kind to change the preference
1493 * @param score the score
1496 GAS_ril_address_preference_feedback (void *solver,
1498 const struct GNUNET_PeerIdentity *peer,
1499 const struct GNUNET_TIME_Relative scope,
1500 enum GNUNET_ATS_PreferenceKind kind,
1503 //TODO! collect reward until next reward calculation
1504 LOG(GNUNET_ERROR_TYPE_DEBUG,
1505 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1506 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1507 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1511 * Start a bulk operation
1513 * @param solver the solver
1516 GAS_ril_bulk_start (void *solver)
1518 //TODO? consideration: keep bulk counter and stop agents during bulk
1520 * bulk counter up, but not really relevant, because there is no complete calculation of the
1521 * bandwidth assignment triggered anyway. Therefore, changes to addresses can come and go as
1522 * they want. Consideration: Step-pause during bulk-start-stop period...
1525 //LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n");
1529 * Bulk operation done
1532 GAS_ril_bulk_stop (void *solver)
1534 //TODO? consideration: keep bulk counter and stop agents during bulk
1536 * bulk counter down, see bulk_start()
1539 //LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n");
1543 * Get the preferred address for a specific peer
1545 * @param solver the solver handle
1546 * @param peer the identity of the peer
1548 const struct ATS_Address *
1549 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1552 * activate agent, return currently chosen address
1554 struct GAS_RIL_Handle *s = solver;
1555 struct RIL_Peer_Agent *agent;
1557 agent = ril_get_agent (s, peer, GNUNET_NO);
1561 LOG(GNUNET_ERROR_TYPE_DEBUG,
1562 "API_get_preferred_address() No agent for peer '%s' do not suggest address\n",
1567 agent->active = GNUNET_YES;
1569 GNUNET_assert(NULL != agent->address_inuse);
1571 LOG(GNUNET_ERROR_TYPE_DEBUG,
1572 "API_get_preferred_address() Activated agent for peer '%s' with %s address\n",
1573 GNUNET_i2s (peer), agent->address_inuse->plugin);
1575 return agent->address_inuse;
1579 * Stop notifying about address and bandwidth changes for this peer
1581 * @param solver the solver handle
1582 * @param peer the peer
1585 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1587 struct GAS_RIL_Handle *s = solver;
1588 struct RIL_Peer_Agent *agent;
1590 agent = ril_get_agent (s, peer, GNUNET_NO);
1591 agent->active = GNUNET_NO;
1593 LOG(GNUNET_ERROR_TYPE_DEBUG,
1594 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1595 GNUNET_i2s (peer), agent->address_inuse->plugin);
1598 /* end of gnunet-service-ats-solver_ril.c */