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
45 * ATS reinforcement learning solver
51 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
52 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
53 * to a particular address. The action of switching to address with index i is depicted by the
54 * number (RIL_ACTION_TYPE_NUM + i).
58 RIL_ACTION_NOTHING = 0,
59 RIL_ACTION_BW_IN_DBL = 1,
60 RIL_ACTION_BW_IN_HLV = 2,
61 RIL_ACTION_BW_IN_INC = 3,
62 RIL_ACTION_BW_IN_DEC = 4,
63 RIL_ACTION_BW_OUT_DBL = 5,
64 RIL_ACTION_BW_OUT_HLV = 6,
65 RIL_ACTION_BW_OUT_INC = 7,
66 RIL_ACTION_BW_OUT_DEC = 8,
67 RIL_ACTION_TYPE_NUM = 9
76 enum RIL_E_Modification
85 * Global learning parameters
87 struct RIL_Learning_Parameters
90 * The TD-algorithm to use
92 enum RIL_Algorithm algorithm;
95 * Learning discount factor in the TD-update
100 * Gradient-descent step-size
105 * Trace-decay factor for eligibility traces
112 double explore_ratio;
115 * Minimal interval time between steps in milliseconds
117 struct GNUNET_TIME_Relative step_time_min;
120 * Maximum interval time between steps in milliseconds
122 struct GNUNET_TIME_Relative step_time_max;
126 * Wrapper for addresses to store them in agent's linked list
128 struct RIL_Address_Wrapped
133 struct RIL_Address_Wrapped *next;
138 struct RIL_Address_Wrapped *prev;
143 struct ATS_Address *address_naked;
146 struct RIL_Peer_Agent
149 * Next agent in solver's linked list
151 struct RIL_Peer_Agent *next;
154 * Previous agent in solver's linked list
156 struct RIL_Peer_Agent *prev;
161 struct GAS_RIL_Handle *envi;
166 struct GNUNET_PeerIdentity peer;
169 * Whether the agent is active or not
174 * Number of performed time-steps
176 unsigned long long step_count;
179 * Experience matrix W
184 * Number of rows of W / Number of state-vector features
189 * Number of columns of W / Number of actions
194 * Last perceived state feature vector
204 * Eligibility trace vector
211 struct ATS_Address * address_inuse;
214 * Head of addresses DLL
216 struct RIL_Address_Wrapped * addresses_head;
219 * Tail of addresses DLL
221 struct RIL_Address_Wrapped * addresses_tail;
224 * Inbound bandwidth assigned by the agent
226 unsigned long long bw_in;
229 * Outbound bandwidth assigned by the agent
231 unsigned long long bw_out;
239 enum GNUNET_ATS_Network_Type type;
242 * Total available inbound bandwidth
244 unsigned long long bw_in_available;
247 * Total assigned outbound bandwidth
249 unsigned long long bw_in_assigned;
252 * Total available outbound bandwidth
254 unsigned long long bw_out_available;
257 * Total assigned outbound bandwidth
259 unsigned long long bw_out_assigned;
263 * A handle for the reinforcement learning solver
265 struct GAS_RIL_Handle
268 * The solver-plugin environment of the solver-plugin API
270 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
275 struct GNUNET_STATISTICS_Handle *stats;
278 * Number of performed steps
280 unsigned long long step_count;
283 * Timestamp for the last time-step
285 struct GNUNET_TIME_Absolute step_time_last;
288 * Task identifier of the next time-step to be executed
290 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
293 * Whether a step is already scheduled
298 * Variable discount factor, dependent on time between steps
300 double discount_variable;
303 * Integrated variable discount factor, dependent on time between steps
305 double discount_integrated;
308 * Lock for bulk operations
313 * Number of changes during a lock
318 * Learning parameters
320 struct RIL_Learning_Parameters parameters;
323 * Array of networks with global assignment state
325 struct RIL_Network * network_entries;
330 unsigned int networks_count;
333 * List of active peer-agents
335 struct RIL_Peer_Agent * agents_head;
336 struct RIL_Peer_Agent * agents_tail;
341 * ---------------------------
345 * Estimate the current action-value for state s and action a
347 * @param agent agent performing the estimation
350 * @return estimation value
353 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
358 for (i = 0; i < agent->m; i++)
360 result += state[i] * agent->W[action][i];
367 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
368 * currently estimated best action) in the current step
370 * @param agent agent performing the step
371 * @return yes, if exploring
374 agent_decide_exploration (struct RIL_Peer_Agent *agent)
376 //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
377 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
378 UINT32_MAX) / (double) UINT32_MAX;
380 if (r < agent->envi->parameters.explore_ratio)
388 * Get the index of the address in the agent's list.
390 * @param agent agent handle
391 * @param address address handle
392 * @return the index, starting with zero
395 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
398 struct RIL_Address_Wrapped *cur;
401 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
404 if (cur->address_naked == address)
414 * Gets the wrapped address from the agent's list
416 * @param agent agent handle
417 * @param address address handle
418 * @return wrapped address
420 static struct RIL_Address_Wrapped *
421 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
423 struct RIL_Address_Wrapped *cur;
425 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
427 if (cur->address_naked == address)
437 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
438 * most reward in the future)
440 * @param agent agent performing the calculation
441 * @param state the state from which to take the action
442 * @return the action promising most future reward
445 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
448 int max_i = RIL_ACTION_INVALID;
450 double max_q = -DBL_MAX;
452 for (i = 0; i < agent->n; i++)
454 cur_q = agent_estimate_q (agent, state, i);
462 GNUNET_assert(RIL_ACTION_INVALID != max_i);
468 * Gets any action, to explore the action space from that state
470 * @param agent agent performing the calculation
471 * @param state the state from which to take the action
475 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
477 // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
478 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
482 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
484 * @param agent the agent performing the update
485 * @param reward the reward received for the last action
486 * @param s_next the new state, the last step got the agent into
487 * @param a_prime the new
490 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
494 double *theta = agent->W[agent->a_old];
496 delta = agent->envi->discount_integrated * reward; //reward
497 delta += agent->envi->discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
498 delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
499 for (i = 0; i < agent->m; i++)
501 theta[i] += agent->envi->parameters.alpha * delta * agent->e[i];
506 * Changes the eligibility trace vector e in various manners:
507 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
508 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
509 * RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
510 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
512 * @param agent the agent handle
513 * @param mod the kind of modification
516 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
519 double *e = agent->e;
521 for (i = 0; i < agent->m; i++)
525 case RIL_E_ACCUMULATE:
532 e[i] *= agent->envi->discount_variable * agent->envi->parameters.lambda;
542 ril_inform (struct GAS_RIL_Handle *solver,
543 enum GAS_Solver_Operation op,
544 enum GAS_Solver_Status stat)
546 if (NULL != solver->plugin_envi->info_cb)
547 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
551 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
552 * notify ATS of its new decision
554 * @param solver solver handle
555 * @param agent agent handle
556 * @param new_address the address which is to be used
557 * @param new_bw_in the new amount of inbound bandwidth set for this address
558 * @param new_bw_out the new amount of outbound bandwidth set for this address
559 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
562 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
563 struct RIL_Peer_Agent *agent,
564 struct ATS_Address *new_address,
565 unsigned long long new_bw_in,
566 unsigned long long new_bw_out,
569 int notify = GNUNET_NO;
571 LOG(GNUNET_ERROR_TYPE_DEBUG, "set_active_suggestion()\n");
574 if (agent->address_inuse != new_address)
576 if (NULL != agent->address_inuse)
578 agent->address_inuse->active = GNUNET_NO;
579 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
580 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
582 if (NULL != new_address)
584 LOG(GNUNET_ERROR_TYPE_DEBUG, "set address active: %s\n", agent->is_active ? "yes" : "no");
585 new_address->active = agent->is_active;
586 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
587 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
589 notify |= GNUNET_YES;
595 if (new_address->active != agent->is_active)
597 new_address->active = agent->is_active;
601 if (agent->bw_in != new_bw_in)
603 agent->bw_in = new_bw_in;
604 new_address->assigned_bw_in.value__ = htonl (new_bw_out);
605 notify |= GNUNET_YES;
607 if (agent->bw_out != new_bw_out)
609 agent->bw_out = new_bw_out;
610 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
611 notify |= GNUNET_YES;
615 if (notify && agent->is_active && (GNUNET_NO == silent))
619 solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
622 else if (agent->address_inuse)
624 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
625 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
629 solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
630 agent->address_inuse);
633 agent->address_inuse = new_address;
637 * Allocates a state vector and fills it with the features present
638 * @param solver the solver handle
639 * @param agent the agent handle
640 * @return pointer to the state vector
643 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
647 struct RIL_Network *net;
648 double *state = GNUNET_malloc (sizeof (double) * agent->m);
649 struct RIL_Address_Wrapped *cur_address;
650 const double *properties;
652 for (i = 0; i < solver->networks_count; i++)
654 net = &solver->network_entries[i];
655 state[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) net->bw_in_assigned;
656 state[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net->bw_in_available;
657 state[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) net->bw_out_assigned;
658 state[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net->bw_out_available;
661 i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
663 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
665 state[i++] = cur_address->address_naked->active;
666 state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
667 state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
668 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
669 cur_address->address_naked);
670 for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
672 state[i++] = properties[k];
680 * For all networks a peer has an address in, this gets the maximum bandwidth which could
681 * theoretically be available in one of the networks. This is used for bandwidth normalization.
683 * @param agent the agent handle
684 * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
686 static long long unsigned
687 ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
690 * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
691 * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
694 struct RIL_Address_Wrapped *cur;
695 struct RIL_Network *net;
697 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
699 net = cur->address_naked->solver_information;
702 if (net->bw_in_available > max)
704 max = net->bw_in_available;
709 if (net->bw_out_available > max)
711 max = net->bw_out_available;
719 * Get the index of the quality-property in question
721 * @param type the quality property type
725 ril_find_property_index (uint32_t type)
727 int existing_types[] = GNUNET_ATS_QualityProperties;
729 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
730 if (existing_types[c] == type)
732 return GNUNET_SYSERR;
736 ril_get_atsi (struct ATS_Address *address, uint32_t type)
739 GNUNET_assert(NULL != address);
741 if ((NULL == address->atsi) || (0 == address->atsi_count))
744 for (c1 = 0; c1 < address->atsi_count; c1++)
746 if (ntohl (address->atsi[c1].type) == type)
747 return ntohl (address->atsi[c1].value);
753 envi_reward_global (struct GAS_RIL_Handle *solver)
756 unsigned int in_available = 0;
757 unsigned int out_available = 0;
758 unsigned int in_assigned = 0;
759 unsigned int out_assigned = 0;
763 for (i = 0; i < solver->networks_count; i++)
765 in_available += solver->network_entries[i].bw_in_available;
766 in_assigned += solver->network_entries[i].bw_in_assigned;
767 out_available += solver->network_entries[i].bw_out_available;
768 out_assigned += solver->network_entries[i].bw_out_assigned;
771 ratio_in = ((double) in_assigned) / ((double) in_available);
772 ratio_out = ((double) out_assigned) / ((double) out_available);
774 // global reward in [1,2]
775 return ((ratio_in + ratio_out) / 2) + 1;
779 envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
781 const double *preferences;
782 const double *properties;
784 double pref_match = 0;
788 preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
790 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
791 agent->address_inuse);
794 prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
795 dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
797 // utilization in [0,1]
798 bw_norm = ((ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
799 / ril_get_max_bw (agent, GNUNET_YES))
800 + (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
801 / ril_get_max_bw (agent, GNUNET_NO))) / 2;
803 // preference matching in [0,4]
804 pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
805 pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
807 // local reward in [1,2]
808 return (pref_match / 4) +1;
812 * Gets the reward for the last performed step, which is calculated in equal
813 * parts from the local (the peer specific) and the global (for all peers
816 * @param solver the solver handle
817 * @param agent the agent handle
821 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
823 struct RIL_Network *net;
826 //punish overutilization
827 net = agent->address_inuse->solver_information;
828 if ((net->bw_in_assigned > net->bw_in_available)
829 || (net->bw_out_assigned > net->bw_out_available))
834 reward += envi_reward_global (solver);
835 reward += envi_reward_local (solver, agent);
840 * Doubles the bandwidth for the active address
842 * @param solver solver handle
843 * @param agent agent handle
844 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
847 envi_action_bw_double (struct GAS_RIL_Handle *solver,
848 struct RIL_Peer_Agent *agent,
853 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2,
854 agent->bw_out, GNUNET_NO);
858 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
859 agent->bw_out * 2, GNUNET_NO);
864 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
865 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
867 * @param solver solver handle
868 * @param agent agent handle
869 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
873 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
874 struct RIL_Peer_Agent *agent,
877 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
878 unsigned long long new_bw;
882 new_bw = agent->bw_in / 2;
885 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
890 new_bw = agent->bw_out / 2;
893 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
899 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
901 * @param solver solver handle
902 * @param agent agent handle
903 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
907 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
909 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
913 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
914 agent->bw_out, GNUNET_NO);
918 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
919 agent->bw_out + (5 * min_bw), GNUNET_NO);
924 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
925 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
927 * @param solver solver handle
928 * @param agent agent handle
929 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
933 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
935 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
936 unsigned long long new_bw;
940 new_bw = agent->bw_in - (5 * min_bw);
943 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
948 new_bw = agent->bw_out - (5 * min_bw);
951 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
957 * Switches to the address given by its index
959 * @param solver solver handle
960 * @param agent agent handle
961 * @param address_index index of the address as it is saved in the agent's list, starting with zero
964 envi_action_address_switch (struct GAS_RIL_Handle *solver,
965 struct RIL_Peer_Agent *agent,
966 unsigned int address_index)
968 struct RIL_Address_Wrapped *cur;
971 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
973 if (i == address_index)
975 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
983 //no address with address_index exists, in this case this action should not be callable
984 GNUNET_assert(GNUNET_NO);
988 * Puts the action into effect by calling the according function
990 * @param solver the solver handle
991 * @param agent the action handle
992 * @param action the action to perform by the solver
995 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1001 case RIL_ACTION_NOTHING:
1003 case RIL_ACTION_BW_IN_DBL:
1004 envi_action_bw_double (solver, agent, GNUNET_YES);
1006 case RIL_ACTION_BW_IN_HLV:
1007 envi_action_bw_halven (solver, agent, GNUNET_YES);
1009 case RIL_ACTION_BW_IN_INC:
1010 envi_action_bw_inc (solver, agent, GNUNET_YES);
1012 case RIL_ACTION_BW_IN_DEC:
1013 envi_action_bw_dec (solver, agent, GNUNET_YES);
1015 case RIL_ACTION_BW_OUT_DBL:
1016 envi_action_bw_double (solver, agent, GNUNET_NO);
1018 case RIL_ACTION_BW_OUT_HLV:
1019 envi_action_bw_halven (solver, agent, GNUNET_NO);
1021 case RIL_ACTION_BW_OUT_INC:
1022 envi_action_bw_inc (solver, agent, GNUNET_NO);
1024 case RIL_ACTION_BW_OUT_DEC:
1025 envi_action_bw_dec (solver, agent, GNUNET_NO);
1028 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1030 address_index = action - RIL_ACTION_TYPE_NUM;
1032 GNUNET_assert(address_index >= 0);
1034 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1036 envi_action_address_switch (solver, agent, address_index);
1039 // error - action does not exist
1040 GNUNET_assert(GNUNET_NO);
1045 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1046 * after having done the last action a_old. It observes the new state s_next and the reward
1047 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1048 * next action is put into effect.
1050 * @param agent the agent performing the step
1053 agent_step (struct RIL_Peer_Agent *agent)
1055 int a_next = RIL_ACTION_INVALID;
1059 s_next = envi_get_state (agent->envi, agent);
1060 reward = envi_get_reward (agent->envi, agent);
1062 LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
1063 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1065 switch (agent->envi->parameters.algorithm)
1067 case RIL_ALGO_SARSA:
1068 agent_modify_eligibility (agent, RIL_E_SET);
1069 if (agent_decide_exploration (agent))
1071 a_next = agent_get_action_explore (agent, s_next);
1075 a_next = agent_get_action_best (agent, s_next);
1077 if (RIL_ACTION_INVALID != agent->a_old)
1079 //updates weights with selected action (on-policy), if not first step
1080 agent_update_weights (agent, reward, s_next, a_next);
1085 a_next = agent_get_action_best (agent, s_next);
1086 if (RIL_ACTION_INVALID != agent->a_old)
1088 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1089 agent_update_weights (agent, reward, s_next, a_next);
1091 if (agent_decide_exploration (agent))
1093 a_next = agent_get_action_explore (agent, s_next);
1094 agent_modify_eligibility (agent, RIL_E_ZERO);
1098 a_next = agent_get_action_best (agent, s_next);
1099 agent_modify_eligibility (agent, RIL_E_SET);
1104 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1106 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
1108 envi_do_action (agent->envi, agent, a_next);
1110 GNUNET_free(agent->s_old);
1111 agent->s_old = s_next;
1112 agent->a_old = a_next;
1114 agent->step_count += 1;
1118 ril_step (struct GAS_RIL_Handle *solver);
1121 * Task for the scheduler, which performs one step and lets the solver know that
1122 * no further step is scheduled.
1124 * @param cls the solver handle
1125 * @param tc the task context for the scheduler
1128 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1130 struct GAS_RIL_Handle *solver = cls;
1132 solver->task_pending = GNUNET_NO;
1137 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1140 struct RIL_Network net;
1141 unsigned long long sum_assigned = 0;
1142 unsigned long long sum_available = 0;
1145 for (i = 0; i < solver->networks_count; i++)
1147 net = solver->network_entries[i];
1148 if (net.bw_in_assigned > 0) //only consider scopes with an active address
1150 sum_assigned += net.bw_in_assigned;
1151 sum_assigned += net.bw_out_assigned;
1152 sum_available += net.bw_in_available;
1153 sum_available += net.bw_out_available;
1156 if (sum_available > 0)
1158 ratio = ((double) sum_assigned) / ((double) sum_available);
1165 return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
1169 * Schedules the next global step in an adaptive way. The more resources are
1170 * left, the earlier the next step is scheduled. This serves the reactivity of
1171 * the solver to changed inputs.
1173 * @param solver the solver handle
1176 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1182 struct GNUNET_TIME_Relative time_next;
1184 if (solver->task_pending)
1186 GNUNET_SCHEDULER_cancel (solver->step_next_task_id);
1189 used_ratio = ril_get_used_resource_ratio (solver);
1192 solver->parameters.step_time_min.rel_value_us
1193 < solver->parameters.step_time_max.rel_value_us);
1195 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1196 solver->parameters.step_time_min).rel_value_us;
1197 offset = (double) solver->parameters.step_time_min.rel_value_us;
1198 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1200 //LOG (GNUNET_ERROR_TYPE_INFO, "used = %f, min = %f, max = %f\n", used_ratio, (double)solver->parameters.step_time_min.rel_value_us, (double)solver->parameters.step_time_max.rel_value_us);
1201 //LOG (GNUNET_ERROR_TYPE_INFO, "factor = %f, offset = %f, y = %f\n", factor, offset, y);
1203 GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1204 GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1206 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned int) y);
1208 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1210 solver->task_pending = GNUNET_YES;
1214 * Triggers one step per agent
1218 ril_step (struct GAS_RIL_Handle *solver)
1220 struct RIL_Peer_Agent *cur;
1221 struct GNUNET_TIME_Absolute time_now;
1222 struct GNUNET_TIME_Relative time_delta;
1225 if (GNUNET_YES == solver->bulk_lock)
1227 solver->bulk_changes++;
1231 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1233 LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
1235 if (0 == solver->step_count)
1237 solver->step_time_last = GNUNET_TIME_absolute_get ();
1240 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1241 time_now = GNUNET_TIME_absolute_get ();
1242 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1243 tau = ((double) time_delta.rel_value_us)
1244 / ((double) solver->parameters.step_time_min.rel_value_us);
1245 solver->step_time_last = time_now;
1247 //calculate reward discounts (once per step for all agents)
1248 solver->discount_variable = pow (M_E, ((-1.) * ((double) solver->parameters.beta) * tau));
1249 solver->discount_integrated = (1 - solver->discount_variable)
1250 / ((double) solver->parameters.beta);
1252 //trigger one step per active agent
1253 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1255 if (cur->is_active && cur->address_inuse)
1261 solver->step_count += 1;
1262 ril_step_schedule_next (solver);
1264 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1270 * Initialize an agent without addresses and its knowledge base
1272 * @param s ril solver
1273 * @param peer the one in question
1274 * @return handle to the new agent
1276 static struct RIL_Peer_Agent *
1277 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1280 struct GAS_RIL_Handle * solver = s;
1281 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
1283 agent->envi = solver;
1284 agent->peer = *peer;
1285 agent->step_count = 0;
1286 agent->is_active = GNUNET_NO;
1287 agent->n = RIL_ACTION_TYPE_NUM;
1288 agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
1289 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1290 for (i = 0; i < agent->n; i++)
1292 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1294 agent->a_old = RIL_ACTION_INVALID;
1295 agent->s_old = envi_get_state (solver, agent);
1296 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1297 agent_modify_eligibility (agent, RIL_E_ZERO);
1299 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
1307 * @param solver the solver handle
1308 * @param agent the agent to retire
1311 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1315 for (i = 0; i < agent->n; i++)
1317 GNUNET_free(agent->W[i]);
1319 GNUNET_free(agent->W);
1320 GNUNET_free(agent->e);
1321 GNUNET_free(agent->s_old);
1326 * Returns the agent for a peer
1328 * @param solver the solver handle
1329 * @param peer the identity of the peer
1330 * @param create whether or not to create an agent, if none is allocated yet
1333 static struct RIL_Peer_Agent *
1334 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1336 struct RIL_Peer_Agent *cur;
1338 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1340 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1348 return agent_init (solver, peer);
1354 * Lookup network struct by type
1356 * @param s the solver handle
1357 * @param type the network type
1358 * @return the network struct
1360 static struct RIL_Network *
1361 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1365 for (i = 0; i < s->networks_count; i++)
1367 if (s->network_entries[i].type == type)
1369 return &s->network_entries[i];
1376 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1377 * considered inactive and not used. Addresses in an inactive network are ignored.
1379 * @param solver solver handle
1380 * @param network the network type
1381 * @return whether or not the network is considered active
1384 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1386 struct RIL_Network *net;
1387 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1389 net = ril_get_network (solver, network);
1390 if (net->bw_out_available < min_bw)
1396 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1397 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1398 * frees the memory of the old vector, and redirects the pointer to the new one.
1400 * @param old pointer to the pointer to the first element of the vector
1401 * @param element_size byte size of the vector elements
1402 * @param hole_start the first element to cut out
1403 * @param hole_length the number of elements to cut out
1404 * @param old_length the length of the old vector
1407 ril_cut_from_vector (void **old,
1408 size_t element_size,
1409 unsigned int hole_start,
1410 unsigned int hole_length,
1411 unsigned int old_length)
1414 char *oldptr = (char *) *old;
1416 unsigned int bytes_before;
1417 unsigned int bytes_hole;
1418 unsigned int bytes_after;
1420 GNUNET_assert(old_length > hole_length);
1421 GNUNET_assert(old_length >= (hole_start + hole_length));
1423 size = element_size * (old_length - hole_length);
1425 bytes_before = element_size * hole_start;
1426 bytes_hole = element_size * hole_length;
1427 bytes_after = element_size * (old_length - hole_start - hole_length);
1435 tmpptr = GNUNET_malloc (size);
1436 memcpy (tmpptr, oldptr, bytes_before);
1437 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1443 *old = (void *) tmpptr;
1447 * Solver API functions
1448 * ---------------------------
1452 * Change relative preference for quality in solver
1454 * @param solver the solver handle
1455 * @param peer the peer to change the preference for
1456 * @param kind the kind to change the preference
1457 * @param pref_rel the normalized preference value for this kind over all clients
1460 GAS_ril_address_change_preference (void *solver,
1461 const struct GNUNET_PeerIdentity *peer,
1462 enum GNUNET_ATS_PreferenceKind kind,
1465 LOG(GNUNET_ERROR_TYPE_DEBUG,
1466 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1467 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1473 * Entry point for the plugin
1475 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1478 libgnunet_plugin_ats_ril_init (void *cls)
1480 struct GNUNET_ATS_PluginEnvironment *env = cls;
1481 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1482 struct RIL_Network * cur;
1486 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1488 GNUNET_assert(NULL != env);
1489 GNUNET_assert(NULL != env->cfg);
1490 GNUNET_assert(NULL != env->stats);
1491 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1492 GNUNET_assert(NULL != env->get_preferences);
1493 GNUNET_assert(NULL != env->get_property);
1496 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
1497 &solver->parameters.step_time_min))
1499 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
1502 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
1503 &solver->parameters.step_time_max))
1505 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
1507 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
1509 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
1510 GNUNET_free (string);
1514 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1516 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
1518 solver->parameters.beta = strtod (string, NULL);
1519 GNUNET_free (string);
1523 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1526 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
1528 solver->parameters.alpha = strtod (string, NULL);
1529 GNUNET_free (string);
1533 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1535 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
1537 solver->parameters.lambda = strtod (string, NULL);
1538 GNUNET_free (string);
1542 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1544 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
1546 solver->parameters.explore_ratio = strtod (string, NULL);
1547 GNUNET_free (string);
1551 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
1554 env->sf.s_add = &GAS_ril_address_add;
1555 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1556 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1557 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
1558 env->sf.s_address_update_network = &GAS_ril_address_change_network;
1559 env->sf.s_get = &GAS_ril_get_preferred_address;
1560 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
1561 env->sf.s_pref = &GAS_ril_address_change_preference;
1562 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
1563 env->sf.s_del = &GAS_ril_address_delete;
1564 env->sf.s_bulk_start = &GAS_ril_bulk_start;
1565 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
1567 solver->plugin_envi = env;
1568 solver->networks_count = env->network_count;
1569 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
1570 solver->step_count = 0;
1572 for (c = 0; c < env->network_count; c++)
1574 cur = &solver->network_entries[c];
1575 cur->type = env->networks[c];
1576 cur->bw_in_available = env->in_quota[c];
1577 cur->bw_in_assigned = 0;
1578 cur->bw_out_available = env->out_quota[c];
1579 cur->bw_out_assigned = 0;
1582 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (
1583 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1584 &ril_step_scheduler_task, solver);
1585 solver->task_pending = GNUNET_YES;
1591 * Exit point for the plugin
1593 * @param cls the solver handle
1596 libgnunet_plugin_ats_ril_done (void *cls)
1598 struct GAS_RIL_Handle *s = cls;
1599 struct RIL_Peer_Agent *cur_agent;
1600 struct RIL_Peer_Agent *next_agent;
1602 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1604 cur_agent = s->agents_head;
1605 while (NULL != cur_agent)
1607 next_agent = cur_agent->next;
1608 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1609 agent_die (s, cur_agent);
1610 cur_agent = next_agent;
1613 if (s->task_pending)
1615 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
1617 GNUNET_free(s->network_entries);
1624 * Add a new address for a peer to the solver
1626 * The address is already contained in the addresses hashmap!
1628 * @param solver the solver Handle
1629 * @param address the address to add
1630 * @param network network type of this address
1633 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1635 struct GAS_RIL_Handle *s = solver;
1636 struct RIL_Peer_Agent *agent;
1637 struct RIL_Address_Wrapped *address_wrapped;
1638 struct RIL_Network *net;
1645 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1647 net = ril_get_network (s, network);
1648 address->solver_information = net;
1650 if (!ril_network_is_active (s, network))
1652 LOG(GNUNET_ERROR_TYPE_DEBUG,
1653 "API_address_add() Did not add %s address %p for peer '%s', network does not have enough bandwidth\n",
1654 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1658 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1661 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1662 address_wrapped->address_naked = address;
1663 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1665 //increase size of W
1666 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
1668 n_new = agent->n + 1;
1671 GNUNET_array_grow(agent->W, agent->n, n_new);
1672 for (i = 0; i < n_new; i++)
1677 GNUNET_array_grow(agent->W[i], agent->m, m_new);
1682 GNUNET_array_grow(agent->W[i], zero, m_new);
1686 //increase size of old state vector
1688 GNUNET_array_grow(agent->s_old, agent->m, m_new);
1691 GNUNET_array_grow(agent->e, agent->m, m_new);
1693 if (NULL == agent->address_inuse)
1695 net->bw_in_assigned += min_bw;
1696 net->bw_out_assigned += min_bw;
1697 envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
1702 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %p for peer '%s'\n",
1703 address->active ? "active" : "inactive", address->plugin, address->addr,
1704 GNUNET_i2s (&address->peer));
1708 * Delete an address in the solver
1710 * The address is not contained in the address hashmap anymore!
1712 * @param solver the solver handle
1713 * @param address the address to remove
1714 * @param session_only delete only session not whole address
1717 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1719 struct GAS_RIL_Handle *s = solver;
1720 struct RIL_Peer_Agent *agent;
1721 struct RIL_Address_Wrapped *address_wrapped;
1722 int address_was_used = address->active;
1727 struct RIL_Network *net;
1728 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1730 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
1731 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
1732 address->addr, GNUNET_i2s (&address->peer));
1734 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1737 net = address->solver_information;
1738 GNUNET_assert(!ril_network_is_active (s, net->type));
1739 LOG(GNUNET_ERROR_TYPE_DEBUG,
1740 "No agent allocated for peer yet, since address was in inactive network\n");
1744 address_index = agent_address_get_index (agent, address);
1745 address_wrapped = agent_address_get (agent, address);
1747 if (NULL == address_wrapped)
1749 net = address->solver_information;
1750 GNUNET_assert(!ril_network_is_active (s, net->type));
1751 LOG(GNUNET_ERROR_TYPE_DEBUG,
1752 "Address not considered by agent, address was in inactive network\n");
1756 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1757 GNUNET_free(address_wrapped);
1760 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
1761 n_new = agent->n - 1;
1763 for (i = 0; i < agent->n; i++)
1765 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
1766 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1767 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1769 GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
1770 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
1772 //correct last action
1773 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1777 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1779 agent->a_old = RIL_ACTION_INVALID;
1781 //decrease old state vector and eligibility vector
1782 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
1783 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1784 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1785 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
1786 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1787 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1791 LOG(GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
1793 if (address_was_used)
1795 net = address->solver_information;
1796 net->bw_in_assigned -= agent->bw_in;
1797 net->bw_out_assigned -= agent->bw_out;
1799 if (NULL != agent->addresses_head) //if peer has an address left, use it
1801 LOG(GNUNET_ERROR_TYPE_DEBUG, "address left: %p\n",
1802 agent->addresses_head->address_naked->addr);
1803 //TODO? check if network/bandwidth update can be done more clever/elegant at different function
1804 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw,
1806 net = agent->addresses_head->address_naked->solver_information;
1807 net->bw_in_assigned -= min_bw;
1808 net->bw_out_assigned -= min_bw;
1812 LOG(GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
1814 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
1820 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
1824 * Update the properties of an address in the solver
1826 * @param solver solver handle
1827 * @param address the address
1828 * @param type the ATSI type in HBO
1829 * @param abs_value the absolute value of the property
1830 * @param rel_value the normalized value
1833 GAS_ril_address_property_changed (void *solver,
1834 struct ATS_Address *address,
1839 LOG(GNUNET_ERROR_TYPE_DEBUG,
1840 "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1841 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1842 address->addr, rel_value);
1848 * Update the session of an address in the solver
1850 * NOTE: values in addresses are already updated
1852 * @param solver solver handle
1853 * @param address the address
1854 * @param cur_session the current session
1855 * @param new_session the new session
1858 GAS_ril_address_session_changed (void *solver,
1859 struct ATS_Address *address,
1860 uint32_t cur_session,
1861 uint32_t new_session)
1864 * TODO? Future Work: Potentially add session activity as a feature in state vector
1866 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1870 * Notify the solver that an address is (not) actively used by transport
1871 * to communicate with a remote peer
1873 * NOTE: values in addresses are already updated
1875 * @param solver solver handle
1876 * @param address the address
1877 * @param in_use usage state
1880 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1882 /* Nothing to do here.
1883 * Possible TODO? Future Work: Potentially add usage variable to state vector
1885 LOG(GNUNET_ERROR_TYPE_DEBUG,
1886 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1887 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1891 * Notify solver that the network an address is located in has changed
1893 * NOTE: values in addresses are already updated
1895 * @param solver solver handle
1896 * @param address the address
1897 * @param current_network the current network
1898 * @param new_network the new network
1901 GAS_ril_address_change_network (void *solver,
1902 struct ATS_Address *address,
1903 uint32_t current_network,
1904 uint32_t new_network)
1906 struct GAS_RIL_Handle *s = solver;
1907 struct RIL_Peer_Agent *agent;
1908 struct RIL_Network *net;
1909 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1911 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
1912 "%s address of peer %s from '%s' to '%s'\n",
1913 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
1914 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
1916 if (address->active && !ril_network_is_active (solver, new_network))
1918 GAS_ril_address_delete (solver, address, GNUNET_NO);
1922 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1925 GNUNET_assert(!ril_network_is_active (solver, current_network));
1927 GAS_ril_address_add (s, address, new_network);
1931 net = ril_get_network (s, current_network);
1932 net->bw_in_assigned -= agent->bw_in;
1933 net->bw_out_assigned -= agent->bw_out;
1935 net = ril_get_network (s, new_network);
1936 net->bw_in_assigned -= min_bw;
1937 net->bw_out_assigned -= min_bw;
1938 address->solver_information = net;
1942 * Give feedback about the current assignment
1944 * @param solver the solver handle
1945 * @param application the application
1946 * @param peer the peer to change the preference for
1947 * @param scope the time interval for this feedback: [now - scope .. now]
1948 * @param kind the kind to change the preference
1949 * @param score the score
1952 GAS_ril_address_preference_feedback (void *solver,
1954 const struct GNUNET_PeerIdentity *peer,
1955 const struct GNUNET_TIME_Relative scope,
1956 enum GNUNET_ATS_PreferenceKind kind,
1959 LOG(GNUNET_ERROR_TYPE_DEBUG,
1960 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1961 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1962 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1966 * Start a bulk operation
1968 * @param solver the solver
1971 GAS_ril_bulk_start (void *solver)
1973 struct GAS_RIL_Handle *s = solver;
1975 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() Locking solver for bulk operation ...\n");
1981 * Bulk operation done
1983 * @param solver the solver handle
1986 GAS_ril_bulk_stop (void *solver)
1988 struct GAS_RIL_Handle *s = solver;
1990 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() Releasing solver from bulk operation ...\n");
1992 if (s->bulk_lock < 1)
1999 if (0 < s->bulk_changes)
2002 s->bulk_changes = 0;
2007 * Tell solver to notify ATS if the address to use changes for a specific
2008 * peer using the bandwidth changed callback
2010 * The solver must only notify about changes for peers with pending address
2013 * @param solver the solver handle
2014 * @param peer the identity of the peer
2016 const struct ATS_Address *
2017 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2020 * activate agent, return currently chosen address
2022 struct GAS_RIL_Handle *s = solver;
2023 struct RIL_Peer_Agent *agent;
2024 struct RIL_Network *net;
2026 agent = ril_get_agent (s, peer, GNUNET_YES);
2028 agent->is_active = GNUNET_YES;
2030 if (agent->address_inuse)
2032 net = agent->address_inuse->solver_information;
2033 net->bw_in_assigned += agent->bw_in;
2034 net->bw_out_assigned += agent->bw_out;
2036 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2039 if (agent->address_inuse)
2041 LOG(GNUNET_ERROR_TYPE_DEBUG,
2042 "API_get_preferred_address() Activated agent for peer '%s' with %s address %p\n",
2043 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2047 LOG(GNUNET_ERROR_TYPE_DEBUG,
2048 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2054 return agent->address_inuse;
2058 * Tell solver stop notifying ATS about changes for this peers
2060 * The solver must only notify about changes for peers with pending address
2063 * @param solver the solver handle
2064 * @param peer the peer
2067 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2069 struct GAS_RIL_Handle *s = solver;
2070 struct RIL_Peer_Agent *agent;
2071 struct RIL_Network *net;
2073 agent = ril_get_agent (s, peer, GNUNET_NO);
2080 if (GNUNET_NO == agent->is_active)
2086 agent->is_active = GNUNET_NO;
2087 if (agent->address_inuse)
2089 net = agent->address_inuse->solver_information;
2090 net->bw_in_assigned -= agent->bw_in;
2091 net->bw_out_assigned -= agent->bw_out;
2093 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2098 LOG(GNUNET_ERROR_TYPE_DEBUG,
2099 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2100 GNUNET_i2s (peer), agent->address_inuse->plugin);
2103 /* end of libgnunet_plugin_ats_ril.c */