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
35 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
36 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 10000)
37 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
38 #define RIL_DEFAULT_DISCOUNT_BETA 0.7
39 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
40 #define RIL_DEFAULT_TRACE_DECAY 0.6
41 #define RIL_EXPLORE_RATIO 0.1
44 * ATS reinforcement learning solver
50 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
51 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
52 * to a particular address. The action of switching to address with index i is depicted by the
53 * number (RIL_ACTION_TYPE_NUM + i).
57 RIL_ACTION_NOTHING = 0,
58 RIL_ACTION_BW_IN_DBL = 1,
59 RIL_ACTION_BW_IN_HLV = 2,
60 RIL_ACTION_BW_IN_INC = 3,
61 RIL_ACTION_BW_IN_DEC = 4,
62 RIL_ACTION_BW_OUT_DBL = 5,
63 RIL_ACTION_BW_OUT_HLV = 6,
64 RIL_ACTION_BW_OUT_INC = 7,
65 RIL_ACTION_BW_OUT_DEC = 8,
66 RIL_ACTION_TYPE_NUM = 9
75 enum RIL_E_Modification
84 * Global learning parameters
86 struct RIL_Learning_Parameters
89 * The TD-algorithm to use
91 enum RIL_Algorithm algorithm;
94 * Learning discount factor in the TD-update
99 * Gradient-descent step-size
104 * Trace-decay factor for eligibility traces
109 * Minimal interval time between steps in milliseconds
111 struct GNUNET_TIME_Relative step_time_min;
114 * Maximum interval time between steps in milliseconds
116 struct GNUNET_TIME_Relative step_time_max;
120 * Wrapper for addresses to store them in agent's linked list
122 struct RIL_Address_Wrapped
127 struct RIL_Address_Wrapped *next;
132 struct RIL_Address_Wrapped *prev;
137 struct ATS_Address *address_naked;
140 struct RIL_Peer_Agent
143 * Next agent in solver's linked list
145 struct RIL_Peer_Agent *next;
148 * Previous agent in solver's linked list
150 struct RIL_Peer_Agent *prev;
155 struct GAS_RIL_Handle *envi;
160 struct GNUNET_PeerIdentity peer;
163 * Whether the agent is active or not
168 * Number of performed time-steps
170 unsigned long long step_count;
173 * Experience matrix W
178 * Number of rows of W / Number of state-vector features
183 * Number of columns of W / Number of actions
188 * Last perceived state feature vector
198 * Eligibility trace vector
205 struct ATS_Address * address_inuse;
208 * Head of addresses DLL
210 struct RIL_Address_Wrapped * addresses_head;
213 * Tail of addresses DLL
215 struct RIL_Address_Wrapped * addresses_tail;
218 * Inbound bandwidth assigned by the agent
220 unsigned long long bw_in;
223 * Outbound bandwidth assigned by the agent
225 unsigned long long bw_out;
233 enum GNUNET_ATS_Network_Type type;
236 * Total available inbound bandwidth
238 unsigned long long bw_in_available;
241 * Total assigned outbound bandwidth
243 unsigned long long bw_in_assigned;
246 * Total available outbound bandwidth
248 unsigned long long bw_out_available;
251 * Total assigned outbound bandwidth
253 unsigned long long bw_out_assigned;
257 * A handle for the reinforcement learning solver
259 struct GAS_RIL_Handle
262 * The solver-plugin environment of the solver-plugin API
264 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
269 struct GNUNET_STATISTICS_Handle *stats;
272 * Number of performed steps
274 unsigned long long step_count;
277 * Timestamp for the last time-step
279 struct GNUNET_TIME_Absolute step_time_last;
282 * Task identifier of the next time-step to be executed
284 GNUNET_SCHEDULER_TaskIdentifier step_next_task;
287 * Variable discount factor, dependent on time between steps
289 double discount_variable;
292 * Integrated variable discount factor, dependent on time between steps
294 double discount_integrated;
297 * Lock for bulk operations
302 * Number of changes during a lock
307 * Learning parameters
309 struct RIL_Learning_Parameters parameters;
312 * Array of networks with global assignment state
314 struct RIL_Network * network_entries;
319 unsigned int networks_count;
322 * List of active peer-agents
324 struct RIL_Peer_Agent * agents_head;
325 struct RIL_Peer_Agent * agents_tail;
330 * ---------------------------
334 * Estimate the current action-value for state s and action a
336 * @param agent agent performing the estimation
339 * @return estimation value
342 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
347 for (i = 0; i < agent->m; i++)
349 result += state[i] * agent->W[action][i];
356 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
357 * currently estimated best action) in the current step
359 * @param agent agent performing the step
360 * @return yes, if exploring
363 agent_decide_exploration (struct RIL_Peer_Agent *agent)
365 //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
367 * An e-greedy replacement could be based on the accuracy of the prediction of the Q-value
369 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
370 UINT32_MAX) / (double) UINT32_MAX;
372 if (r < RIL_EXPLORE_RATIO)
380 * Get the index of the address in the agent's list.
382 * @param agent agent handle
383 * @param address address handle
384 * @return the index, starting with zero
387 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
390 struct RIL_Address_Wrapped *cur;
393 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
396 if (cur->address_naked == address)
406 * Gets the wrapped address from the agent's list
408 * @param agent agent handle
409 * @param address address handle
410 * @return wrapped address
412 static struct RIL_Address_Wrapped *
413 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
415 struct RIL_Address_Wrapped *cur;
417 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
419 if (cur->address_naked == address)
429 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
430 * most reward in the future)
432 * @param agent agent performing the calculation
433 * @param state the state from which to take the action
434 * @return the action promising most future reward
437 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
440 int max_i = RIL_ACTION_INVALID;
442 double max_q = -DBL_MAX;
444 for (i = 0; i < agent->n; i++)
446 cur_q = agent_estimate_q (agent, state, i);
454 GNUNET_assert(RIL_ACTION_INVALID != max_i);
460 * Gets any action, to explore the action space from that state
462 * @param agent agent performing the calculation
463 * @param state the state from which to take the action
467 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
469 // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
470 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
474 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
476 * @param agent the agent performing the update
477 * @param reward the reward received for the last action
478 * @param s_next the new state, the last step got the agent into
479 * @param a_prime the new
482 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
486 double *theta = agent->W[agent->a_old];
488 delta = agent->envi->discount_integrated * reward; //reward
489 delta += agent->envi->discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
490 delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
491 for (i = 0; i < agent->m; i++)
493 theta[i] += agent->envi->parameters.alpha * delta * agent->e[i];
498 * Changes the eligibility trace vector e in various manners:
499 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
500 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
501 * RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
502 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
504 * @param agent the agent handle
505 * @param mod the kind of modification
508 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
511 double *e = agent->e;
513 for (i = 0; i < agent->m; i++)
517 case RIL_E_ACCUMULATE:
524 e[i] = agent->envi->discount_variable * agent->envi->parameters.lambda;
533 static void ril_inform (struct GAS_RIL_Handle *solver,
534 enum GAS_Solver_Operation op, enum GAS_Solver_Status stat)
536 if (NULL != solver->plugin_envi->info_cb)
537 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
541 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
542 * notify ATS of its new decision
544 * @param solver solver handle
545 * @param agent agent handle
546 * @param new_address the address which is to be used
547 * @param new_bw_in the new amount of inbound bandwidth set for this address
548 * @param new_bw_out the new amount of outbound bandwidth set for this address
549 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
552 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
553 struct RIL_Peer_Agent *agent,
554 struct ATS_Address *new_address,
555 unsigned long long new_bw_in,
556 unsigned long long new_bw_out,
559 int notify = GNUNET_NO;
561 LOG(GNUNET_ERROR_TYPE_DEBUG, "set_active_suggestion()\n");
564 if (agent->address_inuse != new_address)
566 if (NULL != agent->address_inuse)
568 agent->address_inuse->active = GNUNET_NO;
569 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
570 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
572 if (NULL != new_address)
574 LOG(GNUNET_ERROR_TYPE_DEBUG, "set address active: %s\n", agent->is_active ? "yes" : "no");
575 new_address->active = agent->is_active;
576 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
577 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
579 notify |= GNUNET_YES;
585 if (new_address->active != agent->is_active)
587 new_address->active = agent->is_active;
591 if (agent->bw_in != new_bw_in)
593 agent->bw_in = new_bw_in;
594 new_address->assigned_bw_in.value__ = htonl (new_bw_out);
595 notify |= GNUNET_YES;
597 if (agent->bw_out != new_bw_out)
599 agent->bw_out = new_bw_out;
600 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
601 notify |= GNUNET_YES;
605 if (notify && agent->is_active && (GNUNET_NO == silent))
609 solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
612 else if (agent->address_inuse)
614 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
615 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
619 solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
620 agent->address_inuse);
623 agent->address_inuse = new_address;
627 * Allocates a state vector and fills it with the features present
628 * @param solver the solver handle
629 * @param agent the agent handle
630 * @return pointer to the state vector
633 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
637 struct RIL_Network *net;
638 double *state = GNUNET_malloc (sizeof (double) * agent->m);
639 struct RIL_Address_Wrapped *cur_address;
640 const double *properties;
642 for (i = 0; i < solver->networks_count; i++)
644 net = &solver->network_entries[i];
645 state[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) net->bw_in_assigned;
646 state[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net->bw_in_available;
647 state[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) net->bw_out_assigned;
648 state[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net->bw_out_available;
651 i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
653 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
655 state[i++] = cur_address->address_naked->active;
656 state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
657 state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
658 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
659 cur_address->address_naked);
660 for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
662 state[i++] = properties[k];
670 * For all networks a peer has an address in, this gets the maximum bandwidth which could
671 * theoretically be available in one of the networks. This is used for bandwidth normalization.
673 * @param agent the agent handle
674 * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
676 static long long unsigned
677 ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
680 * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
681 * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
684 struct RIL_Address_Wrapped *cur;
685 struct RIL_Network *net;
687 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
689 net = cur->address_naked->solver_information;
692 if (net->bw_in_available > max)
694 max = net->bw_in_available;
699 if (net->bw_out_available > max)
701 max = net->bw_out_available;
709 * Get the index of the quality-property in question
711 * @param type the quality property type
715 ril_find_property_index (uint32_t type)
717 int existing_types[] = GNUNET_ATS_QualityProperties;
719 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
720 if (existing_types[c] == type)
722 return GNUNET_SYSERR;
726 envi_reward_global (struct GAS_RIL_Handle *solver)
729 unsigned int in_available = 0;
730 unsigned int out_available = 0;
731 unsigned int in_assigned = 0;
732 unsigned int out_assigned = 0;
736 for (i = 0; i < solver->networks_count; i++)
738 in_available += solver->network_entries[i].bw_in_available;
739 in_assigned += solver->network_entries[i].bw_in_assigned;
740 out_available += solver->network_entries[i].bw_out_available;
741 out_assigned += solver->network_entries[i].bw_out_assigned;
744 ratio_in = ((double) in_assigned) / ((double) in_available);
745 ratio_out = ((double) out_assigned) / ((double) out_available);
747 return ((ratio_in + ratio_out) * 0.5) + 1;
751 envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
753 //TODO! add utilization
755 const double *preferences;
756 const double *properties;
758 double pref_match = 0;
761 preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
763 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
764 agent->address_inuse);
766 //preference matching from latency and bandwidth
767 prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
768 pref_match += 1 - (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * (3 - properties[prop_index])); //invert property as we want to maximize for lower latencies
769 bw_norm = GNUNET_MAX(2, (((
770 ((double) agent->bw_in / (double) ril_get_max_bw(agent, GNUNET_YES)) +
771 ((double) agent->bw_out / (double) ril_get_max_bw(agent, GNUNET_NO))
775 pref_match += 1 - (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
777 return pref_match * 0.5;
781 * Gets the reward for the last performed step, which is calculated in equal
782 * parts from the local (the peer specific) and the global (for all peers
785 * @param solver the solver handle
786 * @param agent the agent handle
790 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
792 struct RIL_Network *net;
795 //punish overutilization
796 net = agent->address_inuse->solver_information;
797 if ((net->bw_in_assigned > net->bw_in_available) || (net->bw_out_assigned > net->bw_out_available))
802 reward += envi_reward_global (solver);
803 reward += envi_reward_local (solver, agent);
808 * Doubles the bandwidth for the active address
810 * @param solver solver handle
811 * @param agent agent handle
812 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
815 envi_action_bw_double (struct GAS_RIL_Handle *solver,
816 struct RIL_Peer_Agent *agent,
821 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2,
822 agent->bw_out, GNUNET_NO);
826 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
827 agent->bw_out * 2, GNUNET_NO);
832 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
833 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
835 * @param solver solver handle
836 * @param agent agent handle
837 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
841 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
842 struct RIL_Peer_Agent *agent,
845 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
846 unsigned long long new_bw;
850 new_bw = agent->bw_in / 2;
853 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
858 new_bw = agent->bw_out / 2;
861 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
867 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
869 * @param solver solver handle
870 * @param agent agent handle
871 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
875 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
877 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
881 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
882 agent->bw_out, GNUNET_NO);
886 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
887 agent->bw_out + (5 * min_bw), GNUNET_NO);
892 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
893 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
895 * @param solver solver handle
896 * @param agent agent handle
897 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
901 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
903 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
904 unsigned long long new_bw;
908 new_bw = agent->bw_in - (5 * min_bw);
911 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
916 new_bw = agent->bw_out - (5 * min_bw);
919 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
925 * Switches to the address given by its index
927 * @param solver solver handle
928 * @param agent agent handle
929 * @param address_index index of the address as it is saved in the agent's list, starting with zero
932 envi_action_address_switch (struct GAS_RIL_Handle *solver,
933 struct RIL_Peer_Agent *agent,
934 unsigned int address_index)
936 struct RIL_Address_Wrapped *cur;
939 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
941 if (i == address_index)
943 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
951 //no address with address_index exists, in this case this action should not be callable
952 GNUNET_assert(GNUNET_NO);
956 * Puts the action into effect by calling the according function
958 * @param solver the solver handle
959 * @param agent the action handle
960 * @param action the action to perform by the solver
963 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
969 case RIL_ACTION_NOTHING:
971 case RIL_ACTION_BW_IN_DBL:
972 envi_action_bw_double (solver, agent, GNUNET_YES);
974 case RIL_ACTION_BW_IN_HLV:
975 envi_action_bw_halven (solver, agent, GNUNET_YES);
977 case RIL_ACTION_BW_IN_INC:
978 envi_action_bw_inc (solver, agent, GNUNET_YES);
980 case RIL_ACTION_BW_IN_DEC:
981 envi_action_bw_dec (solver, agent, GNUNET_YES);
983 case RIL_ACTION_BW_OUT_DBL:
984 envi_action_bw_double (solver, agent, GNUNET_NO);
986 case RIL_ACTION_BW_OUT_HLV:
987 envi_action_bw_halven (solver, agent, GNUNET_NO);
989 case RIL_ACTION_BW_OUT_INC:
990 envi_action_bw_inc (solver, agent, GNUNET_NO);
992 case RIL_ACTION_BW_OUT_DEC:
993 envi_action_bw_dec (solver, agent, GNUNET_NO);
996 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
998 address_index = action - RIL_ACTION_TYPE_NUM;
1000 GNUNET_assert(address_index >= 0);
1002 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1004 envi_action_address_switch (solver, agent, address_index);
1007 // error - action does not exist
1008 GNUNET_assert(GNUNET_NO);
1013 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1014 * after having done the last action a_old. It observes the new state s_next and the reward
1015 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1016 * next action is put into effect.
1018 * @param agent the agent performing the step
1021 agent_step (struct RIL_Peer_Agent *agent)
1023 int a_next = RIL_ACTION_INVALID;
1027 s_next = envi_get_state (agent->envi, agent);
1028 reward = envi_get_reward (agent->envi, agent);
1030 LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
1031 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1033 switch (agent->envi->parameters.algorithm)
1035 case RIL_ALGO_SARSA:
1036 agent_modify_eligibility (agent, RIL_E_SET);
1037 if (agent_decide_exploration (agent))
1039 a_next = agent_get_action_explore (agent, s_next);
1043 a_next = agent_get_action_best (agent, s_next);
1045 if (RIL_ACTION_INVALID != agent->a_old)
1047 //updates weights with selected action (on-policy), if not first step
1048 agent_update_weights (agent, reward, s_next, a_next);
1053 a_next = agent_get_action_best (agent, s_next);
1054 if (RIL_ACTION_INVALID != agent->a_old)
1056 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1057 agent_update_weights (agent, reward, s_next, a_next);
1059 if (agent_decide_exploration (agent))
1061 a_next = agent_get_action_explore (agent, s_next);
1062 agent_modify_eligibility (agent, RIL_E_ZERO);
1066 a_next = agent_get_action_best (agent, s_next);
1067 agent_modify_eligibility (agent, RIL_E_SET);
1072 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1074 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
1076 envi_do_action (agent->envi, agent, a_next);
1078 GNUNET_free(agent->s_old);
1079 agent->s_old = s_next;
1080 agent->a_old = a_next;
1082 agent->step_count += 1;
1086 * Triggers one step per agent
1090 ril_step (struct GAS_RIL_Handle *solver)
1092 struct RIL_Peer_Agent *cur;
1093 struct GNUNET_TIME_Absolute time_now;
1094 struct GNUNET_TIME_Relative time_delta;
1097 if (GNUNET_YES == solver->bulk_lock)
1099 solver->bulk_changes ++;
1103 ril_inform(solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1105 if (0 == solver->step_count) {
1106 solver->step_time_last = GNUNET_TIME_absolute_get ();
1109 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1110 time_now = GNUNET_TIME_absolute_get ();
1111 time_delta = GNUNET_TIME_absolute_get_difference(solver->step_time_last, time_now);
1112 tau = ((double) time_delta.rel_value_us) / ((double) solver->parameters.step_time_min.rel_value_us);
1113 memcpy(&solver->step_time_last, &time_now, sizeof(struct GNUNET_TIME_Absolute));
1115 //calculate reward discounts (once per step for all agents)
1116 solver->discount_variable = pow(M_E, ((-1.) * ((double) solver->parameters.beta) * tau));
1117 solver->discount_integrated = (1 - solver->discount_variable) / ((double) solver->parameters.beta);
1119 //trigger one step per active agent
1120 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1122 if (cur->is_active && cur->address_inuse)
1127 ril_inform(solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1133 * Triggers one multi-agent step and schedules the next one.
1135 * @param cls the solver handle
1136 * @param tc the task context for the scheduler
1139 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1141 struct GAS_RIL_Handle *solver = cls;
1143 LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
1146 solver->step_count += 1;
1148 //TODO! next step scheduling depending on how many resources are left
1149 solver->step_next_task = GNUNET_SCHEDULER_add_delayed (solver->parameters.step_time_max, &ril_periodic_step, solver);
1153 * Initialize an agent without addresses and its knowledge base
1155 * @param s ril solver
1156 * @param peer the one in question
1157 * @return handle to the new agent
1159 static struct RIL_Peer_Agent *
1160 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1163 struct GAS_RIL_Handle * solver = s;
1164 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
1166 agent->envi = solver;
1167 agent->peer = *peer;
1168 agent->step_count = 0;
1169 agent->is_active = GNUNET_NO;
1170 agent->n = RIL_ACTION_TYPE_NUM;
1171 agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
1172 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1173 for (i = 0; i < agent->n; i++)
1175 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1177 agent->a_old = RIL_ACTION_INVALID;
1178 agent->s_old = envi_get_state (solver, agent);
1179 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1180 agent_modify_eligibility (agent, RIL_E_ZERO);
1182 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
1190 * @param solver the solver handle
1191 * @param agent the agent to retire
1194 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1198 for (i = 0; i < agent->n; i++)
1200 GNUNET_free(agent->W[i]);
1202 GNUNET_free(agent->W);
1203 GNUNET_free(agent->e);
1204 GNUNET_free(agent->s_old);
1209 * Returns the agent for a peer
1211 * @param solver the solver handle
1212 * @param peer the identity of the peer
1213 * @param create whether or not to create an agent, if none is allocated yet
1216 static struct RIL_Peer_Agent *
1217 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1219 struct RIL_Peer_Agent *cur;
1221 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1223 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1231 return agent_init (solver, peer);
1237 * Lookup network struct by type
1239 * @param s the solver handle
1240 * @param type the network type
1241 * @return the network struct
1243 static struct RIL_Network *
1244 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1248 for (i = 0; i < s->networks_count; i++)
1250 if (s->network_entries[i].type == type)
1252 return &s->network_entries[i];
1259 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1260 * considered inactive and not used. Addresses in an inactive network are ignored.
1262 * @param solver solver handle
1263 * @param network the network type
1264 * @return whether or not the network is considered active
1267 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1269 struct RIL_Network *net;
1270 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1272 net = ril_get_network (solver, network);
1273 if (net->bw_out_available < min_bw)
1279 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1280 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1281 * frees the memory of the old vector, and redirects the pointer to the new one.
1283 * @param old pointer to the pointer to the first element of the vector
1284 * @param element_size byte size of the vector elements
1285 * @param hole_start the first element to cut out
1286 * @param hole_length the number of elements to cut out
1287 * @param old_length the length of the old vector
1290 ril_cut_from_vector (void **old,
1291 size_t element_size,
1292 unsigned int hole_start,
1293 unsigned int hole_length,
1294 unsigned int old_length)
1297 char *oldptr = (char *) *old;
1299 unsigned int bytes_before;
1300 unsigned int bytes_hole;
1301 unsigned int bytes_after;
1303 GNUNET_assert(old_length > hole_length);
1304 GNUNET_assert(old_length >= (hole_start + hole_length));
1306 size = element_size * (old_length - hole_length);
1308 bytes_before = element_size * hole_start;
1309 bytes_hole = element_size * hole_length;
1310 bytes_after = element_size * (old_length - hole_start - hole_length);
1318 tmpptr = GNUNET_malloc (size);
1319 memcpy (tmpptr, oldptr, bytes_before);
1320 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1326 *old = (void *) tmpptr;
1330 * Solver API functions
1331 * ---------------------------
1335 * Change relative preference for quality in solver
1337 * @param solver the solver handle
1338 * @param peer the peer to change the preference for
1339 * @param kind the kind to change the preference
1340 * @param pref_rel the normalized preference value for this kind over all clients
1343 GAS_ril_address_change_preference (void *solver,
1344 const struct GNUNET_PeerIdentity *peer,
1345 enum GNUNET_ATS_PreferenceKind kind,
1348 LOG(GNUNET_ERROR_TYPE_DEBUG,
1349 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1350 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1356 * Entry point for the plugin
1358 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1361 libgnunet_plugin_ats_ril_init (void *cls)
1363 struct GNUNET_ATS_PluginEnvironment *env = cls;
1364 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1365 struct RIL_Network * cur;
1367 unsigned long long tmp;
1370 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1372 GNUNET_assert(NULL != env);
1373 GNUNET_assert(NULL != env->cfg);
1374 GNUNET_assert(NULL != env->stats);
1375 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1376 GNUNET_assert(NULL != env->get_preferences);
1377 GNUNET_assert(NULL != env->get_property);
1380 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN", &solver->parameters.step_time_min))
1382 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
1385 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX", &solver->parameters.step_time_max))
1387 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
1389 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string)
1390 && NULL != string && 0 == strcmp (string, "SARSA"))
1392 solver->parameters.algorithm = RIL_ALGO_SARSA;
1396 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1399 == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_DISCOUNT_BETA", &tmp))
1401 solver->parameters.beta = (double) tmp / 100;
1405 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1408 == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
1410 solver->parameters.alpha = (double) tmp / 100;
1414 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1416 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_TRACE_DECAY", &tmp))
1418 solver->parameters.lambda = (double) tmp / 100;
1422 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1425 env->sf.s_add = &GAS_ril_address_add;
1426 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1427 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1428 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
1429 env->sf.s_address_update_network = &GAS_ril_address_change_network;
1430 env->sf.s_get = &GAS_ril_get_preferred_address;
1431 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
1432 env->sf.s_pref = &GAS_ril_address_change_preference;
1433 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
1434 env->sf.s_del = &GAS_ril_address_delete;
1435 env->sf.s_bulk_start = &GAS_ril_bulk_start;
1436 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
1438 solver->plugin_envi = env;
1439 solver->networks_count = env->network_count;
1440 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
1441 solver->step_count = 0;
1443 for (c = 0; c < env->network_count; c++)
1445 cur = &solver->network_entries[c];
1446 cur->type = env->networks[c];
1447 cur->bw_in_available = env->in_quota[c];
1448 cur->bw_in_assigned = 0;
1449 cur->bw_out_available = env->out_quota[c];
1450 cur->bw_out_assigned = 0;
1453 solver->step_next_task = GNUNET_SCHEDULER_add_delayed (
1454 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1455 &ril_periodic_step, solver);
1461 * Exit point for the plugin
1463 * @param cls the solver handle
1466 libgnunet_plugin_ats_ril_done (void *cls)
1468 struct GAS_RIL_Handle *s = cls;
1469 struct RIL_Peer_Agent *cur_agent;
1470 struct RIL_Peer_Agent *next_agent;
1472 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1474 cur_agent = s->agents_head;
1475 while (NULL != cur_agent)
1477 next_agent = cur_agent->next;
1478 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1479 agent_die (s, cur_agent);
1480 cur_agent = next_agent;
1483 GNUNET_SCHEDULER_cancel (s->step_next_task);
1484 GNUNET_free(s->network_entries);
1491 * Add a new address for a peer to the solver
1493 * The address is already contained in the addresses hashmap!
1495 * @param solver the solver Handle
1496 * @param address the address to add
1497 * @param network network type of this address
1500 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1502 struct GAS_RIL_Handle *s = solver;
1503 struct RIL_Peer_Agent *agent;
1504 struct RIL_Address_Wrapped *address_wrapped;
1505 struct RIL_Network *net;
1512 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1514 net = ril_get_network (s, network);
1515 address->solver_information = net;
1517 if (!ril_network_is_active (s, network))
1519 LOG(GNUNET_ERROR_TYPE_DEBUG,
1520 "API_address_add() Did not add %s address %p for peer '%s', network does not have enough bandwidth\n",
1521 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1525 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1528 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1529 address_wrapped->address_naked = address;
1530 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1532 //increase size of W
1533 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
1535 n_new = agent->n + 1;
1538 GNUNET_array_grow(agent->W, agent->n, n_new);
1539 for (i = 0; i < n_new; i++)
1544 GNUNET_array_grow(agent->W[i], agent->m, m_new);
1549 GNUNET_array_grow(agent->W[i], zero, m_new);
1553 //increase size of old state vector
1555 GNUNET_array_grow(agent->s_old, agent->m, m_new);
1558 GNUNET_array_grow(agent->e, agent->m, m_new);
1560 if (NULL == agent->address_inuse)
1562 net->bw_in_assigned += min_bw;
1563 net->bw_out_assigned += min_bw;
1564 envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
1569 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %p for peer '%s'\n",
1570 address->active ? "active" : "inactive", address->plugin, address->addr,
1571 GNUNET_i2s (&address->peer));
1575 * Delete an address in the solver
1577 * The address is not contained in the address hashmap anymore!
1579 * @param solver the solver handle
1580 * @param address the address to remove
1581 * @param session_only delete only session not whole address
1584 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1586 struct GAS_RIL_Handle *s = solver;
1587 struct RIL_Peer_Agent *agent;
1588 struct RIL_Address_Wrapped *address_wrapped;
1589 int address_was_used = address->active;
1594 struct RIL_Network *net;
1595 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1597 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
1598 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
1599 address->addr, GNUNET_i2s (&address->peer));
1601 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1604 net = address->solver_information;
1605 GNUNET_assert(!ril_network_is_active (s, net->type));
1606 LOG(GNUNET_ERROR_TYPE_DEBUG,
1607 "No agent allocated for peer yet, since address was in inactive network\n");
1611 address_index = agent_address_get_index (agent, address);
1612 address_wrapped = agent_address_get (agent, address);
1614 if (NULL == address_wrapped)
1616 net = address->solver_information;
1617 GNUNET_assert(!ril_network_is_active (s, net->type));
1618 LOG(GNUNET_ERROR_TYPE_DEBUG,
1619 "Address not considered by agent, address was in inactive network\n");
1623 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1624 GNUNET_free(address_wrapped);
1627 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
1628 n_new = agent->n - 1;
1630 for (i = 0; i < agent->n; i++)
1632 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
1633 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1634 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1636 GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
1637 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
1639 //correct last action
1640 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1644 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1646 agent->a_old = RIL_ACTION_INVALID;
1648 //decrease old state vector and eligibility vector
1649 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
1650 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1651 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1652 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
1653 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1654 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1658 LOG(GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
1660 if (address_was_used)
1662 net = address->solver_information;
1663 net->bw_in_assigned -= agent->bw_in;
1664 net->bw_out_assigned -= agent->bw_out;
1666 if (NULL != agent->addresses_head) //if peer has an address left, use it
1668 LOG(GNUNET_ERROR_TYPE_DEBUG, "address left: %p\n",
1669 agent->addresses_head->address_naked->addr);
1670 //TODO? check if network/bandwidth update can be done more clever/elegant at different function
1671 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw,
1673 net = agent->addresses_head->address_naked->solver_information;
1674 net->bw_in_assigned -= min_bw;
1675 net->bw_out_assigned -= min_bw;
1679 LOG(GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
1681 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
1687 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
1691 * Update the properties of an address in the solver
1693 * @param solver solver handle
1694 * @param address the address
1695 * @param type the ATSI type in HBO
1696 * @param abs_value the absolute value of the property
1697 * @param rel_value the normalized value
1700 GAS_ril_address_property_changed (void *solver,
1701 struct ATS_Address *address,
1706 LOG(GNUNET_ERROR_TYPE_DEBUG,
1707 "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1708 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1709 address->addr, rel_value);
1715 * Update the session of an address in the solver
1717 * NOTE: values in addresses are already updated
1719 * @param solver solver handle
1720 * @param address the address
1721 * @param cur_session the current session
1722 * @param new_session the new session
1725 GAS_ril_address_session_changed (void *solver,
1726 struct ATS_Address *address,
1727 uint32_t cur_session,
1728 uint32_t new_session)
1731 * TODO? Future Work: Potentially add session activity as a feature in state vector
1733 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1737 * Notify the solver that an address is (not) actively used by transport
1738 * to communicate with a remote peer
1740 * NOTE: values in addresses are already updated
1742 * @param solver solver handle
1743 * @param address the address
1744 * @param in_use usage state
1747 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1749 /* Nothing to do here.
1750 * Possible TODO? Future Work: Potentially add usage variable to state vector
1752 LOG(GNUNET_ERROR_TYPE_DEBUG,
1753 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1754 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1758 * Notify solver that the network an address is located in has changed
1760 * NOTE: values in addresses are already updated
1762 * @param solver solver handle
1763 * @param address the address
1764 * @param current_network the current network
1765 * @param new_network the new network
1768 GAS_ril_address_change_network (void *solver,
1769 struct ATS_Address *address,
1770 uint32_t current_network,
1771 uint32_t new_network)
1773 struct GAS_RIL_Handle *s = solver;
1774 struct RIL_Peer_Agent *agent;
1775 struct RIL_Network *net;
1776 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1778 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
1779 "%s address of peer %s from '%s' to '%s'\n",
1780 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
1781 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
1783 if (address->active && !ril_network_is_active (solver, new_network))
1785 GAS_ril_address_delete (solver, address, GNUNET_NO);
1789 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1792 GNUNET_assert(!ril_network_is_active (solver, current_network));
1794 GAS_ril_address_add (s, address, new_network);
1798 net = ril_get_network (s, current_network);
1799 net->bw_in_assigned -= agent->bw_in;
1800 net->bw_out_assigned -= agent->bw_out;
1802 net = ril_get_network (s, new_network);
1803 net->bw_in_assigned -= min_bw;
1804 net->bw_out_assigned -= min_bw;
1805 address->solver_information = net;
1809 * Give feedback about the current assignment
1811 * @param solver the solver handle
1812 * @param application the application
1813 * @param peer the peer to change the preference for
1814 * @param scope the time interval for this feedback: [now - scope .. now]
1815 * @param kind the kind to change the preference
1816 * @param score the score
1819 GAS_ril_address_preference_feedback (void *solver,
1821 const struct GNUNET_PeerIdentity *peer,
1822 const struct GNUNET_TIME_Relative scope,
1823 enum GNUNET_ATS_PreferenceKind kind,
1826 LOG(GNUNET_ERROR_TYPE_DEBUG,
1827 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1828 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1829 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1833 * Start a bulk operation
1835 * @param solver the solver
1838 GAS_ril_bulk_start (void *solver)
1840 struct GAS_RIL_Handle *s = solver;
1842 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() Locking solver for bulk operation ...\n");
1848 * Bulk operation done
1850 * @param solver the solver handle
1853 GAS_ril_bulk_stop (void *solver)
1855 /* TODO! trigger "recalculation" after bulk / trigger "recalculation" after changes (address or preference)
1856 * that has to be properly thought through, not when your mind is blocked completely
1858 struct GAS_RIL_Handle *s = solver;
1860 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() Releasing solver from bulk operation ...\n");
1862 if (s->bulk_lock < 1)
1869 if (0 < s->bulk_changes)
1872 s->bulk_changes = 0;
1877 * Tell solver to notify ATS if the address to use changes for a specific
1878 * peer using the bandwidth changed callback
1880 * The solver must only notify about changes for peers with pending address
1883 * @param solver the solver handle
1884 * @param peer the identity of the peer
1886 const struct ATS_Address *
1887 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1890 * activate agent, return currently chosen address
1892 struct GAS_RIL_Handle *s = solver;
1893 struct RIL_Peer_Agent *agent;
1894 struct RIL_Network *net;
1896 agent = ril_get_agent (s, peer, GNUNET_YES);
1898 agent->is_active = GNUNET_YES;
1900 if (agent->address_inuse)
1902 net = agent->address_inuse->solver_information;
1903 net->bw_in_assigned += agent->bw_in;
1904 net->bw_out_assigned += agent->bw_out;
1906 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
1909 if (agent->address_inuse)
1911 LOG(GNUNET_ERROR_TYPE_DEBUG,
1912 "API_get_preferred_address() Activated agent for peer '%s' with %s address %p\n",
1913 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
1917 LOG(GNUNET_ERROR_TYPE_DEBUG,
1918 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
1924 return agent->address_inuse;
1928 * Tell solver stop notifying ATS about changes for this peers
1930 * The solver must only notify about changes for peers with pending address
1933 * @param solver the solver handle
1934 * @param peer the peer
1937 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1939 struct GAS_RIL_Handle *s = solver;
1940 struct RIL_Peer_Agent *agent;
1941 struct RIL_Network *net;
1943 agent = ril_get_agent (s, peer, GNUNET_NO);
1950 if (GNUNET_NO == agent->is_active)
1956 agent->is_active = GNUNET_NO;
1957 if (agent->address_inuse)
1959 net = agent->address_inuse->solver_information;
1960 net->bw_in_assigned -= agent->bw_in;
1961 net->bw_out_assigned -= agent->bw_out;
1963 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
1968 LOG(GNUNET_ERROR_TYPE_DEBUG,
1969 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1970 GNUNET_i2s (peer), agent->address_inuse->plugin);
1973 /* end of libgnunet_plugin_ats_ril.c */