2 This file is part of GNUnet.
3 (C) 2011-2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/plugin_ats_ril.c
23 * @brief ATS reinforcement learning solver
24 * @author Fabian Oehlmann
25 * @author Matthias Wachs
30 #include "gnunet_ats_plugin.h"
31 #include "gnunet-service-ats_addresses.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
37 #define RIL_MIN_BW (5 * ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__))
38 #define RIL_MAX_BW GNUNET_ATS_MaxBandwidth
40 #define RIL_ACTION_INVALID -1
41 #define RIL_INTERVAL_EXPONENT 10
42 #define RIL_UTILITY_DELAY_MAX 1000
44 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200)
45 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 2000)
46 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
47 #define RIL_DEFAULT_SELECT RIL_SELECT_SOFTMAX
48 #define RIL_DEFAULT_WELFARE RIL_WELFARE_NASH
49 #define RIL_DEFAULT_DISCOUNT_BETA 0.6
50 #define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
51 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.01
52 #define RIL_DEFAULT_TRACE_DECAY 0.5
53 #define RIL_DEFAULT_EXPLORE_RATIO 1
54 #define RIL_DEFAULT_EXPLORE_DECAY 0.95
55 #define RIL_DEFAULT_RBF_DIVISOR 50
56 #define RIL_DEFAULT_TEMPERATURE 0.1
57 #define RIL_DEFAULT_TEMPERATURE_DECAY 1
59 #define RIL_INC_DEC_STEP_SIZE 1
60 #define RIL_NOP_DECAY 0.5
63 * ATS reinforcement learning solver
69 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
70 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
71 * to a particular address. The action of switching to address with index i is depicted by the
72 * number (RIL_ACTION_TYPE_NUM + i).
76 RIL_ACTION_NOTHING = 0,
77 RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions
78 RIL_ACTION_BW_IN_HLV = -3,
79 RIL_ACTION_BW_IN_INC = 1,
80 RIL_ACTION_BW_IN_DEC = 2,
81 RIL_ACTION_BW_OUT_DBL = -4,
82 RIL_ACTION_BW_OUT_HLV = -5,
83 RIL_ACTION_BW_OUT_INC = 3,
84 RIL_ACTION_BW_OUT_DEC = 4,
85 RIL_ACTION_TYPE_NUM = 5
96 RIL_SELECT_SOFTMAX = 0,
97 RIL_SELECT_EGREEDY = 1
103 RIL_WELFARE_EGALITARIAN
106 enum RIL_E_Modification
115 * Global learning parameters
117 struct RIL_Learning_Parameters
120 * The TD-algorithm to use
122 enum RIL_Algorithm algorithm;
125 * Gradient-descent step-size
130 * Learning discount variable in the TD-update for semi-MDPs
135 * Learning discount factor in the TD-update for MDPs
140 * Trace-decay factor for eligibility traces
145 * Whether to accumulate or replace eligibility traces
147 enum RIL_E_Modification eligibility_trace_mode;
150 * Initial softmax action-selection temperature
152 double temperature_init;
155 * Softmax action-selection temperature
160 * Decay factor of the temperature value
162 double temperature_decay;
165 * Which measure of social welfare should be used
167 enum RIL_Welfare social_welfare;
170 * State space divisor
172 unsigned long long rbf_divisor;
175 * Action selection strategy;
177 enum RIL_Select select;
180 * Initial exploration ratio value
185 * Ratio, with what probability an agent should explore in the e-greed policy
190 * Decay factor of the explore ratio
192 double epsilon_decay;
195 * Minimal interval time between steps in milliseconds
197 struct GNUNET_TIME_Relative step_time_min;
200 * Maximum interval time between steps in milliseconds
202 struct GNUNET_TIME_Relative step_time_max;
206 * Wrapper for addresses to store them in agent's linked list
208 struct RIL_Address_Wrapped
213 struct RIL_Address_Wrapped *next;
218 struct RIL_Address_Wrapped *prev;
223 struct ATS_Address *address_naked;
227 struct RIL_Peer_Agent
230 * Next agent in solver's linked list
232 struct RIL_Peer_Agent *next;
235 * Previous agent in solver's linked list
237 struct RIL_Peer_Agent *prev;
242 struct GAS_RIL_Handle *envi;
247 struct GNUNET_PeerIdentity peer;
250 * Whether the agent is active or not
255 * Number of performed time-steps
257 unsigned long long step_count;
260 * Experience matrix W
265 * Number of rows of W / Number of state-vector features
270 * Number of columns of W / Number of actions
275 * Last perceived state feature vector
290 * Whether to reset the eligibility traces to 0 after a Q-exploration step
292 int eligibility_reset;
297 struct ATS_Address *address_inuse;
300 * Head of addresses DLL
302 struct RIL_Address_Wrapped *addresses_head;
305 * Tail of addresses DLL
307 struct RIL_Address_Wrapped *addresses_tail;
310 * Inbound bandwidth assigned by the agent
315 * Outbound bandwidth assigned by the agent
320 * Flag whether a suggestion has to be issued
322 int suggestion_issue;
325 * The address which has to be issued
327 struct ATS_Address *suggestion_address;
330 * The agent's last objective value
332 double objective_old;
345 enum GNUNET_ATS_Network_Type type;
348 * Total available inbound bandwidth
350 uint32_t bw_in_available;
353 * Bandwidth inbound assigned in network after last step
355 uint32_t bw_in_assigned;
358 * Bandwidth inbound actually utilized in the network
360 uint32_t bw_in_utilized;
363 * Total available outbound bandwidth
365 uint32_t bw_out_available;
368 * Bandwidth outbound assigned in network after last step
370 unsigned long long bw_out_assigned;
373 * Bandwidth outbound actually utilized in the network
375 unsigned long long bw_out_utilized;
378 * Number of active agents in scope
380 unsigned int active_agent_count;
383 * The social welfare achieved in the scope
385 double social_welfare;
389 * A handle for the reinforcement learning solver
391 struct GAS_RIL_Handle
394 * The solver-plugin environment of the solver-plugin API
396 struct GNUNET_ATS_PluginEnvironment *env;
399 * Number of performed steps
401 unsigned long long step_count;
404 * Timestamp for the last time-step
406 struct GNUNET_TIME_Absolute step_time_last;
409 * Task identifier of the next time-step to be executed
411 struct GNUNET_SCHEDULER_Task * step_next_task_id;
414 * Variable discount factor, dependent on time between steps
416 double global_discount_variable;
419 * Integrated variable discount factor, dependent on time between steps
421 double global_discount_integrated;
424 * Lock for bulk operations
429 * Number of changes during a lock
434 * Learning parameters
436 struct RIL_Learning_Parameters parameters;
439 * Array of networks with global assignment state
441 struct RIL_Scope * network_entries;
446 unsigned int networks_count;
449 * List of active peer-agents
451 struct RIL_Peer_Agent * agents_head;
452 struct RIL_Peer_Agent * agents_tail;
460 * Simulate steps, i.e. schedule steps immediately
462 unsigned long long simulate;
466 * "Private" functions
467 * ---------------------------
471 * Estimate the current action-value for state s and action a
473 * @param agent agent performing the estimation
476 * @return estimation value
479 agent_q (struct RIL_Peer_Agent *agent,
480 const double *state, int action)
485 for (i = 0; i < agent->m; i++)
487 result += state[i] * agent->W[action][i];
490 GNUNET_assert(!isnan(result));
492 //prevent crash when learning diverges
495 return isinf(result) * UINT32_MAX;
502 * Get the index of the address in the agent's list.
504 * @param agent agent handle
505 * @param address address handle
506 * @return the index, starting with zero
509 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
512 struct RIL_Address_Wrapped *cur;
515 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
518 if (cur->address_naked == address)
526 * Gets the wrapped address from the agent's list
528 * @param agent agent handle
529 * @param address address handle
530 * @return wrapped address
532 static struct RIL_Address_Wrapped *
533 agent_address_get_wrapped (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
535 struct RIL_Address_Wrapped *cur;
537 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
538 if (cur->address_naked == address)
545 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
551 case RIL_ACTION_NOTHING:
554 case RIL_ACTION_BW_IN_INC:
555 case RIL_ACTION_BW_IN_DBL:
556 if (agent->bw_in >= RIL_MAX_BW)
561 case RIL_ACTION_BW_IN_DEC:
562 case RIL_ACTION_BW_IN_HLV:
563 if (agent->bw_in <= 0)
568 case RIL_ACTION_BW_OUT_INC:
569 case RIL_ACTION_BW_OUT_DBL:
570 if (agent->bw_out >= RIL_MAX_BW)
575 case RIL_ACTION_BW_OUT_DEC:
576 case RIL_ACTION_BW_OUT_HLV:
577 if (agent->bw_out <= 0)
583 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
585 address_index = action - RIL_ACTION_TYPE_NUM;
587 GNUNET_assert(address_index >= 0);
589 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
591 if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
592 agent->address_inuse->active)
598 // error - action does not exist
599 GNUNET_assert(GNUNET_NO);
605 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
606 * most reward in the future)
608 * @param agent agent performing the calculation
609 * @param state the state from which to take the action
610 * @return the action promising most future reward
613 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
616 int max_i = RIL_ACTION_INVALID;
618 double max_q = -DBL_MAX;
620 for (i = 0; i < agent->n; i++)
622 if (agent_action_is_possible(agent, i))
624 cur_q = agent_q (agent, state, i);
633 GNUNET_assert(RIL_ACTION_INVALID != max_i);
639 * Chooses a random action from the set of possible ones
641 * @param agent the agent performing the action
642 * @return the action index
645 agent_get_action_random (struct RIL_Peer_Agent *agent)
648 int is_possible[agent->n];
652 for (i = 0; i<agent->n; i++)
654 if (agent_action_is_possible(agent, i))
656 is_possible[i] = GNUNET_YES;
661 is_possible[i] = GNUNET_NO;
665 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
668 for (i = 0; i<agent->n; i++)
678 GNUNET_assert(GNUNET_NO);
679 return RIL_ACTION_INVALID;
684 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
686 * @param agent the agent performing the update
687 * @param reward the reward received for the last action
688 * @param s_next the new state, the last step got the agent into
689 * @param a_prime the new
692 agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
697 double **theta = agent->W;
699 delta = agent->envi->global_discount_integrated * reward; //reward
700 delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
701 delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
703 // 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",
704 // agent->step_count,
705 // agent_q (agent, agent->s_old, agent->a_old),
706 // agent->envi->parameters.alpha,
708 // agent->envi->global_discount_variable,
709 // agent_q (agent, s_next, a_prime),
712 for (k = 0; k < agent->n; k++)
714 for (i = 0; i < agent->m; i++)
716 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
717 // agent->envi->parameters.alpha,
721 theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
728 * Changes the eligibility trace vector e in various manners:
729 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
730 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
731 * #RIL_E_DECAY - multiplies e with discount factor and lambda as in the update rule
732 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
734 * @param agent the agent handle
735 * @param mod the kind of modification
736 * @param feature the feature vector
737 * @param action the action to take
740 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
741 enum RIL_E_Modification mod,
748 for (i = 0; i < agent->m; i++)
752 case RIL_E_ACCUMULATE:
753 agent->E[action][i] += feature[i];
756 agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
759 for (k = 0; k < agent->n; k++)
761 agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
765 for (k = 0; k < agent->n; k++)
775 * Informs the environment about the status of the solver
782 ril_inform (struct GAS_RIL_Handle *solver,
783 enum GAS_Solver_Operation op,
784 enum GAS_Solver_Status stat)
786 if (NULL != solver->env->info_cb)
787 solver->env->info_cb (solver->env->cls, op, stat, GAS_INFO_NONE);
791 * Calculates the maximum bandwidth an agent can assign in a network scope
795 static unsigned long long
796 ril_get_max_bw (struct RIL_Scope *net)
798 return GNUNET_MIN(2 * GNUNET_MAX(net->bw_in_available, net->bw_out_available), GNUNET_ATS_MaxBandwidth);
802 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
803 * notify ATS of its new decision
805 * @param solver solver handle
806 * @param agent agent handle
807 * @param new_address the address which is to be used
808 * @param new_bw_in the new amount of inbound bandwidth set for this address
809 * @param new_bw_out the new amount of outbound bandwidth set for this address
810 * @param silent disables invocation of the bw_changed callback, if #GNUNET_YES
813 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
814 struct RIL_Peer_Agent *agent,
815 struct ATS_Address *new_address,
816 unsigned long long new_bw_in,
817 unsigned long long new_bw_out,
820 int notify = GNUNET_NO;
822 LOG(GNUNET_ERROR_TYPE_DEBUG,
823 " set_active_suggestion() for peer '%s'\n",
824 GNUNET_i2s (&agent->peer));
827 if (agent->address_inuse != new_address)
829 if (NULL != agent->address_inuse)
831 agent->address_inuse->active = GNUNET_NO;
832 agent->address_inuse->assigned_bw_in = 0;
833 agent->address_inuse->assigned_bw_out = 0;
835 if (NULL != new_address)
837 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
838 new_address->active = agent->is_active;
839 new_address->assigned_bw_in = agent->bw_in;
840 new_address->assigned_bw_out = agent->bw_out;
842 notify |= GNUNET_YES;
848 if (new_address->active != agent->is_active)
850 new_address->active = agent->is_active;
851 notify |= GNUNET_YES;
855 if (agent->bw_in != new_bw_in)
857 agent->bw_in = new_bw_in;
858 new_address->assigned_bw_in = new_bw_in;
859 notify |= GNUNET_YES;
861 if (agent->bw_out != new_bw_out)
863 agent->bw_out = new_bw_out;
864 new_address->assigned_bw_out = new_bw_out;
865 notify |= GNUNET_YES;
869 if (notify && agent->is_active && (GNUNET_NO == silent))
873 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
874 agent->suggestion_issue = GNUNET_YES;
875 agent->suggestion_address = new_address;
877 else if (agent->address_inuse)
879 /* disconnect case, no new address */
880 GNUNET_assert(0 == agent->address_inuse->assigned_bw_in);
881 GNUNET_assert(0 == agent->address_inuse->assigned_bw_out);
885 agent->suggestion_issue = GNUNET_YES;
886 agent->suggestion_address = agent->address_inuse;
889 agent->address_inuse = new_address;
894 * Allocates a state vector and fills it with the features present
895 * @param solver the solver handle
896 * @param agent the agent handle
897 * @return pointer to the state vector
900 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
911 unsigned long long max_bw;
913 state = GNUNET_malloc (sizeof(double) * agent->m);
915 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
917 y[0] = (double) agent->bw_out;
918 y[1] = (double) agent->bw_in;
920 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
921 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
923 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
925 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
926 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
929 sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
930 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
939 * Retrieves an ATS information value of an address
941 * @param address the address in question
942 * @param type the ATS information type
946 ril_get_atsi (struct ATS_Address *address, uint32_t type)
949 GNUNET_assert(NULL != address);
951 if ((NULL == address->atsi) || (0 == address->atsi_count))
952 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
954 for (c1 = 0; c1 < address->atsi_count; c1++)
956 if (ntohl (address->atsi[c1].type) == type)
957 return ntohl (address->atsi[c1].value);
959 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
963 * Returns the utility value of the connection an agent manages
965 * @param agent the agent in question
966 * @return the utility value
969 agent_get_utility (struct RIL_Peer_Agent *agent)
971 const double *preferences;
976 preferences = agent->envi->env->get_preferences (agent->envi->env->cls,
979 delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
980 delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
982 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
983 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
984 sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
985 // sqrt((double) (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)/RIL_MIN_BW) * (double) (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)/RIL_MIN_BW));
987 // return (double) (agent->bw_in/RIL_MIN_BW);
988 // return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
993 * Calculates the social welfare within a network scope according to what social
994 * welfare measure is set in the configuration.
996 * @param solver the solver handle
997 * @param scope the network scope in question
998 * @return the social welfare value
1001 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1003 struct RIL_Peer_Agent *cur;
1006 switch (solver->parameters.social_welfare)
1008 case RIL_WELFARE_EGALITARIAN:
1010 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1012 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1014 result = GNUNET_MIN(result, agent_get_utility(cur));
1019 case RIL_WELFARE_NASH:
1021 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1023 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1025 result *= pow(agent_get_utility(cur), 1.0 / (double) scope->active_agent_count);
1030 GNUNET_assert(GNUNET_NO);
1035 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1037 struct RIL_Scope *net;
1038 unsigned long long over_max;
1039 unsigned long long over_in = 0;
1040 unsigned long long over_out = 0;
1042 net = agent->address_inuse->solver_information;
1044 if (net->bw_in_utilized > net->bw_in_available)
1046 over_in = net->bw_in_utilized - net->bw_in_available;
1047 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1049 /* increase quadratically */
1053 if (net->bw_out_utilized > net->bw_out_available)
1055 over_out = net->bw_out_utilized - net->bw_out_available;
1056 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1058 /* increase quadratically */
1059 over_out *= over_out;
1062 over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
1064 return -1.0 * (double) over_max;
1068 * Gets the reward for the last performed step, which is calculated in equal
1069 * parts from the local (the peer specific) and the global (for all peers
1070 * identical) reward.
1072 * @param solver the solver handle
1073 * @param agent the agent handle
1074 * @return the reward
1077 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1079 struct RIL_Scope *net;
1086 net = agent->address_inuse->solver_information;
1088 penalty = envi_get_penalty(solver, agent);
1089 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1090 delta = objective - agent->objective_old;
1091 agent->objective_old = objective;
1093 if (delta != 0 && penalty == 0)
1095 agent->nop_bonus = delta * RIL_NOP_DECAY;
1099 agent->nop_bonus *= RIL_NOP_DECAY;
1102 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1104 reward = delta + steady;
1105 return reward + penalty;
1109 * Doubles the bandwidth for the active address
1111 * @param solver solver handle
1112 * @param agent agent handle
1113 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1116 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1117 struct RIL_Peer_Agent *agent,
1120 unsigned long long new_bw;
1121 unsigned long long max_bw;
1123 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1127 new_bw = agent->bw_in * 2;
1128 if (new_bw < agent->bw_in || new_bw > max_bw)
1130 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1131 agent->bw_out, GNUNET_NO);
1135 new_bw = agent->bw_out * 2;
1136 if (new_bw < agent->bw_out || new_bw > max_bw)
1138 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1144 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1145 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
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_halven (struct GAS_RIL_Handle *solver,
1154 struct RIL_Peer_Agent *agent,
1157 unsigned long long new_bw;
1161 new_bw = agent->bw_in / 2;
1162 if (new_bw <= 0 || new_bw > agent->bw_in)
1164 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1169 new_bw = agent->bw_out / 2;
1170 if (new_bw <= 0 || new_bw > agent->bw_out)
1172 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1178 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1180 * @param solver solver handle
1181 * @param agent agent handle
1182 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1186 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1188 unsigned long long new_bw;
1189 unsigned long long max_bw;
1191 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1195 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1196 if (new_bw < agent->bw_in || new_bw > max_bw)
1198 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1199 agent->bw_out, GNUNET_NO);
1203 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1204 if (new_bw < agent->bw_out || new_bw > max_bw)
1206 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1212 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1213 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1215 * @param solver solver handle
1216 * @param agent agent handle
1217 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1221 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1223 unsigned long long new_bw;
1227 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1228 if (new_bw <= 0 || new_bw > agent->bw_in)
1230 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1235 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1236 if (new_bw <= 0 || new_bw > agent->bw_out)
1238 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1244 * Switches to the address given by its index
1246 * @param solver solver handle
1247 * @param agent agent handle
1248 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1251 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1252 struct RIL_Peer_Agent *agent,
1253 unsigned int address_index)
1255 struct RIL_Address_Wrapped *cur;
1258 //cur = agent_address_get_wrapped(agent, agent->address_inuse);
1260 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1262 if (i == address_index)
1264 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1272 //no address with address_index exists, in this case this action should not be callable
1273 GNUNET_assert(GNUNET_NO);
1277 * Puts the action into effect by calling the according function
1279 * @param solver the solver handle
1280 * @param agent the action handle
1281 * @param action the action to perform by the solver
1284 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1290 case RIL_ACTION_NOTHING:
1292 case RIL_ACTION_BW_IN_DBL:
1293 envi_action_bw_double (solver, agent, GNUNET_YES);
1295 case RIL_ACTION_BW_IN_HLV:
1296 envi_action_bw_halven (solver, agent, GNUNET_YES);
1298 case RIL_ACTION_BW_IN_INC:
1299 envi_action_bw_inc (solver, agent, GNUNET_YES);
1301 case RIL_ACTION_BW_IN_DEC:
1302 envi_action_bw_dec (solver, agent, GNUNET_YES);
1304 case RIL_ACTION_BW_OUT_DBL:
1305 envi_action_bw_double (solver, agent, GNUNET_NO);
1307 case RIL_ACTION_BW_OUT_HLV:
1308 envi_action_bw_halven (solver, agent, GNUNET_NO);
1310 case RIL_ACTION_BW_OUT_INC:
1311 envi_action_bw_inc (solver, agent, GNUNET_NO);
1313 case RIL_ACTION_BW_OUT_DEC:
1314 envi_action_bw_dec (solver, agent, GNUNET_NO);
1317 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1319 address_index = action - RIL_ACTION_TYPE_NUM;
1321 GNUNET_assert(address_index >= 0);
1323 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1325 envi_action_address_switch (solver, agent, address_index);
1328 // error - action does not exist
1329 GNUNET_assert(GNUNET_NO);
1334 * Selects the next action using the e-greedy strategy. I.e. with a probability
1335 * of (1-e) the action with the maximum expected return will be chosen
1336 * (=> exploitation) and with probability (e) a random action will be chosen.
1337 * In case the Q-learning rule is set, the function also resets the eligibility
1338 * traces in the exploration case (after Watkin's Q-learning).
1340 * @param agent the agent selecting an action
1341 * @param state the current state-feature vector
1342 * @return the action index
1345 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1348 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1349 UINT32_MAX) / (double) UINT32_MAX;
1351 if (r < agent->envi->parameters.epsilon) //explore
1353 action = agent_get_action_random(agent);
1354 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1356 agent->eligibility_reset = GNUNET_YES;
1358 agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
1363 action = agent_get_action_max(agent, state);
1369 * Selects the next action with a probability corresponding to its value. The
1370 * probability is calculated using a Boltzmann distribution with a temperature
1371 * value. The higher the temperature, the more are the action selection
1372 * probabilities the same. With a temperature of 0, the selection is greedy,
1373 * i.e. always the action with the highest value is chosen.
1379 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1383 double eqt[agent->n];
1388 a_max = agent_get_action_max(agent, state);
1390 for (i=0; i<agent->n; i++)
1392 if (agent_action_is_possible(agent, i))
1394 eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1396 eqt[i] = isinf(eqt[i]) * UINT32_MAX;
1400 for (i=0; i<agent->n; i++)
1402 if (agent_action_is_possible(agent, i))
1411 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1412 UINT32_MAX) / (double) UINT32_MAX;
1414 for (i=0; i<agent->n; i++)
1420 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1421 agent->eligibility_reset = GNUNET_YES;
1422 agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay;
1428 GNUNET_assert(GNUNET_NO);
1429 return RIL_ACTION_INVALID;
1433 * Select the next action of an agent either according to the e-greedy strategy
1434 * or the softmax strategy.
1436 * @param agent the agent in question
1437 * @param state the current state-feature vector
1438 * @return the action index
1441 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1443 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1445 return agent_select_egreedy(agent, state);
1449 return agent_select_softmax(agent, state);
1454 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1455 * after having done the last action a_old. It observes the new state s_next and the reward
1456 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1457 * next action is put into effect.
1459 * @param agent the agent performing the step
1462 agent_step (struct RIL_Peer_Agent *agent)
1464 int a_next = RIL_ACTION_INVALID;
1469 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1470 GNUNET_i2s (&agent->peer),
1471 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1473 s_next = envi_get_state (agent->envi, agent);
1474 reward = envi_get_reward (agent->envi, agent);
1476 if (agent->eligibility_reset)
1478 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1479 agent->eligibility_reset = GNUNET_NO;
1483 agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
1485 if (RIL_ACTION_INVALID != agent->a_old)
1487 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1490 switch (agent->envi->parameters.algorithm)
1492 case RIL_ALGO_SARSA:
1493 a_next = agent_select_action (agent, s_next);
1494 if (RIL_ACTION_INVALID != agent->a_old)
1496 //updates weights with selected action (on-policy), if not first step
1497 agent_update (agent, reward, s_next, a_next);
1502 a_max = agent_get_action_max (agent, s_next);
1503 if (RIL_ACTION_INVALID != agent->a_old)
1505 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1506 agent_update (agent, reward, s_next, a_max);
1508 a_next = agent_select_action (agent, s_next);
1512 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1514 LOG (GNUNET_ERROR_TYPE_DEBUG, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1521 envi_do_action (agent->envi, agent, a_next);
1523 GNUNET_free(agent->s_old);
1524 agent->s_old = s_next;
1525 agent->a_old = a_next;
1527 agent->step_count += 1;
1531 * Prototype of the ril_step() procedure
1533 * @param solver the solver handle
1536 ril_step (struct GAS_RIL_Handle *solver);
1540 * Task for the scheduler, which performs one step and lets the solver know that
1541 * no further step is scheduled.
1543 * @param cls the solver handle
1544 * @param tc the task context for the scheduler
1547 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1549 struct GAS_RIL_Handle *solver = cls;
1551 solver->step_next_task_id = NULL;
1556 * Determines how much of the available bandwidth is assigned. If more is
1557 * assigned than available it returns 1. The function is used to determine the
1558 * step size of the adaptive stepping.
1560 * @param solver the solver handle
1564 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1567 struct RIL_Scope net;
1568 unsigned long long sum_assigned = 0;
1569 unsigned long long sum_available = 0;
1572 for (i = 0; i < solver->networks_count; i++)
1574 net = solver->network_entries[i];
1575 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1577 sum_assigned += net.bw_in_utilized;
1578 sum_assigned += net.bw_out_utilized;
1579 sum_available += net.bw_in_available;
1580 sum_available += net.bw_out_available;
1583 if (sum_available > 0)
1585 ratio = ((double) sum_assigned) / ((double) sum_available);
1592 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1596 * Lookup network struct by type
1598 * @param s the solver handle
1599 * @param type the network type
1600 * @return the network struct
1602 static struct RIL_Scope *
1603 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1607 for (i = 0; i < s->networks_count; i++)
1609 if (s->network_entries[i].type == type)
1611 return &s->network_entries[i];
1618 * Determines whether more connections are allocated in a network scope, than
1619 * they would theoretically fit. This is used as a heuristic to determine,
1620 * whether a new connection can be allocated or not.
1622 * @param solver the solver handle
1623 * @param network the network scope in question
1624 * @return GNUNET_YES if there are theoretically enough resources left
1627 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1629 struct RIL_Scope *net;
1630 struct RIL_Peer_Agent *agent;
1631 unsigned long long address_count = 0;
1633 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1635 if (agent->address_inuse && agent->is_active)
1637 net = agent->address_inuse->solver_information;
1638 if (net->type == network)
1645 net = ril_get_network (solver, network);
1646 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1650 * Unblocks an agent for which a connection request is there, that could not
1651 * be satisfied. Iterates over the addresses of the agent, if one of its
1652 * addresses can now be allocated in its scope the agent is unblocked,
1653 * otherwise it remains unchanged.
1655 * @param solver the solver handle
1656 * @param agent the agent in question
1660 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1662 struct RIL_Address_Wrapped *addr_wrap;
1663 struct RIL_Scope *net;
1664 unsigned long long start_in;
1665 unsigned long long start_out;
1667 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1669 net = addr_wrap->address_naked->solver_information;
1670 if (ril_network_is_not_full(solver, net->type))
1672 if (NULL == agent->address_inuse)
1674 start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
1675 start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
1676 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, start_in, start_out, silent);
1681 agent->address_inuse = NULL;
1685 * Determines how much the reward needs to be discounted depending on the amount
1686 * of time, which has passed since the last time-step.
1688 * @param solver the solver handle
1691 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1693 struct GNUNET_TIME_Absolute time_now;
1694 struct GNUNET_TIME_Relative time_delta;
1697 // MDP case only for debugging purposes
1698 if (solver->simulate)
1700 solver->global_discount_variable = solver->parameters.gamma;
1701 solver->global_discount_integrated = 1;
1707 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1708 time_now = GNUNET_TIME_absolute_get ();
1709 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1710 solver->step_time_last = time_now;
1711 tau = (double) time_delta.rel_value_us
1712 / (double) solver->parameters.step_time_min.rel_value_us;
1714 //calculate reward discounts (once per step for all agents)
1715 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1716 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1717 / (double) solver->parameters.beta;
1721 * Count the number of active agents/connections in a network scope
1723 * @param solver the solver handle
1724 * @param scope the network scope in question
1725 * @return the number of allocated connections
1728 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1731 struct RIL_Peer_Agent *cur_agent;
1733 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1735 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1744 * Calculates how much bandwidth is assigned in sum in a network scope, either
1745 * in the inbound or in the outbound direction.
1747 * @param solver the solver handle
1748 * @param type the type of the network scope in question
1749 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1750 * otherwise the outbound direction will be summed up
1751 * @return the sum of the assigned bandwidths
1753 static unsigned long long
1754 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1756 struct RIL_Peer_Agent *cur;
1757 struct RIL_Scope *net;
1758 unsigned long long sum = 0;
1760 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1762 if (cur->is_active && cur->address_inuse)
1764 net = cur->address_inuse->solver_information;
1765 if (net->type == type)
1779 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1780 * either in the inbound or in the outbound direction.
1782 * @param solver the solver handle
1783 * @param type the type of the network scope in question
1784 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1785 * otherwise the outbound direction will be summed up
1786 * @return the sum of the utilized bandwidths (in bytes/second)
1788 static unsigned long long
1789 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1791 struct RIL_Peer_Agent *cur;
1792 struct RIL_Scope *net;
1793 unsigned long long sum = 0;
1795 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1797 if (cur->is_active && cur->address_inuse)
1799 net = cur->address_inuse->solver_information;
1800 if (net->type == type)
1803 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
1805 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
1814 * Retrieves the state of the network scope, so that its attributes are up-to-
1817 * @param solver the solver handle
1820 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1823 struct RIL_Scope *net;
1825 for (c = 0; c < solver->networks_count; c++)
1827 net = &solver->network_entries[c];
1828 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1829 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1830 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1831 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1832 net->active_agent_count = ril_network_count_active_agents(solver, net);
1833 net->social_welfare = ril_network_get_social_welfare(solver, net);
1838 * Schedules the next global step in an adaptive way. The more resources are
1839 * left, the earlier the next step is scheduled. This serves the reactivity of
1840 * the solver to changed inputs.
1842 * @param solver the solver handle
1845 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1851 struct GNUNET_TIME_Relative time_next;
1853 used_ratio = ril_get_used_resource_ratio (solver);
1856 solver->parameters.step_time_min.rel_value_us
1857 <= solver->parameters.step_time_max.rel_value_us);
1859 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1860 solver->parameters.step_time_min).rel_value_us;
1861 offset = (double) solver->parameters.step_time_min.rel_value_us;
1862 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1864 GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
1865 GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
1867 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1869 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1875 if (solver->simulate)
1877 time_next = GNUNET_TIME_UNIT_ZERO;
1880 if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1882 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1888 * Triggers one step per agent
1893 ril_step (struct GAS_RIL_Handle *solver)
1895 struct RIL_Peer_Agent *cur;
1897 if (GNUNET_YES == solver->bulk_lock)
1899 solver->bulk_changes++;
1903 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1905 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1907 if (0 == solver->step_count)
1909 solver->step_time_last = GNUNET_TIME_absolute_get ();
1912 ril_calculate_discount (solver);
1913 ril_networks_update_state (solver);
1915 //trigger one step per active, unblocked agent
1916 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1920 if (NULL == cur->address_inuse)
1922 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1924 if (cur->address_inuse)
1931 ril_networks_update_state (solver);
1933 solver->step_count++;
1934 ril_step_schedule_next (solver);
1936 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1938 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1939 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1941 if (cur->suggestion_issue) {
1942 solver->env->bandwidth_changed_cb (solver->env->cls,
1943 cur->suggestion_address);
1944 cur->suggestion_issue = GNUNET_NO;
1947 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1951 * Initializes the matrix W of parameter vectors theta with small random numbers.
1953 * @param agent The respective agent
1956 agent_w_init (struct RIL_Peer_Agent *agent)
1961 for (i = 0; i < agent->n; i++)
1963 for (k = 0; k < agent->m; k++)
1965 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));
1971 * Initialize an agent without addresses and its knowledge base
1973 * @param s ril solver
1974 * @param peer the one in question
1975 * @return handle to the new agent
1977 static struct RIL_Peer_Agent *
1978 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1981 struct GAS_RIL_Handle * solver = s;
1982 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1984 agent->envi = solver;
1985 agent->peer = *peer;
1986 agent->step_count = 0;
1987 agent->is_active = GNUNET_NO;
1988 agent->bw_in = RIL_MIN_BW;
1989 agent->bw_out = RIL_MIN_BW;
1990 agent->suggestion_issue = GNUNET_NO;
1991 agent->n = RIL_ACTION_TYPE_NUM;
1993 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1994 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1995 for (i = 0; i < agent->n; i++)
1997 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1998 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
2000 agent_w_init(agent);
2001 agent->eligibility_reset = GNUNET_NO;
2002 agent->a_old = RIL_ACTION_INVALID;
2003 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
2004 agent->address_inuse = NULL;
2005 agent->objective_old = 0;
2006 agent->nop_bonus = 0;
2014 * @param solver the solver handle
2015 * @param agent the agent to retire
2018 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
2022 for (i = 0; i < agent->n; i++)
2024 GNUNET_free_non_null(agent->W[i]);
2025 GNUNET_free_non_null(agent->E[i]);
2027 GNUNET_free_non_null(agent->W);
2028 GNUNET_free_non_null(agent->E);
2029 GNUNET_free_non_null(agent->s_old);
2034 * Returns the agent for a peer
2036 * @param solver the solver handle
2037 * @param peer the identity of the peer
2038 * @param create whether or not to create an agent, if none is allocated yet
2041 static struct RIL_Peer_Agent *
2042 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
2044 struct RIL_Peer_Agent *cur;
2046 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2048 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
2056 cur = agent_init (solver, peer);
2057 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
2064 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
2065 * considered inactive and not used. Addresses in an inactive network are ignored.
2067 * @param solver solver handle
2068 * @param network the network type
2069 * @return whether or not the network is considered active
2072 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
2074 struct RIL_Scope *net;
2076 net = ril_get_network (solver, network);
2077 return net->bw_out_available >= RIL_MIN_BW;
2081 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
2082 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
2083 * frees the memory of the old vector, and redirects the pointer to the new one.
2085 * @param old pointer to the pointer to the first element of the vector
2086 * @param element_size byte size of the vector elements
2087 * @param hole_start the first element to cut out
2088 * @param hole_length the number of elements to cut out
2089 * @param old_length the length of the old vector
2092 ril_cut_from_vector (void **old,
2093 size_t element_size,
2094 unsigned int hole_start,
2095 unsigned int hole_length,
2096 unsigned int old_length)
2099 char *oldptr = (char *) *old;
2101 unsigned int bytes_before;
2102 unsigned int bytes_hole;
2103 unsigned int bytes_after;
2105 GNUNET_assert(old_length >= hole_length);
2106 GNUNET_assert(old_length >= (hole_start + hole_length));
2108 size = element_size * (old_length - hole_length);
2110 bytes_before = element_size * hole_start;
2111 bytes_hole = element_size * hole_length;
2112 bytes_after = element_size * (old_length - hole_start - hole_length);
2120 tmpptr = GNUNET_malloc (size);
2121 memcpy (tmpptr, oldptr, bytes_before);
2122 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2128 *old = (void *) tmpptr;
2132 * Solver API functions
2133 * ---------------------------
2137 * Change relative preference for quality in solver
2139 * @param solver the solver handle
2140 * @param peer the peer to change the preference for
2141 * @param kind the kind to change the preference
2142 * @param pref_rel the normalized preference value for this kind over all clients
2145 GAS_ril_address_change_preference (void *solver,
2146 const struct GNUNET_PeerIdentity *peer,
2147 enum GNUNET_ATS_PreferenceKind kind,
2150 LOG(GNUNET_ERROR_TYPE_DEBUG,
2151 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2152 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2154 struct GAS_RIL_Handle *s = solver;
2156 s->parameters.temperature = s->parameters.temperature_init;
2157 s->parameters.epsilon = s->parameters.epsilon_init;
2163 * Add a new address for a peer to the solver
2165 * The address is already contained in the addresses hashmap!
2167 * @param solver the solver Handle
2168 * @param address the address to add
2169 * @param network network type of this address
2172 GAS_ril_address_add (void *solver,
2173 struct ATS_Address *address,
2176 struct GAS_RIL_Handle *s = solver;
2177 struct RIL_Peer_Agent *agent;
2178 struct RIL_Address_Wrapped *address_wrapped;
2179 struct RIL_Scope *net;
2187 LOG (GNUNET_ERROR_TYPE_DEBUG,
2188 "API_address_add()\n");
2190 net = ril_get_network (s, network);
2191 address->solver_information = net;
2193 if (!ril_network_is_active (s, network))
2195 LOG(GNUNET_ERROR_TYPE_DEBUG,
2196 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2197 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2201 s->parameters.temperature = s->parameters.temperature_init;
2202 s->parameters.epsilon = s->parameters.epsilon_init;
2204 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2207 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2208 address_wrapped->address_naked = address;
2209 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2211 //increase size of W
2212 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2214 n_new = agent->n + 1;
2217 GNUNET_array_grow(agent->W, agent->n, n_new);
2219 GNUNET_array_grow(agent->E, agent->n, n_new);
2220 for (i = 0; i < n_new; i++)
2225 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2227 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2232 GNUNET_array_grow(agent->W[i], zero, m_new);
2234 GNUNET_array_grow(agent->E[i], zero, m_new);
2238 //increase size of old state vector
2240 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2242 ril_try_unblock_agent(s, agent, GNUNET_NO);
2246 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2247 address->active ? "active" : "inactive", address->plugin, address->addr,
2248 GNUNET_i2s (&address->peer));
2252 * Delete an address in the solver
2254 * The address is not contained in the address hashmap anymore!
2256 * @param solver the solver handle
2257 * @param address the address to remove
2258 * @param session_only delete only session not whole address
2261 GAS_ril_address_delete (void *solver,
2262 struct ATS_Address *address,
2265 struct GAS_RIL_Handle *s = solver;
2266 struct RIL_Peer_Agent *agent;
2267 struct RIL_Address_Wrapped *address_wrapped;
2272 struct RIL_Scope *net;
2274 LOG (GNUNET_ERROR_TYPE_DEBUG,
2275 "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2276 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2278 GNUNET_i2s (&address->peer));
2280 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2283 net = address->solver_information;
2284 GNUNET_assert(! ril_network_is_active (s, net->type));
2285 LOG (GNUNET_ERROR_TYPE_DEBUG,
2286 "No agent allocated for peer yet, since address was in inactive network\n");
2290 s->parameters.temperature = s->parameters.temperature_init;
2291 s->parameters.epsilon = s->parameters.epsilon_init;
2293 address_index = agent_address_get_index (agent, address);
2294 address_wrapped = agent_address_get_wrapped (agent, address);
2296 if (NULL == address_wrapped)
2298 net = address->solver_information;
2299 LOG (GNUNET_ERROR_TYPE_DEBUG,
2300 "Address not considered by agent, address was in inactive network\n");
2303 GNUNET_CONTAINER_DLL_remove (agent->addresses_head,
2304 agent->addresses_tail,
2306 GNUNET_free (address_wrapped);
2309 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2310 n_new = agent->n - 1;
2312 for (i = 0; i < agent->n; i++)
2314 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2315 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2316 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2317 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2318 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2319 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2321 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2322 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2323 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2325 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2327 //correct last action
2328 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2332 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2334 agent->a_old = RIL_ACTION_INVALID;
2336 //decrease old state vector
2337 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2338 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2339 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2343 if (agent->address_inuse == address)
2345 if (NULL != agent->addresses_head) //if peer has an address left, use it
2347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348 "Active address died, suggesting alternative!\n");
2349 envi_set_active_suggestion (s,
2351 agent->addresses_head->address_naked,
2358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2359 "Active address died, suggesting disconnect!\n");
2360 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2364 if (agent->suggestion_address == address)
2366 agent->suggestion_issue = GNUNET_NO;
2367 agent->suggestion_address = NULL;
2369 GNUNET_assert (agent->address_inuse != address);
2374 * Update the properties of an address in the solver
2376 * @param solver solver handle
2377 * @param address the address
2378 * @param type the ATSI type in HBO
2379 * @param abs_value the absolute value of the property
2380 * @param rel_value the normalized value
2383 GAS_ril_address_property_changed (void *solver,
2384 struct ATS_Address *address,
2389 struct GAS_RIL_Handle *s = solver;
2391 LOG(GNUNET_ERROR_TYPE_DEBUG,
2392 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2394 GNUNET_ATS_print_property_type (type),
2395 GNUNET_i2s (&address->peer),
2396 address->addr, rel_value);
2399 s->parameters.temperature = s->parameters.temperature_init;
2400 s->parameters.epsilon = s->parameters.epsilon_init;
2406 * Give feedback about the current assignment
2408 * @param solver the solver handle
2409 * @param application the application
2410 * @param peer the peer to change the preference for
2411 * @param scope the time interval for this feedback: [now - scope .. now]
2412 * @param kind the kind to change the preference
2413 * @param score the score
2416 GAS_ril_address_preference_feedback (void *solver,
2417 struct GNUNET_SERVER_Client *application,
2418 const struct GNUNET_PeerIdentity *peer,
2419 const struct GNUNET_TIME_Relative scope,
2420 enum GNUNET_ATS_PreferenceKind kind,
2423 LOG (GNUNET_ERROR_TYPE_DEBUG,
2424 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2425 "preference %s for %d seconds\n",
2428 GNUNET_ATS_print_preference_type (kind),
2429 scope.rel_value_us / 1000000);
2434 * Start a bulk operation
2436 * @param solver the solver
2439 GAS_ril_bulk_start (void *solver)
2441 struct GAS_RIL_Handle *s = solver;
2443 LOG (GNUNET_ERROR_TYPE_DEBUG,
2444 "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2451 * Bulk operation done
2453 * @param solver the solver handle
2456 GAS_ril_bulk_stop (void *solver)
2458 struct GAS_RIL_Handle *s = solver;
2460 LOG(GNUNET_ERROR_TYPE_DEBUG,
2461 "API_bulk_stop() lock: %d\n",
2464 if (s->bulk_lock < 1)
2471 if (0 < s->bulk_changes)
2474 s->bulk_changes = 0;
2480 * Tell solver to notify ATS if the address to use changes for a specific
2481 * peer using the bandwidth changed callback
2483 * The solver must only notify about changes for peers with pending address
2486 * @param solver the solver handle
2487 * @param peer the identity of the peer
2489 static const struct ATS_Address *
2490 GAS_ril_get_preferred_address (void *solver,
2491 const struct GNUNET_PeerIdentity *peer)
2493 struct GAS_RIL_Handle *s = solver;
2494 struct RIL_Peer_Agent *agent;
2496 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2498 agent = ril_get_agent (s, peer, GNUNET_YES);
2500 agent->is_active = GNUNET_YES;
2501 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2503 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2505 if (agent->address_inuse)
2507 LOG(GNUNET_ERROR_TYPE_DEBUG,
2508 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2509 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2513 LOG(GNUNET_ERROR_TYPE_DEBUG,
2514 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2516 s->parameters.temperature = s->parameters.temperature_init;
2517 s->parameters.epsilon = s->parameters.epsilon_init;
2519 return agent->address_inuse;
2523 * Tell solver stop notifying ATS about changes for this peers
2525 * The solver must only notify about changes for peers with pending address
2528 * @param solver the solver handle
2529 * @param peer the peer
2532 GAS_ril_stop_get_preferred_address (void *solver,
2533 const struct GNUNET_PeerIdentity *peer)
2535 struct GAS_RIL_Handle *s = solver;
2536 struct RIL_Peer_Agent *agent;
2538 LOG (GNUNET_ERROR_TYPE_DEBUG,
2539 "API_stop_get_preferred_address()");
2541 agent = ril_get_agent (s, peer, GNUNET_NO);
2548 if (GNUNET_NO == agent->is_active)
2554 s->parameters.temperature = s->parameters.temperature_init;
2555 s->parameters.epsilon = s->parameters.epsilon_init;
2557 agent->is_active = GNUNET_NO;
2559 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2564 LOG(GNUNET_ERROR_TYPE_DEBUG,
2565 "API_stop_get_preferred_address() Paused agent for peer '%s'\n",
2571 * Entry point for the plugin
2573 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2576 libgnunet_plugin_ats_ril_init (void *cls)
2578 static struct GNUNET_ATS_SolverFunctions sf;
2579 struct GNUNET_ATS_PluginEnvironment *env = cls;
2580 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2581 struct RIL_Scope * cur;
2586 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
2588 GNUNET_assert(NULL != env);
2589 GNUNET_assert(NULL != env->cfg);
2590 GNUNET_assert(NULL != env->stats);
2591 GNUNET_assert(NULL != env->bandwidth_changed_cb);
2592 GNUNET_assert(NULL != env->get_preferences);
2593 GNUNET_assert(NULL != env->get_property);
2595 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2597 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2601 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2602 &solver->parameters.step_time_min))
2604 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2608 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2609 &solver->parameters.step_time_max))
2611 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2614 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2616 GNUNET_STRINGS_utf8_toupper (string, string);
2617 if (0 == strcmp (string, "SARSA"))
2619 solver->parameters.algorithm = RIL_ALGO_SARSA;
2621 if (0 == strcmp (string, "Q-LEARNING"))
2623 solver->parameters.algorithm = RIL_ALGO_Q;
2626 GNUNET_free (string);
2630 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2633 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2635 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2636 GNUNET_free (string);
2640 solver->parameters.select = RIL_DEFAULT_SELECT;
2644 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2645 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2646 "RIL_DISCOUNT_BETA", &f_tmp))
2650 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2651 "RIL_DISCOUNT_BETA", f_tmp);
2655 solver->parameters.beta = f_tmp;
2656 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2657 "RIL_DISCOUNT_BETA", f_tmp);
2661 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2662 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2663 "RIL_DISCOUNT_GAMMA", &f_tmp))
2665 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2667 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2668 "RIL_DISCOUNT_GAMMA", f_tmp);
2672 solver->parameters.gamma = f_tmp;
2673 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2674 "RIL_DISCOUNT_GAMMA", f_tmp);
2678 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2679 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2680 "RIL_GRADIENT_STEP_SIZE", &f_tmp))
2682 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2684 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2685 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2689 solver->parameters.alpha = f_tmp;
2690 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2691 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2695 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2696 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2697 "RIL_TRACE_DECAY", &f_tmp))
2699 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2701 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2702 "RIL_TRACE_DECAY", f_tmp);
2706 solver->parameters.lambda = f_tmp;
2707 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2708 "RIL_TRACE_DECAY", f_tmp);
2712 solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
2713 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2714 "RIL_EXPLORE_RATIO", &f_tmp))
2716 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2718 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2719 "RIL_EXPLORE_RATIO", f_tmp);
2723 solver->parameters.epsilon_init = f_tmp;
2724 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2725 "RIL_EXPLORE_RATIO", f_tmp);
2729 solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
2730 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2731 "RIL_EXPLORE_DECAY", &f_tmp))
2733 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2735 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2736 "RIL_EXPLORE_DECAY", f_tmp);
2740 solver->parameters.epsilon_decay = f_tmp;
2741 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2742 "RIL_EXPLORE_DECAY", f_tmp);
2746 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2747 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2748 "RIL_TEMPERATURE", &f_tmp))
2752 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2753 "RIL_TEMPERATURE", f_tmp);
2757 solver->parameters.temperature_init = f_tmp;
2758 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2759 "RIL_TEMPERATURE", f_tmp);
2763 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
2764 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2765 "RIL_TEMPERATURE_DECAY", &f_tmp))
2767 if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1)
2769 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2770 "RIL_TEMPERATURE_DECAY", f_tmp);
2774 solver->parameters.temperature_decay = f_tmp;
2775 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2776 "RIL_TEMPERATURE_DECAY", f_tmp);
2780 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2782 solver->simulate = GNUNET_NO;
2785 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2787 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2791 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2794 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2796 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2797 GNUNET_free (string);
2801 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2806 sf.s_add = &GAS_ril_address_add;
2807 sf.s_address_update_property = &GAS_ril_address_property_changed;
2808 sf.s_get = &GAS_ril_get_preferred_address;
2809 sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2810 sf.s_pref = &GAS_ril_address_change_preference;
2811 sf.s_feedback = &GAS_ril_address_preference_feedback;
2812 sf.s_del = &GAS_ril_address_delete;
2813 sf.s_bulk_start = &GAS_ril_bulk_start;
2814 sf.s_bulk_stop = &GAS_ril_bulk_stop;
2816 solver->networks_count = env->network_count;
2817 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2818 solver->step_count = 0;
2819 solver->done = GNUNET_NO;
2821 for (c = 0; c < env->network_count; c++)
2823 cur = &solver->network_entries[c];
2825 cur->bw_in_available = env->in_quota[c];
2826 cur->bw_out_available = env->out_quota[c];
2827 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Quotas for %s network: IN %llu - OUT %llu\n", GNUNET_ATS_print_network_type(cur->type), cur->bw_in_available/1024, cur->bw_out_available/1024);
2830 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2831 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2832 solver->parameters.algorithm ? "Q" : "SARSA",
2833 solver->parameters.alpha,
2834 solver->parameters.beta,
2835 solver->parameters.lambda);
2836 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
2837 solver->parameters.epsilon,
2838 solver->parameters.temperature,
2839 solver->parameters.select ? "EGREEDY" : "SOFTMAX");
2840 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2841 solver->parameters.rbf_divisor);
2848 * Exit point for the plugin
2850 * @param cls the solver handle
2853 libgnunet_plugin_ats_ril_done (void *cls)
2855 struct GNUNET_ATS_SolverFunctions *sf = cls;
2856 struct GAS_RIL_Handle *s = sf->cls;
2857 struct RIL_Peer_Agent *cur_agent;
2858 struct RIL_Peer_Agent *next_agent;
2860 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2862 s->done = GNUNET_YES;
2864 cur_agent = s->agents_head;
2865 while (NULL != cur_agent)
2867 next_agent = cur_agent->next;
2868 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2869 agent_die (s, cur_agent);
2870 cur_agent = next_agent;
2873 if (NULL != s->step_next_task_id)
2875 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2877 GNUNET_free(s->network_entries);
2884 /* end of plugin_ats_ril.c */