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 RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
34 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
35 #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
36 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
37 #define RIL_DEFAULT_TRACE_DECAY 0.6
38 #define RIL_EXPLORE_RATIO 0.1
41 * ATS reinforcement learning solver
48 RIL_ACTION_NOTHING = 0,
49 RIL_ACTION_BW_IN_DBL = 1,
50 RIL_ACTION_BW_IN_HLV = 2,
51 RIL_ACTION_BW_IN_INC = 3,
52 RIL_ACTION_BW_IN_DEC = 4,
53 RIL_ACTION_BW_OUT_DBL = 5,
54 RIL_ACTION_BW_OUT_HLV = 6,
55 RIL_ACTION_BW_OUT_INC = 7,
56 RIL_ACTION_BW_OUT_DEC = 8,
57 RIL_ACTION_TYPE_NUM = 9
59 //TODO! add the rest of the actions
63 RIL_ALGO_SARSA = 0, RIL_ALGO_Q = 1
66 enum RIL_E_Modification
68 RIL_E_SET, RIL_E_ZERO, RIL_E_ACCUMULATE, RIL_E_REPLACE
72 * Global learning parameters
74 struct RIL_Learning_Parameters
77 * The TD-algorithm to use
79 enum RIL_Algorithm algorithm;
82 * Learning discount factor in the TD-update
87 * Gradient-descent step-size
92 * Trace-decay factor for eligibility traces
100 * Next agent in solver's linked list
102 struct RIL_Peer_Agent *next;
105 * Previous agent in solver's linked list
107 struct RIL_Peer_Agent *prev;
112 struct GAS_RIL_Handle *envi;
117 struct GNUNET_PeerIdentity peer;
120 * Whether the agent is active or not
125 * Number of performed time-steps
127 unsigned long long step_count;
130 * Experience matrix W
135 * Number of rows of W / Number of state-vector features
140 * Number of columns of W / Number of actions
145 * Last perceived state feature vector
155 * Eligibility trace vector
162 struct ATS_Address * address;
165 * Inbound bandwidth assigned by the agent
167 unsigned long long bw_in;
170 * Outbound bandwidth assigned by the agent
172 unsigned long long bw_out;
180 enum GNUNET_ATS_Network_Type type;
183 * Total available inbound bandwidth
185 unsigned long long bw_in_available;
188 * Total assigned outbound bandwidth
190 unsigned long long bw_in_assigned;
193 * Total available outbound bandwidth
195 unsigned long long bw_out_available;
198 * Total assigned outbound bandwidth
200 unsigned long long bw_out_assigned;
206 * Bandwidth changed callback
208 GAS_bandwidth_changed_cb bw_changed;
211 * Bandwidth changed callback cls
213 void *bw_changed_cls;
216 * ATS function to get preferences
218 GAS_get_preferences get_preferences;
221 * Closure for ATS function to get preferences
223 void *get_preferences_cls;
226 * ATS function to get properties
228 GAS_get_properties get_properties;
231 * Closure for ATS function to get properties
233 void *get_properties_cls;
237 * A handle for the reinforcement learning solver
239 struct GAS_RIL_Handle
244 struct GNUNET_STATISTICS_Handle *stats;
247 * Hashmap containing all valid addresses
249 const struct GNUNET_CONTAINER_MultiHashMap *addresses;
252 * Callbacks for the solver
254 struct RIL_Callbacks *callbacks;
262 * Number of changes while solver was locked
267 * Number of performed time-steps
269 unsigned long long step_count;
272 * Interval time between steps in milliseconds //TODO? put in agent
274 struct GNUNET_TIME_Relative step_time;
277 * Task identifier of the next time-step to be executed //TODO? put in agent
279 GNUNET_SCHEDULER_TaskIdentifier next_step;
282 * Learning parameters
284 struct RIL_Learning_Parameters parameters;
287 * Array of networks with global assignment state
289 struct RIL_Network * network_entries;
294 unsigned int networks_count;
297 * List of active peer-agents
299 struct RIL_Peer_Agent * agents_head;
300 struct RIL_Peer_Agent * agents_tail;
305 * ---------------------------
309 * Estimate the current action-value for state s and action a
310 * @param agent agent performing the estimation
313 * @return estimation value
316 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
321 for (i = 0; i < agent->m; i++)
323 result += state[i] * agent->W[action][i];
330 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
331 * currently estimated best action) in the current step
332 * @param agent agent performing the step
333 * @return yes, if exploring
336 agent_decide_exploration (struct RIL_Peer_Agent *agent)
338 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
339 UINT32_MAX) / (double) UINT32_MAX;
341 if (r < RIL_EXPLORE_RATIO)
349 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
350 * most reward in the future)
351 * @param agent agent performing the calculation
352 * @param state the state from which to take the action
353 * @return the action promising most future reward
356 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
361 double max_q = -DBL_MAX;
363 for (i = 0; i < agent->n; i++)
365 cur_q = agent_estimate_q (agent, state, i);
373 GNUNET_assert(-1 != max_i);
379 * Gets any action, to explore the action space from that state
380 * @param agent agent performing the calculation
381 * @param state the state from which to take the action
385 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
387 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
391 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
392 * @param agent the agent performing the update
393 * @param reward the reward received for the last action
394 * @param s_next the new state, the last step got the agent into
395 * @param a_prime the new
398 agent_update_weights (struct RIL_Peer_Agent *agent,
405 double *theta = agent->W[agent->a_old];
407 delta = reward + agent_estimate_q (agent, s_next, a_prime)
408 - agent_estimate_q (agent, agent->s_old, agent->a_old);
409 for (i = 0; i < agent->m; i++)
411 theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
416 * Changes the eligibility trace vector e in various manners:
417 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
418 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
419 * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
420 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
425 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
426 enum RIL_E_Modification mod)
429 double *e = agent->e;
430 double gamma = agent->envi->parameters.gamma;
431 double lambda = agent->envi->parameters.lambda;
433 for (i = 0; i < agent->m; i++)
437 case RIL_E_ACCUMULATE:
444 e[i] = gamma * lambda;
454 envi_change_active_address (struct GAS_RIL_Handle *solver,
455 struct RIL_Peer_Agent *agent,
456 struct ATS_Address *new_address,
457 unsigned long long new_bw_in,
458 unsigned long long new_bw_out)
460 int notify = GNUNET_NO;
462 if (agent->address != new_address)
464 agent->address->active = GNUNET_NO;
465 agent->address = new_address;
466 agent->address->active = GNUNET_YES;
467 agent->address->assigned_bw_in.value__ = htonl (agent->bw_in);
468 agent->address->assigned_bw_out.value__ = htonl (agent->bw_out);
469 notify |= GNUNET_YES;
471 if (agent->bw_in != new_bw_in)
473 agent->bw_in = new_bw_in;
474 agent->address->assigned_bw_in.value__ = htonl (new_bw_out);
475 notify |= GNUNET_YES;
477 if (agent->bw_out != new_bw_out)
479 agent->bw_out = new_bw_out;
480 agent->address->assigned_bw_out.value__ = htonl (new_bw_out);
481 notify |= GNUNET_YES;
486 solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls,
492 * Allocates a state vector and fills it with the features present
493 * @param solver the solver handle
494 * @return pointer to the state vector
497 envi_get_state (struct GAS_RIL_Handle *solver)
500 struct RIL_Network *net;
501 double *state = GNUNET_malloc (sizeof (double) * solver->networks_count * 4);
503 for (i = 0; i < solver->networks_count; i++)
505 net = &solver->network_entries[i];
506 state[i * 4 + 0] = (double) net->bw_in_assigned;
507 state[i * 4 + 1] = (double) net->bw_in_available;
508 state[i * 4 + 2] = (double) net->bw_out_assigned;
509 state[i * 4 + 3] = (double) net->bw_out_available;
516 * Gets the reward of the last performed step
517 * @param solver solver handle
521 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
523 //TODO! implement reward calculation
525 return (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
526 UINT32_MAX) / (double) UINT32_MAX;
530 envi_action_bw_double (struct GAS_RIL_Handle *solver,
531 struct RIL_Peer_Agent *agent,
536 envi_change_active_address (solver, agent, agent->address, agent->bw_in * 2,
541 envi_change_active_address (solver, agent, agent->address, agent->bw_in,
547 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
548 struct RIL_Peer_Agent *agent,
551 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
552 unsigned long long new_bw;
556 new_bw = agent->bw_in / 2;
559 envi_change_active_address (solver, agent, agent->address, new_bw,
564 new_bw = agent->bw_out / 2;
567 envi_change_active_address (solver, agent, agent->address, agent->bw_in,
573 envi_action_bw_inc (struct GAS_RIL_Handle *solver,
574 struct RIL_Peer_Agent *agent,
577 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
581 envi_change_active_address (solver, agent, agent->address,
582 agent->bw_in + (5 * min_bw), agent->bw_out);
586 envi_change_active_address (solver, agent, agent->address, agent->bw_in,
587 agent->bw_out + (5 * min_bw));
592 envi_action_bw_dec (struct GAS_RIL_Handle *solver,
593 struct RIL_Peer_Agent *agent,
596 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
597 unsigned long long new_bw;
601 new_bw = agent->bw_in - (5 * min_bw);
604 envi_change_active_address (solver, agent, agent->address, new_bw,
609 new_bw = agent->bw_out - (5 * min_bw);
612 envi_change_active_address (solver, agent, agent->address, agent->bw_in,
618 * Puts the action into effect
619 * @param solver solver handle
620 * @param action action to perform by the solver
623 envi_do_action (struct GAS_RIL_Handle *solver,
624 struct RIL_Peer_Agent *agent,
629 case RIL_ACTION_NOTHING:
631 case RIL_ACTION_BW_IN_DBL:
632 envi_action_bw_double (solver, agent, GNUNET_YES);
634 case RIL_ACTION_BW_IN_HLV:
635 envi_action_bw_halven (solver, agent, GNUNET_YES);
637 case RIL_ACTION_BW_IN_INC:
638 envi_action_bw_inc (solver, agent, GNUNET_YES);
640 case RIL_ACTION_BW_IN_DEC:
641 envi_action_bw_dec (solver, agent, GNUNET_YES);
643 case RIL_ACTION_BW_OUT_DBL:
644 envi_action_bw_double (solver, agent, GNUNET_NO);
646 case RIL_ACTION_BW_OUT_HLV:
647 envi_action_bw_halven (solver, agent, GNUNET_NO);
649 case RIL_ACTION_BW_OUT_INC:
650 envi_action_bw_inc (solver, agent, GNUNET_NO);
652 case RIL_ACTION_BW_OUT_DEC:
653 envi_action_bw_dec (solver, agent, GNUNET_NO);
656 // error - action does not exist
657 GNUNET_assert(GNUNET_NO);
662 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
663 * after having done the last action a_old. It observes the new state s_next and the reward
664 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
665 * next action is put into effect.
666 * @param agent the agent performing the step
669 agent_step (struct RIL_Peer_Agent *agent)
675 s_next = envi_get_state (agent->envi);
676 reward = envi_get_reward (agent->envi, agent);
678 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
679 agent->envi->parameters.algorithm ? "Q" : "SARSA");
681 switch (agent->envi->parameters.algorithm)
684 agent_modify_eligibility (agent, RIL_E_SET);
685 if (agent_decide_exploration (agent))
687 a_next = agent_get_action_explore (agent, s_next);
691 a_next = agent_get_action_best (agent, s_next);
693 //updates weights with selected action (on-policy), if not first step
694 if (-1 != agent->a_old)
695 agent_update_weights (agent, reward, s_next, a_next);
699 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
700 a_next = agent_get_action_best (agent, s_next);
701 if (-1 != agent->a_old)
702 agent_update_weights (agent, reward, s_next, a_next);
703 if (agent_decide_exploration (agent))
705 a_next = agent_get_action_explore (agent, s_next);
706 agent_modify_eligibility (agent, RIL_E_ZERO);
710 a_next = agent_get_action_best (agent, s_next);
711 agent_modify_eligibility (agent, RIL_E_SET);
716 GNUNET_assert(-1 != a_next);
718 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
720 envi_do_action (agent->envi, agent, a_next);
722 GNUNET_free(agent->s_old);
723 agent->s_old = s_next;
724 agent->a_old = a_next;
726 agent->step_count += 1;
730 * Cycles through all agents and lets the active ones do a step. Schedules the next step.
731 * @param solver the solver handle
732 * @param tc task context for the scheduler
735 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
737 struct GAS_RIL_Handle *solver = cls;
738 struct RIL_Peer_Agent *cur;
740 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n",
743 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
751 solver->step_count += 1;
752 solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time,
753 &ril_periodic_step, solver);
757 * Initialize an agent without addresses and its knowledge base
758 * @param s ril solver
759 * @param peer the one in question
760 * @return handle to the new agent
762 static struct RIL_Peer_Agent *
763 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
766 struct GAS_RIL_Handle * solver = s;
767 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
769 agent->envi = solver;
771 agent->step_count = 0;
772 agent->active = GNUNET_NO;
773 agent->s_old = envi_get_state (solver);
774 agent->n = RIL_ACTION_TYPE_NUM;
775 agent->m = solver->networks_count * 4;
776 agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
777 for (i = 0; i < agent->n; i++)
779 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
782 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
783 agent_modify_eligibility (agent, RIL_E_ZERO);
785 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail,
793 * @param s solver handle
794 * @param agent the agent to retire
797 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
801 for (i = 0; i < agent->n; i++)
803 GNUNET_free(agent->W[i]);
805 GNUNET_free(agent->W);
806 GNUNET_free(agent->e);
807 GNUNET_free(agent->s_old);
811 ril_remove_agent (struct GAS_RIL_Handle *s, struct RIL_Peer_Agent *agent)
813 struct RIL_Peer_Agent *cur_agent;
814 struct RIL_Peer_Agent *next_agent;
816 cur_agent = s->agents_head;
817 while (NULL != cur_agent)
819 next_agent = cur_agent->next;
821 if (agent == cur_agent)
823 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
824 agent_die (s, cur_agent);
827 cur_agent = next_agent;
832 * Counts the (active) agents
833 * @param solver solver handle
834 * @param active_only whether only active agents should be counted
835 * @return number of agents
838 //ril_count_agents (struct GAS_RIL_Handle *solver, int active_only)
841 // struct RIL_Peer_Agent *cur;
844 // for (cur = solver->agents_head; NULL != cur; cur = cur->next)
846 // if ((!active_only) || (active_only && cur->active))
855 * Returns the agent for a peer
856 * @param s solver handle
857 * @param peer identity of the peer
858 * @param create whether to create an agent if none is allocated yet
861 static struct RIL_Peer_Agent *
862 ril_get_agent (struct GAS_RIL_Handle *solver,
863 const struct GNUNET_PeerIdentity *peer,
866 struct RIL_Peer_Agent *cur;
868 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
870 if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey))
877 return agent_init (solver, peer);
882 ril_network_is_active (struct RIL_Network *network)
884 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
886 if (network->bw_out_available < min_bw)
892 * Iterator, which allocates one agent per peer
895 * @param key peer identity
896 * @param value address
897 * @return whether iterator should continue
900 ril_init_agents_it (void *cls, const struct GNUNET_HashCode *key, void *value)
902 struct GAS_RIL_Handle *solver = cls;
903 struct ATS_Address *address = value;
904 struct RIL_Peer_Agent *agent = NULL;
905 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
907 if (ril_network_is_active (address->solver_information))
909 agent = ril_get_agent (solver, &address->peer, GNUNET_YES);
911 GNUNET_assert(NULL != agent);
913 if (NULL == agent->address)
915 agent->address = address;
916 agent->address->active = GNUNET_YES;
917 agent->bw_in = min_bw;
918 agent->address->assigned_bw_in.value__ = htonl (min_bw);
919 agent->bw_out = min_bw;
920 agent->address->assigned_bw_out.value__ = htonl (min_bw);
927 ril_get_new_address_or_delete (struct GAS_RIL_Handle *solver,
928 struct RIL_Peer_Agent *agent)
930 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
931 //get new address for agent or delete agent
933 agent->address = NULL; //forget current address
934 GNUNET_CONTAINER_multihashmap_iterate (solver->addresses, &ril_init_agents_it,
935 solver); //put another address
937 if (NULL == agent->address) //no other address available
939 agent->active = GNUNET_NO;
940 ril_remove_agent (solver, agent);
944 envi_change_active_address (solver, agent, agent->address, min_bw, min_bw);
949 * Lookup network struct by type
951 * @param s the solver handle
952 * @param type the network type
953 * @return the network struct
955 static struct RIL_Network *
956 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
960 for (i = 0; i < s->networks_count; i++)
962 if (s->network_entries[i].type == type)
964 return &s->network_entries[i];
971 * Solver API functions
972 * ---------------------------
976 * Changes the preferences for a peer in the problem
978 * @param solver the solver handle
979 * @param peer the peer to change the preference for
980 * @param kind the kind to change the preference
981 * @param pref_rel the normalized preference value for this kind over all clients
984 GAS_ril_address_change_preference (void *s,
985 const struct GNUNET_PeerIdentity *peer,
986 enum GNUNET_ATS_PreferenceKind kind,
989 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
990 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
991 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
993 * Nothing to do here. Preferences are considered during reward calculation.
998 * Init the reinforcement learning problem solver
1001 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1002 * out_quota[i] contains outbound quota for network type i
1003 * in_quota[i] contains inbound quota for network type i
1006 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1007 * network[2] == GNUNET_ATS_NET_LAN
1008 * out_quota[2] == 65353
1009 * in_quota[2] == 65353
1011 * @param cfg configuration handle
1012 * @param stats the GNUNET_STATISTICS handle
1013 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1014 * @param addresses hashmap containing all addresses
1015 * @param out_quota array of outbound quotas
1016 * @param in_quota array of outbound quota
1017 * @param dest_length array length for quota arrays
1018 * @param bw_changed_cb callback for changed bandwidth amounts
1019 * @param bw_changed_cb_cls cls for callback
1020 * @param get_preference callback to get relative preferences for a peer
1021 * @param get_preference_cls cls for callback to get relative preferences
1022 * @param get_properties_cls for callback to get relative properties
1023 * @param get_properties_cls cls for callback to get relative properties
1024 * @return handle for the solver on success, NULL on fail
1027 GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1028 const struct GNUNET_STATISTICS_Handle *stats,
1029 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1031 unsigned long long *out_quota,
1032 unsigned long long *in_quota,
1034 GAS_bandwidth_changed_cb bw_changed_cb,
1035 void *bw_changed_cb_cls,
1036 GAS_get_preferences get_preference,
1037 void *get_preference_cls,
1038 GAS_get_properties get_properties,
1039 void *get_properties_cls)
1042 unsigned long long tmp;
1044 struct RIL_Network * cur;
1045 struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle));
1047 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1049 GNUNET_assert(NULL != cfg);
1050 GNUNET_assert(NULL != stats);
1051 GNUNET_assert(NULL != network);
1052 GNUNET_assert(NULL != bw_changed_cb);
1053 GNUNET_assert(NULL != get_preference);
1054 GNUNET_assert(NULL != get_properties);
1057 != GNUNET_CONFIGURATION_get_value_time (cfg, "ats", "RIL_STEP_TIME",
1058 &solver->step_time))
1060 solver->step_time = RIL_DEFAULT_STEP_TIME;
1063 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "RIL_ALGORITHM",
1064 &string) && NULL != string && 0 == strcmp (string, "SARSA"))
1066 solver->parameters.algorithm = RIL_ALGO_SARSA;
1070 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1073 == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_DISCOUNT_FACTOR",
1076 solver->parameters.gamma = (double) tmp / 100;
1080 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
1083 == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1084 "RIL_GRADIENT_STEP_SIZE", &tmp))
1086 solver->parameters.alpha = (double) tmp / 100;
1090 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1093 == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_TRACE_DECAY",
1096 solver->parameters.lambda = (double) tmp / 100;
1100 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1103 solver->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1104 solver->callbacks = GNUNET_malloc (sizeof (struct RIL_Callbacks));
1105 solver->callbacks->bw_changed = bw_changed_cb;
1106 solver->callbacks->bw_changed_cls = bw_changed_cb_cls;
1107 solver->callbacks->get_preferences = get_preference;
1108 solver->callbacks->get_preferences_cls = get_preference_cls;
1109 solver->callbacks->get_properties = get_properties;
1110 solver->callbacks->get_properties_cls = get_properties_cls;
1111 solver->networks_count = dest_length;
1112 solver->network_entries =
1113 GNUNET_malloc (dest_length * sizeof (struct RIL_Network));
1114 solver->bulk_lock = GNUNET_NO;
1115 solver->addresses = addresses;
1116 solver->step_count = 0;
1118 for (c = 0; c < dest_length; c++)
1120 cur = &solver->network_entries[c];
1121 cur->type = network[c];
1122 cur->bw_in_available = in_quota[c];
1123 cur->bw_in_assigned = 0;
1124 cur->bw_out_available = out_quota[c];
1125 cur->bw_out_assigned = 0;
1128 solver->next_step = GNUNET_SCHEDULER_add_delayed (
1129 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (),
1130 1000), &ril_periodic_step, solver);
1136 * Shutdown the reinforcement learning problem solver
1138 * @param solver the respective handle to shutdown
1141 GAS_ril_done (void * solver)
1143 struct GAS_RIL_Handle *s = solver;
1144 struct RIL_Peer_Agent *cur_agent;
1145 struct RIL_Peer_Agent *next_agent;
1147 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1149 cur_agent = s->agents_head;
1150 while (NULL != cur_agent)
1152 next_agent = cur_agent->next;
1153 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1154 agent_die (s, cur_agent);
1155 cur_agent = next_agent;
1158 GNUNET_SCHEDULER_cancel (s->next_step);
1159 GNUNET_free(s->callbacks);
1160 GNUNET_free(s->network_entries);
1165 * Add a single address within a network to the solver
1167 * @param solver the solver Handle
1168 * @param address the address to add
1169 * @param network network type of this address
1172 GAS_ril_address_add (void *solver,
1173 struct ATS_Address *address,
1176 struct GAS_RIL_Handle *s = solver;
1177 //TODO! implement solver address add
1180 * initialize new agent
1182 * increase state vector
1187 address->solver_information = ril_get_network (s, network);
1190 * reiterate all addresses, create new agent if necessary and give the agent the address
1192 GNUNET_CONTAINER_multihashmap_iterate (s->addresses, &ril_init_agents_it,
1195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1196 "API_address_add() Added %s address for peer '%s'\n", address->plugin,
1197 GNUNET_i2s (&address->peer));
1201 * Remove an address from the solver
1203 * @param solver the solver handle
1204 * @param address the address to remove
1205 * @param session_only delete only session not whole address
1208 GAS_ril_address_delete (void *solver,
1209 struct ATS_Address *address,
1212 //TODO! implement solver address delete
1213 //TODO! delete session only
1216 * if (last address of peer)
1219 * decrease state vector
1220 * decrease knowledge matrix
1221 * decrease action vector
1223 struct GAS_RIL_Handle *s = solver;
1224 struct RIL_Peer_Agent *agent;
1226 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1227 "API_address_delete() deleting %s address %p for peer '%s'\n",
1228 address->active ? "active" : "inactive", address,
1229 GNUNET_i2s (&address->peer));
1231 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1235 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1236 "API_address_delete() deleting address for unallocated agent\n");
1240 if (address == agent->address) //if used address deleted
1242 address->active = GNUNET_NO;
1243 ril_get_new_address_or_delete (s, agent);
1246 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1247 "API_address_delete() Deleted %s%s address for peer '%s'\n",
1248 session_only ? "session for " : "", address->plugin,
1249 GNUNET_i2s (&address->peer));
1253 * Transport properties for this address have changed
1255 * @param solver solver handle
1256 * @param address the address
1257 * @param type the ATSI type in HBO
1258 * @param abs_value the absolute value of the property
1259 * @param rel_value the normalized value
1262 GAS_ril_address_property_changed (void *solver,
1263 struct ATS_Address *address,
1268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1269 "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1270 "to %.2f \n", GNUNET_ATS_print_property_type (type),
1271 GNUNET_i2s (&address->peer), address, rel_value);
1273 * Nothing to do here, properties are considered in every reward calculation
1278 * Transport session for this address has changed
1280 * NOTE: values in addresses are already updated
1282 * @param solver solver handle
1283 * @param address the address
1284 * @param cur_session the current session
1285 * @param new_session the new session
1288 GAS_ril_address_session_changed (void *solver,
1289 struct ATS_Address *address,
1290 uint32_t cur_session,
1291 uint32_t new_session)
1293 //TODO? consider session changed in solver behaviour
1295 * Potentially add session activity as a feature in state vector
1297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1301 * Usage for this address has changed
1303 * NOTE: values in addresses are already updated
1305 * @param solver solver handle
1306 * @param address the address
1307 * @param in_use usage state
1310 GAS_ril_address_inuse_changed (void *solver,
1311 struct ATS_Address *address,
1314 //TODO! consider address_inuse_changed according to matthias' email
1316 * See matthias' email
1318 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1319 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1320 address->plugin, GNUNET_i2s (&address->peer),
1321 (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1325 * Network scope for this address has changed
1327 * NOTE: values in addresses are already updated
1329 * @param solver solver handle
1330 * @param address the address
1331 * @param current_network the current network
1332 * @param new_network the new network
1335 GAS_ril_address_change_network (void *solver,
1336 struct ATS_Address *address,
1337 uint32_t current_network,
1338 uint32_t new_network)
1340 struct GAS_RIL_Handle *s = solver;
1341 struct RIL_Peer_Agent *agent;
1342 struct RIL_Network *net;
1344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1345 "API_address_change_network() Network type changed, moving "
1346 "%s address of peer %s from '%s' to '%s'\n",
1347 (GNUNET_YES == address->active) ? "active" : "inactive",
1348 GNUNET_i2s (&address->peer),
1349 GNUNET_ATS_print_network_type (current_network),
1350 GNUNET_ATS_print_network_type (new_network));
1352 address->solver_information = ril_get_network (solver, new_network);
1354 if (address->active)
1356 agent = ril_get_agent (solver, &address->peer, GNUNET_NO);
1358 //remove from old network
1359 net = ril_get_network (s, current_network);
1360 net->bw_in_assigned -= agent->bw_in;
1361 net->bw_out_assigned -= agent->bw_out;
1363 if (ril_network_is_active (ril_get_network (s, new_network)))
1365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New network is active\n");
1366 //add to new network
1367 net = ril_get_network (s, new_network);
1368 net->bw_in_assigned += agent->bw_in;
1369 net->bw_out_assigned += agent->bw_out;
1371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1372 "API_address_change_network() Moved %d inbound and %d "
1373 "outbound\n", agent->bw_in, agent->bw_out);
1375 else //new network for this address is not active => address must not be considered
1377 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New network is not active\n");
1379 net = agent->address->solver_information;
1380 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Before: active address %p is %s\n",
1381 agent->address, GNUNET_ATS_print_network_type (net->type));
1383 address->active = GNUNET_NO;
1384 ril_get_new_address_or_delete (s, agent);
1386 net = agent->address->solver_information;
1387 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "After: active address %p is %s\n",
1388 agent->address, GNUNET_ATS_print_network_type (net->type));
1394 * Get application feedback for a peer
1396 * @param solver the solver handle
1397 * @param application the application
1398 * @param peer the peer to change the preference for
1399 * @param scope the time interval for this feedback: [now - scope .. now]
1400 * @param kind the kind to change the preference
1401 * @param score the score
1404 GAS_ril_address_preference_feedback (void *solver,
1406 const struct GNUNET_PeerIdentity *peer,
1407 const struct GNUNET_TIME_Relative scope,
1408 enum GNUNET_ATS_PreferenceKind kind,
1411 //TODO! collect reward until next reward calculation
1412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1413 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1414 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1415 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1419 * Start a bulk operation
1421 * @param solver the solver
1424 GAS_ril_bulk_start (void *solver)
1426 //TODO? consideration: keep bulk counter and stop agents during bulk
1428 * bulk counter up, but not really relevant, because there is no complete calculation of the
1429 * bandwidth assignment triggered anyway. Therefore, changes to addresses can come and go as
1430 * they want. Consideration: Step-pause during bulk-start-stop period...
1433 //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n");
1437 * Bulk operation done
1440 GAS_ril_bulk_stop (void *solver)
1442 //TODO? consideration: keep bulk counter and stop agents during bulk
1444 * bulk counter down, see bulk_start()
1447 //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n");
1451 * Get the preferred address for a specific peer
1453 * @param solver the solver handle
1454 * @param peer the identity of the peer
1456 const struct ATS_Address *
1457 GAS_ril_get_preferred_address (void *solver,
1458 const struct GNUNET_PeerIdentity *peer)
1461 * activate agent, return currently chosen address
1463 struct GAS_RIL_Handle *s = solver;
1464 struct RIL_Peer_Agent *agent;
1466 agent = ril_get_agent (s, peer, GNUNET_NO);
1470 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1471 "API_get_preferred_address() No agent for peer '%s' do not suggest address\n",
1476 agent->active = GNUNET_YES;
1478 GNUNET_assert(NULL != agent->address);
1480 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1481 "API_get_preferred_address() Activated agent for peer '%s' with %s address\n",
1482 GNUNET_i2s (peer), agent->address->plugin);
1484 return agent->address;
1488 * Stop notifying about address and bandwidth changes for this peer
1490 * @param solver the solver handle
1491 * @param peer the peer
1494 GAS_ril_stop_get_preferred_address (void *solver,
1495 const struct GNUNET_PeerIdentity *peer)
1497 struct GAS_RIL_Handle *s = solver;
1498 struct RIL_Peer_Agent *agent;
1500 agent = ril_get_agent (s, peer, GNUNET_NO);
1501 agent->active = GNUNET_NO;
1503 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1504 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1505 GNUNET_i2s (peer), agent->address->plugin);
1508 /* end of gnunet-service-ats-solver_ril.c */