2 This file is part of GNUnet.
3 Copyright (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,
486 for (i = 0; i < agent->m; i++)
487 result += state[i] * agent->W[action][i];
489 /* prevent crashes if learning diverges */
491 return isnan(result) * UINT32_MAX;
493 return isinf(result) * UINT32_MAX;
499 * Get the index of the address in the agent's list.
501 * @param agent agent handle
502 * @param address address handle
503 * @return the index, starting with zero
506 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
509 struct RIL_Address_Wrapped *cur;
512 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
515 if (cur->address_naked == address)
523 * Gets the wrapped address from the agent's list
525 * @param agent agent handle
526 * @param address address handle
527 * @return wrapped address
529 static struct RIL_Address_Wrapped *
530 agent_address_get_wrapped (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
532 struct RIL_Address_Wrapped *cur;
534 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
535 if (cur->address_naked == address)
542 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
548 case RIL_ACTION_NOTHING:
551 case RIL_ACTION_BW_IN_INC:
552 case RIL_ACTION_BW_IN_DBL:
553 if (agent->bw_in >= RIL_MAX_BW)
558 case RIL_ACTION_BW_IN_DEC:
559 case RIL_ACTION_BW_IN_HLV:
560 if (agent->bw_in <= 0)
565 case RIL_ACTION_BW_OUT_INC:
566 case RIL_ACTION_BW_OUT_DBL:
567 if (agent->bw_out >= RIL_MAX_BW)
572 case RIL_ACTION_BW_OUT_DEC:
573 case RIL_ACTION_BW_OUT_HLV:
574 if (agent->bw_out <= 0)
580 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
582 address_index = action - RIL_ACTION_TYPE_NUM;
584 GNUNET_assert(address_index >= 0);
586 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
588 if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
589 agent->address_inuse->active)
595 // error - action does not exist
596 GNUNET_assert(GNUNET_NO);
602 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
603 * most reward in the future)
605 * @param agent agent performing the calculation
606 * @param state the state from which to take the action
607 * @return the action promising most future reward
610 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
613 int max_i = RIL_ACTION_INVALID;
615 double max_q = -DBL_MAX;
617 for (i = 0; i < agent->n; i++)
619 if (agent_action_is_possible(agent, i))
621 cur_q = agent_q (agent, state, i);
630 GNUNET_assert(RIL_ACTION_INVALID != max_i);
636 * Chooses a random action from the set of possible ones
638 * @param agent the agent performing the action
639 * @return the action index
642 agent_get_action_random (struct RIL_Peer_Agent *agent)
645 int is_possible[agent->n];
649 for (i = 0; i<agent->n; i++)
651 if (agent_action_is_possible(agent, i))
653 is_possible[i] = GNUNET_YES;
658 is_possible[i] = GNUNET_NO;
662 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
665 for (i = 0; i<agent->n; i++)
675 GNUNET_assert(GNUNET_NO);
676 return RIL_ACTION_INVALID;
681 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
683 * @param agent the agent performing the update
684 * @param reward the reward received for the last action
685 * @param s_next the new state, the last step got the agent into
686 * @param a_prime the new
689 agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
694 double **theta = agent->W;
696 delta = agent->envi->global_discount_integrated * reward; //reward
697 delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
698 delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
700 // 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",
701 // agent->step_count,
702 // agent_q (agent, agent->s_old, agent->a_old),
703 // agent->envi->parameters.alpha,
705 // agent->envi->global_discount_variable,
706 // agent_q (agent, s_next, a_prime),
709 for (k = 0; k < agent->n; k++)
711 for (i = 0; i < agent->m; i++)
713 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
714 // agent->envi->parameters.alpha,
718 theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
725 * Changes the eligibility trace vector e in various manners:
726 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
727 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
728 * #RIL_E_DECAY - multiplies e with discount factor and lambda as in the update rule
729 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
731 * @param agent the agent handle
732 * @param mod the kind of modification
733 * @param feature the feature vector
734 * @param action the action to take
737 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
738 enum RIL_E_Modification mod,
745 for (i = 0; i < agent->m; i++)
749 case RIL_E_ACCUMULATE:
750 agent->E[action][i] += feature[i];
753 agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
756 for (k = 0; k < agent->n; k++)
758 agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
762 for (k = 0; k < agent->n; k++)
772 * Informs the environment about the status of the solver
779 ril_inform (struct GAS_RIL_Handle *solver,
780 enum GAS_Solver_Operation op,
781 enum GAS_Solver_Status stat)
783 solver->env->info_cb (solver->env->cls,
790 * Calculates the maximum bandwidth an agent can assign in a network scope
794 static unsigned long long
795 ril_get_max_bw (struct RIL_Scope *net)
797 return GNUNET_MIN(2 * GNUNET_MAX(net->bw_in_available, net->bw_out_available), GNUNET_ATS_MaxBandwidth);
801 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
802 * notify ATS of its new decision
804 * @param solver solver handle
805 * @param agent agent handle
806 * @param new_address the address which is to be used
807 * @param new_bw_in the new amount of inbound bandwidth set for this address
808 * @param new_bw_out the new amount of outbound bandwidth set for this address
809 * @param silent disables invocation of the bw_changed callback, if #GNUNET_YES
812 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
813 struct RIL_Peer_Agent *agent,
814 struct ATS_Address *new_address,
815 unsigned long long new_bw_in,
816 unsigned long long new_bw_out,
819 int notify = GNUNET_NO;
821 LOG(GNUNET_ERROR_TYPE_DEBUG,
822 " set_active_suggestion() for peer '%s'\n",
823 GNUNET_i2s (&agent->peer));
826 if (agent->address_inuse != new_address)
828 if (NULL != agent->address_inuse)
830 agent->address_inuse->active = GNUNET_NO;
831 agent->address_inuse->assigned_bw_in = 0;
832 agent->address_inuse->assigned_bw_out = 0;
834 if (NULL != new_address)
836 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
837 new_address->active = agent->is_active;
838 new_address->assigned_bw_in = agent->bw_in;
839 new_address->assigned_bw_out = agent->bw_out;
841 notify |= GNUNET_YES;
847 if (new_address->active != agent->is_active)
849 new_address->active = agent->is_active;
850 notify |= GNUNET_YES;
854 if (agent->bw_in != new_bw_in)
856 agent->bw_in = new_bw_in;
857 new_address->assigned_bw_in = new_bw_in;
858 notify |= GNUNET_YES;
860 if (agent->bw_out != new_bw_out)
862 agent->bw_out = new_bw_out;
863 new_address->assigned_bw_out = new_bw_out;
864 notify |= GNUNET_YES;
868 if (notify && agent->is_active && (GNUNET_NO == silent))
872 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
873 agent->suggestion_issue = GNUNET_YES;
874 agent->suggestion_address = new_address;
876 else if (agent->address_inuse)
878 /* disconnect case, no new address */
879 GNUNET_assert(0 == agent->address_inuse->assigned_bw_in);
880 GNUNET_assert(0 == agent->address_inuse->assigned_bw_out);
884 agent->suggestion_issue = GNUNET_YES;
885 agent->suggestion_address = agent->address_inuse;
888 agent->address_inuse = new_address;
893 * Allocates a state vector and fills it with the features present
894 * @param solver the solver handle
895 * @param agent the agent handle
896 * @return pointer to the state vector
899 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
910 unsigned long long max_bw;
912 state = GNUNET_malloc (sizeof(double) * agent->m);
914 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
916 y[0] = (double) agent->bw_out;
917 y[1] = (double) agent->bw_in;
919 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
920 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
922 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
924 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
925 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
928 sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
929 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
938 * Retrieves an ATS information value of an address
940 * @param address the address in question
941 * @param type the ATS information type
945 ril_get_atsi (struct ATS_Address *address, uint32_t type)
948 GNUNET_assert(NULL != address);
950 if ((NULL == address->atsi) || (0 == address->atsi_count))
951 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
953 for (c1 = 0; c1 < address->atsi_count; c1++)
955 if (ntohl (address->atsi[c1].type) == type)
956 return ntohl (address->atsi[c1].value);
958 return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
962 * Returns the utility value of the connection an agent manages
964 * @param agent the agent in question
965 * @return the utility value
968 agent_get_utility (struct RIL_Peer_Agent *agent)
970 const double *preferences;
975 preferences = agent->envi->env->get_preferences (agent->envi->env->cls,
978 delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
979 delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
981 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
982 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
983 sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
984 // 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));
986 // return (double) (agent->bw_in/RIL_MIN_BW);
987 // return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
992 * Calculates the social welfare within a network scope according to what social
993 * welfare measure is set in the configuration.
995 * @param solver the solver handle
996 * @param scope the network scope in question
997 * @return the social welfare value
1000 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1002 struct RIL_Peer_Agent *cur;
1005 switch (solver->parameters.social_welfare)
1007 case RIL_WELFARE_EGALITARIAN:
1009 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1011 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1013 result = GNUNET_MIN(result, agent_get_utility(cur));
1018 case RIL_WELFARE_NASH:
1020 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1022 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
1024 result *= pow(agent_get_utility(cur), 1.0 / (double) scope->active_agent_count);
1029 GNUNET_assert(GNUNET_NO);
1034 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1036 struct RIL_Scope *net;
1037 unsigned long long over_max;
1038 unsigned long long over_in = 0;
1039 unsigned long long over_out = 0;
1041 net = agent->address_inuse->solver_information;
1043 if (net->bw_in_utilized > net->bw_in_available)
1045 over_in = net->bw_in_utilized - net->bw_in_available;
1046 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1048 /* increase quadratically */
1052 if (net->bw_out_utilized > net->bw_out_available)
1054 over_out = net->bw_out_utilized - net->bw_out_available;
1055 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1057 /* increase quadratically */
1058 over_out *= over_out;
1061 over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
1063 return -1.0 * (double) over_max;
1067 * Gets the reward for the last performed step, which is calculated in equal
1068 * parts from the local (the peer specific) and the global (for all peers
1069 * identical) reward.
1071 * @param solver the solver handle
1072 * @param agent the agent handle
1073 * @return the reward
1076 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1078 struct RIL_Scope *net;
1085 net = agent->address_inuse->solver_information;
1087 penalty = envi_get_penalty(solver, agent);
1088 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1089 delta = objective - agent->objective_old;
1090 agent->objective_old = objective;
1092 if (delta != 0 && penalty == 0)
1094 agent->nop_bonus = delta * RIL_NOP_DECAY;
1098 agent->nop_bonus *= RIL_NOP_DECAY;
1101 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1103 reward = delta + steady;
1104 return reward + penalty;
1108 * Doubles the bandwidth for the active address
1110 * @param solver solver handle
1111 * @param agent agent handle
1112 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1115 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1116 struct RIL_Peer_Agent *agent,
1119 unsigned long long new_bw;
1120 unsigned long long max_bw;
1122 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1126 new_bw = agent->bw_in * 2;
1127 if (new_bw < agent->bw_in || new_bw > max_bw)
1129 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1130 agent->bw_out, GNUNET_NO);
1134 new_bw = agent->bw_out * 2;
1135 if (new_bw < agent->bw_out || new_bw > max_bw)
1137 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1143 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1144 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1146 * @param solver solver handle
1147 * @param agent agent handle
1148 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1152 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1153 struct RIL_Peer_Agent *agent,
1156 unsigned long long new_bw;
1160 new_bw = agent->bw_in / 2;
1161 if (new_bw <= 0 || new_bw > agent->bw_in)
1163 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1168 new_bw = agent->bw_out / 2;
1169 if (new_bw <= 0 || new_bw > agent->bw_out)
1171 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1177 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1179 * @param solver solver handle
1180 * @param agent agent handle
1181 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1185 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1187 unsigned long long new_bw;
1188 unsigned long long max_bw;
1190 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1194 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1195 if (new_bw < agent->bw_in || new_bw > max_bw)
1197 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1198 agent->bw_out, GNUNET_NO);
1202 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1203 if (new_bw < agent->bw_out || new_bw > max_bw)
1205 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1211 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1212 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1214 * @param solver solver handle
1215 * @param agent agent handle
1216 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1220 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1222 unsigned long long new_bw;
1226 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1227 if (new_bw <= 0 || new_bw > agent->bw_in)
1229 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1234 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1235 if (new_bw <= 0 || new_bw > agent->bw_out)
1237 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1243 * Switches to the address given by its index
1245 * @param solver solver handle
1246 * @param agent agent handle
1247 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1250 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1251 struct RIL_Peer_Agent *agent,
1252 unsigned int address_index)
1254 struct RIL_Address_Wrapped *cur;
1257 //cur = agent_address_get_wrapped(agent, agent->address_inuse);
1259 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1261 if (i == address_index)
1263 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1271 //no address with address_index exists, in this case this action should not be callable
1272 GNUNET_assert(GNUNET_NO);
1276 * Puts the action into effect by calling the according function
1278 * @param solver the solver handle
1279 * @param agent the action handle
1280 * @param action the action to perform by the solver
1283 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1289 case RIL_ACTION_NOTHING:
1291 case RIL_ACTION_BW_IN_DBL:
1292 envi_action_bw_double (solver, agent, GNUNET_YES);
1294 case RIL_ACTION_BW_IN_HLV:
1295 envi_action_bw_halven (solver, agent, GNUNET_YES);
1297 case RIL_ACTION_BW_IN_INC:
1298 envi_action_bw_inc (solver, agent, GNUNET_YES);
1300 case RIL_ACTION_BW_IN_DEC:
1301 envi_action_bw_dec (solver, agent, GNUNET_YES);
1303 case RIL_ACTION_BW_OUT_DBL:
1304 envi_action_bw_double (solver, agent, GNUNET_NO);
1306 case RIL_ACTION_BW_OUT_HLV:
1307 envi_action_bw_halven (solver, agent, GNUNET_NO);
1309 case RIL_ACTION_BW_OUT_INC:
1310 envi_action_bw_inc (solver, agent, GNUNET_NO);
1312 case RIL_ACTION_BW_OUT_DEC:
1313 envi_action_bw_dec (solver, agent, GNUNET_NO);
1316 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1318 address_index = action - RIL_ACTION_TYPE_NUM;
1320 GNUNET_assert(address_index >= 0);
1322 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1324 envi_action_address_switch (solver, agent, address_index);
1327 // error - action does not exist
1328 GNUNET_assert(GNUNET_NO);
1333 * Selects the next action using the e-greedy strategy. I.e. with a probability
1334 * of (1-e) the action with the maximum expected return will be chosen
1335 * (=> exploitation) and with probability (e) a random action will be chosen.
1336 * In case the Q-learning rule is set, the function also resets the eligibility
1337 * traces in the exploration case (after Watkin's Q-learning).
1339 * @param agent the agent selecting an action
1340 * @param state the current state-feature vector
1341 * @return the action index
1344 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1347 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1348 UINT32_MAX) / (double) UINT32_MAX;
1350 if (r < agent->envi->parameters.epsilon) //explore
1352 action = agent_get_action_random(agent);
1353 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1355 agent->eligibility_reset = GNUNET_YES;
1357 agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
1362 action = agent_get_action_max(agent, state);
1368 * Selects the next action with a probability corresponding to its value. The
1369 * probability is calculated using a Boltzmann distribution with a temperature
1370 * value. The higher the temperature, the more are the action selection
1371 * probabilities the same. With a temperature of 0, the selection is greedy,
1372 * i.e. always the action with the highest value is chosen.
1378 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1382 double eqt[agent->n];
1387 a_max = agent_get_action_max(agent, state);
1389 for (i=0; i<agent->n; i++)
1391 if (agent_action_is_possible(agent, i))
1393 eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1395 eqt[i] = isinf(eqt[i]) * UINT32_MAX;
1399 for (i=0; i<agent->n; i++)
1401 if (agent_action_is_possible(agent, i))
1410 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1411 UINT32_MAX) / (double) UINT32_MAX;
1413 for (i=0; i<agent->n; i++)
1419 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1420 agent->eligibility_reset = GNUNET_YES;
1421 agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay;
1427 GNUNET_assert(GNUNET_NO);
1428 return RIL_ACTION_INVALID;
1432 * Select the next action of an agent either according to the e-greedy strategy
1433 * or the softmax strategy.
1435 * @param agent the agent in question
1436 * @param state the current state-feature vector
1437 * @return the action index
1440 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1442 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1444 return agent_select_egreedy(agent, state);
1448 return agent_select_softmax(agent, state);
1453 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1454 * after having done the last action a_old. It observes the new state s_next and the reward
1455 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1456 * next action is put into effect.
1458 * @param agent the agent performing the step
1461 agent_step (struct RIL_Peer_Agent *agent)
1463 int a_next = RIL_ACTION_INVALID;
1468 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1469 GNUNET_i2s (&agent->peer),
1470 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1472 s_next = envi_get_state (agent->envi, agent);
1473 reward = envi_get_reward (agent->envi, agent);
1475 if (agent->eligibility_reset)
1477 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1478 agent->eligibility_reset = GNUNET_NO;
1482 agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
1484 if (RIL_ACTION_INVALID != agent->a_old)
1486 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1489 switch (agent->envi->parameters.algorithm)
1491 case RIL_ALGO_SARSA:
1492 a_next = agent_select_action (agent, s_next);
1493 if (RIL_ACTION_INVALID != agent->a_old)
1495 //updates weights with selected action (on-policy), if not first step
1496 agent_update (agent, reward, s_next, a_next);
1501 a_max = agent_get_action_max (agent, s_next);
1502 if (RIL_ACTION_INVALID != agent->a_old)
1504 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1505 agent_update (agent, reward, s_next, a_max);
1507 a_next = agent_select_action (agent, s_next);
1511 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1513 LOG (GNUNET_ERROR_TYPE_DEBUG, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1520 envi_do_action (agent->envi, agent, a_next);
1522 GNUNET_free(agent->s_old);
1523 agent->s_old = s_next;
1524 agent->a_old = a_next;
1526 agent->step_count += 1;
1530 * Prototype of the ril_step() procedure
1532 * @param solver the solver handle
1535 ril_step (struct GAS_RIL_Handle *solver);
1539 * Task for the scheduler, which performs one step and lets the solver know that
1540 * no further step is scheduled.
1542 * @param cls the solver handle
1543 * @param tc the task context for the scheduler
1546 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1548 struct GAS_RIL_Handle *solver = cls;
1550 solver->step_next_task_id = NULL;
1555 * Determines how much of the available bandwidth is assigned. If more is
1556 * assigned than available it returns 1. The function is used to determine the
1557 * step size of the adaptive stepping.
1559 * @param solver the solver handle
1563 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1566 struct RIL_Scope net;
1567 unsigned long long sum_assigned = 0;
1568 unsigned long long sum_available = 0;
1571 for (i = 0; i < solver->networks_count; i++)
1573 net = solver->network_entries[i];
1574 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1576 sum_assigned += net.bw_in_utilized;
1577 sum_assigned += net.bw_out_utilized;
1578 sum_available += net.bw_in_available;
1579 sum_available += net.bw_out_available;
1582 if (sum_available > 0)
1584 ratio = ((double) sum_assigned) / ((double) sum_available);
1591 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1595 * Lookup network struct by type
1597 * @param s the solver handle
1598 * @param type the network type
1599 * @return the network struct
1601 static struct RIL_Scope *
1602 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1606 for (i = 0; i < s->networks_count; i++)
1608 if (s->network_entries[i].type == type)
1610 return &s->network_entries[i];
1617 * Determines whether more connections are allocated in a network scope, than
1618 * they would theoretically fit. This is used as a heuristic to determine,
1619 * whether a new connection can be allocated or not.
1621 * @param solver the solver handle
1622 * @param network the network scope in question
1623 * @return GNUNET_YES if there are theoretically enough resources left
1626 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1628 struct RIL_Scope *net;
1629 struct RIL_Peer_Agent *agent;
1630 unsigned long long address_count = 0;
1632 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1634 if (agent->address_inuse && agent->is_active)
1636 net = agent->address_inuse->solver_information;
1637 if (net->type == network)
1644 net = ril_get_network (solver, network);
1645 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1649 * Unblocks an agent for which a connection request is there, that could not
1650 * be satisfied. Iterates over the addresses of the agent, if one of its
1651 * addresses can now be allocated in its scope the agent is unblocked,
1652 * otherwise it remains unchanged.
1654 * @param solver the solver handle
1655 * @param agent the agent in question
1659 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1661 struct RIL_Address_Wrapped *addr_wrap;
1662 struct RIL_Scope *net;
1663 unsigned long long start_in;
1664 unsigned long long start_out;
1666 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1668 net = addr_wrap->address_naked->solver_information;
1669 if (ril_network_is_not_full(solver, net->type))
1671 if (NULL == agent->address_inuse)
1673 start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
1674 start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
1675 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, start_in, start_out, silent);
1680 agent->address_inuse = NULL;
1684 * Determines how much the reward needs to be discounted depending on the amount
1685 * of time, which has passed since the last time-step.
1687 * @param solver the solver handle
1690 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1692 struct GNUNET_TIME_Absolute time_now;
1693 struct GNUNET_TIME_Relative time_delta;
1696 // MDP case only for debugging purposes
1697 if (solver->simulate)
1699 solver->global_discount_variable = solver->parameters.gamma;
1700 solver->global_discount_integrated = 1;
1706 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1707 time_now = GNUNET_TIME_absolute_get ();
1708 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1709 solver->step_time_last = time_now;
1710 tau = (double) time_delta.rel_value_us
1711 / (double) solver->parameters.step_time_min.rel_value_us;
1713 //calculate reward discounts (once per step for all agents)
1714 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1715 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1716 / (double) solver->parameters.beta;
1720 * Count the number of active agents/connections in a network scope
1722 * @param solver the solver handle
1723 * @param scope the network scope in question
1724 * @return the number of allocated connections
1727 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1730 struct RIL_Peer_Agent *cur_agent;
1732 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1734 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1743 * Calculates how much bandwidth is assigned in sum in a network scope, either
1744 * in the inbound or in the outbound direction.
1746 * @param solver the solver handle
1747 * @param type the type of the network scope in question
1748 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1749 * otherwise the outbound direction will be summed up
1750 * @return the sum of the assigned bandwidths
1752 static unsigned long long
1753 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1755 struct RIL_Peer_Agent *cur;
1756 struct RIL_Scope *net;
1757 unsigned long long sum = 0;
1759 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1761 if (cur->is_active && cur->address_inuse)
1763 net = cur->address_inuse->solver_information;
1764 if (net->type == type)
1778 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1779 * either in the inbound or in the outbound direction.
1781 * @param solver the solver handle
1782 * @param type the type of the network scope in question
1783 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1784 * otherwise the outbound direction will be summed up
1785 * @return the sum of the utilized bandwidths (in bytes/second)
1787 static unsigned long long
1788 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1790 struct RIL_Peer_Agent *cur;
1791 struct RIL_Scope *net;
1792 unsigned long long sum = 0;
1794 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1796 if (cur->is_active && cur->address_inuse)
1798 net = cur->address_inuse->solver_information;
1799 if (net->type == type)
1802 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
1804 sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
1813 * Retrieves the state of the network scope, so that its attributes are up-to-
1816 * @param solver the solver handle
1819 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1822 struct RIL_Scope *net;
1824 for (c = 0; c < solver->networks_count; c++)
1826 net = &solver->network_entries[c];
1827 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1828 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1829 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1830 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1831 net->active_agent_count = ril_network_count_active_agents(solver, net);
1832 net->social_welfare = ril_network_get_social_welfare(solver, net);
1837 * Schedules the next global step in an adaptive way. The more resources are
1838 * left, the earlier the next step is scheduled. This serves the reactivity of
1839 * the solver to changed inputs.
1841 * @param solver the solver handle
1844 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1850 struct GNUNET_TIME_Relative time_next;
1852 used_ratio = ril_get_used_resource_ratio (solver);
1855 solver->parameters.step_time_min.rel_value_us
1856 <= solver->parameters.step_time_max.rel_value_us);
1858 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1859 solver->parameters.step_time_min).rel_value_us;
1860 offset = (double) solver->parameters.step_time_min.rel_value_us;
1861 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1863 GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
1864 GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
1866 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1868 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1874 if (solver->simulate)
1876 time_next = GNUNET_TIME_UNIT_ZERO;
1879 if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1881 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1887 * Triggers one step per agent
1892 ril_step (struct GAS_RIL_Handle *solver)
1894 struct RIL_Peer_Agent *cur;
1896 if (GNUNET_YES == solver->bulk_lock)
1898 solver->bulk_changes++;
1902 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1904 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1906 if (0 == solver->step_count)
1908 solver->step_time_last = GNUNET_TIME_absolute_get ();
1911 ril_calculate_discount (solver);
1912 ril_networks_update_state (solver);
1914 //trigger one step per active, unblocked agent
1915 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1919 if (NULL == cur->address_inuse)
1921 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1923 if (cur->address_inuse)
1930 ril_networks_update_state (solver);
1932 solver->step_count++;
1933 ril_step_schedule_next (solver);
1935 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1937 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1938 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1940 if (cur->suggestion_issue) {
1941 solver->env->bandwidth_changed_cb (solver->env->cls,
1942 cur->suggestion_address);
1943 cur->suggestion_issue = GNUNET_NO;
1946 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1950 * Initializes the matrix W of parameter vectors theta with small random numbers.
1952 * @param agent The respective agent
1955 agent_w_init (struct RIL_Peer_Agent *agent)
1960 for (i = 0; i < agent->n; i++)
1962 for (k = 0; k < agent->m; k++)
1964 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));
1970 * Initialize an agent without addresses and its knowledge base
1972 * @param s ril solver
1973 * @param peer the one in question
1974 * @return handle to the new agent
1976 static struct RIL_Peer_Agent *
1977 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1980 struct GAS_RIL_Handle * solver = s;
1981 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1983 agent->envi = solver;
1984 agent->peer = *peer;
1985 agent->step_count = 0;
1986 agent->is_active = GNUNET_NO;
1987 agent->bw_in = RIL_MIN_BW;
1988 agent->bw_out = RIL_MIN_BW;
1989 agent->suggestion_issue = GNUNET_NO;
1990 agent->n = RIL_ACTION_TYPE_NUM;
1992 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1993 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1994 for (i = 0; i < agent->n; i++)
1996 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1997 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1999 agent_w_init(agent);
2000 agent->eligibility_reset = GNUNET_NO;
2001 agent->a_old = RIL_ACTION_INVALID;
2002 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
2003 agent->address_inuse = NULL;
2004 agent->objective_old = 0;
2005 agent->nop_bonus = 0;
2013 * @param solver the solver handle
2014 * @param agent the agent to retire
2017 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
2021 for (i = 0; i < agent->n; i++)
2023 GNUNET_free_non_null(agent->W[i]);
2024 GNUNET_free_non_null(agent->E[i]);
2026 GNUNET_free_non_null(agent->W);
2027 GNUNET_free_non_null(agent->E);
2028 GNUNET_free_non_null(agent->s_old);
2033 * Returns the agent for a peer
2035 * @param solver the solver handle
2036 * @param peer the identity of the peer
2037 * @param create whether or not to create an agent, if none is allocated yet
2040 static struct RIL_Peer_Agent *
2041 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
2043 struct RIL_Peer_Agent *cur;
2045 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2047 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
2055 cur = agent_init (solver, peer);
2056 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
2063 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
2064 * considered inactive and not used. Addresses in an inactive network are ignored.
2066 * @param solver solver handle
2067 * @param network the network type
2068 * @return whether or not the network is considered active
2071 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
2073 struct RIL_Scope *net;
2075 net = ril_get_network (solver, network);
2076 return net->bw_out_available >= RIL_MIN_BW;
2080 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
2081 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
2082 * frees the memory of the old vector, and redirects the pointer to the new one.
2084 * @param old pointer to the pointer to the first element of the vector
2085 * @param element_size byte size of the vector elements
2086 * @param hole_start the first element to cut out
2087 * @param hole_length the number of elements to cut out
2088 * @param old_length the length of the old vector
2091 ril_cut_from_vector (void **old,
2092 size_t element_size,
2093 unsigned int hole_start,
2094 unsigned int hole_length,
2095 unsigned int old_length)
2098 char *oldptr = (char *) *old;
2100 unsigned int bytes_before;
2101 unsigned int bytes_hole;
2102 unsigned int bytes_after;
2104 GNUNET_assert(old_length >= hole_length);
2105 GNUNET_assert(old_length >= (hole_start + hole_length));
2107 size = element_size * (old_length - hole_length);
2109 bytes_before = element_size * hole_start;
2110 bytes_hole = element_size * hole_length;
2111 bytes_after = element_size * (old_length - hole_start - hole_length);
2119 tmpptr = GNUNET_malloc (size);
2120 memcpy (tmpptr, oldptr, bytes_before);
2121 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2127 *old = (void *) tmpptr;
2131 * Solver API functions
2132 * ---------------------------
2136 * Change relative preference for quality in solver
2138 * @param solver the solver handle
2139 * @param peer the peer to change the preference for
2140 * @param kind the kind to change the preference
2141 * @param pref_rel the normalized preference value for this kind over all clients
2144 GAS_ril_address_change_preference (void *solver,
2145 const struct GNUNET_PeerIdentity *peer,
2146 enum GNUNET_ATS_PreferenceKind kind,
2149 LOG(GNUNET_ERROR_TYPE_DEBUG,
2150 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2151 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2153 struct GAS_RIL_Handle *s = solver;
2155 s->parameters.temperature = s->parameters.temperature_init;
2156 s->parameters.epsilon = s->parameters.epsilon_init;
2162 * Add a new address for a peer to the solver
2164 * The address is already contained in the addresses hashmap!
2166 * @param solver the solver Handle
2167 * @param address the address to add
2168 * @param network network type of this address
2171 GAS_ril_address_add (void *solver,
2172 struct ATS_Address *address,
2175 struct GAS_RIL_Handle *s = solver;
2176 struct RIL_Peer_Agent *agent;
2177 struct RIL_Address_Wrapped *address_wrapped;
2178 struct RIL_Scope *net;
2186 LOG (GNUNET_ERROR_TYPE_DEBUG,
2187 "API_address_add()\n");
2189 net = ril_get_network (s, network);
2190 address->solver_information = net;
2192 if (!ril_network_is_active (s, network))
2194 LOG(GNUNET_ERROR_TYPE_DEBUG,
2195 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2196 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2200 s->parameters.temperature = s->parameters.temperature_init;
2201 s->parameters.epsilon = s->parameters.epsilon_init;
2203 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2206 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2207 address_wrapped->address_naked = address;
2208 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2210 //increase size of W
2211 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2213 n_new = agent->n + 1;
2216 GNUNET_array_grow(agent->W, agent->n, n_new);
2218 GNUNET_array_grow(agent->E, agent->n, n_new);
2219 for (i = 0; i < n_new; i++)
2224 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2226 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2231 GNUNET_array_grow(agent->W[i], zero, m_new);
2233 GNUNET_array_grow(agent->E[i], zero, m_new);
2237 //increase size of old state vector
2239 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2241 ril_try_unblock_agent(s, agent, GNUNET_NO);
2245 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2246 address->active ? "active" : "inactive", address->plugin, address->addr,
2247 GNUNET_i2s (&address->peer));
2251 * Delete an address in the solver
2253 * The address is not contained in the address hashmap anymore!
2255 * @param solver the solver handle
2256 * @param address the address to remove
2259 GAS_ril_address_delete (void *solver,
2260 struct ATS_Address *address)
2262 struct GAS_RIL_Handle *s = solver;
2263 struct RIL_Peer_Agent *agent;
2264 struct RIL_Address_Wrapped *address_wrapped;
2269 struct RIL_Scope *net;
2271 LOG (GNUNET_ERROR_TYPE_DEBUG,
2272 "API_address_delete() Delete %s %s address %s for peer '%s'\n",
2273 address->active ? "active" : "inactive",
2276 GNUNET_i2s (&address->peer));
2278 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2281 net = address->solver_information;
2282 GNUNET_assert(! ril_network_is_active (s, net->type));
2283 LOG (GNUNET_ERROR_TYPE_DEBUG,
2284 "No agent allocated for peer yet, since address was in inactive network\n");
2288 s->parameters.temperature = s->parameters.temperature_init;
2289 s->parameters.epsilon = s->parameters.epsilon_init;
2291 address_index = agent_address_get_index (agent, address);
2292 address_wrapped = agent_address_get_wrapped (agent, address);
2294 if (NULL == address_wrapped)
2296 net = address->solver_information;
2297 LOG (GNUNET_ERROR_TYPE_DEBUG,
2298 "Address not considered by agent, address was in inactive network\n");
2301 GNUNET_CONTAINER_DLL_remove (agent->addresses_head,
2302 agent->addresses_tail,
2304 GNUNET_free (address_wrapped);
2307 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2308 n_new = agent->n - 1;
2310 for (i = 0; i < agent->n; i++)
2312 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2313 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2314 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2315 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2316 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2317 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2319 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2320 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2321 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2323 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2325 //correct last action
2326 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2330 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2332 agent->a_old = RIL_ACTION_INVALID;
2334 //decrease old state vector
2335 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2336 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2337 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2341 if (agent->address_inuse == address)
2343 if (NULL != agent->addresses_head) //if peer has an address left, use it
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2346 "Active address died, suggesting alternative!\n");
2347 envi_set_active_suggestion (s,
2349 agent->addresses_head->address_naked,
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2357 "Active address died, suggesting disconnect!\n");
2358 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2362 if (agent->suggestion_address == address)
2364 agent->suggestion_issue = GNUNET_NO;
2365 agent->suggestion_address = NULL;
2367 GNUNET_assert (agent->address_inuse != address);
2372 * Update the properties of an address in the solver
2374 * @param solver solver handle
2375 * @param address the address
2376 * @param type the ATSI type
2377 * @param abs_value the absolute value of the property
2378 * @param rel_value the normalized value
2381 GAS_ril_address_property_changed (void *solver,
2382 struct ATS_Address *address,
2383 enum GNUNET_ATS_Property type,
2387 struct GAS_RIL_Handle *s = solver;
2389 LOG(GNUNET_ERROR_TYPE_DEBUG,
2390 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2392 GNUNET_ATS_print_property_type (type),
2393 GNUNET_i2s (&address->peer),
2394 address->addr, rel_value);
2397 s->parameters.temperature = s->parameters.temperature_init;
2398 s->parameters.epsilon = s->parameters.epsilon_init;
2404 * Give feedback about the current assignment
2406 * @param solver the solver handle
2407 * @param application the application
2408 * @param peer the peer to change the preference for
2409 * @param scope the time interval for this feedback: [now - scope .. now]
2410 * @param kind the kind to change the preference
2411 * @param score the score
2414 GAS_ril_address_preference_feedback (void *solver,
2415 struct GNUNET_SERVER_Client *application,
2416 const struct GNUNET_PeerIdentity *peer,
2417 const struct GNUNET_TIME_Relative scope,
2418 enum GNUNET_ATS_PreferenceKind kind,
2421 LOG (GNUNET_ERROR_TYPE_DEBUG,
2422 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2423 "preference %s for %d seconds\n",
2426 GNUNET_ATS_print_preference_type (kind),
2427 scope.rel_value_us / 1000000);
2432 * Start a bulk operation
2434 * @param solver the solver
2437 GAS_ril_bulk_start (void *solver)
2439 struct GAS_RIL_Handle *s = solver;
2441 LOG (GNUNET_ERROR_TYPE_DEBUG,
2442 "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2449 * Bulk operation done
2451 * @param solver the solver handle
2454 GAS_ril_bulk_stop (void *solver)
2456 struct GAS_RIL_Handle *s = solver;
2458 LOG(GNUNET_ERROR_TYPE_DEBUG,
2459 "API_bulk_stop() lock: %d\n",
2462 if (s->bulk_lock < 1)
2469 if (0 < s->bulk_changes)
2472 s->bulk_changes = 0;
2478 * Tell solver to notify ATS if the address to use changes for a specific
2479 * peer using the bandwidth changed callback
2481 * The solver must only notify about changes for peers with pending address
2484 * @param solver the solver handle
2485 * @param peer the identity of the peer
2487 static const struct ATS_Address *
2488 GAS_ril_get_preferred_address (void *solver,
2489 const struct GNUNET_PeerIdentity *peer)
2491 struct GAS_RIL_Handle *s = solver;
2492 struct RIL_Peer_Agent *agent;
2494 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2496 agent = ril_get_agent (s, peer, GNUNET_YES);
2498 agent->is_active = GNUNET_YES;
2499 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2501 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2503 if (agent->address_inuse)
2505 LOG(GNUNET_ERROR_TYPE_DEBUG,
2506 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2507 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2511 LOG(GNUNET_ERROR_TYPE_DEBUG,
2512 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2514 s->parameters.temperature = s->parameters.temperature_init;
2515 s->parameters.epsilon = s->parameters.epsilon_init;
2517 return agent->address_inuse;
2521 * Tell solver stop notifying ATS about changes for this peers
2523 * The solver must only notify about changes for peers with pending address
2526 * @param solver the solver handle
2527 * @param peer the peer
2530 GAS_ril_stop_get_preferred_address (void *solver,
2531 const struct GNUNET_PeerIdentity *peer)
2533 struct GAS_RIL_Handle *s = solver;
2534 struct RIL_Peer_Agent *agent;
2536 LOG (GNUNET_ERROR_TYPE_DEBUG,
2537 "API_stop_get_preferred_address()");
2539 agent = ril_get_agent (s, peer, GNUNET_NO);
2546 if (GNUNET_NO == agent->is_active)
2552 s->parameters.temperature = s->parameters.temperature_init;
2553 s->parameters.epsilon = s->parameters.epsilon_init;
2555 agent->is_active = GNUNET_NO;
2557 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2562 LOG(GNUNET_ERROR_TYPE_DEBUG,
2563 "API_stop_get_preferred_address() Paused agent for peer '%s'\n",
2569 * Entry point for the plugin
2571 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2574 libgnunet_plugin_ats_ril_init (void *cls)
2576 static struct GNUNET_ATS_SolverFunctions sf;
2577 struct GNUNET_ATS_PluginEnvironment *env = cls;
2578 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2579 struct RIL_Scope * cur;
2584 LOG (GNUNET_ERROR_TYPE_DEBUG,
2585 "API_init() Initializing RIL solver\n");
2587 GNUNET_assert (NULL != env);
2588 GNUNET_assert (NULL != env->cfg);
2589 GNUNET_assert (NULL != env->stats);
2590 GNUNET_assert (NULL != env->bandwidth_changed_cb);
2591 GNUNET_assert (NULL != env->get_preferences);
2593 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2595 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2599 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2600 &solver->parameters.step_time_min))
2602 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2606 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2607 &solver->parameters.step_time_max))
2609 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2612 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2614 GNUNET_STRINGS_utf8_toupper (string, string);
2615 if (0 == strcmp (string, "SARSA"))
2617 solver->parameters.algorithm = RIL_ALGO_SARSA;
2619 if (0 == strcmp (string, "Q-LEARNING"))
2621 solver->parameters.algorithm = RIL_ALGO_Q;
2624 GNUNET_free (string);
2628 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2631 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2633 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2634 GNUNET_free (string);
2638 solver->parameters.select = RIL_DEFAULT_SELECT;
2642 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2643 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2644 "RIL_DISCOUNT_BETA", &f_tmp))
2648 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2649 "RIL_DISCOUNT_BETA", f_tmp);
2653 solver->parameters.beta = f_tmp;
2654 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2655 "RIL_DISCOUNT_BETA", f_tmp);
2659 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2660 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2661 "RIL_DISCOUNT_GAMMA", &f_tmp))
2663 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2665 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2666 "RIL_DISCOUNT_GAMMA", f_tmp);
2670 solver->parameters.gamma = f_tmp;
2671 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2672 "RIL_DISCOUNT_GAMMA", f_tmp);
2676 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2677 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2678 "RIL_GRADIENT_STEP_SIZE", &f_tmp))
2680 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2682 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2683 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2687 solver->parameters.alpha = f_tmp;
2688 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2689 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2693 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2694 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2695 "RIL_TRACE_DECAY", &f_tmp))
2697 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2699 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2700 "RIL_TRACE_DECAY", f_tmp);
2704 solver->parameters.lambda = f_tmp;
2705 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2706 "RIL_TRACE_DECAY", f_tmp);
2710 solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
2711 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2712 "RIL_EXPLORE_RATIO", &f_tmp))
2714 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2716 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2717 "RIL_EXPLORE_RATIO", f_tmp);
2721 solver->parameters.epsilon_init = f_tmp;
2722 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2723 "RIL_EXPLORE_RATIO", f_tmp);
2727 solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
2728 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2729 "RIL_EXPLORE_DECAY", &f_tmp))
2731 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2733 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2734 "RIL_EXPLORE_DECAY", f_tmp);
2738 solver->parameters.epsilon_decay = f_tmp;
2739 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2740 "RIL_EXPLORE_DECAY", f_tmp);
2744 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2745 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2746 "RIL_TEMPERATURE", &f_tmp))
2750 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2751 "RIL_TEMPERATURE", f_tmp);
2755 solver->parameters.temperature_init = f_tmp;
2756 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2757 "RIL_TEMPERATURE", f_tmp);
2761 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
2762 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2763 "RIL_TEMPERATURE_DECAY", &f_tmp))
2765 if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1)
2767 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2768 "RIL_TEMPERATURE_DECAY", f_tmp);
2772 solver->parameters.temperature_decay = f_tmp;
2773 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2774 "RIL_TEMPERATURE_DECAY", f_tmp);
2778 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2780 solver->simulate = GNUNET_NO;
2783 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2785 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2789 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2792 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2794 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2795 GNUNET_free (string);
2799 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2804 sf.s_add = &GAS_ril_address_add;
2805 sf.s_address_update_property = &GAS_ril_address_property_changed;
2806 sf.s_get = &GAS_ril_get_preferred_address;
2807 sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2808 sf.s_pref = &GAS_ril_address_change_preference;
2809 sf.s_feedback = &GAS_ril_address_preference_feedback;
2810 sf.s_del = &GAS_ril_address_delete;
2811 sf.s_bulk_start = &GAS_ril_bulk_start;
2812 sf.s_bulk_stop = &GAS_ril_bulk_stop;
2814 solver->networks_count = env->network_count;
2815 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2816 solver->step_count = 0;
2817 solver->done = GNUNET_NO;
2819 for (c = 0; c < env->network_count; c++)
2821 cur = &solver->network_entries[c];
2823 cur->bw_in_available = env->in_quota[c];
2824 cur->bw_out_available = env->out_quota[c];
2825 LOG (GNUNET_ERROR_TYPE_DEBUG,
2826 "init() Quotas for %s network: IN %llu - OUT %llu\n",
2827 GNUNET_ATS_print_network_type(cur->type),
2828 cur->bw_in_available/1024,
2829 cur->bw_out_available/1024);
2832 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2833 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2834 solver->parameters.algorithm ? "Q" : "SARSA",
2835 solver->parameters.alpha,
2836 solver->parameters.beta,
2837 solver->parameters.lambda);
2838 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
2839 solver->parameters.epsilon,
2840 solver->parameters.temperature,
2841 solver->parameters.select ? "EGREEDY" : "SOFTMAX");
2842 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2843 solver->parameters.rbf_divisor);
2850 * Exit point for the plugin
2852 * @param cls the solver handle
2855 libgnunet_plugin_ats_ril_done (void *cls)
2857 struct GNUNET_ATS_SolverFunctions *sf = cls;
2858 struct GAS_RIL_Handle *s = sf->cls;
2859 struct RIL_Peer_Agent *cur_agent;
2860 struct RIL_Peer_Agent *next_agent;
2862 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2864 s->done = GNUNET_YES;
2866 cur_agent = s->agents_head;
2867 while (NULL != cur_agent)
2869 next_agent = cur_agent->next;
2870 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2871 agent_die (s, cur_agent);
2872 cur_agent = next_agent;
2875 if (NULL != s->step_next_task_id)
2877 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2879 GNUNET_free(s->network_entries);
2886 /* end of plugin_ats_ril.c */