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/libgnunet_plugin_ats_ril.c
23 * @brief ATS reinforcement learning solver
24 * @author Fabian Oehlmann
25 * @author Matthias Wachs
27 #include "libgnunet_plugin_ats_ril.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
31 #define RIL_ACTION_INVALID -1
32 #define RIL_FEATURES_ADDRESS_COUNT (3 + GNUNET_ATS_QualityPropertiesCount)
33 #define RIL_FEATURES_NETWORK_COUNT 4
34 #define RIL_INTERVAL_EXPONENT 10
36 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
37 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
38 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
39 #define RIL_DEFAULT_DISCOUNT_BETA 0.7
40 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.3
41 #define RIL_DEFAULT_TRACE_DECAY 0.5
42 #define RIL_DEFAULT_EXPLORE_RATIO 0.1
43 #define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5
46 * ATS reinforcement learning solver
52 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
53 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
54 * to a particular address. The action of switching to address with index i is depicted by the
55 * number (RIL_ACTION_TYPE_NUM + i).
59 RIL_ACTION_NOTHING = 0,
60 RIL_ACTION_BW_IN_DBL = 1,
61 RIL_ACTION_BW_IN_HLV = 2,
62 RIL_ACTION_BW_IN_INC = 3,
63 RIL_ACTION_BW_IN_DEC = 4,
64 RIL_ACTION_BW_OUT_DBL = 5,
65 RIL_ACTION_BW_OUT_HLV = 6,
66 RIL_ACTION_BW_OUT_INC = 7,
67 RIL_ACTION_BW_OUT_DEC = 8,
68 RIL_ACTION_TYPE_NUM = 9
77 enum RIL_E_Modification
86 * Global learning parameters
88 struct RIL_Learning_Parameters
91 * The TD-algorithm to use
93 enum RIL_Algorithm algorithm;
96 * Learning discount factor in the TD-update
101 * Gradient-descent step-size
106 * Trace-decay factor for eligibility traces
111 * Ratio, whith what probability an agent should explore in the e-greed policy
113 double explore_ratio;
116 * How big the share of the global part of the reward signal is
118 double reward_global_share;
121 * Minimal interval time between steps in milliseconds
123 struct GNUNET_TIME_Relative step_time_min;
126 * Maximum interval time between steps in milliseconds
128 struct GNUNET_TIME_Relative step_time_max;
132 * Wrapper for addresses to store them in agent's linked list
134 struct RIL_Address_Wrapped
139 struct RIL_Address_Wrapped *next;
144 struct RIL_Address_Wrapped *prev;
149 struct ATS_Address *address_naked;
152 struct RIL_Peer_Agent
155 * Next agent in solver's linked list
157 struct RIL_Peer_Agent *next;
160 * Previous agent in solver's linked list
162 struct RIL_Peer_Agent *prev;
167 struct GAS_RIL_Handle *envi;
172 struct GNUNET_PeerIdentity peer;
175 * Whether the agent is active or not
180 * Number of performed time-steps
182 unsigned long long step_count;
185 * Experience matrix W
190 * Number of rows of W / Number of state-vector features
195 * Number of columns of W / Number of actions
200 * Last perceived state feature vector
210 * Eligibility trace vector
217 struct ATS_Address * address_inuse;
220 * Head of addresses DLL
222 struct RIL_Address_Wrapped * addresses_head;
225 * Tail of addresses DLL
227 struct RIL_Address_Wrapped * addresses_tail;
230 * Inbound bandwidth assigned by the agent
232 unsigned long long bw_in;
235 * Outbound bandwidth assigned by the agent
237 unsigned long long bw_out;
240 * Flag whether a suggestion has to be issued
242 int suggestion_issue;
245 * The address which has to be issued ()
247 struct ATS_Address * suggestion_address;
255 enum GNUNET_ATS_Network_Type type;
258 * Total available inbound bandwidth
260 unsigned long long bw_in_available;
263 * Total available outbound bandwidth
265 unsigned long long bw_out_available;
269 * A handle for the reinforcement learning solver
271 struct GAS_RIL_Handle
274 * The solver-plugin environment of the solver-plugin API
276 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
281 struct GNUNET_STATISTICS_Handle *stats;
284 * Number of performed steps
286 unsigned long long step_count;
289 * Timestamp for the last time-step
291 struct GNUNET_TIME_Absolute step_time_last;
294 * Task identifier of the next time-step to be executed
296 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
299 * Whether a step is already scheduled
304 * Variable discount factor, dependent on time between steps
306 double discount_variable;
309 * Integrated variable discount factor, dependent on time between steps
311 double discount_integrated;
314 * State vector for networks for the current step
316 double *state_networks;
319 * Lock for bulk operations
324 * Number of changes during a lock
329 * Learning parameters
331 struct RIL_Learning_Parameters parameters;
334 * Array of networks with global assignment state
336 struct RIL_Network * network_entries;
341 unsigned int networks_count;
344 * List of active peer-agents
346 struct RIL_Peer_Agent * agents_head;
347 struct RIL_Peer_Agent * agents_tail;
352 * ---------------------------
356 * Estimate the current action-value for state s and action a
358 * @param agent agent performing the estimation
361 * @return estimation value
364 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
369 for (i = 0; i < agent->m; i++)
371 result += state[i] * agent->W[action][i];
378 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
379 * currently estimated best action) in the current step
381 * @param agent agent performing the step
382 * @return yes, if exploring
385 agent_decide_exploration (struct RIL_Peer_Agent *agent)
387 //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
388 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
389 UINT32_MAX) / (double) UINT32_MAX;
391 if (r < agent->envi->parameters.explore_ratio)
399 * Get the index of the address in the agent's list.
401 * @param agent agent handle
402 * @param address address handle
403 * @return the index, starting with zero
406 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
409 struct RIL_Address_Wrapped *cur;
412 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
415 if (cur->address_naked == address)
425 * Gets the wrapped address from the agent's list
427 * @param agent agent handle
428 * @param address address handle
429 * @return wrapped address
431 static struct RIL_Address_Wrapped *
432 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
434 struct RIL_Address_Wrapped *cur;
436 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
438 if (cur->address_naked == address)
448 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
449 * most reward in the future)
451 * @param agent agent performing the calculation
452 * @param state the state from which to take the action
453 * @return the action promising most future reward
456 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
459 int max_i = RIL_ACTION_INVALID;
461 double max_q = -DBL_MAX;
463 for (i = 0; i < agent->n; i++)
465 cur_q = agent_estimate_q (agent, state, i);
473 GNUNET_assert(RIL_ACTION_INVALID != max_i);
479 * Gets any action, to explore the action space from that state
481 * @param agent agent performing the calculation
482 * @param state the state from which to take the action
486 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
488 // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
489 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
493 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
495 * @param agent the agent performing the update
496 * @param reward the reward received for the last action
497 * @param s_next the new state, the last step got the agent into
498 * @param a_prime the new
501 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
505 double *theta = agent->W[agent->a_old];
507 delta = agent->envi->discount_integrated * reward; //reward
508 delta += agent->envi->discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
509 delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
510 for (i = 0; i < agent->m; i++)
512 theta[i] += agent->envi->parameters.alpha * delta * agent->e[i];
517 * Changes the eligibility trace vector e in various manners:
518 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
519 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
520 * RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
521 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
523 * @param agent the agent handle
524 * @param mod the kind of modification
527 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
530 double *e = agent->e;
532 for (i = 0; i < agent->m; i++)
536 case RIL_E_ACCUMULATE:
543 e[i] *= agent->envi->discount_variable * agent->envi->parameters.lambda;
553 ril_inform (struct GAS_RIL_Handle *solver,
554 enum GAS_Solver_Operation op,
555 enum GAS_Solver_Status stat)
557 if (NULL != solver->plugin_envi->info_cb)
558 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
562 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
563 * notify ATS of its new decision
565 * @param solver solver handle
566 * @param agent agent handle
567 * @param new_address the address which is to be used
568 * @param new_bw_in the new amount of inbound bandwidth set for this address
569 * @param new_bw_out the new amount of outbound bandwidth set for this address
570 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
573 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
574 struct RIL_Peer_Agent *agent,
575 struct ATS_Address *new_address,
576 unsigned long long new_bw_in,
577 unsigned long long new_bw_out,
580 int notify = GNUNET_NO;
582 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
585 if (agent->address_inuse != new_address)
587 if (NULL != agent->address_inuse)
589 agent->address_inuse->active = GNUNET_NO;
590 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
591 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
593 if (NULL != new_address)
595 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
596 new_address->active = agent->is_active;
597 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
598 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
600 notify |= GNUNET_YES;
606 if (new_address->active != agent->is_active)
608 new_address->active = agent->is_active;
609 notify |= GNUNET_YES;
613 if (agent->bw_in != new_bw_in)
615 agent->bw_in = new_bw_in;
616 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
617 notify |= GNUNET_YES;
619 if (agent->bw_out != new_bw_out)
621 agent->bw_out = new_bw_out;
622 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
623 notify |= GNUNET_YES;
627 if (notify && agent->is_active && (GNUNET_NO == silent))
631 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
632 agent->suggestion_issue = GNUNET_YES;
633 agent->suggestion_address = new_address;
635 else if (agent->address_inuse)
637 //disconnect case, no new address
638 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
639 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
643 agent->suggestion_issue = GNUNET_YES;
644 agent->suggestion_address = agent->address_inuse;
647 agent->address_inuse = new_address;
650 static unsigned long long
651 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
653 struct RIL_Peer_Agent *cur;
654 struct RIL_Network *net;
655 unsigned long long sum = 0;
657 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
659 if (cur->is_active && cur->address_inuse)
661 net = cur->address_inuse->solver_information;
662 if (net->type == type)
676 envi_state_networks (struct GAS_RIL_Handle *solver)
679 struct RIL_Network net;
681 for (i = 0; i < solver->networks_count; i++)
683 net = solver->network_entries[i];
684 solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) ril_network_get_assigned(solver, net.type, GNUNET_YES);
685 solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net.bw_in_available;
686 solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) ril_network_get_assigned(solver, net.type, GNUNET_NO);
687 solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net.bw_out_available;
692 * Allocates a state vector and fills it with the features present
693 * @param solver the solver handle
694 * @param agent the agent handle
695 * @return pointer to the state vector
698 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
702 double *state = GNUNET_malloc (sizeof (double) * agent->m);
703 struct RIL_Address_Wrapped *cur_address;
704 const double *preferences;
705 const double *properties;
707 //copy global networks state
708 for (i = 0; i < solver->networks_count; i++)
710 state[i * RIL_FEATURES_NETWORK_COUNT + 0] = solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 0];
711 state[i * RIL_FEATURES_NETWORK_COUNT + 1] = solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 1];
712 state[i * RIL_FEATURES_NETWORK_COUNT + 2] = solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 2];
713 state[i * RIL_FEATURES_NETWORK_COUNT + 3] = solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 3];
717 i = i * RIL_FEATURES_NETWORK_COUNT;
718 preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
720 for (k = 0; k < GNUNET_ATS_PreferenceCount; k++)
722 state[i++] = preferences[k];
725 //get address specific features
726 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
728 //when changing the number of address specific state features, change RIL_FEATURES_ADDRESS_COUNT macro
729 state[i++] = cur_address->address_naked->active;
730 state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
731 state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
732 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
733 cur_address->address_naked);
734 for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
736 state[i++] = properties[k];
744 * For all networks a peer has an address in, this gets the maximum bandwidth which could
745 * theoretically be available in one of the networks. This is used for bandwidth normalization.
747 * @param agent the agent handle
748 * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
750 static long long unsigned
751 ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
754 * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
755 * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
758 struct RIL_Address_Wrapped *cur;
759 struct RIL_Network *net;
761 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
763 net = cur->address_naked->solver_information;
766 if (net->bw_in_available > max)
768 max = net->bw_in_available;
773 if (net->bw_out_available > max)
775 max = net->bw_out_available;
783 * Get the index of the quality-property in question
785 * @param type the quality property type
789 ril_find_property_index (uint32_t type)
791 int existing_types[] = GNUNET_ATS_QualityProperties;
793 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
794 if (existing_types[c] == type)
796 return GNUNET_SYSERR;
800 ril_get_atsi (struct ATS_Address *address, uint32_t type)
803 GNUNET_assert(NULL != address);
805 if ((NULL == address->atsi) || (0 == address->atsi_count))
808 for (c1 = 0; c1 < address->atsi_count; c1++)
810 if (ntohl (address->atsi[c1].type) == type)
811 return ntohl (address->atsi[c1].value);
817 envi_reward_global (struct GAS_RIL_Handle *solver)
820 struct RIL_Network net;
821 unsigned int sum_in_available = 0;
822 unsigned int sum_out_available = 0;
823 unsigned int sum_in_assigned = 0;
824 unsigned int sum_out_assigned = 0;
828 for (i = 0; i < solver->networks_count; i++)
830 net = solver->network_entries[i];
831 sum_in_available += net.bw_in_available;
832 sum_in_assigned += ril_network_get_assigned(solver, net.type, GNUNET_YES);
833 sum_out_available += net.bw_out_available;
834 sum_out_assigned += ril_network_get_assigned(solver, net.type, GNUNET_NO);
837 ratio_in = ((double) sum_in_assigned) / ((double) sum_in_available);
838 ratio_out = ((double) sum_out_assigned) / ((double) sum_out_available);
840 // global reward in [1,2]
841 return ((ratio_in + ratio_out) / 2) + 1;
845 envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
847 const double *preferences;
848 const double *properties;
850 double pref_match = 0;
854 preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
856 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
857 agent->address_inuse);
860 prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
861 dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
863 // utilization in [0,1]
864 bw_norm = ((ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
865 / ril_get_max_bw (agent, GNUNET_YES))
866 + (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
867 / ril_get_max_bw (agent, GNUNET_NO))) / 2;
869 // preference matching in [0,4]
870 pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
871 pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
873 // local reward in [1,2]
874 return (pref_match / 4) +1;
878 * Gets the reward for the last performed step, which is calculated in equal
879 * parts from the local (the peer specific) and the global (for all peers
882 * @param solver the solver handle
883 * @param agent the agent handle
887 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
889 struct RIL_Network *net;
892 //punish overutilization
893 net = agent->address_inuse->solver_information;
894 if ((ril_network_get_assigned(solver, net->type, GNUNET_YES) > net->bw_in_available)
895 || (ril_network_get_assigned(solver, net->type, GNUNET_NO) > net->bw_out_available))
900 reward += envi_reward_global (solver) * (solver->parameters.reward_global_share);
901 reward += envi_reward_local (solver, agent) * (1 - solver->parameters.reward_global_share);
907 * Doubles the bandwidth for the active address
909 * @param solver solver handle
910 * @param agent agent handle
911 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
914 envi_action_bw_double (struct GAS_RIL_Handle *solver,
915 struct RIL_Peer_Agent *agent,
918 unsigned long long new_bw;
922 new_bw = agent->bw_in * 2;
923 if (new_bw < agent->bw_in)
925 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
926 agent->bw_out, GNUNET_NO);
930 new_bw = agent->bw_out * 2;
931 if (new_bw < agent->bw_out)
933 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
939 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
940 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
942 * @param solver solver handle
943 * @param agent agent handle
944 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
948 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
949 struct RIL_Peer_Agent *agent,
952 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
953 unsigned long long new_bw;
957 new_bw = agent->bw_in / 2;
958 if (new_bw < min_bw || new_bw > agent->bw_in)
960 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
965 new_bw = agent->bw_out / 2;
966 if (new_bw < min_bw || new_bw > agent->bw_out)
968 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
974 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
976 * @param solver solver handle
977 * @param agent agent handle
978 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
982 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
984 unsigned long long new_bw;
985 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
989 new_bw = agent->bw_in + (5 * min_bw);
990 if (new_bw < agent->bw_in)
992 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
993 agent->bw_out, GNUNET_NO);
997 new_bw = agent->bw_out + (5 * min_bw);
998 if (new_bw < agent->bw_out)
1000 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1006 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1007 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1009 * @param solver solver handle
1010 * @param agent agent handle
1011 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1015 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1017 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1018 unsigned long long new_bw;
1022 new_bw = agent->bw_in - (5 * min_bw);
1023 if (new_bw < min_bw || new_bw > agent->bw_in)
1025 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1030 new_bw = agent->bw_out - (5 * min_bw);
1031 if (new_bw < min_bw || new_bw > agent->bw_in)
1033 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1039 * Switches to the address given by its index
1041 * @param solver solver handle
1042 * @param agent agent handle
1043 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1046 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1047 struct RIL_Peer_Agent *agent,
1048 unsigned int address_index)
1050 struct RIL_Address_Wrapped *cur;
1053 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1055 if (i == address_index)
1057 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1065 //no address with address_index exists, in this case this action should not be callable
1066 GNUNET_assert(GNUNET_NO);
1070 * Puts the action into effect by calling the according function
1072 * @param solver the solver handle
1073 * @param agent the action handle
1074 * @param action the action to perform by the solver
1077 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1083 case RIL_ACTION_NOTHING:
1085 case RIL_ACTION_BW_IN_DBL:
1086 envi_action_bw_double (solver, agent, GNUNET_YES);
1088 case RIL_ACTION_BW_IN_HLV:
1089 envi_action_bw_halven (solver, agent, GNUNET_YES);
1091 case RIL_ACTION_BW_IN_INC:
1092 envi_action_bw_inc (solver, agent, GNUNET_YES);
1094 case RIL_ACTION_BW_IN_DEC:
1095 envi_action_bw_dec (solver, agent, GNUNET_YES);
1097 case RIL_ACTION_BW_OUT_DBL:
1098 envi_action_bw_double (solver, agent, GNUNET_NO);
1100 case RIL_ACTION_BW_OUT_HLV:
1101 envi_action_bw_halven (solver, agent, GNUNET_NO);
1103 case RIL_ACTION_BW_OUT_INC:
1104 envi_action_bw_inc (solver, agent, GNUNET_NO);
1106 case RIL_ACTION_BW_OUT_DEC:
1107 envi_action_bw_dec (solver, agent, GNUNET_NO);
1110 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1112 address_index = action - RIL_ACTION_TYPE_NUM;
1114 GNUNET_assert(address_index >= 0);
1116 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1118 envi_action_address_switch (solver, agent, address_index);
1121 // error - action does not exist
1122 GNUNET_assert(GNUNET_NO);
1127 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1128 * after having done the last action a_old. It observes the new state s_next and the reward
1129 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1130 * next action is put into effect.
1132 * @param agent the agent performing the step
1135 agent_step (struct RIL_Peer_Agent *agent)
1137 int a_next = RIL_ACTION_INVALID;
1141 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1142 GNUNET_i2s (&agent->peer),
1143 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1145 s_next = envi_get_state (agent->envi, agent);
1146 reward = envi_get_reward (agent->envi, agent);
1148 switch (agent->envi->parameters.algorithm)
1150 case RIL_ALGO_SARSA:
1151 agent_modify_eligibility (agent, RIL_E_SET);
1152 if (agent_decide_exploration (agent))
1154 a_next = agent_get_action_explore (agent, s_next);
1158 a_next = agent_get_action_best (agent, s_next);
1160 if (RIL_ACTION_INVALID != agent->a_old)
1162 //updates weights with selected action (on-policy), if not first step
1163 agent_update_weights (agent, reward, s_next, a_next);
1168 a_next = agent_get_action_best (agent, s_next);
1169 if (RIL_ACTION_INVALID != agent->a_old)
1171 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1172 agent_update_weights (agent, reward, s_next, a_next);
1174 if (agent_decide_exploration (agent))
1176 a_next = agent_get_action_explore (agent, s_next);
1177 agent_modify_eligibility (agent, RIL_E_ZERO);
1181 a_next = agent_get_action_best (agent, s_next);
1182 agent_modify_eligibility (agent, RIL_E_SET);
1187 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1189 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
1191 envi_do_action (agent->envi, agent, a_next);
1193 GNUNET_free(agent->s_old);
1194 agent->s_old = s_next;
1195 agent->a_old = a_next;
1197 agent->step_count += 1;
1201 ril_step (struct GAS_RIL_Handle *solver);
1204 * Task for the scheduler, which performs one step and lets the solver know that
1205 * no further step is scheduled.
1207 * @param cls the solver handle
1208 * @param tc the task context for the scheduler
1211 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1213 struct GAS_RIL_Handle *solver = cls;
1215 solver->task_pending = GNUNET_NO;
1220 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1223 struct RIL_Network net;
1224 unsigned long long sum_assigned = 0;
1225 unsigned long long sum_available = 0;
1228 for (i = 0; i < solver->networks_count; i++)
1230 net = solver->network_entries[i];
1231 if (ril_network_get_assigned(solver, net.type, GNUNET_YES) > 0) //only consider scopes with an active address
1233 sum_assigned += ril_network_get_assigned(solver, net.type, GNUNET_YES);
1234 sum_assigned += ril_network_get_assigned(solver, net.type, GNUNET_NO);
1235 sum_available += net.bw_in_available;
1236 sum_available += net.bw_out_available;
1239 if (sum_available > 0)
1241 ratio = ((double) sum_assigned) / ((double) sum_available);
1248 return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
1252 * Schedules the next global step in an adaptive way. The more resources are
1253 * left, the earlier the next step is scheduled. This serves the reactivity of
1254 * the solver to changed inputs.
1256 * @param solver the solver handle
1259 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1265 struct GNUNET_TIME_Relative time_next;
1267 if (solver->task_pending)
1269 GNUNET_SCHEDULER_cancel (solver->step_next_task_id);
1272 used_ratio = ril_get_used_resource_ratio (solver);
1275 solver->parameters.step_time_min.rel_value_us
1276 < solver->parameters.step_time_max.rel_value_us);
1278 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1279 solver->parameters.step_time_min).rel_value_us;
1280 offset = (double) solver->parameters.step_time_min.rel_value_us;
1281 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1283 GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1284 GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1286 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned int) y);
1288 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1290 solver->task_pending = GNUNET_YES;
1294 * Triggers one step per agent
1298 ril_step (struct GAS_RIL_Handle *solver)
1300 struct RIL_Peer_Agent *cur;
1301 struct GNUNET_TIME_Absolute time_now;
1302 struct GNUNET_TIME_Relative time_delta;
1305 if (GNUNET_YES == solver->bulk_lock)
1307 solver->bulk_changes++;
1311 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1313 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1315 if (0 == solver->step_count)
1317 solver->step_time_last = GNUNET_TIME_absolute_get ();
1320 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1321 time_now = GNUNET_TIME_absolute_get ();
1322 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1323 tau = ((double) time_delta.rel_value_us)
1324 / ((double) solver->parameters.step_time_min.rel_value_us);
1325 solver->step_time_last = time_now;
1327 //calculate reward discounts (once per step for all agents)
1328 solver->discount_variable = pow (M_E, ((-1.) * ((double) solver->parameters.beta) * tau));
1329 solver->discount_integrated = (1 - solver->discount_variable)
1330 / ((double) solver->parameters.beta);
1332 //calculate network state vector
1333 envi_state_networks(solver);
1335 //trigger one step per active agent
1336 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1338 if (cur->is_active && cur->address_inuse)
1344 solver->step_count += 1;
1345 ril_step_schedule_next (solver);
1347 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1349 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1350 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1352 if (cur->suggestion_issue) {
1353 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1354 cur->suggestion_issue = GNUNET_NO;
1357 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1363 ril_count_agents (struct GAS_RIL_Handle *solver)
1366 struct RIL_Peer_Agent *cur_agent;
1368 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1376 agent_w_start (struct RIL_Peer_Agent *agent)
1379 struct RIL_Peer_Agent *other;
1383 count = ril_count_agents(agent->envi);
1388 for (other = agent->envi->agents_head; NULL != other; other = other->next)
1390 for (i = 0; i < agent->n; i++)
1392 for (k = 0; k < agent->m; k++)
1394 agent->W[i][k] += (other->W[i][k] / (double) count);
1401 * Initialize an agent without addresses and its knowledge base
1403 * @param s ril solver
1404 * @param peer the one in question
1405 * @return handle to the new agent
1407 static struct RIL_Peer_Agent *
1408 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1411 struct GAS_RIL_Handle * solver = s;
1412 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
1414 agent->envi = solver;
1415 agent->peer = *peer;
1416 agent->step_count = 0;
1417 agent->is_active = GNUNET_NO;
1420 agent->suggestion_issue = GNUNET_NO;
1421 agent->n = RIL_ACTION_TYPE_NUM;
1422 agent->m = (solver->networks_count * RIL_FEATURES_NETWORK_COUNT) + GNUNET_ATS_PreferenceCount;
1423 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1424 for (i = 0; i < agent->n; i++)
1426 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1428 agent_w_start(agent);
1429 agent->a_old = RIL_ACTION_INVALID;
1430 agent->s_old = envi_get_state (solver, agent);
1431 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1432 agent_modify_eligibility (agent, RIL_E_ZERO);
1434 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
1442 * @param solver the solver handle
1443 * @param agent the agent to retire
1446 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1450 for (i = 0; i < agent->n; i++)
1452 GNUNET_free(agent->W[i]);
1454 GNUNET_free(agent->W);
1455 GNUNET_free(agent->e);
1456 GNUNET_free(agent->s_old);
1461 * Returns the agent for a peer
1463 * @param solver the solver handle
1464 * @param peer the identity of the peer
1465 * @param create whether or not to create an agent, if none is allocated yet
1468 static struct RIL_Peer_Agent *
1469 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1471 struct RIL_Peer_Agent *cur;
1473 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1475 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1483 return agent_init (solver, peer);
1489 * Lookup network struct by type
1491 * @param s the solver handle
1492 * @param type the network type
1493 * @return the network struct
1495 static struct RIL_Network *
1496 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1500 for (i = 0; i < s->networks_count; i++)
1502 if (s->network_entries[i].type == type)
1504 return &s->network_entries[i];
1511 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1512 * considered inactive and not used. Addresses in an inactive network are ignored.
1514 * @param solver solver handle
1515 * @param network the network type
1516 * @return whether or not the network is considered active
1519 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1521 struct RIL_Network *net;
1522 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1524 net = ril_get_network (solver, network);
1525 if (net->bw_out_available < min_bw)
1531 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1532 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1533 * frees the memory of the old vector, and redirects the pointer to the new one.
1535 * @param old pointer to the pointer to the first element of the vector
1536 * @param element_size byte size of the vector elements
1537 * @param hole_start the first element to cut out
1538 * @param hole_length the number of elements to cut out
1539 * @param old_length the length of the old vector
1542 ril_cut_from_vector (void **old,
1543 size_t element_size,
1544 unsigned int hole_start,
1545 unsigned int hole_length,
1546 unsigned int old_length)
1549 char *oldptr = (char *) *old;
1551 unsigned int bytes_before;
1552 unsigned int bytes_hole;
1553 unsigned int bytes_after;
1555 GNUNET_assert(old_length > hole_length);
1556 GNUNET_assert(old_length >= (hole_start + hole_length));
1558 size = element_size * (old_length - hole_length);
1560 bytes_before = element_size * hole_start;
1561 bytes_hole = element_size * hole_length;
1562 bytes_after = element_size * (old_length - hole_start - hole_length);
1570 tmpptr = GNUNET_malloc (size);
1571 memcpy (tmpptr, oldptr, bytes_before);
1572 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1578 *old = (void *) tmpptr;
1582 * Solver API functions
1583 * ---------------------------
1587 * Change relative preference for quality in solver
1589 * @param solver the solver handle
1590 * @param peer the peer to change the preference for
1591 * @param kind the kind to change the preference
1592 * @param pref_rel the normalized preference value for this kind over all clients
1595 GAS_ril_address_change_preference (void *solver,
1596 const struct GNUNET_PeerIdentity *peer,
1597 enum GNUNET_ATS_PreferenceKind kind,
1600 LOG(GNUNET_ERROR_TYPE_DEBUG,
1601 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1602 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1608 * Entry point for the plugin
1610 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1613 libgnunet_plugin_ats_ril_init (void *cls)
1615 struct GNUNET_ATS_PluginEnvironment *env = cls;
1616 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1617 struct RIL_Network * cur;
1621 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1623 GNUNET_assert(NULL != env);
1624 GNUNET_assert(NULL != env->cfg);
1625 GNUNET_assert(NULL != env->stats);
1626 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1627 GNUNET_assert(NULL != env->get_preferences);
1628 GNUNET_assert(NULL != env->get_property);
1631 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
1632 &solver->parameters.step_time_min))
1634 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
1637 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
1638 &solver->parameters.step_time_max))
1640 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
1642 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
1644 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
1645 GNUNET_free (string);
1649 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1651 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
1653 solver->parameters.beta = strtod (string, NULL);
1654 GNUNET_free (string);
1658 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1661 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
1663 solver->parameters.alpha = strtod (string, NULL);
1664 GNUNET_free (string);
1668 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1670 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
1672 solver->parameters.lambda = strtod (string, NULL);
1673 GNUNET_free (string);
1677 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1679 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
1681 solver->parameters.explore_ratio = strtod (string, NULL);
1682 GNUNET_free (string);
1686 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
1688 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
1690 solver->parameters.reward_global_share = strtod (string, NULL);
1691 GNUNET_free (string);
1695 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
1698 env->sf.s_add = &GAS_ril_address_add;
1699 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1700 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1701 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
1702 env->sf.s_address_update_network = &GAS_ril_address_change_network;
1703 env->sf.s_get = &GAS_ril_get_preferred_address;
1704 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
1705 env->sf.s_pref = &GAS_ril_address_change_preference;
1706 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
1707 env->sf.s_del = &GAS_ril_address_delete;
1708 env->sf.s_bulk_start = &GAS_ril_bulk_start;
1709 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
1711 solver->plugin_envi = env;
1712 solver->networks_count = env->network_count;
1713 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
1714 solver->step_count = 0;
1715 solver->state_networks = GNUNET_malloc (solver->networks_count * RIL_FEATURES_NETWORK_COUNT * sizeof (double));
1717 for (c = 0; c < env->network_count; c++)
1719 cur = &solver->network_entries[c];
1720 cur->type = env->networks[c];
1721 cur->bw_in_available = env->in_quota[c];
1722 cur->bw_out_available = env->out_quota[c];
1725 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (
1726 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1727 &ril_step_scheduler_task, solver);
1728 solver->task_pending = GNUNET_YES;
1734 * Exit point for the plugin
1736 * @param cls the solver handle
1739 libgnunet_plugin_ats_ril_done (void *cls)
1741 struct GAS_RIL_Handle *s = cls;
1742 struct RIL_Peer_Agent *cur_agent;
1743 struct RIL_Peer_Agent *next_agent;
1745 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1747 cur_agent = s->agents_head;
1748 while (NULL != cur_agent)
1750 next_agent = cur_agent->next;
1751 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1752 agent_die (s, cur_agent);
1753 cur_agent = next_agent;
1756 if (s->task_pending)
1758 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
1760 GNUNET_free(s->network_entries);
1761 GNUNET_free(s->state_networks);
1768 * Add a new address for a peer to the solver
1770 * The address is already contained in the addresses hashmap!
1772 * @param solver the solver Handle
1773 * @param address the address to add
1774 * @param network network type of this address
1777 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1779 struct GAS_RIL_Handle *s = solver;
1780 struct RIL_Peer_Agent *agent;
1781 struct RIL_Address_Wrapped *address_wrapped;
1782 struct RIL_Network *net;
1789 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1791 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
1793 net = ril_get_network (s, network);
1794 address->solver_information = net;
1796 if (!ril_network_is_active (s, network))
1798 LOG(GNUNET_ERROR_TYPE_DEBUG,
1799 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
1800 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1804 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1807 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1808 address_wrapped->address_naked = address;
1809 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1811 //increase size of W
1812 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
1814 n_new = agent->n + 1;
1817 GNUNET_array_grow(agent->W, agent->n, n_new);
1818 for (i = 0; i < n_new; i++)
1823 GNUNET_array_grow(agent->W[i], agent->m, m_new);
1828 GNUNET_array_grow(agent->W[i], zero, m_new);
1832 //increase size of old state vector
1834 GNUNET_array_grow(agent->s_old, agent->m, m_new);
1837 GNUNET_array_grow(agent->e, agent->m, m_new);
1839 if (NULL == agent->address_inuse)
1841 envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
1846 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
1847 address->active ? "active" : "inactive", address->plugin, address->addr,
1848 GNUNET_i2s (&address->peer));
1852 * Delete an address in the solver
1854 * The address is not contained in the address hashmap anymore!
1856 * @param solver the solver handle
1857 * @param address the address to remove
1858 * @param session_only delete only session not whole address
1861 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1863 struct GAS_RIL_Handle *s = solver;
1864 struct RIL_Peer_Agent *agent;
1865 struct RIL_Address_Wrapped *address_wrapped;
1866 int address_was_used = address->active;
1871 struct RIL_Network *net;
1872 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1874 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
1875 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
1876 address->addr, GNUNET_i2s (&address->peer));
1878 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1881 net = address->solver_information;
1882 GNUNET_assert(!ril_network_is_active (s, net->type));
1883 LOG(GNUNET_ERROR_TYPE_DEBUG,
1884 "No agent allocated for peer yet, since address was in inactive network\n");
1888 address_index = agent_address_get_index (agent, address);
1889 address_wrapped = agent_address_get (agent, address);
1891 if (NULL == address_wrapped)
1893 net = address->solver_information;
1894 GNUNET_assert(!ril_network_is_active (s, net->type));
1895 LOG(GNUNET_ERROR_TYPE_DEBUG,
1896 "Address not considered by agent, address was in inactive network\n");
1900 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1901 GNUNET_free(address_wrapped);
1904 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
1905 n_new = agent->n - 1;
1907 for (i = 0; i < agent->n; i++)
1909 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
1910 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1911 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1913 GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
1914 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
1916 //correct last action
1917 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1921 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1923 agent->a_old = RIL_ACTION_INVALID;
1925 //decrease old state vector and eligibility vector
1926 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
1927 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1928 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1929 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
1930 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1931 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1935 LOG(GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
1937 if (address_was_used)
1939 if (NULL != agent->addresses_head) //if peer has an address left, use it
1941 LOG(GNUNET_ERROR_TYPE_DEBUG, "address left: %s\n",
1942 agent->addresses_head->address_naked->addr);
1944 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw,
1949 LOG(GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
1951 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
1955 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted: %s\n", address->addr);
1961 * Update the properties of an address in the solver
1963 * @param solver solver handle
1964 * @param address the address
1965 * @param type the ATSI type in HBO
1966 * @param abs_value the absolute value of the property
1967 * @param rel_value the normalized value
1970 GAS_ril_address_property_changed (void *solver,
1971 struct ATS_Address *address,
1976 LOG(GNUNET_ERROR_TYPE_DEBUG,
1977 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
1978 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1979 address->addr, rel_value);
1985 * Update the session of an address in the solver
1987 * NOTE: values in addresses are already updated
1989 * @param solver solver handle
1990 * @param address the address
1991 * @param cur_session the current session
1992 * @param new_session the new session
1995 GAS_ril_address_session_changed (void *solver,
1996 struct ATS_Address *address,
1997 uint32_t cur_session,
1998 uint32_t new_session)
2001 * TODO? Future Work: Potentially add session activity as a feature in state vector
2003 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2007 * Notify the solver that an address is (not) actively used by transport
2008 * to communicate with a remote peer
2010 * NOTE: values in addresses are already updated
2012 * @param solver solver handle
2013 * @param address the address
2014 * @param in_use usage state
2017 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2020 * TODO? Future Work: Potentially add usage variable to state vector
2022 LOG(GNUNET_ERROR_TYPE_DEBUG,
2023 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2024 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2028 * Notify solver that the network an address is located in has changed
2030 * NOTE: values in addresses are already updated
2032 * @param solver solver handle
2033 * @param address the address
2034 * @param current_network the current network
2035 * @param new_network the new network
2038 GAS_ril_address_change_network (void *solver,
2039 struct ATS_Address *address,
2040 uint32_t current_network,
2041 uint32_t new_network)
2043 struct GAS_RIL_Handle *s = solver;
2044 struct RIL_Peer_Agent *agent;
2046 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2047 "%s address of peer %s from '%s' to '%s'\n",
2048 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2049 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2051 if (address->active && !ril_network_is_active (solver, new_network))
2053 GAS_ril_address_delete (solver, address, GNUNET_NO);
2057 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2060 GNUNET_assert(!ril_network_is_active (solver, current_network));
2062 GAS_ril_address_add (s, address, new_network);
2066 address->solver_information = ril_get_network(solver, new_network);
2070 * Give feedback about the current assignment
2072 * @param solver the solver handle
2073 * @param application the application
2074 * @param peer the peer to change the preference for
2075 * @param scope the time interval for this feedback: [now - scope .. now]
2076 * @param kind the kind to change the preference
2077 * @param score the score
2080 GAS_ril_address_preference_feedback (void *solver,
2082 const struct GNUNET_PeerIdentity *peer,
2083 const struct GNUNET_TIME_Relative scope,
2084 enum GNUNET_ATS_PreferenceKind kind,
2087 LOG(GNUNET_ERROR_TYPE_DEBUG,
2088 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2089 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2090 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2094 * Start a bulk operation
2096 * @param solver the solver
2099 GAS_ril_bulk_start (void *solver)
2101 struct GAS_RIL_Handle *s = solver;
2103 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2109 * Bulk operation done
2111 * @param solver the solver handle
2114 GAS_ril_bulk_stop (void *solver)
2116 struct GAS_RIL_Handle *s = solver;
2118 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2120 if (s->bulk_lock < 1)
2127 if (0 < s->bulk_changes)
2130 s->bulk_changes = 0;
2135 * Tell solver to notify ATS if the address to use changes for a specific
2136 * peer using the bandwidth changed callback
2138 * The solver must only notify about changes for peers with pending address
2141 * @param solver the solver handle
2142 * @param peer the identity of the peer
2144 const struct ATS_Address *
2145 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2148 * activate agent, return currently chosen address
2150 struct GAS_RIL_Handle *s = solver;
2151 struct RIL_Peer_Agent *agent;
2153 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2155 agent = ril_get_agent (s, peer, GNUNET_YES);
2157 agent->is_active = GNUNET_YES;
2159 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2162 if (agent->address_inuse)
2164 LOG(GNUNET_ERROR_TYPE_DEBUG,
2165 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2166 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2170 LOG(GNUNET_ERROR_TYPE_DEBUG,
2171 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2175 return agent->address_inuse;
2179 * Tell solver stop notifying ATS about changes for this peers
2181 * The solver must only notify about changes for peers with pending address
2184 * @param solver the solver handle
2185 * @param peer the peer
2188 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2190 struct GAS_RIL_Handle *s = solver;
2191 struct RIL_Peer_Agent *agent;
2193 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2195 agent = ril_get_agent (s, peer, GNUNET_NO);
2202 if (GNUNET_NO == agent->is_active)
2208 agent->is_active = GNUNET_NO;
2210 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2215 LOG(GNUNET_ERROR_TYPE_DEBUG,
2216 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2217 GNUNET_i2s (peer), agent->address_inuse->plugin);
2220 /* end of libgnunet_plugin_ats_ril.c */