2 This file is part of GNUnet.
3 (C) 2011-2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/plugin_ats_ril.c
23 * @brief ATS reinforcement learning solver
24 * @author Fabian Oehlmann
25 * @author Matthias Wachs
30 #include "gnunet_ats_plugin.h"
31 #include "gnunet-service-ats_addresses.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
37 #define RIL_MIN_BW (5 * ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__))
38 #define RIL_MAX_BW GNUNET_ATS_MaxBandwidth
40 #define RIL_ACTION_INVALID -1
41 #define RIL_INTERVAL_EXPONENT 10
42 #define RIL_UTILITY_DELAY_MAX 1000
44 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200)
45 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 2000)
46 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
47 #define RIL_DEFAULT_SELECT RIL_SELECT_SOFTMAX
48 #define RIL_DEFAULT_WELFARE RIL_WELFARE_NASH
49 #define RIL_DEFAULT_DISCOUNT_BETA 0.6
50 #define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
51 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.01
52 #define RIL_DEFAULT_TRACE_DECAY 0.5
53 #define RIL_DEFAULT_EXPLORE_RATIO 1
54 #define RIL_DEFAULT_EXPLORE_DECAY 0.95
55 #define RIL_DEFAULT_RBF_DIVISOR 50
56 #define RIL_DEFAULT_TEMPERATURE 0.1
57 #define RIL_DEFAULT_TEMPERATURE_DECAY 1
59 #define RIL_INC_DEC_STEP_SIZE 1
60 #define RIL_NOP_DECAY 0.5
63 * ATS reinforcement learning solver
69 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
70 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
71 * to a particular address. The action of switching to address with index i is depicted by the
72 * number (RIL_ACTION_TYPE_NUM + i).
76 RIL_ACTION_NOTHING = 0,
77 RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions
78 RIL_ACTION_BW_IN_HLV = -3,
79 RIL_ACTION_BW_IN_INC = 1,
80 RIL_ACTION_BW_IN_DEC = 2,
81 RIL_ACTION_BW_OUT_DBL = -4,
82 RIL_ACTION_BW_OUT_HLV = -5,
83 RIL_ACTION_BW_OUT_INC = 3,
84 RIL_ACTION_BW_OUT_DEC = 4,
85 RIL_ACTION_TYPE_NUM = 5
96 RIL_SELECT_SOFTMAX = 0,
97 RIL_SELECT_EGREEDY = 1
103 RIL_WELFARE_EGALITARIAN
106 enum RIL_E_Modification
115 * Global learning parameters
117 struct RIL_Learning_Parameters
120 * The TD-algorithm to use
122 enum RIL_Algorithm algorithm;
125 * Gradient-descent step-size
130 * Learning discount variable in the TD-update for semi-MDPs
135 * Learning discount factor in the TD-update for MDPs
140 * Trace-decay factor for eligibility traces
145 * Whether to accumulate or replace eligibility traces
147 enum RIL_E_Modification eligibility_trace_mode;
150 * Initial softmax action-selection temperature
152 double temperature_init;
155 * Softmax action-selection temperature
160 * Decay factor of the temperature value
162 double temperature_decay;
165 * Which measure of social welfare should be used
167 enum RIL_Welfare social_welfare;
170 * State space divisor
172 unsigned long long rbf_divisor;
175 * Action selection strategy;
177 enum RIL_Select select;
180 * Initial exploration ratio value
185 * Ratio, with what probability an agent should explore in the e-greed policy
190 * Decay factor of the explore ratio
192 double epsilon_decay;
195 * Minimal interval time between steps in milliseconds
197 struct GNUNET_TIME_Relative step_time_min;
200 * Maximum interval time between steps in milliseconds
202 struct GNUNET_TIME_Relative step_time_max;
206 * Wrapper for addresses to store them in agent's linked list
208 struct RIL_Address_Wrapped
213 struct RIL_Address_Wrapped *next;
218 struct RIL_Address_Wrapped *prev;
223 struct ATS_Address *address_naked;
226 struct RIL_Peer_Agent
229 * Next agent in solver's linked list
231 struct RIL_Peer_Agent *next;
234 * Previous agent in solver's linked list
236 struct RIL_Peer_Agent *prev;
241 struct GAS_RIL_Handle *envi;
246 struct GNUNET_PeerIdentity peer;
249 * Whether the agent is active or not
254 * Number of performed time-steps
256 unsigned long long step_count;
259 * Experience matrix W
264 * Number of rows of W / Number of state-vector features
269 * Number of columns of W / Number of actions
274 * Last perceived state feature vector
289 * Whether to reset the eligibility traces to 0 after a Q-exploration step
291 int eligibility_reset;
296 struct ATS_Address * address_inuse;
299 * Head of addresses DLL
301 struct RIL_Address_Wrapped * addresses_head;
304 * Tail of addresses DLL
306 struct RIL_Address_Wrapped * addresses_tail;
309 * Inbound bandwidth assigned by the agent
314 * Outbound bandwidth assigned by the agent
319 * Flag whether a suggestion has to be issued
321 int suggestion_issue;
324 * The address which has to be issued
326 struct ATS_Address * suggestion_address;
329 * The agent's last objective value
331 double objective_old;
344 enum GNUNET_ATS_Network_Type type;
347 * Total available inbound bandwidth
349 uint32_t bw_in_available;
352 * Bandwidth inbound assigned in network after last step
354 uint32_t bw_in_assigned;
357 * Bandwidth inbound actually utilized in the network
359 uint32_t bw_in_utilized;
362 * Total available outbound bandwidth
364 uint32_t bw_out_available;
367 * Bandwidth outbound assigned in network after last step
369 unsigned long long bw_out_assigned;
372 * Bandwidth outbound actually utilized in the network
374 unsigned long long bw_out_utilized;
377 * Number of active agents in scope
379 unsigned int active_agent_count;
382 * The social welfare achieved in the scope
384 double social_welfare;
388 * A handle for the reinforcement learning solver
390 struct GAS_RIL_Handle
393 * The solver-plugin environment of the solver-plugin API
395 struct GNUNET_ATS_PluginEnvironment *env;
398 * Number of performed steps
400 unsigned long long step_count;
403 * Timestamp for the last time-step
405 struct GNUNET_TIME_Absolute step_time_last;
408 * Task identifier of the next time-step to be executed
410 struct GNUNET_SCHEDULER_Task * step_next_task_id;
413 * Variable discount factor, dependent on time between steps
415 double global_discount_variable;
418 * Integrated variable discount factor, dependent on time between steps
420 double global_discount_integrated;
423 * Lock for bulk operations
428 * Number of changes during a lock
433 * Learning parameters
435 struct RIL_Learning_Parameters parameters;
438 * Array of networks with global assignment state
440 struct RIL_Scope * network_entries;
445 unsigned int networks_count;
448 * List of active peer-agents
450 struct RIL_Peer_Agent * agents_head;
451 struct RIL_Peer_Agent * agents_tail;
459 * Simulate steps, i.e. schedule steps immediately
461 unsigned long long simulate;
465 * "Private" functions
466 * ---------------------------
470 * Estimate the current action-value for state s and action a
472 * @param agent agent performing the estimation
475 * @return estimation value
478 agent_q (struct RIL_Peer_Agent *agent, double *state, int action)
483 for (i = 0; i < agent->m; i++)
485 result += state[i] * agent->W[action][i];
488 GNUNET_assert(!isnan(result));
490 //prevent crash when learning diverges
493 return isinf(result) * UINT32_MAX;
500 * Get the index of the address in the agent's list.
502 * @param agent agent handle
503 * @param address address handle
504 * @return the index, starting with zero
507 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
510 struct RIL_Address_Wrapped *cur;
513 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
516 if (cur->address_naked == address)
524 * Gets the wrapped address from the agent's list
526 * @param agent agent handle
527 * @param address address handle
528 * @return wrapped address
530 static struct RIL_Address_Wrapped *
531 agent_address_get_wrapped (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
533 struct RIL_Address_Wrapped *cur;
535 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
536 if (cur->address_naked == address)
543 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
549 case RIL_ACTION_NOTHING:
552 case RIL_ACTION_BW_IN_INC:
553 case RIL_ACTION_BW_IN_DBL:
554 if (agent->bw_in >= RIL_MAX_BW)
559 case RIL_ACTION_BW_IN_DEC:
560 case RIL_ACTION_BW_IN_HLV:
561 if (agent->bw_in <= 0)
566 case RIL_ACTION_BW_OUT_INC:
567 case RIL_ACTION_BW_OUT_DBL:
568 if (agent->bw_out >= RIL_MAX_BW)
573 case RIL_ACTION_BW_OUT_DEC:
574 case RIL_ACTION_BW_OUT_HLV:
575 if (agent->bw_out <= 0)
581 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
583 address_index = action - RIL_ACTION_TYPE_NUM;
585 GNUNET_assert(address_index >= 0);
587 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
589 if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
590 agent->address_inuse->active)
596 // error - action does not exist
597 GNUNET_assert(GNUNET_NO);
603 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
604 * most reward in the future)
606 * @param agent agent performing the calculation
607 * @param state the state from which to take the action
608 * @return the action promising most future reward
611 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
614 int max_i = RIL_ACTION_INVALID;
616 double max_q = -DBL_MAX;
618 for (i = 0; i < agent->n; i++)
620 if (agent_action_is_possible(agent, i))
622 cur_q = agent_q (agent, state, i);
631 GNUNET_assert(RIL_ACTION_INVALID != max_i);
637 * Chooses a random action from the set of possible ones
639 * @param agent the agent performing the action
640 * @return the action index
643 agent_get_action_random (struct RIL_Peer_Agent *agent)
646 int is_possible[agent->n];
650 for (i = 0; i<agent->n; i++)
652 if (agent_action_is_possible(agent, i))
654 is_possible[i] = GNUNET_YES;
659 is_possible[i] = GNUNET_NO;
663 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
666 for (i = 0; i<agent->n; i++)
676 GNUNET_assert(GNUNET_NO);
677 return RIL_ACTION_INVALID;
682 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
684 * @param agent the agent performing the update
685 * @param reward the reward received for the last action
686 * @param s_next the new state, the last step got the agent into
687 * @param a_prime the new
690 agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
695 double **theta = agent->W;
697 delta = agent->envi->global_discount_integrated * reward; //reward
698 delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
699 delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
701 // 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",
702 // agent->step_count,
703 // agent_q (agent, agent->s_old, agent->a_old),
704 // agent->envi->parameters.alpha,
706 // agent->envi->global_discount_variable,
707 // agent_q (agent, s_next, a_prime),
710 for (k = 0; k < agent->n; k++)
712 for (i = 0; i < agent->m; i++)
714 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
715 // agent->envi->parameters.alpha,
719 theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
726 * Changes the eligibility trace vector e in various manners:
727 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
728 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
729 * #RIL_E_DECAY - multiplies e with discount factor and lambda as in the update rule
730 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
732 * @param agent the agent handle
733 * @param mod the kind of modification
734 * @param feature the feature vector
735 * @param action the action to take
738 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
739 enum RIL_E_Modification mod,
746 for (i = 0; i < agent->m; i++)
750 case RIL_E_ACCUMULATE:
751 agent->E[action][i] += feature[i];
754 agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
757 for (k = 0; k < agent->n; k++)
759 agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
763 for (k = 0; k < agent->n; k++)
773 * Informs the environment about the status of the solver
780 ril_inform (struct GAS_RIL_Handle *solver,
781 enum GAS_Solver_Operation op,
782 enum GAS_Solver_Status stat)
784 if (NULL != solver->env->info_cb)
785 solver->env->info_cb (solver->env->cls, op, stat, GAS_INFO_NONE);
789 * Calculates the maximum bandwidth an agent can assign in a network scope
793 static unsigned long long
794 ril_get_max_bw (struct RIL_Scope *net)
796 return GNUNET_MIN(2 * GNUNET_MAX(net->bw_in_available, net->bw_out_available), GNUNET_ATS_MaxBandwidth);
800 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
801 * notify ATS of its new decision
803 * @param solver solver handle
804 * @param agent agent handle
805 * @param new_address the address which is to be used
806 * @param new_bw_in the new amount of inbound bandwidth set for this address
807 * @param new_bw_out the new amount of outbound bandwidth set for this address
808 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
811 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
812 struct RIL_Peer_Agent *agent,
813 struct ATS_Address *new_address,
814 unsigned long long new_bw_in,
815 unsigned long long new_bw_out,
818 int notify = GNUNET_NO;
820 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
823 if (agent->address_inuse != new_address)
825 if (NULL != agent->address_inuse)
827 agent->address_inuse->active = GNUNET_NO;
828 agent->address_inuse->assigned_bw_in = 0;
829 agent->address_inuse->assigned_bw_out = 0;
831 if (NULL != new_address)
833 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
834 new_address->active = agent->is_active;
835 new_address->assigned_bw_in = agent->bw_in;
836 new_address->assigned_bw_out = agent->bw_out;
838 notify |= GNUNET_YES;
844 if (new_address->active != agent->is_active)
846 new_address->active = agent->is_active;
847 notify |= GNUNET_YES;
851 if (agent->bw_in != new_bw_in)
853 agent->bw_in = new_bw_in;
854 new_address->assigned_bw_in = new_bw_in;
855 notify |= GNUNET_YES;
857 if (agent->bw_out != new_bw_out)
859 agent->bw_out = new_bw_out;
860 new_address->assigned_bw_out = new_bw_out;
861 notify |= GNUNET_YES;
865 if (notify && agent->is_active && (GNUNET_NO == silent))
869 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
870 agent->suggestion_issue = GNUNET_YES;
871 agent->suggestion_address = new_address;
873 else if (agent->address_inuse)
875 /* disconnect case, no new address */
876 GNUNET_assert(0 == agent->address_inuse->assigned_bw_in);
877 GNUNET_assert(0 == agent->address_inuse->assigned_bw_out);
881 agent->suggestion_issue = GNUNET_YES;
882 agent->suggestion_address = agent->address_inuse;
885 agent->address_inuse = new_address;
890 * Allocates a state vector and fills it with the features present
891 * @param solver the solver handle
892 * @param agent the agent handle
893 * @return pointer to the state vector
896 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
907 unsigned long long max_bw;
909 state = GNUNET_malloc (sizeof(double) * agent->m);
911 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
913 y[0] = (double) agent->bw_out;
914 y[1] = (double) agent->bw_in;
916 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
917 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
919 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
921 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
922 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
925 sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
926 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
935 * Retrieves an ATS information value of an address
937 * @param address the address in question
938 * @param type the ATS information type
942 ril_get_atsi (struct ATS_Address *address, uint32_t type)
945 GNUNET_assert(NULL != address);
947 if ((NULL == address->atsi) || (0 == address->atsi_count))
948 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
950 for (c1 = 0; c1 < address->atsi_count; c1++)
952 if (ntohl (address->atsi[c1].type) == type)
953 return ntohl (address->atsi[c1].value);
955 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
959 * Returns the utility value of the connection an agent manages
961 * @param agent the agent in question
962 * @return the utility value
965 agent_get_utility (struct RIL_Peer_Agent *agent)
967 const double *preferences;
972 preferences = agent->envi->env->get_preferences (agent->envi->env->cls,
975 delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
976 delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
978 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
979 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
980 sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
981 // 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));
983 // return (double) (agent->bw_in/RIL_MIN_BW);
984 // return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
989 * Calculates the social welfare within a network scope according to what social
990 * welfare measure is set in the configuration.
992 * @param solver the solver handle
993 * @param scope the network scope in question
994 * @return the social welfare value
997 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
999 struct RIL_Peer_Agent *cur;
1002 switch (solver->parameters.social_welfare)
1004 case RIL_WELFARE_EGALITARIAN:
1006 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1008 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1010 result = GNUNET_MIN(result, agent_get_utility(cur));
1015 case RIL_WELFARE_NASH:
1017 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1019 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1021 result *= pow(agent_get_utility(cur), 1.0 / (double) scope->active_agent_count);
1026 GNUNET_assert(GNUNET_NO);
1031 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1033 struct RIL_Scope *net;
1034 unsigned long long over_max;
1035 unsigned long long over_in = 0;
1036 unsigned long long over_out = 0;
1038 net = agent->address_inuse->solver_information;
1040 if (net->bw_in_utilized > net->bw_in_available)
1042 over_in = net->bw_in_utilized - net->bw_in_available;
1043 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1045 /* increase quadratically */
1049 if (net->bw_out_utilized > net->bw_out_available)
1051 over_out = net->bw_out_utilized - net->bw_out_available;
1052 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1054 /* increase quadratically */
1055 over_out *= over_out;
1058 over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
1060 return -1.0 * (double) over_max;
1064 * Gets the reward for the last performed step, which is calculated in equal
1065 * parts from the local (the peer specific) and the global (for all peers
1066 * identical) reward.
1068 * @param solver the solver handle
1069 * @param agent the agent handle
1070 * @return the reward
1073 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1075 struct RIL_Scope *net;
1082 net = agent->address_inuse->solver_information;
1084 penalty = envi_get_penalty(solver, agent);
1085 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1086 delta = objective - agent->objective_old;
1087 agent->objective_old = objective;
1089 if (delta != 0 && penalty == 0)
1091 agent->nop_bonus = delta * RIL_NOP_DECAY;
1095 agent->nop_bonus *= RIL_NOP_DECAY;
1098 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1100 reward = delta + steady;
1101 return reward + penalty;
1105 * Doubles the bandwidth for the active address
1107 * @param solver solver handle
1108 * @param agent agent handle
1109 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1112 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1113 struct RIL_Peer_Agent *agent,
1116 unsigned long long new_bw;
1117 unsigned long long max_bw;
1119 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1123 new_bw = agent->bw_in * 2;
1124 if (new_bw < agent->bw_in || new_bw > max_bw)
1126 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1127 agent->bw_out, GNUNET_NO);
1131 new_bw = agent->bw_out * 2;
1132 if (new_bw < agent->bw_out || new_bw > max_bw)
1134 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1140 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1141 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1143 * @param solver solver handle
1144 * @param agent agent handle
1145 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1149 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1150 struct RIL_Peer_Agent *agent,
1153 unsigned long long new_bw;
1157 new_bw = agent->bw_in / 2;
1158 if (new_bw <= 0 || new_bw > agent->bw_in)
1160 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1165 new_bw = agent->bw_out / 2;
1166 if (new_bw <= 0 || new_bw > agent->bw_out)
1168 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1174 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1176 * @param solver solver handle
1177 * @param agent agent handle
1178 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1182 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1184 unsigned long long new_bw;
1185 unsigned long long max_bw;
1187 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1191 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1192 if (new_bw < agent->bw_in || new_bw > max_bw)
1194 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1195 agent->bw_out, GNUNET_NO);
1199 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1200 if (new_bw < agent->bw_out || new_bw > max_bw)
1202 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1208 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1209 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1211 * @param solver solver handle
1212 * @param agent agent handle
1213 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1217 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1219 unsigned long long new_bw;
1223 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1224 if (new_bw <= 0 || new_bw > agent->bw_in)
1226 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1231 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1232 if (new_bw <= 0 || new_bw > agent->bw_out)
1234 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1240 * Switches to the address given by its index
1242 * @param solver solver handle
1243 * @param agent agent handle
1244 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1247 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1248 struct RIL_Peer_Agent *agent,
1249 unsigned int address_index)
1251 struct RIL_Address_Wrapped *cur;
1254 //cur = agent_address_get_wrapped(agent, agent->address_inuse);
1256 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1258 if (i == address_index)
1260 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1268 //no address with address_index exists, in this case this action should not be callable
1269 GNUNET_assert(GNUNET_NO);
1273 * Puts the action into effect by calling the according function
1275 * @param solver the solver handle
1276 * @param agent the action handle
1277 * @param action the action to perform by the solver
1280 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1286 case RIL_ACTION_NOTHING:
1288 case RIL_ACTION_BW_IN_DBL:
1289 envi_action_bw_double (solver, agent, GNUNET_YES);
1291 case RIL_ACTION_BW_IN_HLV:
1292 envi_action_bw_halven (solver, agent, GNUNET_YES);
1294 case RIL_ACTION_BW_IN_INC:
1295 envi_action_bw_inc (solver, agent, GNUNET_YES);
1297 case RIL_ACTION_BW_IN_DEC:
1298 envi_action_bw_dec (solver, agent, GNUNET_YES);
1300 case RIL_ACTION_BW_OUT_DBL:
1301 envi_action_bw_double (solver, agent, GNUNET_NO);
1303 case RIL_ACTION_BW_OUT_HLV:
1304 envi_action_bw_halven (solver, agent, GNUNET_NO);
1306 case RIL_ACTION_BW_OUT_INC:
1307 envi_action_bw_inc (solver, agent, GNUNET_NO);
1309 case RIL_ACTION_BW_OUT_DEC:
1310 envi_action_bw_dec (solver, agent, GNUNET_NO);
1313 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1315 address_index = action - RIL_ACTION_TYPE_NUM;
1317 GNUNET_assert(address_index >= 0);
1319 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1321 envi_action_address_switch (solver, agent, address_index);
1324 // error - action does not exist
1325 GNUNET_assert(GNUNET_NO);
1330 * Selects the next action using the e-greedy strategy. I.e. with a probability
1331 * of (1-e) the action with the maximum expected return will be chosen
1332 * (=> exploitation) and with probability (e) a random action will be chosen.
1333 * In case the Q-learning rule is set, the function also resets the eligibility
1334 * traces in the exploration case (after Watkin's Q-learning).
1336 * @param agent the agent selecting an action
1337 * @param state the current state-feature vector
1338 * @return the action index
1341 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1344 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1345 UINT32_MAX) / (double) UINT32_MAX;
1347 if (r < agent->envi->parameters.epsilon) //explore
1349 action = agent_get_action_random(agent);
1350 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1352 agent->eligibility_reset = GNUNET_YES;
1354 agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
1359 action = agent_get_action_max(agent, state);
1365 * Selects the next action with a probability corresponding to its value. The
1366 * probability is calculated using a Boltzmann distribution with a temperature
1367 * value. The higher the temperature, the more are the action selection
1368 * probabilities the same. With a temperature of 0, the selection is greedy,
1369 * i.e. always the action with the highest value is chosen.
1375 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1379 double eqt[agent->n];
1384 a_max = agent_get_action_max(agent, state);
1386 for (i=0; i<agent->n; i++)
1388 if (agent_action_is_possible(agent, i))
1390 eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1394 for (i=0; i<agent->n; i++)
1396 if (agent_action_is_possible(agent, i))
1405 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1406 UINT32_MAX) / (double) UINT32_MAX;
1408 for (i=0; i<agent->n; i++)
1414 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1415 agent->eligibility_reset = GNUNET_YES;
1416 agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay;
1422 GNUNET_assert(GNUNET_NO);
1423 return RIL_ACTION_INVALID;
1427 * Select the next action of an agent either according to the e-greedy strategy
1428 * or the softmax strategy.
1430 * @param agent the agent in question
1431 * @param state the current state-feature vector
1432 * @return the action index
1435 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1437 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1439 return agent_select_egreedy(agent, state);
1443 return agent_select_softmax(agent, state);
1448 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1449 * after having done the last action a_old. It observes the new state s_next and the reward
1450 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1451 * next action is put into effect.
1453 * @param agent the agent performing the step
1456 agent_step (struct RIL_Peer_Agent *agent)
1458 int a_next = RIL_ACTION_INVALID;
1463 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1464 GNUNET_i2s (&agent->peer),
1465 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1467 s_next = envi_get_state (agent->envi, agent);
1468 reward = envi_get_reward (agent->envi, agent);
1470 if (agent->eligibility_reset)
1472 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1473 agent->eligibility_reset = GNUNET_NO;
1477 agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
1479 if (RIL_ACTION_INVALID != agent->a_old)
1481 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1484 switch (agent->envi->parameters.algorithm)
1486 case RIL_ALGO_SARSA:
1487 a_next = agent_select_action (agent, s_next);
1488 if (RIL_ACTION_INVALID != agent->a_old)
1490 //updates weights with selected action (on-policy), if not first step
1491 agent_update (agent, reward, s_next, a_next);
1496 a_max = agent_get_action_max (agent, s_next);
1497 if (RIL_ACTION_INVALID != agent->a_old)
1499 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1500 agent_update (agent, reward, s_next, a_max);
1502 a_next = agent_select_action (agent, s_next);
1506 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1508 LOG (GNUNET_ERROR_TYPE_DEBUG, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1515 envi_do_action (agent->envi, agent, a_next);
1517 GNUNET_free(agent->s_old);
1518 agent->s_old = s_next;
1519 agent->a_old = a_next;
1521 agent->step_count += 1;
1525 * Prototype of the ril_step() procedure
1527 * @param solver the solver handle
1530 ril_step (struct GAS_RIL_Handle *solver);
1533 * Task for the scheduler, which performs one step and lets the solver know that
1534 * no further step is scheduled.
1536 * @param cls the solver handle
1537 * @param tc the task context for the scheduler
1540 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1542 struct GAS_RIL_Handle *solver = cls;
1544 solver->step_next_task_id = NULL;
1549 * Determines how much of the available bandwidth is assigned. If more is
1550 * assigned than available it returns 1. The function is used to determine the
1551 * step size of the adaptive stepping.
1553 * @param solver the solver handle
1557 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1560 struct RIL_Scope net;
1561 unsigned long long sum_assigned = 0;
1562 unsigned long long sum_available = 0;
1565 for (i = 0; i < solver->networks_count; i++)
1567 net = solver->network_entries[i];
1568 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1570 sum_assigned += net.bw_in_utilized;
1571 sum_assigned += net.bw_out_utilized;
1572 sum_available += net.bw_in_available;
1573 sum_available += net.bw_out_available;
1576 if (sum_available > 0)
1578 ratio = ((double) sum_assigned) / ((double) sum_available);
1585 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1589 * Lookup network struct by type
1591 * @param s the solver handle
1592 * @param type the network type
1593 * @return the network struct
1595 static struct RIL_Scope *
1596 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1600 for (i = 0; i < s->networks_count; i++)
1602 if (s->network_entries[i].type == type)
1604 return &s->network_entries[i];
1611 * Determines whether more connections are allocated in a network scope, than
1612 * they would theoretically fit. This is used as a heuristic to determine,
1613 * whether a new connection can be allocated or not.
1615 * @param solver the solver handle
1616 * @param network the network scope in question
1617 * @return GNUNET_YES if there are theoretically enough resources left
1620 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1622 struct RIL_Scope *net;
1623 struct RIL_Peer_Agent *agent;
1624 unsigned long long address_count = 0;
1626 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1628 if (agent->address_inuse && agent->is_active)
1630 net = agent->address_inuse->solver_information;
1631 if (net->type == network)
1638 net = ril_get_network (solver, network);
1639 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1643 * Unblocks an agent for which a connection request is there, that could not
1644 * be satisfied. Iterates over the addresses of the agent, if one of its
1645 * addresses can now be allocated in its scope the agent is unblocked,
1646 * otherwise it remains unchanged.
1648 * @param solver the solver handle
1649 * @param agent the agent in question
1653 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1655 struct RIL_Address_Wrapped *addr_wrap;
1656 struct RIL_Scope *net;
1657 unsigned long long start_in;
1658 unsigned long long start_out;
1660 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1662 net = addr_wrap->address_naked->solver_information;
1663 if (ril_network_is_not_full(solver, net->type))
1665 if (NULL == agent->address_inuse)
1667 start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
1668 start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
1669 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, start_in, start_out, silent);
1674 agent->address_inuse = NULL;
1678 * Determines how much the reward needs to be discounted depending on the amount
1679 * of time, which has passed since the last time-step.
1681 * @param solver the solver handle
1684 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1686 struct GNUNET_TIME_Absolute time_now;
1687 struct GNUNET_TIME_Relative time_delta;
1690 // MDP case only for debugging purposes
1691 if (solver->simulate)
1693 solver->global_discount_variable = solver->parameters.gamma;
1694 solver->global_discount_integrated = 1;
1700 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1701 time_now = GNUNET_TIME_absolute_get ();
1702 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1703 solver->step_time_last = time_now;
1704 tau = (double) time_delta.rel_value_us
1705 / (double) solver->parameters.step_time_min.rel_value_us;
1707 //calculate reward discounts (once per step for all agents)
1708 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1709 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1710 / (double) solver->parameters.beta;
1714 * Count the number of active agents/connections in a network scope
1716 * @param solver the solver handle
1717 * @param scope the network scope in question
1718 * @return the number of allocated connections
1721 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1724 struct RIL_Peer_Agent *cur_agent;
1726 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1728 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1737 * Calculates how much bandwidth is assigned in sum in a network scope, either
1738 * in the inbound or in the outbound direction.
1740 * @param solver the solver handle
1741 * @param type the type of the network scope in question
1742 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1743 * otherwise the outbound direction will be summed up
1744 * @return the sum of the assigned bandwidths
1746 static unsigned long long
1747 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1749 struct RIL_Peer_Agent *cur;
1750 struct RIL_Scope *net;
1751 unsigned long long sum = 0;
1753 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1755 if (cur->is_active && cur->address_inuse)
1757 net = cur->address_inuse->solver_information;
1758 if (net->type == type)
1772 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1773 * either in the inbound or in the outbound direction.
1775 * @param solver the solver handle
1776 * @param type the type of the network scope in question
1777 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1778 * otherwise the outbound direction will be summed up
1779 * @return the sum of the utilized bandwidths (in bytes/second)
1781 static unsigned long long
1782 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1784 struct RIL_Peer_Agent *cur;
1785 struct RIL_Scope *net;
1786 unsigned long long sum = 0;
1788 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1790 if (cur->is_active && cur->address_inuse)
1792 net = cur->address_inuse->solver_information;
1793 if (net->type == type)
1796 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
1798 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
1807 * Retrieves the state of the network scope, so that its attributes are up-to-
1810 * @param solver the solver handle
1813 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1816 struct RIL_Scope *net;
1818 for (c = 0; c < solver->networks_count; c++)
1820 net = &solver->network_entries[c];
1821 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1822 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1823 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1824 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1825 net->active_agent_count = ril_network_count_active_agents(solver, net);
1826 net->social_welfare = ril_network_get_social_welfare(solver, net);
1831 * Schedules the next global step in an adaptive way. The more resources are
1832 * left, the earlier the next step is scheduled. This serves the reactivity of
1833 * the solver to changed inputs.
1835 * @param solver the solver handle
1838 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1844 struct GNUNET_TIME_Relative time_next;
1846 used_ratio = ril_get_used_resource_ratio (solver);
1849 solver->parameters.step_time_min.rel_value_us
1850 <= solver->parameters.step_time_max.rel_value_us);
1852 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1853 solver->parameters.step_time_min).rel_value_us;
1854 offset = (double) solver->parameters.step_time_min.rel_value_us;
1855 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1857 GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
1858 GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
1860 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1862 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1868 if (solver->simulate)
1870 time_next = GNUNET_TIME_UNIT_ZERO;
1873 if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1875 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1881 * Triggers one step per agent
1886 ril_step (struct GAS_RIL_Handle *solver)
1888 struct RIL_Peer_Agent *cur;
1890 if (GNUNET_YES == solver->bulk_lock)
1892 solver->bulk_changes++;
1896 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1898 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1900 if (0 == solver->step_count)
1902 solver->step_time_last = GNUNET_TIME_absolute_get ();
1905 ril_calculate_discount (solver);
1906 ril_networks_update_state (solver);
1908 //trigger one step per active, unblocked agent
1909 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1913 if (NULL == cur->address_inuse)
1915 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1917 if (cur->address_inuse)
1924 ril_networks_update_state (solver);
1926 solver->step_count += 1;
1927 ril_step_schedule_next (solver);
1929 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1931 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1932 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1934 if (cur->suggestion_issue) {
1935 solver->env->bandwidth_changed_cb(solver->env->cls, cur->suggestion_address);
1936 cur->suggestion_issue = GNUNET_NO;
1939 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1943 * Initializes the matrix W of parameter vectors theta with small random numbers.
1945 * @param agent The respective agent
1948 agent_w_init (struct RIL_Peer_Agent *agent)
1953 for (i = 0; i < agent->n; i++)
1955 for (k = 0; k < agent->m; k++)
1957 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));
1963 * Initialize an agent without addresses and its knowledge base
1965 * @param s ril solver
1966 * @param peer the one in question
1967 * @return handle to the new agent
1969 static struct RIL_Peer_Agent *
1970 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1973 struct GAS_RIL_Handle * solver = s;
1974 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1976 agent->envi = solver;
1977 agent->peer = *peer;
1978 agent->step_count = 0;
1979 agent->is_active = GNUNET_NO;
1980 agent->bw_in = RIL_MIN_BW;
1981 agent->bw_out = RIL_MIN_BW;
1982 agent->suggestion_issue = GNUNET_NO;
1983 agent->n = RIL_ACTION_TYPE_NUM;
1985 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1986 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1987 for (i = 0; i < agent->n; i++)
1989 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1990 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1992 agent_w_init(agent);
1993 agent->eligibility_reset = GNUNET_NO;
1994 agent->a_old = RIL_ACTION_INVALID;
1995 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1996 agent->address_inuse = NULL;
1997 agent->objective_old = 0;
1998 agent->nop_bonus = 0;
2006 * @param solver the solver handle
2007 * @param agent the agent to retire
2010 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
2014 for (i = 0; i < agent->n; i++)
2016 GNUNET_free_non_null(agent->W[i]);
2017 GNUNET_free_non_null(agent->E[i]);
2019 GNUNET_free_non_null(agent->W);
2020 GNUNET_free_non_null(agent->E);
2021 GNUNET_free_non_null(agent->s_old);
2026 * Returns the agent for a peer
2028 * @param solver the solver handle
2029 * @param peer the identity of the peer
2030 * @param create whether or not to create an agent, if none is allocated yet
2033 static struct RIL_Peer_Agent *
2034 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
2036 struct RIL_Peer_Agent *cur;
2038 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2040 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
2048 cur = agent_init (solver, peer);
2049 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
2056 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
2057 * considered inactive and not used. Addresses in an inactive network are ignored.
2059 * @param solver solver handle
2060 * @param network the network type
2061 * @return whether or not the network is considered active
2064 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
2066 struct RIL_Scope *net;
2068 net = ril_get_network (solver, network);
2069 return net->bw_out_available >= RIL_MIN_BW;
2073 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
2074 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
2075 * frees the memory of the old vector, and redirects the pointer to the new one.
2077 * @param old pointer to the pointer to the first element of the vector
2078 * @param element_size byte size of the vector elements
2079 * @param hole_start the first element to cut out
2080 * @param hole_length the number of elements to cut out
2081 * @param old_length the length of the old vector
2084 ril_cut_from_vector (void **old,
2085 size_t element_size,
2086 unsigned int hole_start,
2087 unsigned int hole_length,
2088 unsigned int old_length)
2091 char *oldptr = (char *) *old;
2093 unsigned int bytes_before;
2094 unsigned int bytes_hole;
2095 unsigned int bytes_after;
2097 GNUNET_assert(old_length >= hole_length);
2098 GNUNET_assert(old_length >= (hole_start + hole_length));
2100 size = element_size * (old_length - hole_length);
2102 bytes_before = element_size * hole_start;
2103 bytes_hole = element_size * hole_length;
2104 bytes_after = element_size * (old_length - hole_start - hole_length);
2112 tmpptr = GNUNET_malloc (size);
2113 memcpy (tmpptr, oldptr, bytes_before);
2114 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2120 *old = (void *) tmpptr;
2124 * Solver API functions
2125 * ---------------------------
2129 * Change relative preference for quality in solver
2131 * @param solver the solver handle
2132 * @param peer the peer to change the preference for
2133 * @param kind the kind to change the preference
2134 * @param pref_rel the normalized preference value for this kind over all clients
2137 GAS_ril_address_change_preference (void *solver,
2138 const struct GNUNET_PeerIdentity *peer,
2139 enum GNUNET_ATS_PreferenceKind kind,
2142 LOG(GNUNET_ERROR_TYPE_DEBUG,
2143 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2144 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2146 struct GAS_RIL_Handle *s = solver;
2148 s->parameters.temperature = s->parameters.temperature_init;
2149 s->parameters.epsilon = s->parameters.epsilon_init;
2155 * Add a new address for a peer to the solver
2157 * The address is already contained in the addresses hashmap!
2159 * @param solver the solver Handle
2160 * @param address the address to add
2161 * @param network network type of this address
2164 GAS_ril_address_add (void *solver,
2165 struct ATS_Address *address,
2168 struct GAS_RIL_Handle *s = solver;
2169 struct RIL_Peer_Agent *agent;
2170 struct RIL_Address_Wrapped *address_wrapped;
2171 struct RIL_Scope *net;
2179 LOG (GNUNET_ERROR_TYPE_DEBUG,
2180 "API_address_add()\n");
2182 net = ril_get_network (s, network);
2183 address->solver_information = net;
2185 if (!ril_network_is_active (s, network))
2187 LOG(GNUNET_ERROR_TYPE_DEBUG,
2188 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2189 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2193 s->parameters.temperature = s->parameters.temperature_init;
2194 s->parameters.epsilon = s->parameters.epsilon_init;
2196 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2199 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2200 address_wrapped->address_naked = address;
2201 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2203 //increase size of W
2204 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2206 n_new = agent->n + 1;
2209 GNUNET_array_grow(agent->W, agent->n, n_new);
2211 GNUNET_array_grow(agent->E, agent->n, n_new);
2212 for (i = 0; i < n_new; i++)
2217 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2219 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2224 GNUNET_array_grow(agent->W[i], zero, m_new);
2226 GNUNET_array_grow(agent->E[i], zero, m_new);
2230 //increase size of old state vector
2232 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2234 ril_try_unblock_agent(s, agent, GNUNET_NO);
2238 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2239 address->active ? "active" : "inactive", address->plugin, address->addr,
2240 GNUNET_i2s (&address->peer));
2244 * Delete an address in the solver
2246 * The address is not contained in the address hashmap anymore!
2248 * @param solver the solver handle
2249 * @param address the address to remove
2250 * @param session_only delete only session not whole address
2253 GAS_ril_address_delete (void *solver,
2254 struct ATS_Address *address,
2257 struct GAS_RIL_Handle *s = solver;
2258 struct RIL_Peer_Agent *agent;
2259 struct RIL_Address_Wrapped *address_wrapped;
2260 int address_was_used = address->active;
2265 struct RIL_Scope *net;
2267 LOG (GNUNET_ERROR_TYPE_DEBUG,
2268 "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2269 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2271 GNUNET_i2s (&address->peer));
2273 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2276 net = address->solver_information;
2277 GNUNET_assert(!ril_network_is_active (s, net->type));
2278 LOG(GNUNET_ERROR_TYPE_DEBUG,
2279 "No agent allocated for peer yet, since address was in inactive network\n");
2283 s->parameters.temperature = s->parameters.temperature_init;
2284 s->parameters.epsilon = s->parameters.epsilon_init;
2286 address_index = agent_address_get_index (agent, address);
2287 address_wrapped = agent_address_get_wrapped (agent, address);
2289 if (NULL == address_wrapped)
2291 net = address->solver_information;
2292 LOG(GNUNET_ERROR_TYPE_DEBUG,
2293 "Address not considered by agent, address was in inactive network\n");
2297 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2298 GNUNET_free(address_wrapped);
2301 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2302 n_new = agent->n - 1;
2304 for (i = 0; i < agent->n; i++)
2306 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2307 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2308 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2309 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2310 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2311 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2313 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2314 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2315 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2317 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2319 //correct last action
2320 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2324 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2326 agent->a_old = RIL_ACTION_INVALID;
2328 //decrease old state vector
2329 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2330 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2331 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2335 if (address_was_used)
2337 if (NULL != agent->addresses_head) //if peer has an address left, use it
2339 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, agent->bw_in, agent->bw_out,
2344 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2352 * Update the properties of an address in the solver
2354 * @param solver solver handle
2355 * @param address the address
2356 * @param type the ATSI type in HBO
2357 * @param abs_value the absolute value of the property
2358 * @param rel_value the normalized value
2361 GAS_ril_address_property_changed (void *solver,
2362 struct ATS_Address *address,
2367 struct GAS_RIL_Handle *s = solver;
2369 LOG(GNUNET_ERROR_TYPE_DEBUG,
2370 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2372 GNUNET_ATS_print_property_type (type),
2373 GNUNET_i2s (&address->peer),
2374 address->addr, rel_value);
2377 s->parameters.temperature = s->parameters.temperature_init;
2378 s->parameters.epsilon = s->parameters.epsilon_init;
2384 * Give feedback about the current assignment
2386 * @param solver the solver handle
2387 * @param application the application
2388 * @param peer the peer to change the preference for
2389 * @param scope the time interval for this feedback: [now - scope .. now]
2390 * @param kind the kind to change the preference
2391 * @param score the score
2394 GAS_ril_address_preference_feedback (void *solver,
2395 struct GNUNET_SERVER_Client *application,
2396 const struct GNUNET_PeerIdentity *peer,
2397 const struct GNUNET_TIME_Relative scope,
2398 enum GNUNET_ATS_PreferenceKind kind,
2401 LOG (GNUNET_ERROR_TYPE_DEBUG,
2402 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2403 "preference %s for %d seconds\n",
2406 GNUNET_ATS_print_preference_type (kind),
2407 scope.rel_value_us / 1000000);
2412 * Start a bulk operation
2414 * @param solver the solver
2417 GAS_ril_bulk_start (void *solver)
2419 struct GAS_RIL_Handle *s = solver;
2421 LOG (GNUNET_ERROR_TYPE_DEBUG,
2422 "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2429 * Bulk operation done
2431 * @param solver the solver handle
2434 GAS_ril_bulk_stop (void *solver)
2436 struct GAS_RIL_Handle *s = solver;
2438 LOG(GNUNET_ERROR_TYPE_DEBUG,
2439 "API_bulk_stop() lock: %d\n",
2442 if (s->bulk_lock < 1)
2449 if (0 < s->bulk_changes)
2452 s->bulk_changes = 0;
2458 * Tell solver to notify ATS if the address to use changes for a specific
2459 * peer using the bandwidth changed callback
2461 * The solver must only notify about changes for peers with pending address
2464 * @param solver the solver handle
2465 * @param peer the identity of the peer
2467 static const struct ATS_Address *
2468 GAS_ril_get_preferred_address (void *solver,
2469 const struct GNUNET_PeerIdentity *peer)
2471 struct GAS_RIL_Handle *s = solver;
2472 struct RIL_Peer_Agent *agent;
2474 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2476 agent = ril_get_agent (s, peer, GNUNET_YES);
2478 agent->is_active = GNUNET_YES;
2479 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2481 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2483 if (agent->address_inuse)
2485 LOG(GNUNET_ERROR_TYPE_DEBUG,
2486 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2487 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2491 LOG(GNUNET_ERROR_TYPE_DEBUG,
2492 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2494 s->parameters.temperature = s->parameters.temperature_init;
2495 s->parameters.epsilon = s->parameters.epsilon_init;
2497 return agent->address_inuse;
2501 * Tell solver stop notifying ATS about changes for this peers
2503 * The solver must only notify about changes for peers with pending address
2506 * @param solver the solver handle
2507 * @param peer the peer
2510 GAS_ril_stop_get_preferred_address (void *solver,
2511 const struct GNUNET_PeerIdentity *peer)
2513 struct GAS_RIL_Handle *s = solver;
2514 struct RIL_Peer_Agent *agent;
2516 LOG (GNUNET_ERROR_TYPE_DEBUG,
2517 "API_stop_get_preferred_address()");
2519 agent = ril_get_agent (s, peer, GNUNET_NO);
2526 if (GNUNET_NO == agent->is_active)
2532 s->parameters.temperature = s->parameters.temperature_init;
2533 s->parameters.epsilon = s->parameters.epsilon_init;
2535 agent->is_active = GNUNET_NO;
2537 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2542 LOG(GNUNET_ERROR_TYPE_DEBUG,
2543 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2544 GNUNET_i2s (peer), agent->address_inuse->plugin);
2549 * Entry point for the plugin
2551 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2554 libgnunet_plugin_ats_ril_init (void *cls)
2556 static struct GNUNET_ATS_SolverFunctions sf;
2557 struct GNUNET_ATS_PluginEnvironment *env = cls;
2558 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2559 struct RIL_Scope * cur;
2564 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
2566 GNUNET_assert(NULL != env);
2567 GNUNET_assert(NULL != env->cfg);
2568 GNUNET_assert(NULL != env->stats);
2569 GNUNET_assert(NULL != env->bandwidth_changed_cb);
2570 GNUNET_assert(NULL != env->get_preferences);
2571 GNUNET_assert(NULL != env->get_property);
2573 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2575 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2579 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2580 &solver->parameters.step_time_min))
2582 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2586 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2587 &solver->parameters.step_time_max))
2589 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2592 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2594 GNUNET_STRINGS_utf8_toupper (string, string);
2595 if (0 == strcmp (string, "SARSA"))
2597 solver->parameters.algorithm = RIL_ALGO_SARSA;
2599 if (0 == strcmp (string, "Q-LEARNING"))
2601 solver->parameters.algorithm = RIL_ALGO_Q;
2604 GNUNET_free (string);
2608 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2611 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2613 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2614 GNUNET_free (string);
2618 solver->parameters.select = RIL_DEFAULT_SELECT;
2622 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2623 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2624 "RIL_DISCOUNT_BETA", &f_tmp))
2628 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2629 "RIL_DISCOUNT_BETA", f_tmp);
2633 solver->parameters.beta = f_tmp;
2634 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2635 "RIL_DISCOUNT_BETA", f_tmp);
2639 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2640 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2641 "RIL_DISCOUNT_GAMMA", &f_tmp))
2643 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2645 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2646 "RIL_DISCOUNT_GAMMA", f_tmp);
2650 solver->parameters.gamma = f_tmp;
2651 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2652 "RIL_DISCOUNT_GAMMA", f_tmp);
2656 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2657 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2658 "RIL_GRADIENT_STEP_SIZE", &f_tmp))
2660 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2662 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2663 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2667 solver->parameters.alpha = f_tmp;
2668 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2669 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2673 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2674 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2675 "RIL_TRACE_DECAY", &f_tmp))
2677 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2679 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2680 "RIL_TRACE_DECAY", f_tmp);
2684 solver->parameters.lambda = f_tmp;
2685 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2686 "RIL_TRACE_DECAY", f_tmp);
2690 solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
2691 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2692 "RIL_EXPLORE_RATIO", &f_tmp))
2694 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2696 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2697 "RIL_EXPLORE_RATIO", f_tmp);
2701 solver->parameters.epsilon_init = f_tmp;
2702 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2703 "RIL_EXPLORE_RATIO", f_tmp);
2707 solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
2708 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2709 "RIL_EXPLORE_DECAY", &f_tmp))
2711 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2713 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2714 "RIL_EXPLORE_DECAY", f_tmp);
2718 solver->parameters.epsilon_decay = f_tmp;
2719 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2720 "RIL_EXPLORE_DECAY", f_tmp);
2724 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2725 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2726 "RIL_TEMPERATURE", &f_tmp))
2730 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2731 "RIL_TEMPERATURE", f_tmp);
2735 solver->parameters.temperature_init = f_tmp;
2736 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2737 "RIL_TEMPERATURE", f_tmp);
2741 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
2742 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2743 "RIL_TEMPERATURE_DECAY", &f_tmp))
2745 if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1)
2747 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2748 "RIL_TEMPERATURE_DECAY", f_tmp);
2752 solver->parameters.temperature_decay = f_tmp;
2753 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2754 "RIL_TEMPERATURE_DECAY", f_tmp);
2758 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2760 solver->simulate = GNUNET_NO;
2763 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2765 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2769 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2772 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2774 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2775 GNUNET_free (string);
2779 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2784 sf.s_add = &GAS_ril_address_add;
2785 sf.s_address_update_property = &GAS_ril_address_property_changed;
2786 sf.s_get = &GAS_ril_get_preferred_address;
2787 sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2788 sf.s_pref = &GAS_ril_address_change_preference;
2789 sf.s_feedback = &GAS_ril_address_preference_feedback;
2790 sf.s_del = &GAS_ril_address_delete;
2791 sf.s_bulk_start = &GAS_ril_bulk_start;
2792 sf.s_bulk_stop = &GAS_ril_bulk_stop;
2794 solver->networks_count = env->network_count;
2795 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2796 solver->step_count = 0;
2797 solver->done = GNUNET_NO;
2799 for (c = 0; c < env->network_count; c++)
2801 cur = &solver->network_entries[c];
2803 cur->bw_in_available = env->in_quota[c];
2804 cur->bw_out_available = env->out_quota[c];
2805 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);
2808 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2809 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2810 solver->parameters.algorithm ? "Q" : "SARSA",
2811 solver->parameters.alpha,
2812 solver->parameters.beta,
2813 solver->parameters.lambda);
2814 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
2815 solver->parameters.epsilon,
2816 solver->parameters.temperature,
2817 solver->parameters.select ? "EGREEDY" : "SOFTMAX");
2818 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2819 solver->parameters.rbf_divisor);
2826 * Exit point for the plugin
2828 * @param cls the solver handle
2831 libgnunet_plugin_ats_ril_done (void *cls)
2833 struct GNUNET_ATS_SolverFunctions *sf = cls;
2834 struct GAS_RIL_Handle *s = sf->cls;
2835 struct RIL_Peer_Agent *cur_agent;
2836 struct RIL_Peer_Agent *next_agent;
2838 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2840 s->done = GNUNET_YES;
2842 cur_agent = s->agents_head;
2843 while (NULL != cur_agent)
2845 next_agent = cur_agent->next;
2846 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2847 agent_die (s, cur_agent);
2848 cur_agent = next_agent;
2851 if (NULL != s->step_next_task_id)
2853 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2855 GNUNET_free(s->network_entries);
2862 /* end of plugin_ats_ril.c */