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_BW_IN_DBL = 0,
49 RIL_ACTION_BW_OUT_DBL = 1,
50 RIL_ACTION_BW_IN_HLV = 2,
51 RIL_ACTION_BW_OUT_HLV = 3,
52 RIL_ACTION_TYPE_NUM = 4
54 //TODO! add the rest of the actions
62 enum RIL_E_Modification
64 RIL_E_SET, RIL_E_ZERO, RIL_E_ACCUMULATE, RIL_E_REPLACE
68 * Global learning parameters
70 struct RIL_Learning_Parameters
73 * The TD-algorithm to use
75 enum RIL_Algorithm algorithm;
78 * Learning discount factor in the TD-update
83 * Gradient-descent step-size
88 * Trace-decay factor for eligibility traces
96 * Next agent in solver's linked list
98 struct RIL_Peer_Agent *next;
101 * Previous agent in solver's linked list
103 struct RIL_Peer_Agent *prev;
108 struct GAS_RIL_Handle *envi;
113 struct GNUNET_PeerIdentity peer;
116 * Whether the agent is active or not
121 * Number of performed time-steps
123 unsigned long long step_count;
126 * Experience matrix W
131 * Number of rows of W / Number of state-vector features
136 * Number of columns of W / Number of actions
141 * Last perceived state feature vector
151 * Eligibility trace vector
158 struct ATS_Address * address;
161 * Inbound bandwidth assigned by the agent
163 unsigned long long bw_in;
166 * Outbound bandwidth assigned by the agent
168 unsigned long long bw_out;
176 enum GNUNET_ATS_Network_Type type;
179 * Total available inbound bandwidth
181 unsigned long long bw_in_available;
184 * Total assigned outbound bandwidth
186 unsigned long long bw_in_assigned;
189 * Total available outbound bandwidth
191 unsigned long long bw_out_available;
194 * Total assigned outbound bandwidth
196 unsigned long long bw_out_assigned;
202 * Bandwidth changed callback
204 GAS_bandwidth_changed_cb bw_changed;
207 * Bandwidth changed callback cls
209 void *bw_changed_cls;
212 * ATS function to get preferences
214 GAS_get_preferences get_preferences;
217 * Closure for ATS function to get preferences
219 void *get_preferences_cls;
222 * ATS function to get properties
224 GAS_get_properties get_properties;
227 * Closure for ATS function to get properties
229 void *get_properties_cls;
233 * A handle for the reinforcement learning solver
235 struct GAS_RIL_Handle
240 struct GNUNET_STATISTICS_Handle *stats;
243 * Hashmap containing all valid addresses
245 const struct GNUNET_CONTAINER_MultiHashMap *addresses;
248 * Callbacks for the solver
250 struct RIL_Callbacks *callbacks;
258 * Number of changes while solver was locked
263 * Number of performed time-steps
265 unsigned long long step_count;
268 * Interval time between steps in milliseconds //TODO? put in agent
270 struct GNUNET_TIME_Relative step_time;
273 * Task identifier of the next time-step to be executed //TODO? put in agent
275 GNUNET_SCHEDULER_TaskIdentifier next_step;
278 * Learning parameters
280 struct RIL_Learning_Parameters parameters;
283 * Array of networks with global assignment state
285 struct RIL_Network * network_entries;
290 unsigned int networks_count;
293 * List of active peer-agents
295 struct RIL_Peer_Agent * agents_head;
296 struct RIL_Peer_Agent * agents_tail;
301 * ---------------------------
305 * Estimate the current action-value for state s and action a
306 * @param agent agent performing the estimation
309 * @return estimation value
312 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
317 for (i = 0; i < agent->m; i++)
319 result += state[i] * agent->W[action][i];
326 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
327 * currently estimated best action) in the current step
328 * @param agent agent performing the step
329 * @return yes, if exploring
332 agent_decide_exploration (struct RIL_Peer_Agent *agent)
334 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
335 UINT32_MAX) / (double) UINT32_MAX;
337 if (r < RIL_EXPLORE_RATIO)
345 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
346 * most reward in the future)
347 * @param agent agent performing the calculation
348 * @param state the state from which to take the action
349 * @return the action promising most future reward
352 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
357 double max_q = -DBL_MAX;
359 for (i = 0; i < agent->n; i++)
361 cur_q = agent_estimate_q (agent, state, i);
369 GNUNET_assert(-1 != max_i);
375 * Gets any action, to explore the action space from that state
376 * @param agent agent performing the calculation
377 * @param state the state from which to take the action
381 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
383 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
387 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
388 * @param agent the agent performing the update
389 * @param reward the reward received for the last action
390 * @param s_next the new state, the last step got the agent into
391 * @param a_prime the new
394 agent_update_weights (struct RIL_Peer_Agent *agent,
401 double *theta = agent->W[agent->a_old];
403 delta = reward + agent_estimate_q (agent, s_next, a_prime)
404 - agent_estimate_q (agent, agent->s_old, agent->a_old);
405 for (i = 0; i < agent->m; i++)
407 theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
412 * Changes the eligibility trace vector e in various manners:
413 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
414 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
415 * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
416 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
421 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
422 enum RIL_E_Modification mod)
425 double *e = agent->e;
426 double gamma = agent->envi->parameters.gamma;
427 double lambda = agent->envi->parameters.lambda;
429 for (i = 0; i < agent->m; i++)
433 case RIL_E_ACCUMULATE:
440 e[i] = gamma * lambda;
450 envi_change_active_address (struct GAS_RIL_Handle *solver,
451 struct RIL_Peer_Agent *agent,
452 struct ATS_Address *new_address,
453 unsigned long long new_bw_in,
454 unsigned long long new_bw_out)
456 int notify = GNUNET_NO;
458 if (agent->address != new_address)
460 agent->address->active = GNUNET_NO;
461 agent->address = new_address;
462 agent->address->active = GNUNET_YES;
463 agent->address->assigned_bw_in.value__ = htonl (agent->bw_in);
464 agent->address->assigned_bw_out.value__ = htonl (agent->bw_out);
465 notify |= GNUNET_YES;
467 if (agent->bw_in != new_bw_in)
469 agent->bw_in = new_bw_in;
470 agent->address->assigned_bw_in.value__ = htonl (new_bw_out);
471 notify |= GNUNET_YES;
473 if (agent->bw_out != new_bw_out)
475 agent->bw_out = new_bw_out;
476 agent->address->assigned_bw_out.value__ = htonl (new_bw_out);
477 notify |= GNUNET_YES;
483 solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls,
489 * Allocates a state vector and fills it with the features present
490 * @param solver the solver handle
491 * @return pointer to the state vector
494 envi_get_state (struct GAS_RIL_Handle *solver)
497 struct RIL_Network *net;
498 double *state = GNUNET_malloc (sizeof (double) * solver->networks_count * 4);
500 for (i = 0; i < solver->networks_count; i++)
502 net = &solver->network_entries[i];
503 state[i*4 + 0] = (double) net->bw_in_assigned;
504 state[i*4 + 1] = (double) net->bw_in_available;
505 state[i*4 + 2] = (double) net->bw_out_assigned;
506 state[i*4 + 3] = (double) net->bw_out_available;
513 * Gets the reward of the last performed step
514 * @param solver solver handle
518 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
520 //TODO! implement reward calculation
522 return (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
523 UINT32_MAX) / (double) UINT32_MAX;
527 envi_action_bw_double (struct GAS_RIL_Handle *solver,
528 struct RIL_Peer_Agent *agent,
533 envi_change_active_address(solver, agent, agent->address, agent->bw_in * 2, agent->bw_out);
537 envi_change_active_address(solver, agent, agent->address, agent->bw_in, agent->bw_out * 2);
542 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
543 struct RIL_Peer_Agent *agent,
546 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
547 unsigned long long new_bw;
551 new_bw = agent->bw_in / 2;
554 envi_change_active_address(solver, agent, agent->address, new_bw, agent->bw_out);
558 new_bw = agent->bw_out / 2;
561 envi_change_active_address(solver, agent, agent->address, agent->bw_in, new_bw);
566 * Puts the action into effect
567 * @param solver solver handle
568 * @param action action to perform by the solver
571 envi_do_action (struct GAS_RIL_Handle *solver,
572 struct RIL_Peer_Agent *agent,
577 case RIL_ACTION_BW_IN_DBL:
578 envi_action_bw_double (solver, agent, GNUNET_YES);
580 case RIL_ACTION_BW_IN_HLV:
581 envi_action_bw_halven (solver, agent, GNUNET_YES);
583 case RIL_ACTION_BW_OUT_DBL:
584 envi_action_bw_double (solver, agent, GNUNET_NO);
586 case RIL_ACTION_BW_OUT_HLV:
587 envi_action_bw_halven (solver, agent, GNUNET_NO);
593 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
594 * after having done the last action a_old. It observes the new state s_next and the reward
595 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
596 * next action is put into effect.
597 * @param agent the agent performing the step
600 agent_step (struct RIL_Peer_Agent *agent)
606 s_next = envi_get_state (agent->envi);
607 reward = envi_get_reward (agent->envi, agent);
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
610 agent->envi->parameters.algorithm ? "Q" : "SARSA");
612 switch (agent->envi->parameters.algorithm)
615 agent_modify_eligibility (agent, RIL_E_SET);
616 if (agent_decide_exploration (agent))
618 a_next = agent_get_action_explore (agent, s_next);
622 a_next = agent_get_action_best (agent, s_next);
624 //updates weights with selected action (on-policy), if not first step
625 if (-1 != agent->a_old)
626 agent_update_weights (agent, reward, s_next, a_next);
630 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
631 a_next = agent_get_action_best (agent, s_next);
632 if (-1 != agent->a_old)
633 agent_update_weights (agent, reward, s_next, a_next);
634 if (agent_decide_exploration (agent))
636 a_next = agent_get_action_explore (agent, s_next);
637 agent_modify_eligibility (agent, RIL_E_ZERO);
641 a_next = agent_get_action_best (agent, s_next);
642 agent_modify_eligibility (agent, RIL_E_SET);
647 GNUNET_assert(-1 != a_next);
649 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
651 envi_do_action (agent->envi, agent, a_next);
653 GNUNET_free(agent->s_old);
654 agent->s_old = s_next;
655 agent->a_old = a_next;
657 agent->step_count += 1;
661 * Cycles through all agents and lets the active ones do a step. Schedules the next step.
662 * @param solver the solver handle
663 * @param tc task context for the scheduler
666 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
668 struct GAS_RIL_Handle *solver = cls;
669 struct RIL_Peer_Agent *cur;
671 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n",
674 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
682 solver->step_count += 1;
683 solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time,
684 &ril_periodic_step, solver);
688 * Initialize an agent without addresses and its knowledge base
689 * @param s ril solver
690 * @param peer the one in question
691 * @return handle to the new agent
693 static struct RIL_Peer_Agent *
694 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
697 struct GAS_RIL_Handle * solver = s;
698 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
700 agent->envi = solver;
702 agent->step_count = 0;
703 agent->active = GNUNET_NO;
704 agent->s_old = envi_get_state (solver);
705 agent->n = RIL_ACTION_TYPE_NUM;
706 agent->m = solver->networks_count * 4;
707 agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
708 for (i = 0; i < agent->n; i++)
710 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
713 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
714 agent_modify_eligibility (agent, RIL_E_ZERO);
716 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail,
724 * @param s solver handle
725 * @param agent the agent to retire
728 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
732 for (i = 0; i < agent->n; i++)
734 GNUNET_free(agent->W[i]);
736 GNUNET_free(agent->W);
737 GNUNET_free(agent->e);
738 GNUNET_free(agent->s_old);
742 ril_remove_agent (struct GAS_RIL_Handle *s, struct RIL_Peer_Agent *agent)
744 struct RIL_Peer_Agent *cur_agent;
745 struct RIL_Peer_Agent *next_agent;
747 cur_agent = s->agents_head;
748 while (NULL != cur_agent)
750 next_agent = cur_agent->next;
752 if (agent == cur_agent)
754 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
755 agent_die (s, cur_agent);
758 cur_agent = next_agent;
763 * Counts the (active) agents
764 * @param solver solver handle
765 * @param active_only whether only active agents should be counted
766 * @return number of agents
769 //ril_count_agents (struct GAS_RIL_Handle *solver, int active_only)
772 // struct RIL_Peer_Agent *cur;
775 // for (cur = solver->agents_head; NULL != cur; cur = cur->next)
777 // if ((!active_only) || (active_only && cur->active))
788 * Returns the agent for a peer
789 * @param s solver handle
790 * @param peer identity of the peer
791 * @param create whether to create an agent if none is allocated yet
794 static struct RIL_Peer_Agent *
795 ril_get_agent (struct GAS_RIL_Handle *solver,
796 const struct GNUNET_PeerIdentity *peer,
799 struct RIL_Peer_Agent *cur;
801 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
803 if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey))
810 return agent_init (solver, peer);
815 ril_network_is_active (struct RIL_Network *network)
817 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
819 if (network->bw_out_available < min_bw)
825 * Iterator, which allocates one agent per peer
828 * @param key peer identity
829 * @param value address
830 * @return whether iterator should continue
833 ril_init_agents_it (void *cls, const struct GNUNET_HashCode *key, void *value)
835 struct GAS_RIL_Handle *solver = cls;
836 struct ATS_Address *address = value;
837 struct RIL_Peer_Agent *agent = NULL;
838 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
840 if (ril_network_is_active(address->solver_information))
842 agent = ril_get_agent (solver, &address->peer, GNUNET_YES);
844 GNUNET_assert(NULL != agent);
846 if (NULL == agent->address)
848 agent->address = address;
849 agent->address->active = GNUNET_YES;
850 agent->bw_in = min_bw;
851 agent->address->assigned_bw_in.value__ = htonl (min_bw);
852 agent->bw_out = min_bw;
853 agent->address->assigned_bw_out.value__ = htonl (min_bw);
860 ril_get_new_address_or_delete (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
862 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
863 //get new address for agent or delete agent
865 agent->address = NULL; //forget current address
866 GNUNET_CONTAINER_multihashmap_iterate (solver->addresses, &ril_init_agents_it,
867 solver); //put another address
869 if (NULL == agent->address) //no other address available
871 agent->active = GNUNET_NO;
872 ril_remove_agent(solver, agent);
876 envi_change_active_address(solver, agent, agent->address, min_bw, min_bw);
881 * Lookup network struct by type
883 * @param s the solver handle
884 * @param type the network type
885 * @return the network struct
887 static struct RIL_Network *
888 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
892 for (i = 0; i < s->networks_count; i++)
894 if (s->network_entries[i].type == type) {
895 return &s->network_entries[i];
902 * Solver API functions
903 * ---------------------------
907 * Changes the preferences for a peer in the problem
909 * @param solver the solver handle
910 * @param peer the peer to change the preference for
911 * @param kind the kind to change the preference
912 * @param pref_rel the normalized preference value for this kind over all clients
915 GAS_ril_address_change_preference (void *s,
916 const struct GNUNET_PeerIdentity *peer,
917 enum GNUNET_ATS_PreferenceKind kind,
920 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
921 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
922 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
924 * Nothing to do here. Preferences are considered during reward calculation.
929 * Init the reinforcement learning problem solver
932 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
933 * out_quota[i] contains outbound quota for network type i
934 * in_quota[i] contains inbound quota for network type i
937 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
938 * network[2] == GNUNET_ATS_NET_LAN
939 * out_quota[2] == 65353
940 * in_quota[2] == 65353
942 * @param cfg configuration handle
943 * @param stats the GNUNET_STATISTICS handle
944 * @param network array of GNUNET_ATS_NetworkType with length dest_length
945 * @param addresses hashmap containing all addresses
946 * @param out_quota array of outbound quotas
947 * @param in_quota array of outbound quota
948 * @param dest_length array length for quota arrays
949 * @param bw_changed_cb callback for changed bandwidth amounts
950 * @param bw_changed_cb_cls cls for callback
951 * @param get_preference callback to get relative preferences for a peer
952 * @param get_preference_cls cls for callback to get relative preferences
953 * @param get_properties_cls for callback to get relative properties
954 * @param get_properties_cls cls for callback to get relative properties
955 * @return handle for the solver on success, NULL on fail
958 GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
959 const struct GNUNET_STATISTICS_Handle *stats,
960 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
962 unsigned long long *out_quota,
963 unsigned long long *in_quota,
965 GAS_bandwidth_changed_cb bw_changed_cb,
966 void *bw_changed_cb_cls,
967 GAS_get_preferences get_preference,
968 void *get_preference_cls,
969 GAS_get_properties get_properties,
970 void *get_properties_cls)
973 unsigned long long tmp;
975 struct RIL_Network * cur;
976 struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle));
978 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
980 GNUNET_assert(NULL != cfg);
981 GNUNET_assert(NULL != stats);
982 GNUNET_assert(NULL != network);
983 GNUNET_assert(NULL != bw_changed_cb);
984 GNUNET_assert(NULL != get_preference);
985 GNUNET_assert(NULL != get_properties);
988 != GNUNET_CONFIGURATION_get_value_time (cfg, "ats", "RIL_STEP_TIME",
991 solver->step_time = RIL_DEFAULT_STEP_TIME;
994 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "RIL_ALGORITHM",
995 &string) && NULL != string && 0 == strcmp (string, "SARSA"))
997 solver->parameters.algorithm = RIL_ALGO_SARSA;
1001 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1004 == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_DISCOUNT_FACTOR",
1007 solver->parameters.gamma = (double) tmp / 100;
1011 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
1014 == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1015 "RIL_GRADIENT_STEP_SIZE", &tmp))
1017 solver->parameters.alpha = (double) tmp / 100;
1021 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1024 == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_TRACE_DECAY",
1027 solver->parameters.lambda = (double) tmp / 100;
1031 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1034 solver->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1035 solver->callbacks = GNUNET_malloc (sizeof (struct RIL_Callbacks));
1036 solver->callbacks->bw_changed = bw_changed_cb;
1037 solver->callbacks->bw_changed_cls = bw_changed_cb_cls;
1038 solver->callbacks->get_preferences = get_preference;
1039 solver->callbacks->get_preferences_cls = get_preference_cls;
1040 solver->callbacks->get_properties = get_properties;
1041 solver->callbacks->get_properties_cls = get_properties_cls;
1042 solver->networks_count = dest_length;
1043 solver->network_entries =
1044 GNUNET_malloc (dest_length * sizeof (struct RIL_Network));
1045 solver->bulk_lock = GNUNET_NO;
1046 solver->addresses = addresses;
1047 solver->step_count = 0;
1049 for (c = 0; c < dest_length; c++)
1051 cur = &solver->network_entries[c];
1052 cur->type = network[c];
1053 cur->bw_in_available = in_quota[c];
1054 cur->bw_in_assigned = 0;
1055 cur->bw_out_available = out_quota[c];
1056 cur->bw_out_assigned = 0;
1059 solver->next_step = GNUNET_SCHEDULER_add_delayed (
1060 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (),
1061 1000), &ril_periodic_step, solver);
1067 * Shutdown the reinforcement learning problem solver
1069 * @param solver the respective handle to shutdown
1072 GAS_ril_done (void * solver)
1074 struct GAS_RIL_Handle *s = solver;
1075 struct RIL_Peer_Agent *cur_agent;
1076 struct RIL_Peer_Agent *next_agent;
1078 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1080 cur_agent = s->agents_head;
1081 while (NULL != cur_agent)
1083 next_agent = cur_agent->next;
1084 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1085 agent_die (s, cur_agent);
1086 cur_agent = next_agent;
1089 GNUNET_SCHEDULER_cancel (s->next_step);
1090 GNUNET_free(s->callbacks);
1091 GNUNET_free(s->network_entries);
1096 * Add a single address within a network to the solver
1098 * @param solver the solver Handle
1099 * @param address the address to add
1100 * @param network network type of this address
1103 GAS_ril_address_add (void *solver,
1104 struct ATS_Address *address,
1107 struct GAS_RIL_Handle *s = solver;
1108 //TODO! implement solver address add
1111 * initialize new agent
1113 * increase state vector
1118 address->solver_information = ril_get_network(s, network);
1121 * reiterate all addresses, create new agent if necessary and give the agent the address
1123 GNUNET_CONTAINER_multihashmap_iterate (s->addresses, &ril_init_agents_it,
1126 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1127 "API_address_add() Added %s address for peer '%s'\n", address->plugin,
1128 GNUNET_i2s (&address->peer));
1132 * Remove an address from the solver
1134 * @param solver the solver handle
1135 * @param address the address to remove
1136 * @param session_only delete only session not whole address
1139 GAS_ril_address_delete (void *solver,
1140 struct ATS_Address *address,
1143 //TODO! implement solver address delete
1144 //TODO! delete session only
1147 * if (last address of peer)
1150 * decrease state vector
1151 * decrease knowledge matrix
1152 * decrease action vector
1154 struct GAS_RIL_Handle *s = solver;
1155 struct RIL_Peer_Agent *agent;
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() deleting %s address %p for peer '%s'\n",
1158 address->active ? "active" : "inactive",
1160 GNUNET_i2s(&address->peer));
1162 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() deleting address for unallocated agent\n");
1170 if (address == agent->address) //if used address deleted
1172 address->active = GNUNET_NO;
1173 ril_get_new_address_or_delete(s, agent);
1176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1177 "API_address_delete() Deleted %s%s address for peer '%s'\n",
1178 session_only ? "session for " : "", address->plugin,
1179 GNUNET_i2s (&address->peer));
1183 * Transport properties for this address have changed
1185 * @param solver solver handle
1186 * @param address the address
1187 * @param type the ATSI type in HBO
1188 * @param abs_value the absolute value of the property
1189 * @param rel_value the normalized value
1192 GAS_ril_address_property_changed (void *solver,
1193 struct ATS_Address *address,
1198 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1199 "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1200 "to %.2f \n", GNUNET_ATS_print_property_type (type),
1201 GNUNET_i2s (&address->peer), address, rel_value);
1203 * Nothing to do here, properties are considered in every reward calculation
1208 * Transport session for this address has changed
1210 * NOTE: values in addresses are already updated
1212 * @param solver solver handle
1213 * @param address the address
1214 * @param cur_session the current session
1215 * @param new_session the new session
1218 GAS_ril_address_session_changed (void *solver,
1219 struct ATS_Address *address,
1220 uint32_t cur_session,
1221 uint32_t new_session)
1223 //TODO? consider session changed in solver behaviour
1225 * Potentially add session activity as a feature in state vector
1227 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1231 * Usage for this address has changed
1233 * NOTE: values in addresses are already updated
1235 * @param solver solver handle
1236 * @param address the address
1237 * @param in_use usage state
1240 GAS_ril_address_inuse_changed (void *solver,
1241 struct ATS_Address *address,
1244 //TODO! consider address_inuse_changed according to matthias' email
1246 * See matthias' email
1248 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1249 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1250 address->plugin, GNUNET_i2s (&address->peer),
1251 (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1255 * Network scope for this address has changed
1257 * NOTE: values in addresses are already updated
1259 * @param solver solver handle
1260 * @param address the address
1261 * @param current_network the current network
1262 * @param new_network the new network
1265 GAS_ril_address_change_network (void *solver,
1266 struct ATS_Address *address,
1267 uint32_t current_network,
1268 uint32_t new_network)
1270 struct GAS_RIL_Handle *s = solver;
1271 struct RIL_Peer_Agent *agent;
1272 struct RIL_Network *net;
1274 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1275 "API_address_change_network() Network type changed, moving "
1276 "%s address of peer %s from '%s' to '%s'\n",
1277 (GNUNET_YES == address->active) ? "active" : "inactive",
1278 GNUNET_i2s (&address->peer),
1279 GNUNET_ATS_print_network_type (current_network),
1280 GNUNET_ATS_print_network_type (new_network));
1282 address->solver_information = ril_get_network(solver, new_network);
1284 if (address->active)
1286 agent = ril_get_agent(solver, &address->peer, GNUNET_NO);
1288 //remove from old network
1289 net = ril_get_network (s, current_network);
1290 net->bw_in_assigned -= agent->bw_in;
1291 net->bw_out_assigned -= agent->bw_out;
1293 if (ril_network_is_active(ril_get_network(s, new_network)))
1295 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New network is active\n");
1296 //add to new network
1297 net = ril_get_network (s, new_network);
1298 net->bw_in_assigned += agent->bw_in;
1299 net->bw_out_assigned += agent->bw_out;
1301 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1302 "API_address_change_network() Moved %d inbound and %d "
1303 "outbound\n", agent->bw_in, agent->bw_out);
1305 else //new network for this address is not active => address must not be considered
1307 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New network is not active\n");
1309 net = agent->address->solver_information;
1310 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Before: active address %p is %s\n", agent->address, GNUNET_ATS_print_network_type(net->type));
1312 address->active = GNUNET_NO;
1313 ril_get_new_address_or_delete(s, agent);
1315 net = agent->address->solver_information;
1316 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "After: active address %p is %s\n", agent->address, GNUNET_ATS_print_network_type(net->type));
1322 * Get application feedback for a peer
1324 * @param solver the solver handle
1325 * @param application the application
1326 * @param peer the peer to change the preference for
1327 * @param scope the time interval for this feedback: [now - scope .. now]
1328 * @param kind the kind to change the preference
1329 * @param score the score
1332 GAS_ril_address_preference_feedback (void *solver,
1334 const struct GNUNET_PeerIdentity *peer,
1335 const struct GNUNET_TIME_Relative scope,
1336 enum GNUNET_ATS_PreferenceKind kind,
1339 //TODO! collect reward until next reward calculation
1340 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1341 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1342 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1343 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1347 * Start a bulk operation
1349 * @param solver the solver
1352 GAS_ril_bulk_start (void *solver)
1354 //TODO? consideration: keep bulk counter and stop agents during bulk
1356 * bulk counter up, but not really relevant, because there is no complete calculation of the
1357 * bandwidth assignment triggered anyway. Therefore, changes to addresses can come and go as
1358 * they want. Consideration: Step-pause during bulk-start-stop period...
1361 //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n");
1365 * Bulk operation done
1368 GAS_ril_bulk_stop (void *solver)
1370 //TODO? consideration: keep bulk counter and stop agents during bulk
1372 * bulk counter down, see bulk_start()
1375 //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n");
1379 * Get the preferred address for a specific peer
1381 * @param solver the solver handle
1382 * @param peer the identity of the peer
1384 const struct ATS_Address *
1385 GAS_ril_get_preferred_address (void *solver,
1386 const struct GNUNET_PeerIdentity *peer)
1389 * activate agent, return currently chosen address
1391 struct GAS_RIL_Handle *s = solver;
1392 struct RIL_Peer_Agent *agent;
1394 agent = ril_get_agent (s, peer, GNUNET_NO);
1398 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1399 "API_get_preferred_address() No agent for peer '%s' do not suggest address\n",
1404 agent->active = GNUNET_YES;
1406 GNUNET_assert(NULL != agent->address);
1408 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1409 "API_get_preferred_address() Activated agent for peer '%s' with %s address\n",
1410 GNUNET_i2s (peer), agent->address->plugin);
1412 return agent->address;
1416 * Stop notifying about address and bandwidth changes for this peer
1418 * @param solver the solver handle
1419 * @param peer the peer
1422 GAS_ril_stop_get_preferred_address (void *solver,
1423 const struct GNUNET_PeerIdentity *peer)
1425 struct GAS_RIL_Handle *s = solver;
1426 struct RIL_Peer_Agent *agent;
1428 agent = ril_get_agent (s, peer, GNUNET_NO);
1429 agent->active = GNUNET_NO;
1431 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1432 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1433 GNUNET_i2s (peer), agent->address->plugin);
1436 /* end of gnunet-service-ats-solver_ril.c */