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 (5 * 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, 300)
39 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 2000)
40 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
41 #define RIL_DEFAULT_SELECT RIL_SELECT_EGREEDY
42 #define RIL_DEFAULT_WELFARE RIL_WELFARE_NASH
43 #define RIL_DEFAULT_DISCOUNT_BETA 2.0
44 #define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
45 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.001
46 #define RIL_DEFAULT_TRACE_DECAY 0.5
47 #define RIL_DEFAULT_EXPLORE_RATIO 1
48 #define RIL_DEFAULT_EXPLORE_DECAY 0.95
49 #define RIL_DEFAULT_RBF_DIVISOR 50
50 #define RIL_DEFAULT_TEMPERATURE 10
51 #define RIL_DEFAULT_TEMPERATURE_DECAY 0.999
53 #define RIL_INC_DEC_STEP_SIZE 1
54 #define RIL_NOP_DECAY 0.5
57 * ATS reinforcement learning solver
63 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
64 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
65 * to a particular address. The action of switching to address with index i is depicted by the
66 * number (RIL_ACTION_TYPE_NUM + i).
70 RIL_ACTION_NOTHING = 0,
71 RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions
72 RIL_ACTION_BW_IN_HLV = -3,
73 RIL_ACTION_BW_IN_INC = 1,
74 RIL_ACTION_BW_IN_DEC = 2,
75 RIL_ACTION_BW_OUT_DBL = -4,
76 RIL_ACTION_BW_OUT_HLV = -5,
77 RIL_ACTION_BW_OUT_INC = 3,
78 RIL_ACTION_BW_OUT_DEC = 4,
79 RIL_ACTION_TYPE_NUM = 5
90 RIL_SELECT_SOFTMAX = 0,
91 RIL_SELECT_EGREEDY = 1
97 RIL_WELFARE_EGALITARIAN
100 enum RIL_E_Modification
109 * Global learning parameters
111 struct RIL_Learning_Parameters
114 * The TD-algorithm to use
116 enum RIL_Algorithm algorithm;
119 * Gradient-descent step-size
124 * Learning discount variable in the TD-update for semi-MDPs
129 * Learning discount factor in the TD-update for MDPs
134 * Trace-decay factor for eligibility traces
139 * Whether to accumulate or replace eligibility traces
141 enum RIL_E_Modification eligibility_trace_mode;
144 * Initial softmax action-selection temperature
146 double temperature_init;
149 * Softmax action-selection temperature
154 * Decay factor of the temperature value
156 double temperature_decay;
159 * Which measure of social welfare should be used
161 enum RIL_Welfare social_welfare;
164 * State space divisor
166 unsigned long long rbf_divisor;
169 * Action selection strategy;
171 enum RIL_Select select;
174 * Initial exploration ratio value
176 double explore_ratio_init;
179 * Ratio, with what probability an agent should explore in the e-greed policy
181 double explore_ratio;
184 * Decay factor of the explore ratio
186 double explore_ratio_decay;
189 * Minimal interval time between steps in milliseconds
191 struct GNUNET_TIME_Relative step_time_min;
194 * Maximum interval time between steps in milliseconds
196 struct GNUNET_TIME_Relative step_time_max;
200 * Wrapper for addresses to store them in agent's linked list
202 struct RIL_Address_Wrapped
207 struct RIL_Address_Wrapped *next;
212 struct RIL_Address_Wrapped *prev;
217 struct ATS_Address *address_naked;
220 struct RIL_Peer_Agent
223 * Next agent in solver's linked list
225 struct RIL_Peer_Agent *next;
228 * Previous agent in solver's linked list
230 struct RIL_Peer_Agent *prev;
235 struct GAS_RIL_Handle *envi;
240 struct GNUNET_PeerIdentity peer;
243 * Whether the agent is active or not
248 * Number of performed time-steps
250 unsigned long long step_count;
253 * Experience matrix W
258 * Number of rows of W / Number of state-vector features
263 * Number of columns of W / Number of actions
268 * Last perceived state feature vector
283 * Whether to reset the eligibility traces to 0 after a Q-exploration step
285 int eligibility_reset;
290 struct ATS_Address * address_inuse;
293 * Head of addresses DLL
295 struct RIL_Address_Wrapped * addresses_head;
298 * Tail of addresses DLL
300 struct RIL_Address_Wrapped * addresses_tail;
303 * Inbound bandwidth assigned by the agent
305 unsigned long long bw_in;
308 * Outbound bandwidth assigned by the agent
310 unsigned long long bw_out;
313 * Flag whether a suggestion has to be issued
315 int suggestion_issue;
318 * The address which has to be issued
320 struct ATS_Address * suggestion_address;
323 * The agent's last objective value
325 double objective_old;
338 enum GNUNET_ATS_Network_Type type;
341 * Total available inbound bandwidth
343 unsigned long long bw_in_available;
346 * Bandwidth inbound assigned in network after last step
348 unsigned long long bw_in_assigned;
351 * Bandwidth inbound actually utilized in the network
353 unsigned long long bw_in_utilized;
356 * Total available outbound bandwidth
358 unsigned long long bw_out_available;
361 * Bandwidth outbound assigned in network after last step
363 unsigned long long bw_out_assigned;
366 * Bandwidth outbound actually utilized in the network
368 unsigned long long bw_out_utilized;
371 * Number of active agents in scope
373 unsigned int agent_count;
376 * The social welfare achieved in the scope
378 double social_welfare;
382 * A handle for the reinforcement learning solver
384 struct GAS_RIL_Handle
387 * The solver-plugin environment of the solver-plugin API
389 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
394 struct GNUNET_STATISTICS_Handle *stats;
397 * Number of performed steps
399 unsigned long long step_count;
402 * Timestamp for the last time-step
404 struct GNUNET_TIME_Absolute step_time_last;
407 * Task identifier of the next time-step to be executed
409 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
412 * Variable discount factor, dependent on time between steps
414 double global_discount_variable;
417 * Integrated variable discount factor, dependent on time between steps
419 double global_discount_integrated;
422 * Lock for bulk operations
427 * Number of changes during a lock
432 * Learning parameters
434 struct RIL_Learning_Parameters parameters;
437 * Array of networks with global assignment state
439 struct RIL_Scope * network_entries;
444 unsigned int networks_count;
447 * List of active peer-agents
449 struct RIL_Peer_Agent * agents_head;
450 struct RIL_Peer_Agent * agents_tail;
458 * Simulate steps, i.e. schedule steps immediately
460 unsigned long long simulate;
464 * "Private" functions
465 * ---------------------------
469 * Estimate the current action-value for state s and action a
471 * @param agent agent performing the estimation
474 * @return estimation value
477 agent_q (struct RIL_Peer_Agent *agent, double *state, int action)
482 for (i = 0; i < agent->m; i++)
484 result += state[i] * agent->W[action][i];
487 GNUNET_assert(!isnan(result));
489 //prevent crash when learning diverges
492 return isinf(result) * UINT32_MAX;
499 * Get the index of the address in the agent's list.
501 * @param agent agent handle
502 * @param address address handle
503 * @return the index, starting with zero
506 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
509 struct RIL_Address_Wrapped *cur;
512 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
515 if (cur->address_naked == address)
523 * Gets the wrapped address from the agent's list
525 * @param agent agent handle
526 * @param address address handle
527 * @return wrapped address
529 static struct RIL_Address_Wrapped *
530 agent_address_get_wrapped (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
532 struct RIL_Address_Wrapped *cur;
534 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
535 if (cur->address_naked == address)
542 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
548 case RIL_ACTION_NOTHING:
551 case RIL_ACTION_BW_IN_INC:
552 case RIL_ACTION_BW_IN_DBL:
553 if (agent->bw_in >= RIL_MAX_BW)
558 case RIL_ACTION_BW_IN_DEC:
559 case RIL_ACTION_BW_IN_HLV:
560 if (agent->bw_in <= RIL_MIN_BW)
565 case RIL_ACTION_BW_OUT_INC:
566 case RIL_ACTION_BW_OUT_DBL:
567 if (agent->bw_out >= RIL_MAX_BW)
572 case RIL_ACTION_BW_OUT_DEC:
573 case RIL_ACTION_BW_OUT_HLV:
574 if (agent->bw_out <= RIL_MIN_BW)
580 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
582 address_index = action - RIL_ACTION_TYPE_NUM;
584 GNUNET_assert(address_index >= 0);
586 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
588 if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
589 agent->address_inuse->active)
595 // error - action does not exist
596 GNUNET_assert(GNUNET_NO);
602 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
603 * most reward in the future)
605 * @param agent agent performing the calculation
606 * @param state the state from which to take the action
607 * @return the action promising most future reward
610 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
613 int max_i = RIL_ACTION_INVALID;
615 double max_q = -DBL_MAX;
617 for (i = 0; i < agent->n; i++)
619 if (agent_action_is_possible(agent, i))
621 cur_q = agent_q (agent, state, i);
630 GNUNET_assert(RIL_ACTION_INVALID != max_i);
636 * Chooses a random action from the set of possible ones
638 * @param agent the agent performing the action
639 * @return the action index
642 agent_get_action_random (struct RIL_Peer_Agent *agent)
645 int is_possible[agent->n];
649 for (i = 0; i<agent->n; i++)
651 if (agent_action_is_possible(agent, i))
653 is_possible[i] = GNUNET_YES;
658 is_possible[i] = GNUNET_NO;
662 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
665 for (i = 0; i<agent->n; i++)
675 GNUNET_assert(GNUNET_NO);
680 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
682 * @param agent the agent performing the update
683 * @param reward the reward received for the last action
684 * @param s_next the new state, the last step got the agent into
685 * @param a_prime the new
688 agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
693 double **theta = agent->W;
695 delta = agent->envi->global_discount_integrated * reward; //reward
696 delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
697 delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
699 // 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",
700 // agent->step_count,
701 // agent_q (agent, agent->s_old, agent->a_old),
702 // agent->envi->parameters.alpha,
704 // agent->envi->global_discount_variable,
705 // agent_q (agent, s_next, a_prime),
708 for (k = 0; k < agent->n; k++)
710 for (i = 0; i < agent->m; i++)
712 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
713 // agent->envi->parameters.alpha,
717 theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
724 * Changes the eligibility trace vector e in various manners:
725 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
726 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
727 * #RIL_E_DISCOUNT - multiplies e with discount factor and lambda as in the update rule
728 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
730 * @param agent the agent handle
731 * @param mod the kind of modification
732 * @param feature the feature vector
735 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
736 enum RIL_E_Modification mod,
743 for (i = 0; i < agent->m; i++)
747 case RIL_E_ACCUMULATE:
748 agent->E[action][i] += feature[i];
751 agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
754 for (k = 0; k < agent->n; k++)
756 agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
760 for (k = 0; k < agent->n; k++)
770 * Informs the environment about the status of the solver
777 ril_inform (struct GAS_RIL_Handle *solver,
778 enum GAS_Solver_Operation op,
779 enum GAS_Solver_Status stat)
781 if (NULL != solver->plugin_envi->info_cb)
782 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
787 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
788 * notify ATS of its new decision
790 * @param solver solver handle
791 * @param agent agent handle
792 * @param new_address the address which is to be used
793 * @param new_bw_in the new amount of inbound bandwidth set for this address
794 * @param new_bw_out the new amount of outbound bandwidth set for this address
795 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
798 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
799 struct RIL_Peer_Agent *agent,
800 struct ATS_Address *new_address,
801 unsigned long long new_bw_in,
802 unsigned long long new_bw_out,
805 int notify = GNUNET_NO;
807 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
810 if (agent->address_inuse != new_address)
812 if (NULL != agent->address_inuse)
814 agent->address_inuse->active = GNUNET_NO;
815 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
816 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
818 if (NULL != new_address)
820 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
821 new_address->active = agent->is_active;
822 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
823 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
825 notify |= GNUNET_YES;
831 if (new_address->active != agent->is_active)
833 new_address->active = agent->is_active;
834 notify |= GNUNET_YES;
838 if (agent->bw_in != new_bw_in)
840 agent->bw_in = new_bw_in;
841 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
842 notify |= GNUNET_YES;
844 if (agent->bw_out != new_bw_out)
846 agent->bw_out = new_bw_out;
847 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
848 notify |= GNUNET_YES;
852 if (notify && agent->is_active && (GNUNET_NO == silent))
856 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
857 agent->suggestion_issue = GNUNET_YES;
858 agent->suggestion_address = new_address;
860 else if (agent->address_inuse)
862 //disconnect case, no new address
863 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
864 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
868 agent->suggestion_issue = GNUNET_YES;
869 agent->suggestion_address = agent->address_inuse;
872 agent->address_inuse = new_address;
877 * Allocates a state vector and fills it with the features present
878 * @param solver the solver handle
879 * @param agent the agent handle
880 * @return pointer to the state vector
883 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
894 unsigned long long max_bw;
896 state = GNUNET_malloc (sizeof(double) * agent->m);
900 y[0] = (double) agent->bw_out;
901 y[1] = (double) agent->bw_in;
903 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
904 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
906 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
908 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
909 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
912 sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
913 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
922 * Retrieves an ATS information value of an address
924 * @param address the address in question
925 * @param type the ATS information type
929 ril_get_atsi (struct ATS_Address *address, uint32_t type)
932 GNUNET_assert(NULL != address);
934 if ((NULL == address->atsi) || (0 == address->atsi_count))
935 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
937 for (c1 = 0; c1 < address->atsi_count; c1++)
939 if (ntohl (address->atsi[c1].type) == type)
940 return ntohl (address->atsi[c1].value);
942 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
946 * Returns the utility value of the connection an agent manages
948 * @param agent the agent in question
949 * @return the utility value
952 agent_get_utility (struct RIL_Peer_Agent *agent)
954 const double *preferences;
959 preferences = agent->envi->plugin_envi->get_preferences (agent->envi->plugin_envi->get_preference_cls,
962 delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
963 delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
965 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
966 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
967 sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
969 // return (double) (agent->bw_in/RIL_MIN_BW);
970 // return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
975 * Calculates the social welfare within a network scope according to what social
976 * welfare measure is set in the configuration.
978 * @param solver the solver handle
979 * @param scope the network scope in question
980 * @return the social welfare value
983 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
985 struct RIL_Peer_Agent *cur;
988 switch (solver->parameters.social_welfare)
990 case RIL_WELFARE_EGALITARIAN:
992 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
994 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
996 result = GNUNET_MIN(result, agent_get_utility(cur));
1001 case RIL_WELFARE_NASH:
1003 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1005 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1007 result *= pow(agent_get_utility(cur), 1.0 / (double) scope->agent_count);
1012 GNUNET_assert(GNUNET_NO);
1017 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1019 struct RIL_Scope *net;
1020 unsigned long long over_max;
1021 unsigned long long over_in = 0;
1022 unsigned long long over_out = 0;
1024 net = agent->address_inuse->solver_information;
1026 if (net->bw_in_utilized > net->bw_in_available)
1028 over_in = net->bw_in_utilized - net->bw_in_available;
1029 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1034 if (net->bw_out_utilized > net->bw_out_available)
1036 over_out = net->bw_out_utilized - net->bw_out_available;
1037 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1042 over_max = GNUNET_MAX (over_in , over_out) / RIL_MIN_BW;
1044 return -1.0 * (double) over_max;
1048 * Gets the reward for the last performed step, which is calculated in equal
1049 * parts from the local (the peer specific) and the global (for all peers
1050 * identical) reward.
1052 * @param solver the solver handle
1053 * @param agent the agent handle
1054 * @return the reward
1057 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1059 struct RIL_Scope *net;
1066 net = agent->address_inuse->solver_information;
1068 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1069 delta = objective - agent->objective_old;
1070 agent->objective_old = objective;
1074 agent->nop_bonus = delta * RIL_NOP_DECAY;
1078 agent->nop_bonus *= RIL_NOP_DECAY;
1081 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1083 penalty = envi_get_penalty(solver, agent);
1085 reward = delta + steady;
1086 return reward + penalty;
1090 * Doubles the bandwidth for the active address
1092 * @param solver solver handle
1093 * @param agent agent handle
1094 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1097 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1098 struct RIL_Peer_Agent *agent,
1101 unsigned long long new_bw;
1105 new_bw = agent->bw_in * 2;
1106 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1107 new_bw = RIL_MAX_BW;
1108 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1109 agent->bw_out, GNUNET_NO);
1113 new_bw = agent->bw_out * 2;
1114 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1115 new_bw = RIL_MAX_BW;
1116 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1122 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1123 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1125 * @param solver solver handle
1126 * @param agent agent handle
1127 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1131 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1132 struct RIL_Peer_Agent *agent,
1135 unsigned long long new_bw;
1139 new_bw = agent->bw_in / 2;
1140 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1141 new_bw = RIL_MIN_BW;
1142 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1147 new_bw = agent->bw_out / 2;
1148 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1149 new_bw = RIL_MIN_BW;
1150 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1156 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1158 * @param solver solver handle
1159 * @param agent agent handle
1160 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1164 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1166 unsigned long long new_bw;
1170 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1171 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1172 new_bw = RIL_MAX_BW;
1173 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1174 agent->bw_out, GNUNET_NO);
1178 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1179 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1180 new_bw = RIL_MAX_BW;
1181 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1187 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1188 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1190 * @param solver solver handle
1191 * @param agent agent handle
1192 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1196 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1198 unsigned long long new_bw;
1202 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1203 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1204 new_bw = RIL_MIN_BW;
1205 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1210 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1211 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1212 new_bw = RIL_MIN_BW;
1213 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1219 * Switches to the address given by its index
1221 * @param solver solver handle
1222 * @param agent agent handle
1223 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1226 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1227 struct RIL_Peer_Agent *agent,
1228 unsigned int address_index)
1230 struct RIL_Address_Wrapped *cur;
1233 cur = agent_address_get_wrapped(agent, agent->address_inuse);
1235 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1237 if (i == address_index)
1239 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1247 //no address with address_index exists, in this case this action should not be callable
1248 GNUNET_assert(GNUNET_NO);
1252 * Puts the action into effect by calling the according function
1254 * @param solver the solver handle
1255 * @param agent the action handle
1256 * @param action the action to perform by the solver
1259 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1265 case RIL_ACTION_NOTHING:
1267 case RIL_ACTION_BW_IN_DBL:
1268 envi_action_bw_double (solver, agent, GNUNET_YES);
1270 case RIL_ACTION_BW_IN_HLV:
1271 envi_action_bw_halven (solver, agent, GNUNET_YES);
1273 case RIL_ACTION_BW_IN_INC:
1274 envi_action_bw_inc (solver, agent, GNUNET_YES);
1276 case RIL_ACTION_BW_IN_DEC:
1277 envi_action_bw_dec (solver, agent, GNUNET_YES);
1279 case RIL_ACTION_BW_OUT_DBL:
1280 envi_action_bw_double (solver, agent, GNUNET_NO);
1282 case RIL_ACTION_BW_OUT_HLV:
1283 envi_action_bw_halven (solver, agent, GNUNET_NO);
1285 case RIL_ACTION_BW_OUT_INC:
1286 envi_action_bw_inc (solver, agent, GNUNET_NO);
1288 case RIL_ACTION_BW_OUT_DEC:
1289 envi_action_bw_dec (solver, agent, GNUNET_NO);
1292 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1294 address_index = action - RIL_ACTION_TYPE_NUM;
1296 GNUNET_assert(address_index >= 0);
1298 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1300 envi_action_address_switch (solver, agent, address_index);
1303 // error - action does not exist
1304 GNUNET_assert(GNUNET_NO);
1309 * Selects the next action using the e-greedy strategy. I.e. with a probability
1310 * of (1-e) the action with the maximum expected return will be chosen
1311 * (=> exploitation) and with probability (e) a random action will be chosen.
1312 * In case the Q-learning rule is set, the function also resets the eligibility
1313 * traces in the exploration case (after Watkin's Q-learning).
1315 * @param agent the agent selecting an action
1316 * @param state the current state-feature vector
1317 * @return the action index
1320 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1323 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1324 UINT32_MAX) / (double) UINT32_MAX;
1326 if (r < agent->envi->parameters.explore_ratio) //explore
1328 action = agent_get_action_random(agent);
1329 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1331 agent->eligibility_reset = GNUNET_YES;
1333 agent->envi->parameters.explore_ratio *= agent->envi->parameters.explore_ratio_decay;
1338 action = agent_get_action_max(agent, state);
1344 * Selects the next action with a probability corresponding to its value. The
1345 * probability is calculated using a Boltzmann distribution with a temperature
1346 * value. The higher the temperature, the more are the action selection
1347 * probabilities the same. With a temperature of 0, the selection is greedy,
1348 * i.e. always the action with the highest value is chosen.
1354 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1358 double eqt[agent->n];
1363 a_max = agent_get_action_max(agent, state);
1365 for (i=0; i<agent->n; i++)
1367 if (agent_action_is_possible(agent, i))
1369 eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1373 for (i=0; i<agent->n; i++)
1375 if (agent_action_is_possible(agent, i))
1384 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1385 UINT32_MAX) / (double) UINT32_MAX;
1387 for (i=0; i<agent->n; i++)
1393 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1394 agent->eligibility_reset = GNUNET_YES;
1395 agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay;
1401 GNUNET_assert(GNUNET_NO);
1405 * Select the next action of an agent either according to the e-greedy strategy
1406 * or the softmax strategy.
1408 * @param agent the agent in question
1409 * @param state the current state-feature vector
1410 * @return the action index
1413 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1415 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1417 return agent_select_egreedy(agent, state);
1421 return agent_select_softmax(agent, state);
1426 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1427 * after having done the last action a_old. It observes the new state s_next and the reward
1428 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1429 * next action is put into effect.
1431 * @param agent the agent performing the step
1434 agent_step (struct RIL_Peer_Agent *agent)
1436 int a_next = RIL_ACTION_INVALID;
1441 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1442 GNUNET_i2s (&agent->peer),
1443 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1445 s_next = envi_get_state (agent->envi, agent);
1446 reward = envi_get_reward (agent->envi, agent);
1448 if (agent->eligibility_reset)
1450 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1451 agent->eligibility_reset = GNUNET_NO;
1455 agent_modify_eligibility (agent, RIL_E_DISCOUNT, NULL, -1);
1457 if (RIL_ACTION_INVALID != agent->a_old)
1459 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1462 switch (agent->envi->parameters.algorithm)
1464 case RIL_ALGO_SARSA:
1465 a_next = agent_select_action (agent, s_next);
1466 if (RIL_ACTION_INVALID != agent->a_old)
1468 //updates weights with selected action (on-policy), if not first step
1469 agent_update (agent, reward, s_next, a_next);
1474 a_max = agent_get_action_max (agent, s_next);
1475 if (RIL_ACTION_INVALID != agent->a_old)
1477 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1478 agent_update (agent, reward, s_next, a_max);
1480 a_next = agent_select_action (agent, s_next);
1484 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1486 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1493 envi_do_action (agent->envi, agent, a_next);
1495 GNUNET_free(agent->s_old);
1496 agent->s_old = s_next;
1497 agent->a_old = a_next;
1499 agent->step_count += 1;
1503 * Prototype of the ril_step() procedure
1505 * @param solver the solver handle
1508 ril_step (struct GAS_RIL_Handle *solver);
1511 * Task for the scheduler, which performs one step and lets the solver know that
1512 * no further step is scheduled.
1514 * @param cls the solver handle
1515 * @param tc the task context for the scheduler
1518 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1520 struct GAS_RIL_Handle *solver = cls;
1522 solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1527 * Determines how much of the available bandwidth is assigned. If more is
1528 * assigned than available it returns 1. The function is used to determine the
1529 * step size of the adaptive stepping.
1531 * @param solver the solver handle
1535 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1538 struct RIL_Scope net;
1539 unsigned long long sum_assigned = 0;
1540 unsigned long long sum_available = 0;
1543 for (i = 0; i < solver->networks_count; i++)
1545 net = solver->network_entries[i];
1546 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1548 sum_assigned += net.bw_in_utilized;
1549 sum_assigned += net.bw_out_utilized;
1550 sum_available += net.bw_in_available;
1551 sum_available += net.bw_out_available;
1554 if (sum_available > 0)
1556 ratio = ((double) sum_assigned) / ((double) sum_available);
1563 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1567 * Lookup network struct by type
1569 * @param s the solver handle
1570 * @param type the network type
1571 * @return the network struct
1573 static struct RIL_Scope *
1574 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1578 for (i = 0; i < s->networks_count; i++)
1580 if (s->network_entries[i].type == type)
1582 return &s->network_entries[i];
1589 * Determines whether more connections are allocated in a network scope, than
1590 * they would theoretically fit. This is used as a heuristic to determine,
1591 * whether a new connection can be allocated or not.
1593 * @param solver the solver handle
1594 * @param network the network scope in question
1595 * @return GNUNET_YES if there are theoretically enough resources left
1598 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1600 struct RIL_Scope *net;
1601 struct RIL_Peer_Agent *agent;
1602 unsigned long long address_count = 0;
1604 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1606 if (agent->address_inuse && agent->is_active)
1608 net = agent->address_inuse->solver_information;
1609 if (net->type == network)
1616 net = ril_get_network (solver, network);
1617 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1621 * Unblocks an agent for which a connection request is there, that could not
1622 * be satisfied. Iterates over the addresses of the agent, if one of its
1623 * addresses can now be allocated in its scope the agent is unblocked,
1624 * otherwise it remains unchanged.
1626 * @param solver the solver handle
1627 * @param agent the agent in question
1631 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1633 struct RIL_Address_Wrapped *addr_wrap;
1634 struct RIL_Scope *net;
1636 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1638 net = addr_wrap->address_naked->solver_information;
1639 if (ril_network_is_not_full(solver, net->type))
1641 if (NULL == agent->address_inuse)
1642 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, agent->bw_in, agent->bw_out, silent);
1646 agent->address_inuse = NULL;
1650 * Determines how much the reward needs to be discounted depending on the amount
1651 * of time, which has passed since the last time-step.
1653 * @param solver the solver handle
1656 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1658 struct GNUNET_TIME_Absolute time_now;
1659 struct GNUNET_TIME_Relative time_delta;
1662 // MDP case only for debugging purposes
1663 if (solver->simulate)
1665 solver->global_discount_variable = solver->parameters.gamma;
1666 solver->global_discount_integrated = 1;
1672 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1673 time_now = GNUNET_TIME_absolute_get ();
1674 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1675 solver->step_time_last = time_now;
1676 tau = (double) time_delta.rel_value_us
1677 / (double) solver->parameters.step_time_min.rel_value_us;
1679 //calculate reward discounts (once per step for all agents)
1680 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1681 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1682 / (double) solver->parameters.beta;
1686 * Count the number of active agents/connections in a network scope
1688 * @param solver the solver handle
1689 * @param scope the network scope in question
1690 * @return the number of allocated connections
1693 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1696 struct RIL_Peer_Agent *cur_agent;
1698 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1700 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1709 * Calculates how much bandwidth is assigned in sum in a network scope, either
1710 * in the inbound or in the outbound direction.
1712 * @param solver the solver handle
1713 * @param type the type of the network scope in question
1714 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1715 * otherwise the outbound direction will be summed up
1716 * @return the sum of the assigned bandwidths
1718 static unsigned long long
1719 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1721 struct RIL_Peer_Agent *cur;
1722 struct RIL_Scope *net;
1723 unsigned long long sum = 0;
1725 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1727 if (cur->is_active && cur->address_inuse)
1729 net = cur->address_inuse->solver_information;
1730 if (net->type == type)
1744 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1745 * either in the inbound or in the outbound direction.
1747 * @param solver the solver handle
1748 * @param type the type of the network scope in question
1749 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1750 * otherwise the outbound direction will be summed up
1751 * @return the sum of the utilized bandwidths (in bytes/second)
1753 static unsigned long long
1754 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1756 struct RIL_Peer_Agent *cur;
1757 struct RIL_Scope *net;
1758 unsigned long long sum = 0;
1760 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1762 if (cur->is_active && cur->address_inuse)
1764 net = cur->address_inuse->solver_information;
1765 if (net->type == type)
1768 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
1770 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
1779 * Retrieves the state of the network scope, so that its attributes are up-to-
1782 * @param solver the solver handle
1785 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1788 struct RIL_Scope *net;
1790 for (c = 0; c < solver->networks_count; c++)
1792 net = &solver->network_entries[c];
1793 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1794 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1795 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1796 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1797 net->agent_count = ril_network_count_active_agents(solver, net);
1798 net->social_welfare = ril_network_get_social_welfare(solver, net);
1803 * Schedules the next global step in an adaptive way. The more resources are
1804 * left, the earlier the next step is scheduled. This serves the reactivity of
1805 * the solver to changed inputs.
1807 * @param solver the solver handle
1810 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1816 struct GNUNET_TIME_Relative time_next;
1818 used_ratio = ril_get_used_resource_ratio (solver);
1821 solver->parameters.step_time_min.rel_value_us
1822 <= solver->parameters.step_time_max.rel_value_us);
1824 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1825 solver->parameters.step_time_min).rel_value_us;
1826 offset = (double) solver->parameters.step_time_min.rel_value_us;
1827 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1829 GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
1830 GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
1832 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1834 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1840 if (solver->simulate)
1842 time_next = GNUNET_TIME_UNIT_ZERO;
1845 if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1847 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1853 * Triggers one step per agent
1858 ril_step (struct GAS_RIL_Handle *solver)
1860 struct RIL_Peer_Agent *cur;
1862 if (GNUNET_YES == solver->bulk_lock)
1864 solver->bulk_changes++;
1868 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1870 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1872 if (0 == solver->step_count)
1874 solver->step_time_last = GNUNET_TIME_absolute_get ();
1877 ril_calculate_discount (solver);
1878 ril_networks_update_state (solver);
1880 //trigger one step per active, unblocked agent
1881 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1885 if (NULL == cur->address_inuse)
1887 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1889 if (cur->address_inuse)
1896 ril_networks_update_state (solver);
1898 solver->step_count += 1;
1899 ril_step_schedule_next (solver);
1901 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1903 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1904 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1906 if (cur->suggestion_issue) {
1907 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1908 cur->suggestion_issue = GNUNET_NO;
1911 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1915 * Initializes the matrix W of parameter vectors theta with small random numbers.
1917 * @param agent The respective agent
1920 agent_w_init (struct RIL_Peer_Agent *agent)
1925 for (i = 0; i < agent->n; i++)
1927 for (k = 0; k < agent->m; k++)
1929 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));
1935 * Initialize an agent without addresses and its knowledge base
1937 * @param s ril solver
1938 * @param peer the one in question
1939 * @return handle to the new agent
1941 static struct RIL_Peer_Agent *
1942 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1945 struct GAS_RIL_Handle * solver = s;
1946 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1948 agent->envi = solver;
1949 agent->peer = *peer;
1950 agent->step_count = 0;
1951 agent->is_active = GNUNET_NO;
1952 agent->bw_in = RIL_MIN_BW;
1953 agent->bw_out = RIL_MIN_BW;
1954 agent->suggestion_issue = GNUNET_NO;
1955 agent->n = RIL_ACTION_TYPE_NUM;
1957 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1958 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1959 for (i = 0; i < agent->n; i++)
1961 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1962 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1964 agent_w_init(agent);
1965 agent->eligibility_reset = GNUNET_NO;
1966 agent->a_old = RIL_ACTION_INVALID;
1967 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1968 agent->address_inuse = NULL;
1969 agent->objective_old = 0;
1970 agent->nop_bonus = 0;
1978 * @param solver the solver handle
1979 * @param agent the agent to retire
1982 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1986 for (i = 0; i < agent->n; i++)
1988 GNUNET_free_non_null(agent->W[i]);
1989 GNUNET_free_non_null(agent->E[i]);
1991 GNUNET_free_non_null(agent->W);
1992 GNUNET_free_non_null(agent->E);
1993 GNUNET_free_non_null(agent->s_old);
1998 * Returns the agent for a peer
2000 * @param solver the solver handle
2001 * @param peer the identity of the peer
2002 * @param create whether or not to create an agent, if none is allocated yet
2005 static struct RIL_Peer_Agent *
2006 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
2008 struct RIL_Peer_Agent *cur;
2010 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2012 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
2020 cur = agent_init (solver, peer);
2021 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
2028 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
2029 * considered inactive and not used. Addresses in an inactive network are ignored.
2031 * @param solver solver handle
2032 * @param network the network type
2033 * @return whether or not the network is considered active
2036 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
2038 struct RIL_Scope *net;
2040 net = ril_get_network (solver, network);
2041 return net->bw_out_available >= RIL_MIN_BW;
2045 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
2046 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
2047 * frees the memory of the old vector, and redirects the pointer to the new one.
2049 * @param old pointer to the pointer to the first element of the vector
2050 * @param element_size byte size of the vector elements
2051 * @param hole_start the first element to cut out
2052 * @param hole_length the number of elements to cut out
2053 * @param old_length the length of the old vector
2056 ril_cut_from_vector (void **old,
2057 size_t element_size,
2058 unsigned int hole_start,
2059 unsigned int hole_length,
2060 unsigned int old_length)
2063 char *oldptr = (char *) *old;
2065 unsigned int bytes_before;
2066 unsigned int bytes_hole;
2067 unsigned int bytes_after;
2069 GNUNET_assert(old_length >= hole_length);
2070 GNUNET_assert(old_length >= (hole_start + hole_length));
2072 size = element_size * (old_length - hole_length);
2074 bytes_before = element_size * hole_start;
2075 bytes_hole = element_size * hole_length;
2076 bytes_after = element_size * (old_length - hole_start - hole_length);
2084 tmpptr = GNUNET_malloc (size);
2085 memcpy (tmpptr, oldptr, bytes_before);
2086 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2092 *old = (void *) tmpptr;
2096 * Solver API functions
2097 * ---------------------------
2101 * Change relative preference for quality in solver
2103 * @param solver the solver handle
2104 * @param peer the peer to change the preference for
2105 * @param kind the kind to change the preference
2106 * @param pref_rel the normalized preference value for this kind over all clients
2109 GAS_ril_address_change_preference (void *solver,
2110 const struct GNUNET_PeerIdentity *peer,
2111 enum GNUNET_ATS_PreferenceKind kind,
2114 LOG(GNUNET_ERROR_TYPE_DEBUG,
2115 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2116 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2118 struct GAS_RIL_Handle *s = solver;
2120 s->parameters.temperature = s->parameters.temperature_init;
2121 s->parameters.explore_ratio = s->parameters.explore_ratio_init;
2126 * Entry point for the plugin
2128 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2131 libgnunet_plugin_ats_ril_init (void *cls)
2133 struct GNUNET_ATS_PluginEnvironment *env = cls;
2134 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2135 struct RIL_Scope * cur;
2139 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
2141 GNUNET_assert(NULL != env);
2142 GNUNET_assert(NULL != env->cfg);
2143 GNUNET_assert(NULL != env->stats);
2144 GNUNET_assert(NULL != env->bandwidth_changed_cb);
2145 GNUNET_assert(NULL != env->get_preferences);
2146 GNUNET_assert(NULL != env->get_property);
2148 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2150 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2154 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2155 &solver->parameters.step_time_min))
2157 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2161 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2162 &solver->parameters.step_time_max))
2164 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2167 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2169 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
2170 GNUNET_free (string);
2174 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2177 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2179 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2180 GNUNET_free (string);
2184 solver->parameters.select = RIL_DEFAULT_SELECT;
2187 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
2189 solver->parameters.beta = strtod (string, NULL);
2190 GNUNET_free (string);
2191 if (!(solver->parameters.beta > 0))
2193 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);
2194 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2199 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2202 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
2204 solver->parameters.gamma = strtod (string, NULL);
2205 GNUNET_free (string);
2206 if (!(solver->parameters.gamma < 1) || (solver->parameters.gamma < 0))
2208 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);
2209 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2214 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2218 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
2220 solver->parameters.alpha = strtod (string, NULL);
2221 GNUNET_free (string);
2222 if (!(solver->parameters.alpha > 0) || solver->parameters.alpha > 1)
2224 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);
2225 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2230 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2233 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
2235 solver->parameters.lambda = strtod (string, NULL);
2236 GNUNET_free (string);
2237 if (solver->parameters.lambda < 0 || solver->parameters.lambda > 1)
2239 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);
2240 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2245 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2248 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
2250 solver->parameters.explore_ratio_init = strtod (string, NULL);
2251 GNUNET_free (string);
2252 if (solver->parameters.explore_ratio_init < 0 || solver->parameters.explore_ratio_init > 1)
2254 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);
2255 solver->parameters.explore_ratio_init = RIL_DEFAULT_EXPLORE_RATIO;
2260 solver->parameters.explore_ratio_init = RIL_DEFAULT_EXPLORE_RATIO;
2263 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_DECAY", &string))
2265 solver->parameters.explore_ratio_decay = strtod (string, NULL);
2266 GNUNET_free (string);
2267 if (solver->parameters.explore_ratio_decay < 0 || solver->parameters.explore_ratio_decay > 1)
2269 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_DECAY);
2270 solver->parameters.explore_ratio_decay = RIL_DEFAULT_EXPLORE_DECAY;
2275 solver->parameters.explore_ratio_decay = RIL_DEFAULT_EXPLORE_DECAY;
2278 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE", &string))
2280 solver->parameters.temperature_init = strtod (string, NULL);
2281 GNUNET_free (string);
2282 if (solver->parameters.temperature_init <= 0)
2284 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE not positive. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE);
2285 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2290 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2293 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE_DECAY", &string))
2295 solver->parameters.temperature_decay = strtod (string, NULL);
2296 GNUNET_free (string);
2297 if (solver->parameters.temperature_decay <= 0 || solver->parameters.temperature_decay > 1)
2299 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE_DECAY not in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE_DECAY);
2300 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
2305 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
2308 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2310 solver->simulate = GNUNET_NO;
2313 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2315 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2319 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2322 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2324 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2325 GNUNET_free (string);
2329 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2332 env->sf.s_add = &GAS_ril_address_add;
2333 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
2334 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
2335 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
2336 env->sf.s_address_update_network = &GAS_ril_address_change_network;
2337 env->sf.s_get = &GAS_ril_get_preferred_address;
2338 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2339 env->sf.s_pref = &GAS_ril_address_change_preference;
2340 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
2341 env->sf.s_del = &GAS_ril_address_delete;
2342 env->sf.s_bulk_start = &GAS_ril_bulk_start;
2343 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
2345 solver->plugin_envi = env;
2346 solver->networks_count = env->network_count;
2347 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2348 solver->step_count = 0;
2349 solver->done = GNUNET_NO;
2351 for (c = 0; c < env->network_count; c++)
2353 cur = &solver->network_entries[c];
2354 cur->type = env->networks[c];
2355 cur->bw_in_available = env->in_quota[c];
2356 cur->bw_out_available = env->out_quota[c];
2357 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);
2360 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2361 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2362 solver->parameters.algorithm ? "Q" : "SARSA",
2363 solver->parameters.alpha,
2364 solver->parameters.beta,
2365 solver->parameters.lambda);
2366 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
2367 solver->parameters.explore_ratio,
2368 solver->parameters.temperature,
2369 solver->parameters.select ? "EGREEDY" : "SOFTMAX");
2370 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2371 solver->parameters.rbf_divisor);
2377 * Exit point for the plugin
2379 * @param cls the solver handle
2382 libgnunet_plugin_ats_ril_done (void *cls)
2384 struct GAS_RIL_Handle *s = cls;
2385 struct RIL_Peer_Agent *cur_agent;
2386 struct RIL_Peer_Agent *next_agent;
2388 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2390 s->done = GNUNET_YES;
2392 cur_agent = s->agents_head;
2393 while (NULL != cur_agent)
2395 next_agent = cur_agent->next;
2396 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2397 agent_die (s, cur_agent);
2398 cur_agent = next_agent;
2401 if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
2403 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2405 GNUNET_free(s->network_entries);
2412 * Add a new address for a peer to the solver
2414 * The address is already contained in the addresses hashmap!
2416 * @param solver the solver Handle
2417 * @param address the address to add
2418 * @param network network type of this address
2421 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
2423 struct GAS_RIL_Handle *s = solver;
2424 struct RIL_Peer_Agent *agent;
2425 struct RIL_Address_Wrapped *address_wrapped;
2426 struct RIL_Scope *net;
2434 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
2436 net = ril_get_network (s, network);
2437 address->solver_information = net;
2439 if (!ril_network_is_active (s, network))
2441 LOG(GNUNET_ERROR_TYPE_DEBUG,
2442 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2443 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2447 s->parameters.temperature = s->parameters.temperature_init;
2448 s->parameters.explore_ratio = s->parameters.explore_ratio_init;
2450 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2453 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2454 address_wrapped->address_naked = address;
2455 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2457 //increase size of W
2458 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2460 n_new = agent->n + 1;
2463 GNUNET_array_grow(agent->W, agent->n, n_new);
2465 GNUNET_array_grow(agent->E, agent->n, n_new);
2466 for (i = 0; i < n_new; i++)
2471 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2473 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2478 GNUNET_array_grow(agent->W[i], zero, m_new);
2480 GNUNET_array_grow(agent->E[i], zero, m_new);
2484 //increase size of old state vector
2486 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2488 ril_try_unblock_agent(s, agent, GNUNET_NO);
2492 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2493 address->active ? "active" : "inactive", address->plugin, address->addr,
2494 GNUNET_i2s (&address->peer));
2498 * Delete an address in the solver
2500 * The address is not contained in the address hashmap anymore!
2502 * @param solver the solver handle
2503 * @param address the address to remove
2504 * @param session_only delete only session not whole address
2507 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
2509 struct GAS_RIL_Handle *s = solver;
2510 struct RIL_Peer_Agent *agent;
2511 struct RIL_Address_Wrapped *address_wrapped;
2512 int address_was_used = address->active;
2517 struct RIL_Scope *net;
2519 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2520 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2521 address->addr, GNUNET_i2s (&address->peer));
2523 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2526 net = address->solver_information;
2527 GNUNET_assert(!ril_network_is_active (s, net->type));
2528 LOG(GNUNET_ERROR_TYPE_DEBUG,
2529 "No agent allocated for peer yet, since address was in inactive network\n");
2533 s->parameters.temperature = s->parameters.temperature_init;
2534 s->parameters.explore_ratio = s->parameters.explore_ratio_init;
2536 address_index = agent_address_get_index (agent, address);
2537 address_wrapped = agent_address_get_wrapped (agent, address);
2539 if (NULL == address_wrapped)
2541 net = address->solver_information;
2542 LOG(GNUNET_ERROR_TYPE_DEBUG,
2543 "Address not considered by agent, address was in inactive network\n");
2547 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2548 GNUNET_free(address_wrapped);
2551 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2552 n_new = agent->n - 1;
2554 for (i = 0; i < agent->n; i++)
2556 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2557 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2558 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2559 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2560 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2561 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2563 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2564 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2565 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2567 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2569 //correct last action
2570 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2574 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2576 agent->a_old = RIL_ACTION_INVALID;
2578 //decrease old state vector
2579 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2580 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2581 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2585 if (address_was_used)
2587 if (NULL != agent->addresses_head) //if peer has an address left, use it
2589 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, agent->bw_in, agent->bw_out,
2594 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2602 * Update the properties of an address in the solver
2604 * @param solver solver handle
2605 * @param address the address
2606 * @param type the ATSI type in HBO
2607 * @param abs_value the absolute value of the property
2608 * @param rel_value the normalized value
2611 GAS_ril_address_property_changed (void *solver,
2612 struct ATS_Address *address,
2617 LOG(GNUNET_ERROR_TYPE_DEBUG,
2618 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2619 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2620 address->addr, rel_value);
2622 struct GAS_RIL_Handle *s = solver;
2624 s->parameters.temperature = s->parameters.temperature_init;
2625 s->parameters.explore_ratio = s->parameters.explore_ratio_init;
2630 * Update the session of an address in the solver
2632 * NOTE: values in addresses are already updated
2634 * @param solver solver handle
2635 * @param address the address
2636 * @param cur_session the current session
2637 * @param new_session the new session
2640 GAS_ril_address_session_changed (void *solver,
2641 struct ATS_Address *address,
2642 uint32_t cur_session,
2643 uint32_t new_session)
2645 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2649 * Notify the solver that an address is (not) actively used by transport
2650 * to communicate with a remote peer
2652 * NOTE: values in addresses are already updated
2654 * @param solver solver handle
2655 * @param address the address
2656 * @param in_use usage state
2659 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2661 LOG(GNUNET_ERROR_TYPE_DEBUG,
2662 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2663 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2667 * Notify solver that the network an address is located in has changed
2669 * NOTE: values in addresses are already updated
2671 * @param solver solver handle
2672 * @param address the address
2673 * @param current_network the current network
2674 * @param new_network the new network
2677 GAS_ril_address_change_network (void *solver,
2678 struct ATS_Address *address,
2679 uint32_t current_network,
2680 uint32_t new_network)
2682 struct GAS_RIL_Handle *s = solver;
2683 struct RIL_Peer_Agent *agent;
2685 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2686 "%s address of peer %s from '%s' to '%s'\n",
2687 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2688 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2690 s->parameters.temperature = s->parameters.temperature_init;
2691 s->parameters.explore_ratio = s->parameters.explore_ratio_init;
2693 if (address->active && !ril_network_is_active (solver, new_network))
2695 GAS_ril_address_delete (solver, address, GNUNET_NO);
2699 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2702 GNUNET_assert(!ril_network_is_active (solver, current_network));
2704 GAS_ril_address_add (s, address, new_network);
2708 address->solver_information = ril_get_network(solver, new_network);
2712 * Give feedback about the current assignment
2714 * @param solver the solver handle
2715 * @param application the application
2716 * @param peer the peer to change the preference for
2717 * @param scope the time interval for this feedback: [now - scope .. now]
2718 * @param kind the kind to change the preference
2719 * @param score the score
2722 GAS_ril_address_preference_feedback (void *solver,
2724 const struct GNUNET_PeerIdentity *peer,
2725 const struct GNUNET_TIME_Relative scope,
2726 enum GNUNET_ATS_PreferenceKind kind,
2729 LOG(GNUNET_ERROR_TYPE_DEBUG,
2730 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2731 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2732 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2736 * Start a bulk operation
2738 * @param solver the solver
2741 GAS_ril_bulk_start (void *solver)
2743 struct GAS_RIL_Handle *s = solver;
2745 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2751 * Bulk operation done
2753 * @param solver the solver handle
2756 GAS_ril_bulk_stop (void *solver)
2758 struct GAS_RIL_Handle *s = solver;
2760 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2762 if (s->bulk_lock < 1)
2769 if (0 < s->bulk_changes)
2772 s->bulk_changes = 0;
2777 * Tell solver to notify ATS if the address to use changes for a specific
2778 * peer using the bandwidth changed callback
2780 * The solver must only notify about changes for peers with pending address
2783 * @param solver the solver handle
2784 * @param peer the identity of the peer
2786 const struct ATS_Address *
2787 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2789 struct GAS_RIL_Handle *s = solver;
2790 struct RIL_Peer_Agent *agent;
2792 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2794 agent = ril_get_agent (s, peer, GNUNET_YES);
2796 agent->is_active = GNUNET_YES;
2797 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2799 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2801 if (agent->address_inuse)
2803 LOG(GNUNET_ERROR_TYPE_DEBUG,
2804 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2805 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2809 LOG(GNUNET_ERROR_TYPE_DEBUG,
2810 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2812 s->parameters.temperature = s->parameters.temperature_init;
2813 s->parameters.explore_ratio = s->parameters.explore_ratio_init;
2815 return agent->address_inuse;
2819 * Tell solver stop notifying ATS about changes for this peers
2821 * The solver must only notify about changes for peers with pending address
2824 * @param solver the solver handle
2825 * @param peer the peer
2828 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2830 struct GAS_RIL_Handle *s = solver;
2831 struct RIL_Peer_Agent *agent;
2833 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2835 agent = ril_get_agent (s, peer, GNUNET_NO);
2842 if (GNUNET_NO == agent->is_active)
2848 s->parameters.temperature = s->parameters.temperature_init;
2849 s->parameters.explore_ratio = s->parameters.explore_ratio_init;
2851 agent->is_active = GNUNET_NO;
2853 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2858 LOG(GNUNET_ERROR_TYPE_DEBUG,
2859 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2860 GNUNET_i2s (peer), agent->address_inuse->plugin);
2863 /* end of plugin_ats_ril.c */