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.
17 * @file ats/plugin_ats_ril.c
18 * @brief ATS reinforcement learning solver
19 * @author Fabian Oehlmann
20 * @author Matthias Wachs
25 #include "gnunet_ats_plugin.h"
26 #include "gnunet-service-ats_addresses.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
32 #define RIL_MIN_BW (5 * ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__))
33 #define RIL_MAX_BW GNUNET_ATS_MaxBandwidth
35 #define RIL_ACTION_INVALID -1
36 #define RIL_INTERVAL_EXPONENT 10
37 #define RIL_UTILITY_DELAY_MAX 1000
39 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200)
40 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 2000)
41 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
42 #define RIL_DEFAULT_SELECT RIL_SELECT_SOFTMAX
43 #define RIL_DEFAULT_WELFARE RIL_WELFARE_NASH
44 #define RIL_DEFAULT_DISCOUNT_BETA 0.6
45 #define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
46 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.01
47 #define RIL_DEFAULT_TRACE_DECAY 0.5
48 #define RIL_DEFAULT_EXPLORE_RATIO 1
49 #define RIL_DEFAULT_EXPLORE_DECAY 0.95
50 #define RIL_DEFAULT_RBF_DIVISOR 50
51 #define RIL_DEFAULT_TEMPERATURE 0.1
52 #define RIL_DEFAULT_TEMPERATURE_DECAY 1
54 #define RIL_INC_DEC_STEP_SIZE 1
55 #define RIL_NOP_DECAY 0.5
58 * ATS reinforcement learning solver
64 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
65 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
66 * to a particular address. The action of switching to address with index i is depicted by the
67 * number (RIL_ACTION_TYPE_NUM + i).
71 RIL_ACTION_NOTHING = 0,
72 RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions
73 RIL_ACTION_BW_IN_HLV = -3,
74 RIL_ACTION_BW_IN_INC = 1,
75 RIL_ACTION_BW_IN_DEC = 2,
76 RIL_ACTION_BW_OUT_DBL = -4,
77 RIL_ACTION_BW_OUT_HLV = -5,
78 RIL_ACTION_BW_OUT_INC = 3,
79 RIL_ACTION_BW_OUT_DEC = 4,
80 RIL_ACTION_TYPE_NUM = 5
91 RIL_SELECT_SOFTMAX = 0,
92 RIL_SELECT_EGREEDY = 1
98 RIL_WELFARE_EGALITARIAN
101 enum RIL_E_Modification
110 * Global learning parameters
112 struct RIL_Learning_Parameters
115 * The TD-algorithm to use
117 enum RIL_Algorithm algorithm;
120 * Gradient-descent step-size
125 * Learning discount variable in the TD-update for semi-MDPs
130 * Learning discount factor in the TD-update for MDPs
135 * Trace-decay factor for eligibility traces
140 * Whether to accumulate or replace eligibility traces
142 enum RIL_E_Modification eligibility_trace_mode;
145 * Initial softmax action-selection temperature
147 double temperature_init;
150 * Softmax action-selection temperature
155 * Decay factor of the temperature value
157 double temperature_decay;
160 * Which measure of social welfare should be used
162 enum RIL_Welfare social_welfare;
165 * State space divisor
167 unsigned long long rbf_divisor;
170 * Action selection strategy;
172 enum RIL_Select select;
175 * Initial exploration ratio value
180 * Ratio, with what probability an agent should explore in the e-greed policy
185 * Decay factor of the explore ratio
187 double epsilon_decay;
190 * Minimal interval time between steps in milliseconds
192 struct GNUNET_TIME_Relative step_time_min;
195 * Maximum interval time between steps in milliseconds
197 struct GNUNET_TIME_Relative step_time_max;
201 * Wrapper for addresses to store them in agent's linked list
203 struct RIL_Address_Wrapped
208 struct RIL_Address_Wrapped *next;
213 struct RIL_Address_Wrapped *prev;
218 struct ATS_Address *address_naked;
222 struct RIL_Peer_Agent
225 * Next agent in solver's linked list
227 struct RIL_Peer_Agent *next;
230 * Previous agent in solver's linked list
232 struct RIL_Peer_Agent *prev;
237 struct GAS_RIL_Handle *envi;
242 struct GNUNET_PeerIdentity peer;
245 * Whether the agent is active or not
250 * Number of performed time-steps
252 unsigned long long step_count;
255 * Experience matrix W
260 * Number of rows of W / Number of state-vector features
265 * Number of columns of W / Number of actions
270 * Last perceived state feature vector
285 * Whether to reset the eligibility traces to 0 after a Q-exploration step
287 int eligibility_reset;
292 struct ATS_Address *address_inuse;
295 * Head of addresses DLL
297 struct RIL_Address_Wrapped *addresses_head;
300 * Tail of addresses DLL
302 struct RIL_Address_Wrapped *addresses_tail;
305 * Inbound bandwidth assigned by the agent
310 * Outbound bandwidth assigned by the agent
315 * Flag whether a suggestion has to be issued
317 int suggestion_issue;
320 * The address which has to be issued
322 struct ATS_Address *suggestion_address;
325 * The agent's last objective value
327 double objective_old;
340 enum GNUNET_ATS_Network_Type type;
343 * Total available inbound bandwidth
345 uint32_t bw_in_available;
348 * Bandwidth inbound assigned in network after last step
350 uint32_t bw_in_assigned;
353 * Bandwidth inbound actually utilized in the network
355 uint32_t bw_in_utilized;
358 * Total available outbound bandwidth
360 uint32_t bw_out_available;
363 * Bandwidth outbound assigned in network after last step
365 unsigned long long bw_out_assigned;
368 * Bandwidth outbound actually utilized in the network
370 unsigned long long bw_out_utilized;
373 * Number of active agents in scope
375 unsigned int active_agent_count;
378 * The social welfare achieved in the scope
380 double social_welfare;
384 * A handle for the reinforcement learning solver
386 struct GAS_RIL_Handle
389 * The solver-plugin environment of the solver-plugin API
391 struct GNUNET_ATS_PluginEnvironment *env;
394 * Number of performed steps
396 unsigned long long step_count;
399 * Timestamp for the last time-step
401 struct GNUNET_TIME_Absolute step_time_last;
404 * Task identifier of the next time-step to be executed
406 struct GNUNET_SCHEDULER_Task * step_next_task_id;
409 * Variable discount factor, dependent on time between steps
411 double global_discount_variable;
414 * Integrated variable discount factor, dependent on time between steps
416 double global_discount_integrated;
419 * Lock for bulk operations
424 * Number of changes during a lock
429 * Learning parameters
431 struct RIL_Learning_Parameters parameters;
434 * Array of networks with global assignment state
436 struct RIL_Scope * network_entries;
441 unsigned int networks_count;
444 * List of active peer-agents
446 struct RIL_Peer_Agent * agents_head;
447 struct RIL_Peer_Agent * agents_tail;
455 * Simulate steps, i.e. schedule steps immediately
457 unsigned long long simulate;
461 * "Private" functions
462 * ---------------------------
466 * Estimate the current action-value for state s and action a
468 * @param agent agent performing the estimation
471 * @return estimation value
474 agent_q (struct RIL_Peer_Agent *agent,
481 for (i = 0; i < agent->m; i++)
482 result += state[i] * agent->W[action][i];
484 /* prevent crashes if learning diverges */
486 return isnan(result) * UINT32_MAX;
488 return isinf(result) * UINT32_MAX;
494 * Get the index of the address in the agent's list.
496 * @param agent agent handle
497 * @param address address handle
498 * @return the index, starting with zero
501 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
504 struct RIL_Address_Wrapped *cur;
507 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
510 if (cur->address_naked == address)
518 * Gets the wrapped address from the agent's list
520 * @param agent agent handle
521 * @param address address handle
522 * @return wrapped address
524 static struct RIL_Address_Wrapped *
525 agent_address_get_wrapped (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
527 struct RIL_Address_Wrapped *cur;
529 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
530 if (cur->address_naked == address)
537 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
543 case RIL_ACTION_NOTHING:
546 case RIL_ACTION_BW_IN_INC:
547 case RIL_ACTION_BW_IN_DBL:
548 if (agent->bw_in >= RIL_MAX_BW)
553 case RIL_ACTION_BW_IN_DEC:
554 case RIL_ACTION_BW_IN_HLV:
555 if (agent->bw_in <= 0)
560 case RIL_ACTION_BW_OUT_INC:
561 case RIL_ACTION_BW_OUT_DBL:
562 if (agent->bw_out >= RIL_MAX_BW)
567 case RIL_ACTION_BW_OUT_DEC:
568 case RIL_ACTION_BW_OUT_HLV:
569 if (agent->bw_out <= 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];
748 agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
751 for (k = 0; k < agent->n; k++)
753 agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
757 for (k = 0; k < agent->n; k++)
767 * Informs the environment about the status of the solver
774 ril_inform (struct GAS_RIL_Handle *solver,
775 enum GAS_Solver_Operation op,
776 enum GAS_Solver_Status stat)
778 solver->env->info_cb (solver->env->cls,
785 * Calculates the maximum bandwidth an agent can assign in a network scope
789 static unsigned long long
790 ril_get_max_bw (struct RIL_Scope *net)
792 return GNUNET_MIN(2 * GNUNET_MAX(net->bw_in_available, net->bw_out_available), GNUNET_ATS_MaxBandwidth);
796 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
797 * notify ATS of its new decision
799 * @param solver solver handle
800 * @param agent agent handle
801 * @param new_address the address which is to be used
802 * @param new_bw_in the new amount of inbound bandwidth set for this address
803 * @param new_bw_out the new amount of outbound bandwidth set for this address
804 * @param silent disables invocation of the bw_changed callback, if #GNUNET_YES
807 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
808 struct RIL_Peer_Agent *agent,
809 struct ATS_Address *new_address,
810 unsigned long long new_bw_in,
811 unsigned long long new_bw_out,
814 int notify = GNUNET_NO;
816 LOG(GNUNET_ERROR_TYPE_DEBUG,
817 " set_active_suggestion() for peer '%s'\n",
818 GNUNET_i2s (&agent->peer));
821 if (agent->address_inuse != new_address)
823 if (NULL != agent->address_inuse)
825 agent->address_inuse->active = GNUNET_NO;
826 agent->address_inuse->assigned_bw_in = 0;
827 agent->address_inuse->assigned_bw_out = 0;
829 if (NULL != new_address)
831 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
832 new_address->active = agent->is_active;
833 new_address->assigned_bw_in = agent->bw_in;
834 new_address->assigned_bw_out = agent->bw_out;
836 notify |= GNUNET_YES;
842 if (new_address->active != agent->is_active)
844 new_address->active = agent->is_active;
845 notify |= GNUNET_YES;
849 if (agent->bw_in != new_bw_in)
851 agent->bw_in = new_bw_in;
852 new_address->assigned_bw_in = new_bw_in;
853 notify |= GNUNET_YES;
855 if (agent->bw_out != new_bw_out)
857 agent->bw_out = new_bw_out;
858 new_address->assigned_bw_out = new_bw_out;
859 notify |= GNUNET_YES;
863 if (notify && agent->is_active && (GNUNET_NO == silent))
867 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
868 agent->suggestion_issue = GNUNET_YES;
869 agent->suggestion_address = new_address;
871 else if (agent->address_inuse)
873 /* disconnect case, no new address */
874 GNUNET_assert(0 == agent->address_inuse->assigned_bw_in);
875 GNUNET_assert(0 == agent->address_inuse->assigned_bw_out);
879 agent->suggestion_issue = GNUNET_YES;
880 agent->suggestion_address = agent->address_inuse;
883 agent->address_inuse = new_address;
888 * Allocates a state vector and fills it with the features present
889 * @param solver the solver handle
890 * @param agent the agent handle
891 * @return pointer to the state vector
894 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
905 unsigned long long max_bw;
907 state = GNUNET_malloc (sizeof(double) * agent->m);
909 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
911 y[0] = (double) agent->bw_out;
912 y[1] = (double) agent->bw_in;
914 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
915 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
917 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
919 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
920 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
923 sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
924 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
934 * Returns the utility value of the connection an agent manages
936 * @param agent the agent in question
937 * @return the utility value
940 agent_get_utility (struct RIL_Peer_Agent *agent)
942 const double *preferences;
947 preferences = agent->envi->env->get_preferences (agent->envi->env->cls,
950 delay_atsi = agent->address_inuse->norm_delay.norm;
951 delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
953 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
954 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
955 sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
960 * Calculates the social welfare within a network scope according to what social
961 * welfare measure is set in the configuration.
963 * @param solver the solver handle
964 * @param scope the network scope in question
965 * @return the social welfare value
968 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
970 struct RIL_Peer_Agent *cur;
973 switch (solver->parameters.social_welfare)
975 case RIL_WELFARE_EGALITARIAN:
977 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
979 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
981 result = GNUNET_MIN(result, agent_get_utility(cur));
986 case RIL_WELFARE_NASH:
988 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
990 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
992 result *= pow(agent_get_utility(cur), 1.0 / (double) scope->active_agent_count);
997 GNUNET_assert(GNUNET_NO);
1002 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1004 struct RIL_Scope *net;
1005 unsigned long long over_max;
1006 unsigned long long over_in = 0;
1007 unsigned long long over_out = 0;
1009 net = agent->address_inuse->solver_information;
1011 if (net->bw_in_utilized > net->bw_in_available)
1013 over_in = net->bw_in_utilized - net->bw_in_available;
1014 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1016 /* increase quadratically */
1020 if (net->bw_out_utilized > net->bw_out_available)
1022 over_out = net->bw_out_utilized - net->bw_out_available;
1023 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1025 /* increase quadratically */
1026 over_out *= over_out;
1029 over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
1031 return -1.0 * (double) over_max;
1035 * Gets the reward for the last performed step, which is calculated in equal
1036 * parts from the local (the peer specific) and the global (for all peers
1037 * identical) reward.
1039 * @param solver the solver handle
1040 * @param agent the agent handle
1041 * @return the reward
1044 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1046 struct RIL_Scope *net;
1053 net = agent->address_inuse->solver_information;
1055 penalty = envi_get_penalty(solver, agent);
1056 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1057 delta = objective - agent->objective_old;
1058 agent->objective_old = objective;
1060 if (delta != 0 && penalty == 0)
1062 agent->nop_bonus = delta * RIL_NOP_DECAY;
1066 agent->nop_bonus *= RIL_NOP_DECAY;
1069 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1071 reward = delta + steady;
1072 return reward + penalty;
1076 * Doubles the bandwidth for the active address
1078 * @param solver solver handle
1079 * @param agent agent handle
1080 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1083 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1084 struct RIL_Peer_Agent *agent,
1087 unsigned long long new_bw;
1088 unsigned long long max_bw;
1090 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1094 new_bw = agent->bw_in * 2;
1095 if (new_bw < agent->bw_in || new_bw > max_bw)
1097 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1098 agent->bw_out, GNUNET_NO);
1102 new_bw = agent->bw_out * 2;
1103 if (new_bw < agent->bw_out || new_bw > max_bw)
1105 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1111 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1112 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1114 * @param solver solver handle
1115 * @param agent agent handle
1116 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1120 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1121 struct RIL_Peer_Agent *agent,
1124 unsigned long long new_bw;
1128 new_bw = agent->bw_in / 2;
1129 if (new_bw <= 0 || new_bw > agent->bw_in)
1131 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1136 new_bw = agent->bw_out / 2;
1137 if (new_bw <= 0 || new_bw > agent->bw_out)
1139 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1145 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1147 * @param solver solver handle
1148 * @param agent agent handle
1149 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1153 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1155 unsigned long long new_bw;
1156 unsigned long long max_bw;
1158 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1162 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1163 if (new_bw < agent->bw_in || new_bw > max_bw)
1165 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1166 agent->bw_out, GNUNET_NO);
1170 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1171 if (new_bw < agent->bw_out || new_bw > max_bw)
1173 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1179 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1180 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1182 * @param solver solver handle
1183 * @param agent agent handle
1184 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1188 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1190 unsigned long long new_bw;
1194 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1195 if (new_bw <= 0 || new_bw > agent->bw_in)
1197 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1202 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1203 if (new_bw <= 0 || new_bw > agent->bw_out)
1205 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1211 * Switches to the address given by its index
1213 * @param solver solver handle
1214 * @param agent agent handle
1215 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1218 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1219 struct RIL_Peer_Agent *agent,
1220 unsigned int address_index)
1222 struct RIL_Address_Wrapped *cur;
1225 //cur = agent_address_get_wrapped(agent, agent->address_inuse);
1227 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1229 if (i == address_index)
1231 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1239 //no address with address_index exists, in this case this action should not be callable
1240 GNUNET_assert(GNUNET_NO);
1244 * Puts the action into effect by calling the according function
1246 * @param solver the solver handle
1247 * @param agent the action handle
1248 * @param action the action to perform by the solver
1251 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1257 case RIL_ACTION_NOTHING:
1259 case RIL_ACTION_BW_IN_DBL:
1260 envi_action_bw_double (solver, agent, GNUNET_YES);
1262 case RIL_ACTION_BW_IN_HLV:
1263 envi_action_bw_halven (solver, agent, GNUNET_YES);
1265 case RIL_ACTION_BW_IN_INC:
1266 envi_action_bw_inc (solver, agent, GNUNET_YES);
1268 case RIL_ACTION_BW_IN_DEC:
1269 envi_action_bw_dec (solver, agent, GNUNET_YES);
1271 case RIL_ACTION_BW_OUT_DBL:
1272 envi_action_bw_double (solver, agent, GNUNET_NO);
1274 case RIL_ACTION_BW_OUT_HLV:
1275 envi_action_bw_halven (solver, agent, GNUNET_NO);
1277 case RIL_ACTION_BW_OUT_INC:
1278 envi_action_bw_inc (solver, agent, GNUNET_NO);
1280 case RIL_ACTION_BW_OUT_DEC:
1281 envi_action_bw_dec (solver, agent, GNUNET_NO);
1284 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1286 address_index = action - RIL_ACTION_TYPE_NUM;
1288 GNUNET_assert(address_index >= 0);
1290 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1292 envi_action_address_switch (solver, agent, address_index);
1295 // error - action does not exist
1296 GNUNET_assert(GNUNET_NO);
1301 * Selects the next action using the e-greedy strategy. I.e. with a probability
1302 * of (1-e) the action with the maximum expected return will be chosen
1303 * (=> exploitation) and with probability (e) a random action will be chosen.
1304 * In case the Q-learning rule is set, the function also resets the eligibility
1305 * traces in the exploration case (after Watkin's Q-learning).
1307 * @param agent the agent selecting an action
1308 * @param state the current state-feature vector
1309 * @return the action index
1312 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1315 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1316 UINT32_MAX) / (double) UINT32_MAX;
1318 if (r < agent->envi->parameters.epsilon) //explore
1320 action = agent_get_action_random(agent);
1321 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1323 agent->eligibility_reset = GNUNET_YES;
1325 agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
1330 action = agent_get_action_max(agent, state);
1336 * Selects the next action with a probability corresponding to its value. The
1337 * probability is calculated using a Boltzmann distribution with a temperature
1338 * value. The higher the temperature, the more are the action selection
1339 * probabilities the same. With a temperature of 0, the selection is greedy,
1340 * i.e. always the action with the highest value is chosen.
1346 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1350 double eqt[agent->n];
1355 a_max = agent_get_action_max(agent, state);
1357 for (i=0; i<agent->n; i++)
1359 if (agent_action_is_possible(agent, i))
1361 eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1363 eqt[i] = isinf(eqt[i]) * UINT32_MAX;
1367 for (i=0; i<agent->n; i++)
1369 if (agent_action_is_possible(agent, i))
1378 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1379 UINT32_MAX) / (double) UINT32_MAX;
1381 for (i=0; i<agent->n; i++)
1387 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1388 agent->eligibility_reset = GNUNET_YES;
1389 agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay;
1395 GNUNET_assert(GNUNET_NO);
1396 return RIL_ACTION_INVALID;
1400 * Select the next action of an agent either according to the e-greedy strategy
1401 * or the softmax strategy.
1403 * @param agent the agent in question
1404 * @param state the current state-feature vector
1405 * @return the action index
1408 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1410 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1412 return agent_select_egreedy(agent, state);
1416 return agent_select_softmax(agent, state);
1421 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1422 * after having done the last action a_old. It observes the new state s_next and the reward
1423 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1424 * next action is put into effect.
1426 * @param agent the agent performing the step
1429 agent_step (struct RIL_Peer_Agent *agent)
1431 int a_next = RIL_ACTION_INVALID;
1436 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1437 GNUNET_i2s (&agent->peer),
1438 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1440 s_next = envi_get_state (agent->envi, agent);
1441 reward = envi_get_reward (agent->envi, agent);
1443 if (agent->eligibility_reset)
1445 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1446 agent->eligibility_reset = GNUNET_NO;
1450 agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
1452 if (RIL_ACTION_INVALID != agent->a_old)
1454 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1457 switch (agent->envi->parameters.algorithm)
1459 case RIL_ALGO_SARSA:
1460 a_next = agent_select_action (agent, s_next);
1461 if (RIL_ACTION_INVALID != agent->a_old)
1463 //updates weights with selected action (on-policy), if not first step
1464 agent_update (agent, reward, s_next, a_next);
1469 a_max = agent_get_action_max (agent, s_next);
1470 if (RIL_ACTION_INVALID != agent->a_old)
1472 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1473 agent_update (agent, reward, s_next, a_max);
1475 a_next = agent_select_action (agent, s_next);
1479 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1481 LOG (GNUNET_ERROR_TYPE_DEBUG, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1488 envi_do_action (agent->envi, agent, a_next);
1490 GNUNET_free(agent->s_old);
1491 agent->s_old = s_next;
1492 agent->a_old = a_next;
1494 agent->step_count += 1;
1498 * Prototype of the ril_step() procedure
1500 * @param solver the solver handle
1503 ril_step (struct GAS_RIL_Handle *solver);
1507 * Task for the scheduler, which performs one step and lets the solver know that
1508 * no further step is scheduled.
1510 * @param cls the solver handle
1513 ril_step_scheduler_task (void *cls)
1515 struct GAS_RIL_Handle *solver = cls;
1517 solver->step_next_task_id = NULL;
1522 * Determines how much of the available bandwidth is assigned. If more is
1523 * assigned than available it returns 1. The function is used to determine the
1524 * step size of the adaptive stepping.
1526 * @param solver the solver handle
1530 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1533 struct RIL_Scope net;
1534 unsigned long long sum_assigned = 0;
1535 unsigned long long sum_available = 0;
1538 for (i = 0; i < solver->networks_count; i++)
1540 net = solver->network_entries[i];
1541 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1543 sum_assigned += net.bw_in_utilized;
1544 sum_assigned += net.bw_out_utilized;
1545 sum_available += net.bw_in_available;
1546 sum_available += net.bw_out_available;
1549 if (sum_available > 0)
1551 ratio = ((double) sum_assigned) / ((double) sum_available);
1558 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1562 * Lookup network struct by type
1564 * @param s the solver handle
1565 * @param type the network type
1566 * @return the network struct
1568 static struct RIL_Scope *
1569 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1573 for (i = 0; i < s->networks_count; i++)
1575 if (s->network_entries[i].type == type)
1577 return &s->network_entries[i];
1584 * Determines whether more connections are allocated in a network scope, than
1585 * they would theoretically fit. This is used as a heuristic to determine,
1586 * whether a new connection can be allocated or not.
1588 * @param solver the solver handle
1589 * @param network the network scope in question
1590 * @return GNUNET_YES if there are theoretically enough resources left
1593 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1595 struct RIL_Scope *net;
1596 struct RIL_Peer_Agent *agent;
1597 unsigned long long address_count = 0;
1599 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1601 if (agent->address_inuse && agent->is_active)
1603 net = agent->address_inuse->solver_information;
1604 if (net->type == network)
1611 net = ril_get_network (solver, network);
1612 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1616 * Unblocks an agent for which a connection request is there, that could not
1617 * be satisfied. Iterates over the addresses of the agent, if one of its
1618 * addresses can now be allocated in its scope the agent is unblocked,
1619 * otherwise it remains unchanged.
1621 * @param solver the solver handle
1622 * @param agent the agent in question
1626 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1628 struct RIL_Address_Wrapped *addr_wrap;
1629 struct RIL_Scope *net;
1630 unsigned long long start_in;
1631 unsigned long long start_out;
1633 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1635 net = addr_wrap->address_naked->solver_information;
1636 if (ril_network_is_not_full(solver, net->type))
1638 if (NULL == agent->address_inuse)
1640 start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
1641 start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
1642 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, start_in, start_out, silent);
1647 agent->address_inuse = NULL;
1651 * Determines how much the reward needs to be discounted depending on the amount
1652 * of time, which has passed since the last time-step.
1654 * @param solver the solver handle
1657 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1659 struct GNUNET_TIME_Absolute time_now;
1660 struct GNUNET_TIME_Relative time_delta;
1663 // MDP case only for debugging purposes
1664 if (solver->simulate)
1666 solver->global_discount_variable = solver->parameters.gamma;
1667 solver->global_discount_integrated = 1;
1673 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1674 time_now = GNUNET_TIME_absolute_get ();
1675 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1676 solver->step_time_last = time_now;
1677 tau = (double) time_delta.rel_value_us
1678 / (double) solver->parameters.step_time_min.rel_value_us;
1680 //calculate reward discounts (once per step for all agents)
1681 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1682 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1683 / (double) solver->parameters.beta;
1687 * Count the number of active agents/connections in a network scope
1689 * @param solver the solver handle
1690 * @param scope the network scope in question
1691 * @return the number of allocated connections
1694 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1697 struct RIL_Peer_Agent *cur_agent;
1699 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1701 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1710 * Calculates how much bandwidth is assigned in sum in a network scope, either
1711 * in the inbound or in the outbound direction.
1713 * @param solver the solver handle
1714 * @param type the type of the network scope in question
1715 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1716 * otherwise the outbound direction will be summed up
1717 * @return the sum of the assigned bandwidths
1719 static unsigned long long
1720 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1722 struct RIL_Peer_Agent *cur;
1723 struct RIL_Scope *net;
1724 unsigned long long sum = 0;
1726 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1728 if (cur->is_active && cur->address_inuse)
1730 net = cur->address_inuse->solver_information;
1731 if (net->type == type)
1745 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1746 * either in the inbound or in the outbound direction.
1748 * @param solver the solver handle
1749 * @param type the type of the network scope in question
1750 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1751 * otherwise the outbound direction will be summed up
1752 * @return the sum of the utilized bandwidths (in bytes/second)
1754 static unsigned long long
1755 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1757 struct RIL_Peer_Agent *cur;
1758 struct RIL_Scope *net;
1759 unsigned long long sum = 0;
1761 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1763 if (cur->is_active && cur->address_inuse)
1765 net = cur->address_inuse->solver_information;
1766 if (net->type == type)
1769 sum += cur->address_inuse->norm_utilization_in.norm;
1771 sum += cur->address_inuse->norm_utilization_out.norm;
1780 * Retrieves the state of the network scope, so that its attributes are up-to-
1783 * @param solver the solver handle
1786 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1789 struct RIL_Scope *net;
1791 for (c = 0; c < solver->networks_count; c++)
1793 net = &solver->network_entries[c];
1794 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1795 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1796 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1797 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1798 net->active_agent_count = ril_network_count_active_agents(solver, net);
1799 net->social_welfare = ril_network_get_social_welfare(solver, net);
1804 * Schedules the next global step in an adaptive way. The more resources are
1805 * left, the earlier the next step is scheduled. This serves the reactivity of
1806 * the solver to changed inputs.
1808 * @param solver the solver handle
1811 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1817 struct GNUNET_TIME_Relative time_next;
1819 used_ratio = ril_get_used_resource_ratio (solver);
1822 solver->parameters.step_time_min.rel_value_us
1823 <= solver->parameters.step_time_max.rel_value_us);
1825 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1826 solver->parameters.step_time_min).rel_value_us;
1827 offset = (double) solver->parameters.step_time_min.rel_value_us;
1828 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1830 GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
1831 GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
1833 time_next = GNUNET_TIME_relative_saturating_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1835 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1841 if (solver->simulate)
1843 time_next = GNUNET_TIME_UNIT_ZERO;
1846 if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1848 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1854 * Triggers one step per agent
1859 ril_step (struct GAS_RIL_Handle *solver)
1861 struct RIL_Peer_Agent *cur;
1863 if (GNUNET_YES == solver->bulk_lock)
1865 solver->bulk_changes++;
1869 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1871 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1873 if (0 == solver->step_count)
1875 solver->step_time_last = GNUNET_TIME_absolute_get ();
1878 ril_calculate_discount (solver);
1879 ril_networks_update_state (solver);
1881 //trigger one step per active, unblocked agent
1882 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1886 if (NULL == cur->address_inuse)
1888 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1890 if (cur->address_inuse)
1897 ril_networks_update_state (solver);
1899 solver->step_count++;
1900 ril_step_schedule_next (solver);
1902 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1904 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1905 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1907 if (cur->suggestion_issue) {
1908 solver->env->bandwidth_changed_cb (solver->env->cls,
1909 cur->suggestion_address);
1910 cur->suggestion_issue = GNUNET_NO;
1913 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1917 * Initializes the matrix W of parameter vectors theta with small random numbers.
1919 * @param agent The respective agent
1922 agent_w_init (struct RIL_Peer_Agent *agent)
1927 for (i = 0; i < agent->n; i++)
1929 for (k = 0; k < agent->m; k++)
1931 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));
1937 * Initialize an agent without addresses and its knowledge base
1939 * @param s ril solver
1940 * @param peer the one in question
1941 * @return handle to the new agent
1943 static struct RIL_Peer_Agent *
1944 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1947 struct GAS_RIL_Handle * solver = s;
1948 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1950 agent->envi = solver;
1951 agent->peer = *peer;
1952 agent->step_count = 0;
1953 agent->is_active = GNUNET_NO;
1954 agent->bw_in = RIL_MIN_BW;
1955 agent->bw_out = RIL_MIN_BW;
1956 agent->suggestion_issue = GNUNET_NO;
1957 agent->n = RIL_ACTION_TYPE_NUM;
1959 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1960 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1961 for (i = 0; i < agent->n; i++)
1963 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1964 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1966 agent_w_init(agent);
1967 agent->eligibility_reset = GNUNET_NO;
1968 agent->a_old = RIL_ACTION_INVALID;
1969 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1970 agent->address_inuse = NULL;
1971 agent->objective_old = 0;
1972 agent->nop_bonus = 0;
1980 * @param solver the solver handle
1981 * @param agent the agent to retire
1984 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1988 for (i = 0; i < agent->n; i++)
1990 GNUNET_free_non_null(agent->W[i]);
1991 GNUNET_free_non_null(agent->E[i]);
1993 GNUNET_free_non_null(agent->W);
1994 GNUNET_free_non_null(agent->E);
1995 GNUNET_free_non_null(agent->s_old);
2000 * Returns the agent for a peer
2002 * @param solver the solver handle
2003 * @param peer the identity of the peer
2004 * @param create whether or not to create an agent, if none is allocated yet
2007 static struct RIL_Peer_Agent *
2008 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
2010 struct RIL_Peer_Agent *cur;
2012 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2014 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
2022 cur = agent_init (solver, peer);
2023 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
2030 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
2031 * considered inactive and not used. Addresses in an inactive network are ignored.
2033 * @param solver solver handle
2034 * @param network the network type
2035 * @return whether or not the network is considered active
2038 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
2040 struct RIL_Scope *net;
2042 net = ril_get_network (solver, network);
2043 return net->bw_out_available >= RIL_MIN_BW;
2047 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
2048 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
2049 * frees the memory of the old vector, and redirects the pointer to the new one.
2051 * @param old pointer to the pointer to the first element of the vector
2052 * @param element_size byte size of the vector elements
2053 * @param hole_start the first element to cut out
2054 * @param hole_length the number of elements to cut out
2055 * @param old_length the length of the old vector
2058 ril_cut_from_vector (void **old,
2059 size_t element_size,
2060 unsigned int hole_start,
2061 unsigned int hole_length,
2062 unsigned int old_length)
2065 char *oldptr = (char *) *old;
2067 unsigned int bytes_before;
2068 unsigned int bytes_hole;
2069 unsigned int bytes_after;
2071 GNUNET_assert(old_length >= hole_length);
2072 GNUNET_assert(old_length >= (hole_start + hole_length));
2074 size = element_size * (old_length - hole_length);
2076 bytes_before = element_size * hole_start;
2077 bytes_hole = element_size * hole_length;
2078 bytes_after = element_size * (old_length - hole_start - hole_length);
2086 tmpptr = GNUNET_malloc (size);
2087 GNUNET_memcpy (tmpptr, oldptr, bytes_before);
2088 GNUNET_memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2094 *old = (void *) tmpptr;
2098 * Solver API functions
2099 * ---------------------------
2103 * Change relative preference for quality in solver
2105 * @param solver the solver handle
2106 * @param peer the peer to change the preference for
2107 * @param kind the kind to change the preference
2108 * @param pref_rel the normalized preference value for this kind over all clients
2111 GAS_ril_address_change_preference (void *solver,
2112 const struct GNUNET_PeerIdentity *peer,
2113 enum GNUNET_ATS_PreferenceKind kind,
2116 LOG(GNUNET_ERROR_TYPE_DEBUG,
2117 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2118 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2120 struct GAS_RIL_Handle *s = solver;
2122 s->parameters.temperature = s->parameters.temperature_init;
2123 s->parameters.epsilon = s->parameters.epsilon_init;
2129 * Add a new address for a peer to the solver
2131 * The address is already contained in the addresses hashmap!
2133 * @param solver the solver Handle
2134 * @param address the address to add
2135 * @param network network type of this address
2138 GAS_ril_address_add (void *solver,
2139 struct ATS_Address *address,
2142 struct GAS_RIL_Handle *s = solver;
2143 struct RIL_Peer_Agent *agent;
2144 struct RIL_Address_Wrapped *address_wrapped;
2145 struct RIL_Scope *net;
2153 LOG (GNUNET_ERROR_TYPE_DEBUG,
2154 "API_address_add()\n");
2156 net = ril_get_network (s, network);
2157 address->solver_information = net;
2159 if (!ril_network_is_active (s, network))
2161 LOG(GNUNET_ERROR_TYPE_DEBUG,
2162 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2163 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2167 s->parameters.temperature = s->parameters.temperature_init;
2168 s->parameters.epsilon = s->parameters.epsilon_init;
2170 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2173 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2174 address_wrapped->address_naked = address;
2175 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2177 //increase size of W
2178 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2180 n_new = agent->n + 1;
2183 GNUNET_array_grow(agent->W, agent->n, n_new);
2185 GNUNET_array_grow(agent->E, agent->n, n_new);
2186 for (i = 0; i < n_new; i++)
2191 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2193 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2198 GNUNET_array_grow(agent->W[i], zero, m_new);
2200 GNUNET_array_grow(agent->E[i], zero, m_new);
2204 //increase size of old state vector
2206 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2208 ril_try_unblock_agent(s, agent, GNUNET_NO);
2212 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2213 address->active ? "active" : "inactive", address->plugin, address->addr,
2214 GNUNET_i2s (&address->peer));
2218 * Delete an address in the solver
2220 * The address is not contained in the address hashmap anymore!
2222 * @param solver the solver handle
2223 * @param address the address to remove
2226 GAS_ril_address_delete (void *solver,
2227 struct ATS_Address *address)
2229 struct GAS_RIL_Handle *s = solver;
2230 struct RIL_Peer_Agent *agent;
2231 struct RIL_Address_Wrapped *address_wrapped;
2236 struct RIL_Scope *net;
2238 LOG (GNUNET_ERROR_TYPE_DEBUG,
2239 "API_address_delete() Delete %s %s address %s for peer '%s'\n",
2240 address->active ? "active" : "inactive",
2243 GNUNET_i2s (&address->peer));
2245 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2248 net = address->solver_information;
2249 GNUNET_assert(! ril_network_is_active (s, net->type));
2250 LOG (GNUNET_ERROR_TYPE_DEBUG,
2251 "No agent allocated for peer yet, since address was in inactive network\n");
2255 s->parameters.temperature = s->parameters.temperature_init;
2256 s->parameters.epsilon = s->parameters.epsilon_init;
2258 address_index = agent_address_get_index (agent, address);
2259 address_wrapped = agent_address_get_wrapped (agent, address);
2261 if (NULL == address_wrapped)
2263 net = address->solver_information;
2264 LOG (GNUNET_ERROR_TYPE_DEBUG,
2265 "Address not considered by agent, address was in inactive network\n");
2268 GNUNET_CONTAINER_DLL_remove (agent->addresses_head,
2269 agent->addresses_tail,
2271 GNUNET_free (address_wrapped);
2274 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2275 n_new = agent->n - 1;
2277 for (i = 0; i < agent->n; i++)
2279 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2280 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2281 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2282 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2283 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2284 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2286 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2287 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2288 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2290 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2292 //correct last action
2293 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2297 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2299 agent->a_old = RIL_ACTION_INVALID;
2301 //decrease old state vector
2302 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2303 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2304 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2308 if (agent->address_inuse == address)
2310 if (NULL != agent->addresses_head) //if peer has an address left, use it
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2313 "Active address died, suggesting alternative!\n");
2314 envi_set_active_suggestion (s,
2316 agent->addresses_head->address_naked,
2323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324 "Active address died, suggesting disconnect!\n");
2325 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2329 if (agent->suggestion_address == address)
2331 agent->suggestion_issue = GNUNET_NO;
2332 agent->suggestion_address = NULL;
2334 GNUNET_assert (agent->address_inuse != address);
2339 * Update the properties of an address in the solver
2341 * @param solver solver handle
2342 * @param address the address
2345 GAS_ril_address_property_changed (void *solver,
2346 struct ATS_Address *address)
2348 struct GAS_RIL_Handle *s = solver;
2350 LOG(GNUNET_ERROR_TYPE_DEBUG,
2351 "Properties for peer '%s' address changed\n",
2352 GNUNET_i2s (&address->peer));
2353 s->parameters.temperature = s->parameters.temperature_init;
2354 s->parameters.epsilon = s->parameters.epsilon_init;
2360 * Give feedback about the current assignment
2362 * @param solver the solver handle
2363 * @param application the application
2364 * @param peer the peer to change the preference for
2365 * @param scope the time interval for this feedback: [now - scope .. now]
2366 * @param kind the kind to change the preference
2367 * @param score the score
2370 GAS_ril_address_preference_feedback (void *solver,
2371 struct GNUNET_SERVICE_Client *application,
2372 const struct GNUNET_PeerIdentity *peer,
2373 const struct GNUNET_TIME_Relative scope,
2374 enum GNUNET_ATS_PreferenceKind kind,
2377 LOG (GNUNET_ERROR_TYPE_DEBUG,
2378 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2379 "preference %s for %d seconds\n",
2382 GNUNET_ATS_print_preference_type (kind),
2383 scope.rel_value_us / 1000000);
2388 * Start a bulk operation
2390 * @param solver the solver
2393 GAS_ril_bulk_start (void *solver)
2395 struct GAS_RIL_Handle *s = solver;
2397 LOG (GNUNET_ERROR_TYPE_DEBUG,
2398 "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2405 * Bulk operation done
2407 * @param solver the solver handle
2410 GAS_ril_bulk_stop (void *solver)
2412 struct GAS_RIL_Handle *s = solver;
2414 LOG(GNUNET_ERROR_TYPE_DEBUG,
2415 "API_bulk_stop() lock: %d\n",
2418 if (s->bulk_lock < 1)
2425 if (0 < s->bulk_changes)
2428 s->bulk_changes = 0;
2434 * Tell solver to notify ATS if the address to use changes for a specific
2435 * peer using the bandwidth changed callback
2437 * The solver must only notify about changes for peers with pending address
2440 * @param solver the solver handle
2441 * @param peer the identity of the peer
2444 GAS_ril_get_preferred_address (void *solver,
2445 const struct GNUNET_PeerIdentity *peer)
2447 struct GAS_RIL_Handle *s = solver;
2448 struct RIL_Peer_Agent *agent;
2450 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2452 agent = ril_get_agent (s, peer, GNUNET_YES);
2454 agent->is_active = GNUNET_YES;
2455 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2457 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2459 if (agent->address_inuse)
2461 LOG(GNUNET_ERROR_TYPE_DEBUG,
2462 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2463 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2467 LOG(GNUNET_ERROR_TYPE_DEBUG,
2468 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2470 s->parameters.temperature = s->parameters.temperature_init;
2471 s->parameters.epsilon = s->parameters.epsilon_init;
2473 if (NULL != agent->address_inuse)
2474 s->env->bandwidth_changed_cb (s->env->cls,
2475 agent->address_inuse);
2480 * Tell solver stop notifying ATS about changes for this peers
2482 * The solver must only notify about changes for peers with pending address
2485 * @param solver the solver handle
2486 * @param peer the peer
2489 GAS_ril_stop_get_preferred_address (void *solver,
2490 const struct GNUNET_PeerIdentity *peer)
2492 struct GAS_RIL_Handle *s = solver;
2493 struct RIL_Peer_Agent *agent;
2495 LOG (GNUNET_ERROR_TYPE_DEBUG,
2496 "API_stop_get_preferred_address()");
2498 agent = ril_get_agent (s, peer, GNUNET_NO);
2505 if (GNUNET_NO == agent->is_active)
2511 s->parameters.temperature = s->parameters.temperature_init;
2512 s->parameters.epsilon = s->parameters.epsilon_init;
2514 agent->is_active = GNUNET_NO;
2516 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2521 LOG(GNUNET_ERROR_TYPE_DEBUG,
2522 "API_stop_get_preferred_address() Paused agent for peer '%s'\n",
2528 * Entry point for the plugin
2530 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2533 libgnunet_plugin_ats_ril_init (void *cls)
2535 static struct GNUNET_ATS_SolverFunctions sf;
2536 struct GNUNET_ATS_PluginEnvironment *env = cls;
2537 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2538 struct RIL_Scope * cur;
2543 LOG (GNUNET_ERROR_TYPE_DEBUG,
2544 "API_init() Initializing RIL solver\n");
2546 GNUNET_assert (NULL != env);
2547 GNUNET_assert (NULL != env->cfg);
2548 GNUNET_assert (NULL != env->stats);
2549 GNUNET_assert (NULL != env->bandwidth_changed_cb);
2550 GNUNET_assert (NULL != env->get_preferences);
2552 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2554 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2558 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2559 &solver->parameters.step_time_min))
2561 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2565 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2566 &solver->parameters.step_time_max))
2568 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2571 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2573 GNUNET_STRINGS_utf8_toupper (string, string);
2574 if (0 == strcmp (string, "SARSA"))
2576 solver->parameters.algorithm = RIL_ALGO_SARSA;
2578 if (0 == strcmp (string, "Q-LEARNING"))
2580 solver->parameters.algorithm = RIL_ALGO_Q;
2583 GNUNET_free (string);
2587 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2590 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2592 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2593 GNUNET_free (string);
2597 solver->parameters.select = RIL_DEFAULT_SELECT;
2601 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2602 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2603 "RIL_DISCOUNT_BETA", &f_tmp))
2607 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2608 "RIL_DISCOUNT_BETA", f_tmp);
2612 solver->parameters.beta = f_tmp;
2613 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2614 "RIL_DISCOUNT_BETA", f_tmp);
2618 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2619 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2620 "RIL_DISCOUNT_GAMMA", &f_tmp))
2622 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2624 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2625 "RIL_DISCOUNT_GAMMA", f_tmp);
2629 solver->parameters.gamma = f_tmp;
2630 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2631 "RIL_DISCOUNT_GAMMA", f_tmp);
2635 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2636 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2637 "RIL_GRADIENT_STEP_SIZE", &f_tmp))
2639 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2641 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2642 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2646 solver->parameters.alpha = f_tmp;
2647 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2648 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2652 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2653 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2654 "RIL_TRACE_DECAY", &f_tmp))
2656 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2658 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2659 "RIL_TRACE_DECAY", f_tmp);
2663 solver->parameters.lambda = f_tmp;
2664 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2665 "RIL_TRACE_DECAY", f_tmp);
2669 solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
2670 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2671 "RIL_EXPLORE_RATIO", &f_tmp))
2673 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2675 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2676 "RIL_EXPLORE_RATIO", f_tmp);
2680 solver->parameters.epsilon_init = f_tmp;
2681 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2682 "RIL_EXPLORE_RATIO", f_tmp);
2686 solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
2687 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2688 "RIL_EXPLORE_DECAY", &f_tmp))
2690 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2692 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2693 "RIL_EXPLORE_DECAY", f_tmp);
2697 solver->parameters.epsilon_decay = f_tmp;
2698 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2699 "RIL_EXPLORE_DECAY", f_tmp);
2703 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2704 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2705 "RIL_TEMPERATURE", &f_tmp))
2709 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2710 "RIL_TEMPERATURE", f_tmp);
2714 solver->parameters.temperature_init = f_tmp;
2715 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2716 "RIL_TEMPERATURE", f_tmp);
2720 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
2721 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2722 "RIL_TEMPERATURE_DECAY", &f_tmp))
2724 if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1)
2726 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2727 "RIL_TEMPERATURE_DECAY", f_tmp);
2731 solver->parameters.temperature_decay = f_tmp;
2732 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2733 "RIL_TEMPERATURE_DECAY", f_tmp);
2737 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2739 solver->simulate = GNUNET_NO;
2742 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2744 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2748 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2751 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2753 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2754 GNUNET_free (string);
2758 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2763 sf.s_add = &GAS_ril_address_add;
2764 sf.s_address_update_property = &GAS_ril_address_property_changed;
2765 sf.s_get = &GAS_ril_get_preferred_address;
2766 sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2767 sf.s_pref = &GAS_ril_address_change_preference;
2768 sf.s_feedback = &GAS_ril_address_preference_feedback;
2769 sf.s_del = &GAS_ril_address_delete;
2770 sf.s_bulk_start = &GAS_ril_bulk_start;
2771 sf.s_bulk_stop = &GAS_ril_bulk_stop;
2773 solver->networks_count = env->network_count;
2774 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2775 solver->step_count = 0;
2776 solver->done = GNUNET_NO;
2778 for (c = 0; c < env->network_count; c++)
2780 cur = &solver->network_entries[c];
2782 cur->bw_in_available = env->in_quota[c];
2783 cur->bw_out_available = env->out_quota[c];
2784 LOG (GNUNET_ERROR_TYPE_DEBUG,
2785 "init() Quotas for %s network: IN %llu - OUT %llu\n",
2786 GNUNET_ATS_print_network_type(cur->type),
2787 cur->bw_in_available/1024,
2788 cur->bw_out_available/1024);
2791 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2792 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2793 solver->parameters.algorithm ? "Q" : "SARSA",
2794 solver->parameters.alpha,
2795 solver->parameters.beta,
2796 solver->parameters.lambda);
2797 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
2798 solver->parameters.epsilon,
2799 solver->parameters.temperature,
2800 solver->parameters.select ? "EGREEDY" : "SOFTMAX");
2801 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2802 solver->parameters.rbf_divisor);
2809 * Exit point for the plugin
2811 * @param cls the solver handle
2814 libgnunet_plugin_ats_ril_done (void *cls)
2816 struct GNUNET_ATS_SolverFunctions *sf = cls;
2817 struct GAS_RIL_Handle *s = sf->cls;
2818 struct RIL_Peer_Agent *cur_agent;
2819 struct RIL_Peer_Agent *next_agent;
2821 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2823 s->done = GNUNET_YES;
2825 cur_agent = s->agents_head;
2826 while (NULL != cur_agent)
2828 next_agent = cur_agent->next;
2829 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2830 agent_die (s, cur_agent);
2831 cur_agent = next_agent;
2834 if (NULL != s->step_next_task_id)
2836 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2838 GNUNET_free(s->network_entries);
2845 /* end of plugin_ats_ril.c */