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 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
36 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
37 #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
38 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
39 #define RIL_DEFAULT_TRACE_DECAY 0.6
40 #define RIL_EXPLORE_RATIO 0.1
43 * ATS reinforcement learning solver
49 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
50 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
51 * to a particular address. The action of switching to address with index i is depicted by the
52 * number (RIL_ACTION_TYPE_NUM + i).
56 RIL_ACTION_NOTHING = 0,
57 RIL_ACTION_BW_IN_DBL = 1,
58 RIL_ACTION_BW_IN_HLV = 2,
59 RIL_ACTION_BW_IN_INC = 3,
60 RIL_ACTION_BW_IN_DEC = 4,
61 RIL_ACTION_BW_OUT_DBL = 5,
62 RIL_ACTION_BW_OUT_HLV = 6,
63 RIL_ACTION_BW_OUT_INC = 7,
64 RIL_ACTION_BW_OUT_DEC = 8,
65 RIL_ACTION_TYPE_NUM = 9
74 enum RIL_E_Modification
83 * Global learning parameters
85 struct RIL_Learning_Parameters
88 * The TD-algorithm to use
90 enum RIL_Algorithm algorithm;
93 * Learning discount factor in the TD-update
98 * Gradient-descent step-size
103 * Trace-decay factor for eligibility traces
109 * Wrapper for addresses to store them in agent's linked list
111 struct RIL_Address_Wrapped
116 struct RIL_Address_Wrapped *next;
121 struct RIL_Address_Wrapped *prev;
126 struct ATS_Address *address_naked;
129 struct RIL_Peer_Agent
132 * Next agent in solver's linked list
134 struct RIL_Peer_Agent *next;
137 * Previous agent in solver's linked list
139 struct RIL_Peer_Agent *prev;
144 struct GAS_RIL_Handle *envi;
149 struct GNUNET_PeerIdentity peer;
152 * Whether the agent is active or not
157 * Number of performed time-steps
159 unsigned long long step_count;
162 * Experience matrix W
167 * Number of rows of W / Number of state-vector features
172 * Number of columns of W / Number of actions
177 * Last perceived state feature vector
187 * Eligibility trace vector
194 struct ATS_Address * address_inuse;
197 * Head of addresses DLL
199 struct RIL_Address_Wrapped * addresses_head;
202 * Tail of addresses DLL
204 struct RIL_Address_Wrapped * addresses_tail;
207 * Inbound bandwidth assigned by the agent
209 unsigned long long bw_in;
212 * Outbound bandwidth assigned by the agent
214 unsigned long long bw_out;
222 enum GNUNET_ATS_Network_Type type;
225 * Total available inbound bandwidth
227 unsigned long long bw_in_available;
230 * Total assigned outbound bandwidth
232 unsigned long long bw_in_assigned;
235 * Total available outbound bandwidth
237 unsigned long long bw_out_available;
240 * Total assigned outbound bandwidth
242 unsigned long long bw_out_assigned;
246 * A handle for the reinforcement learning solver
248 struct GAS_RIL_Handle
253 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
258 struct GNUNET_STATISTICS_Handle *stats;
261 * Number of performed time-steps
263 unsigned long long step_count;
266 * Interval time between steps in milliseconds //TODO? Future Work: Heterogeneous stepping among agents
268 struct GNUNET_TIME_Relative step_time;
271 * Task identifier of the next time-step to be executed
273 GNUNET_SCHEDULER_TaskIdentifier next_step;
276 * Learning parameters
278 struct RIL_Learning_Parameters parameters;
281 * Array of networks with global assignment state
283 struct RIL_Network * network_entries;
288 unsigned int networks_count;
291 * List of active peer-agents
293 struct RIL_Peer_Agent * agents_head;
294 struct RIL_Peer_Agent * agents_tail;
299 * ---------------------------
303 * Estimate the current action-value for state s and action a
305 * @param agent agent performing the estimation
308 * @return estimation value
311 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
316 for (i = 0; i < agent->m; i++)
318 result += state[i] * agent->W[action][i];
325 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
326 * currently estimated best action) in the current step
328 * @param agent agent performing the step
329 * @return yes, if exploring
332 agent_decide_exploration (struct RIL_Peer_Agent *agent)
334 //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
336 * An e-greedy replacement could be based on the accuracy of the prediction of the Q-value
338 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
339 UINT32_MAX) / (double) UINT32_MAX;
341 if (r < RIL_EXPLORE_RATIO)
349 * Get the index of the address in the agent's list.
351 * @param agent agent handle
352 * @param address address handle
353 * @return the index, starting with zero
356 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
359 struct RIL_Address_Wrapped *cur;
362 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
365 if (cur->address_naked == address)
375 * Gets the wrapped address from the agent's list
377 * @param agent agent handle
378 * @param address address handle
379 * @return wrapped address
381 static struct RIL_Address_Wrapped *
382 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
384 struct RIL_Address_Wrapped *cur;
386 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
388 if (cur->address_naked == address)
398 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
399 * most reward in the future)
401 * @param agent agent performing the calculation
402 * @param state the state from which to take the action
403 * @return the action promising most future reward
406 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
409 int max_i = RIL_ACTION_INVALID;
411 double max_q = -DBL_MAX;
413 for (i = 0; i < agent->n; i++)
415 cur_q = agent_estimate_q (agent, state, i);
423 GNUNET_assert(RIL_ACTION_INVALID != max_i);
429 * Gets any action, to explore the action space from that state
431 * @param agent agent performing the calculation
432 * @param state the state from which to take the action
436 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
438 // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
439 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
443 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
445 * @param agent the agent performing the update
446 * @param reward the reward received for the last action
447 * @param s_next the new state, the last step got the agent into
448 * @param a_prime the new
451 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
455 double *theta = agent->W[agent->a_old];
457 delta = reward + agent_estimate_q (agent, s_next, a_prime)
458 - agent_estimate_q (agent, agent->s_old, agent->a_old);
459 for (i = 0; i < agent->m; i++)
461 theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
466 * Changes the eligibility trace vector e in various manners:
467 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
468 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
469 * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
470 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
472 * @param agent the agent handle
473 * @param mod the kind of modification
476 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
479 double *e = agent->e;
480 double gamma = agent->envi->parameters.gamma;
481 double lambda = agent->envi->parameters.lambda;
483 for (i = 0; i < agent->m; i++)
487 case RIL_E_ACCUMULATE:
494 e[i] = gamma * lambda;
504 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
505 * notify ATS of its new decision
507 * @param solver solver handle
508 * @param agent agent handle
509 * @param new_address the address which is to be used
510 * @param new_bw_in the new amount of inbound bandwidth set for this address
511 * @param new_bw_out the new amount of outbound bandwidth set for this address
512 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
515 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
516 struct RIL_Peer_Agent *agent,
517 struct ATS_Address *new_address,
518 unsigned long long new_bw_in,
519 unsigned long long new_bw_out,
522 int notify = GNUNET_NO;
524 LOG(GNUNET_ERROR_TYPE_DEBUG, "set_active_suggestion()\n");
527 if (agent->address_inuse != new_address)
529 if (NULL != agent->address_inuse)
531 agent->address_inuse->active = GNUNET_NO;
532 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
533 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
535 if (NULL != new_address)
537 LOG(GNUNET_ERROR_TYPE_DEBUG, "set address active: %s\n", agent->is_active ? "yes" : "no");
538 new_address->active = agent->is_active;
539 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
540 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
542 notify |= GNUNET_YES;
548 if (new_address->active != agent->is_active)
550 new_address->active = agent->is_active;
554 if (agent->bw_in != new_bw_in)
556 agent->bw_in = new_bw_in;
557 new_address->assigned_bw_in.value__ = htonl (new_bw_out);
558 notify |= GNUNET_YES;
560 if (agent->bw_out != new_bw_out)
562 agent->bw_out = new_bw_out;
563 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
564 notify |= GNUNET_YES;
568 if (notify && agent->is_active && (GNUNET_NO == silent))
572 solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
575 else if (agent->address_inuse)
577 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
578 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
582 solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
583 agent->address_inuse);
586 agent->address_inuse = new_address;
590 * Allocates a state vector and fills it with the features present
592 * @param solver the solver handle
593 * @return pointer to the state vector
596 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
600 struct RIL_Network *net;
601 double *state = GNUNET_malloc (sizeof (double) * agent->m);
602 struct RIL_Address_Wrapped *cur_address;
603 const double *properties;
605 for (i = 0; i < solver->networks_count; i++)
607 net = &solver->network_entries[i];
608 state[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) net->bw_in_assigned;
609 state[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net->bw_in_available;
610 state[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) net->bw_out_assigned;
611 state[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net->bw_out_available;
614 i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
616 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
618 state[i++] = cur_address->address_naked->active;
619 state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
620 state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
621 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
622 cur_address->address_naked);
623 for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
625 state[i++] = properties[k];
633 * For all networks a peer has an address in, this gets the maximum bandwidth which could
634 * theoretically be available in one of the networks. This is used for bandwidth normalization.
636 * @param solver the solver handle
637 * @param agent the agent handle
638 * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
640 static long long unsigned
641 ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
644 * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
645 * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
648 struct RIL_Address_Wrapped *cur;
649 struct RIL_Network *net;
651 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
653 net = cur->address_naked->solver_information;
656 if (net->bw_in_available > max)
658 max = net->bw_in_available;
663 if (net->bw_out_available > max)
665 max = net->bw_out_available;
673 * Get the index of the quality-property in question
675 * @param type the quality property type
679 ril_find_property_index (uint32_t type)
681 int existing_types[] = GNUNET_ATS_QualityProperties;
683 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
684 if (existing_types[c] == type)
686 return GNUNET_SYSERR;
690 envi_reward_global (struct GAS_RIL_Handle *solver)
693 unsigned int in_available = 0;
694 unsigned int out_available = 0;
695 unsigned int in_assigned = 0;
696 unsigned int out_assigned = 0;
700 for (i = 0; i < solver->networks_count; i++)
702 in_available += solver->network_entries[i].bw_in_available;
703 in_assigned += solver->network_entries[i].bw_in_assigned;
704 out_available += solver->network_entries[i].bw_out_available;
705 out_assigned += solver->network_entries[i].bw_out_assigned;
708 ratio_in = ((double) in_assigned) / ((double) in_available);
709 ratio_out = ((double) out_assigned) / ((double) out_available);
711 return ((ratio_in + ratio_out) * 0.5) + 1;
715 envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
717 const double *preferences;
718 const double *properties;
720 double pref_match = 0;
723 preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
725 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
726 agent->address_inuse);
728 //preference matching from latency and bandwidth
729 prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
730 pref_match += 1 - (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * (3 - properties[prop_index])); //invert property as we want to maximize for lower latencies
731 bw_norm = GNUNET_MAX(2, (((
732 ((double) agent->bw_in / (double) ril_get_max_bw(agent, GNUNET_YES)) +
733 ((double) agent->bw_out / (double) ril_get_max_bw(agent, GNUNET_NO))
737 pref_match += 1 - (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
739 return pref_match * 0.5;
743 * Gets the reward for the last performed step, which is calculated in equal
744 * parts from the local (the peer specific) and the global (for all peers
747 * @param solver solver handle
751 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
753 struct RIL_Network *net;
756 //punish overutilization
757 net = agent->address_inuse->solver_information;
758 if ((net->bw_in_assigned > net->bw_in_available) || (net->bw_out_assigned > net->bw_out_available))
763 reward += envi_reward_global (solver);
764 reward += envi_reward_local (solver, agent);
769 * Doubles the bandwidth for the active address
771 * @param solver solver handle
772 * @param agent agent handle
773 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
776 envi_action_bw_double (struct GAS_RIL_Handle *solver,
777 struct RIL_Peer_Agent *agent,
782 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2,
783 agent->bw_out, GNUNET_NO);
787 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
788 agent->bw_out * 2, GNUNET_NO);
793 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
794 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
796 * @param solver solver handle
797 * @param agent agent handle
798 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
802 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
803 struct RIL_Peer_Agent *agent,
806 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
807 unsigned long long new_bw;
811 new_bw = agent->bw_in / 2;
814 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
819 new_bw = agent->bw_out / 2;
822 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
828 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
830 * @param solver solver handle
831 * @param agent agent handle
832 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
836 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
838 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
842 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
843 agent->bw_out, GNUNET_NO);
847 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
848 agent->bw_out + (5 * min_bw), GNUNET_NO);
853 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
854 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
856 * @param solver solver handle
857 * @param agent agent handle
858 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
862 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
864 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
865 unsigned long long new_bw;
869 new_bw = agent->bw_in - (5 * min_bw);
872 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
877 new_bw = agent->bw_out - (5 * min_bw);
880 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
886 * Switches to the address given by its index
888 * @param solver solver handle
889 * @param agent agent handle
890 * @param address_index index of the address as it is saved in the agent's list, starting with zero
893 envi_action_address_switch (struct GAS_RIL_Handle *solver,
894 struct RIL_Peer_Agent *agent,
895 unsigned int address_index)
897 struct RIL_Address_Wrapped *cur;
900 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
902 if (i == address_index)
904 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
912 //no address with address_index exists, in this case this action should not be callable
913 GNUNET_assert(GNUNET_NO);
917 * Puts the action into effect by calling the according function
919 * @param solver solver handle
920 * @param action action to perform by the solver
923 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
929 case RIL_ACTION_NOTHING:
931 case RIL_ACTION_BW_IN_DBL:
932 envi_action_bw_double (solver, agent, GNUNET_YES);
934 case RIL_ACTION_BW_IN_HLV:
935 envi_action_bw_halven (solver, agent, GNUNET_YES);
937 case RIL_ACTION_BW_IN_INC:
938 envi_action_bw_inc (solver, agent, GNUNET_YES);
940 case RIL_ACTION_BW_IN_DEC:
941 envi_action_bw_dec (solver, agent, GNUNET_YES);
943 case RIL_ACTION_BW_OUT_DBL:
944 envi_action_bw_double (solver, agent, GNUNET_NO);
946 case RIL_ACTION_BW_OUT_HLV:
947 envi_action_bw_halven (solver, agent, GNUNET_NO);
949 case RIL_ACTION_BW_OUT_INC:
950 envi_action_bw_inc (solver, agent, GNUNET_NO);
952 case RIL_ACTION_BW_OUT_DEC:
953 envi_action_bw_dec (solver, agent, GNUNET_NO);
956 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
958 address_index = action - RIL_ACTION_TYPE_NUM;
960 GNUNET_assert(address_index >= 0);
962 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
964 envi_action_address_switch (solver, agent, address_index);
967 // error - action does not exist
968 GNUNET_assert(GNUNET_NO);
973 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
974 * after having done the last action a_old. It observes the new state s_next and the reward
975 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
976 * next action is put into effect.
978 * @param agent the agent performing the step
981 agent_step (struct RIL_Peer_Agent *agent)
983 int a_next = RIL_ACTION_INVALID;
987 s_next = envi_get_state (agent->envi, agent);
988 reward = envi_get_reward (agent->envi, agent);
990 LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
991 agent->envi->parameters.algorithm ? "Q" : "SARSA");
993 switch (agent->envi->parameters.algorithm)
996 agent_modify_eligibility (agent, RIL_E_SET);
997 if (agent_decide_exploration (agent))
999 a_next = agent_get_action_explore (agent, s_next);
1003 a_next = agent_get_action_best (agent, s_next);
1005 if (RIL_ACTION_INVALID != agent->a_old)
1007 //updates weights with selected action (on-policy), if not first step
1008 agent_update_weights (agent, reward, s_next, a_next);
1013 a_next = agent_get_action_best (agent, s_next);
1014 if (RIL_ACTION_INVALID != agent->a_old)
1016 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1017 agent_update_weights (agent, reward, s_next, a_next);
1019 if (agent_decide_exploration (agent))
1021 a_next = agent_get_action_explore (agent, s_next);
1022 agent_modify_eligibility (agent, RIL_E_ZERO);
1026 a_next = agent_get_action_best (agent, s_next);
1027 agent_modify_eligibility (agent, RIL_E_SET);
1032 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1034 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
1036 envi_do_action (agent->envi, agent, a_next);
1038 GNUNET_free(agent->s_old);
1039 agent->s_old = s_next;
1040 agent->a_old = a_next;
1042 agent->step_count += 1;
1046 * Cycles through all agents and lets the active ones do a step. Schedules the next step.
1048 * @param solver the solver handle
1049 * @param tc task context for the scheduler
1052 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1054 struct GAS_RIL_Handle *solver = cls;
1055 struct RIL_Peer_Agent *cur;
1057 LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
1059 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1061 if (cur->is_active && cur->address_inuse)
1067 solver->step_count += 1;
1068 solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, &ril_periodic_step, solver);
1072 * Initialize an agent without addresses and its knowledge base
1074 * @param s ril solver
1075 * @param peer the one in question
1076 * @return handle to the new agent
1078 static struct RIL_Peer_Agent *
1079 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1082 struct GAS_RIL_Handle * solver = s;
1083 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
1085 agent->envi = solver;
1086 agent->peer = *peer;
1087 agent->step_count = 0;
1088 agent->is_active = GNUNET_NO;
1089 agent->n = RIL_ACTION_TYPE_NUM;
1090 agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
1091 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1092 for (i = 0; i < agent->n; i++)
1094 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1096 agent->a_old = RIL_ACTION_INVALID;
1097 agent->s_old = envi_get_state (solver, agent);
1098 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1099 agent_modify_eligibility (agent, RIL_E_ZERO);
1101 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
1109 * @param s solver handle
1110 * @param agent the agent to retire
1113 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1117 for (i = 0; i < agent->n; i++)
1119 GNUNET_free(agent->W[i]);
1121 GNUNET_free(agent->W);
1122 GNUNET_free(agent->e);
1123 GNUNET_free(agent->s_old);
1128 * Returns the agent for a peer
1130 * @param s solver handle
1131 * @param peer identity of the peer
1132 * @param create whether to create an agent if none is allocated yet
1135 static struct RIL_Peer_Agent *
1136 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1138 struct RIL_Peer_Agent *cur;
1140 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1142 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1150 return agent_init (solver, peer);
1156 * Lookup network struct by type
1158 * @param s the solver handle
1159 * @param type the network type
1160 * @return the network struct
1162 static struct RIL_Network *
1163 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1167 for (i = 0; i < s->networks_count; i++)
1169 if (s->network_entries[i].type == type)
1171 return &s->network_entries[i];
1178 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1179 * considered inactive and not used. Addresses in an inactive network are ignored.
1181 * @param solver solver handle
1182 * @param network the network type
1183 * @return whether or not the network is considered active
1186 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1188 struct RIL_Network *net;
1189 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1191 net = ril_get_network (solver, network);
1192 if (net->bw_out_available < min_bw)
1198 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1199 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1200 * frees the memory of the old vector, and redirects the pointer to the new one.
1202 * @param old pointer to the pointer to the first element of the vector
1203 * @param element_size byte size of the vector elements
1204 * @param hole_start the first element to cut out
1205 * @param hole_length the number of elements to cut out
1206 * @param old_length the length of the old vector
1209 ril_cut_from_vector (void **old,
1210 size_t element_size,
1211 unsigned int hole_start,
1212 unsigned int hole_length,
1213 unsigned int old_length)
1216 char *oldptr = (char *) *old;
1218 unsigned int bytes_before;
1219 unsigned int bytes_hole;
1220 unsigned int bytes_after;
1222 GNUNET_assert(old_length > hole_length);
1223 GNUNET_assert(old_length >= (hole_start + hole_length));
1225 size = element_size * (old_length - hole_length);
1227 bytes_before = element_size * hole_start;
1228 bytes_hole = element_size * hole_length;
1229 bytes_after = element_size * (old_length - hole_start - hole_length);
1237 tmpptr = GNUNET_malloc (size);
1238 memcpy (tmpptr, oldptr, bytes_before);
1239 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1245 *old = (void *) tmpptr;
1249 * Solver API functions
1250 * ---------------------------
1254 * Change relative preference for quality in solver
1256 * @param solver the solver handle
1257 * @param peer the peer to change the preference for
1258 * @param kind the kind to change the preference
1259 * @param pref_rel the normalized preference value for this kind over all clients
1262 GAS_ril_address_change_preference (void *s,
1263 const struct GNUNET_PeerIdentity *peer,
1264 enum GNUNET_ATS_PreferenceKind kind,
1267 LOG(GNUNET_ERROR_TYPE_DEBUG,
1268 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1269 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1271 * Nothing to do here. Preferences are considered during reward calculation.
1276 * Entry point for the plugin
1278 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1281 libgnunet_plugin_ats_ril_init (void *cls)
1283 struct GNUNET_ATS_PluginEnvironment *env = cls;
1284 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1285 struct RIL_Network * cur;
1287 unsigned long long tmp;
1290 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1292 GNUNET_assert(NULL != env);
1293 GNUNET_assert(NULL != env->cfg);
1294 GNUNET_assert(NULL != env->stats);
1295 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1296 GNUNET_assert(NULL != env->get_preferences);
1297 GNUNET_assert(NULL != env->get_property);
1300 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
1302 solver->step_time = RIL_DEFAULT_STEP_TIME;
1304 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string)
1305 && NULL != string && 0 == strcmp (string, "SARSA"))
1307 solver->parameters.algorithm = RIL_ALGO_SARSA;
1311 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1314 == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
1316 solver->parameters.gamma = (double) tmp / 100;
1320 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
1323 == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
1325 solver->parameters.alpha = (double) tmp / 100;
1329 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1331 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_TRACE_DECAY", &tmp))
1333 solver->parameters.lambda = (double) tmp / 100;
1337 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1340 env->sf.s_add = &GAS_ril_address_add;
1341 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1342 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1343 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
1344 env->sf.s_address_update_network = &GAS_ril_address_change_network;
1345 env->sf.s_get = &GAS_ril_get_preferred_address;
1346 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
1347 env->sf.s_pref = &GAS_ril_address_change_preference;
1348 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
1349 env->sf.s_del = &GAS_ril_address_delete;
1350 env->sf.s_bulk_start = &GAS_ril_bulk_start;
1351 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
1353 solver->plugin_envi = env;
1354 solver->networks_count = env->network_count;
1355 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
1356 solver->step_count = 0;
1358 for (c = 0; c < env->network_count; c++)
1360 cur = &solver->network_entries[c];
1361 cur->type = env->networks[c];
1362 cur->bw_in_available = env->in_quota[c];
1363 cur->bw_in_assigned = 0;
1364 cur->bw_out_available = env->out_quota[c];
1365 cur->bw_out_assigned = 0;
1368 solver->next_step = GNUNET_SCHEDULER_add_delayed (
1369 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1370 &ril_periodic_step, solver);
1376 * Exit point for the plugin
1378 * @param cls the solver handle
1381 libgnunet_plugin_ats_ril_done (void *cls)
1383 struct GAS_RIL_Handle *s = cls;
1384 struct RIL_Peer_Agent *cur_agent;
1385 struct RIL_Peer_Agent *next_agent;
1387 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1389 cur_agent = s->agents_head;
1390 while (NULL != cur_agent)
1392 next_agent = cur_agent->next;
1393 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1394 agent_die (s, cur_agent);
1395 cur_agent = next_agent;
1398 GNUNET_SCHEDULER_cancel (s->next_step);
1399 GNUNET_free(s->network_entries);
1406 * Add a new address for a peer to the solver
1408 * The address is already contained in the addresses hashmap!
1410 * @param solver the solver Handle
1411 * @param address the address to add
1412 * @param network network type of this address
1415 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1417 struct GAS_RIL_Handle *s = solver;
1418 struct RIL_Peer_Agent *agent;
1419 struct RIL_Address_Wrapped *address_wrapped;
1420 struct RIL_Network *net;
1427 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1429 net = ril_get_network (s, network);
1430 address->solver_information = net;
1432 if (!ril_network_is_active (s, network))
1434 LOG(GNUNET_ERROR_TYPE_DEBUG,
1435 "API_address_add() Did not add %s address %p for peer '%s', network does not have enough bandwidth\n",
1436 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1440 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1443 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1444 address_wrapped->address_naked = address;
1445 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1447 //increase size of W
1448 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
1450 n_new = agent->n + 1;
1453 GNUNET_array_grow(agent->W, agent->n, n_new);
1454 for (i = 0; i < n_new; i++)
1459 GNUNET_array_grow(agent->W[i], agent->m, m_new);
1464 GNUNET_array_grow(agent->W[i], zero, m_new);
1468 //increase size of old state vector
1470 GNUNET_array_grow(agent->s_old, agent->m, m_new);
1473 GNUNET_array_grow(agent->e, agent->m, m_new);
1475 if (NULL == agent->address_inuse)
1477 net->bw_in_assigned += min_bw;
1478 net->bw_out_assigned += min_bw;
1479 envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
1482 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %p for peer '%s'\n",
1483 address->active ? "active" : "inactive", address->plugin, address->addr,
1484 GNUNET_i2s (&address->peer));
1488 * Delete an address in the solver
1490 * The address is not contained in the address hashmap anymore!
1492 * @param solver the solver handle
1493 * @param address the address to remove
1494 * @param session_only delete only session not whole address
1497 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1499 struct GAS_RIL_Handle *s = solver;
1500 struct RIL_Peer_Agent *agent;
1501 struct RIL_Address_Wrapped *address_wrapped;
1502 int address_was_used = address->active;
1507 struct RIL_Network *net;
1508 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1510 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
1511 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
1512 address->addr, GNUNET_i2s (&address->peer));
1514 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1517 net = address->solver_information;
1518 GNUNET_assert(!ril_network_is_active (s, net->type));
1519 LOG(GNUNET_ERROR_TYPE_DEBUG,
1520 "No agent allocated for peer yet, since address was in inactive network\n");
1524 address_index = agent_address_get_index (agent, address);
1525 address_wrapped = agent_address_get (agent, address);
1527 if (NULL == address_wrapped)
1529 net = address->solver_information;
1530 GNUNET_assert(!ril_network_is_active (s, net->type));
1531 LOG(GNUNET_ERROR_TYPE_DEBUG,
1532 "Address not considered by agent, address was in inactive network\n");
1536 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1537 GNUNET_free(address_wrapped);
1540 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
1541 n_new = agent->n - 1;
1543 for (i = 0; i < agent->n; i++)
1545 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
1546 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1547 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1549 GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
1550 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
1552 //correct last action
1553 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1557 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1559 agent->a_old = RIL_ACTION_INVALID;
1561 //decrease old state vector and eligibility vector
1562 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
1563 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1564 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1565 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
1566 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1567 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1571 LOG(GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
1573 if (address_was_used)
1575 net = address->solver_information;
1576 net->bw_in_assigned -= agent->bw_in;
1577 net->bw_out_assigned -= agent->bw_out;
1579 if (NULL != agent->addresses_head) //if peer has an address left, use it
1581 LOG(GNUNET_ERROR_TYPE_DEBUG, "address left: %p\n",
1582 agent->addresses_head->address_naked->addr);
1583 //TODO? check if network/bandwidth update can be done more clever/elegant at different function
1584 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw,
1586 net = agent->addresses_head->address_naked->solver_information;
1587 net->bw_in_assigned -= min_bw;
1588 net->bw_out_assigned -= min_bw;
1592 LOG(GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
1594 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
1598 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
1602 * Update the properties of an address in the solver
1604 * @param solver solver handle
1605 * @param address the address
1606 * @param type the ATSI type in HBO
1607 * @param abs_value the absolute value of the property
1608 * @param rel_value the normalized value
1611 GAS_ril_address_property_changed (void *solver,
1612 struct ATS_Address *address,
1617 LOG(GNUNET_ERROR_TYPE_DEBUG,
1618 "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1619 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1620 address->addr, rel_value);
1622 * Nothing to do here, properties are considered in every reward calculation
1627 * Update the session of an address in the solver
1629 * NOTE: values in addresses are already updated
1631 * @param solver solver handle
1632 * @param address the address
1633 * @param cur_session the current session
1634 * @param new_session the new session
1637 GAS_ril_address_session_changed (void *solver,
1638 struct ATS_Address *address,
1639 uint32_t cur_session,
1640 uint32_t new_session)
1643 * TODO? Future Work: Potentially add session activity as a feature in state vector
1645 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1649 * Notify the solver that an address is (not) actively used by transport
1650 * to communicate with a remote peer
1652 * NOTE: values in addresses are already updated
1654 * @param solver solver handle
1655 * @param address the address
1656 * @param in_use usage state
1659 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1661 /* Nothing to do here.
1662 * Possible TODO? Future Work: Use usage as state vector
1664 LOG(GNUNET_ERROR_TYPE_DEBUG,
1665 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1666 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1670 * Notify solver that the network an address is located in has changed
1672 * NOTE: values in addresses are already updated
1674 * @param solver solver handle
1675 * @param address the address
1676 * @param current_network the current network
1677 * @param new_network the new network
1680 GAS_ril_address_change_network (void *solver,
1681 struct ATS_Address *address,
1682 uint32_t current_network,
1683 uint32_t new_network)
1685 struct GAS_RIL_Handle *s = solver;
1686 struct RIL_Peer_Agent *agent;
1687 struct RIL_Network *net;
1688 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1690 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
1691 "%s address of peer %s from '%s' to '%s'\n",
1692 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
1693 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
1695 if (address->active && !ril_network_is_active (solver, new_network))
1697 GAS_ril_address_delete (solver, address, GNUNET_NO);
1701 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1704 GNUNET_assert(!ril_network_is_active (solver, current_network));
1706 GAS_ril_address_add (s, address, new_network);
1710 net = ril_get_network (s, current_network);
1711 net->bw_in_assigned -= agent->bw_in;
1712 net->bw_out_assigned -= agent->bw_out;
1714 net = ril_get_network (s, new_network);
1715 net->bw_in_assigned -= min_bw;
1716 net->bw_out_assigned -= min_bw;
1717 address->solver_information = net;
1721 * Give feedback about the current assignment
1723 * @param solver the solver handle
1724 * @param application the application
1725 * @param peer the peer to change the preference for
1726 * @param scope the time interval for this feedback: [now - scope .. now]
1727 * @param kind the kind to change the preference
1728 * @param score the score
1731 GAS_ril_address_preference_feedback (void *solver,
1733 const struct GNUNET_PeerIdentity *peer,
1734 const struct GNUNET_TIME_Relative scope,
1735 enum GNUNET_ATS_PreferenceKind kind,
1738 LOG(GNUNET_ERROR_TYPE_DEBUG,
1739 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1740 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1741 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1745 * Start a bulk operation
1747 * Since new calculations of the assignment are not triggered by a change of preferences, as it
1748 * happens in the proportional and the mlp solver, there is no need to block this solver.
1750 * @param solver the solver
1753 GAS_ril_bulk_start (void *solver)
1759 * Bulk operation done
1761 * Since new calculations of the assignment are not triggered by a change of preferences, as it
1762 * happens in the proportional and the mlp solver, there is no need to block this solver.
1764 * @param solver the solver handle
1767 GAS_ril_bulk_stop (void *solver)
1773 * Tell solver to notify ATS if the address to use changes for a specific
1774 * peer using the bandwidth changed callback
1776 * The solver must only notify about changes for peers with pending address
1779 * @param solver the solver handle
1780 * @param peer the identity of the peer
1782 const struct ATS_Address *
1783 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1786 * activate agent, return currently chosen address
1788 struct GAS_RIL_Handle *s = solver;
1789 struct RIL_Peer_Agent *agent;
1790 struct RIL_Network *net;
1792 agent = ril_get_agent (s, peer, GNUNET_YES);
1794 agent->is_active = GNUNET_YES;
1796 if (agent->address_inuse)
1798 net = agent->address_inuse->solver_information;
1799 net->bw_in_assigned += agent->bw_in;
1800 net->bw_out_assigned += agent->bw_out;
1802 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
1805 if (agent->address_inuse)
1807 LOG(GNUNET_ERROR_TYPE_DEBUG,
1808 "API_get_preferred_address() Activated agent for peer '%s' with %s address %p\n",
1809 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
1813 LOG(GNUNET_ERROR_TYPE_DEBUG,
1814 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
1818 return agent->address_inuse;
1822 * Tell solver stop notifying ATS about changes for this peers
1824 * The solver must only notify about changes for peers with pending address
1827 * @param solver the solver handle
1828 * @param peer the peer
1831 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1833 struct GAS_RIL_Handle *s = solver;
1834 struct RIL_Peer_Agent *agent;
1835 struct RIL_Network *net;
1837 agent = ril_get_agent (s, peer, GNUNET_NO);
1844 if (GNUNET_NO == agent->is_active)
1850 agent->is_active = GNUNET_NO;
1851 if (agent->address_inuse)
1853 net = agent->address_inuse->solver_information;
1854 net->bw_in_assigned -= agent->bw_in;
1855 net->bw_out_assigned -= agent->bw_out;
1857 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
1860 LOG(GNUNET_ERROR_TYPE_DEBUG,
1861 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1862 GNUNET_i2s (peer), agent->address_inuse->plugin);
1865 /* end of libgnunet_plugin_ats_ril.c */