2 This file is part of GNUnet.
3 Copyright (C) 2011-2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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).
74 enum RIL_Action_Type {
75 RIL_ACTION_NOTHING = 0,
76 RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions
77 RIL_ACTION_BW_IN_HLV = -3,
78 RIL_ACTION_BW_IN_INC = 1,
79 RIL_ACTION_BW_IN_DEC = 2,
80 RIL_ACTION_BW_OUT_DBL = -4,
81 RIL_ACTION_BW_OUT_HLV = -5,
82 RIL_ACTION_BW_OUT_INC = 3,
83 RIL_ACTION_BW_OUT_DEC = 4,
84 RIL_ACTION_TYPE_NUM = 5
93 RIL_SELECT_SOFTMAX = 0,
94 RIL_SELECT_EGREEDY = 1
99 RIL_WELFARE_EGALITARIAN
102 enum RIL_E_Modification {
110 * Global learning parameters
112 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
179 * Ratio, with what probability an agent should explore in the e-greed policy
184 * Decay factor of the explore ratio
186 double epsilon_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 {
206 struct RIL_Address_Wrapped *next;
211 struct RIL_Address_Wrapped *prev;
216 struct ATS_Address *address_naked;
220 struct RIL_Peer_Agent {
222 * Next agent in solver's linked list
224 struct RIL_Peer_Agent *next;
227 * Previous agent in solver's linked list
229 struct RIL_Peer_Agent *prev;
234 struct GAS_RIL_Handle *envi;
239 struct GNUNET_PeerIdentity peer;
242 * Whether the agent is active or not
247 * Number of performed time-steps
249 unsigned long long step_count;
252 * Experience matrix W
257 * Number of rows of W / Number of state-vector features
262 * Number of columns of W / Number of actions
267 * Last perceived state feature vector
282 * Whether to reset the eligibility traces to 0 after a Q-exploration step
284 int eligibility_reset;
289 struct ATS_Address *address_inuse;
292 * Head of addresses DLL
294 struct RIL_Address_Wrapped *addresses_head;
297 * Tail of addresses DLL
299 struct RIL_Address_Wrapped *addresses_tail;
302 * Inbound bandwidth assigned by the agent
307 * Outbound bandwidth assigned by the agent
312 * Flag whether a suggestion has to be issued
314 int suggestion_issue;
317 * The address which has to be issued
319 struct ATS_Address *suggestion_address;
322 * The agent's last objective value
324 double objective_old;
336 enum GNUNET_NetworkType type;
339 * Total available inbound bandwidth
341 uint32_t bw_in_available;
344 * Bandwidth inbound assigned in network after last step
346 uint32_t bw_in_assigned;
349 * Bandwidth inbound actually utilized in the network
351 uint32_t bw_in_utilized;
354 * Total available outbound bandwidth
356 uint32_t bw_out_available;
359 * Bandwidth outbound assigned in network after last step
361 unsigned long long bw_out_assigned;
364 * Bandwidth outbound actually utilized in the network
366 unsigned long long bw_out_utilized;
369 * Number of active agents in scope
371 unsigned int active_agent_count;
374 * The social welfare achieved in the scope
376 double social_welfare;
380 * A handle for the reinforcement learning solver
382 struct GAS_RIL_Handle {
384 * The solver-plugin environment of the solver-plugin API
386 struct GNUNET_ATS_PluginEnvironment *env;
389 * Number of performed steps
391 unsigned long long step_count;
394 * Timestamp for the last time-step
396 struct GNUNET_TIME_Absolute step_time_last;
399 * Task identifier of the next time-step to be executed
401 struct GNUNET_SCHEDULER_Task * step_next_task_id;
404 * Variable discount factor, dependent on time between steps
406 double global_discount_variable;
409 * Integrated variable discount factor, dependent on time between steps
411 double global_discount_integrated;
414 * Lock for bulk operations
419 * Number of changes during a lock
424 * Learning parameters
426 struct RIL_Learning_Parameters parameters;
429 * Array of networks with global assignment state
431 struct RIL_Scope * network_entries;
436 unsigned int networks_count;
439 * List of active peer-agents
441 struct RIL_Peer_Agent * agents_head;
442 struct RIL_Peer_Agent * agents_tail;
450 * Simulate steps, i.e. schedule steps immediately
452 unsigned long long simulate;
456 * "Private" functions
457 * ---------------------------
461 * Estimate the current action-value for state s and action a
463 * @param agent agent performing the estimation
466 * @return estimation value
469 agent_q(struct RIL_Peer_Agent *agent,
476 for (i = 0; i < agent->m; i++)
477 result += state[i] * agent->W[action][i];
479 /* prevent crashes if learning diverges */
481 return isnan(result) * UINT32_MAX;
483 return isinf(result) * UINT32_MAX;
489 * Get the index of the address in the agent's list.
491 * @param agent agent handle
492 * @param address address handle
493 * @return the index, starting with zero
496 agent_address_get_index(struct RIL_Peer_Agent *agent, struct ATS_Address *address)
499 struct RIL_Address_Wrapped *cur;
502 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
505 if (cur->address_naked == address)
513 * Gets the wrapped address from the agent's list
515 * @param agent agent handle
516 * @param address address handle
517 * @return wrapped address
519 static struct RIL_Address_Wrapped *
520 agent_address_get_wrapped(struct RIL_Peer_Agent *agent, struct ATS_Address *address)
522 struct RIL_Address_Wrapped *cur;
524 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
525 if (cur->address_naked == address)
532 agent_action_is_possible(struct RIL_Peer_Agent *agent, int action)
538 case RIL_ACTION_NOTHING:
542 case RIL_ACTION_BW_IN_INC:
543 case RIL_ACTION_BW_IN_DBL:
544 if (agent->bw_in >= RIL_MAX_BW)
550 case RIL_ACTION_BW_IN_DEC:
551 case RIL_ACTION_BW_IN_HLV:
552 if (agent->bw_in <= 0)
558 case RIL_ACTION_BW_OUT_INC:
559 case RIL_ACTION_BW_OUT_DBL:
560 if (agent->bw_out >= RIL_MAX_BW)
566 case RIL_ACTION_BW_OUT_DEC:
567 case RIL_ACTION_BW_OUT_HLV:
568 if (agent->bw_out <= 0)
575 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
577 address_index = action - RIL_ACTION_TYPE_NUM;
579 GNUNET_assert(address_index >= 0);
581 address_index <= agent_address_get_index(agent, agent->addresses_tail->address_naked));
583 if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
584 agent->address_inuse->active)
590 // error - action does not exist
591 GNUNET_assert(GNUNET_NO);
597 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
598 * most reward in the future)
600 * @param agent agent performing the calculation
601 * @param state the state from which to take the action
602 * @return the action promising most future reward
605 agent_get_action_max(struct RIL_Peer_Agent *agent, double *state)
608 int max_i = RIL_ACTION_INVALID;
610 double max_q = -DBL_MAX;
612 for (i = 0; i < agent->n; i++)
614 if (agent_action_is_possible(agent, i))
616 cur_q = agent_q(agent, state, i);
625 GNUNET_assert(RIL_ACTION_INVALID != max_i);
631 * Chooses a random action from the set of possible ones
633 * @param agent the agent performing the action
634 * @return the action index
637 agent_get_action_random(struct RIL_Peer_Agent *agent)
640 int is_possible[agent->n];
644 for (i = 0; i < agent->n; i++)
646 if (agent_action_is_possible(agent, i))
648 is_possible[i] = GNUNET_YES;
653 is_possible[i] = GNUNET_NO;
657 r = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, sum);
660 for (i = 0; i < agent->n; i++)
670 GNUNET_assert(GNUNET_NO);
671 return RIL_ACTION_INVALID;
676 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
678 * @param agent the agent performing the update
679 * @param reward the reward received for the last action
680 * @param s_next the new state, the last step got the agent into
681 * @param a_prime the new
684 agent_update(struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
689 double **theta = agent->W;
691 delta = agent->envi->global_discount_integrated * reward; //reward
692 delta += agent->envi->global_discount_variable * agent_q(agent, s_next, a_prime); //discounted future value
693 delta -= agent_q(agent, agent->s_old, agent->a_old); //one step
695 // 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",
696 // agent->step_count,
697 // agent_q (agent, agent->s_old, agent->a_old),
698 // agent->envi->parameters.alpha,
700 // agent->envi->global_discount_variable,
701 // agent_q (agent, s_next, a_prime),
704 for (k = 0; k < agent->n; k++)
706 for (i = 0; i < agent->m; i++)
708 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
709 // agent->envi->parameters.alpha,
713 theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
720 * Changes the eligibility trace vector e in various manners:
721 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
722 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
723 * #RIL_E_DECAY - multiplies e with discount factor and lambda as in the update rule
724 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
726 * @param agent the agent handle
727 * @param mod the kind of modification
728 * @param feature the feature vector
729 * @param action the action to take
732 agent_modify_eligibility(struct RIL_Peer_Agent *agent,
733 enum RIL_E_Modification mod,
740 for (i = 0; i < agent->m; i++)
744 case RIL_E_ACCUMULATE:
745 agent->E[action][i] += feature[i];
749 agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
753 for (k = 0; k < agent->n; k++)
755 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 solver->env->info_cb(solver->env->cls,
788 * Calculates the maximum bandwidth an agent can assign in a network scope
792 static unsigned long long
793 ril_get_max_bw(struct RIL_Scope *net)
795 return GNUNET_MIN(2 * GNUNET_MAX(net->bw_in_available, net->bw_out_available), GNUNET_ATS_MaxBandwidth);
799 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
800 * notify ATS of its new decision
802 * @param solver solver handle
803 * @param agent agent handle
804 * @param new_address the address which is to be used
805 * @param new_bw_in the new amount of inbound bandwidth set for this address
806 * @param new_bw_out the new amount of outbound bandwidth set for this address
807 * @param silent disables invocation of the bw_changed callback, if #GNUNET_YES
810 envi_set_active_suggestion(struct GAS_RIL_Handle *solver,
811 struct RIL_Peer_Agent *agent,
812 struct ATS_Address *new_address,
813 unsigned long long new_bw_in,
814 unsigned long long new_bw_out,
817 int notify = GNUNET_NO;
819 LOG(GNUNET_ERROR_TYPE_DEBUG,
820 " set_active_suggestion() for peer '%s'\n",
821 GNUNET_i2s(&agent->peer));
824 if (agent->address_inuse != new_address)
826 if (NULL != agent->address_inuse)
828 agent->address_inuse->active = GNUNET_NO;
829 agent->address_inuse->assigned_bw_in = 0;
830 agent->address_inuse->assigned_bw_out = 0;
832 if (NULL != new_address)
834 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
835 new_address->active = agent->is_active;
836 new_address->assigned_bw_in = agent->bw_in;
837 new_address->assigned_bw_out = agent->bw_out;
839 notify |= GNUNET_YES;
845 if (new_address->active != agent->is_active)
847 new_address->active = agent->is_active;
848 notify |= GNUNET_YES;
852 if (agent->bw_in != new_bw_in)
854 agent->bw_in = new_bw_in;
855 new_address->assigned_bw_in = new_bw_in;
856 notify |= GNUNET_YES;
858 if (agent->bw_out != new_bw_out)
860 agent->bw_out = new_bw_out;
861 new_address->assigned_bw_out = new_bw_out;
862 notify |= GNUNET_YES;
866 if (notify && agent->is_active && (GNUNET_NO == silent))
870 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
871 agent->suggestion_issue = GNUNET_YES;
872 agent->suggestion_address = new_address;
874 else if (agent->address_inuse)
876 /* disconnect case, no new address */
877 GNUNET_assert(0 == agent->address_inuse->assigned_bw_in);
878 GNUNET_assert(0 == agent->address_inuse->assigned_bw_out);
882 agent->suggestion_issue = GNUNET_YES;
883 agent->suggestion_address = agent->address_inuse;
886 agent->address_inuse = new_address;
891 * Allocates a state vector and fills it with the features present
892 * @param solver the solver handle
893 * @param agent the agent handle
894 * @return pointer to the state vector
897 envi_get_state(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
908 unsigned long long max_bw;
910 state = GNUNET_malloc(sizeof(double) * agent->m);
912 max_bw = ril_get_max_bw((struct RIL_Scope *)agent->address_inuse->solver_information);
914 y[0] = (double)agent->bw_out;
915 y[1] = (double)agent->bw_in;
917 m = agent_address_get_index(agent, agent->address_inuse) * (solver->parameters.rbf_divisor + 1) * (solver->parameters.rbf_divisor + 1);
918 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
920 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
922 x[0] = (double)i * (double)max_bw / (double)solver->parameters.rbf_divisor;
923 x[1] = (double)k * (double)max_bw / (double)solver->parameters.rbf_divisor;
926 sigma = (((double)max_bw / ((double)solver->parameters.rbf_divisor + 1)) * 0.5);
927 f = exp(-((d[0] * d[0] + d[1] * d[1]) / (2 * sigma * sigma)));
937 * Returns the utility value of the connection an agent manages
939 * @param agent the agent in question
940 * @return the utility value
943 agent_get_utility(struct RIL_Peer_Agent *agent)
945 const double *preferences;
950 preferences = agent->envi->env->get_preferences(agent->envi->env->cls,
953 delay_atsi = agent->address_inuse->norm_delay.norm;
954 delay_norm = RIL_UTILITY_DELAY_MAX * exp(-delay_atsi * 0.00001);
956 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
957 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
958 sqrt((double)(agent->bw_in / RIL_MIN_BW) * (double)(agent->bw_out / RIL_MIN_BW));
963 * Calculates the social welfare within a network scope according to what social
964 * welfare measure is set in the configuration.
966 * @param solver the solver handle
967 * @param scope the network scope in question
968 * @return the social welfare value
971 ril_network_get_social_welfare(struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
973 struct RIL_Peer_Agent *cur;
976 switch (solver->parameters.social_welfare)
978 case RIL_WELFARE_EGALITARIAN:
980 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
982 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
984 result = GNUNET_MIN(result, agent_get_utility(cur));
989 case RIL_WELFARE_NASH:
991 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
993 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
995 result *= pow(agent_get_utility(cur), 1.0 / (double)scope->active_agent_count);
1000 GNUNET_assert(GNUNET_NO);
1005 envi_get_penalty(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1007 struct RIL_Scope *net;
1008 unsigned long long over_max;
1009 unsigned long long over_in = 0;
1010 unsigned long long over_out = 0;
1012 net = agent->address_inuse->solver_information;
1014 if (net->bw_in_utilized > net->bw_in_available)
1016 over_in = net->bw_in_utilized - net->bw_in_available;
1017 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1019 /* increase quadratically */
1023 if (net->bw_out_utilized > net->bw_out_available)
1025 over_out = net->bw_out_utilized - net->bw_out_available;
1026 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1028 /* increase quadratically */
1029 over_out *= over_out;
1032 over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
1034 return -1.0 * (double)over_max;
1038 * Gets the reward for the last performed step, which is calculated in equal
1039 * parts from the local (the peer specific) and the global (for all peers
1040 * identical) reward.
1042 * @param solver the solver handle
1043 * @param agent the agent handle
1044 * @return the reward
1047 envi_get_reward(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1049 struct RIL_Scope *net;
1056 net = agent->address_inuse->solver_information;
1058 penalty = envi_get_penalty(solver, agent);
1059 objective = (agent_get_utility(agent) + net->social_welfare) / 2;
1060 delta = objective - agent->objective_old;
1061 agent->objective_old = objective;
1063 if (delta != 0 && penalty == 0)
1065 agent->nop_bonus = delta * RIL_NOP_DECAY;
1069 agent->nop_bonus *= RIL_NOP_DECAY;
1072 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1074 reward = delta + steady;
1075 return reward + penalty;
1079 * Doubles the bandwidth for the active address
1081 * @param solver solver handle
1082 * @param agent agent handle
1083 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1086 envi_action_bw_double(struct GAS_RIL_Handle *solver,
1087 struct RIL_Peer_Agent *agent,
1090 unsigned long long new_bw;
1091 unsigned long long max_bw;
1093 max_bw = ril_get_max_bw((struct RIL_Scope *)agent->address_inuse->solver_information);
1097 new_bw = agent->bw_in * 2;
1098 if (new_bw < agent->bw_in || new_bw > max_bw)
1100 envi_set_active_suggestion(solver, agent, agent->address_inuse, new_bw,
1101 agent->bw_out, GNUNET_NO);
1105 new_bw = agent->bw_out * 2;
1106 if (new_bw < agent->bw_out || new_bw > max_bw)
1108 envi_set_active_suggestion(solver, agent, agent->address_inuse, agent->bw_in,
1114 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1115 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1117 * @param solver solver handle
1118 * @param agent agent handle
1119 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1123 envi_action_bw_halven(struct GAS_RIL_Handle *solver,
1124 struct RIL_Peer_Agent *agent,
1127 unsigned long long new_bw;
1131 new_bw = agent->bw_in / 2;
1132 if (new_bw <= 0 || new_bw > agent->bw_in)
1134 envi_set_active_suggestion(solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1139 new_bw = agent->bw_out / 2;
1140 if (new_bw <= 0 || new_bw > agent->bw_out)
1142 envi_set_active_suggestion(solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1148 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1150 * @param solver solver handle
1151 * @param agent agent handle
1152 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1156 envi_action_bw_inc(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1158 unsigned long long new_bw;
1159 unsigned long long max_bw;
1161 max_bw = ril_get_max_bw((struct RIL_Scope *)agent->address_inuse->solver_information);
1165 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1166 if (new_bw < agent->bw_in || new_bw > max_bw)
1168 envi_set_active_suggestion(solver, agent, agent->address_inuse, new_bw,
1169 agent->bw_out, GNUNET_NO);
1173 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1174 if (new_bw < agent->bw_out || new_bw > max_bw)
1176 envi_set_active_suggestion(solver, agent, agent->address_inuse, agent->bw_in,
1182 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1183 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1185 * @param solver solver handle
1186 * @param agent agent handle
1187 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1191 envi_action_bw_dec(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1193 unsigned long long new_bw;
1197 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1198 if (new_bw <= 0 || new_bw > agent->bw_in)
1200 envi_set_active_suggestion(solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1205 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1206 if (new_bw <= 0 || new_bw > agent->bw_out)
1208 envi_set_active_suggestion(solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1214 * Switches to the address given by its index
1216 * @param solver solver handle
1217 * @param agent agent handle
1218 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1221 envi_action_address_switch(struct GAS_RIL_Handle *solver,
1222 struct RIL_Peer_Agent *agent,
1223 unsigned int address_index)
1225 struct RIL_Address_Wrapped *cur;
1228 //cur = agent_address_get_wrapped(agent, agent->address_inuse);
1230 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1232 if (i == address_index)
1234 envi_set_active_suggestion(solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1242 //no address with address_index exists, in this case this action should not be callable
1243 GNUNET_assert(GNUNET_NO);
1247 * Puts the action into effect by calling the according function
1249 * @param solver the solver handle
1250 * @param agent the action handle
1251 * @param action the action to perform by the solver
1254 envi_do_action(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1260 case RIL_ACTION_NOTHING:
1263 case RIL_ACTION_BW_IN_DBL:
1264 envi_action_bw_double(solver, agent, GNUNET_YES);
1267 case RIL_ACTION_BW_IN_HLV:
1268 envi_action_bw_halven(solver, agent, GNUNET_YES);
1271 case RIL_ACTION_BW_IN_INC:
1272 envi_action_bw_inc(solver, agent, GNUNET_YES);
1275 case RIL_ACTION_BW_IN_DEC:
1276 envi_action_bw_dec(solver, agent, GNUNET_YES);
1279 case RIL_ACTION_BW_OUT_DBL:
1280 envi_action_bw_double(solver, agent, GNUNET_NO);
1283 case RIL_ACTION_BW_OUT_HLV:
1284 envi_action_bw_halven(solver, agent, GNUNET_NO);
1287 case RIL_ACTION_BW_OUT_INC:
1288 envi_action_bw_inc(solver, agent, GNUNET_NO);
1291 case RIL_ACTION_BW_OUT_DEC:
1292 envi_action_bw_dec(solver, agent, GNUNET_NO);
1296 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1298 address_index = action - RIL_ACTION_TYPE_NUM;
1300 GNUNET_assert(address_index >= 0);
1302 address_index <= agent_address_get_index(agent, agent->addresses_tail->address_naked));
1304 envi_action_address_switch(solver, agent, address_index);
1307 // error - action does not exist
1308 GNUNET_assert(GNUNET_NO);
1313 * Selects the next action using the e-greedy strategy. I.e. with a probability
1314 * of (1-e) the action with the maximum expected return will be chosen
1315 * (=> exploitation) and with probability (e) a random action will be chosen.
1316 * In case the Q-learning rule is set, the function also resets the eligibility
1317 * traces in the exploration case (after Watkin's Q-learning).
1319 * @param agent the agent selecting an action
1320 * @param state the current state-feature vector
1321 * @return the action index
1324 agent_select_egreedy(struct RIL_Peer_Agent *agent, double *state)
1327 double r = (double)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
1328 UINT32_MAX) / (double)UINT32_MAX;
1330 if (r < agent->envi->parameters.epsilon) //explore
1332 action = agent_get_action_random(agent);
1333 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1335 agent->eligibility_reset = GNUNET_YES;
1337 agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
1342 action = agent_get_action_max(agent, state);
1348 * Selects the next action with a probability corresponding to its value. The
1349 * probability is calculated using a Boltzmann distribution with a temperature
1350 * value. The higher the temperature, the more are the action selection
1351 * probabilities the same. With a temperature of 0, the selection is greedy,
1352 * i.e. always the action with the highest value is chosen.
1358 agent_select_softmax(struct RIL_Peer_Agent *agent, double *state)
1362 double eqt[agent->n];
1367 a_max = agent_get_action_max(agent, state);
1369 for (i = 0; i < agent->n; i++)
1371 if (agent_action_is_possible(agent, i))
1373 eqt[i] = exp(agent_q(agent, state, i) / agent->envi->parameters.temperature);
1375 eqt[i] = isinf(eqt[i]) * UINT32_MAX;
1379 for (i = 0; i < agent->n; i++)
1381 if (agent_action_is_possible(agent, i))
1383 p[i] = eqt[i] / sum;
1390 r = (double)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
1391 UINT32_MAX) / (double)UINT32_MAX;
1393 for (i = 0; i < agent->n; i++)
1399 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1400 agent->eligibility_reset = GNUNET_YES;
1401 agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay;
1407 GNUNET_assert(GNUNET_NO);
1408 return RIL_ACTION_INVALID;
1412 * Select the next action of an agent either according to the e-greedy strategy
1413 * or the softmax strategy.
1415 * @param agent the agent in question
1416 * @param state the current state-feature vector
1417 * @return the action index
1420 agent_select_action(struct RIL_Peer_Agent *agent, double *state)
1422 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1424 return agent_select_egreedy(agent, state);
1428 return agent_select_softmax(agent, state);
1433 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1434 * after having done the last action a_old. It observes the new state s_next and the reward
1435 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1436 * next action is put into effect.
1438 * @param agent the agent performing the step
1441 agent_step(struct RIL_Peer_Agent *agent)
1443 int a_next = RIL_ACTION_INVALID;
1448 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1449 GNUNET_i2s(&agent->peer),
1450 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1452 s_next = envi_get_state(agent->envi, agent);
1453 reward = envi_get_reward(agent->envi, agent);
1455 if (agent->eligibility_reset)
1457 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1458 agent->eligibility_reset = GNUNET_NO;
1462 agent_modify_eligibility(agent, RIL_E_DECAY, NULL, -1);
1464 if (RIL_ACTION_INVALID != agent->a_old)
1466 agent_modify_eligibility(agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1469 switch (agent->envi->parameters.algorithm)
1471 case RIL_ALGO_SARSA:
1472 a_next = agent_select_action(agent, s_next);
1473 if (RIL_ACTION_INVALID != agent->a_old)
1475 //updates weights with selected action (on-policy), if not first step
1476 agent_update(agent, reward, s_next, a_next);
1481 a_max = agent_get_action_max(agent, s_next);
1482 if (RIL_ACTION_INVALID != agent->a_old)
1484 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1485 agent_update(agent, reward, s_next, a_max);
1487 a_next = agent_select_action(agent, s_next);
1491 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1493 LOG(GNUNET_ERROR_TYPE_DEBUG, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1496 agent->bw_in / 1024,
1497 agent->bw_out / 1024,
1500 envi_do_action(agent->envi, agent, a_next);
1502 GNUNET_free(agent->s_old);
1503 agent->s_old = s_next;
1504 agent->a_old = a_next;
1506 agent->step_count += 1;
1510 * Prototype of the ril_step() procedure
1512 * @param solver the solver handle
1515 ril_step(struct GAS_RIL_Handle *solver);
1519 * Task for the scheduler, which performs one step and lets the solver know that
1520 * no further step is scheduled.
1522 * @param cls the solver handle
1525 ril_step_scheduler_task(void *cls)
1527 struct GAS_RIL_Handle *solver = cls;
1529 solver->step_next_task_id = NULL;
1534 * Determines how much of the available bandwidth is assigned. If more is
1535 * assigned than available it returns 1. The function is used to determine the
1536 * step size of the adaptive stepping.
1538 * @param solver the solver handle
1542 ril_get_used_resource_ratio(struct GAS_RIL_Handle *solver)
1545 struct RIL_Scope net;
1546 unsigned long long sum_assigned = 0;
1547 unsigned long long sum_available = 0;
1550 for (i = 0; i < solver->networks_count; i++)
1552 net = solver->network_entries[i];
1553 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1555 sum_assigned += net.bw_in_utilized;
1556 sum_assigned += net.bw_out_utilized;
1557 sum_available += net.bw_in_available;
1558 sum_available += net.bw_out_available;
1561 if (sum_available > 0)
1563 ratio = ((double)sum_assigned) / ((double)sum_available);
1570 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1574 * Lookup network struct by type
1576 * @param s the solver handle
1577 * @param type the network type
1578 * @return the network struct
1580 static struct RIL_Scope *
1581 ril_get_network(struct GAS_RIL_Handle *s, uint32_t type)
1585 for (i = 0; i < s->networks_count; i++)
1587 if (s->network_entries[i].type == type)
1589 return &s->network_entries[i];
1596 * Determines whether more connections are allocated in a network scope, than
1597 * they would theoretically fit. This is used as a heuristic to determine,
1598 * whether a new connection can be allocated or not.
1600 * @param solver the solver handle
1601 * @param network the network scope in question
1602 * @return GNUNET_YES if there are theoretically enough resources left
1605 ril_network_is_not_full(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType network)
1607 struct RIL_Scope *net;
1608 struct RIL_Peer_Agent *agent;
1609 unsigned long long address_count = 0;
1611 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1613 if (agent->address_inuse && agent->is_active)
1615 net = agent->address_inuse->solver_information;
1616 if (net->type == network)
1623 net = ril_get_network(solver, network);
1624 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1628 * Unblocks an agent for which a connection request is there, that could not
1629 * be satisfied. Iterates over the addresses of the agent, if one of its
1630 * addresses can now be allocated in its scope the agent is unblocked,
1631 * otherwise it remains unchanged.
1633 * @param solver the solver handle
1634 * @param agent the agent in question
1638 ril_try_unblock_agent(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1640 struct RIL_Address_Wrapped *addr_wrap;
1641 struct RIL_Scope *net;
1642 unsigned long long start_in;
1643 unsigned long long start_out;
1645 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1647 net = addr_wrap->address_naked->solver_information;
1648 if (ril_network_is_not_full(solver, net->type))
1650 if (NULL == agent->address_inuse)
1652 start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
1653 start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
1654 envi_set_active_suggestion(solver, agent, addr_wrap->address_naked, start_in, start_out, silent);
1659 agent->address_inuse = NULL;
1663 * Determines how much the reward needs to be discounted depending on the amount
1664 * of time, which has passed since the last time-step.
1666 * @param solver the solver handle
1669 ril_calculate_discount(struct GAS_RIL_Handle *solver)
1671 struct GNUNET_TIME_Absolute time_now;
1672 struct GNUNET_TIME_Relative time_delta;
1675 // MDP case only for debugging purposes
1676 if (solver->simulate)
1678 solver->global_discount_variable = solver->parameters.gamma;
1679 solver->global_discount_integrated = 1;
1685 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1686 time_now = GNUNET_TIME_absolute_get();
1687 time_delta = GNUNET_TIME_absolute_get_difference(solver->step_time_last, time_now);
1688 solver->step_time_last = time_now;
1689 tau = (double)time_delta.rel_value_us
1690 / (double)solver->parameters.step_time_min.rel_value_us;
1692 //calculate reward discounts (once per step for all agents)
1693 solver->global_discount_variable = pow(M_E, ((-1.0) * ((double)solver->parameters.beta) * tau));
1694 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1695 / (double)solver->parameters.beta;
1699 * Count the number of active agents/connections in a network scope
1701 * @param solver the solver handle
1702 * @param scope the network scope in question
1703 * @return the number of allocated connections
1706 ril_network_count_active_agents(struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1709 struct RIL_Peer_Agent *cur_agent;
1711 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1713 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1722 * Calculates how much bandwidth is assigned in sum in a network scope, either
1723 * in the inbound or in the outbound direction.
1725 * @param solver the solver handle
1726 * @param type the type of the network scope in question
1727 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1728 * otherwise the outbound direction will be summed up
1729 * @return the sum of the assigned bandwidths
1731 static unsigned long long
1732 ril_network_get_assigned(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType type, int direction_in)
1734 struct RIL_Peer_Agent *cur;
1735 struct RIL_Scope *net;
1736 unsigned long long sum = 0;
1738 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1740 if (cur->is_active && cur->address_inuse)
1742 net = cur->address_inuse->solver_information;
1743 if (net->type == type)
1757 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1758 * either in the inbound or in the outbound direction.
1760 * @param solver the solver handle
1761 * @param type the type of the network scope in question
1762 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1763 * otherwise the outbound direction will be summed up
1764 * @return the sum of the utilized bandwidths (in bytes/second)
1766 static unsigned long long
1767 ril_network_get_utilized(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType type, int direction_in)
1769 struct RIL_Peer_Agent *cur;
1770 struct RIL_Scope *net;
1771 unsigned long long sum = 0;
1773 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1775 if (cur->is_active && cur->address_inuse)
1777 net = cur->address_inuse->solver_information;
1778 if (net->type == type)
1781 sum += cur->address_inuse->norm_utilization_in.norm;
1783 sum += cur->address_inuse->norm_utilization_out.norm;
1792 * Retrieves the state of the network scope, so that its attributes are up-to-
1795 * @param solver the solver handle
1798 ril_networks_update_state(struct GAS_RIL_Handle *solver)
1801 struct RIL_Scope *net;
1803 for (c = 0; c < solver->networks_count; c++)
1805 net = &solver->network_entries[c];
1806 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1807 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1808 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1809 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1810 net->active_agent_count = ril_network_count_active_agents(solver, net);
1811 net->social_welfare = ril_network_get_social_welfare(solver, net);
1816 * Schedules the next global step in an adaptive way. The more resources are
1817 * left, the earlier the next step is scheduled. This serves the reactivity of
1818 * the solver to changed inputs.
1820 * @param solver the solver handle
1823 ril_step_schedule_next(struct GAS_RIL_Handle *solver)
1829 struct GNUNET_TIME_Relative time_next;
1831 used_ratio = ril_get_used_resource_ratio(solver);
1834 solver->parameters.step_time_min.rel_value_us
1835 <= solver->parameters.step_time_max.rel_value_us);
1837 factor = (double)GNUNET_TIME_relative_subtract(solver->parameters.step_time_max,
1838 solver->parameters.step_time_min).rel_value_us;
1839 offset = (double)solver->parameters.step_time_min.rel_value_us;
1840 y = factor * pow(used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1842 GNUNET_assert(y <= (double)solver->parameters.step_time_max.rel_value_us);
1843 GNUNET_assert(y >= (double)solver->parameters.step_time_min.rel_value_us);
1845 time_next = GNUNET_TIME_relative_saturating_multiply(GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long)y);
1847 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1853 if (solver->simulate)
1855 time_next = GNUNET_TIME_UNIT_ZERO;
1858 if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1860 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed(time_next, &ril_step_scheduler_task,
1866 * Triggers one step per agent
1871 ril_step(struct GAS_RIL_Handle *solver)
1873 struct RIL_Peer_Agent *cur;
1875 if (GNUNET_YES == solver->bulk_lock)
1877 solver->bulk_changes++;
1881 ril_inform(solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1883 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1885 if (0 == solver->step_count)
1887 solver->step_time_last = GNUNET_TIME_absolute_get();
1890 ril_calculate_discount(solver);
1891 ril_networks_update_state(solver);
1893 //trigger one step per active, unblocked agent
1894 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1898 if (NULL == cur->address_inuse)
1900 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1902 if (cur->address_inuse)
1909 ril_networks_update_state(solver);
1911 solver->step_count++;
1912 ril_step_schedule_next(solver);
1914 ril_inform(solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1916 ril_inform(solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1917 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1919 if (cur->suggestion_issue)
1921 solver->env->bandwidth_changed_cb(solver->env->cls,
1922 cur->suggestion_address);
1923 cur->suggestion_issue = GNUNET_NO;
1926 ril_inform(solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1930 * Initializes the matrix W of parameter vectors theta with small random numbers.
1932 * @param agent The respective agent
1935 agent_w_init(struct RIL_Peer_Agent *agent)
1940 for (i = 0; i < agent->n; i++)
1942 for (k = 0; k < agent->m; k++)
1944 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));
1950 * Initialize an agent without addresses and its knowledge base
1952 * @param s ril solver
1953 * @param peer the one in question
1954 * @return handle to the new agent
1956 static struct RIL_Peer_Agent *
1957 agent_init(void *s, const struct GNUNET_PeerIdentity *peer)
1960 struct GAS_RIL_Handle * solver = s;
1961 struct RIL_Peer_Agent * agent = GNUNET_new(struct RIL_Peer_Agent);
1963 agent->envi = solver;
1964 agent->peer = *peer;
1965 agent->step_count = 0;
1966 agent->is_active = GNUNET_NO;
1967 agent->bw_in = RIL_MIN_BW;
1968 agent->bw_out = RIL_MIN_BW;
1969 agent->suggestion_issue = GNUNET_NO;
1970 agent->n = RIL_ACTION_TYPE_NUM;
1972 agent->W = (double **)GNUNET_malloc(sizeof(double *) * agent->n);
1973 agent->E = (double **)GNUNET_malloc(sizeof(double *) * agent->n);
1974 for (i = 0; i < agent->n; i++)
1976 agent->W[i] = (double *)GNUNET_malloc(sizeof(double) * agent->m);
1977 agent->E[i] = (double *)GNUNET_malloc(sizeof(double) * agent->m);
1979 agent_w_init(agent);
1980 agent->eligibility_reset = GNUNET_NO;
1981 agent->a_old = RIL_ACTION_INVALID;
1982 agent->s_old = GNUNET_malloc(sizeof(double) * agent->m);
1983 agent->address_inuse = NULL;
1984 agent->objective_old = 0;
1985 agent->nop_bonus = 0;
1993 * @param solver the solver handle
1994 * @param agent the agent to retire
1997 agent_die(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
2001 for (i = 0; i < agent->n; i++)
2003 GNUNET_free_non_null(agent->W[i]);
2004 GNUNET_free_non_null(agent->E[i]);
2006 GNUNET_free_non_null(agent->W);
2007 GNUNET_free_non_null(agent->E);
2008 GNUNET_free_non_null(agent->s_old);
2013 * Returns the agent for a peer
2015 * @param solver the solver handle
2016 * @param peer the identity of the peer
2017 * @param create whether or not to create an agent, if none is allocated yet
2020 static struct RIL_Peer_Agent *
2021 ril_get_agent(struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
2023 struct RIL_Peer_Agent *cur;
2025 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2027 if (0 == GNUNET_memcmp(peer, &cur->peer))
2035 cur = agent_init(solver, peer);
2036 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
2043 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
2044 * considered inactive and not used. Addresses in an inactive network are ignored.
2046 * @param solver solver handle
2047 * @param network the network type
2048 * @return whether or not the network is considered active
2051 ril_network_is_active(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType network)
2053 struct RIL_Scope *net;
2055 net = ril_get_network(solver, network);
2056 return net->bw_out_available >= RIL_MIN_BW;
2060 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
2061 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
2062 * frees the memory of the old vector, and redirects the pointer to the new one.
2064 * @param old pointer to the pointer to the first element of the vector
2065 * @param element_size byte size of the vector elements
2066 * @param hole_start the first element to cut out
2067 * @param hole_length the number of elements to cut out
2068 * @param old_length the length of the old vector
2071 ril_cut_from_vector(void **old,
2072 size_t element_size,
2073 unsigned int hole_start,
2074 unsigned int hole_length,
2075 unsigned int old_length)
2078 char *oldptr = (char *)*old;
2080 unsigned int bytes_before;
2081 unsigned int bytes_hole;
2082 unsigned int bytes_after;
2084 GNUNET_assert(old_length >= hole_length);
2085 GNUNET_assert(old_length >= (hole_start + hole_length));
2087 size = element_size * (old_length - hole_length);
2089 bytes_before = element_size * hole_start;
2090 bytes_hole = element_size * hole_length;
2091 bytes_after = element_size * (old_length - hole_start - hole_length);
2099 tmpptr = GNUNET_malloc(size);
2100 GNUNET_memcpy(tmpptr, oldptr, bytes_before);
2101 GNUNET_memcpy(tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2107 *old = (void *)tmpptr;
2111 * Solver API functions
2112 * ---------------------------
2116 * Change relative preference for quality in solver
2118 * @param solver the solver handle
2119 * @param peer the peer to change the preference for
2120 * @param kind the kind to change the preference
2121 * @param pref_rel the normalized preference value for this kind over all clients
2124 GAS_ril_address_change_preference(void *solver,
2125 const struct GNUNET_PeerIdentity *peer,
2126 enum GNUNET_ATS_PreferenceKind kind,
2129 LOG(GNUNET_ERROR_TYPE_DEBUG,
2130 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2131 GNUNET_ATS_print_preference_type(kind), GNUNET_i2s(peer), pref_rel);
2133 struct GAS_RIL_Handle *s = solver;
2135 s->parameters.temperature = s->parameters.temperature_init;
2136 s->parameters.epsilon = s->parameters.epsilon_init;
2142 * Add a new address for a peer to the solver
2144 * The address is already contained in the addresses hashmap!
2146 * @param solver the solver Handle
2147 * @param address the address to add
2148 * @param network network type of this address
2151 GAS_ril_address_add(void *solver,
2152 struct ATS_Address *address,
2155 struct GAS_RIL_Handle *s = solver;
2156 struct RIL_Peer_Agent *agent;
2157 struct RIL_Address_Wrapped *address_wrapped;
2158 struct RIL_Scope *net;
2166 LOG(GNUNET_ERROR_TYPE_DEBUG,
2167 "API_address_add()\n");
2169 net = ril_get_network(s, network);
2170 address->solver_information = net;
2172 if (!ril_network_is_active(s, network))
2174 LOG(GNUNET_ERROR_TYPE_DEBUG,
2175 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2176 address->plugin, address->addr, GNUNET_i2s(&address->peer));
2180 s->parameters.temperature = s->parameters.temperature_init;
2181 s->parameters.epsilon = s->parameters.epsilon_init;
2183 agent = ril_get_agent(s, &address->peer, GNUNET_YES);
2186 address_wrapped = GNUNET_new(struct RIL_Address_Wrapped);
2187 address_wrapped->address_naked = address;
2188 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2190 //increase size of W
2191 m_new = agent->m + ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1));
2193 n_new = agent->n + 1;
2196 GNUNET_array_grow(agent->W, agent->n, n_new);
2198 GNUNET_array_grow(agent->E, agent->n, n_new);
2199 for (i = 0; i < n_new; i++)
2204 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2206 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2211 GNUNET_array_grow(agent->W[i], zero, m_new);
2213 GNUNET_array_grow(agent->E[i], zero, m_new);
2217 //increase size of old state vector
2219 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2221 ril_try_unblock_agent(s, agent, GNUNET_NO);
2225 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2226 address->active ? "active" : "inactive", address->plugin, address->addr,
2227 GNUNET_i2s(&address->peer));
2231 * Delete an address in the solver
2233 * The address is not contained in the address hashmap anymore!
2235 * @param solver the solver handle
2236 * @param address the address to remove
2239 GAS_ril_address_delete(void *solver,
2240 struct ATS_Address *address)
2242 struct GAS_RIL_Handle *s = solver;
2243 struct RIL_Peer_Agent *agent;
2244 struct RIL_Address_Wrapped *address_wrapped;
2249 struct RIL_Scope *net;
2251 LOG(GNUNET_ERROR_TYPE_DEBUG,
2252 "API_address_delete() Delete %s %s address %s for peer '%s'\n",
2253 address->active ? "active" : "inactive",
2256 GNUNET_i2s(&address->peer));
2258 agent = ril_get_agent(s, &address->peer, GNUNET_NO);
2261 net = address->solver_information;
2262 GNUNET_assert(!ril_network_is_active(s, net->type));
2263 LOG(GNUNET_ERROR_TYPE_DEBUG,
2264 "No agent allocated for peer yet, since address was in inactive network\n");
2268 s->parameters.temperature = s->parameters.temperature_init;
2269 s->parameters.epsilon = s->parameters.epsilon_init;
2271 address_index = agent_address_get_index(agent, address);
2272 address_wrapped = agent_address_get_wrapped(agent, address);
2274 if (NULL == address_wrapped)
2276 net = address->solver_information;
2277 LOG(GNUNET_ERROR_TYPE_DEBUG,
2278 "Address not considered by agent, address was in inactive network\n");
2281 GNUNET_CONTAINER_DLL_remove(agent->addresses_head,
2282 agent->addresses_tail,
2284 GNUNET_free(address_wrapped);
2287 m_new = agent->m - ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1));
2288 n_new = agent->n - 1;
2290 for (i = 0; i < agent->n; i++)
2292 ril_cut_from_vector((void **)&agent->W[i], sizeof(double),
2293 address_index * ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)),
2294 ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)), agent->m);
2295 ril_cut_from_vector((void **)&agent->E[i], sizeof(double),
2296 address_index * ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)),
2297 ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)), agent->m);
2299 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2300 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2301 ril_cut_from_vector((void **)&agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2303 ril_cut_from_vector((void **)&agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2305 //correct last action
2306 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2310 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2312 agent->a_old = RIL_ACTION_INVALID;
2314 //decrease old state vector
2315 ril_cut_from_vector((void **)&agent->s_old, sizeof(double),
2316 address_index * ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)),
2317 ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)), agent->m);
2321 if (agent->address_inuse == address)
2323 if (NULL != agent->addresses_head) //if peer has an address left, use it
2325 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2326 "Active address died, suggesting alternative!\n");
2327 envi_set_active_suggestion(s,
2329 agent->addresses_head->address_naked,
2336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2337 "Active address died, suggesting disconnect!\n");
2338 envi_set_active_suggestion(s, agent, NULL, 0, 0, GNUNET_NO);
2342 if (agent->suggestion_address == address)
2344 agent->suggestion_issue = GNUNET_NO;
2345 agent->suggestion_address = NULL;
2347 GNUNET_assert(agent->address_inuse != address);
2352 * Update the properties of an address in the solver
2354 * @param solver solver handle
2355 * @param address the address
2358 GAS_ril_address_property_changed(void *solver,
2359 struct ATS_Address *address)
2361 struct GAS_RIL_Handle *s = solver;
2363 LOG(GNUNET_ERROR_TYPE_DEBUG,
2364 "Properties for peer '%s' address changed\n",
2365 GNUNET_i2s(&address->peer));
2366 s->parameters.temperature = s->parameters.temperature_init;
2367 s->parameters.epsilon = s->parameters.epsilon_init;
2373 * Give feedback about the current assignment
2375 * @param solver the solver handle
2376 * @param application the application
2377 * @param peer the peer to change the preference for
2378 * @param scope the time interval for this feedback: [now - scope .. now]
2379 * @param kind the kind to change the preference
2380 * @param score the score
2383 GAS_ril_address_preference_feedback(void *solver,
2384 struct GNUNET_SERVICE_Client *application,
2385 const struct GNUNET_PeerIdentity *peer,
2386 const struct GNUNET_TIME_Relative scope,
2387 enum GNUNET_ATS_PreferenceKind kind,
2390 LOG(GNUNET_ERROR_TYPE_DEBUG,
2391 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2392 "preference %s for %d seconds\n",
2395 GNUNET_ATS_print_preference_type(kind),
2396 scope.rel_value_us / 1000000);
2401 * Start a bulk operation
2403 * @param solver the solver
2406 GAS_ril_bulk_start(void *solver)
2408 struct GAS_RIL_Handle *s = solver;
2410 LOG(GNUNET_ERROR_TYPE_DEBUG,
2411 "API_bulk_start() lock: %d\n", s->bulk_lock + 1);
2418 * Bulk operation done
2420 * @param solver the solver handle
2423 GAS_ril_bulk_stop(void *solver)
2425 struct GAS_RIL_Handle *s = solver;
2427 LOG(GNUNET_ERROR_TYPE_DEBUG,
2428 "API_bulk_stop() lock: %d\n",
2431 if (s->bulk_lock < 1)
2438 if (0 < s->bulk_changes)
2441 s->bulk_changes = 0;
2447 * Tell solver to notify ATS if the address to use changes for a specific
2448 * peer using the bandwidth changed callback
2450 * The solver must only notify about changes for peers with pending address
2453 * @param solver the solver handle
2454 * @param peer the identity of the peer
2457 GAS_ril_get_preferred_address(void *solver,
2458 const struct GNUNET_PeerIdentity *peer)
2460 struct GAS_RIL_Handle *s = solver;
2461 struct RIL_Peer_Agent *agent;
2463 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2465 agent = ril_get_agent(s, peer, GNUNET_YES);
2467 agent->is_active = GNUNET_YES;
2468 envi_set_active_suggestion(solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2470 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2472 if (agent->address_inuse)
2474 LOG(GNUNET_ERROR_TYPE_DEBUG,
2475 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2476 GNUNET_i2s(peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2480 LOG(GNUNET_ERROR_TYPE_DEBUG,
2481 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2483 s->parameters.temperature = s->parameters.temperature_init;
2484 s->parameters.epsilon = s->parameters.epsilon_init;
2486 if (NULL != agent->address_inuse)
2487 s->env->bandwidth_changed_cb(s->env->cls,
2488 agent->address_inuse);
2493 * Tell solver stop notifying ATS about changes for this peers
2495 * The solver must only notify about changes for peers with pending address
2498 * @param solver the solver handle
2499 * @param peer the peer
2502 GAS_ril_stop_get_preferred_address(void *solver,
2503 const struct GNUNET_PeerIdentity *peer)
2505 struct GAS_RIL_Handle *s = solver;
2506 struct RIL_Peer_Agent *agent;
2508 LOG(GNUNET_ERROR_TYPE_DEBUG,
2509 "API_stop_get_preferred_address()");
2511 agent = ril_get_agent(s, peer, GNUNET_NO);
2518 if (GNUNET_NO == agent->is_active)
2524 s->parameters.temperature = s->parameters.temperature_init;
2525 s->parameters.epsilon = s->parameters.epsilon_init;
2527 agent->is_active = GNUNET_NO;
2529 envi_set_active_suggestion(s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2534 LOG(GNUNET_ERROR_TYPE_DEBUG,
2535 "API_stop_get_preferred_address() Paused agent for peer '%s'\n",
2541 * Entry point for the plugin
2543 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2546 libgnunet_plugin_ats_ril_init(void *cls)
2548 static struct GNUNET_ATS_SolverFunctions sf;
2549 struct GNUNET_ATS_PluginEnvironment *env = cls;
2550 struct GAS_RIL_Handle *solver = GNUNET_new(struct GAS_RIL_Handle);
2551 struct RIL_Scope * cur;
2556 LOG(GNUNET_ERROR_TYPE_DEBUG,
2557 "API_init() Initializing RIL solver\n");
2559 GNUNET_assert(NULL != env);
2560 GNUNET_assert(NULL != env->cfg);
2561 GNUNET_assert(NULL != env->stats);
2562 GNUNET_assert(NULL != env->bandwidth_changed_cb);
2563 GNUNET_assert(NULL != env->get_preferences);
2565 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2567 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2571 != GNUNET_CONFIGURATION_get_value_time(env->cfg, "ats", "RIL_STEP_TIME_MIN",
2572 &solver->parameters.step_time_min))
2574 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2578 != GNUNET_CONFIGURATION_get_value_time(env->cfg, "ats", "RIL_STEP_TIME_MAX",
2579 &solver->parameters.step_time_max))
2581 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2584 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(env->cfg, "ats", "RIL_ALGORITHM", &string))
2586 GNUNET_STRINGS_utf8_toupper(string, string);
2587 if (0 == strcmp(string, "SARSA"))
2589 solver->parameters.algorithm = RIL_ALGO_SARSA;
2591 if (0 == strcmp(string, "Q-LEARNING"))
2593 solver->parameters.algorithm = RIL_ALGO_Q;
2596 GNUNET_free(string);
2600 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2603 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(env->cfg, "ats", "RIL_SELECT", &string))
2605 solver->parameters.select = !strcmp(string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2606 GNUNET_free(string);
2610 solver->parameters.select = RIL_DEFAULT_SELECT;
2614 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2615 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2616 "RIL_DISCOUNT_BETA", &f_tmp))
2620 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2621 "RIL_DISCOUNT_BETA", f_tmp);
2625 solver->parameters.beta = f_tmp;
2626 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2627 "RIL_DISCOUNT_BETA", f_tmp);
2631 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2632 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2633 "RIL_DISCOUNT_GAMMA", &f_tmp))
2635 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2637 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2638 "RIL_DISCOUNT_GAMMA", f_tmp);
2642 solver->parameters.gamma = f_tmp;
2643 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2644 "RIL_DISCOUNT_GAMMA", f_tmp);
2648 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2649 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2650 "RIL_GRADIENT_STEP_SIZE", &f_tmp))
2652 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2654 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2655 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2659 solver->parameters.alpha = f_tmp;
2660 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2661 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2665 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2666 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2667 "RIL_TRACE_DECAY", &f_tmp))
2669 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2671 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2672 "RIL_TRACE_DECAY", f_tmp);
2676 solver->parameters.lambda = f_tmp;
2677 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2678 "RIL_TRACE_DECAY", f_tmp);
2682 solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
2683 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2684 "RIL_EXPLORE_RATIO", &f_tmp))
2686 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2688 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2689 "RIL_EXPLORE_RATIO", f_tmp);
2693 solver->parameters.epsilon_init = f_tmp;
2694 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2695 "RIL_EXPLORE_RATIO", f_tmp);
2699 solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
2700 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2701 "RIL_EXPLORE_DECAY", &f_tmp))
2703 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2705 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2706 "RIL_EXPLORE_DECAY", f_tmp);
2710 solver->parameters.epsilon_decay = f_tmp;
2711 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2712 "RIL_EXPLORE_DECAY", f_tmp);
2716 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2717 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2718 "RIL_TEMPERATURE", &f_tmp))
2722 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2723 "RIL_TEMPERATURE", f_tmp);
2727 solver->parameters.temperature_init = f_tmp;
2728 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2729 "RIL_TEMPERATURE", f_tmp);
2733 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
2734 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2735 "RIL_TEMPERATURE_DECAY", &f_tmp))
2737 if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1)
2739 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2740 "RIL_TEMPERATURE_DECAY", f_tmp);
2744 solver->parameters.temperature_decay = f_tmp;
2745 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2746 "RIL_TEMPERATURE_DECAY", f_tmp);
2750 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2752 solver->simulate = GNUNET_NO;
2755 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2757 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2761 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2764 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2766 solver->parameters.social_welfare = !strcmp(string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2767 GNUNET_free(string);
2771 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2776 sf.s_add = &GAS_ril_address_add;
2777 sf.s_address_update_property = &GAS_ril_address_property_changed;
2778 sf.s_get = &GAS_ril_get_preferred_address;
2779 sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2780 sf.s_pref = &GAS_ril_address_change_preference;
2781 sf.s_feedback = &GAS_ril_address_preference_feedback;
2782 sf.s_del = &GAS_ril_address_delete;
2783 sf.s_bulk_start = &GAS_ril_bulk_start;
2784 sf.s_bulk_stop = &GAS_ril_bulk_stop;
2786 solver->networks_count = env->network_count;
2787 solver->network_entries = GNUNET_malloc(env->network_count * sizeof(struct RIL_Scope));
2788 solver->step_count = 0;
2789 solver->done = GNUNET_NO;
2791 for (c = 0; c < env->network_count; c++)
2793 cur = &solver->network_entries[c];
2795 cur->bw_in_available = env->in_quota[c];
2796 cur->bw_out_available = env->out_quota[c];
2797 LOG(GNUNET_ERROR_TYPE_DEBUG,
2798 "init() Quotas for %s network: IN %llu - OUT %llu\n",
2799 GNUNET_NT_to_string(cur->type),
2800 cur->bw_in_available / 1024,
2801 cur->bw_out_available / 1024);
2804 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2805 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2806 solver->parameters.algorithm ? "Q" : "SARSA",
2807 solver->parameters.alpha,
2808 solver->parameters.beta,
2809 solver->parameters.lambda);
2810 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
2811 solver->parameters.epsilon,
2812 solver->parameters.temperature,
2813 solver->parameters.select ? "EGREEDY" : "SOFTMAX");
2814 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2815 solver->parameters.rbf_divisor);
2822 * Exit point for the plugin
2824 * @param cls the solver handle
2827 libgnunet_plugin_ats_ril_done(void *cls)
2829 struct GNUNET_ATS_SolverFunctions *sf = cls;
2830 struct GAS_RIL_Handle *s = sf->cls;
2831 struct RIL_Peer_Agent *cur_agent;
2832 struct RIL_Peer_Agent *next_agent;
2834 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2836 s->done = GNUNET_YES;
2838 cur_agent = s->agents_head;
2839 while (NULL != cur_agent)
2841 next_agent = cur_agent->next;
2842 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2843 agent_die(s, cur_agent);
2844 cur_agent = next_agent;
2847 if (NULL != s->step_next_task_id)
2849 GNUNET_SCHEDULER_cancel(s->step_next_task_id);
2851 GNUNET_free(s->network_entries);
2858 /* end of plugin_ats_ril.c */