2 This file is part of GNUnet.
3 (C) 2011-2014 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
30 #include "gnunet_ats_plugin.h"
31 #include "gnunet-service-ats_addresses.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
37 #define RIL_MIN_BW (5 * ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__))
38 #define RIL_MAX_BW GNUNET_ATS_MaxBandwidth
40 #define RIL_ACTION_INVALID -1
41 #define RIL_INTERVAL_EXPONENT 10
42 #define RIL_UTILITY_DELAY_MAX 1000
44 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200)
45 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 2000)
46 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
47 #define RIL_DEFAULT_SELECT RIL_SELECT_SOFTMAX
48 #define RIL_DEFAULT_WELFARE RIL_WELFARE_NASH
49 #define RIL_DEFAULT_DISCOUNT_BETA 0.6
50 #define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
51 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.01
52 #define RIL_DEFAULT_TRACE_DECAY 0.5
53 #define RIL_DEFAULT_EXPLORE_RATIO 1
54 #define RIL_DEFAULT_EXPLORE_DECAY 0.95
55 #define RIL_DEFAULT_RBF_DIVISOR 50
56 #define RIL_DEFAULT_TEMPERATURE 0.1
57 #define RIL_DEFAULT_TEMPERATURE_DECAY 1
59 #define RIL_INC_DEC_STEP_SIZE 1
60 #define RIL_NOP_DECAY 0.5
63 * ATS reinforcement learning solver
69 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
70 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
71 * to a particular address. The action of switching to address with index i is depicted by the
72 * number (RIL_ACTION_TYPE_NUM + i).
76 RIL_ACTION_NOTHING = 0,
77 RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions
78 RIL_ACTION_BW_IN_HLV = -3,
79 RIL_ACTION_BW_IN_INC = 1,
80 RIL_ACTION_BW_IN_DEC = 2,
81 RIL_ACTION_BW_OUT_DBL = -4,
82 RIL_ACTION_BW_OUT_HLV = -5,
83 RIL_ACTION_BW_OUT_INC = 3,
84 RIL_ACTION_BW_OUT_DEC = 4,
85 RIL_ACTION_TYPE_NUM = 5
96 RIL_SELECT_SOFTMAX = 0,
97 RIL_SELECT_EGREEDY = 1
103 RIL_WELFARE_EGALITARIAN
106 enum RIL_E_Modification
115 * Global learning parameters
117 struct RIL_Learning_Parameters
120 * The TD-algorithm to use
122 enum RIL_Algorithm algorithm;
125 * Gradient-descent step-size
130 * Learning discount variable in the TD-update for semi-MDPs
135 * Learning discount factor in the TD-update for MDPs
140 * Trace-decay factor for eligibility traces
145 * Whether to accumulate or replace eligibility traces
147 enum RIL_E_Modification eligibility_trace_mode;
150 * Initial softmax action-selection temperature
152 double temperature_init;
155 * Softmax action-selection temperature
160 * Decay factor of the temperature value
162 double temperature_decay;
165 * Which measure of social welfare should be used
167 enum RIL_Welfare social_welfare;
170 * State space divisor
172 unsigned long long rbf_divisor;
175 * Action selection strategy;
177 enum RIL_Select select;
180 * Initial exploration ratio value
185 * Ratio, with what probability an agent should explore in the e-greed policy
190 * Decay factor of the explore ratio
192 double epsilon_decay;
195 * Minimal interval time between steps in milliseconds
197 struct GNUNET_TIME_Relative step_time_min;
200 * Maximum interval time between steps in milliseconds
202 struct GNUNET_TIME_Relative step_time_max;
206 * Wrapper for addresses to store them in agent's linked list
208 struct RIL_Address_Wrapped
213 struct RIL_Address_Wrapped *next;
218 struct RIL_Address_Wrapped *prev;
223 struct ATS_Address *address_naked;
226 struct RIL_Peer_Agent
229 * Next agent in solver's linked list
231 struct RIL_Peer_Agent *next;
234 * Previous agent in solver's linked list
236 struct RIL_Peer_Agent *prev;
241 struct GAS_RIL_Handle *envi;
246 struct GNUNET_PeerIdentity peer;
249 * Whether the agent is active or not
254 * Number of performed time-steps
256 unsigned long long step_count;
259 * Experience matrix W
264 * Number of rows of W / Number of state-vector features
269 * Number of columns of W / Number of actions
274 * Last perceived state feature vector
289 * Whether to reset the eligibility traces to 0 after a Q-exploration step
291 int eligibility_reset;
296 struct ATS_Address * address_inuse;
299 * Head of addresses DLL
301 struct RIL_Address_Wrapped * addresses_head;
304 * Tail of addresses DLL
306 struct RIL_Address_Wrapped * addresses_tail;
309 * Inbound bandwidth assigned by the agent
314 * Outbound bandwidth assigned by the agent
319 * Flag whether a suggestion has to be issued
321 int suggestion_issue;
324 * The address which has to be issued
326 struct ATS_Address * suggestion_address;
329 * The agent's last objective value
331 double objective_old;
344 enum GNUNET_ATS_Network_Type type;
347 * Total available inbound bandwidth
349 uint32_t bw_in_available;
352 * Bandwidth inbound assigned in network after last step
354 uint32_t bw_in_assigned;
357 * Bandwidth inbound actually utilized in the network
359 uint32_t bw_in_utilized;
362 * Total available outbound bandwidth
364 uint32_t bw_out_available;
367 * Bandwidth outbound assigned in network after last step
369 unsigned long long bw_out_assigned;
372 * Bandwidth outbound actually utilized in the network
374 unsigned long long bw_out_utilized;
377 * Number of active agents in scope
379 unsigned int active_agent_count;
382 * The social welfare achieved in the scope
384 double social_welfare;
388 * A handle for the reinforcement learning solver
390 struct GAS_RIL_Handle
393 * The solver-plugin environment of the solver-plugin API
395 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
400 struct GNUNET_STATISTICS_Handle *stats;
403 * Number of performed steps
405 unsigned long long step_count;
408 * Timestamp for the last time-step
410 struct GNUNET_TIME_Absolute step_time_last;
413 * Task identifier of the next time-step to be executed
415 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
418 * Variable discount factor, dependent on time between steps
420 double global_discount_variable;
423 * Integrated variable discount factor, dependent on time between steps
425 double global_discount_integrated;
428 * Lock for bulk operations
433 * Number of changes during a lock
438 * Learning parameters
440 struct RIL_Learning_Parameters parameters;
443 * Array of networks with global assignment state
445 struct RIL_Scope * network_entries;
450 unsigned int networks_count;
453 * List of active peer-agents
455 struct RIL_Peer_Agent * agents_head;
456 struct RIL_Peer_Agent * agents_tail;
464 * Simulate steps, i.e. schedule steps immediately
466 unsigned long long simulate;
470 * "Private" functions
471 * ---------------------------
475 * Estimate the current action-value for state s and action a
477 * @param agent agent performing the estimation
480 * @return estimation value
483 agent_q (struct RIL_Peer_Agent *agent, double *state, int action)
488 for (i = 0; i < agent->m; i++)
490 result += state[i] * agent->W[action][i];
493 GNUNET_assert(!isnan(result));
495 //prevent crash when learning diverges
498 return isinf(result) * UINT32_MAX;
505 * Get the index of the address in the agent's list.
507 * @param agent agent handle
508 * @param address address handle
509 * @return the index, starting with zero
512 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
515 struct RIL_Address_Wrapped *cur;
518 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
521 if (cur->address_naked == address)
529 * Gets the wrapped address from the agent's list
531 * @param agent agent handle
532 * @param address address handle
533 * @return wrapped address
535 static struct RIL_Address_Wrapped *
536 agent_address_get_wrapped (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
538 struct RIL_Address_Wrapped *cur;
540 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
541 if (cur->address_naked == address)
548 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
554 case RIL_ACTION_NOTHING:
557 case RIL_ACTION_BW_IN_INC:
558 case RIL_ACTION_BW_IN_DBL:
559 if (agent->bw_in >= RIL_MAX_BW)
564 case RIL_ACTION_BW_IN_DEC:
565 case RIL_ACTION_BW_IN_HLV:
566 if (agent->bw_in <= 0)
571 case RIL_ACTION_BW_OUT_INC:
572 case RIL_ACTION_BW_OUT_DBL:
573 if (agent->bw_out >= RIL_MAX_BW)
578 case RIL_ACTION_BW_OUT_DEC:
579 case RIL_ACTION_BW_OUT_HLV:
580 if (agent->bw_out <= 0)
586 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
588 address_index = action - RIL_ACTION_TYPE_NUM;
590 GNUNET_assert(address_index >= 0);
592 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
594 if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
595 agent->address_inuse->active)
601 // error - action does not exist
602 GNUNET_assert(GNUNET_NO);
608 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
609 * most reward in the future)
611 * @param agent agent performing the calculation
612 * @param state the state from which to take the action
613 * @return the action promising most future reward
616 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
619 int max_i = RIL_ACTION_INVALID;
621 double max_q = -DBL_MAX;
623 for (i = 0; i < agent->n; i++)
625 if (agent_action_is_possible(agent, i))
627 cur_q = agent_q (agent, state, i);
636 GNUNET_assert(RIL_ACTION_INVALID != max_i);
642 * Chooses a random action from the set of possible ones
644 * @param agent the agent performing the action
645 * @return the action index
648 agent_get_action_random (struct RIL_Peer_Agent *agent)
651 int is_possible[agent->n];
655 for (i = 0; i<agent->n; i++)
657 if (agent_action_is_possible(agent, i))
659 is_possible[i] = GNUNET_YES;
664 is_possible[i] = GNUNET_NO;
668 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
671 for (i = 0; i<agent->n; i++)
681 GNUNET_assert(GNUNET_NO);
682 return RIL_ACTION_INVALID;
687 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
689 * @param agent the agent performing the update
690 * @param reward the reward received for the last action
691 * @param s_next the new state, the last step got the agent into
692 * @param a_prime the new
695 agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
700 double **theta = agent->W;
702 delta = agent->envi->global_discount_integrated * reward; //reward
703 delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
704 delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
706 // 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",
707 // agent->step_count,
708 // agent_q (agent, agent->s_old, agent->a_old),
709 // agent->envi->parameters.alpha,
711 // agent->envi->global_discount_variable,
712 // agent_q (agent, s_next, a_prime),
715 for (k = 0; k < agent->n; k++)
717 for (i = 0; i < agent->m; i++)
719 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
720 // agent->envi->parameters.alpha,
724 theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
731 * Changes the eligibility trace vector e in various manners:
732 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
733 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
734 * #RIL_E_DECAY - multiplies e with discount factor and lambda as in the update rule
735 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
737 * @param agent the agent handle
738 * @param mod the kind of modification
739 * @param feature the feature vector
740 * @param action the action to take
743 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
744 enum RIL_E_Modification mod,
751 for (i = 0; i < agent->m; i++)
755 case RIL_E_ACCUMULATE:
756 agent->E[action][i] += feature[i];
759 agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
762 for (k = 0; k < agent->n; k++)
764 agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
768 for (k = 0; k < agent->n; k++)
778 * Informs the environment about the status of the solver
785 ril_inform (struct GAS_RIL_Handle *solver,
786 enum GAS_Solver_Operation op,
787 enum GAS_Solver_Status stat)
789 if (NULL != solver->plugin_envi->info_cb)
790 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
794 * Calculates the maximum bandwidth an agent can assign in a network scope
798 static unsigned long long
799 ril_get_max_bw (struct RIL_Scope *net)
801 return GNUNET_MIN(2 * GNUNET_MAX(net->bw_in_available, net->bw_out_available), GNUNET_ATS_MaxBandwidth);
805 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
806 * notify ATS of its new decision
808 * @param solver solver handle
809 * @param agent agent handle
810 * @param new_address the address which is to be used
811 * @param new_bw_in the new amount of inbound bandwidth set for this address
812 * @param new_bw_out the new amount of outbound bandwidth set for this address
813 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
816 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
817 struct RIL_Peer_Agent *agent,
818 struct ATS_Address *new_address,
819 unsigned long long new_bw_in,
820 unsigned long long new_bw_out,
823 int notify = GNUNET_NO;
825 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
828 if (agent->address_inuse != new_address)
830 if (NULL != agent->address_inuse)
832 agent->address_inuse->active = GNUNET_NO;
833 agent->address_inuse->assigned_bw_in = 0;
834 agent->address_inuse->assigned_bw_out = 0;
836 if (NULL != new_address)
838 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
839 new_address->active = agent->is_active;
840 new_address->assigned_bw_in = agent->bw_in;
841 new_address->assigned_bw_out = agent->bw_out;
843 notify |= GNUNET_YES;
849 if (new_address->active != agent->is_active)
851 new_address->active = agent->is_active;
852 notify |= GNUNET_YES;
856 if (agent->bw_in != new_bw_in)
858 agent->bw_in = new_bw_in;
859 new_address->assigned_bw_in = new_bw_in;
860 notify |= GNUNET_YES;
862 if (agent->bw_out != new_bw_out)
864 agent->bw_out = new_bw_out;
865 new_address->assigned_bw_out = new_bw_out;
866 notify |= GNUNET_YES;
870 if (notify && agent->is_active && (GNUNET_NO == silent))
874 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
875 agent->suggestion_issue = GNUNET_YES;
876 agent->suggestion_address = new_address;
878 else if (agent->address_inuse)
880 /* disconnect case, no new address */
881 GNUNET_assert(0 == agent->address_inuse->assigned_bw_in);
882 GNUNET_assert(0 == agent->address_inuse->assigned_bw_out);
886 agent->suggestion_issue = GNUNET_YES;
887 agent->suggestion_address = agent->address_inuse;
890 agent->address_inuse = new_address;
895 * Allocates a state vector and fills it with the features present
896 * @param solver the solver handle
897 * @param agent the agent handle
898 * @return pointer to the state vector
901 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
912 unsigned long long max_bw;
914 state = GNUNET_malloc (sizeof(double) * agent->m);
916 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
918 y[0] = (double) agent->bw_out;
919 y[1] = (double) agent->bw_in;
921 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
922 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
924 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
926 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
927 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
930 sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
931 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
940 * Retrieves an ATS information value of an address
942 * @param address the address in question
943 * @param type the ATS information type
947 ril_get_atsi (struct ATS_Address *address, uint32_t type)
950 GNUNET_assert(NULL != address);
952 if ((NULL == address->atsi) || (0 == address->atsi_count))
953 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
955 for (c1 = 0; c1 < address->atsi_count; c1++)
957 if (ntohl (address->atsi[c1].type) == type)
958 return ntohl (address->atsi[c1].value);
960 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
964 * Returns the utility value of the connection an agent manages
966 * @param agent the agent in question
967 * @return the utility value
970 agent_get_utility (struct RIL_Peer_Agent *agent)
972 const double *preferences;
977 preferences = agent->envi->plugin_envi->get_preferences (agent->envi->plugin_envi->get_preference_cls,
980 delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
981 delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
983 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
984 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
985 sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
986 // sqrt((double) (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)/RIL_MIN_BW) * (double) (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)/RIL_MIN_BW));
988 // return (double) (agent->bw_in/RIL_MIN_BW);
989 // return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
994 * Calculates the social welfare within a network scope according to what social
995 * welfare measure is set in the configuration.
997 * @param solver the solver handle
998 * @param scope the network scope in question
999 * @return the social welfare value
1002 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1004 struct RIL_Peer_Agent *cur;
1007 switch (solver->parameters.social_welfare)
1009 case RIL_WELFARE_EGALITARIAN:
1011 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1013 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1015 result = GNUNET_MIN(result, agent_get_utility(cur));
1020 case RIL_WELFARE_NASH:
1022 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1024 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1026 result *= pow(agent_get_utility(cur), 1.0 / (double) scope->active_agent_count);
1031 GNUNET_assert(GNUNET_NO);
1036 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1038 struct RIL_Scope *net;
1039 unsigned long long over_max;
1040 unsigned long long over_in = 0;
1041 unsigned long long over_out = 0;
1043 net = agent->address_inuse->solver_information;
1045 if (net->bw_in_utilized > net->bw_in_available)
1047 over_in = net->bw_in_utilized - net->bw_in_available;
1048 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1050 /* increase quadratically */
1054 if (net->bw_out_utilized > net->bw_out_available)
1056 over_out = net->bw_out_utilized - net->bw_out_available;
1057 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1059 /* increase quadratically */
1060 over_out *= over_out;
1063 over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
1065 return -1.0 * (double) over_max;
1069 * Gets the reward for the last performed step, which is calculated in equal
1070 * parts from the local (the peer specific) and the global (for all peers
1071 * identical) reward.
1073 * @param solver the solver handle
1074 * @param agent the agent handle
1075 * @return the reward
1078 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1080 struct RIL_Scope *net;
1087 net = agent->address_inuse->solver_information;
1089 penalty = envi_get_penalty(solver, agent);
1090 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1091 delta = objective - agent->objective_old;
1092 agent->objective_old = objective;
1094 if (delta != 0 && penalty == 0)
1096 agent->nop_bonus = delta * RIL_NOP_DECAY;
1100 agent->nop_bonus *= RIL_NOP_DECAY;
1103 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1105 reward = delta + steady;
1106 return reward + penalty;
1110 * Doubles the bandwidth for the active address
1112 * @param solver solver handle
1113 * @param agent agent handle
1114 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1117 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1118 struct RIL_Peer_Agent *agent,
1121 unsigned long long new_bw;
1122 unsigned long long max_bw;
1124 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1128 new_bw = agent->bw_in * 2;
1129 if (new_bw < agent->bw_in || new_bw > max_bw)
1131 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1132 agent->bw_out, GNUNET_NO);
1136 new_bw = agent->bw_out * 2;
1137 if (new_bw < agent->bw_out || new_bw > max_bw)
1139 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1145 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1146 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1148 * @param solver solver handle
1149 * @param agent agent handle
1150 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1154 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1155 struct RIL_Peer_Agent *agent,
1158 unsigned long long new_bw;
1162 new_bw = agent->bw_in / 2;
1163 if (new_bw <= 0 || new_bw > agent->bw_in)
1165 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1170 new_bw = agent->bw_out / 2;
1171 if (new_bw <= 0 || new_bw > agent->bw_out)
1173 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1179 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1181 * @param solver solver handle
1182 * @param agent agent handle
1183 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1187 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1189 unsigned long long new_bw;
1190 unsigned long long max_bw;
1192 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1196 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1197 if (new_bw < agent->bw_in || new_bw > max_bw)
1199 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1200 agent->bw_out, GNUNET_NO);
1204 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1205 if (new_bw < agent->bw_out || new_bw > max_bw)
1207 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1213 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1214 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1216 * @param solver solver handle
1217 * @param agent agent handle
1218 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1222 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1224 unsigned long long new_bw;
1228 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1229 if (new_bw <= 0 || new_bw > agent->bw_in)
1231 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1236 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1237 if (new_bw <= 0 || new_bw > agent->bw_out)
1239 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1245 * Switches to the address given by its index
1247 * @param solver solver handle
1248 * @param agent agent handle
1249 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1252 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1253 struct RIL_Peer_Agent *agent,
1254 unsigned int address_index)
1256 struct RIL_Address_Wrapped *cur;
1259 //cur = agent_address_get_wrapped(agent, agent->address_inuse);
1261 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1263 if (i == address_index)
1265 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1273 //no address with address_index exists, in this case this action should not be callable
1274 GNUNET_assert(GNUNET_NO);
1278 * Puts the action into effect by calling the according function
1280 * @param solver the solver handle
1281 * @param agent the action handle
1282 * @param action the action to perform by the solver
1285 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1291 case RIL_ACTION_NOTHING:
1293 case RIL_ACTION_BW_IN_DBL:
1294 envi_action_bw_double (solver, agent, GNUNET_YES);
1296 case RIL_ACTION_BW_IN_HLV:
1297 envi_action_bw_halven (solver, agent, GNUNET_YES);
1299 case RIL_ACTION_BW_IN_INC:
1300 envi_action_bw_inc (solver, agent, GNUNET_YES);
1302 case RIL_ACTION_BW_IN_DEC:
1303 envi_action_bw_dec (solver, agent, GNUNET_YES);
1305 case RIL_ACTION_BW_OUT_DBL:
1306 envi_action_bw_double (solver, agent, GNUNET_NO);
1308 case RIL_ACTION_BW_OUT_HLV:
1309 envi_action_bw_halven (solver, agent, GNUNET_NO);
1311 case RIL_ACTION_BW_OUT_INC:
1312 envi_action_bw_inc (solver, agent, GNUNET_NO);
1314 case RIL_ACTION_BW_OUT_DEC:
1315 envi_action_bw_dec (solver, agent, GNUNET_NO);
1318 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1320 address_index = action - RIL_ACTION_TYPE_NUM;
1322 GNUNET_assert(address_index >= 0);
1324 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1326 envi_action_address_switch (solver, agent, address_index);
1329 // error - action does not exist
1330 GNUNET_assert(GNUNET_NO);
1335 * Selects the next action using the e-greedy strategy. I.e. with a probability
1336 * of (1-e) the action with the maximum expected return will be chosen
1337 * (=> exploitation) and with probability (e) a random action will be chosen.
1338 * In case the Q-learning rule is set, the function also resets the eligibility
1339 * traces in the exploration case (after Watkin's Q-learning).
1341 * @param agent the agent selecting an action
1342 * @param state the current state-feature vector
1343 * @return the action index
1346 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1349 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1350 UINT32_MAX) / (double) UINT32_MAX;
1352 if (r < agent->envi->parameters.epsilon) //explore
1354 action = agent_get_action_random(agent);
1355 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1357 agent->eligibility_reset = GNUNET_YES;
1359 agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
1364 action = agent_get_action_max(agent, state);
1370 * Selects the next action with a probability corresponding to its value. The
1371 * probability is calculated using a Boltzmann distribution with a temperature
1372 * value. The higher the temperature, the more are the action selection
1373 * probabilities the same. With a temperature of 0, the selection is greedy,
1374 * i.e. always the action with the highest value is chosen.
1380 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1384 double eqt[agent->n];
1389 a_max = agent_get_action_max(agent, state);
1391 for (i=0; i<agent->n; i++)
1393 if (agent_action_is_possible(agent, i))
1395 eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1399 for (i=0; i<agent->n; i++)
1401 if (agent_action_is_possible(agent, i))
1410 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1411 UINT32_MAX) / (double) UINT32_MAX;
1413 for (i=0; i<agent->n; i++)
1419 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1420 agent->eligibility_reset = GNUNET_YES;
1421 agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay;
1427 GNUNET_assert(GNUNET_NO);
1428 return RIL_ACTION_INVALID;
1432 * Select the next action of an agent either according to the e-greedy strategy
1433 * or the softmax strategy.
1435 * @param agent the agent in question
1436 * @param state the current state-feature vector
1437 * @return the action index
1440 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1442 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1444 return agent_select_egreedy(agent, state);
1448 return agent_select_softmax(agent, state);
1453 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1454 * after having done the last action a_old. It observes the new state s_next and the reward
1455 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1456 * next action is put into effect.
1458 * @param agent the agent performing the step
1461 agent_step (struct RIL_Peer_Agent *agent)
1463 int a_next = RIL_ACTION_INVALID;
1468 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1469 GNUNET_i2s (&agent->peer),
1470 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1472 s_next = envi_get_state (agent->envi, agent);
1473 reward = envi_get_reward (agent->envi, agent);
1475 if (agent->eligibility_reset)
1477 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1478 agent->eligibility_reset = GNUNET_NO;
1482 agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
1484 if (RIL_ACTION_INVALID != agent->a_old)
1486 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1489 switch (agent->envi->parameters.algorithm)
1491 case RIL_ALGO_SARSA:
1492 a_next = agent_select_action (agent, s_next);
1493 if (RIL_ACTION_INVALID != agent->a_old)
1495 //updates weights with selected action (on-policy), if not first step
1496 agent_update (agent, reward, s_next, a_next);
1501 a_max = agent_get_action_max (agent, s_next);
1502 if (RIL_ACTION_INVALID != agent->a_old)
1504 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1505 agent_update (agent, reward, s_next, a_max);
1507 a_next = agent_select_action (agent, s_next);
1511 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1513 LOG (GNUNET_ERROR_TYPE_DEBUG, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1520 envi_do_action (agent->envi, agent, a_next);
1522 GNUNET_free(agent->s_old);
1523 agent->s_old = s_next;
1524 agent->a_old = a_next;
1526 agent->step_count += 1;
1530 * Prototype of the ril_step() procedure
1532 * @param solver the solver handle
1535 ril_step (struct GAS_RIL_Handle *solver);
1538 * Task for the scheduler, which performs one step and lets the solver know that
1539 * no further step is scheduled.
1541 * @param cls the solver handle
1542 * @param tc the task context for the scheduler
1545 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1547 struct GAS_RIL_Handle *solver = cls;
1549 solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1554 * Determines how much of the available bandwidth is assigned. If more is
1555 * assigned than available it returns 1. The function is used to determine the
1556 * step size of the adaptive stepping.
1558 * @param solver the solver handle
1562 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1565 struct RIL_Scope net;
1566 unsigned long long sum_assigned = 0;
1567 unsigned long long sum_available = 0;
1570 for (i = 0; i < solver->networks_count; i++)
1572 net = solver->network_entries[i];
1573 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1575 sum_assigned += net.bw_in_utilized;
1576 sum_assigned += net.bw_out_utilized;
1577 sum_available += net.bw_in_available;
1578 sum_available += net.bw_out_available;
1581 if (sum_available > 0)
1583 ratio = ((double) sum_assigned) / ((double) sum_available);
1590 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1594 * Lookup network struct by type
1596 * @param s the solver handle
1597 * @param type the network type
1598 * @return the network struct
1600 static struct RIL_Scope *
1601 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1605 for (i = 0; i < s->networks_count; i++)
1607 if (s->network_entries[i].type == type)
1609 return &s->network_entries[i];
1616 * Determines whether more connections are allocated in a network scope, than
1617 * they would theoretically fit. This is used as a heuristic to determine,
1618 * whether a new connection can be allocated or not.
1620 * @param solver the solver handle
1621 * @param network the network scope in question
1622 * @return GNUNET_YES if there are theoretically enough resources left
1625 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1627 struct RIL_Scope *net;
1628 struct RIL_Peer_Agent *agent;
1629 unsigned long long address_count = 0;
1631 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1633 if (agent->address_inuse && agent->is_active)
1635 net = agent->address_inuse->solver_information;
1636 if (net->type == network)
1643 net = ril_get_network (solver, network);
1644 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1648 * Unblocks an agent for which a connection request is there, that could not
1649 * be satisfied. Iterates over the addresses of the agent, if one of its
1650 * addresses can now be allocated in its scope the agent is unblocked,
1651 * otherwise it remains unchanged.
1653 * @param solver the solver handle
1654 * @param agent the agent in question
1658 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1660 struct RIL_Address_Wrapped *addr_wrap;
1661 struct RIL_Scope *net;
1662 unsigned long long start_in;
1663 unsigned long long start_out;
1665 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1667 net = addr_wrap->address_naked->solver_information;
1668 if (ril_network_is_not_full(solver, net->type))
1670 if (NULL == agent->address_inuse)
1672 start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
1673 start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
1674 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, start_in, start_out, silent);
1679 agent->address_inuse = NULL;
1683 * Determines how much the reward needs to be discounted depending on the amount
1684 * of time, which has passed since the last time-step.
1686 * @param solver the solver handle
1689 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1691 struct GNUNET_TIME_Absolute time_now;
1692 struct GNUNET_TIME_Relative time_delta;
1695 // MDP case only for debugging purposes
1696 if (solver->simulate)
1698 solver->global_discount_variable = solver->parameters.gamma;
1699 solver->global_discount_integrated = 1;
1705 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1706 time_now = GNUNET_TIME_absolute_get ();
1707 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1708 solver->step_time_last = time_now;
1709 tau = (double) time_delta.rel_value_us
1710 / (double) solver->parameters.step_time_min.rel_value_us;
1712 //calculate reward discounts (once per step for all agents)
1713 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1714 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1715 / (double) solver->parameters.beta;
1719 * Count the number of active agents/connections in a network scope
1721 * @param solver the solver handle
1722 * @param scope the network scope in question
1723 * @return the number of allocated connections
1726 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1729 struct RIL_Peer_Agent *cur_agent;
1731 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1733 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1742 * Calculates how much bandwidth is assigned in sum in a network scope, either
1743 * in the inbound or in the outbound direction.
1745 * @param solver the solver handle
1746 * @param type the type of the network scope in question
1747 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1748 * otherwise the outbound direction will be summed up
1749 * @return the sum of the assigned bandwidths
1751 static unsigned long long
1752 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1754 struct RIL_Peer_Agent *cur;
1755 struct RIL_Scope *net;
1756 unsigned long long sum = 0;
1758 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1760 if (cur->is_active && cur->address_inuse)
1762 net = cur->address_inuse->solver_information;
1763 if (net->type == type)
1777 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1778 * either in the inbound or in the outbound direction.
1780 * @param solver the solver handle
1781 * @param type the type of the network scope in question
1782 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1783 * otherwise the outbound direction will be summed up
1784 * @return the sum of the utilized bandwidths (in bytes/second)
1786 static unsigned long long
1787 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1789 struct RIL_Peer_Agent *cur;
1790 struct RIL_Scope *net;
1791 unsigned long long sum = 0;
1793 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1795 if (cur->is_active && cur->address_inuse)
1797 net = cur->address_inuse->solver_information;
1798 if (net->type == type)
1801 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
1803 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
1812 * Retrieves the state of the network scope, so that its attributes are up-to-
1815 * @param solver the solver handle
1818 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1821 struct RIL_Scope *net;
1823 for (c = 0; c < solver->networks_count; c++)
1825 net = &solver->network_entries[c];
1826 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1827 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1828 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1829 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1830 net->active_agent_count = ril_network_count_active_agents(solver, net);
1831 net->social_welfare = ril_network_get_social_welfare(solver, net);
1836 * Schedules the next global step in an adaptive way. The more resources are
1837 * left, the earlier the next step is scheduled. This serves the reactivity of
1838 * the solver to changed inputs.
1840 * @param solver the solver handle
1843 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1849 struct GNUNET_TIME_Relative time_next;
1851 used_ratio = ril_get_used_resource_ratio (solver);
1854 solver->parameters.step_time_min.rel_value_us
1855 <= solver->parameters.step_time_max.rel_value_us);
1857 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1858 solver->parameters.step_time_min).rel_value_us;
1859 offset = (double) solver->parameters.step_time_min.rel_value_us;
1860 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1862 GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
1863 GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
1865 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1867 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1873 if (solver->simulate)
1875 time_next = GNUNET_TIME_UNIT_ZERO;
1878 if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1880 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1886 * Triggers one step per agent
1891 ril_step (struct GAS_RIL_Handle *solver)
1893 struct RIL_Peer_Agent *cur;
1895 if (GNUNET_YES == solver->bulk_lock)
1897 solver->bulk_changes++;
1901 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1903 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1905 if (0 == solver->step_count)
1907 solver->step_time_last = GNUNET_TIME_absolute_get ();
1910 ril_calculate_discount (solver);
1911 ril_networks_update_state (solver);
1913 //trigger one step per active, unblocked agent
1914 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1918 if (NULL == cur->address_inuse)
1920 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1922 if (cur->address_inuse)
1929 ril_networks_update_state (solver);
1931 solver->step_count += 1;
1932 ril_step_schedule_next (solver);
1934 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1936 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1937 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1939 if (cur->suggestion_issue) {
1940 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1941 cur->suggestion_issue = GNUNET_NO;
1944 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1948 * Initializes the matrix W of parameter vectors theta with small random numbers.
1950 * @param agent The respective agent
1953 agent_w_init (struct RIL_Peer_Agent *agent)
1958 for (i = 0; i < agent->n; i++)
1960 for (k = 0; k < agent->m; k++)
1962 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));
1968 * Initialize an agent without addresses and its knowledge base
1970 * @param s ril solver
1971 * @param peer the one in question
1972 * @return handle to the new agent
1974 static struct RIL_Peer_Agent *
1975 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1978 struct GAS_RIL_Handle * solver = s;
1979 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1981 agent->envi = solver;
1982 agent->peer = *peer;
1983 agent->step_count = 0;
1984 agent->is_active = GNUNET_NO;
1985 agent->bw_in = RIL_MIN_BW;
1986 agent->bw_out = RIL_MIN_BW;
1987 agent->suggestion_issue = GNUNET_NO;
1988 agent->n = RIL_ACTION_TYPE_NUM;
1990 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1991 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1992 for (i = 0; i < agent->n; i++)
1994 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1995 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1997 agent_w_init(agent);
1998 agent->eligibility_reset = GNUNET_NO;
1999 agent->a_old = RIL_ACTION_INVALID;
2000 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
2001 agent->address_inuse = NULL;
2002 agent->objective_old = 0;
2003 agent->nop_bonus = 0;
2011 * @param solver the solver handle
2012 * @param agent the agent to retire
2015 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
2019 for (i = 0; i < agent->n; i++)
2021 GNUNET_free_non_null(agent->W[i]);
2022 GNUNET_free_non_null(agent->E[i]);
2024 GNUNET_free_non_null(agent->W);
2025 GNUNET_free_non_null(agent->E);
2026 GNUNET_free_non_null(agent->s_old);
2031 * Returns the agent for a peer
2033 * @param solver the solver handle
2034 * @param peer the identity of the peer
2035 * @param create whether or not to create an agent, if none is allocated yet
2038 static struct RIL_Peer_Agent *
2039 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
2041 struct RIL_Peer_Agent *cur;
2043 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2045 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
2053 cur = agent_init (solver, peer);
2054 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
2061 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
2062 * considered inactive and not used. Addresses in an inactive network are ignored.
2064 * @param solver solver handle
2065 * @param network the network type
2066 * @return whether or not the network is considered active
2069 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
2071 struct RIL_Scope *net;
2073 net = ril_get_network (solver, network);
2074 return net->bw_out_available >= RIL_MIN_BW;
2078 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
2079 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
2080 * frees the memory of the old vector, and redirects the pointer to the new one.
2082 * @param old pointer to the pointer to the first element of the vector
2083 * @param element_size byte size of the vector elements
2084 * @param hole_start the first element to cut out
2085 * @param hole_length the number of elements to cut out
2086 * @param old_length the length of the old vector
2089 ril_cut_from_vector (void **old,
2090 size_t element_size,
2091 unsigned int hole_start,
2092 unsigned int hole_length,
2093 unsigned int old_length)
2096 char *oldptr = (char *) *old;
2098 unsigned int bytes_before;
2099 unsigned int bytes_hole;
2100 unsigned int bytes_after;
2102 GNUNET_assert(old_length >= hole_length);
2103 GNUNET_assert(old_length >= (hole_start + hole_length));
2105 size = element_size * (old_length - hole_length);
2107 bytes_before = element_size * hole_start;
2108 bytes_hole = element_size * hole_length;
2109 bytes_after = element_size * (old_length - hole_start - hole_length);
2117 tmpptr = GNUNET_malloc (size);
2118 memcpy (tmpptr, oldptr, bytes_before);
2119 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2125 *old = (void *) tmpptr;
2129 * Solver API functions
2130 * ---------------------------
2134 * Change relative preference for quality in solver
2136 * @param solver the solver handle
2137 * @param peer the peer to change the preference for
2138 * @param kind the kind to change the preference
2139 * @param pref_rel the normalized preference value for this kind over all clients
2142 GAS_ril_address_change_preference (void *solver,
2143 const struct GNUNET_PeerIdentity *peer,
2144 enum GNUNET_ATS_PreferenceKind kind,
2147 LOG(GNUNET_ERROR_TYPE_DEBUG,
2148 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2149 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2151 struct GAS_RIL_Handle *s = solver;
2153 s->parameters.temperature = s->parameters.temperature_init;
2154 s->parameters.epsilon = s->parameters.epsilon_init;
2160 * Add a new address for a peer to the solver
2162 * The address is already contained in the addresses hashmap!
2164 * @param solver the solver Handle
2165 * @param address the address to add
2166 * @param network network type of this address
2169 GAS_ril_address_add (void *solver,
2170 struct ATS_Address *address,
2173 struct GAS_RIL_Handle *s = solver;
2174 struct RIL_Peer_Agent *agent;
2175 struct RIL_Address_Wrapped *address_wrapped;
2176 struct RIL_Scope *net;
2184 LOG (GNUNET_ERROR_TYPE_DEBUG,
2185 "API_address_add()\n");
2187 net = ril_get_network (s, network);
2188 address->solver_information = net;
2190 if (!ril_network_is_active (s, network))
2192 LOG(GNUNET_ERROR_TYPE_DEBUG,
2193 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2194 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2198 s->parameters.temperature = s->parameters.temperature_init;
2199 s->parameters.epsilon = s->parameters.epsilon_init;
2201 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2204 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2205 address_wrapped->address_naked = address;
2206 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2208 //increase size of W
2209 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2211 n_new = agent->n + 1;
2214 GNUNET_array_grow(agent->W, agent->n, n_new);
2216 GNUNET_array_grow(agent->E, agent->n, n_new);
2217 for (i = 0; i < n_new; i++)
2222 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2224 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2229 GNUNET_array_grow(agent->W[i], zero, m_new);
2231 GNUNET_array_grow(agent->E[i], zero, m_new);
2235 //increase size of old state vector
2237 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2239 ril_try_unblock_agent(s, agent, GNUNET_NO);
2243 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2244 address->active ? "active" : "inactive", address->plugin, address->addr,
2245 GNUNET_i2s (&address->peer));
2249 * Delete an address in the solver
2251 * The address is not contained in the address hashmap anymore!
2253 * @param solver the solver handle
2254 * @param address the address to remove
2255 * @param session_only delete only session not whole address
2258 GAS_ril_address_delete (void *solver,
2259 struct ATS_Address *address,
2262 struct GAS_RIL_Handle *s = solver;
2263 struct RIL_Peer_Agent *agent;
2264 struct RIL_Address_Wrapped *address_wrapped;
2265 int address_was_used = address->active;
2270 struct RIL_Scope *net;
2272 LOG (GNUNET_ERROR_TYPE_DEBUG,
2273 "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2274 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2276 GNUNET_i2s (&address->peer));
2278 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2281 net = address->solver_information;
2282 GNUNET_assert(!ril_network_is_active (s, net->type));
2283 LOG(GNUNET_ERROR_TYPE_DEBUG,
2284 "No agent allocated for peer yet, since address was in inactive network\n");
2288 s->parameters.temperature = s->parameters.temperature_init;
2289 s->parameters.epsilon = s->parameters.epsilon_init;
2291 address_index = agent_address_get_index (agent, address);
2292 address_wrapped = agent_address_get_wrapped (agent, address);
2294 if (NULL == address_wrapped)
2296 net = address->solver_information;
2297 LOG(GNUNET_ERROR_TYPE_DEBUG,
2298 "Address not considered by agent, address was in inactive network\n");
2302 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2303 GNUNET_free(address_wrapped);
2306 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2307 n_new = agent->n - 1;
2309 for (i = 0; i < agent->n; i++)
2311 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2312 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2313 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2314 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2315 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2316 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2318 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2319 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2320 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2322 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2324 //correct last action
2325 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2329 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2331 agent->a_old = RIL_ACTION_INVALID;
2333 //decrease old state vector
2334 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2335 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2336 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2340 if (address_was_used)
2342 if (NULL != agent->addresses_head) //if peer has an address left, use it
2344 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, agent->bw_in, agent->bw_out,
2349 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2357 * Update the properties of an address in the solver
2359 * @param solver solver handle
2360 * @param address the address
2361 * @param type the ATSI type in HBO
2362 * @param abs_value the absolute value of the property
2363 * @param rel_value the normalized value
2366 GAS_ril_address_property_changed (void *solver,
2367 struct ATS_Address *address,
2372 struct GAS_RIL_Handle *s = solver;
2374 LOG(GNUNET_ERROR_TYPE_DEBUG,
2375 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2377 GNUNET_ATS_print_property_type (type),
2378 GNUNET_i2s (&address->peer),
2379 address->addr, rel_value);
2382 s->parameters.temperature = s->parameters.temperature_init;
2383 s->parameters.epsilon = s->parameters.epsilon_init;
2389 * Update the session of an address in the solver
2391 * NOTE: values in addresses are already updated
2393 * @param solver solver handle
2394 * @param address the address
2395 * @param cur_session the current session
2396 * @param new_session the new session
2399 GAS_ril_address_session_changed (void *solver,
2400 struct ATS_Address *address,
2401 uint32_t cur_session,
2402 uint32_t new_session)
2404 LOG(GNUNET_ERROR_TYPE_DEBUG,
2405 "API_address_session_changed()\n");
2410 * Notify the solver that an address is (not) actively used by transport
2411 * to communicate with a remote peer
2413 * NOTE: values in addresses are already updated
2415 * @param solver solver handle
2416 * @param address the address
2417 * @param in_use usage state
2420 GAS_ril_address_inuse_changed (void *solver,
2421 struct ATS_Address *address,
2424 LOG(GNUNET_ERROR_TYPE_DEBUG,
2425 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2426 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2431 * Notify solver that the network an address is located in has changed
2433 * NOTE: values in addresses are already updated
2435 * @param solver solver handle
2436 * @param address the address
2437 * @param current_network the current network
2438 * @param new_network the new network
2441 GAS_ril_address_change_network (void *solver,
2442 struct ATS_Address *address,
2443 uint32_t current_network,
2444 uint32_t new_network)
2446 struct GAS_RIL_Handle *s = solver;
2447 struct RIL_Peer_Agent *agent;
2449 LOG(GNUNET_ERROR_TYPE_DEBUG,
2450 "API_address_change_network() Network type changed, moving "
2451 "%s address of peer %s from '%s' to '%s'\n",
2452 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2453 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2455 s->parameters.temperature = s->parameters.temperature_init;
2456 s->parameters.epsilon = s->parameters.epsilon_init;
2458 if (address->active && !ril_network_is_active (solver, new_network))
2460 GAS_ril_address_delete (solver, address, GNUNET_NO);
2464 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2467 GNUNET_assert(!ril_network_is_active (solver, current_network));
2469 GAS_ril_address_add (s, address, new_network);
2473 address->solver_information = ril_get_network(solver, new_network);
2478 * Give feedback about the current assignment
2480 * @param solver the solver handle
2481 * @param application the application
2482 * @param peer the peer to change the preference for
2483 * @param scope the time interval for this feedback: [now - scope .. now]
2484 * @param kind the kind to change the preference
2485 * @param score the score
2488 GAS_ril_address_preference_feedback (void *solver,
2490 const struct GNUNET_PeerIdentity *peer,
2491 const struct GNUNET_TIME_Relative scope,
2492 enum GNUNET_ATS_PreferenceKind kind,
2495 LOG(GNUNET_ERROR_TYPE_DEBUG,
2496 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2497 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2498 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2503 * Start a bulk operation
2505 * @param solver the solver
2508 GAS_ril_bulk_start (void *solver)
2510 struct GAS_RIL_Handle *s = solver;
2512 LOG (GNUNET_ERROR_TYPE_DEBUG,
2513 "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2520 * Bulk operation done
2522 * @param solver the solver handle
2525 GAS_ril_bulk_stop (void *solver)
2527 struct GAS_RIL_Handle *s = solver;
2529 LOG(GNUNET_ERROR_TYPE_DEBUG,
2530 "API_bulk_stop() lock: %d\n",
2533 if (s->bulk_lock < 1)
2540 if (0 < s->bulk_changes)
2543 s->bulk_changes = 0;
2549 * Tell solver to notify ATS if the address to use changes for a specific
2550 * peer using the bandwidth changed callback
2552 * The solver must only notify about changes for peers with pending address
2555 * @param solver the solver handle
2556 * @param peer the identity of the peer
2558 static const struct ATS_Address *
2559 GAS_ril_get_preferred_address (void *solver,
2560 const struct GNUNET_PeerIdentity *peer)
2562 struct GAS_RIL_Handle *s = solver;
2563 struct RIL_Peer_Agent *agent;
2565 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2567 agent = ril_get_agent (s, peer, GNUNET_YES);
2569 agent->is_active = GNUNET_YES;
2570 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2572 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2574 if (agent->address_inuse)
2576 LOG(GNUNET_ERROR_TYPE_DEBUG,
2577 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2578 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2582 LOG(GNUNET_ERROR_TYPE_DEBUG,
2583 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2585 s->parameters.temperature = s->parameters.temperature_init;
2586 s->parameters.epsilon = s->parameters.epsilon_init;
2588 return agent->address_inuse;
2592 * Tell solver stop notifying ATS about changes for this peers
2594 * The solver must only notify about changes for peers with pending address
2597 * @param solver the solver handle
2598 * @param peer the peer
2601 GAS_ril_stop_get_preferred_address (void *solver,
2602 const struct GNUNET_PeerIdentity *peer)
2604 struct GAS_RIL_Handle *s = solver;
2605 struct RIL_Peer_Agent *agent;
2607 LOG (GNUNET_ERROR_TYPE_DEBUG,
2608 "API_stop_get_preferred_address()");
2610 agent = ril_get_agent (s, peer, GNUNET_NO);
2617 if (GNUNET_NO == agent->is_active)
2623 s->parameters.temperature = s->parameters.temperature_init;
2624 s->parameters.epsilon = s->parameters.epsilon_init;
2626 agent->is_active = GNUNET_NO;
2628 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2633 LOG(GNUNET_ERROR_TYPE_DEBUG,
2634 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2635 GNUNET_i2s (peer), agent->address_inuse->plugin);
2640 * Entry point for the plugin
2642 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2645 libgnunet_plugin_ats_ril_init (void *cls)
2647 struct GNUNET_ATS_PluginEnvironment *env = cls;
2648 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2649 struct RIL_Scope * cur;
2654 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
2656 GNUNET_assert(NULL != env);
2657 GNUNET_assert(NULL != env->cfg);
2658 GNUNET_assert(NULL != env->stats);
2659 GNUNET_assert(NULL != env->bandwidth_changed_cb);
2660 GNUNET_assert(NULL != env->get_preferences);
2661 GNUNET_assert(NULL != env->get_property);
2663 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2665 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2669 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2670 &solver->parameters.step_time_min))
2672 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2676 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2677 &solver->parameters.step_time_max))
2679 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2682 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2684 GNUNET_STRINGS_utf8_toupper (string, string);
2685 if (0 == strcmp (string, "SARSA"))
2687 solver->parameters.algorithm = RIL_ALGO_SARSA;
2689 if (0 == strcmp (string, "Q-LEARNING"))
2691 solver->parameters.algorithm = RIL_ALGO_Q;
2694 GNUNET_free (string);
2698 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2701 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2703 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2704 GNUNET_free (string);
2708 solver->parameters.select = RIL_DEFAULT_SELECT;
2712 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2713 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2714 "RIL_DISCOUNT_BETA", &f_tmp))
2718 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2719 "RIL_DISCOUNT_BETA", f_tmp);
2723 solver->parameters.beta = f_tmp;
2724 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2725 "RIL_DISCOUNT_BETA", f_tmp);
2729 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2730 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2731 "RIL_DISCOUNT_GAMMA", &f_tmp))
2733 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2735 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2736 "RIL_DISCOUNT_GAMMA", f_tmp);
2740 solver->parameters.gamma = f_tmp;
2741 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2742 "RIL_DISCOUNT_GAMMA", f_tmp);
2746 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2747 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2748 "RIL_GRADIENT_STEP_SIZE", &f_tmp))
2750 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2752 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2753 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2757 solver->parameters.alpha = f_tmp;
2758 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2759 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2763 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2764 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2765 "RIL_TRACE_DECAY", &f_tmp))
2767 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2769 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2770 "RIL_TRACE_DECAY", f_tmp);
2774 solver->parameters.lambda = f_tmp;
2775 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2776 "RIL_TRACE_DECAY", f_tmp);
2780 solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
2781 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2782 "RIL_EXPLORE_RATIO", &f_tmp))
2784 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2786 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2787 "RIL_EXPLORE_RATIO", f_tmp);
2791 solver->parameters.epsilon_init = f_tmp;
2792 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2793 "RIL_EXPLORE_RATIO", f_tmp);
2797 solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
2798 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2799 "RIL_EXPLORE_DECAY", &f_tmp))
2801 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2803 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2804 "RIL_EXPLORE_DECAY", f_tmp);
2808 solver->parameters.epsilon_decay = f_tmp;
2809 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2810 "RIL_EXPLORE_DECAY", f_tmp);
2814 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2815 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2816 "RIL_TEMPERATURE", &f_tmp))
2820 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2821 "RIL_TEMPERATURE", f_tmp);
2825 solver->parameters.temperature_init = f_tmp;
2826 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2827 "RIL_TEMPERATURE", f_tmp);
2831 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
2832 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2833 "RIL_TEMPERATURE_DECAY", &f_tmp))
2835 if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1)
2837 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2838 "RIL_TEMPERATURE_DECAY", f_tmp);
2842 solver->parameters.temperature_decay = f_tmp;
2843 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2844 "RIL_TEMPERATURE_DECAY", f_tmp);
2848 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2850 solver->simulate = GNUNET_NO;
2853 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2855 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2859 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2862 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2864 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2865 GNUNET_free (string);
2869 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2872 env->sf.s_add = &GAS_ril_address_add;
2873 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
2874 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
2875 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
2876 env->sf.s_address_update_network = &GAS_ril_address_change_network;
2877 env->sf.s_get = &GAS_ril_get_preferred_address;
2878 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2879 env->sf.s_pref = &GAS_ril_address_change_preference;
2880 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
2881 env->sf.s_del = &GAS_ril_address_delete;
2882 env->sf.s_bulk_start = &GAS_ril_bulk_start;
2883 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
2885 solver->plugin_envi = env;
2886 solver->networks_count = env->network_count;
2887 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2888 solver->step_count = 0;
2889 solver->done = GNUNET_NO;
2891 for (c = 0; c < env->network_count; c++)
2893 cur = &solver->network_entries[c];
2894 cur->type = env->networks[c];
2895 cur->bw_in_available = env->in_quota[c];
2896 cur->bw_out_available = env->out_quota[c];
2897 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);
2900 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2901 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2902 solver->parameters.algorithm ? "Q" : "SARSA",
2903 solver->parameters.alpha,
2904 solver->parameters.beta,
2905 solver->parameters.lambda);
2906 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
2907 solver->parameters.epsilon,
2908 solver->parameters.temperature,
2909 solver->parameters.select ? "EGREEDY" : "SOFTMAX");
2910 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2911 solver->parameters.rbf_divisor);
2918 * Exit point for the plugin
2920 * @param cls the solver handle
2923 libgnunet_plugin_ats_ril_done (void *cls)
2925 struct GAS_RIL_Handle *s = cls;
2926 struct RIL_Peer_Agent *cur_agent;
2927 struct RIL_Peer_Agent *next_agent;
2929 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2931 s->done = GNUNET_YES;
2933 cur_agent = s->agents_head;
2934 while (NULL != cur_agent)
2936 next_agent = cur_agent->next;
2937 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2938 agent_die (s, cur_agent);
2939 cur_agent = next_agent;
2942 if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
2944 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2946 GNUNET_free(s->network_entries);
2953 /* end of plugin_ats_ril.c */