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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "agent_update_weights() MUH a_old = %d\n", agent->a_old);
404 delta = reward + agent_estimate_q (agent, s_next, a_prime)
405 - agent_estimate_q (agent, agent->s_old, agent->a_old);
406 for (i = 0; i < agent->m; i++)
408 theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
413 * Changes the eligibility trace vector e in various manners:
414 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
415 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
416 * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
417 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
422 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
423 enum RIL_E_Modification mod)
426 double *e = agent->e;
427 double gamma = agent->envi->parameters.gamma;
428 double lambda = agent->envi->parameters.lambda;
430 for (i = 0; i < agent->m; i++)
434 case RIL_E_ACCUMULATE:
441 e[i] = gamma * lambda;
451 * Allocates a state vector and fills it with the features present
452 * @param solver the solver handle
453 * @return pointer to the state vector
456 envi_get_state (struct GAS_RIL_Handle *solver)
459 struct RIL_Network *net;
460 double *state = GNUNET_malloc (sizeof (double) * solver->networks_count * 4);
462 for (i = 0; i < solver->networks_count; i++)
464 net = &solver->network_entries[i];
465 state[i*4 + 0] = (double) net->bw_in_assigned;
466 state[i*4 + 1] = (double) net->bw_in_available;
467 state[i*4 + 2] = (double) net->bw_out_assigned;
468 state[i*4 + 3] = (double) net->bw_out_available;
475 * Gets the reward of the last performed step
476 * @param solver solver handle
480 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
482 //TODO! implement reward calculation
484 return (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
485 UINT32_MAX) / (double) UINT32_MAX;
489 envi_action_bw_double (struct GAS_RIL_Handle *solver,
490 struct RIL_Peer_Agent *agent,
496 agent->address->assigned_bw_in.value__ = htonl (agent->bw_in);
497 solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls,
503 agent->address->assigned_bw_out.value__ = htonl (agent->bw_out);
504 solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls,
510 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
511 struct RIL_Peer_Agent *agent,
514 if ((direction_in && 1 == agent->bw_in)
515 || (!direction_in && 1 == agent->bw_out))
522 agent->address->assigned_bw_in.value__ = htonl (agent->bw_in);
523 solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls,
529 agent->address->assigned_bw_out.value__ = htonl (agent->bw_out);
530 solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls,
536 * Puts the action into effect
537 * @param solver solver handle
538 * @param action action to perform by the solver
541 envi_do_action (struct GAS_RIL_Handle *solver,
542 struct RIL_Peer_Agent *agent,
547 case RIL_ACTION_BW_IN_DBL:
548 envi_action_bw_double (solver, agent, GNUNET_YES);
550 case RIL_ACTION_BW_IN_HLV:
551 envi_action_bw_halven (solver, agent, GNUNET_YES);
553 case RIL_ACTION_BW_OUT_DBL:
554 envi_action_bw_double (solver, agent, GNUNET_NO);
556 case RIL_ACTION_BW_OUT_HLV:
557 envi_action_bw_halven (solver, agent, GNUNET_NO);
563 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
564 * after having done the last action a_old. It observes the new state s_next and the reward
565 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
566 * next action is put into effect.
567 * @param agent the agent performing the step
570 agent_step (struct RIL_Peer_Agent *agent)
576 s_next = envi_get_state (agent->envi);
577 reward = envi_get_reward (agent->envi, agent);
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
580 agent->envi->parameters.algorithm ? "Q" : "SARSA");
582 switch (agent->envi->parameters.algorithm)
585 agent_modify_eligibility (agent, RIL_E_SET);
586 if (agent_decide_exploration (agent))
588 a_next = agent_get_action_explore (agent, s_next);
592 a_next = agent_get_action_best (agent, s_next);
594 //updates weights with selected action (on-policy), if not first step
595 if (-1 != agent->a_old)
596 agent_update_weights (agent, reward, s_next, a_next);
600 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
601 a_next = agent_get_action_best (agent, s_next);
602 if (-1 != agent->a_old)
603 agent_update_weights (agent, reward, s_next, a_next);
604 if (agent_decide_exploration (agent))
606 a_next = agent_get_action_explore (agent, s_next);
607 agent_modify_eligibility (agent, RIL_E_ZERO);
611 a_next = agent_get_action_best (agent, s_next);
612 agent_modify_eligibility (agent, RIL_E_SET);
617 GNUNET_assert(-1 != a_next);
619 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
621 envi_do_action (agent->envi, agent, a_next);
623 GNUNET_free(agent->s_old);
624 agent->s_old = s_next;
625 agent->a_old = a_next;
627 agent->step_count += 1;
631 * Cycles through all agents and lets the active ones do a step. Schedules the next step.
632 * @param solver the solver handle
633 * @param tc task context for the scheduler
636 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
638 struct GAS_RIL_Handle *solver = cls;
639 struct RIL_Peer_Agent *cur;
641 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n",
644 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
652 solver->step_count += 1;
653 solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time,
654 &ril_periodic_step, solver);
658 * Initialize an agent without addresses and its knowledge base
659 * @param s ril solver
660 * @param peer the one in question
661 * @return handle to the new agent
663 static struct RIL_Peer_Agent *
664 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
667 struct GAS_RIL_Handle * solver = s;
668 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
670 agent->envi = solver;
672 agent->step_count = 0;
673 agent->active = GNUNET_NO;
674 agent->s_old = envi_get_state (solver);
675 agent->n = RIL_ACTION_TYPE_NUM;
676 agent->m = solver->networks_count * 4;
677 agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
678 for (i = 0; i < agent->n; i++)
680 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
683 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
684 agent_modify_eligibility (agent, RIL_E_ZERO);
686 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail,
694 * @param s solver handle
695 * @param agent the agent to retire
698 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
702 for (i = 0; i < agent->n; i++)
704 GNUNET_free(agent->W[i]);
706 GNUNET_free(agent->W);
707 GNUNET_free(agent->e);
708 GNUNET_free(agent->s_old);
712 ril_remove_agent (struct GAS_RIL_Handle *s, struct RIL_Peer_Agent *agent)
714 struct RIL_Peer_Agent *cur_agent;
715 struct RIL_Peer_Agent *next_agent;
717 cur_agent = s->agents_head;
718 while (NULL != cur_agent)
720 next_agent = cur_agent->next;
722 if (agent == cur_agent)
723 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
724 agent_die (s, cur_agent);
726 cur_agent = next_agent;
731 * Counts the (active) agents
732 * @param solver solver handle
733 * @param active_only whether only active agents should be counted
734 * @return number of agents
737 ril_count_agents (struct GAS_RIL_Handle *solver, int active_only)
740 struct RIL_Peer_Agent *cur;
743 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
745 if ((!active_only) || (active_only && cur->active))
754 * Returns the agent for a peer
755 * @param s solver handle
756 * @param peer identity of the peer
757 * @param create whether to create an agent if none is allocated yet
760 static struct RIL_Peer_Agent *
761 ril_get_agent (struct GAS_RIL_Handle *solver,
762 const struct GNUNET_PeerIdentity *peer,
765 struct RIL_Peer_Agent *cur;
767 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
769 if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey))
776 return agent_init (solver, peer);
781 ril_network_is_active (struct RIL_Network *network)
783 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
785 if (network->bw_out_available < min_bw)
791 * Iterator, which allocates one agent per peer
794 * @param key peer identity
795 * @param value address
796 * @return whether iterator should continue
799 ril_init_agents_it (void *cls, const struct GNUNET_HashCode *key, void *value)
801 struct GAS_RIL_Handle *solver = cls;
802 struct ATS_Address *address = value;
803 struct RIL_Peer_Agent *agent = NULL;
804 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
806 if (ril_network_is_active(address->solver_information))
808 agent = ril_get_agent (solver, &address->peer, GNUNET_YES);
810 GNUNET_assert(NULL != agent);
812 if (NULL == agent->address)
814 agent->address = address;
815 agent->address->active = GNUNET_YES;
816 agent->bw_in = min_bw;
817 agent->address->assigned_bw_in.value__ = htonl (min_bw);
818 agent->bw_out = min_bw;
819 agent->address->assigned_bw_out.value__ = htonl (min_bw);
826 * Lookup network struct by type
828 * @param s the solver handle
829 * @param type the network type
830 * @return the network struct
832 static struct RIL_Network *
833 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
837 for (i = 0; i < s->networks_count; i++)
839 if (s->network_entries[i].type == type) {
840 return &s->network_entries[i];
847 * Solver API functions
848 * ---------------------------
852 * Changes the preferences for a peer in the problem
854 * @param solver the solver handle
855 * @param peer the peer to change the preference for
856 * @param kind the kind to change the preference
857 * @param pref_rel the normalized preference value for this kind over all clients
860 GAS_ril_address_change_preference (void *s,
861 const struct GNUNET_PeerIdentity *peer,
862 enum GNUNET_ATS_PreferenceKind kind,
865 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
866 "API_address_change_preference() Preference `%s' for peer `%s' changed to %.2f \n",
867 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
869 * Nothing to do here. Preferences are considered during reward calculation.
874 * Init the reinforcement learning problem solver
877 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
878 * out_quota[i] contains outbound quota for network type i
879 * in_quota[i] contains inbound quota for network type i
882 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
883 * network[2] == GNUNET_ATS_NET_LAN
884 * out_quota[2] == 65353
885 * in_quota[2] == 65353
887 * @param cfg configuration handle
888 * @param stats the GNUNET_STATISTICS handle
889 * @param network array of GNUNET_ATS_NetworkType with length dest_length
890 * @param addresses hashmap containing all addresses
891 * @param out_quota array of outbound quotas
892 * @param in_quota array of outbound quota
893 * @param dest_length array length for quota arrays
894 * @param bw_changed_cb callback for changed bandwidth amounts
895 * @param bw_changed_cb_cls cls for callback
896 * @param get_preference callback to get relative preferences for a peer
897 * @param get_preference_cls cls for callback to get relative preferences
898 * @param get_properties_cls for callback to get relative properties
899 * @param get_properties_cls cls for callback to get relative properties
900 * @return handle for the solver on success, NULL on fail
903 GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
904 const struct GNUNET_STATISTICS_Handle *stats,
905 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
907 unsigned long long *out_quota,
908 unsigned long long *in_quota,
910 GAS_bandwidth_changed_cb bw_changed_cb,
911 void *bw_changed_cb_cls,
912 GAS_get_preferences get_preference,
913 void *get_preference_cls,
914 GAS_get_properties get_properties,
915 void *get_properties_cls)
918 unsigned long long tmp;
920 struct RIL_Network * cur;
921 struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle));
923 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
925 GNUNET_assert(NULL != cfg);
926 GNUNET_assert(NULL != stats);
927 GNUNET_assert(NULL != network);
928 GNUNET_assert(NULL != bw_changed_cb);
929 GNUNET_assert(NULL != get_preference);
930 GNUNET_assert(NULL != get_properties);
933 != GNUNET_CONFIGURATION_get_value_time (cfg, "ats", "RIL_STEP_TIME",
936 solver->step_time = RIL_DEFAULT_STEP_TIME;
939 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "RIL_ALGORITHM",
940 &string) && NULL != string && 0 == strcmp (string, "SARSA"))
942 solver->parameters.algorithm = RIL_ALGO_SARSA;
946 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
949 == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_DISCOUNT_FACTOR",
952 solver->parameters.gamma = (double) tmp / 100;
956 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
959 == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
960 "RIL_GRADIENT_STEP_SIZE", &tmp))
962 solver->parameters.alpha = (double) tmp / 100;
966 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
969 == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_TRACE_DECAY",
972 solver->parameters.lambda = (double) tmp / 100;
976 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
979 solver->stats = (struct GNUNET_STATISTICS_Handle *) stats;
980 solver->callbacks = GNUNET_malloc (sizeof (struct RIL_Callbacks));
981 solver->callbacks->bw_changed = bw_changed_cb;
982 solver->callbacks->bw_changed_cls = bw_changed_cb_cls;
983 solver->callbacks->get_preferences = get_preference;
984 solver->callbacks->get_preferences_cls = get_preference_cls;
985 solver->callbacks->get_properties = get_properties;
986 solver->callbacks->get_properties_cls = get_properties_cls;
987 solver->networks_count = dest_length;
988 solver->network_entries =
989 GNUNET_malloc (dest_length * sizeof (struct RIL_Network));
990 solver->bulk_lock = GNUNET_NO;
991 solver->addresses = addresses;
992 solver->step_count = 0;
994 for (c = 0; c < dest_length; c++)
996 cur = &solver->network_entries[c];
997 cur->type = network[c];
998 cur->bw_in_available = in_quota[c];
999 cur->bw_in_assigned = 0;
1000 cur->bw_out_available = out_quota[c];
1001 cur->bw_out_assigned = 0;
1004 solver->next_step = GNUNET_SCHEDULER_add_delayed (
1005 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (),
1006 1000), &ril_periodic_step, solver);
1012 * Shutdown the reinforcement learning problem solver
1014 * @param solver the respective handle to shutdown
1017 GAS_ril_done (void * solver)
1019 struct GAS_RIL_Handle *s = solver;
1020 struct RIL_Peer_Agent *cur_agent;
1021 struct RIL_Peer_Agent *next_agent;
1023 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1025 cur_agent = s->agents_head;
1026 while (NULL != cur_agent)
1028 next_agent = cur_agent->next;
1029 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1030 agent_die (s, cur_agent);
1031 cur_agent = next_agent;
1034 GNUNET_SCHEDULER_cancel (s->next_step);
1035 GNUNET_free(s->callbacks);
1036 GNUNET_free(s->network_entries);
1041 * Add a single address within a network to the solver
1043 * @param solver the solver Handle
1044 * @param address the address to add
1045 * @param network network type of this address
1048 GAS_ril_address_add (void *solver,
1049 struct ATS_Address *address,
1052 struct GAS_RIL_Handle *s = solver;
1053 //TODO! implement solver address add
1056 * initialize new agent
1058 * increase state vector
1063 address->solver_information = ril_get_network(s, network);
1066 * reiterate all addresses, create new agent if necessary and give the agent the address
1068 GNUNET_CONTAINER_multihashmap_iterate (s->addresses, &ril_init_agents_it,
1071 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1072 "API_address_add() Added %s address for peer '%s'\n", address->plugin,
1073 GNUNET_i2s (&address->peer));
1077 * Remove an address from the solver
1079 * @param solver the solver handle
1080 * @param address the address to remove
1081 * @param session_only delete only session not whole address
1084 GAS_ril_address_delete (void *solver,
1085 struct ATS_Address *address,
1088 //TODO! implement solver address delete
1089 //TODO! delete session only
1092 * if (last address of peer)
1095 * decrease state vector
1096 * decrease knowledge matrix
1097 * decrease action vector
1099 struct GAS_RIL_Handle *s = solver;
1100 struct RIL_Peer_Agent *agent;
1102 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() deleting address for unallocated agent");
1110 if (0 == memcmp (agent->address->addr, address->addr, address->addr_len)) //if used address deleted
1112 agent->address = NULL; //delete address
1113 GNUNET_CONTAINER_multihashmap_iterate (s->addresses, &ril_init_agents_it,
1114 solver); //put another address
1115 if (NULL == agent->address) //no other address available
1117 agent->active = GNUNET_NO;
1118 ril_remove_agent (solver, agent);
1122 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1123 "API_address_delete() Deleted %s%s address for peer '%s'\n",
1124 session_only ? "session for " : "", address->plugin,
1125 GNUNET_i2s (&address->peer));
1129 * Transport properties for this address have changed
1131 * @param solver solver handle
1132 * @param address the address
1133 * @param type the ATSI type in HBO
1134 * @param abs_value the absolute value of the property
1135 * @param rel_value the normalized value
1138 GAS_ril_address_property_changed (void *solver,
1139 struct ATS_Address *address,
1144 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1145 "API_address_property_changed() Property `%s' for peer `%s' address %p changed "
1146 "to %.2f \n", GNUNET_ATS_print_property_type (type),
1147 GNUNET_i2s (&address->peer), address, rel_value);
1149 * Nothing to do here, properties are considered in every reward calculation
1154 * Transport session for this address has changed
1156 * NOTE: values in addresses are already updated
1158 * @param solver solver handle
1159 * @param address the address
1160 * @param cur_session the current session
1161 * @param new_session the new session
1164 GAS_ril_address_session_changed (void *solver,
1165 struct ATS_Address *address,
1166 uint32_t cur_session,
1167 uint32_t new_session)
1169 //TODO? consider session changed in solver behaviour
1171 * Potentially add session activity as a feature in state vector
1173 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1177 * Usage for this address has changed
1179 * NOTE: values in addresses are already updated
1181 * @param solver solver handle
1182 * @param address the address
1183 * @param in_use usage state
1186 GAS_ril_address_inuse_changed (void *solver,
1187 struct ATS_Address *address,
1190 //TODO! consider address_inuse_changed according to matthias' email
1192 * See matthias' email
1194 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1195 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1196 address->plugin, GNUNET_i2s (&address->peer),
1197 (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1201 * Network scope for this address has changed
1203 * NOTE: values in addresses are already updated
1205 * @param solver solver handle
1206 * @param address the address
1207 * @param current_network the current network
1208 * @param new_network the new network
1211 GAS_ril_address_change_network (void *solver,
1212 struct ATS_Address *address,
1213 uint32_t current_network,
1214 uint32_t new_network)
1216 struct GAS_RIL_Handle *s = solver;
1217 struct RIL_Peer_Agent *agent;
1218 struct RIL_Network *net;
1220 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1221 "API_address_change_network() Network type changed, moving "
1222 "%s address of peer %s from `%s' to `%s'\n",
1223 (GNUNET_YES == address->active) ? "active" : "inactive",
1224 GNUNET_i2s (&address->peer),
1225 GNUNET_ATS_print_network_type (current_network),
1226 GNUNET_ATS_print_network_type (new_network));
1228 address->solver_information = ril_get_network(solver, new_network);
1230 if (address->active)
1232 agent = ril_get_agent(solver, &address->peer, GNUNET_NO);
1234 //remove from old network
1235 net = ril_get_network (s, current_network);
1236 net->bw_in_assigned -= agent->bw_in;
1237 net->bw_out_assigned -= agent->bw_out;
1239 if (ril_network_is_active(ril_get_network(s, new_network)))
1241 //add to new network
1242 net = ril_get_network (s, new_network);
1243 net->bw_in_assigned += agent->bw_in;
1244 net->bw_out_assigned += agent->bw_out;
1246 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1247 "API_address_change_network() Moved %d inbound and %d "
1248 "outbound\n", agent->bw_in, agent->bw_out);
1250 else //new network for this address is not active => address must not be considered
1252 address->active = GNUNET_NO;
1253 agent->address = NULL; //delete address
1254 GNUNET_CONTAINER_multihashmap_iterate (s->addresses, &ril_init_agents_it,
1255 solver); //put another address
1256 if (NULL == agent->address) //no other address available
1258 agent->active = GNUNET_NO;
1259 ril_remove_agent(s, agent);
1266 * Get application feedback for a peer
1268 * @param solver the solver handle
1269 * @param application the application
1270 * @param peer the peer to change the preference for
1271 * @param scope the time interval for this feedback: [now - scope .. now]
1272 * @param kind the kind to change the preference
1273 * @param score the score
1276 GAS_ril_address_preference_feedback (void *solver,
1278 const struct GNUNET_PeerIdentity *peer,
1279 const struct GNUNET_TIME_Relative scope,
1280 enum GNUNET_ATS_PreferenceKind kind,
1283 //TODO! collect reward until next reward calculation
1284 //TODO! Find out application
1285 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1286 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1287 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1288 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1292 * Start a bulk operation
1294 * @param solver the solver
1297 GAS_ril_bulk_start (void *solver)
1299 //TODO? consideration: keep bulk counter and stop agents during bulk
1301 * bulk counter up, but not really relevant, because there is no complete calculation of the
1302 * bandwidth assignment triggered anyway. Therefore, changes to addresses can come and go as
1303 * they want. Consideration: Step-pause during bulk-start-stop period...
1306 //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n");
1310 * Bulk operation done
1313 GAS_ril_bulk_stop (void *solver)
1315 //TODO? consideration: keep bulk counter and stop agents during bulk
1317 * bulk counter down, see bulk_start()
1320 //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n");
1324 * Get the preferred address for a specific peer
1326 * @param solver the solver handle
1327 * @param peer the identity of the peer
1329 const struct ATS_Address *
1330 GAS_ril_get_preferred_address (void *solver,
1331 const struct GNUNET_PeerIdentity *peer)
1334 * activate agent, return currently chosen address
1336 struct GAS_RIL_Handle *s = solver;
1337 struct RIL_Peer_Agent *agent;
1339 agent = ril_get_agent (s, peer, GNUNET_NO);
1346 agent->active = GNUNET_YES;
1348 GNUNET_assert(NULL != agent->address);
1350 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1351 "API_get_preferred_address() Activated agent for peer '%s' with %s address\n",
1352 GNUNET_i2s (peer), agent->address->plugin);
1354 return agent->address;
1358 * Stop notifying about address and bandwidth changes for this peer
1360 * @param solver the solver handle
1361 * @param peer the peer
1364 GAS_ril_stop_get_preferred_address (void *solver,
1365 const struct GNUNET_PeerIdentity *peer)
1367 struct GAS_RIL_Handle *s = solver;
1368 struct RIL_Peer_Agent *agent;
1370 agent = ril_get_agent (s, peer, GNUNET_NO);
1371 agent->active = GNUNET_NO;
1373 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1374 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1375 GNUNET_i2s (peer), agent->address->plugin);
1378 /* end of gnunet-service-ats-solver_ril.c */