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 1024 * 250 //GNUNET_ATS_MaxBandwidth
34 #define RIL_ACTION_INVALID -1
35 #define RIL_INTERVAL_EXPONENT 10
36 #define RIL_UTILITY_DELAY_MAX 100
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 = 3,
77 RIL_ACTION_BW_OUT_DEC = 4,
78 RIL_ACTION_TYPE_NUM = 5
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
267 * Whether to reset the eligibility traces to 0 after a Q-exploration step
269 int eligibility_reset;
274 struct ATS_Address * address_inuse;
277 * Head of addresses DLL
279 struct RIL_Address_Wrapped * addresses_head;
282 * Tail of addresses DLL
284 struct RIL_Address_Wrapped * addresses_tail;
287 * Inbound bandwidth assigned by the agent
289 unsigned long long bw_in;
292 * Outbound bandwidth assigned by the agent
294 unsigned long long bw_out;
297 * Flag whether a suggestion has to be issued
299 int suggestion_issue;
302 * The address which has to be issued
304 struct ATS_Address * suggestion_address;
307 * The agent's last objective value
309 double objective_old;
322 enum GNUNET_ATS_Network_Type type;
325 * Total available inbound bandwidth
327 unsigned long long bw_in_available;
330 * Bandwidth inbound assigned in network after last step
332 unsigned long long bw_in_assigned;
335 * Bandwidth inbound actually utilized in the network
337 unsigned long long bw_in_utilized;
340 * Total available outbound bandwidth
342 unsigned long long bw_out_available;
345 * Bandwidth outbound assigned in network after last step
347 unsigned long long bw_out_assigned;
350 * Bandwidth outbound actually utilized in the network
352 unsigned long long bw_out_utilized;
355 * Number of active agents in scope
357 unsigned int agent_count;
360 * The social welfare achieved in the scope
362 double social_welfare;
366 * A handle for the reinforcement learning solver
368 struct GAS_RIL_Handle
371 * The solver-plugin environment of the solver-plugin API
373 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
378 struct GNUNET_STATISTICS_Handle *stats;
381 * Number of performed steps
383 unsigned long long step_count;
386 * Timestamp for the last time-step
388 struct GNUNET_TIME_Absolute step_time_last;
391 * Task identifier of the next time-step to be executed
393 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
396 * Variable discount factor, dependent on time between steps
398 double global_discount_variable;
401 * Integrated variable discount factor, dependent on time between steps
403 double global_discount_integrated;
406 * Lock for bulk operations
411 * Number of changes during a lock
416 * Learning parameters
418 struct RIL_Learning_Parameters parameters;
421 * Array of networks with global assignment state
423 struct RIL_Scope * network_entries;
428 unsigned int networks_count;
431 * List of active peer-agents
433 struct RIL_Peer_Agent * agents_head;
434 struct RIL_Peer_Agent * agents_tail;
442 * Simulate steps, i.e. schedule steps immediately
444 unsigned long long simulate;
448 * "Private" functions
449 * ---------------------------
453 * Estimate the current action-value for state s and action a
455 * @param agent agent performing the estimation
458 * @return estimation value
461 agent_q (struct RIL_Peer_Agent *agent, double *state, int action)
466 for (i = 0; i < agent->m; i++)
468 result += state[i] * agent->W[action][i];
471 GNUNET_assert(!isnan(result));
473 //prevent crash when learning diverges
476 return isinf(result) * UINT32_MAX;
483 * Get the index of the address in the agent's list.
485 * @param agent agent handle
486 * @param address address handle
487 * @return the index, starting with zero
490 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
493 struct RIL_Address_Wrapped *cur;
496 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
499 if (cur->address_naked == address)
507 * Gets the wrapped address from the agent's list
509 * @param agent agent handle
510 * @param address address handle
511 * @return wrapped address
513 static struct RIL_Address_Wrapped *
514 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
516 struct RIL_Address_Wrapped *cur;
518 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
519 if (cur->address_naked == address)
526 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
532 case RIL_ACTION_NOTHING:
535 case RIL_ACTION_BW_IN_INC:
536 case RIL_ACTION_BW_IN_DBL:
537 if (agent->bw_in >= RIL_MAX_BW)
542 case RIL_ACTION_BW_IN_DEC:
543 case RIL_ACTION_BW_IN_HLV:
544 if (agent->bw_in <= RIL_MIN_BW)
549 case RIL_ACTION_BW_OUT_INC:
550 case RIL_ACTION_BW_OUT_DBL:
551 if (agent->bw_out >= RIL_MAX_BW)
556 case RIL_ACTION_BW_OUT_DEC:
557 case RIL_ACTION_BW_OUT_HLV:
558 if (agent->bw_out <= RIL_MIN_BW)
564 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
566 address_index = action - RIL_ACTION_TYPE_NUM;
568 GNUNET_assert(address_index >= 0);
570 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
572 if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
573 agent->address_inuse->active)
579 // error - action does not exist
580 GNUNET_assert(GNUNET_NO);
586 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
587 * most reward in the future)
589 * @param agent agent performing the calculation
590 * @param state the state from which to take the action
591 * @return the action promising most future reward
594 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
597 int max_i = RIL_ACTION_INVALID;
599 double max_q = -DBL_MAX;
601 for (i = 0; i < agent->n; i++)
603 if (agent_action_is_possible(agent, i))
605 cur_q = agent_q (agent, state, i);
614 GNUNET_assert(RIL_ACTION_INVALID != max_i);
620 * Chooses a random action from the set of possible ones
622 * @param agent the agent performing the action
623 * @return the action index
626 agent_get_action_random (struct RIL_Peer_Agent *agent)
629 int is_possible[agent->n];
633 for (i = 0; i<agent->n; i++)
635 if (agent_action_is_possible(agent, i))
637 is_possible[i] = GNUNET_YES;
642 is_possible[i] = GNUNET_NO;
646 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
649 for (i = 0; i<agent->n; i++)
659 GNUNET_assert(GNUNET_NO);
664 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
666 * @param agent the agent performing the update
667 * @param reward the reward received for the last action
668 * @param s_next the new state, the last step got the agent into
669 * @param a_prime the new
672 agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
676 double *theta = agent->W[agent->a_old];
678 delta = agent->envi->global_discount_integrated * reward; //reward
679 delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
680 delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
682 // 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",
683 // agent->step_count,
684 // agent_q (agent, agent->s_old, agent->a_old),
685 // agent->envi->parameters.alpha,
687 // agent->envi->global_discount_variable,
688 // agent_q (agent, s_next, a_prime),
691 for (i = 0; i < agent->m; i++)
693 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
694 // agent->envi->parameters.alpha,
698 theta[i] += agent->envi->parameters.alpha * delta * agent->E[agent->a_old][i];
704 * Changes the eligibility trace vector e in various manners:
705 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
706 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
707 * #RIL_E_DISCOUNT - multiplies e with discount factor and lambda as in the update rule
708 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
710 * @param agent the agent handle
711 * @param mod the kind of modification
712 * @param feature the feature vector
715 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
716 enum RIL_E_Modification mod,
723 for (i = 0; i < agent->m; i++)
727 case RIL_E_ACCUMULATE:
728 agent->E[action][i] += feature[i];
731 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];
734 for (k = 0; k < agent->n; k++)
736 agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
740 for (k = 0; k < agent->n; k++)
750 * Informs the environment about the status of the solver
757 ril_inform (struct GAS_RIL_Handle *solver,
758 enum GAS_Solver_Operation op,
759 enum GAS_Solver_Status stat)
761 if (NULL != solver->plugin_envi->info_cb)
762 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
767 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
768 * notify ATS of its new decision
770 * @param solver solver handle
771 * @param agent agent handle
772 * @param new_address the address which is to be used
773 * @param new_bw_in the new amount of inbound bandwidth set for this address
774 * @param new_bw_out the new amount of outbound bandwidth set for this address
775 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
778 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
779 struct RIL_Peer_Agent *agent,
780 struct ATS_Address *new_address,
781 unsigned long long new_bw_in,
782 unsigned long long new_bw_out,
785 int notify = GNUNET_NO;
787 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
790 if (agent->address_inuse != new_address)
792 if (NULL != agent->address_inuse)
794 agent->address_inuse->active = GNUNET_NO;
795 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
796 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
798 if (NULL != new_address)
800 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
801 new_address->active = agent->is_active;
802 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
803 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
805 notify |= GNUNET_YES;
811 if (new_address->active != agent->is_active)
813 new_address->active = agent->is_active;
814 notify |= GNUNET_YES;
818 if (agent->bw_in != new_bw_in)
820 agent->bw_in = new_bw_in;
821 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
822 notify |= GNUNET_YES;
824 if (agent->bw_out != new_bw_out)
826 agent->bw_out = new_bw_out;
827 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
828 notify |= GNUNET_YES;
832 if (notify && agent->is_active && (GNUNET_NO == silent))
836 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
837 agent->suggestion_issue = GNUNET_YES;
838 agent->suggestion_address = new_address;
840 else if (agent->address_inuse)
842 //disconnect case, no new address
843 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
844 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
848 agent->suggestion_issue = GNUNET_YES;
849 agent->suggestion_address = agent->address_inuse;
852 agent->address_inuse = new_address;
857 * Allocates a state vector and fills it with the features present
858 * @param solver the solver handle
859 * @param agent the agent handle
860 * @return pointer to the state vector
863 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
874 unsigned long long max_bw;
876 state = GNUNET_malloc (sizeof(double) * agent->m);
880 y[0] = (double) agent->bw_out;
881 y[1] = (double) agent->bw_in;
883 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
884 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
886 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
888 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
889 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
892 sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
893 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
902 * Retrieves an ATS information value of an address
904 * @param address the address in question
905 * @param type the ATS information type
909 ril_get_atsi (struct ATS_Address *address, uint32_t type)
912 GNUNET_assert(NULL != address);
914 if ((NULL == address->atsi) || (0 == address->atsi_count))
915 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
917 for (c1 = 0; c1 < address->atsi_count; c1++)
919 if (ntohl (address->atsi[c1].type) == type)
920 return ntohl (address->atsi[c1].value);
922 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
926 * Returns the utility value of the connection an agent manages
928 * @param agent the agent in question
929 * @return the utility value
932 agent_get_utility (struct RIL_Peer_Agent *agent)
934 const double *preferences;
939 preferences = agent->envi->plugin_envi->get_preferences (agent->envi->plugin_envi->get_preference_cls,
942 delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
943 delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
945 pref_match = (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm);
946 pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * (double) (agent->bw_in/RIL_MIN_BW));
947 pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * (double) (agent->bw_out/RIL_MIN_BW));
949 // return (double) (agent->bw_in/RIL_MIN_BW);
950 // return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
955 * Calculates the social welfare within a network scope according to what social
956 * welfare measure is set in the configuration.
958 * @param solver the solver handle
959 * @param scope the network scope in question
960 * @return the social welfare value
963 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
965 struct RIL_Peer_Agent *cur;
968 switch (solver->parameters.social_welfare)
970 case RIL_WELFARE_EGALITARIAN:
972 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
974 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
976 result = GNUNET_MIN(result, agent_get_utility(cur));
981 case RIL_WELFARE_NASH:
983 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
985 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
987 result *= agent_get_utility(cur);
990 return pow(result, 1.0 / (double) scope->agent_count);
992 GNUNET_assert(GNUNET_NO);
997 * Gets the reward for the last performed step, which is calculated in equal
998 * parts from the local (the peer specific) and the global (for all peers
1001 * @param solver the solver handle
1002 * @param agent the agent handle
1003 * @return the reward
1006 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1008 struct RIL_Scope *net;
1009 unsigned long long overutilization;
1010 unsigned long long over_in = 0;
1011 unsigned long long over_out = 0;
1016 net = agent->address_inuse->solver_information;
1018 //TODO make sure in tests to have utilization property updated
1019 if (net->bw_in_assigned > net->bw_in_available)
1020 over_in = net->bw_in_assigned - net->bw_in_available;
1021 if (net->bw_out_assigned > net->bw_out_available)
1022 over_out = net->bw_out_assigned - net->bw_out_available;
1023 overutilization = GNUNET_MAX(over_in, over_out) / RIL_MIN_BW;
1025 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1026 delta = objective - agent->objective_old;
1027 agent->objective_old = objective;
1031 agent->nop_bonus = delta * 0.5;
1034 LOG(GNUNET_ERROR_TYPE_DEBUG, "agent->nop_bonus: %f\n", agent->nop_bonus);
1036 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1038 if (0 != overutilization)
1040 return -1.0 * (double) overutilization;
1044 return delta + steady;
1049 * Doubles the bandwidth for the active address
1051 * @param solver solver handle
1052 * @param agent agent handle
1053 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1056 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1057 struct RIL_Peer_Agent *agent,
1060 unsigned long long new_bw;
1064 new_bw = agent->bw_in * 2;
1065 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1066 new_bw = RIL_MAX_BW;
1067 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1068 agent->bw_out, GNUNET_NO);
1072 new_bw = agent->bw_out * 2;
1073 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1074 new_bw = RIL_MAX_BW;
1075 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1081 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1082 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1084 * @param solver solver handle
1085 * @param agent agent handle
1086 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1090 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1091 struct RIL_Peer_Agent *agent,
1094 unsigned long long new_bw;
1098 new_bw = agent->bw_in / 2;
1099 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1100 new_bw = RIL_MIN_BW;
1101 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1106 new_bw = agent->bw_out / 2;
1107 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1108 new_bw = RIL_MIN_BW;
1109 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1115 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1117 * @param solver solver handle
1118 * @param agent agent handle
1119 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1123 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1125 unsigned long long new_bw;
1129 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1130 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1131 new_bw = RIL_MAX_BW;
1132 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1133 agent->bw_out, GNUNET_NO);
1137 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1138 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1139 new_bw = RIL_MAX_BW;
1140 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1146 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1147 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1149 * @param solver solver handle
1150 * @param agent agent handle
1151 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1155 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1157 unsigned long long new_bw;
1161 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1162 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1163 new_bw = RIL_MIN_BW;
1164 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1169 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1170 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1171 new_bw = RIL_MIN_BW;
1172 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1178 * Switches to the address given by its index
1180 * @param solver solver handle
1181 * @param agent agent handle
1182 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1185 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1186 struct RIL_Peer_Agent *agent,
1187 unsigned int address_index)
1189 struct RIL_Address_Wrapped *cur;
1192 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1194 if (i == address_index)
1196 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1204 //no address with address_index exists, in this case this action should not be callable
1205 GNUNET_assert(GNUNET_NO);
1209 * Puts the action into effect by calling the according function
1211 * @param solver the solver handle
1212 * @param agent the action handle
1213 * @param action the action to perform by the solver
1216 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1222 case RIL_ACTION_NOTHING:
1224 case RIL_ACTION_BW_IN_DBL:
1225 envi_action_bw_double (solver, agent, GNUNET_YES);
1227 case RIL_ACTION_BW_IN_HLV:
1228 envi_action_bw_halven (solver, agent, GNUNET_YES);
1230 case RIL_ACTION_BW_IN_INC:
1231 envi_action_bw_inc (solver, agent, GNUNET_YES);
1233 case RIL_ACTION_BW_IN_DEC:
1234 envi_action_bw_dec (solver, agent, GNUNET_YES);
1236 case RIL_ACTION_BW_OUT_DBL:
1237 envi_action_bw_double (solver, agent, GNUNET_NO);
1239 case RIL_ACTION_BW_OUT_HLV:
1240 envi_action_bw_halven (solver, agent, GNUNET_NO);
1242 case RIL_ACTION_BW_OUT_INC:
1243 envi_action_bw_inc (solver, agent, GNUNET_NO);
1245 case RIL_ACTION_BW_OUT_DEC:
1246 envi_action_bw_dec (solver, agent, GNUNET_NO);
1249 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1251 address_index = action - RIL_ACTION_TYPE_NUM;
1253 GNUNET_assert(address_index >= 0);
1255 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1257 envi_action_address_switch (solver, agent, address_index);
1260 // error - action does not exist
1261 GNUNET_assert(GNUNET_NO);
1266 * Selects the next action using the e-greedy strategy. I.e. with a probability
1267 * of (1-e) the action with the maximum expected return will be chosen
1268 * (=> exploitation) and with probability (e) a random action will be chosen.
1269 * In case the Q-learning rule is set, the function also resets the eligibility
1270 * traces in the exploration case (after Watkin's Q-learning).
1272 * @param agent the agent selecting an action
1273 * @param state the current state-feature vector
1274 * @return the action index
1277 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1280 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1281 UINT32_MAX) / (double) UINT32_MAX;
1283 if (r < agent->envi->parameters.explore_ratio) //explore
1285 action = agent_get_action_random(agent);
1286 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1288 agent->eligibility_reset = GNUNET_YES;
1294 action = agent_get_action_max(agent, state);
1300 * Selects the next action with a probability corresponding to its value. The
1301 * probability is calculated using a Boltzmann distribution with a temperature
1302 * value. The higher the temperature, the more are the action selection
1303 * probabilities the same. With a temperature of 0, the selection is greedy,
1304 * i.e. always the action with the highest value is chosen.
1310 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1314 double eqt[agent->n];
1319 a_max = agent_get_action_max(agent, state);
1321 for (i=0; i<agent->n; i++)
1323 if (agent_action_is_possible(agent, i))
1325 eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1329 for (i=0; i<agent->n; i++)
1331 if (agent_action_is_possible(agent, i))
1340 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1341 UINT32_MAX) / (double) UINT32_MAX;
1343 for (i=0; i<agent->n; i++)
1347 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1350 agent->eligibility_reset = GNUNET_YES;
1356 GNUNET_assert(GNUNET_NO);
1360 * Select the next action of an agent either according to the e-greedy strategy
1361 * or the softmax strategy.
1363 * @param agent the agent in question
1364 * @param state the current state-feature vector
1365 * @return the action index
1368 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1370 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1372 return agent_select_egreedy(agent, state);
1376 return agent_select_softmax(agent, state);
1381 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1382 * after having done the last action a_old. It observes the new state s_next and the reward
1383 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1384 * next action is put into effect.
1386 * @param agent the agent performing the step
1389 agent_step (struct RIL_Peer_Agent *agent)
1391 int a_next = RIL_ACTION_INVALID;
1396 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1397 GNUNET_i2s (&agent->peer),
1398 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1400 s_next = envi_get_state (agent->envi, agent);
1401 reward = envi_get_reward (agent->envi, agent);
1403 if (agent->eligibility_reset)
1405 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1406 agent->eligibility_reset = GNUNET_NO;
1410 agent_modify_eligibility (agent, RIL_E_DISCOUNT, NULL, -1);
1412 if (RIL_ACTION_INVALID != agent->a_old)
1414 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1417 switch (agent->envi->parameters.algorithm)
1419 case RIL_ALGO_SARSA:
1420 a_next = agent_select_action (agent, s_next);
1421 if (RIL_ACTION_INVALID != agent->a_old)
1423 //updates weights with selected action (on-policy), if not first step
1424 agent_update (agent, reward, s_next, a_next);
1429 a_max = agent_get_action_max (agent, s_next);
1430 if (RIL_ACTION_INVALID != agent->a_old)
1432 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1433 agent_update (agent, reward, s_next, a_max);
1435 a_next = agent_select_action (agent, s_next);
1439 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1441 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1448 envi_do_action (agent->envi, agent, a_next);
1450 GNUNET_free(agent->s_old);
1451 agent->s_old = s_next;
1452 agent->a_old = a_next;
1454 agent->step_count += 1;
1458 * Prototype of the ril_step() procedure
1460 * @param solver the solver handle
1463 ril_step (struct GAS_RIL_Handle *solver);
1466 * Task for the scheduler, which performs one step and lets the solver know that
1467 * no further step is scheduled.
1469 * @param cls the solver handle
1470 * @param tc the task context for the scheduler
1473 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1475 struct GAS_RIL_Handle *solver = cls;
1477 solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1482 * Determines how much of the available bandwidth is assigned. If more is
1483 * assigned than available it returns 1. The function is used to determine the
1484 * step size of the adaptive stepping.
1486 * @param solver the solver handle
1490 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1493 struct RIL_Scope net;
1494 unsigned long long sum_assigned = 0;
1495 unsigned long long sum_available = 0;
1498 for (i = 0; i < solver->networks_count; i++)
1500 net = solver->network_entries[i];
1501 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1503 sum_assigned += net.bw_in_assigned;
1504 sum_assigned += net.bw_out_assigned;
1505 sum_available += net.bw_in_available;
1506 sum_available += net.bw_out_available;
1509 if (sum_available > 0)
1511 ratio = ((double) sum_assigned) / ((double) sum_available);
1518 return ratio > 1 ? 1 : ratio; //overassignment is possible, cap at 1
1522 * Lookup network struct by type
1524 * @param s the solver handle
1525 * @param type the network type
1526 * @return the network struct
1528 static struct RIL_Scope *
1529 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1533 for (i = 0; i < s->networks_count; i++)
1535 if (s->network_entries[i].type == type)
1537 return &s->network_entries[i];
1544 * Determines whether more connections are allocated in a network scope, than
1545 * they would theoretically fit. This is used as a heuristic to determine,
1546 * whether a new connection can be allocated or not.
1548 * @param solver the solver handle
1549 * @param network the network scope in question
1550 * @return GNUNET_YES if there are theoretically enough resources left
1553 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1555 struct RIL_Scope *net;
1556 struct RIL_Peer_Agent *agent;
1557 unsigned long long address_count = 0;
1559 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1561 if (agent->address_inuse && agent->is_active)
1563 net = agent->address_inuse->solver_information;
1564 if (net->type == network)
1571 net = ril_get_network (solver, network);
1572 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1576 * Unblocks an agent for which a connection request is there, that could not
1577 * be satisfied. Iterates over the addresses of the agent, if one of its
1578 * addresses can now be allocated in its scope the agent is unblocked,
1579 * otherwise it remains unchanged.
1581 * @param solver the solver handle
1582 * @param agent the agent in question
1586 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1588 struct RIL_Address_Wrapped *addr_wrap;
1589 struct RIL_Scope *net;
1591 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1593 net = addr_wrap->address_naked->solver_information;
1594 if (ril_network_is_not_full(solver, net->type))
1596 if (NULL == agent->address_inuse)
1597 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, RIL_MIN_BW, RIL_MIN_BW, silent);
1601 agent->address_inuse = NULL;
1605 * Determines how much the reward needs to be discounted depending on the amount
1606 * of time, which has passed since the last time-step.
1608 * @param solver the solver handle
1611 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1613 struct GNUNET_TIME_Absolute time_now;
1614 struct GNUNET_TIME_Relative time_delta;
1617 // MDP case - TODO remove when debugged
1618 if (solver->simulate)
1620 solver->global_discount_variable = solver->parameters.gamma;
1621 solver->global_discount_integrated = 1;
1627 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1628 time_now = GNUNET_TIME_absolute_get ();
1629 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1630 solver->step_time_last = time_now;
1631 tau = (double) time_delta.rel_value_us
1632 / (double) solver->parameters.step_time_min.rel_value_us;
1634 //calculate reward discounts (once per step for all agents)
1635 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1636 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1637 / (double) solver->parameters.beta;
1641 * Count the number of active agents/connections in a network scope
1643 * @param solver the solver handle
1644 * @param scope the network scope in question
1645 * @return the number of allocated connections
1648 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1651 struct RIL_Peer_Agent *cur_agent;
1653 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1655 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1664 * Calculates how much bandwidth is assigned in sum in a network scope, either
1665 * in the inbound or in the outbound direction.
1667 * @param solver the solver handle
1668 * @param type the type of the network scope in question
1669 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1670 * otherwise the outbound direction will be summed up
1671 * @return the sum of the assigned bandwidths
1673 static unsigned long long
1674 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1676 struct RIL_Peer_Agent *cur;
1677 struct RIL_Scope *net;
1678 unsigned long long sum = 0;
1680 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1682 if (cur->is_active && cur->address_inuse)
1684 net = cur->address_inuse->solver_information;
1685 if (net->type == type)
1699 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1700 * either in the inbound or in the outbound direction.
1702 * @param solver the solver handle
1703 * @param type the type of the network scope in question
1704 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1705 * otherwise the outbound direction will be summed up
1706 * @return the sum of the utilized bandwidths (in bytes/second)
1708 static unsigned long long
1709 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1711 struct RIL_Peer_Agent *cur;
1712 struct RIL_Scope *net;
1713 unsigned long long sum = 0;
1715 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1717 if (cur->is_active && cur->address_inuse)
1719 net = cur->address_inuse->solver_information;
1720 if (net->type == type)
1723 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
1725 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
1734 * Retrieves the state of the network scope, so that its attributes are up-to-
1737 * @param solver the solver handle
1740 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1743 struct RIL_Scope *net;
1745 for (c = 0; c < solver->networks_count; c++)
1747 net = &solver->network_entries[c];
1748 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1749 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1750 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1751 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1752 net->agent_count = ril_network_count_active_agents(solver, net);
1753 net->social_welfare = ril_network_get_social_welfare(solver, net);
1758 * Schedules the next global step in an adaptive way. The more resources are
1759 * left, the earlier the next step is scheduled. This serves the reactivity of
1760 * the solver to changed inputs.
1762 * @param solver the solver handle
1765 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1771 struct GNUNET_TIME_Relative time_next;
1773 used_ratio = ril_get_used_resource_ratio (solver);
1776 solver->parameters.step_time_min.rel_value_us
1777 <= solver->parameters.step_time_max.rel_value_us);
1779 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1780 solver->parameters.step_time_min).rel_value_us;
1781 offset = (double) solver->parameters.step_time_min.rel_value_us;
1782 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1784 GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1785 GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1787 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1789 if (solver->simulate)
1791 time_next = GNUNET_TIME_UNIT_ZERO;
1794 if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1796 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1802 * Triggers one step per agent
1807 ril_step (struct GAS_RIL_Handle *solver)
1809 struct RIL_Peer_Agent *cur;
1811 if (GNUNET_YES == solver->bulk_lock)
1813 solver->bulk_changes++;
1817 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1819 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1821 if (0 == solver->step_count)
1823 solver->step_time_last = GNUNET_TIME_absolute_get ();
1826 ril_calculate_discount (solver);
1827 ril_networks_update_state (solver);
1829 //trigger one step per active, unblocked agent
1830 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1834 if (NULL == cur->address_inuse)
1836 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1838 if (cur->address_inuse)
1845 ril_networks_update_state (solver);
1847 solver->step_count += 1;
1848 ril_step_schedule_next (solver);
1850 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1852 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1853 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1855 if (cur->suggestion_issue) {
1856 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1857 cur->suggestion_issue = GNUNET_NO;
1860 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1864 * Initializes the matrix W of parameter vectors theta with small random numbers.
1866 * @param agent The respective agent
1869 agent_w_init (struct RIL_Peer_Agent *agent)
1874 for (i = 0; i < agent->n; i++)
1876 for (k = 0; k < agent->m; k++)
1878 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));
1884 * Initialize an agent without addresses and its knowledge base
1886 * @param s ril solver
1887 * @param peer the one in question
1888 * @return handle to the new agent
1890 static struct RIL_Peer_Agent *
1891 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1894 struct GAS_RIL_Handle * solver = s;
1895 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1897 agent->envi = solver;
1898 agent->peer = *peer;
1899 agent->step_count = 0;
1900 agent->is_active = GNUNET_NO;
1901 agent->bw_in = RIL_MIN_BW;
1902 agent->bw_out = RIL_MIN_BW;
1903 agent->suggestion_issue = GNUNET_NO;
1904 agent->n = RIL_ACTION_TYPE_NUM;
1906 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1907 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1908 for (i = 0; i < agent->n; i++)
1910 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1911 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1913 agent_w_init(agent);
1914 agent->eligibility_reset = GNUNET_NO;
1915 agent->a_old = RIL_ACTION_INVALID;
1916 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1917 agent->address_inuse = NULL;
1918 agent->objective_old = 0;
1926 * @param solver the solver handle
1927 * @param agent the agent to retire
1930 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1934 for (i = 0; i < agent->n; i++)
1936 GNUNET_free_non_null(agent->W[i]);
1937 GNUNET_free_non_null(agent->E[i]);
1939 GNUNET_free_non_null(agent->W);
1940 GNUNET_free_non_null(agent->E);
1941 GNUNET_free_non_null(agent->s_old);
1946 * Returns the agent for a peer
1948 * @param solver the solver handle
1949 * @param peer the identity of the peer
1950 * @param create whether or not to create an agent, if none is allocated yet
1953 static struct RIL_Peer_Agent *
1954 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1956 struct RIL_Peer_Agent *cur;
1958 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1960 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1968 cur = agent_init (solver, peer);
1969 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
1976 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1977 * considered inactive and not used. Addresses in an inactive network are ignored.
1979 * @param solver solver handle
1980 * @param network the network type
1981 * @return whether or not the network is considered active
1984 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1986 struct RIL_Scope *net;
1988 net = ril_get_network (solver, network);
1989 return net->bw_out_available >= RIL_MIN_BW;
1993 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1994 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1995 * frees the memory of the old vector, and redirects the pointer to the new one.
1997 * @param old pointer to the pointer to the first element of the vector
1998 * @param element_size byte size of the vector elements
1999 * @param hole_start the first element to cut out
2000 * @param hole_length the number of elements to cut out
2001 * @param old_length the length of the old vector
2004 ril_cut_from_vector (void **old,
2005 size_t element_size,
2006 unsigned int hole_start,
2007 unsigned int hole_length,
2008 unsigned int old_length)
2011 char *oldptr = (char *) *old;
2013 unsigned int bytes_before;
2014 unsigned int bytes_hole;
2015 unsigned int bytes_after;
2017 GNUNET_assert(old_length >= hole_length);
2018 GNUNET_assert(old_length >= (hole_start + hole_length));
2020 size = element_size * (old_length - hole_length);
2022 bytes_before = element_size * hole_start;
2023 bytes_hole = element_size * hole_length;
2024 bytes_after = element_size * (old_length - hole_start - hole_length);
2032 tmpptr = GNUNET_malloc (size);
2033 memcpy (tmpptr, oldptr, bytes_before);
2034 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2040 *old = (void *) tmpptr;
2044 * Solver API functions
2045 * ---------------------------
2049 * Change relative preference for quality in solver
2051 * @param solver the solver handle
2052 * @param peer the peer to change the preference for
2053 * @param kind the kind to change the preference
2054 * @param pref_rel the normalized preference value for this kind over all clients
2057 GAS_ril_address_change_preference (void *solver,
2058 const struct GNUNET_PeerIdentity *peer,
2059 enum GNUNET_ATS_PreferenceKind kind,
2062 LOG(GNUNET_ERROR_TYPE_DEBUG,
2063 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2064 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2070 * Entry point for the plugin
2072 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2075 libgnunet_plugin_ats_ril_init (void *cls)
2077 struct GNUNET_ATS_PluginEnvironment *env = cls;
2078 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2079 struct RIL_Scope * cur;
2083 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
2085 GNUNET_assert(NULL != env);
2086 GNUNET_assert(NULL != env->cfg);
2087 GNUNET_assert(NULL != env->stats);
2088 GNUNET_assert(NULL != env->bandwidth_changed_cb);
2089 GNUNET_assert(NULL != env->get_preferences);
2090 GNUNET_assert(NULL != env->get_property);
2092 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2094 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2097 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2098 &solver->parameters.step_time_min))
2100 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2103 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2104 &solver->parameters.step_time_max))
2106 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2108 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2110 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
2111 GNUNET_free (string);
2115 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2117 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2119 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2120 GNUNET_free (string);
2124 solver->parameters.select = RIL_DEFAULT_SELECT;
2126 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
2128 solver->parameters.beta = strtod (string, NULL);
2129 GNUNET_free (string);
2130 if (!(solver->parameters.beta > 0))
2132 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);
2133 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2138 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2140 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
2142 solver->parameters.gamma = strtod (string, NULL);
2143 GNUNET_free (string);
2144 if (!(solver->parameters.gamma < 1) || (solver->parameters.gamma < 0))
2146 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);
2147 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2152 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2155 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
2157 solver->parameters.alpha = strtod (string, NULL);
2158 GNUNET_free (string);
2159 if (!(solver->parameters.alpha > 0) || solver->parameters.alpha > 1)
2161 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);
2162 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2167 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2169 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
2171 solver->parameters.lambda = strtod (string, NULL);
2172 GNUNET_free (string);
2173 if (solver->parameters.lambda < 0 || solver->parameters.lambda > 1)
2175 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);
2176 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2181 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2183 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
2185 solver->parameters.explore_ratio = strtod (string, NULL);
2186 GNUNET_free (string);
2187 if (solver->parameters.explore_ratio < 0 || solver->parameters.explore_ratio > 1)
2189 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);
2190 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
2195 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
2197 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
2199 solver->parameters.reward_global_share = strtod (string, NULL);
2200 GNUNET_free (string);
2201 if (solver->parameters.reward_global_share < 0 || solver->parameters.reward_global_share > 1)
2203 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);
2204 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
2209 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
2211 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE", &string))
2213 solver->parameters.temperature = strtod (string, NULL);
2214 GNUNET_free (string);
2215 if (solver->parameters.temperature <= 0)
2217 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE not positive. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE);
2218 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
2223 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
2225 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2227 solver->simulate = GNUNET_NO;
2229 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2231 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2235 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2237 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2239 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2240 GNUNET_free (string);
2244 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2247 env->sf.s_add = &GAS_ril_address_add;
2248 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
2249 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
2250 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
2251 env->sf.s_address_update_network = &GAS_ril_address_change_network;
2252 env->sf.s_get = &GAS_ril_get_preferred_address;
2253 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2254 env->sf.s_pref = &GAS_ril_address_change_preference;
2255 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
2256 env->sf.s_del = &GAS_ril_address_delete;
2257 env->sf.s_bulk_start = &GAS_ril_bulk_start;
2258 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
2260 solver->plugin_envi = env;
2261 solver->networks_count = env->network_count;
2262 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2263 solver->step_count = 0;
2264 solver->done = GNUNET_NO;
2266 for (c = 0; c < env->network_count; c++)
2268 cur = &solver->network_entries[c];
2269 cur->type = env->networks[c];
2270 cur->bw_in_available = env->in_quota[c];
2271 cur->bw_out_available = env->out_quota[c];
2272 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);
2275 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2276 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2277 solver->parameters.algorithm ? "Q" : "SARSA",
2278 solver->parameters.alpha,
2279 solver->parameters.beta,
2280 solver->parameters.lambda);
2281 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s, global_share = %f\n",
2282 solver->parameters.explore_ratio,
2283 solver->parameters.temperature,
2284 solver->parameters.select ? "EGREEDY" : "SOFTMAX",
2285 solver->parameters.reward_global_share);
2286 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2287 solver->parameters.rbf_divisor);
2293 * Exit point for the plugin
2295 * @param cls the solver handle
2298 libgnunet_plugin_ats_ril_done (void *cls)
2300 struct GAS_RIL_Handle *s = cls;
2301 struct RIL_Peer_Agent *cur_agent;
2302 struct RIL_Peer_Agent *next_agent;
2304 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2306 s->done = GNUNET_YES;
2308 cur_agent = s->agents_head;
2309 while (NULL != cur_agent)
2311 next_agent = cur_agent->next;
2312 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2313 agent_die (s, cur_agent);
2314 cur_agent = next_agent;
2317 if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
2319 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2321 GNUNET_free(s->network_entries);
2328 * Add a new address for a peer to the solver
2330 * The address is already contained in the addresses hashmap!
2332 * @param solver the solver Handle
2333 * @param address the address to add
2334 * @param network network type of this address
2337 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
2339 struct GAS_RIL_Handle *s = solver;
2340 struct RIL_Peer_Agent *agent;
2341 struct RIL_Address_Wrapped *address_wrapped;
2342 struct RIL_Scope *net;
2350 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
2352 net = ril_get_network (s, network);
2353 address->solver_information = net;
2355 if (!ril_network_is_active (s, network))
2357 LOG(GNUNET_ERROR_TYPE_DEBUG,
2358 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2359 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2363 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2366 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2367 address_wrapped->address_naked = address;
2368 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2370 //increase size of W
2371 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2373 n_new = agent->n + 1;
2376 GNUNET_array_grow(agent->W, agent->n, n_new);
2378 GNUNET_array_grow(agent->E, agent->n, n_new);
2379 for (i = 0; i < n_new; i++)
2384 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2386 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2391 GNUNET_array_grow(agent->W[i], zero, m_new);
2393 GNUNET_array_grow(agent->E[i], zero, m_new);
2397 //increase size of old state vector
2399 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2401 ril_try_unblock_agent(s, agent, GNUNET_NO);
2405 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2406 address->active ? "active" : "inactive", address->plugin, address->addr,
2407 GNUNET_i2s (&address->peer));
2411 * Delete an address in the solver
2413 * The address is not contained in the address hashmap anymore!
2415 * @param solver the solver handle
2416 * @param address the address to remove
2417 * @param session_only delete only session not whole address
2420 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
2422 struct GAS_RIL_Handle *s = solver;
2423 struct RIL_Peer_Agent *agent;
2424 struct RIL_Address_Wrapped *address_wrapped;
2425 int address_was_used = address->active;
2430 struct RIL_Scope *net;
2432 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2433 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2434 address->addr, GNUNET_i2s (&address->peer));
2436 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2439 net = address->solver_information;
2440 GNUNET_assert(!ril_network_is_active (s, net->type));
2441 LOG(GNUNET_ERROR_TYPE_DEBUG,
2442 "No agent allocated for peer yet, since address was in inactive network\n");
2446 address_index = agent_address_get_index (agent, address);
2447 address_wrapped = agent_address_get (agent, address);
2449 if (NULL == address_wrapped)
2451 net = address->solver_information;
2452 GNUNET_assert(!ril_network_is_active (s, net->type));
2453 LOG(GNUNET_ERROR_TYPE_DEBUG,
2454 "Address not considered by agent, address was in inactive network\n");
2458 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2459 GNUNET_free(address_wrapped);
2462 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2463 n_new = agent->n - 1;
2465 for (i = 0; i < agent->n; i++)
2467 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2468 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2469 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2470 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2471 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2472 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2474 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2475 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2476 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2478 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2480 //correct last action
2481 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2485 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2487 agent->a_old = RIL_ACTION_INVALID;
2489 //decrease old state vector
2490 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2491 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2492 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2496 if (address_was_used)
2498 if (NULL != agent->addresses_head) //if peer has an address left, use it
2500 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, RIL_MIN_BW, RIL_MIN_BW,
2505 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2513 * Update the properties of an address in the solver
2515 * @param solver solver handle
2516 * @param address the address
2517 * @param type the ATSI type in HBO
2518 * @param abs_value the absolute value of the property
2519 * @param rel_value the normalized value
2522 GAS_ril_address_property_changed (void *solver,
2523 struct ATS_Address *address,
2528 LOG(GNUNET_ERROR_TYPE_DEBUG,
2529 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2530 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2531 address->addr, rel_value);
2537 * Update the session of an address in the solver
2539 * NOTE: values in addresses are already updated
2541 * @param solver solver handle
2542 * @param address the address
2543 * @param cur_session the current session
2544 * @param new_session the new session
2547 GAS_ril_address_session_changed (void *solver,
2548 struct ATS_Address *address,
2549 uint32_t cur_session,
2550 uint32_t new_session)
2552 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2556 * Notify the solver that an address is (not) actively used by transport
2557 * to communicate with a remote peer
2559 * NOTE: values in addresses are already updated
2561 * @param solver solver handle
2562 * @param address the address
2563 * @param in_use usage state
2566 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2568 LOG(GNUNET_ERROR_TYPE_DEBUG,
2569 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2570 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2574 * Notify solver that the network an address is located in has changed
2576 * NOTE: values in addresses are already updated
2578 * @param solver solver handle
2579 * @param address the address
2580 * @param current_network the current network
2581 * @param new_network the new network
2584 GAS_ril_address_change_network (void *solver,
2585 struct ATS_Address *address,
2586 uint32_t current_network,
2587 uint32_t new_network)
2589 struct GAS_RIL_Handle *s = solver;
2590 struct RIL_Peer_Agent *agent;
2592 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2593 "%s address of peer %s from '%s' to '%s'\n",
2594 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2595 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2597 if (address->active && !ril_network_is_active (solver, new_network))
2599 GAS_ril_address_delete (solver, address, GNUNET_NO);
2603 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2606 GNUNET_assert(!ril_network_is_active (solver, current_network));
2608 GAS_ril_address_add (s, address, new_network);
2612 address->solver_information = ril_get_network(solver, new_network);
2616 * Give feedback about the current assignment
2618 * @param solver the solver handle
2619 * @param application the application
2620 * @param peer the peer to change the preference for
2621 * @param scope the time interval for this feedback: [now - scope .. now]
2622 * @param kind the kind to change the preference
2623 * @param score the score
2626 GAS_ril_address_preference_feedback (void *solver,
2628 const struct GNUNET_PeerIdentity *peer,
2629 const struct GNUNET_TIME_Relative scope,
2630 enum GNUNET_ATS_PreferenceKind kind,
2633 LOG(GNUNET_ERROR_TYPE_DEBUG,
2634 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2635 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2636 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2640 * Start a bulk operation
2642 * @param solver the solver
2645 GAS_ril_bulk_start (void *solver)
2647 struct GAS_RIL_Handle *s = solver;
2649 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2655 * Bulk operation done
2657 * @param solver the solver handle
2660 GAS_ril_bulk_stop (void *solver)
2662 struct GAS_RIL_Handle *s = solver;
2664 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2666 if (s->bulk_lock < 1)
2673 if (0 < s->bulk_changes)
2676 s->bulk_changes = 0;
2681 * Tell solver to notify ATS if the address to use changes for a specific
2682 * peer using the bandwidth changed callback
2684 * The solver must only notify about changes for peers with pending address
2687 * @param solver the solver handle
2688 * @param peer the identity of the peer
2690 const struct ATS_Address *
2691 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2694 * activate agent, return currently chosen address
2696 struct GAS_RIL_Handle *s = solver;
2697 struct RIL_Peer_Agent *agent;
2699 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2701 agent = ril_get_agent (s, peer, GNUNET_YES);
2703 agent->is_active = GNUNET_YES;
2704 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2706 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2708 if (agent->address_inuse)
2710 LOG(GNUNET_ERROR_TYPE_DEBUG,
2711 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2712 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2716 LOG(GNUNET_ERROR_TYPE_DEBUG,
2717 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2721 return agent->address_inuse;
2725 * Tell solver stop notifying ATS about changes for this peers
2727 * The solver must only notify about changes for peers with pending address
2730 * @param solver the solver handle
2731 * @param peer the peer
2734 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2736 struct GAS_RIL_Handle *s = solver;
2737 struct RIL_Peer_Agent *agent;
2739 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2741 agent = ril_get_agent (s, peer, GNUNET_NO);
2748 if (GNUNET_NO == agent->is_active)
2754 agent->is_active = GNUNET_NO;
2756 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2761 LOG(GNUNET_ERROR_TYPE_DEBUG,
2762 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2763 GNUNET_i2s (peer), agent->address_inuse->plugin);
2766 /* end of plugin_ats_ril.c */