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 * Last inbound bandwidth used
206 unsigned long long bw_in;
209 * Last outbound bandwidth used
211 unsigned long long bw_out;
215 struct RIL_Peer_Agent
218 * Next agent in solver's linked list
220 struct RIL_Peer_Agent *next;
223 * Previous agent in solver's linked list
225 struct RIL_Peer_Agent *prev;
230 struct GAS_RIL_Handle *envi;
235 struct GNUNET_PeerIdentity peer;
238 * Whether the agent is active or not
243 * Number of performed time-steps
245 unsigned long long step_count;
248 * Experience matrix W
253 * Number of rows of W / Number of state-vector features
258 * Number of columns of W / Number of actions
263 * Last perceived state feature vector
278 * Whether to reset the eligibility traces to 0 after a Q-exploration step
280 int eligibility_reset;
285 struct ATS_Address * address_inuse;
288 * Head of addresses DLL
290 struct RIL_Address_Wrapped * addresses_head;
293 * Tail of addresses DLL
295 struct RIL_Address_Wrapped * addresses_tail;
298 * Inbound bandwidth assigned by the agent
300 unsigned long long bw_in;
303 * Outbound bandwidth assigned by the agent
305 unsigned long long bw_out;
308 * Flag whether a suggestion has to be issued
310 int suggestion_issue;
313 * The address which has to be issued
315 struct ATS_Address * suggestion_address;
318 * The agent's last objective value
320 double objective_old;
333 enum GNUNET_ATS_Network_Type type;
336 * Total available inbound bandwidth
338 unsigned long long bw_in_available;
341 * Bandwidth inbound assigned in network after last step
343 unsigned long long bw_in_assigned;
346 * Bandwidth inbound actually utilized in the network
348 unsigned long long bw_in_utilized;
351 * Total available outbound bandwidth
353 unsigned long long bw_out_available;
356 * Bandwidth outbound assigned in network after last step
358 unsigned long long bw_out_assigned;
361 * Bandwidth outbound actually utilized in the network
363 unsigned long long bw_out_utilized;
366 * Number of active agents in scope
368 unsigned int agent_count;
371 * The social welfare achieved in the scope
373 double social_welfare;
377 * A handle for the reinforcement learning solver
379 struct GAS_RIL_Handle
382 * The solver-plugin environment of the solver-plugin API
384 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
389 struct GNUNET_STATISTICS_Handle *stats;
392 * Number of performed steps
394 unsigned long long step_count;
397 * Timestamp for the last time-step
399 struct GNUNET_TIME_Absolute step_time_last;
402 * Task identifier of the next time-step to be executed
404 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
407 * Variable discount factor, dependent on time between steps
409 double global_discount_variable;
412 * Integrated variable discount factor, dependent on time between steps
414 double global_discount_integrated;
417 * Lock for bulk operations
422 * Number of changes during a lock
427 * Learning parameters
429 struct RIL_Learning_Parameters parameters;
432 * Array of networks with global assignment state
434 struct RIL_Scope * network_entries;
439 unsigned int networks_count;
442 * List of active peer-agents
444 struct RIL_Peer_Agent * agents_head;
445 struct RIL_Peer_Agent * agents_tail;
453 * Simulate steps, i.e. schedule steps immediately
455 unsigned long long simulate;
459 * "Private" functions
460 * ---------------------------
464 * Estimate the current action-value for state s and action a
466 * @param agent agent performing the estimation
469 * @return estimation value
472 agent_q (struct RIL_Peer_Agent *agent, double *state, int action)
477 for (i = 0; i < agent->m; i++)
479 result += state[i] * agent->W[action][i];
482 GNUNET_assert(!isnan(result));
484 //prevent crash when learning diverges
487 return isinf(result) * UINT32_MAX;
494 * Get the index of the address in the agent's list.
496 * @param agent agent handle
497 * @param address address handle
498 * @return the index, starting with zero
501 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
504 struct RIL_Address_Wrapped *cur;
507 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
510 if (cur->address_naked == address)
518 * Gets the wrapped address from the agent's list
520 * @param agent agent handle
521 * @param address address handle
522 * @return wrapped address
524 static struct RIL_Address_Wrapped *
525 agent_address_get_wrapped (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
527 struct RIL_Address_Wrapped *cur;
529 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
530 if (cur->address_naked == address)
537 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
543 case RIL_ACTION_NOTHING:
546 case RIL_ACTION_BW_IN_INC:
547 case RIL_ACTION_BW_IN_DBL:
548 if (agent->bw_in >= RIL_MAX_BW)
553 case RIL_ACTION_BW_IN_DEC:
554 case RIL_ACTION_BW_IN_HLV:
555 if (agent->bw_in <= RIL_MIN_BW)
560 case RIL_ACTION_BW_OUT_INC:
561 case RIL_ACTION_BW_OUT_DBL:
562 if (agent->bw_out >= RIL_MAX_BW)
567 case RIL_ACTION_BW_OUT_DEC:
568 case RIL_ACTION_BW_OUT_HLV:
569 if (agent->bw_out <= RIL_MIN_BW)
575 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
577 address_index = action - RIL_ACTION_TYPE_NUM;
579 GNUNET_assert(address_index >= 0);
581 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
583 if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
584 agent->address_inuse->active)
590 // error - action does not exist
591 GNUNET_assert(GNUNET_NO);
597 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
598 * most reward in the future)
600 * @param agent agent performing the calculation
601 * @param state the state from which to take the action
602 * @return the action promising most future reward
605 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
608 int max_i = RIL_ACTION_INVALID;
610 double max_q = -DBL_MAX;
612 for (i = 0; i < agent->n; i++)
614 if (agent_action_is_possible(agent, i))
616 cur_q = agent_q (agent, state, i);
625 GNUNET_assert(RIL_ACTION_INVALID != max_i);
631 * Chooses a random action from the set of possible ones
633 * @param agent the agent performing the action
634 * @return the action index
637 agent_get_action_random (struct RIL_Peer_Agent *agent)
640 int is_possible[agent->n];
644 for (i = 0; i<agent->n; i++)
646 if (agent_action_is_possible(agent, i))
648 is_possible[i] = GNUNET_YES;
653 is_possible[i] = GNUNET_NO;
657 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
660 for (i = 0; i<agent->n; i++)
670 GNUNET_assert(GNUNET_NO);
675 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
677 * @param agent the agent performing the update
678 * @param reward the reward received for the last action
679 * @param s_next the new state, the last step got the agent into
680 * @param a_prime the new
683 agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
688 double **theta = agent->W;
690 delta = agent->envi->global_discount_integrated * reward; //reward
691 delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
692 delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
694 // 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",
695 // agent->step_count,
696 // agent_q (agent, agent->s_old, agent->a_old),
697 // agent->envi->parameters.alpha,
699 // agent->envi->global_discount_variable,
700 // agent_q (agent, s_next, a_prime),
703 for (k = 0; k < agent->n; k++)
705 for (i = 0; i < agent->m; i++)
707 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
708 // agent->envi->parameters.alpha,
712 theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
719 * Changes the eligibility trace vector e in various manners:
720 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
721 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
722 * #RIL_E_DISCOUNT - multiplies e with discount factor and lambda as in the update rule
723 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
725 * @param agent the agent handle
726 * @param mod the kind of modification
727 * @param feature the feature vector
730 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
731 enum RIL_E_Modification mod,
738 for (i = 0; i < agent->m; i++)
742 case RIL_E_ACCUMULATE:
743 agent->E[action][i] += feature[i];
746 agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
749 for (k = 0; k < agent->n; k++)
751 agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
755 for (k = 0; k < agent->n; k++)
765 * Informs the environment about the status of the solver
772 ril_inform (struct GAS_RIL_Handle *solver,
773 enum GAS_Solver_Operation op,
774 enum GAS_Solver_Status stat)
776 if (NULL != solver->plugin_envi->info_cb)
777 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
782 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
783 * notify ATS of its new decision
785 * @param solver solver handle
786 * @param agent agent handle
787 * @param new_address the address which is to be used
788 * @param new_bw_in the new amount of inbound bandwidth set for this address
789 * @param new_bw_out the new amount of outbound bandwidth set for this address
790 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
793 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
794 struct RIL_Peer_Agent *agent,
795 struct ATS_Address *new_address,
796 unsigned long long new_bw_in,
797 unsigned long long new_bw_out,
800 int notify = GNUNET_NO;
802 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
805 if (agent->address_inuse != new_address)
807 if (NULL != agent->address_inuse)
809 agent->address_inuse->active = GNUNET_NO;
810 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
811 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
813 if (NULL != new_address)
815 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
816 new_address->active = agent->is_active;
817 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
818 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
820 notify |= GNUNET_YES;
826 if (new_address->active != agent->is_active)
828 new_address->active = agent->is_active;
829 notify |= GNUNET_YES;
833 if (agent->bw_in != new_bw_in)
835 agent->bw_in = new_bw_in;
836 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
837 notify |= GNUNET_YES;
839 if (agent->bw_out != new_bw_out)
841 agent->bw_out = new_bw_out;
842 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
843 notify |= GNUNET_YES;
847 if (notify && agent->is_active && (GNUNET_NO == silent))
851 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
852 agent->suggestion_issue = GNUNET_YES;
853 agent->suggestion_address = new_address;
855 else if (agent->address_inuse)
857 //disconnect case, no new address
858 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
859 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
863 agent->suggestion_issue = GNUNET_YES;
864 agent->suggestion_address = agent->address_inuse;
867 agent->address_inuse = new_address;
872 * Allocates a state vector and fills it with the features present
873 * @param solver the solver handle
874 * @param agent the agent handle
875 * @return pointer to the state vector
878 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
889 unsigned long long max_bw;
891 state = GNUNET_malloc (sizeof(double) * agent->m);
895 y[0] = (double) agent->bw_out;
896 y[1] = (double) agent->bw_in;
898 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
899 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
901 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
903 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
904 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
907 sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
908 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
917 * Retrieves an ATS information value of an address
919 * @param address the address in question
920 * @param type the ATS information type
924 ril_get_atsi (struct ATS_Address *address, uint32_t type)
927 GNUNET_assert(NULL != address);
929 if ((NULL == address->atsi) || (0 == address->atsi_count))
930 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
932 for (c1 = 0; c1 < address->atsi_count; c1++)
934 if (ntohl (address->atsi[c1].type) == type)
935 return ntohl (address->atsi[c1].value);
937 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
941 * Returns the utility value of the connection an agent manages
943 * @param agent the agent in question
944 * @return the utility value
947 agent_get_utility (struct RIL_Peer_Agent *agent)
949 const double *preferences;
954 preferences = agent->envi->plugin_envi->get_preferences (agent->envi->plugin_envi->get_preference_cls,
957 delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
958 delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
960 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
961 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
962 sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
964 // return (double) (agent->bw_in/RIL_MIN_BW);
965 // return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
970 * Calculates the social welfare within a network scope according to what social
971 * welfare measure is set in the configuration.
973 * @param solver the solver handle
974 * @param scope the network scope in question
975 * @return the social welfare value
978 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
980 struct RIL_Peer_Agent *cur;
983 switch (solver->parameters.social_welfare)
985 case RIL_WELFARE_EGALITARIAN:
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 = GNUNET_MIN(result, agent_get_utility(cur));
996 case RIL_WELFARE_NASH:
998 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1000 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1002 result *= pow(agent_get_utility(cur), 1.0 / (double) scope->agent_count);
1007 GNUNET_assert(GNUNET_NO);
1012 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1014 struct RIL_Scope *net;
1015 unsigned long long over_max;
1016 unsigned long long over_in = 0;
1017 unsigned long long over_out = 0;
1019 net = agent->address_inuse->solver_information;
1021 if (net->bw_in_utilized > net->bw_in_available)
1023 over_in = net->bw_in_utilized - net->bw_in_available;
1024 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1029 if (net->bw_out_utilized > net->bw_out_available)
1031 over_out = net->bw_out_utilized - net->bw_out_available;
1032 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1037 over_max = GNUNET_MAX (over_in , over_out) / RIL_MIN_BW;
1039 return -1.0 * (double) over_max;
1043 * Gets the reward for the last performed step, which is calculated in equal
1044 * parts from the local (the peer specific) and the global (for all peers
1045 * identical) reward.
1047 * @param solver the solver handle
1048 * @param agent the agent handle
1049 * @return the reward
1052 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1054 struct RIL_Scope *net;
1062 net = agent->address_inuse->solver_information;
1064 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1065 delta = objective - agent->objective_old;
1066 agent->objective_old = objective;
1070 agent->nop_bonus = delta * RIL_NOP_DECAY;
1074 agent->nop_bonus *= RIL_NOP_DECAY;
1077 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1080 penalty = envi_get_penalty(solver, agent);
1082 reward = delta + steady;
1083 return ((1 - pen_share) * reward) + (pen_share * penalty);
1087 * Doubles the bandwidth for the active address
1089 * @param solver solver handle
1090 * @param agent agent handle
1091 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1094 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1095 struct RIL_Peer_Agent *agent,
1098 unsigned long long new_bw;
1102 new_bw = agent->bw_in * 2;
1103 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1104 new_bw = RIL_MAX_BW;
1105 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1106 agent->bw_out, GNUNET_NO);
1110 new_bw = agent->bw_out * 2;
1111 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1112 new_bw = RIL_MAX_BW;
1113 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1119 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1120 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1122 * @param solver solver handle
1123 * @param agent agent handle
1124 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1128 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1129 struct RIL_Peer_Agent *agent,
1132 unsigned long long new_bw;
1136 new_bw = agent->bw_in / 2;
1137 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1138 new_bw = RIL_MIN_BW;
1139 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1144 new_bw = agent->bw_out / 2;
1145 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1146 new_bw = RIL_MIN_BW;
1147 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1153 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1155 * @param solver solver handle
1156 * @param agent agent handle
1157 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1161 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1163 unsigned long long new_bw;
1167 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1168 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1169 new_bw = RIL_MAX_BW;
1170 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1171 agent->bw_out, GNUNET_NO);
1175 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1176 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1177 new_bw = RIL_MAX_BW;
1178 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1184 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1185 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1187 * @param solver solver handle
1188 * @param agent agent handle
1189 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1193 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1195 unsigned long long new_bw;
1199 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1200 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1201 new_bw = RIL_MIN_BW;
1202 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1207 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1208 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1209 new_bw = RIL_MIN_BW;
1210 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1216 * Switches to the address given by its index
1218 * @param solver solver handle
1219 * @param agent agent handle
1220 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1223 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1224 struct RIL_Peer_Agent *agent,
1225 unsigned int address_index)
1227 struct RIL_Address_Wrapped *cur;
1230 cur = agent_address_get_wrapped(agent, agent->address_inuse);
1231 cur->bw_in = agent->bw_in;
1232 cur->bw_out = agent->bw_out;
1234 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1236 if (i == address_index)
1238 envi_set_active_suggestion (solver, agent, cur->address_naked, cur->bw_in, cur->bw_out,
1246 //no address with address_index exists, in this case this action should not be callable
1247 GNUNET_assert(GNUNET_NO);
1251 * Puts the action into effect by calling the according function
1253 * @param solver the solver handle
1254 * @param agent the action handle
1255 * @param action the action to perform by the solver
1258 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1264 case RIL_ACTION_NOTHING:
1266 case RIL_ACTION_BW_IN_DBL:
1267 envi_action_bw_double (solver, agent, GNUNET_YES);
1269 case RIL_ACTION_BW_IN_HLV:
1270 envi_action_bw_halven (solver, agent, GNUNET_YES);
1272 case RIL_ACTION_BW_IN_INC:
1273 envi_action_bw_inc (solver, agent, GNUNET_YES);
1275 case RIL_ACTION_BW_IN_DEC:
1276 envi_action_bw_dec (solver, agent, GNUNET_YES);
1278 case RIL_ACTION_BW_OUT_DBL:
1279 envi_action_bw_double (solver, agent, GNUNET_NO);
1281 case RIL_ACTION_BW_OUT_HLV:
1282 envi_action_bw_halven (solver, agent, GNUNET_NO);
1284 case RIL_ACTION_BW_OUT_INC:
1285 envi_action_bw_inc (solver, agent, GNUNET_NO);
1287 case RIL_ACTION_BW_OUT_DEC:
1288 envi_action_bw_dec (solver, agent, GNUNET_NO);
1291 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1293 address_index = action - RIL_ACTION_TYPE_NUM;
1295 GNUNET_assert(address_index >= 0);
1297 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1299 envi_action_address_switch (solver, agent, address_index);
1302 // error - action does not exist
1303 GNUNET_assert(GNUNET_NO);
1308 * Selects the next action using the e-greedy strategy. I.e. with a probability
1309 * of (1-e) the action with the maximum expected return will be chosen
1310 * (=> exploitation) and with probability (e) a random action will be chosen.
1311 * In case the Q-learning rule is set, the function also resets the eligibility
1312 * traces in the exploration case (after Watkin's Q-learning).
1314 * @param agent the agent selecting an action
1315 * @param state the current state-feature vector
1316 * @return the action index
1319 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1322 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1323 UINT32_MAX) / (double) UINT32_MAX;
1325 if (r < agent->envi->parameters.explore_ratio) //explore
1327 action = agent_get_action_random(agent);
1328 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1330 agent->eligibility_reset = GNUNET_YES;
1336 action = agent_get_action_max(agent, state);
1342 * Selects the next action with a probability corresponding to its value. The
1343 * probability is calculated using a Boltzmann distribution with a temperature
1344 * value. The higher the temperature, the more are the action selection
1345 * probabilities the same. With a temperature of 0, the selection is greedy,
1346 * i.e. always the action with the highest value is chosen.
1352 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1356 double eqt[agent->n];
1361 a_max = agent_get_action_max(agent, state);
1363 for (i=0; i<agent->n; i++)
1365 if (agent_action_is_possible(agent, i))
1367 eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1371 for (i=0; i<agent->n; i++)
1373 if (agent_action_is_possible(agent, i))
1382 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1383 UINT32_MAX) / (double) UINT32_MAX;
1385 for (i=0; i<agent->n; i++)
1389 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1392 agent->eligibility_reset = GNUNET_YES;
1398 GNUNET_assert(GNUNET_NO);
1402 * Select the next action of an agent either according to the e-greedy strategy
1403 * or the softmax strategy.
1405 * @param agent the agent in question
1406 * @param state the current state-feature vector
1407 * @return the action index
1410 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1412 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1414 return agent_select_egreedy(agent, state);
1418 return agent_select_softmax(agent, state);
1423 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1424 * after having done the last action a_old. It observes the new state s_next and the reward
1425 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1426 * next action is put into effect.
1428 * @param agent the agent performing the step
1431 agent_step (struct RIL_Peer_Agent *agent)
1433 int a_next = RIL_ACTION_INVALID;
1438 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1439 GNUNET_i2s (&agent->peer),
1440 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1442 s_next = envi_get_state (agent->envi, agent);
1443 reward = envi_get_reward (agent->envi, agent);
1445 if (agent->eligibility_reset)
1447 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1448 agent->eligibility_reset = GNUNET_NO;
1452 agent_modify_eligibility (agent, RIL_E_DISCOUNT, NULL, -1);
1454 if (RIL_ACTION_INVALID != agent->a_old)
1456 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1459 switch (agent->envi->parameters.algorithm)
1461 case RIL_ALGO_SARSA:
1462 a_next = agent_select_action (agent, s_next);
1463 if (RIL_ACTION_INVALID != agent->a_old)
1465 //updates weights with selected action (on-policy), if not first step
1466 agent_update (agent, reward, s_next, a_next);
1471 a_max = agent_get_action_max (agent, s_next);
1472 if (RIL_ACTION_INVALID != agent->a_old)
1474 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1475 agent_update (agent, reward, s_next, a_max);
1477 a_next = agent_select_action (agent, s_next);
1481 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1483 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1490 envi_do_action (agent->envi, agent, a_next);
1492 GNUNET_free(agent->s_old);
1493 agent->s_old = s_next;
1494 agent->a_old = a_next;
1496 agent->step_count += 1;
1500 * Prototype of the ril_step() procedure
1502 * @param solver the solver handle
1505 ril_step (struct GAS_RIL_Handle *solver);
1508 * Task for the scheduler, which performs one step and lets the solver know that
1509 * no further step is scheduled.
1511 * @param cls the solver handle
1512 * @param tc the task context for the scheduler
1515 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1517 struct GAS_RIL_Handle *solver = cls;
1519 solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1524 * Determines how much of the available bandwidth is assigned. If more is
1525 * assigned than available it returns 1. The function is used to determine the
1526 * step size of the adaptive stepping.
1528 * @param solver the solver handle
1532 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1535 struct RIL_Scope net;
1536 unsigned long long sum_assigned = 0;
1537 unsigned long long sum_available = 0;
1540 for (i = 0; i < solver->networks_count; i++)
1542 net = solver->network_entries[i];
1543 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1545 sum_assigned += net.bw_in_utilized;
1546 sum_assigned += net.bw_out_utilized;
1547 sum_available += net.bw_in_available;
1548 sum_available += net.bw_out_available;
1551 if (sum_available > 0)
1553 ratio = ((double) sum_assigned) / ((double) sum_available);
1560 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1564 * Lookup network struct by type
1566 * @param s the solver handle
1567 * @param type the network type
1568 * @return the network struct
1570 static struct RIL_Scope *
1571 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1575 for (i = 0; i < s->networks_count; i++)
1577 if (s->network_entries[i].type == type)
1579 return &s->network_entries[i];
1586 * Determines whether more connections are allocated in a network scope, than
1587 * they would theoretically fit. This is used as a heuristic to determine,
1588 * whether a new connection can be allocated or not.
1590 * @param solver the solver handle
1591 * @param network the network scope in question
1592 * @return GNUNET_YES if there are theoretically enough resources left
1595 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1597 struct RIL_Scope *net;
1598 struct RIL_Peer_Agent *agent;
1599 unsigned long long address_count = 0;
1601 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1603 if (agent->address_inuse && agent->is_active)
1605 net = agent->address_inuse->solver_information;
1606 if (net->type == network)
1613 net = ril_get_network (solver, network);
1614 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1618 * Unblocks an agent for which a connection request is there, that could not
1619 * be satisfied. Iterates over the addresses of the agent, if one of its
1620 * addresses can now be allocated in its scope the agent is unblocked,
1621 * otherwise it remains unchanged.
1623 * @param solver the solver handle
1624 * @param agent the agent in question
1628 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1630 struct RIL_Address_Wrapped *addr_wrap;
1631 struct RIL_Scope *net;
1633 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1635 net = addr_wrap->address_naked->solver_information;
1636 if (ril_network_is_not_full(solver, net->type))
1638 if (NULL == agent->address_inuse)
1639 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, addr_wrap->bw_in, addr_wrap->bw_out, silent);
1643 agent->address_inuse = NULL;
1647 * Determines how much the reward needs to be discounted depending on the amount
1648 * of time, which has passed since the last time-step.
1650 * @param solver the solver handle
1653 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1655 struct GNUNET_TIME_Absolute time_now;
1656 struct GNUNET_TIME_Relative time_delta;
1659 // MDP case - TODO! remove when debugged and test SMDP case
1660 if (solver->simulate)
1662 solver->global_discount_variable = solver->parameters.gamma;
1663 solver->global_discount_integrated = 1;
1669 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1670 time_now = GNUNET_TIME_absolute_get ();
1671 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1672 solver->step_time_last = time_now;
1673 tau = (double) time_delta.rel_value_us
1674 / (double) solver->parameters.step_time_min.rel_value_us;
1676 //calculate reward discounts (once per step for all agents)
1677 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1678 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1679 / (double) solver->parameters.beta;
1683 * Count the number of active agents/connections in a network scope
1685 * @param solver the solver handle
1686 * @param scope the network scope in question
1687 * @return the number of allocated connections
1690 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1693 struct RIL_Peer_Agent *cur_agent;
1695 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1697 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1706 * Calculates how much bandwidth is assigned in sum in a network scope, either
1707 * in the inbound or in the outbound direction.
1709 * @param solver the solver handle
1710 * @param type the type of the network scope in question
1711 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1712 * otherwise the outbound direction will be summed up
1713 * @return the sum of the assigned bandwidths
1715 static unsigned long long
1716 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1718 struct RIL_Peer_Agent *cur;
1719 struct RIL_Scope *net;
1720 unsigned long long sum = 0;
1722 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1724 if (cur->is_active && cur->address_inuse)
1726 net = cur->address_inuse->solver_information;
1727 if (net->type == type)
1741 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1742 * either in the inbound or in the outbound direction.
1744 * @param solver the solver handle
1745 * @param type the type of the network scope in question
1746 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1747 * otherwise the outbound direction will be summed up
1748 * @return the sum of the utilized bandwidths (in bytes/second)
1750 static unsigned long long
1751 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1753 struct RIL_Peer_Agent *cur;
1754 struct RIL_Scope *net;
1755 unsigned long long sum = 0;
1757 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1759 if (cur->is_active && cur->address_inuse)
1761 net = cur->address_inuse->solver_information;
1762 if (net->type == type)
1765 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
1767 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
1776 * Retrieves the state of the network scope, so that its attributes are up-to-
1779 * @param solver the solver handle
1782 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1785 struct RIL_Scope *net;
1787 for (c = 0; c < solver->networks_count; c++)
1789 net = &solver->network_entries[c];
1790 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1791 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1792 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1793 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1794 net->agent_count = ril_network_count_active_agents(solver, net);
1795 net->social_welfare = ril_network_get_social_welfare(solver, net);
1800 * Schedules the next global step in an adaptive way. The more resources are
1801 * left, the earlier the next step is scheduled. This serves the reactivity of
1802 * the solver to changed inputs.
1804 * @param solver the solver handle
1807 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1813 struct GNUNET_TIME_Relative time_next;
1815 used_ratio = ril_get_used_resource_ratio (solver);
1818 solver->parameters.step_time_min.rel_value_us
1819 <= solver->parameters.step_time_max.rel_value_us);
1821 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1822 solver->parameters.step_time_min).rel_value_us;
1823 offset = (double) solver->parameters.step_time_min.rel_value_us;
1824 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1826 GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
1827 GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
1829 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1831 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1837 if (solver->simulate)
1839 time_next = GNUNET_TIME_UNIT_ZERO;
1842 if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1844 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1850 * Triggers one step per agent
1855 ril_step (struct GAS_RIL_Handle *solver)
1857 struct RIL_Peer_Agent *cur;
1859 if (GNUNET_YES == solver->bulk_lock)
1861 solver->bulk_changes++;
1865 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1867 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1869 if (0 == solver->step_count)
1871 solver->step_time_last = GNUNET_TIME_absolute_get ();
1874 ril_calculate_discount (solver);
1875 ril_networks_update_state (solver);
1877 //trigger one step per active, unblocked agent
1878 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1882 if (NULL == cur->address_inuse)
1884 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1886 if (cur->address_inuse)
1893 ril_networks_update_state (solver);
1895 solver->step_count += 1;
1896 ril_step_schedule_next (solver);
1898 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1900 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1901 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1903 if (cur->suggestion_issue) {
1904 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1905 cur->suggestion_issue = GNUNET_NO;
1908 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1912 * Initializes the matrix W of parameter vectors theta with small random numbers.
1914 * @param agent The respective agent
1917 agent_w_init (struct RIL_Peer_Agent *agent)
1922 for (i = 0; i < agent->n; i++)
1924 for (k = 0; k < agent->m; k++)
1926 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));
1932 * Initialize an agent without addresses and its knowledge base
1934 * @param s ril solver
1935 * @param peer the one in question
1936 * @return handle to the new agent
1938 static struct RIL_Peer_Agent *
1939 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1942 struct GAS_RIL_Handle * solver = s;
1943 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1945 agent->envi = solver;
1946 agent->peer = *peer;
1947 agent->step_count = 0;
1948 agent->is_active = GNUNET_NO;
1949 agent->bw_in = RIL_MIN_BW;
1950 agent->bw_out = RIL_MIN_BW;
1951 agent->suggestion_issue = GNUNET_NO;
1952 agent->n = RIL_ACTION_TYPE_NUM;
1954 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1955 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1956 for (i = 0; i < agent->n; i++)
1958 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1959 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1961 agent_w_init(agent);
1962 agent->eligibility_reset = GNUNET_NO;
1963 agent->a_old = RIL_ACTION_INVALID;
1964 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1965 agent->address_inuse = NULL;
1966 agent->objective_old = 0;
1967 agent->nop_bonus = 0;
1975 * @param solver the solver handle
1976 * @param agent the agent to retire
1979 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1983 for (i = 0; i < agent->n; i++)
1985 GNUNET_free_non_null(agent->W[i]);
1986 GNUNET_free_non_null(agent->E[i]);
1988 GNUNET_free_non_null(agent->W);
1989 GNUNET_free_non_null(agent->E);
1990 GNUNET_free_non_null(agent->s_old);
1995 * Returns the agent for a peer
1997 * @param solver the solver handle
1998 * @param peer the identity of the peer
1999 * @param create whether or not to create an agent, if none is allocated yet
2002 static struct RIL_Peer_Agent *
2003 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
2005 struct RIL_Peer_Agent *cur;
2007 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2009 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
2017 cur = agent_init (solver, peer);
2018 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
2025 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
2026 * considered inactive and not used. Addresses in an inactive network are ignored.
2028 * @param solver solver handle
2029 * @param network the network type
2030 * @return whether or not the network is considered active
2033 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
2035 struct RIL_Scope *net;
2037 net = ril_get_network (solver, network);
2038 return net->bw_out_available >= RIL_MIN_BW;
2042 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
2043 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
2044 * frees the memory of the old vector, and redirects the pointer to the new one.
2046 * @param old pointer to the pointer to the first element of the vector
2047 * @param element_size byte size of the vector elements
2048 * @param hole_start the first element to cut out
2049 * @param hole_length the number of elements to cut out
2050 * @param old_length the length of the old vector
2053 ril_cut_from_vector (void **old,
2054 size_t element_size,
2055 unsigned int hole_start,
2056 unsigned int hole_length,
2057 unsigned int old_length)
2060 char *oldptr = (char *) *old;
2062 unsigned int bytes_before;
2063 unsigned int bytes_hole;
2064 unsigned int bytes_after;
2066 GNUNET_assert(old_length >= hole_length);
2067 GNUNET_assert(old_length >= (hole_start + hole_length));
2069 size = element_size * (old_length - hole_length);
2071 bytes_before = element_size * hole_start;
2072 bytes_hole = element_size * hole_length;
2073 bytes_after = element_size * (old_length - hole_start - hole_length);
2081 tmpptr = GNUNET_malloc (size);
2082 memcpy (tmpptr, oldptr, bytes_before);
2083 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2089 *old = (void *) tmpptr;
2093 * Solver API functions
2094 * ---------------------------
2098 * Change relative preference for quality in solver
2100 * @param solver the solver handle
2101 * @param peer the peer to change the preference for
2102 * @param kind the kind to change the preference
2103 * @param pref_rel the normalized preference value for this kind over all clients
2106 GAS_ril_address_change_preference (void *solver,
2107 const struct GNUNET_PeerIdentity *peer,
2108 enum GNUNET_ATS_PreferenceKind kind,
2111 LOG(GNUNET_ERROR_TYPE_DEBUG,
2112 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2113 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2119 * Entry point for the plugin
2121 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2124 libgnunet_plugin_ats_ril_init (void *cls)
2126 struct GNUNET_ATS_PluginEnvironment *env = cls;
2127 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2128 struct RIL_Scope * cur;
2132 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
2134 GNUNET_assert(NULL != env);
2135 GNUNET_assert(NULL != env->cfg);
2136 GNUNET_assert(NULL != env->stats);
2137 GNUNET_assert(NULL != env->bandwidth_changed_cb);
2138 GNUNET_assert(NULL != env->get_preferences);
2139 GNUNET_assert(NULL != env->get_property);
2141 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2143 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2146 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2147 &solver->parameters.step_time_min))
2149 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2152 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2153 &solver->parameters.step_time_max))
2155 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2157 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2159 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
2160 GNUNET_free (string);
2164 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2166 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2168 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2169 GNUNET_free (string);
2173 solver->parameters.select = RIL_DEFAULT_SELECT;
2175 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
2177 solver->parameters.beta = strtod (string, NULL);
2178 GNUNET_free (string);
2179 if (!(solver->parameters.beta > 0))
2181 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);
2182 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2187 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2189 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
2191 solver->parameters.gamma = strtod (string, NULL);
2192 GNUNET_free (string);
2193 if (!(solver->parameters.gamma < 1) || (solver->parameters.gamma < 0))
2195 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);
2196 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2201 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2204 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
2206 solver->parameters.alpha = strtod (string, NULL);
2207 GNUNET_free (string);
2208 if (!(solver->parameters.alpha > 0) || solver->parameters.alpha > 1)
2210 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);
2211 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2216 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2218 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
2220 solver->parameters.lambda = strtod (string, NULL);
2221 GNUNET_free (string);
2222 if (solver->parameters.lambda < 0 || solver->parameters.lambda > 1)
2224 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);
2225 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2230 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2232 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
2234 solver->parameters.explore_ratio = strtod (string, NULL);
2235 GNUNET_free (string);
2236 if (solver->parameters.explore_ratio < 0 || solver->parameters.explore_ratio > 1)
2238 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);
2239 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
2244 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
2246 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
2248 solver->parameters.reward_global_share = strtod (string, NULL);
2249 GNUNET_free (string);
2250 if (solver->parameters.reward_global_share < 0 || solver->parameters.reward_global_share > 1)
2252 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);
2253 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
2258 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
2260 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE", &string))
2262 solver->parameters.temperature = strtod (string, NULL);
2263 GNUNET_free (string);
2264 if (solver->parameters.temperature <= 0)
2266 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE not positive. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE);
2267 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
2272 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
2274 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2276 solver->simulate = GNUNET_NO;
2278 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2280 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2284 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2286 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2288 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2289 GNUNET_free (string);
2293 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2296 env->sf.s_add = &GAS_ril_address_add;
2297 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
2298 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
2299 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
2300 env->sf.s_address_update_network = &GAS_ril_address_change_network;
2301 env->sf.s_get = &GAS_ril_get_preferred_address;
2302 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2303 env->sf.s_pref = &GAS_ril_address_change_preference;
2304 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
2305 env->sf.s_del = &GAS_ril_address_delete;
2306 env->sf.s_bulk_start = &GAS_ril_bulk_start;
2307 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
2309 solver->plugin_envi = env;
2310 solver->networks_count = env->network_count;
2311 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2312 solver->step_count = 0;
2313 solver->done = GNUNET_NO;
2315 for (c = 0; c < env->network_count; c++)
2317 cur = &solver->network_entries[c];
2318 cur->type = env->networks[c];
2319 cur->bw_in_available = env->in_quota[c];
2320 cur->bw_out_available = env->out_quota[c];
2321 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);
2324 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2325 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2326 solver->parameters.algorithm ? "Q" : "SARSA",
2327 solver->parameters.alpha,
2328 solver->parameters.beta,
2329 solver->parameters.lambda);
2330 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s, global_share = %f\n",
2331 solver->parameters.explore_ratio,
2332 solver->parameters.temperature,
2333 solver->parameters.select ? "EGREEDY" : "SOFTMAX",
2334 solver->parameters.reward_global_share);
2335 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2336 solver->parameters.rbf_divisor);
2342 * Exit point for the plugin
2344 * @param cls the solver handle
2347 libgnunet_plugin_ats_ril_done (void *cls)
2349 struct GAS_RIL_Handle *s = cls;
2350 struct RIL_Peer_Agent *cur_agent;
2351 struct RIL_Peer_Agent *next_agent;
2353 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2355 s->done = GNUNET_YES;
2357 cur_agent = s->agents_head;
2358 while (NULL != cur_agent)
2360 next_agent = cur_agent->next;
2361 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2362 agent_die (s, cur_agent);
2363 cur_agent = next_agent;
2366 if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
2368 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2370 GNUNET_free(s->network_entries);
2377 * Add a new address for a peer to the solver
2379 * The address is already contained in the addresses hashmap!
2381 * @param solver the solver Handle
2382 * @param address the address to add
2383 * @param network network type of this address
2386 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
2388 struct GAS_RIL_Handle *s = solver;
2389 struct RIL_Peer_Agent *agent;
2390 struct RIL_Address_Wrapped *address_wrapped;
2391 struct RIL_Scope *net;
2399 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
2401 net = ril_get_network (s, network);
2402 address->solver_information = net;
2404 if (!ril_network_is_active (s, network))
2406 LOG(GNUNET_ERROR_TYPE_DEBUG,
2407 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2408 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2412 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2415 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2416 address_wrapped->address_naked = address;
2417 address_wrapped->bw_in = net->bw_in_available > net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
2418 address_wrapped->bw_out = net->bw_out_available > net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
2419 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2421 //increase size of W
2422 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2424 n_new = agent->n + 1;
2427 GNUNET_array_grow(agent->W, agent->n, n_new);
2429 GNUNET_array_grow(agent->E, agent->n, n_new);
2430 for (i = 0; i < n_new; i++)
2435 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2437 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2442 GNUNET_array_grow(agent->W[i], zero, m_new);
2444 GNUNET_array_grow(agent->E[i], zero, m_new);
2448 //increase size of old state vector
2450 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2452 ril_try_unblock_agent(s, agent, GNUNET_NO);
2456 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2457 address->active ? "active" : "inactive", address->plugin, address->addr,
2458 GNUNET_i2s (&address->peer));
2462 * Delete an address in the solver
2464 * The address is not contained in the address hashmap anymore!
2466 * @param solver the solver handle
2467 * @param address the address to remove
2468 * @param session_only delete only session not whole address
2471 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
2473 struct GAS_RIL_Handle *s = solver;
2474 struct RIL_Peer_Agent *agent;
2475 struct RIL_Address_Wrapped *address_wrapped;
2476 int address_was_used = address->active;
2481 struct RIL_Scope *net;
2483 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2484 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2485 address->addr, GNUNET_i2s (&address->peer));
2487 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2490 net = address->solver_information;
2491 GNUNET_assert(!ril_network_is_active (s, net->type));
2492 LOG(GNUNET_ERROR_TYPE_DEBUG,
2493 "No agent allocated for peer yet, since address was in inactive network\n");
2497 address_index = agent_address_get_index (agent, address);
2498 address_wrapped = agent_address_get_wrapped (agent, address);
2500 if (NULL == address_wrapped)
2502 net = address->solver_information;
2503 LOG(GNUNET_ERROR_TYPE_DEBUG,
2504 "Address not considered by agent, address was in inactive network\n");
2508 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2509 GNUNET_free(address_wrapped);
2512 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2513 n_new = agent->n - 1;
2515 for (i = 0; i < agent->n; i++)
2517 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2518 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2519 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2520 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2521 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2522 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2524 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2525 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2526 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2528 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2530 //correct last action
2531 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2535 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2537 agent->a_old = RIL_ACTION_INVALID;
2539 //decrease old state vector
2540 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2541 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2542 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2546 if (address_was_used)
2548 if (NULL != agent->addresses_head) //if peer has an address left, use it
2550 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, agent->addresses_head->bw_in, agent->addresses_head->bw_out,
2555 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2563 * Update the properties of an address in the solver
2565 * @param solver solver handle
2566 * @param address the address
2567 * @param type the ATSI type in HBO
2568 * @param abs_value the absolute value of the property
2569 * @param rel_value the normalized value
2572 GAS_ril_address_property_changed (void *solver,
2573 struct ATS_Address *address,
2578 LOG(GNUNET_ERROR_TYPE_DEBUG,
2579 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2580 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2581 address->addr, rel_value);
2587 * Update the session of an address in the solver
2589 * NOTE: values in addresses are already updated
2591 * @param solver solver handle
2592 * @param address the address
2593 * @param cur_session the current session
2594 * @param new_session the new session
2597 GAS_ril_address_session_changed (void *solver,
2598 struct ATS_Address *address,
2599 uint32_t cur_session,
2600 uint32_t new_session)
2602 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2606 * Notify the solver that an address is (not) actively used by transport
2607 * to communicate with a remote peer
2609 * NOTE: values in addresses are already updated
2611 * @param solver solver handle
2612 * @param address the address
2613 * @param in_use usage state
2616 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2618 LOG(GNUNET_ERROR_TYPE_DEBUG,
2619 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2620 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2624 * Notify solver that the network an address is located in has changed
2626 * NOTE: values in addresses are already updated
2628 * @param solver solver handle
2629 * @param address the address
2630 * @param current_network the current network
2631 * @param new_network the new network
2634 GAS_ril_address_change_network (void *solver,
2635 struct ATS_Address *address,
2636 uint32_t current_network,
2637 uint32_t new_network)
2639 struct GAS_RIL_Handle *s = solver;
2640 struct RIL_Peer_Agent *agent;
2642 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2643 "%s address of peer %s from '%s' to '%s'\n",
2644 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2645 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2647 if (address->active && !ril_network_is_active (solver, new_network))
2649 GAS_ril_address_delete (solver, address, GNUNET_NO);
2653 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2656 GNUNET_assert(!ril_network_is_active (solver, current_network));
2658 GAS_ril_address_add (s, address, new_network);
2662 address->solver_information = ril_get_network(solver, new_network);
2666 * Give feedback about the current assignment
2668 * @param solver the solver handle
2669 * @param application the application
2670 * @param peer the peer to change the preference for
2671 * @param scope the time interval for this feedback: [now - scope .. now]
2672 * @param kind the kind to change the preference
2673 * @param score the score
2676 GAS_ril_address_preference_feedback (void *solver,
2678 const struct GNUNET_PeerIdentity *peer,
2679 const struct GNUNET_TIME_Relative scope,
2680 enum GNUNET_ATS_PreferenceKind kind,
2683 LOG(GNUNET_ERROR_TYPE_DEBUG,
2684 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2685 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2686 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2690 * Start a bulk operation
2692 * @param solver the solver
2695 GAS_ril_bulk_start (void *solver)
2697 struct GAS_RIL_Handle *s = solver;
2699 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2705 * Bulk operation done
2707 * @param solver the solver handle
2710 GAS_ril_bulk_stop (void *solver)
2712 struct GAS_RIL_Handle *s = solver;
2714 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2716 if (s->bulk_lock < 1)
2723 if (0 < s->bulk_changes)
2726 s->bulk_changes = 0;
2731 * Tell solver to notify ATS if the address to use changes for a specific
2732 * peer using the bandwidth changed callback
2734 * The solver must only notify about changes for peers with pending address
2737 * @param solver the solver handle
2738 * @param peer the identity of the peer
2740 const struct ATS_Address *
2741 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2744 * activate agent, return currently chosen address
2746 struct GAS_RIL_Handle *s = solver;
2747 struct RIL_Peer_Agent *agent;
2749 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2751 agent = ril_get_agent (s, peer, GNUNET_YES);
2753 agent->is_active = GNUNET_YES;
2754 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2756 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2758 if (agent->address_inuse)
2760 LOG(GNUNET_ERROR_TYPE_DEBUG,
2761 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2762 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2766 LOG(GNUNET_ERROR_TYPE_DEBUG,
2767 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2770 return agent->address_inuse;
2774 * Tell solver stop notifying ATS about changes for this peers
2776 * The solver must only notify about changes for peers with pending address
2779 * @param solver the solver handle
2780 * @param peer the peer
2783 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2785 struct GAS_RIL_Handle *s = solver;
2786 struct RIL_Peer_Agent *agent;
2788 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2790 agent = ril_get_agent (s, peer, GNUNET_NO);
2797 if (GNUNET_NO == agent->is_active)
2803 agent->is_active = GNUNET_NO;
2805 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2810 LOG(GNUNET_ERROR_TYPE_DEBUG,
2811 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2812 GNUNET_i2s (peer), agent->address_inuse->plugin);
2815 /* end of plugin_ats_ril.c */