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/plugin_ats_ril.c
23 * @brief ATS reinforcement learning solver
24 * @author Fabian Oehlmann
25 * @author Matthias Wachs
27 #include "plugin_ats_ril.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
31 #define RIL_MIN_BW ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)
32 #define RIL_MAX_BW GNUNET_ATS_MaxBandwidth
34 #define RIL_ACTION_INVALID -1
35 #define RIL_INTERVAL_EXPONENT 10
36 #define RIL_UTILITY_MAX (double) RIL_MAX_BW
38 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
39 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
40 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
41 #define RIL_DEFAULT_SELECT RIL_SELECT_EGREEDY
42 #define RIL_DEFAULT_WELFARE RIL_WELFARE_EGALITARIAN
43 #define RIL_DEFAULT_DISCOUNT_BETA 1.0
44 #define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
45 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.1
46 #define RIL_DEFAULT_TRACE_DECAY 0.5
47 #define RIL_DEFAULT_EXPLORE_RATIO 0.1
48 #define RIL_DEFAULT_RBF_DIVISOR 10
49 #define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5
50 #define RIL_DEFAULT_TEMPERATURE 1.0
52 #define RIL_INC_DEC_STEP_SIZE 1
53 #define RIL_NOP_BONUS 0.5
56 * ATS reinforcement learning solver
62 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
63 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
64 * to a particular address. The action of switching to address with index i is depicted by the
65 * number (RIL_ACTION_TYPE_NUM + i).
69 RIL_ACTION_NOTHING = 0,
70 RIL_ACTION_BW_IN_DBL = -2, //TODO! put actions back
71 RIL_ACTION_BW_IN_HLV = -3,
72 RIL_ACTION_BW_IN_INC = 1,
73 RIL_ACTION_BW_IN_DEC = 2,
74 RIL_ACTION_BW_OUT_DBL = -4,
75 RIL_ACTION_BW_OUT_HLV = -5,
76 RIL_ACTION_BW_OUT_INC = -6,
77 RIL_ACTION_BW_OUT_DEC = -7,
78 RIL_ACTION_TYPE_NUM = 3
89 RIL_SELECT_SOFTMAX = 0,
90 RIL_SELECT_EGREEDY = 1
96 RIL_WELFARE_EGALITARIAN
99 enum RIL_E_Modification
108 * Global learning parameters
110 struct RIL_Learning_Parameters
113 * The TD-algorithm to use
115 enum RIL_Algorithm algorithm;
118 * Gradient-descent step-size
123 * Learning discount variable in the TD-update for semi-MDPs
128 * Learning discount factor in the TD-update for MDPs
133 * Trace-decay factor for eligibility traces
138 * Whether to accumulate or replace eligibility traces
140 enum RIL_E_Modification eligibility_trace_mode;
143 * Softmax action-selection temperature
148 * Which measure of social welfare should be used
150 enum RIL_Welfare social_welfare;
153 * State space divisor
155 unsigned long long rbf_divisor;
158 * Action selection strategy;
160 enum RIL_Select select;
163 * Ratio, with what probability an agent should explore in the e-greed policy
165 double explore_ratio;
168 * How big the share of the global part of the reward signal is
170 double reward_global_share;
173 * Minimal interval time between steps in milliseconds
175 struct GNUNET_TIME_Relative step_time_min;
178 * Maximum interval time between steps in milliseconds
180 struct GNUNET_TIME_Relative step_time_max;
184 * Wrapper for addresses to store them in agent's linked list
186 struct RIL_Address_Wrapped
191 struct RIL_Address_Wrapped *next;
196 struct RIL_Address_Wrapped *prev;
201 struct ATS_Address *address_naked;
204 struct RIL_Peer_Agent
207 * Next agent in solver's linked list
209 struct RIL_Peer_Agent *next;
212 * Previous agent in solver's linked list
214 struct RIL_Peer_Agent *prev;
219 struct GAS_RIL_Handle *envi;
224 struct GNUNET_PeerIdentity peer;
227 * Whether the agent is active or not
232 * Number of performed time-steps
234 unsigned long long step_count;
237 * Experience matrix W
242 * Number of rows of W / Number of state-vector features
247 * Number of columns of W / Number of actions
252 * Last perceived state feature vector
269 struct ATS_Address * address_inuse;
272 * Head of addresses DLL
274 struct RIL_Address_Wrapped * addresses_head;
277 * Tail of addresses DLL
279 struct RIL_Address_Wrapped * addresses_tail;
282 * Inbound bandwidth assigned by the agent
284 unsigned long long bw_in;
287 * Outbound bandwidth assigned by the agent
289 unsigned long long bw_out;
292 * Flag whether a suggestion has to be issued
294 int suggestion_issue;
297 * The address which has to be issued
299 struct ATS_Address * suggestion_address;
302 * The agent's last objective value
304 double objective_old;
317 enum GNUNET_ATS_Network_Type type;
320 * Total available inbound bandwidth
322 unsigned long long bw_in_available;
325 * Bandwidth inbound assigned in network after last step
327 unsigned long long bw_in_assigned;
330 * Total available outbound bandwidth
332 unsigned long long bw_out_available;
335 * * Bandwidth outbound assigned in network after last step
337 unsigned long long bw_out_assigned;
340 * Number of active agents in scope
342 unsigned int agent_count;
345 * The social welfare achieved in the scope
347 double social_welfare;
351 * A handle for the reinforcement learning solver
353 struct GAS_RIL_Handle
356 * The solver-plugin environment of the solver-plugin API
358 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
363 struct GNUNET_STATISTICS_Handle *stats;
366 * Number of performed steps
368 unsigned long long step_count;
371 * Timestamp for the last time-step
373 struct GNUNET_TIME_Absolute step_time_last;
376 * Task identifier of the next time-step to be executed
378 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
381 * Variable discount factor, dependent on time between steps
383 double global_discount_variable;
386 * Integrated variable discount factor, dependent on time between steps
388 double global_discount_integrated;
391 * Lock for bulk operations
396 * Number of changes during a lock
401 * Learning parameters
403 struct RIL_Learning_Parameters parameters;
406 * Array of networks with global assignment state
408 struct RIL_Scope * network_entries;
413 unsigned int networks_count;
416 * List of active peer-agents
418 struct RIL_Peer_Agent * agents_head;
419 struct RIL_Peer_Agent * agents_tail;
427 * Simulate steps, i.e. schedule steps immediately
429 unsigned long long simulate;
434 * ---------------------------
438 * Estimate the current action-value for state s and action a
440 * @param agent agent performing the estimation
443 * @return estimation value
446 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
451 for (i = 0; i < agent->m; i++)
453 result += state[i] * agent->W[action][i];
456 GNUNET_assert(!isnan(result));
458 //prevent crash when learning diverges
461 return isinf(result) * UINT32_MAX;
468 * Get the index of the address in the agent's list.
470 * @param agent agent handle
471 * @param address address handle
472 * @return the index, starting with zero
475 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
478 struct RIL_Address_Wrapped *cur;
481 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
484 if (cur->address_naked == address)
492 * Gets the wrapped address from the agent's list
494 * @param agent agent handle
495 * @param address address handle
496 * @return wrapped address
498 static struct RIL_Address_Wrapped *
499 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
501 struct RIL_Address_Wrapped *cur;
503 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
504 if (cur->address_naked == address)
511 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
517 case RIL_ACTION_NOTHING:
520 case RIL_ACTION_BW_IN_INC:
521 case RIL_ACTION_BW_IN_DBL:
522 if (agent->bw_in >= RIL_MAX_BW)
527 case RIL_ACTION_BW_IN_DEC:
528 case RIL_ACTION_BW_IN_HLV:
529 if (agent->bw_in <= RIL_MIN_BW)
534 case RIL_ACTION_BW_OUT_INC:
535 case RIL_ACTION_BW_OUT_DBL:
536 if (agent->bw_out >= RIL_MAX_BW)
541 case RIL_ACTION_BW_OUT_DEC:
542 case RIL_ACTION_BW_OUT_HLV:
543 if (agent->bw_out <= RIL_MIN_BW)
549 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
551 address_index = action - RIL_ACTION_TYPE_NUM;
553 GNUNET_assert(address_index >= 0);
555 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
557 if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
558 agent->address_inuse->active)
564 // error - action does not exist
565 GNUNET_assert(GNUNET_NO);
571 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
572 * most reward in the future)
574 * @param agent agent performing the calculation
575 * @param state the state from which to take the action
576 * @return the action promising most future reward
579 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
582 int max_i = RIL_ACTION_INVALID;
584 double max_q = -DBL_MAX;
586 for (i = 0; i < agent->n; i++)
588 if (agent_action_is_possible(agent, i))
590 cur_q = agent_estimate_q (agent, state, i);
599 GNUNET_assert(RIL_ACTION_INVALID != max_i);
606 agent_get_action_random (struct RIL_Peer_Agent *agent)
609 int is_possible[agent->n];
613 for (i = 0; i<agent->n; i++)
615 if (agent_action_is_possible(agent, i))
617 is_possible[i] = GNUNET_YES;
622 is_possible[i] = GNUNET_NO;
626 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
629 for (i = 0; i<agent->n; i++)
639 GNUNET_assert(GNUNET_NO);
644 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
646 * @param agent the agent performing the update
647 * @param reward the reward received for the last action
648 * @param s_next the new state, the last step got the agent into
649 * @param a_prime the new
652 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
656 double *theta = agent->W[agent->a_old];
658 delta = agent->envi->global_discount_integrated * reward; //reward
659 delta += agent->envi->global_discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
660 delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
662 // LOG(GNUNET_ERROR_TYPE_INFO, "update() Step# %llu Q(s,a): %f a: %f r: %f y: %f Q(s+1,a+1) = %f delta: %f\n",
663 // agent->step_count,
664 // agent_estimate_q (agent, agent->s_old, agent->a_old),
665 // agent->envi->parameters.alpha,
667 // agent->envi->global_discount_variable,
668 // agent_estimate_q (agent, s_next, a_prime),
671 for (i = 0; i < agent->m; i++)
673 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
674 // agent->envi->parameters.alpha,
678 theta[i] += agent->envi->parameters.alpha * delta * agent->s_old[i] * agent->E[a_prime][i];
684 * Changes the eligibility trace vector e in various manners:
685 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
686 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
687 * #RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
688 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
690 * @param agent the agent handle
691 * @param mod the kind of modification
692 * @param feature the feature vector
695 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
696 enum RIL_E_Modification mod,
703 for (i = 0; i < agent->m; i++)
707 case RIL_E_ACCUMULATE:
708 agent->E[action][i] += feature[i];
711 agent->E[action][i] = (agent->envi->global_discount_variable * agent->envi->parameters.lambda * agent->E[action][i]) > feature[i] ? agent->E[action][i] : feature[i];
714 agent->E[action][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
717 for (k = 0; k < agent->n; k++)
728 ril_inform (struct GAS_RIL_Handle *solver,
729 enum GAS_Solver_Operation op,
730 enum GAS_Solver_Status stat)
732 if (NULL != solver->plugin_envi->info_cb)
733 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
738 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
739 * notify ATS of its new decision
741 * @param solver solver handle
742 * @param agent agent handle
743 * @param new_address the address which is to be used
744 * @param new_bw_in the new amount of inbound bandwidth set for this address
745 * @param new_bw_out the new amount of outbound bandwidth set for this address
746 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
749 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
750 struct RIL_Peer_Agent *agent,
751 struct ATS_Address *new_address,
752 unsigned long long new_bw_in,
753 unsigned long long new_bw_out,
756 int notify = GNUNET_NO;
758 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
761 if (agent->address_inuse != new_address)
763 if (NULL != agent->address_inuse)
765 agent->address_inuse->active = GNUNET_NO;
766 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
767 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
769 if (NULL != new_address)
771 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
772 new_address->active = agent->is_active;
773 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
774 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
776 notify |= GNUNET_YES;
782 if (new_address->active != agent->is_active)
784 new_address->active = agent->is_active;
785 notify |= GNUNET_YES;
789 if (agent->bw_in != new_bw_in)
791 agent->bw_in = new_bw_in;
792 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
793 notify |= GNUNET_YES;
795 if (agent->bw_out != new_bw_out)
797 agent->bw_out = new_bw_out;
798 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
799 notify |= GNUNET_YES;
803 if (notify && agent->is_active && (GNUNET_NO == silent))
807 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
808 agent->suggestion_issue = GNUNET_YES;
809 agent->suggestion_address = new_address;
811 else if (agent->address_inuse)
813 //disconnect case, no new address
814 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
815 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
819 agent->suggestion_issue = GNUNET_YES;
820 agent->suggestion_address = agent->address_inuse;
823 agent->address_inuse = new_address;
827 static unsigned long long
828 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
830 struct RIL_Peer_Agent *cur;
831 struct RIL_Scope *net;
832 unsigned long long sum = 0;
834 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
836 if (cur->is_active && cur->address_inuse)
838 net = cur->address_inuse->solver_information;
839 if (net->type == type)
853 * Allocates a state vector and fills it with the features present
854 * @param solver the solver handle
855 * @param agent the agent handle
856 * @return pointer to the state vector
859 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
870 unsigned long long max_bw;
872 state = GNUNET_malloc (sizeof(double) * agent->m);
876 y[0] = (double) agent->bw_out;
877 y[1] = (double) agent->bw_in;
879 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
880 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
882 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
884 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
885 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
888 sigma = (((double) max_bw / (double) solver->parameters.rbf_divisor) / 2.0) * M_SQRT2;
889 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
898 // * For all networks a peer has an address in, this gets the maximum bandwidth which could
899 // * theoretically be available in one of the networks. This is used for bandwidth normalization.
901 // * @param agent the agent handle
902 // * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
904 //static unsigned long long
905 //ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
908 // * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
909 // * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
911 // unsigned long long max = 0;
912 // struct RIL_Address_Wrapped *cur;
913 // struct RIL_Scope *net;
915 // for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
917 // net = cur->address_naked->solver_information;
920 // if (net->bw_in_available > max)
922 // max = net->bw_in_available;
927 // if (net->bw_out_available > max)
929 // max = net->bw_out_available;
937 // * Get the index of the quality-property in question
939 // * @param type the quality property type
940 // * @return the index
943 //ril_find_property_index (uint32_t type)
945 // int existing_types[] = GNUNET_ATS_QualityProperties;
947 // for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
948 // if (existing_types[c] == type)
950 // return GNUNET_SYSERR;
954 //ril_get_atsi (struct ATS_Address *address, uint32_t type)
957 // GNUNET_assert(NULL != address);
959 // if ((NULL == address->atsi) || (0 == address->atsi_count))
962 // for (c1 = 0; c1 < address->atsi_count; c1++)
964 // if (ntohl (address->atsi[c1].type) == type)
965 // return ntohl (address->atsi[c1].value);
972 //envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
974 // const double *preferences;
975 // const double *properties;
977 // double pref_match = 0;
981 // preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
983 // properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
984 // agent->address_inuse);
987 // prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
988 // dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
990 // // utilization in [0,1]
991 // bw_norm = (((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
992 // / (double) ril_get_max_bw (agent, GNUNET_YES))
993 // + ((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
994 // / (double) ril_get_max_bw (agent, GNUNET_NO))) / 2;
996 // // preference matching in [0,4]
997 // pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
998 // pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
1000 // // local reward in [1,2]
1001 // return (pref_match / 4) +1;
1005 agent_get_utility (struct RIL_Peer_Agent *agent)
1007 return (double) (agent->bw_in/RIL_MIN_BW);
1008 // return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
1012 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1014 struct RIL_Peer_Agent *cur;
1017 switch (solver->parameters.social_welfare)
1019 case RIL_WELFARE_EGALITARIAN:
1020 result = RIL_UTILITY_MAX;
1021 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1023 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1025 result = GNUNET_MIN(result, agent_get_utility(cur));
1030 case RIL_WELFARE_NASH:
1032 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1034 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1036 result *= agent_get_utility(cur);
1039 return pow(result, 1.0 / (double) scope->agent_count);
1041 GNUNET_assert(GNUNET_NO);
1046 * Gets the reward for the last performed step, which is calculated in equal
1047 * parts from the local (the peer specific) and the global (for all peers
1048 * identical) reward.
1050 * @param solver the solver handle
1051 * @param agent the agent handle
1052 * @return the reward
1055 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1057 struct RIL_Scope *net;
1058 unsigned long long overutilization;
1059 unsigned long long over_in = 0;
1060 unsigned long long over_out = 0;
1065 net = agent->address_inuse->solver_information;
1067 if (net->bw_in_assigned > net->bw_in_available)
1068 over_in = net->bw_in_assigned - net->bw_in_available;
1069 if (net->bw_out_assigned > net->bw_out_available)
1070 over_out = net->bw_out_assigned - net->bw_out_available;
1071 overutilization = GNUNET_MAX(over_in, over_out) / RIL_MIN_BW;
1073 objective = agent_get_utility (agent) + net->social_welfare;
1074 delta = objective - agent->objective_old;
1075 agent->objective_old = objective;
1079 agent->nop_bonus = 0;
1082 LOG(GNUNET_ERROR_TYPE_DEBUG, "utility: %f, welfare: %f, objective, overutilization: %d\n", agent_get_utility (agent), net->social_welfare, objective, overutilization);
1084 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1086 if (0 != overutilization)
1088 return -1.0 * (double) overutilization;
1092 return delta + steady;
1097 * Doubles the bandwidth for the active address
1099 * @param solver solver handle
1100 * @param agent agent handle
1101 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1104 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1105 struct RIL_Peer_Agent *agent,
1108 unsigned long long new_bw;
1112 new_bw = agent->bw_in * 2;
1113 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1114 new_bw = RIL_MAX_BW;
1115 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1116 agent->bw_out, GNUNET_NO);
1120 new_bw = agent->bw_out * 2;
1121 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1122 new_bw = RIL_MAX_BW;
1123 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1129 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1130 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1132 * @param solver solver handle
1133 * @param agent agent handle
1134 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1138 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1139 struct RIL_Peer_Agent *agent,
1142 unsigned long long new_bw;
1146 new_bw = agent->bw_in / 2;
1147 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1148 new_bw = RIL_MIN_BW;
1149 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1154 new_bw = agent->bw_out / 2;
1155 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1156 new_bw = RIL_MIN_BW;
1157 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1163 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1165 * @param solver solver handle
1166 * @param agent agent handle
1167 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1171 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1173 unsigned long long new_bw;
1177 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1178 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1179 new_bw = RIL_MAX_BW;
1180 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1181 agent->bw_out, GNUNET_NO);
1185 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1186 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1187 new_bw = RIL_MAX_BW;
1188 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1194 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1195 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1197 * @param solver solver handle
1198 * @param agent agent handle
1199 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1203 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1205 unsigned long long new_bw;
1209 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1210 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1211 new_bw = RIL_MIN_BW;
1212 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1217 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1218 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1219 new_bw = RIL_MIN_BW;
1220 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1226 * Switches to the address given by its index
1228 * @param solver solver handle
1229 * @param agent agent handle
1230 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1233 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1234 struct RIL_Peer_Agent *agent,
1235 unsigned int address_index)
1237 struct RIL_Address_Wrapped *cur;
1240 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1242 if (i == address_index)
1244 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1252 //no address with address_index exists, in this case this action should not be callable
1253 GNUNET_assert(GNUNET_NO);
1257 * Puts the action into effect by calling the according function
1259 * @param solver the solver handle
1260 * @param agent the action handle
1261 * @param action the action to perform by the solver
1264 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1270 case RIL_ACTION_NOTHING:
1272 case RIL_ACTION_BW_IN_DBL:
1273 envi_action_bw_double (solver, agent, GNUNET_YES);
1275 case RIL_ACTION_BW_IN_HLV:
1276 envi_action_bw_halven (solver, agent, GNUNET_YES);
1278 case RIL_ACTION_BW_IN_INC:
1279 envi_action_bw_inc (solver, agent, GNUNET_YES);
1281 case RIL_ACTION_BW_IN_DEC:
1282 envi_action_bw_dec (solver, agent, GNUNET_YES);
1284 case RIL_ACTION_BW_OUT_DBL:
1285 envi_action_bw_double (solver, agent, GNUNET_NO);
1287 case RIL_ACTION_BW_OUT_HLV:
1288 envi_action_bw_halven (solver, agent, GNUNET_NO);
1290 case RIL_ACTION_BW_OUT_INC:
1291 envi_action_bw_inc (solver, agent, GNUNET_NO);
1293 case RIL_ACTION_BW_OUT_DEC:
1294 envi_action_bw_dec (solver, agent, GNUNET_NO);
1297 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1299 address_index = action - RIL_ACTION_TYPE_NUM;
1301 GNUNET_assert(address_index >= 0);
1303 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1305 envi_action_address_switch (solver, agent, address_index);
1308 // error - action does not exist
1309 GNUNET_assert(GNUNET_NO);
1314 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1317 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1318 UINT32_MAX) / (double) UINT32_MAX;
1320 if (r < agent->envi->parameters.explore_ratio) //explore
1322 action = agent_get_action_random(agent);
1323 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1325 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, action);
1331 action = agent_get_action_max(agent, state);
1332 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1334 agent_modify_eligibility(agent, RIL_E_DISCOUNT, NULL, action);
1341 * Selects the next action with a probability corresponding to its value. The
1342 * probability is calculated using a Boltzmann distribution with a temperature
1343 * value. The higher the temperature, the more are the action selection
1344 * probabilities the same. With a temperature of 0, the selection is greedy,
1345 * i.e. always the action with the highest value is chosen.
1351 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1355 double eqt[agent->n];
1360 a_max = agent_get_action_max(agent, state);
1362 for (i=0; i<agent->n; i++)
1364 if (agent_action_is_possible(agent, i))
1366 eqt[i] = exp(agent_estimate_q(agent,state,i) / agent->envi->parameters.temperature);
1370 for (i=0; i<agent->n; i++)
1372 if (agent_action_is_possible(agent, i))
1381 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1382 UINT32_MAX) / (double) UINT32_MAX;
1384 for (i=0; i<agent->n; i++)
1388 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1391 agent_modify_eligibility(agent, RIL_E_DISCOUNT, NULL, i);
1393 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1399 GNUNET_assert(GNUNET_NO);
1403 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1405 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1407 return agent_select_egreedy(agent, state);
1411 return agent_select_softmax(agent, state);
1416 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1417 * after having done the last action a_old. It observes the new state s_next and the reward
1418 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1419 * next action is put into effect.
1421 * @param agent the agent performing the step
1424 agent_step (struct RIL_Peer_Agent *agent)
1426 int a_next = RIL_ACTION_INVALID;
1431 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1432 GNUNET_i2s (&agent->peer),
1433 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1435 s_next = envi_get_state (agent->envi, agent);
1436 reward = envi_get_reward (agent->envi, agent);
1438 switch (agent->envi->parameters.algorithm)
1440 case RIL_ALGO_SARSA:
1441 a_next = agent_select_action (agent, s_next);
1442 if (RIL_ACTION_INVALID != agent->a_old)
1444 //updates weights with selected action (on-policy), if not first step
1445 agent_update_weights (agent, reward, s_next, a_next);
1447 agent_modify_eligibility (agent, RIL_E_DISCOUNT, s_next, a_next);
1451 a_max = agent_get_action_max (agent, s_next);
1452 if (RIL_ACTION_INVALID != agent->a_old)
1454 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1455 agent_update_weights (agent, reward, s_next, a_max);
1457 a_next = agent_select_action (agent, s_next);
1461 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1463 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, s_next, a_next);
1465 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1472 envi_do_action (agent->envi, agent, a_next);
1474 GNUNET_free(agent->s_old);
1475 agent->s_old = s_next;
1476 agent->a_old = a_next;
1478 agent->step_count += 1;
1482 ril_step (struct GAS_RIL_Handle *solver);
1485 * Task for the scheduler, which performs one step and lets the solver know that
1486 * no further step is scheduled.
1488 * @param cls the solver handle
1489 * @param tc the task context for the scheduler
1492 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1494 struct GAS_RIL_Handle *solver = cls;
1496 solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1501 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1504 struct RIL_Scope net;
1505 unsigned long long sum_assigned = 0;
1506 unsigned long long sum_available = 0;
1509 for (i = 0; i < solver->networks_count; i++)
1511 net = solver->network_entries[i];
1512 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1514 sum_assigned += net.bw_in_assigned;
1515 sum_assigned += net.bw_out_assigned;
1516 sum_available += net.bw_in_available;
1517 sum_available += net.bw_out_available;
1520 if (sum_available > 0)
1522 ratio = ((double) sum_assigned) / ((double) sum_available);
1529 return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
1533 * Lookup network struct by type
1535 * @param s the solver handle
1536 * @param type the network type
1537 * @return the network struct
1539 static struct RIL_Scope *
1540 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1544 for (i = 0; i < s->networks_count; i++)
1546 if (s->network_entries[i].type == type)
1548 return &s->network_entries[i];
1555 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1557 struct RIL_Scope *net;
1558 struct RIL_Peer_Agent *agent;
1559 unsigned long long address_count = 0;
1561 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1563 if (agent->address_inuse && agent->is_active)
1565 net = agent->address_inuse->solver_information;
1566 if (net->type == network)
1573 net = ril_get_network (solver, network);
1574 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1578 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1580 struct RIL_Address_Wrapped *addr_wrap;
1581 struct RIL_Scope *net;
1583 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1585 net = addr_wrap->address_naked->solver_information;
1586 if (ril_network_is_not_full(solver, net->type))
1588 if (NULL == agent->address_inuse)
1589 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, RIL_MIN_BW, RIL_MIN_BW, silent);
1593 agent->address_inuse = NULL;
1597 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1599 struct GNUNET_TIME_Absolute time_now;
1600 struct GNUNET_TIME_Relative time_delta;
1603 // MDP case - TODO remove when debugged
1604 if (solver->simulate)
1606 solver->global_discount_variable = solver->parameters.gamma;
1607 solver->global_discount_integrated = 1;
1613 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1614 time_now = GNUNET_TIME_absolute_get ();
1615 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1616 solver->step_time_last = time_now;
1617 tau = (double) time_delta.rel_value_us
1618 / (double) solver->parameters.step_time_min.rel_value_us;
1620 //calculate reward discounts (once per step for all agents)
1621 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1622 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1623 / (double) solver->parameters.beta;
1627 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1630 struct RIL_Peer_Agent *cur_agent;
1632 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1634 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1643 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1646 struct RIL_Scope *net;
1648 for (c = 0; c < solver->networks_count; c++)
1650 net = &solver->network_entries[c];
1651 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1652 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1653 net->agent_count = ril_network_count_active_agents(solver, net);
1654 net->social_welfare = ril_network_get_social_welfare(solver, net);
1659 * Schedules the next global step in an adaptive way. The more resources are
1660 * left, the earlier the next step is scheduled. This serves the reactivity of
1661 * the solver to changed inputs.
1663 * @param solver the solver handle
1666 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1672 struct GNUNET_TIME_Relative time_next;
1674 used_ratio = ril_get_used_resource_ratio (solver);
1677 solver->parameters.step_time_min.rel_value_us
1678 <= solver->parameters.step_time_max.rel_value_us);
1680 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1681 solver->parameters.step_time_min).rel_value_us;
1682 offset = (double) solver->parameters.step_time_min.rel_value_us;
1683 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1685 GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1686 GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1688 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1690 if (solver->simulate)
1692 time_next = GNUNET_TIME_UNIT_ZERO;
1695 if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1697 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1703 * Triggers one step per agent
1708 ril_step (struct GAS_RIL_Handle *solver)
1710 struct RIL_Peer_Agent *cur;
1712 if (GNUNET_YES == solver->bulk_lock)
1714 solver->bulk_changes++;
1718 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1720 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1722 if (0 == solver->step_count)
1724 solver->step_time_last = GNUNET_TIME_absolute_get ();
1727 ril_calculate_discount (solver);
1728 ril_networks_update_state (solver);
1730 //trigger one step per active, unblocked agent
1731 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1735 if (NULL == cur->address_inuse)
1737 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1739 if (cur->address_inuse)
1746 ril_networks_update_state (solver);
1748 solver->step_count += 1;
1749 ril_step_schedule_next (solver);
1751 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1753 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1754 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1756 if (cur->suggestion_issue) {
1757 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1758 cur->suggestion_issue = GNUNET_NO;
1761 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1765 * Initializes the matrix W of parameter vectors theta with small random numbers.
1767 * @param agent The respective agent
1770 agent_w_init (struct RIL_Peer_Agent *agent)
1775 for (i = 0; i < agent->n; i++)
1777 for (k = 0; k < agent->m; k++)
1779 agent->W[i][k] = agent->envi->parameters.alpha * (1.0 - 2.0*((double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX)/(double)UINT32_MAX));
1785 * Initialize an agent without addresses and its knowledge base
1787 * @param s ril solver
1788 * @param peer the one in question
1789 * @return handle to the new agent
1791 static struct RIL_Peer_Agent *
1792 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1795 struct GAS_RIL_Handle * solver = s;
1796 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1798 agent->envi = solver;
1799 agent->peer = *peer;
1800 agent->step_count = 0;
1801 agent->is_active = GNUNET_NO;
1802 agent->bw_in = RIL_MIN_BW;
1803 agent->bw_out = RIL_MIN_BW;
1804 agent->suggestion_issue = GNUNET_NO;
1805 agent->n = RIL_ACTION_TYPE_NUM;
1807 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1808 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1809 for (i = 0; i < agent->n; i++)
1811 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1812 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1814 agent_w_init(agent);
1815 agent->a_old = RIL_ACTION_INVALID;
1816 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1817 agent->address_inuse = NULL;
1818 agent->objective_old = 0;
1826 * @param solver the solver handle
1827 * @param agent the agent to retire
1830 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1834 for (i = 0; i < agent->n; i++)
1836 GNUNET_free_non_null(agent->W[i]);
1837 GNUNET_free_non_null(agent->E[i]);
1839 GNUNET_free_non_null(agent->W);
1840 GNUNET_free_non_null(agent->E);
1841 GNUNET_free_non_null(agent->s_old);
1846 * Returns the agent for a peer
1848 * @param solver the solver handle
1849 * @param peer the identity of the peer
1850 * @param create whether or not to create an agent, if none is allocated yet
1853 static struct RIL_Peer_Agent *
1854 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1856 struct RIL_Peer_Agent *cur;
1858 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1860 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1868 cur = agent_init (solver, peer);
1869 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
1876 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1877 * considered inactive and not used. Addresses in an inactive network are ignored.
1879 * @param solver solver handle
1880 * @param network the network type
1881 * @return whether or not the network is considered active
1884 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1886 struct RIL_Scope *net;
1888 net = ril_get_network (solver, network);
1889 return net->bw_out_available >= RIL_MIN_BW;
1893 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1894 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1895 * frees the memory of the old vector, and redirects the pointer to the new one.
1897 * @param old pointer to the pointer to the first element of the vector
1898 * @param element_size byte size of the vector elements
1899 * @param hole_start the first element to cut out
1900 * @param hole_length the number of elements to cut out
1901 * @param old_length the length of the old vector
1904 ril_cut_from_vector (void **old,
1905 size_t element_size,
1906 unsigned int hole_start,
1907 unsigned int hole_length,
1908 unsigned int old_length)
1911 char *oldptr = (char *) *old;
1913 unsigned int bytes_before;
1914 unsigned int bytes_hole;
1915 unsigned int bytes_after;
1917 GNUNET_assert(old_length >= hole_length);
1918 GNUNET_assert(old_length >= (hole_start + hole_length));
1920 size = element_size * (old_length - hole_length);
1922 bytes_before = element_size * hole_start;
1923 bytes_hole = element_size * hole_length;
1924 bytes_after = element_size * (old_length - hole_start - hole_length);
1932 tmpptr = GNUNET_malloc (size);
1933 memcpy (tmpptr, oldptr, bytes_before);
1934 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1940 *old = (void *) tmpptr;
1944 * Solver API functions
1945 * ---------------------------
1949 * Change relative preference for quality in solver
1951 * @param solver the solver handle
1952 * @param peer the peer to change the preference for
1953 * @param kind the kind to change the preference
1954 * @param pref_rel the normalized preference value for this kind over all clients
1957 GAS_ril_address_change_preference (void *solver,
1958 const struct GNUNET_PeerIdentity *peer,
1959 enum GNUNET_ATS_PreferenceKind kind,
1962 LOG(GNUNET_ERROR_TYPE_DEBUG,
1963 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1964 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1970 * Entry point for the plugin
1972 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1975 libgnunet_plugin_ats_ril_init (void *cls)
1977 struct GNUNET_ATS_PluginEnvironment *env = cls;
1978 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1979 struct RIL_Scope * cur;
1983 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1985 GNUNET_assert(NULL != env);
1986 GNUNET_assert(NULL != env->cfg);
1987 GNUNET_assert(NULL != env->stats);
1988 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1989 GNUNET_assert(NULL != env->get_preferences);
1990 GNUNET_assert(NULL != env->get_property);
1992 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
1994 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
1997 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
1998 &solver->parameters.step_time_min))
2000 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2003 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2004 &solver->parameters.step_time_max))
2006 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2008 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2010 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
2011 GNUNET_free (string);
2015 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2017 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2019 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2020 GNUNET_free (string);
2024 solver->parameters.select = RIL_DEFAULT_SELECT;
2026 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
2028 solver->parameters.beta = strtod (string, NULL);
2029 GNUNET_free (string);
2030 if (!(solver->parameters.beta > 0))
2032 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_DISCOUNT_BETA not configured as positive number. Set to default value of %f instead.\n", RIL_DEFAULT_DISCOUNT_BETA);
2033 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2038 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2040 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
2042 solver->parameters.gamma = strtod (string, NULL);
2043 GNUNET_free (string);
2044 if (!(solver->parameters.gamma < 1) || (solver->parameters.gamma < 0))
2046 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_DISCOUNT_GAMMA not configured in range [0,1[. Set to default value of %f instead.\n", RIL_DEFAULT_DISCOUNT_GAMMA);
2047 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2052 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2055 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
2057 solver->parameters.alpha = strtod (string, NULL);
2058 GNUNET_free (string);
2059 if (!(solver->parameters.alpha > 0) || solver->parameters.alpha > 1)
2061 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_GRADIENT_STEP_SIZE not configured in range ]0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_GRADIENT_STEP_SIZE);
2062 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2067 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2069 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
2071 solver->parameters.lambda = strtod (string, NULL);
2072 GNUNET_free (string);
2073 if (solver->parameters.lambda < 0 || solver->parameters.lambda > 1)
2075 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TRACE_DECAY not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_TRACE_DECAY);
2076 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2081 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2083 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
2085 solver->parameters.explore_ratio = strtod (string, NULL);
2086 GNUNET_free (string);
2087 if (solver->parameters.explore_ratio < 0 || solver->parameters.explore_ratio > 1)
2089 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_EXPLORE_RATIO not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_EXPLORE_RATIO);
2090 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
2095 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
2097 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
2099 solver->parameters.reward_global_share = strtod (string, NULL);
2100 GNUNET_free (string);
2101 if (solver->parameters.reward_global_share < 0 || solver->parameters.reward_global_share > 1)
2103 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_GLOBAL_REWARD_SHARE not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_GLOBAL_REWARD_SHARE);
2104 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
2109 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
2111 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE", &string))
2113 solver->parameters.temperature = strtod (string, NULL);
2114 GNUNET_free (string);
2115 if (solver->parameters.temperature <= 0)
2117 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE not positive. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE);
2118 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
2123 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
2125 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2127 solver->simulate = GNUNET_NO;
2129 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2131 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2135 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2137 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2139 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2140 GNUNET_free (string);
2144 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2147 env->sf.s_add = &GAS_ril_address_add;
2148 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
2149 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
2150 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
2151 env->sf.s_address_update_network = &GAS_ril_address_change_network;
2152 env->sf.s_get = &GAS_ril_get_preferred_address;
2153 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2154 env->sf.s_pref = &GAS_ril_address_change_preference;
2155 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
2156 env->sf.s_del = &GAS_ril_address_delete;
2157 env->sf.s_bulk_start = &GAS_ril_bulk_start;
2158 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
2160 solver->plugin_envi = env;
2161 solver->networks_count = env->network_count;
2162 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2163 solver->step_count = 0;
2164 solver->done = GNUNET_NO;
2166 for (c = 0; c < env->network_count; c++)
2168 cur = &solver->network_entries[c];
2169 cur->type = env->networks[c];
2170 cur->bw_in_available = env->in_quota[c];
2171 cur->bw_out_available = env->out_quota[c];
2172 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Quotas for %s network: IN %llu - OUT %llu\n", GNUNET_ATS_print_network_type(cur->type), cur->bw_in_available/1024, cur->bw_out_available/1024);
2175 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2176 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2177 solver->parameters.algorithm ? "Q" : "SARSA",
2178 solver->parameters.alpha,
2179 solver->parameters.beta,
2180 solver->parameters.lambda);
2181 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s, global_share = %f\n",
2182 solver->parameters.explore_ratio,
2183 solver->parameters.temperature,
2184 solver->parameters.select ? "EGREEDY" : "SOFTMAX",
2185 solver->parameters.reward_global_share);
2186 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2187 solver->parameters.rbf_divisor);
2193 * Exit point for the plugin
2195 * @param cls the solver handle
2198 libgnunet_plugin_ats_ril_done (void *cls)
2200 struct GAS_RIL_Handle *s = cls;
2201 struct RIL_Peer_Agent *cur_agent;
2202 struct RIL_Peer_Agent *next_agent;
2204 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2206 s->done = GNUNET_YES;
2208 cur_agent = s->agents_head;
2209 while (NULL != cur_agent)
2211 next_agent = cur_agent->next;
2212 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2213 agent_die (s, cur_agent);
2214 cur_agent = next_agent;
2217 if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
2219 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2221 GNUNET_free(s->network_entries);
2228 * Add a new address for a peer to the solver
2230 * The address is already contained in the addresses hashmap!
2232 * @param solver the solver Handle
2233 * @param address the address to add
2234 * @param network network type of this address
2237 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
2239 struct GAS_RIL_Handle *s = solver;
2240 struct RIL_Peer_Agent *agent;
2241 struct RIL_Address_Wrapped *address_wrapped;
2242 struct RIL_Scope *net;
2250 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
2252 net = ril_get_network (s, network);
2253 address->solver_information = net;
2255 if (!ril_network_is_active (s, network))
2257 LOG(GNUNET_ERROR_TYPE_DEBUG,
2258 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2259 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2263 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2266 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2267 address_wrapped->address_naked = address;
2268 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2270 //increase size of W
2271 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2273 n_new = agent->n + 1;
2276 GNUNET_array_grow(agent->W, agent->n, n_new);
2278 GNUNET_array_grow(agent->E, agent->n, n_new);
2279 for (i = 0; i < n_new; i++)
2284 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2286 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2291 GNUNET_array_grow(agent->W[i], zero, m_new);
2293 GNUNET_array_grow(agent->E[i], zero, m_new);
2297 //increase size of old state vector
2299 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2301 ril_try_unblock_agent(s, agent, GNUNET_NO);
2305 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2306 address->active ? "active" : "inactive", address->plugin, address->addr,
2307 GNUNET_i2s (&address->peer));
2311 * Delete an address in the solver
2313 * The address is not contained in the address hashmap anymore!
2315 * @param solver the solver handle
2316 * @param address the address to remove
2317 * @param session_only delete only session not whole address
2320 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
2322 struct GAS_RIL_Handle *s = solver;
2323 struct RIL_Peer_Agent *agent;
2324 struct RIL_Address_Wrapped *address_wrapped;
2325 int address_was_used = address->active;
2330 struct RIL_Scope *net;
2332 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2333 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2334 address->addr, GNUNET_i2s (&address->peer));
2336 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2339 net = address->solver_information;
2340 GNUNET_assert(!ril_network_is_active (s, net->type));
2341 LOG(GNUNET_ERROR_TYPE_DEBUG,
2342 "No agent allocated for peer yet, since address was in inactive network\n");
2346 address_index = agent_address_get_index (agent, address);
2347 address_wrapped = agent_address_get (agent, address);
2349 if (NULL == address_wrapped)
2351 net = address->solver_information;
2352 GNUNET_assert(!ril_network_is_active (s, net->type));
2353 LOG(GNUNET_ERROR_TYPE_DEBUG,
2354 "Address not considered by agent, address was in inactive network\n");
2358 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2359 GNUNET_free(address_wrapped);
2362 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2363 n_new = agent->n - 1;
2365 for (i = 0; i < agent->n; i++)
2367 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2368 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2369 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2370 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2371 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2372 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2374 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2375 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2376 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2378 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2380 //correct last action
2381 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2385 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2387 agent->a_old = RIL_ACTION_INVALID;
2389 //decrease old state vector
2390 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2391 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2392 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2396 if (address_was_used)
2398 if (NULL != agent->addresses_head) //if peer has an address left, use it
2400 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, RIL_MIN_BW, RIL_MIN_BW,
2405 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2413 * Update the properties of an address in the solver
2415 * @param solver solver handle
2416 * @param address the address
2417 * @param type the ATSI type in HBO
2418 * @param abs_value the absolute value of the property
2419 * @param rel_value the normalized value
2422 GAS_ril_address_property_changed (void *solver,
2423 struct ATS_Address *address,
2428 LOG(GNUNET_ERROR_TYPE_DEBUG,
2429 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2430 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2431 address->addr, rel_value);
2437 * Update the session of an address in the solver
2439 * NOTE: values in addresses are already updated
2441 * @param solver solver handle
2442 * @param address the address
2443 * @param cur_session the current session
2444 * @param new_session the new session
2447 GAS_ril_address_session_changed (void *solver,
2448 struct ATS_Address *address,
2449 uint32_t cur_session,
2450 uint32_t new_session)
2452 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2456 * Notify the solver that an address is (not) actively used by transport
2457 * to communicate with a remote peer
2459 * NOTE: values in addresses are already updated
2461 * @param solver solver handle
2462 * @param address the address
2463 * @param in_use usage state
2466 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2468 LOG(GNUNET_ERROR_TYPE_DEBUG,
2469 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2470 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2474 * Notify solver that the network an address is located in has changed
2476 * NOTE: values in addresses are already updated
2478 * @param solver solver handle
2479 * @param address the address
2480 * @param current_network the current network
2481 * @param new_network the new network
2484 GAS_ril_address_change_network (void *solver,
2485 struct ATS_Address *address,
2486 uint32_t current_network,
2487 uint32_t new_network)
2489 struct GAS_RIL_Handle *s = solver;
2490 struct RIL_Peer_Agent *agent;
2492 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2493 "%s address of peer %s from '%s' to '%s'\n",
2494 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2495 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2497 if (address->active && !ril_network_is_active (solver, new_network))
2499 GAS_ril_address_delete (solver, address, GNUNET_NO);
2503 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2506 GNUNET_assert(!ril_network_is_active (solver, current_network));
2508 GAS_ril_address_add (s, address, new_network);
2512 address->solver_information = ril_get_network(solver, new_network);
2516 * Give feedback about the current assignment
2518 * @param solver the solver handle
2519 * @param application the application
2520 * @param peer the peer to change the preference for
2521 * @param scope the time interval for this feedback: [now - scope .. now]
2522 * @param kind the kind to change the preference
2523 * @param score the score
2526 GAS_ril_address_preference_feedback (void *solver,
2528 const struct GNUNET_PeerIdentity *peer,
2529 const struct GNUNET_TIME_Relative scope,
2530 enum GNUNET_ATS_PreferenceKind kind,
2533 LOG(GNUNET_ERROR_TYPE_DEBUG,
2534 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2535 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2536 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2540 * Start a bulk operation
2542 * @param solver the solver
2545 GAS_ril_bulk_start (void *solver)
2547 struct GAS_RIL_Handle *s = solver;
2549 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2555 * Bulk operation done
2557 * @param solver the solver handle
2560 GAS_ril_bulk_stop (void *solver)
2562 struct GAS_RIL_Handle *s = solver;
2564 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2566 if (s->bulk_lock < 1)
2573 if (0 < s->bulk_changes)
2576 s->bulk_changes = 0;
2581 * Tell solver to notify ATS if the address to use changes for a specific
2582 * peer using the bandwidth changed callback
2584 * The solver must only notify about changes for peers with pending address
2587 * @param solver the solver handle
2588 * @param peer the identity of the peer
2590 const struct ATS_Address *
2591 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2594 * activate agent, return currently chosen address
2596 struct GAS_RIL_Handle *s = solver;
2597 struct RIL_Peer_Agent *agent;
2599 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2601 agent = ril_get_agent (s, peer, GNUNET_YES);
2603 agent->is_active = GNUNET_YES;
2604 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2606 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2608 if (agent->address_inuse)
2610 LOG(GNUNET_ERROR_TYPE_DEBUG,
2611 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2612 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2616 LOG(GNUNET_ERROR_TYPE_DEBUG,
2617 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2621 return agent->address_inuse;
2625 * Tell solver stop notifying ATS about changes for this peers
2627 * The solver must only notify about changes for peers with pending address
2630 * @param solver the solver handle
2631 * @param peer the peer
2634 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2636 struct GAS_RIL_Handle *s = solver;
2637 struct RIL_Peer_Agent *agent;
2639 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2641 agent = ril_get_agent (s, peer, GNUNET_NO);
2648 if (GNUNET_NO == agent->is_active)
2654 agent->is_active = GNUNET_NO;
2656 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2661 LOG(GNUNET_ERROR_TYPE_DEBUG,
2662 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2663 GNUNET_i2s (peer), agent->address_inuse->plugin);
2666 /* end of plugin_ats_ril.c */