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 (1 * 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_DELAY_MAX 1000
38 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200)
39 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1000)
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 50
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_DECAY 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? Potentially add more actions
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_wrapped (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)
677 double **theta = agent->W;
679 delta = agent->envi->global_discount_integrated * reward; //reward
680 delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
681 delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
683 // 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",
684 // agent->step_count,
685 // agent_q (agent, agent->s_old, agent->a_old),
686 // agent->envi->parameters.alpha,
688 // agent->envi->global_discount_variable,
689 // agent_q (agent, s_next, a_prime),
692 for (k = 0; k < agent->n; k++)
694 for (i = 0; i < agent->m; i++)
696 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
697 // agent->envi->parameters.alpha,
701 theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
708 * Changes the eligibility trace vector e in various manners:
709 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
710 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
711 * #RIL_E_DISCOUNT - multiplies e with discount factor and lambda as in the update rule
712 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
714 * @param agent the agent handle
715 * @param mod the kind of modification
716 * @param feature the feature vector
719 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
720 enum RIL_E_Modification mod,
727 for (i = 0; i < agent->m; i++)
731 case RIL_E_ACCUMULATE:
732 agent->E[action][i] += feature[i];
735 agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
738 for (k = 0; k < agent->n; k++)
740 agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
744 for (k = 0; k < agent->n; k++)
754 * Informs the environment about the status of the solver
761 ril_inform (struct GAS_RIL_Handle *solver,
762 enum GAS_Solver_Operation op,
763 enum GAS_Solver_Status stat)
765 if (NULL != solver->plugin_envi->info_cb)
766 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
771 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
772 * notify ATS of its new decision
774 * @param solver solver handle
775 * @param agent agent handle
776 * @param new_address the address which is to be used
777 * @param new_bw_in the new amount of inbound bandwidth set for this address
778 * @param new_bw_out the new amount of outbound bandwidth set for this address
779 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
782 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
783 struct RIL_Peer_Agent *agent,
784 struct ATS_Address *new_address,
785 unsigned long long new_bw_in,
786 unsigned long long new_bw_out,
789 int notify = GNUNET_NO;
791 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
794 if (agent->address_inuse != new_address)
796 if (NULL != agent->address_inuse)
798 agent->address_inuse->active = GNUNET_NO;
799 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
800 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
802 if (NULL != new_address)
804 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
805 new_address->active = agent->is_active;
806 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
807 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
809 notify |= GNUNET_YES;
815 if (new_address->active != agent->is_active)
817 new_address->active = agent->is_active;
818 notify |= GNUNET_YES;
822 if (agent->bw_in != new_bw_in)
824 agent->bw_in = new_bw_in;
825 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
826 notify |= GNUNET_YES;
828 if (agent->bw_out != new_bw_out)
830 agent->bw_out = new_bw_out;
831 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
832 notify |= GNUNET_YES;
836 if (notify && agent->is_active && (GNUNET_NO == silent))
840 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
841 agent->suggestion_issue = GNUNET_YES;
842 agent->suggestion_address = new_address;
844 else if (agent->address_inuse)
846 //disconnect case, no new address
847 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
848 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
852 agent->suggestion_issue = GNUNET_YES;
853 agent->suggestion_address = agent->address_inuse;
856 agent->address_inuse = new_address;
861 * Allocates a state vector and fills it with the features present
862 * @param solver the solver handle
863 * @param agent the agent handle
864 * @return pointer to the state vector
867 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
878 unsigned long long max_bw;
880 state = GNUNET_malloc (sizeof(double) * agent->m);
884 y[0] = (double) agent->bw_out;
885 y[1] = (double) agent->bw_in;
887 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
888 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
890 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
892 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
893 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
896 sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
897 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
906 * Retrieves an ATS information value of an address
908 * @param address the address in question
909 * @param type the ATS information type
913 ril_get_atsi (struct ATS_Address *address, uint32_t type)
916 GNUNET_assert(NULL != address);
918 if ((NULL == address->atsi) || (0 == address->atsi_count))
919 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
921 for (c1 = 0; c1 < address->atsi_count; c1++)
923 if (ntohl (address->atsi[c1].type) == type)
924 return ntohl (address->atsi[c1].value);
926 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
930 * Returns the utility value of the connection an agent manages
932 * @param agent the agent in question
933 * @return the utility value
936 agent_get_utility (struct RIL_Peer_Agent *agent)
938 const double *preferences;
943 preferences = agent->envi->plugin_envi->get_preferences (agent->envi->plugin_envi->get_preference_cls,
946 delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
947 delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
949 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
950 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
951 sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
953 // return (double) (agent->bw_in/RIL_MIN_BW);
954 // return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
959 * Calculates the social welfare within a network scope according to what social
960 * welfare measure is set in the configuration.
962 * @param solver the solver handle
963 * @param scope the network scope in question
964 * @return the social welfare value
967 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
969 struct RIL_Peer_Agent *cur;
972 switch (solver->parameters.social_welfare)
974 case RIL_WELFARE_EGALITARIAN:
976 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
978 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
980 result = GNUNET_MIN(result, agent_get_utility(cur));
985 case RIL_WELFARE_NASH:
987 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
989 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
991 result *= pow(agent_get_utility(cur), 1.0 / (double) scope->agent_count);
996 GNUNET_assert(GNUNET_NO);
1001 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1003 struct RIL_Scope *net;
1004 unsigned long long over_max;
1005 unsigned long long over_in = 0;
1006 unsigned long long over_out = 0;
1008 net = agent->address_inuse->solver_information;
1010 if (net->bw_in_utilized > net->bw_in_available)
1012 over_in = net->bw_in_utilized - net->bw_in_available;
1013 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1018 if (net->bw_out_utilized > net->bw_out_available)
1020 over_out = net->bw_out_utilized - net->bw_out_available;
1021 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1026 over_max = GNUNET_MAX (over_in , over_out) / RIL_MIN_BW;
1028 return -1.0 * (double) over_max;
1032 * Gets the reward for the last performed step, which is calculated in equal
1033 * parts from the local (the peer specific) and the global (for all peers
1034 * identical) reward.
1036 * @param solver the solver handle
1037 * @param agent the agent handle
1038 * @return the reward
1041 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1043 struct RIL_Scope *net;
1051 net = agent->address_inuse->solver_information;
1053 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1054 delta = objective - agent->objective_old;
1055 agent->objective_old = objective;
1059 agent->nop_bonus = delta * RIL_NOP_DECAY;
1063 agent->nop_bonus *= RIL_NOP_DECAY;
1066 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1069 penalty = envi_get_penalty(solver, agent);
1071 reward = delta + steady;
1072 return ((1 - pen_share) * reward) + (pen_share * penalty);
1076 * Doubles the bandwidth for the active address
1078 * @param solver solver handle
1079 * @param agent agent handle
1080 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1083 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1084 struct RIL_Peer_Agent *agent,
1087 unsigned long long new_bw;
1091 new_bw = agent->bw_in * 2;
1092 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1093 new_bw = RIL_MAX_BW;
1094 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1095 agent->bw_out, GNUNET_NO);
1099 new_bw = agent->bw_out * 2;
1100 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1101 new_bw = RIL_MAX_BW;
1102 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1108 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1109 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1111 * @param solver solver handle
1112 * @param agent agent handle
1113 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1117 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1118 struct RIL_Peer_Agent *agent,
1121 unsigned long long new_bw;
1125 new_bw = agent->bw_in / 2;
1126 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1127 new_bw = RIL_MIN_BW;
1128 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1133 new_bw = agent->bw_out / 2;
1134 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1135 new_bw = RIL_MIN_BW;
1136 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1142 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1144 * @param solver solver handle
1145 * @param agent agent handle
1146 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1150 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1152 unsigned long long new_bw;
1156 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1157 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1158 new_bw = RIL_MAX_BW;
1159 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1160 agent->bw_out, GNUNET_NO);
1164 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1165 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1166 new_bw = RIL_MAX_BW;
1167 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1173 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1174 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1176 * @param solver solver handle
1177 * @param agent agent handle
1178 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1182 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1184 unsigned long long new_bw;
1188 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1189 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1190 new_bw = RIL_MIN_BW;
1191 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1196 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1197 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1198 new_bw = RIL_MIN_BW;
1199 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1205 * Switches to the address given by its index
1207 * @param solver solver handle
1208 * @param agent agent handle
1209 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1212 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1213 struct RIL_Peer_Agent *agent,
1214 unsigned int address_index)
1216 struct RIL_Address_Wrapped *cur;
1219 cur = agent_address_get_wrapped(agent, agent->address_inuse);
1221 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1223 if (i == address_index)
1225 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1233 //no address with address_index exists, in this case this action should not be callable
1234 GNUNET_assert(GNUNET_NO);
1238 * Puts the action into effect by calling the according function
1240 * @param solver the solver handle
1241 * @param agent the action handle
1242 * @param action the action to perform by the solver
1245 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1251 case RIL_ACTION_NOTHING:
1253 case RIL_ACTION_BW_IN_DBL:
1254 envi_action_bw_double (solver, agent, GNUNET_YES);
1256 case RIL_ACTION_BW_IN_HLV:
1257 envi_action_bw_halven (solver, agent, GNUNET_YES);
1259 case RIL_ACTION_BW_IN_INC:
1260 envi_action_bw_inc (solver, agent, GNUNET_YES);
1262 case RIL_ACTION_BW_IN_DEC:
1263 envi_action_bw_dec (solver, agent, GNUNET_YES);
1265 case RIL_ACTION_BW_OUT_DBL:
1266 envi_action_bw_double (solver, agent, GNUNET_NO);
1268 case RIL_ACTION_BW_OUT_HLV:
1269 envi_action_bw_halven (solver, agent, GNUNET_NO);
1271 case RIL_ACTION_BW_OUT_INC:
1272 envi_action_bw_inc (solver, agent, GNUNET_NO);
1274 case RIL_ACTION_BW_OUT_DEC:
1275 envi_action_bw_dec (solver, agent, GNUNET_NO);
1278 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1280 address_index = action - RIL_ACTION_TYPE_NUM;
1282 GNUNET_assert(address_index >= 0);
1284 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1286 envi_action_address_switch (solver, agent, address_index);
1289 // error - action does not exist
1290 GNUNET_assert(GNUNET_NO);
1295 * Selects the next action using the e-greedy strategy. I.e. with a probability
1296 * of (1-e) the action with the maximum expected return will be chosen
1297 * (=> exploitation) and with probability (e) a random action will be chosen.
1298 * In case the Q-learning rule is set, the function also resets the eligibility
1299 * traces in the exploration case (after Watkin's Q-learning).
1301 * @param agent the agent selecting an action
1302 * @param state the current state-feature vector
1303 * @return the action index
1306 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1309 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1310 UINT32_MAX) / (double) UINT32_MAX;
1312 if (r < agent->envi->parameters.explore_ratio) //explore
1314 action = agent_get_action_random(agent);
1315 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1317 agent->eligibility_reset = GNUNET_YES;
1323 action = agent_get_action_max(agent, state);
1329 * Selects the next action with a probability corresponding to its value. The
1330 * probability is calculated using a Boltzmann distribution with a temperature
1331 * value. The higher the temperature, the more are the action selection
1332 * probabilities the same. With a temperature of 0, the selection is greedy,
1333 * i.e. always the action with the highest value is chosen.
1339 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1343 double eqt[agent->n];
1348 a_max = agent_get_action_max(agent, state);
1350 for (i=0; i<agent->n; i++)
1352 if (agent_action_is_possible(agent, i))
1354 eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1358 for (i=0; i<agent->n; i++)
1360 if (agent_action_is_possible(agent, i))
1369 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1370 UINT32_MAX) / (double) UINT32_MAX;
1372 for (i=0; i<agent->n; i++)
1376 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1379 agent->eligibility_reset = GNUNET_YES;
1385 GNUNET_assert(GNUNET_NO);
1389 * Select the next action of an agent either according to the e-greedy strategy
1390 * or the softmax strategy.
1392 * @param agent the agent in question
1393 * @param state the current state-feature vector
1394 * @return the action index
1397 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1399 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1401 return agent_select_egreedy(agent, state);
1405 return agent_select_softmax(agent, state);
1410 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1411 * after having done the last action a_old. It observes the new state s_next and the reward
1412 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1413 * next action is put into effect.
1415 * @param agent the agent performing the step
1418 agent_step (struct RIL_Peer_Agent *agent)
1420 int a_next = RIL_ACTION_INVALID;
1425 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1426 GNUNET_i2s (&agent->peer),
1427 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1429 s_next = envi_get_state (agent->envi, agent);
1430 reward = envi_get_reward (agent->envi, agent);
1432 if (agent->eligibility_reset)
1434 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1435 agent->eligibility_reset = GNUNET_NO;
1439 agent_modify_eligibility (agent, RIL_E_DISCOUNT, NULL, -1);
1441 if (RIL_ACTION_INVALID != agent->a_old)
1443 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1446 switch (agent->envi->parameters.algorithm)
1448 case RIL_ALGO_SARSA:
1449 a_next = agent_select_action (agent, s_next);
1450 if (RIL_ACTION_INVALID != agent->a_old)
1452 //updates weights with selected action (on-policy), if not first step
1453 agent_update (agent, reward, s_next, a_next);
1458 a_max = agent_get_action_max (agent, s_next);
1459 if (RIL_ACTION_INVALID != agent->a_old)
1461 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1462 agent_update (agent, reward, s_next, a_max);
1464 a_next = agent_select_action (agent, s_next);
1468 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1470 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1477 envi_do_action (agent->envi, agent, a_next);
1479 GNUNET_free(agent->s_old);
1480 agent->s_old = s_next;
1481 agent->a_old = a_next;
1483 agent->step_count += 1;
1487 * Prototype of the ril_step() procedure
1489 * @param solver the solver handle
1492 ril_step (struct GAS_RIL_Handle *solver);
1495 * Task for the scheduler, which performs one step and lets the solver know that
1496 * no further step is scheduled.
1498 * @param cls the solver handle
1499 * @param tc the task context for the scheduler
1502 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1504 struct GAS_RIL_Handle *solver = cls;
1506 solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1511 * Determines how much of the available bandwidth is assigned. If more is
1512 * assigned than available it returns 1. The function is used to determine the
1513 * step size of the adaptive stepping.
1515 * @param solver the solver handle
1519 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1522 struct RIL_Scope net;
1523 unsigned long long sum_assigned = 0;
1524 unsigned long long sum_available = 0;
1527 for (i = 0; i < solver->networks_count; i++)
1529 net = solver->network_entries[i];
1530 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1532 sum_assigned += net.bw_in_utilized;
1533 sum_assigned += net.bw_out_utilized;
1534 sum_available += net.bw_in_available;
1535 sum_available += net.bw_out_available;
1538 if (sum_available > 0)
1540 ratio = ((double) sum_assigned) / ((double) sum_available);
1547 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1551 * Lookup network struct by type
1553 * @param s the solver handle
1554 * @param type the network type
1555 * @return the network struct
1557 static struct RIL_Scope *
1558 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1562 for (i = 0; i < s->networks_count; i++)
1564 if (s->network_entries[i].type == type)
1566 return &s->network_entries[i];
1573 * Determines whether more connections are allocated in a network scope, than
1574 * they would theoretically fit. This is used as a heuristic to determine,
1575 * whether a new connection can be allocated or not.
1577 * @param solver the solver handle
1578 * @param network the network scope in question
1579 * @return GNUNET_YES if there are theoretically enough resources left
1582 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1584 struct RIL_Scope *net;
1585 struct RIL_Peer_Agent *agent;
1586 unsigned long long address_count = 0;
1588 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1590 if (agent->address_inuse && agent->is_active)
1592 net = agent->address_inuse->solver_information;
1593 if (net->type == network)
1600 net = ril_get_network (solver, network);
1601 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1605 * Unblocks an agent for which a connection request is there, that could not
1606 * be satisfied. Iterates over the addresses of the agent, if one of its
1607 * addresses can now be allocated in its scope the agent is unblocked,
1608 * otherwise it remains unchanged.
1610 * @param solver the solver handle
1611 * @param agent the agent in question
1615 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1617 struct RIL_Address_Wrapped *addr_wrap;
1618 struct RIL_Scope *net;
1620 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1622 net = addr_wrap->address_naked->solver_information;
1623 if (ril_network_is_not_full(solver, net->type))
1625 if (NULL == agent->address_inuse)
1626 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, agent->bw_in, agent->bw_out, silent);
1630 agent->address_inuse = NULL;
1634 * Determines how much the reward needs to be discounted depending on the amount
1635 * of time, which has passed since the last time-step.
1637 * @param solver the solver handle
1640 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1642 struct GNUNET_TIME_Absolute time_now;
1643 struct GNUNET_TIME_Relative time_delta;
1646 // MDP case - TODO! remove when debugged and test SMDP case
1647 if (solver->simulate)
1649 solver->global_discount_variable = solver->parameters.gamma;
1650 solver->global_discount_integrated = 1;
1656 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1657 time_now = GNUNET_TIME_absolute_get ();
1658 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1659 solver->step_time_last = time_now;
1660 tau = (double) time_delta.rel_value_us
1661 / (double) solver->parameters.step_time_min.rel_value_us;
1663 //calculate reward discounts (once per step for all agents)
1664 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1665 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1666 / (double) solver->parameters.beta;
1670 * Count the number of active agents/connections in a network scope
1672 * @param solver the solver handle
1673 * @param scope the network scope in question
1674 * @return the number of allocated connections
1677 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1680 struct RIL_Peer_Agent *cur_agent;
1682 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1684 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1693 * Calculates how much bandwidth is assigned in sum in a network scope, either
1694 * in the inbound or in the outbound direction.
1696 * @param solver the solver handle
1697 * @param type the type of the network scope in question
1698 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1699 * otherwise the outbound direction will be summed up
1700 * @return the sum of the assigned bandwidths
1702 static unsigned long long
1703 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1705 struct RIL_Peer_Agent *cur;
1706 struct RIL_Scope *net;
1707 unsigned long long sum = 0;
1709 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1711 if (cur->is_active && cur->address_inuse)
1713 net = cur->address_inuse->solver_information;
1714 if (net->type == type)
1728 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1729 * either in the inbound or in the outbound direction.
1731 * @param solver the solver handle
1732 * @param type the type of the network scope in question
1733 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1734 * otherwise the outbound direction will be summed up
1735 * @return the sum of the utilized bandwidths (in bytes/second)
1737 static unsigned long long
1738 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1740 struct RIL_Peer_Agent *cur;
1741 struct RIL_Scope *net;
1742 unsigned long long sum = 0;
1744 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1746 if (cur->is_active && cur->address_inuse)
1748 net = cur->address_inuse->solver_information;
1749 if (net->type == type)
1752 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
1754 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
1763 * Retrieves the state of the network scope, so that its attributes are up-to-
1766 * @param solver the solver handle
1769 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1772 struct RIL_Scope *net;
1774 for (c = 0; c < solver->networks_count; c++)
1776 net = &solver->network_entries[c];
1777 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1778 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1779 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1780 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1781 net->agent_count = ril_network_count_active_agents(solver, net);
1782 net->social_welfare = ril_network_get_social_welfare(solver, net);
1787 * Schedules the next global step in an adaptive way. The more resources are
1788 * left, the earlier the next step is scheduled. This serves the reactivity of
1789 * the solver to changed inputs.
1791 * @param solver the solver handle
1794 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1800 struct GNUNET_TIME_Relative time_next;
1802 used_ratio = ril_get_used_resource_ratio (solver);
1805 solver->parameters.step_time_min.rel_value_us
1806 <= solver->parameters.step_time_max.rel_value_us);
1808 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1809 solver->parameters.step_time_min).rel_value_us;
1810 offset = (double) solver->parameters.step_time_min.rel_value_us;
1811 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1813 GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
1814 GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
1816 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1818 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1824 if (solver->simulate)
1826 time_next = GNUNET_TIME_UNIT_ZERO;
1829 if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1831 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1837 * Triggers one step per agent
1842 ril_step (struct GAS_RIL_Handle *solver)
1844 struct RIL_Peer_Agent *cur;
1846 if (GNUNET_YES == solver->bulk_lock)
1848 solver->bulk_changes++;
1852 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1854 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1856 if (0 == solver->step_count)
1858 solver->step_time_last = GNUNET_TIME_absolute_get ();
1861 ril_calculate_discount (solver);
1862 ril_networks_update_state (solver);
1864 //trigger one step per active, unblocked agent
1865 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1869 if (NULL == cur->address_inuse)
1871 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1873 if (cur->address_inuse)
1880 ril_networks_update_state (solver);
1882 solver->step_count += 1;
1883 ril_step_schedule_next (solver);
1885 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1887 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1888 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1890 if (cur->suggestion_issue) {
1891 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1892 cur->suggestion_issue = GNUNET_NO;
1895 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1899 * Initializes the matrix W of parameter vectors theta with small random numbers.
1901 * @param agent The respective agent
1904 agent_w_init (struct RIL_Peer_Agent *agent)
1909 for (i = 0; i < agent->n; i++)
1911 for (k = 0; k < agent->m; k++)
1913 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));
1919 * Initialize an agent without addresses and its knowledge base
1921 * @param s ril solver
1922 * @param peer the one in question
1923 * @return handle to the new agent
1925 static struct RIL_Peer_Agent *
1926 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1929 struct GAS_RIL_Handle * solver = s;
1930 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1932 agent->envi = solver;
1933 agent->peer = *peer;
1934 agent->step_count = 0;
1935 agent->is_active = GNUNET_NO;
1936 agent->bw_in = RIL_MIN_BW;
1937 agent->bw_out = RIL_MIN_BW;
1938 agent->suggestion_issue = GNUNET_NO;
1939 agent->n = RIL_ACTION_TYPE_NUM;
1941 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1942 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1943 for (i = 0; i < agent->n; i++)
1945 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1946 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1948 agent_w_init(agent);
1949 agent->eligibility_reset = GNUNET_NO;
1950 agent->a_old = RIL_ACTION_INVALID;
1951 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1952 agent->address_inuse = NULL;
1953 agent->objective_old = 0;
1954 agent->nop_bonus = 0;
1962 * @param solver the solver handle
1963 * @param agent the agent to retire
1966 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1970 for (i = 0; i < agent->n; i++)
1972 GNUNET_free_non_null(agent->W[i]);
1973 GNUNET_free_non_null(agent->E[i]);
1975 GNUNET_free_non_null(agent->W);
1976 GNUNET_free_non_null(agent->E);
1977 GNUNET_free_non_null(agent->s_old);
1982 * Returns the agent for a peer
1984 * @param solver the solver handle
1985 * @param peer the identity of the peer
1986 * @param create whether or not to create an agent, if none is allocated yet
1989 static struct RIL_Peer_Agent *
1990 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1992 struct RIL_Peer_Agent *cur;
1994 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1996 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
2004 cur = agent_init (solver, peer);
2005 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
2012 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
2013 * considered inactive and not used. Addresses in an inactive network are ignored.
2015 * @param solver solver handle
2016 * @param network the network type
2017 * @return whether or not the network is considered active
2020 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
2022 struct RIL_Scope *net;
2024 net = ril_get_network (solver, network);
2025 return net->bw_out_available >= RIL_MIN_BW;
2029 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
2030 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
2031 * frees the memory of the old vector, and redirects the pointer to the new one.
2033 * @param old pointer to the pointer to the first element of the vector
2034 * @param element_size byte size of the vector elements
2035 * @param hole_start the first element to cut out
2036 * @param hole_length the number of elements to cut out
2037 * @param old_length the length of the old vector
2040 ril_cut_from_vector (void **old,
2041 size_t element_size,
2042 unsigned int hole_start,
2043 unsigned int hole_length,
2044 unsigned int old_length)
2047 char *oldptr = (char *) *old;
2049 unsigned int bytes_before;
2050 unsigned int bytes_hole;
2051 unsigned int bytes_after;
2053 GNUNET_assert(old_length >= hole_length);
2054 GNUNET_assert(old_length >= (hole_start + hole_length));
2056 size = element_size * (old_length - hole_length);
2058 bytes_before = element_size * hole_start;
2059 bytes_hole = element_size * hole_length;
2060 bytes_after = element_size * (old_length - hole_start - hole_length);
2068 tmpptr = GNUNET_malloc (size);
2069 memcpy (tmpptr, oldptr, bytes_before);
2070 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2076 *old = (void *) tmpptr;
2080 * Solver API functions
2081 * ---------------------------
2085 * Change relative preference for quality in solver
2087 * @param solver the solver handle
2088 * @param peer the peer to change the preference for
2089 * @param kind the kind to change the preference
2090 * @param pref_rel the normalized preference value for this kind over all clients
2093 GAS_ril_address_change_preference (void *solver,
2094 const struct GNUNET_PeerIdentity *peer,
2095 enum GNUNET_ATS_PreferenceKind kind,
2098 LOG(GNUNET_ERROR_TYPE_DEBUG,
2099 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2100 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2106 * Entry point for the plugin
2108 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2111 libgnunet_plugin_ats_ril_init (void *cls)
2113 struct GNUNET_ATS_PluginEnvironment *env = cls;
2114 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2115 struct RIL_Scope * cur;
2119 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
2121 GNUNET_assert(NULL != env);
2122 GNUNET_assert(NULL != env->cfg);
2123 GNUNET_assert(NULL != env->stats);
2124 GNUNET_assert(NULL != env->bandwidth_changed_cb);
2125 GNUNET_assert(NULL != env->get_preferences);
2126 GNUNET_assert(NULL != env->get_property);
2128 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2130 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2133 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2134 &solver->parameters.step_time_min))
2136 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2139 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2140 &solver->parameters.step_time_max))
2142 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2144 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2146 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
2147 GNUNET_free (string);
2151 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2153 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2155 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2156 GNUNET_free (string);
2160 solver->parameters.select = RIL_DEFAULT_SELECT;
2162 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
2164 solver->parameters.beta = strtod (string, NULL);
2165 GNUNET_free (string);
2166 if (!(solver->parameters.beta > 0))
2168 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);
2169 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2174 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2176 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
2178 solver->parameters.gamma = strtod (string, NULL);
2179 GNUNET_free (string);
2180 if (!(solver->parameters.gamma < 1) || (solver->parameters.gamma < 0))
2182 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);
2183 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2188 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2191 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
2193 solver->parameters.alpha = strtod (string, NULL);
2194 GNUNET_free (string);
2195 if (!(solver->parameters.alpha > 0) || solver->parameters.alpha > 1)
2197 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);
2198 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2203 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2205 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
2207 solver->parameters.lambda = strtod (string, NULL);
2208 GNUNET_free (string);
2209 if (solver->parameters.lambda < 0 || solver->parameters.lambda > 1)
2211 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);
2212 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2217 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2219 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
2221 solver->parameters.explore_ratio = strtod (string, NULL);
2222 GNUNET_free (string);
2223 if (solver->parameters.explore_ratio < 0 || solver->parameters.explore_ratio > 1)
2225 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);
2226 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
2231 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
2233 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
2235 solver->parameters.reward_global_share = strtod (string, NULL);
2236 GNUNET_free (string);
2237 if (solver->parameters.reward_global_share < 0 || solver->parameters.reward_global_share > 1)
2239 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);
2240 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
2245 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
2247 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE", &string))
2249 solver->parameters.temperature = strtod (string, NULL);
2250 GNUNET_free (string);
2251 if (solver->parameters.temperature <= 0)
2253 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE not positive. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE);
2254 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
2259 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
2261 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2263 solver->simulate = GNUNET_NO;
2265 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2267 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2271 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2273 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2275 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2276 GNUNET_free (string);
2280 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2283 env->sf.s_add = &GAS_ril_address_add;
2284 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
2285 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
2286 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
2287 env->sf.s_address_update_network = &GAS_ril_address_change_network;
2288 env->sf.s_get = &GAS_ril_get_preferred_address;
2289 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2290 env->sf.s_pref = &GAS_ril_address_change_preference;
2291 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
2292 env->sf.s_del = &GAS_ril_address_delete;
2293 env->sf.s_bulk_start = &GAS_ril_bulk_start;
2294 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
2296 solver->plugin_envi = env;
2297 solver->networks_count = env->network_count;
2298 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2299 solver->step_count = 0;
2300 solver->done = GNUNET_NO;
2302 for (c = 0; c < env->network_count; c++)
2304 cur = &solver->network_entries[c];
2305 cur->type = env->networks[c];
2306 cur->bw_in_available = env->in_quota[c];
2307 cur->bw_out_available = env->out_quota[c];
2308 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);
2311 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2312 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2313 solver->parameters.algorithm ? "Q" : "SARSA",
2314 solver->parameters.alpha,
2315 solver->parameters.beta,
2316 solver->parameters.lambda);
2317 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s, global_share = %f\n",
2318 solver->parameters.explore_ratio,
2319 solver->parameters.temperature,
2320 solver->parameters.select ? "EGREEDY" : "SOFTMAX",
2321 solver->parameters.reward_global_share);
2322 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2323 solver->parameters.rbf_divisor);
2329 * Exit point for the plugin
2331 * @param cls the solver handle
2334 libgnunet_plugin_ats_ril_done (void *cls)
2336 struct GAS_RIL_Handle *s = cls;
2337 struct RIL_Peer_Agent *cur_agent;
2338 struct RIL_Peer_Agent *next_agent;
2340 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2342 s->done = GNUNET_YES;
2344 cur_agent = s->agents_head;
2345 while (NULL != cur_agent)
2347 next_agent = cur_agent->next;
2348 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2349 agent_die (s, cur_agent);
2350 cur_agent = next_agent;
2353 if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
2355 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2357 GNUNET_free(s->network_entries);
2364 * Add a new address for a peer to the solver
2366 * The address is already contained in the addresses hashmap!
2368 * @param solver the solver Handle
2369 * @param address the address to add
2370 * @param network network type of this address
2373 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
2375 struct GAS_RIL_Handle *s = solver;
2376 struct RIL_Peer_Agent *agent;
2377 struct RIL_Address_Wrapped *address_wrapped;
2378 struct RIL_Scope *net;
2386 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
2388 net = ril_get_network (s, network);
2389 address->solver_information = net;
2391 if (!ril_network_is_active (s, network))
2393 LOG(GNUNET_ERROR_TYPE_DEBUG,
2394 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2395 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2399 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2402 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2403 address_wrapped->address_naked = address;
2404 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2406 //increase size of W
2407 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2409 n_new = agent->n + 1;
2412 GNUNET_array_grow(agent->W, agent->n, n_new);
2414 GNUNET_array_grow(agent->E, agent->n, n_new);
2415 for (i = 0; i < n_new; i++)
2420 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2422 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2427 GNUNET_array_grow(agent->W[i], zero, m_new);
2429 GNUNET_array_grow(agent->E[i], zero, m_new);
2433 //increase size of old state vector
2435 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2437 ril_try_unblock_agent(s, agent, GNUNET_NO);
2441 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2442 address->active ? "active" : "inactive", address->plugin, address->addr,
2443 GNUNET_i2s (&address->peer));
2447 * Delete an address in the solver
2449 * The address is not contained in the address hashmap anymore!
2451 * @param solver the solver handle
2452 * @param address the address to remove
2453 * @param session_only delete only session not whole address
2456 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
2458 struct GAS_RIL_Handle *s = solver;
2459 struct RIL_Peer_Agent *agent;
2460 struct RIL_Address_Wrapped *address_wrapped;
2461 int address_was_used = address->active;
2466 struct RIL_Scope *net;
2468 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2469 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2470 address->addr, GNUNET_i2s (&address->peer));
2472 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2475 net = address->solver_information;
2476 GNUNET_assert(!ril_network_is_active (s, net->type));
2477 LOG(GNUNET_ERROR_TYPE_DEBUG,
2478 "No agent allocated for peer yet, since address was in inactive network\n");
2482 address_index = agent_address_get_index (agent, address);
2483 address_wrapped = agent_address_get_wrapped (agent, address);
2485 if (NULL == address_wrapped)
2487 net = address->solver_information;
2488 LOG(GNUNET_ERROR_TYPE_DEBUG,
2489 "Address not considered by agent, address was in inactive network\n");
2493 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2494 GNUNET_free(address_wrapped);
2497 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2498 n_new = agent->n - 1;
2500 for (i = 0; i < agent->n; i++)
2502 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2503 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2504 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2505 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2506 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2507 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2509 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2510 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2511 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2513 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2515 //correct last action
2516 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2520 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2522 agent->a_old = RIL_ACTION_INVALID;
2524 //decrease old state vector
2525 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2526 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2527 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2531 if (address_was_used)
2533 if (NULL != agent->addresses_head) //if peer has an address left, use it
2535 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, agent->bw_in, agent->bw_out,
2540 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2548 * Update the properties of an address in the solver
2550 * @param solver solver handle
2551 * @param address the address
2552 * @param type the ATSI type in HBO
2553 * @param abs_value the absolute value of the property
2554 * @param rel_value the normalized value
2557 GAS_ril_address_property_changed (void *solver,
2558 struct ATS_Address *address,
2563 LOG(GNUNET_ERROR_TYPE_DEBUG,
2564 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2565 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2566 address->addr, rel_value);
2572 * Update the session of an address in the solver
2574 * NOTE: values in addresses are already updated
2576 * @param solver solver handle
2577 * @param address the address
2578 * @param cur_session the current session
2579 * @param new_session the new session
2582 GAS_ril_address_session_changed (void *solver,
2583 struct ATS_Address *address,
2584 uint32_t cur_session,
2585 uint32_t new_session)
2587 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2591 * Notify the solver that an address is (not) actively used by transport
2592 * to communicate with a remote peer
2594 * NOTE: values in addresses are already updated
2596 * @param solver solver handle
2597 * @param address the address
2598 * @param in_use usage state
2601 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2603 LOG(GNUNET_ERROR_TYPE_DEBUG,
2604 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2605 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2609 * Notify solver that the network an address is located in has changed
2611 * NOTE: values in addresses are already updated
2613 * @param solver solver handle
2614 * @param address the address
2615 * @param current_network the current network
2616 * @param new_network the new network
2619 GAS_ril_address_change_network (void *solver,
2620 struct ATS_Address *address,
2621 uint32_t current_network,
2622 uint32_t new_network)
2624 struct GAS_RIL_Handle *s = solver;
2625 struct RIL_Peer_Agent *agent;
2627 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2628 "%s address of peer %s from '%s' to '%s'\n",
2629 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2630 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2632 if (address->active && !ril_network_is_active (solver, new_network))
2634 GAS_ril_address_delete (solver, address, GNUNET_NO);
2638 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2641 GNUNET_assert(!ril_network_is_active (solver, current_network));
2643 GAS_ril_address_add (s, address, new_network);
2647 address->solver_information = ril_get_network(solver, new_network);
2651 * Give feedback about the current assignment
2653 * @param solver the solver handle
2654 * @param application the application
2655 * @param peer the peer to change the preference for
2656 * @param scope the time interval for this feedback: [now - scope .. now]
2657 * @param kind the kind to change the preference
2658 * @param score the score
2661 GAS_ril_address_preference_feedback (void *solver,
2663 const struct GNUNET_PeerIdentity *peer,
2664 const struct GNUNET_TIME_Relative scope,
2665 enum GNUNET_ATS_PreferenceKind kind,
2668 LOG(GNUNET_ERROR_TYPE_DEBUG,
2669 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2670 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2671 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2675 * Start a bulk operation
2677 * @param solver the solver
2680 GAS_ril_bulk_start (void *solver)
2682 struct GAS_RIL_Handle *s = solver;
2684 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2690 * Bulk operation done
2692 * @param solver the solver handle
2695 GAS_ril_bulk_stop (void *solver)
2697 struct GAS_RIL_Handle *s = solver;
2699 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2701 if (s->bulk_lock < 1)
2708 if (0 < s->bulk_changes)
2711 s->bulk_changes = 0;
2716 * Tell solver to notify ATS if the address to use changes for a specific
2717 * peer using the bandwidth changed callback
2719 * The solver must only notify about changes for peers with pending address
2722 * @param solver the solver handle
2723 * @param peer the identity of the peer
2725 const struct ATS_Address *
2726 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2729 * activate agent, return currently chosen address
2731 struct GAS_RIL_Handle *s = solver;
2732 struct RIL_Peer_Agent *agent;
2734 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2736 agent = ril_get_agent (s, peer, GNUNET_YES);
2738 agent->is_active = GNUNET_YES;
2739 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2741 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2743 if (agent->address_inuse)
2745 LOG(GNUNET_ERROR_TYPE_DEBUG,
2746 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2747 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2751 LOG(GNUNET_ERROR_TYPE_DEBUG,
2752 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2755 return agent->address_inuse;
2759 * Tell solver stop notifying ATS about changes for this peers
2761 * The solver must only notify about changes for peers with pending address
2764 * @param solver the solver handle
2765 * @param peer the peer
2768 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2770 struct GAS_RIL_Handle *s = solver;
2771 struct RIL_Peer_Agent *agent;
2773 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2775 agent = ril_get_agent (s, peer, GNUNET_NO);
2782 if (GNUNET_NO == agent->is_active)
2788 agent->is_active = GNUNET_NO;
2790 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2795 LOG(GNUNET_ERROR_TYPE_DEBUG,
2796 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2797 GNUNET_i2s (peer), agent->address_inuse->plugin);
2800 /* end of plugin_ats_ril.c */