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)));
939 * Returns the utility value of the connection an agent manages
941 * @param agent the agent in question
942 * @return the utility value
945 agent_get_utility (struct RIL_Peer_Agent *agent)
947 const double *preferences;
952 preferences = agent->envi->env->get_preferences (agent->envi->env->cls,
955 delay_atsi = agent->address_inuse->norm_delay.norm;
956 delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
958 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
959 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
960 sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
965 * Calculates the social welfare within a network scope according to what social
966 * welfare measure is set in the configuration.
968 * @param solver the solver handle
969 * @param scope the network scope in question
970 * @return the social welfare value
973 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
975 struct RIL_Peer_Agent *cur;
978 switch (solver->parameters.social_welfare)
980 case RIL_WELFARE_EGALITARIAN:
982 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
984 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
986 result = GNUNET_MIN(result, agent_get_utility(cur));
991 case RIL_WELFARE_NASH:
993 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
995 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
997 result *= pow(agent_get_utility(cur), 1.0 / (double) scope->active_agent_count);
1002 GNUNET_assert(GNUNET_NO);
1007 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1009 struct RIL_Scope *net;
1010 unsigned long long over_max;
1011 unsigned long long over_in = 0;
1012 unsigned long long over_out = 0;
1014 net = agent->address_inuse->solver_information;
1016 if (net->bw_in_utilized > net->bw_in_available)
1018 over_in = net->bw_in_utilized - net->bw_in_available;
1019 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1021 /* increase quadratically */
1025 if (net->bw_out_utilized > net->bw_out_available)
1027 over_out = net->bw_out_utilized - net->bw_out_available;
1028 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1030 /* increase quadratically */
1031 over_out *= over_out;
1034 over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
1036 return -1.0 * (double) over_max;
1040 * Gets the reward for the last performed step, which is calculated in equal
1041 * parts from the local (the peer specific) and the global (for all peers
1042 * identical) reward.
1044 * @param solver the solver handle
1045 * @param agent the agent handle
1046 * @return the reward
1049 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1051 struct RIL_Scope *net;
1058 net = agent->address_inuse->solver_information;
1060 penalty = envi_get_penalty(solver, agent);
1061 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1062 delta = objective - agent->objective_old;
1063 agent->objective_old = objective;
1065 if (delta != 0 && penalty == 0)
1067 agent->nop_bonus = delta * RIL_NOP_DECAY;
1071 agent->nop_bonus *= RIL_NOP_DECAY;
1074 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1076 reward = delta + steady;
1077 return reward + penalty;
1081 * Doubles the bandwidth for the active address
1083 * @param solver solver handle
1084 * @param agent agent handle
1085 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1088 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1089 struct RIL_Peer_Agent *agent,
1092 unsigned long long new_bw;
1093 unsigned long long max_bw;
1095 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1099 new_bw = agent->bw_in * 2;
1100 if (new_bw < agent->bw_in || new_bw > max_bw)
1102 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1103 agent->bw_out, GNUNET_NO);
1107 new_bw = agent->bw_out * 2;
1108 if (new_bw < agent->bw_out || new_bw > max_bw)
1110 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1116 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1117 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1119 * @param solver solver handle
1120 * @param agent agent handle
1121 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1125 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1126 struct RIL_Peer_Agent *agent,
1129 unsigned long long new_bw;
1133 new_bw = agent->bw_in / 2;
1134 if (new_bw <= 0 || new_bw > agent->bw_in)
1136 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1141 new_bw = agent->bw_out / 2;
1142 if (new_bw <= 0 || new_bw > agent->bw_out)
1144 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1150 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1152 * @param solver solver handle
1153 * @param agent agent handle
1154 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1158 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1160 unsigned long long new_bw;
1161 unsigned long long max_bw;
1163 max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1167 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1168 if (new_bw < agent->bw_in || new_bw > max_bw)
1170 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1171 agent->bw_out, GNUNET_NO);
1175 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1176 if (new_bw < agent->bw_out || new_bw > max_bw)
1178 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1184 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1185 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1187 * @param solver solver handle
1188 * @param agent agent handle
1189 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1193 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1195 unsigned long long new_bw;
1199 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1200 if (new_bw <= 0 || new_bw > agent->bw_in)
1202 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1207 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1208 if (new_bw <= 0 || new_bw > agent->bw_out)
1210 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1216 * Switches to the address given by its index
1218 * @param solver solver handle
1219 * @param agent agent handle
1220 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1223 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1224 struct RIL_Peer_Agent *agent,
1225 unsigned int address_index)
1227 struct RIL_Address_Wrapped *cur;
1230 //cur = agent_address_get_wrapped(agent, agent->address_inuse);
1232 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1234 if (i == address_index)
1236 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1244 //no address with address_index exists, in this case this action should not be callable
1245 GNUNET_assert(GNUNET_NO);
1249 * Puts the action into effect by calling the according function
1251 * @param solver the solver handle
1252 * @param agent the action handle
1253 * @param action the action to perform by the solver
1256 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1262 case RIL_ACTION_NOTHING:
1264 case RIL_ACTION_BW_IN_DBL:
1265 envi_action_bw_double (solver, agent, GNUNET_YES);
1267 case RIL_ACTION_BW_IN_HLV:
1268 envi_action_bw_halven (solver, agent, GNUNET_YES);
1270 case RIL_ACTION_BW_IN_INC:
1271 envi_action_bw_inc (solver, agent, GNUNET_YES);
1273 case RIL_ACTION_BW_IN_DEC:
1274 envi_action_bw_dec (solver, agent, GNUNET_YES);
1276 case RIL_ACTION_BW_OUT_DBL:
1277 envi_action_bw_double (solver, agent, GNUNET_NO);
1279 case RIL_ACTION_BW_OUT_HLV:
1280 envi_action_bw_halven (solver, agent, GNUNET_NO);
1282 case RIL_ACTION_BW_OUT_INC:
1283 envi_action_bw_inc (solver, agent, GNUNET_NO);
1285 case RIL_ACTION_BW_OUT_DEC:
1286 envi_action_bw_dec (solver, agent, GNUNET_NO);
1289 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1291 address_index = action - RIL_ACTION_TYPE_NUM;
1293 GNUNET_assert(address_index >= 0);
1295 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1297 envi_action_address_switch (solver, agent, address_index);
1300 // error - action does not exist
1301 GNUNET_assert(GNUNET_NO);
1306 * Selects the next action using the e-greedy strategy. I.e. with a probability
1307 * of (1-e) the action with the maximum expected return will be chosen
1308 * (=> exploitation) and with probability (e) a random action will be chosen.
1309 * In case the Q-learning rule is set, the function also resets the eligibility
1310 * traces in the exploration case (after Watkin's Q-learning).
1312 * @param agent the agent selecting an action
1313 * @param state the current state-feature vector
1314 * @return the action index
1317 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1320 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1321 UINT32_MAX) / (double) UINT32_MAX;
1323 if (r < agent->envi->parameters.epsilon) //explore
1325 action = agent_get_action_random(agent);
1326 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1328 agent->eligibility_reset = GNUNET_YES;
1330 agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
1335 action = agent_get_action_max(agent, state);
1341 * Selects the next action with a probability corresponding to its value. The
1342 * probability is calculated using a Boltzmann distribution with a temperature
1343 * value. The higher the temperature, the more are the action selection
1344 * probabilities the same. With a temperature of 0, the selection is greedy,
1345 * i.e. always the action with the highest value is chosen.
1351 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1355 double eqt[agent->n];
1360 a_max = agent_get_action_max(agent, state);
1362 for (i=0; i<agent->n; i++)
1364 if (agent_action_is_possible(agent, i))
1366 eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1368 eqt[i] = isinf(eqt[i]) * UINT32_MAX;
1372 for (i=0; i<agent->n; i++)
1374 if (agent_action_is_possible(agent, i))
1383 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1384 UINT32_MAX) / (double) UINT32_MAX;
1386 for (i=0; i<agent->n; i++)
1392 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1393 agent->eligibility_reset = GNUNET_YES;
1394 agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay;
1400 GNUNET_assert(GNUNET_NO);
1401 return RIL_ACTION_INVALID;
1405 * Select the next action of an agent either according to the e-greedy strategy
1406 * or the softmax strategy.
1408 * @param agent the agent in question
1409 * @param state the current state-feature vector
1410 * @return the action index
1413 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1415 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1417 return agent_select_egreedy(agent, state);
1421 return agent_select_softmax(agent, state);
1426 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1427 * after having done the last action a_old. It observes the new state s_next and the reward
1428 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1429 * next action is put into effect.
1431 * @param agent the agent performing the step
1434 agent_step (struct RIL_Peer_Agent *agent)
1436 int a_next = RIL_ACTION_INVALID;
1441 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1442 GNUNET_i2s (&agent->peer),
1443 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1445 s_next = envi_get_state (agent->envi, agent);
1446 reward = envi_get_reward (agent->envi, agent);
1448 if (agent->eligibility_reset)
1450 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1451 agent->eligibility_reset = GNUNET_NO;
1455 agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
1457 if (RIL_ACTION_INVALID != agent->a_old)
1459 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1462 switch (agent->envi->parameters.algorithm)
1464 case RIL_ALGO_SARSA:
1465 a_next = agent_select_action (agent, s_next);
1466 if (RIL_ACTION_INVALID != agent->a_old)
1468 //updates weights with selected action (on-policy), if not first step
1469 agent_update (agent, reward, s_next, a_next);
1474 a_max = agent_get_action_max (agent, s_next);
1475 if (RIL_ACTION_INVALID != agent->a_old)
1477 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1478 agent_update (agent, reward, s_next, a_max);
1480 a_next = agent_select_action (agent, s_next);
1484 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1486 LOG (GNUNET_ERROR_TYPE_DEBUG, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1493 envi_do_action (agent->envi, agent, a_next);
1495 GNUNET_free(agent->s_old);
1496 agent->s_old = s_next;
1497 agent->a_old = a_next;
1499 agent->step_count += 1;
1503 * Prototype of the ril_step() procedure
1505 * @param solver the solver handle
1508 ril_step (struct GAS_RIL_Handle *solver);
1512 * Task for the scheduler, which performs one step and lets the solver know that
1513 * no further step is scheduled.
1515 * @param cls the solver handle
1516 * @param tc the task context for the scheduler
1519 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1521 struct GAS_RIL_Handle *solver = cls;
1523 solver->step_next_task_id = NULL;
1528 * Determines how much of the available bandwidth is assigned. If more is
1529 * assigned than available it returns 1. The function is used to determine the
1530 * step size of the adaptive stepping.
1532 * @param solver the solver handle
1536 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1539 struct RIL_Scope net;
1540 unsigned long long sum_assigned = 0;
1541 unsigned long long sum_available = 0;
1544 for (i = 0; i < solver->networks_count; i++)
1546 net = solver->network_entries[i];
1547 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1549 sum_assigned += net.bw_in_utilized;
1550 sum_assigned += net.bw_out_utilized;
1551 sum_available += net.bw_in_available;
1552 sum_available += net.bw_out_available;
1555 if (sum_available > 0)
1557 ratio = ((double) sum_assigned) / ((double) sum_available);
1564 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1568 * Lookup network struct by type
1570 * @param s the solver handle
1571 * @param type the network type
1572 * @return the network struct
1574 static struct RIL_Scope *
1575 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1579 for (i = 0; i < s->networks_count; i++)
1581 if (s->network_entries[i].type == type)
1583 return &s->network_entries[i];
1590 * Determines whether more connections are allocated in a network scope, than
1591 * they would theoretically fit. This is used as a heuristic to determine,
1592 * whether a new connection can be allocated or not.
1594 * @param solver the solver handle
1595 * @param network the network scope in question
1596 * @return GNUNET_YES if there are theoretically enough resources left
1599 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1601 struct RIL_Scope *net;
1602 struct RIL_Peer_Agent *agent;
1603 unsigned long long address_count = 0;
1605 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1607 if (agent->address_inuse && agent->is_active)
1609 net = agent->address_inuse->solver_information;
1610 if (net->type == network)
1617 net = ril_get_network (solver, network);
1618 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1622 * Unblocks an agent for which a connection request is there, that could not
1623 * be satisfied. Iterates over the addresses of the agent, if one of its
1624 * addresses can now be allocated in its scope the agent is unblocked,
1625 * otherwise it remains unchanged.
1627 * @param solver the solver handle
1628 * @param agent the agent in question
1632 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1634 struct RIL_Address_Wrapped *addr_wrap;
1635 struct RIL_Scope *net;
1636 unsigned long long start_in;
1637 unsigned long long start_out;
1639 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1641 net = addr_wrap->address_naked->solver_information;
1642 if (ril_network_is_not_full(solver, net->type))
1644 if (NULL == agent->address_inuse)
1646 start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
1647 start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
1648 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, start_in, start_out, silent);
1653 agent->address_inuse = NULL;
1657 * Determines how much the reward needs to be discounted depending on the amount
1658 * of time, which has passed since the last time-step.
1660 * @param solver the solver handle
1663 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1665 struct GNUNET_TIME_Absolute time_now;
1666 struct GNUNET_TIME_Relative time_delta;
1669 // MDP case only for debugging purposes
1670 if (solver->simulate)
1672 solver->global_discount_variable = solver->parameters.gamma;
1673 solver->global_discount_integrated = 1;
1679 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1680 time_now = GNUNET_TIME_absolute_get ();
1681 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1682 solver->step_time_last = time_now;
1683 tau = (double) time_delta.rel_value_us
1684 / (double) solver->parameters.step_time_min.rel_value_us;
1686 //calculate reward discounts (once per step for all agents)
1687 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1688 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1689 / (double) solver->parameters.beta;
1693 * Count the number of active agents/connections in a network scope
1695 * @param solver the solver handle
1696 * @param scope the network scope in question
1697 * @return the number of allocated connections
1700 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1703 struct RIL_Peer_Agent *cur_agent;
1705 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1707 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1716 * Calculates how much bandwidth is assigned in sum in a network scope, either
1717 * in the inbound or in the outbound direction.
1719 * @param solver the solver handle
1720 * @param type the type of the network scope in question
1721 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1722 * otherwise the outbound direction will be summed up
1723 * @return the sum of the assigned bandwidths
1725 static unsigned long long
1726 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1728 struct RIL_Peer_Agent *cur;
1729 struct RIL_Scope *net;
1730 unsigned long long sum = 0;
1732 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1734 if (cur->is_active && cur->address_inuse)
1736 net = cur->address_inuse->solver_information;
1737 if (net->type == type)
1751 * Calculates how much bandwidth is actually utilized in sum in a network scope,
1752 * either in the inbound or in the outbound direction.
1754 * @param solver the solver handle
1755 * @param type the type of the network scope in question
1756 * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1757 * otherwise the outbound direction will be summed up
1758 * @return the sum of the utilized bandwidths (in bytes/second)
1760 static unsigned long long
1761 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1763 struct RIL_Peer_Agent *cur;
1764 struct RIL_Scope *net;
1765 unsigned long long sum = 0;
1767 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1769 if (cur->is_active && cur->address_inuse)
1771 net = cur->address_inuse->solver_information;
1772 if (net->type == type)
1775 sum += cur->address_inuse->norm_utilization_in.norm;
1777 sum += cur->address_inuse->norm_utilization_out.norm;
1786 * Retrieves the state of the network scope, so that its attributes are up-to-
1789 * @param solver the solver handle
1792 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1795 struct RIL_Scope *net;
1797 for (c = 0; c < solver->networks_count; c++)
1799 net = &solver->network_entries[c];
1800 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1801 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1802 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1803 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1804 net->active_agent_count = ril_network_count_active_agents(solver, net);
1805 net->social_welfare = ril_network_get_social_welfare(solver, net);
1810 * Schedules the next global step in an adaptive way. The more resources are
1811 * left, the earlier the next step is scheduled. This serves the reactivity of
1812 * the solver to changed inputs.
1814 * @param solver the solver handle
1817 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1823 struct GNUNET_TIME_Relative time_next;
1825 used_ratio = ril_get_used_resource_ratio (solver);
1828 solver->parameters.step_time_min.rel_value_us
1829 <= solver->parameters.step_time_max.rel_value_us);
1831 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1832 solver->parameters.step_time_min).rel_value_us;
1833 offset = (double) solver->parameters.step_time_min.rel_value_us;
1834 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1836 GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
1837 GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
1839 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1841 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1847 if (solver->simulate)
1849 time_next = GNUNET_TIME_UNIT_ZERO;
1852 if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1854 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1860 * Triggers one step per agent
1865 ril_step (struct GAS_RIL_Handle *solver)
1867 struct RIL_Peer_Agent *cur;
1869 if (GNUNET_YES == solver->bulk_lock)
1871 solver->bulk_changes++;
1875 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1877 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1879 if (0 == solver->step_count)
1881 solver->step_time_last = GNUNET_TIME_absolute_get ();
1884 ril_calculate_discount (solver);
1885 ril_networks_update_state (solver);
1887 //trigger one step per active, unblocked agent
1888 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1892 if (NULL == cur->address_inuse)
1894 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1896 if (cur->address_inuse)
1903 ril_networks_update_state (solver);
1905 solver->step_count++;
1906 ril_step_schedule_next (solver);
1908 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1910 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1911 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1913 if (cur->suggestion_issue) {
1914 solver->env->bandwidth_changed_cb (solver->env->cls,
1915 cur->suggestion_address);
1916 cur->suggestion_issue = GNUNET_NO;
1919 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1923 * Initializes the matrix W of parameter vectors theta with small random numbers.
1925 * @param agent The respective agent
1928 agent_w_init (struct RIL_Peer_Agent *agent)
1933 for (i = 0; i < agent->n; i++)
1935 for (k = 0; k < agent->m; k++)
1937 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));
1943 * Initialize an agent without addresses and its knowledge base
1945 * @param s ril solver
1946 * @param peer the one in question
1947 * @return handle to the new agent
1949 static struct RIL_Peer_Agent *
1950 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1953 struct GAS_RIL_Handle * solver = s;
1954 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1956 agent->envi = solver;
1957 agent->peer = *peer;
1958 agent->step_count = 0;
1959 agent->is_active = GNUNET_NO;
1960 agent->bw_in = RIL_MIN_BW;
1961 agent->bw_out = RIL_MIN_BW;
1962 agent->suggestion_issue = GNUNET_NO;
1963 agent->n = RIL_ACTION_TYPE_NUM;
1965 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1966 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1967 for (i = 0; i < agent->n; i++)
1969 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1970 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1972 agent_w_init(agent);
1973 agent->eligibility_reset = GNUNET_NO;
1974 agent->a_old = RIL_ACTION_INVALID;
1975 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1976 agent->address_inuse = NULL;
1977 agent->objective_old = 0;
1978 agent->nop_bonus = 0;
1986 * @param solver the solver handle
1987 * @param agent the agent to retire
1990 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1994 for (i = 0; i < agent->n; i++)
1996 GNUNET_free_non_null(agent->W[i]);
1997 GNUNET_free_non_null(agent->E[i]);
1999 GNUNET_free_non_null(agent->W);
2000 GNUNET_free_non_null(agent->E);
2001 GNUNET_free_non_null(agent->s_old);
2006 * Returns the agent for a peer
2008 * @param solver the solver handle
2009 * @param peer the identity of the peer
2010 * @param create whether or not to create an agent, if none is allocated yet
2013 static struct RIL_Peer_Agent *
2014 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
2016 struct RIL_Peer_Agent *cur;
2018 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2020 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
2028 cur = agent_init (solver, peer);
2029 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
2036 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
2037 * considered inactive and not used. Addresses in an inactive network are ignored.
2039 * @param solver solver handle
2040 * @param network the network type
2041 * @return whether or not the network is considered active
2044 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
2046 struct RIL_Scope *net;
2048 net = ril_get_network (solver, network);
2049 return net->bw_out_available >= RIL_MIN_BW;
2053 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
2054 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
2055 * frees the memory of the old vector, and redirects the pointer to the new one.
2057 * @param old pointer to the pointer to the first element of the vector
2058 * @param element_size byte size of the vector elements
2059 * @param hole_start the first element to cut out
2060 * @param hole_length the number of elements to cut out
2061 * @param old_length the length of the old vector
2064 ril_cut_from_vector (void **old,
2065 size_t element_size,
2066 unsigned int hole_start,
2067 unsigned int hole_length,
2068 unsigned int old_length)
2071 char *oldptr = (char *) *old;
2073 unsigned int bytes_before;
2074 unsigned int bytes_hole;
2075 unsigned int bytes_after;
2077 GNUNET_assert(old_length >= hole_length);
2078 GNUNET_assert(old_length >= (hole_start + hole_length));
2080 size = element_size * (old_length - hole_length);
2082 bytes_before = element_size * hole_start;
2083 bytes_hole = element_size * hole_length;
2084 bytes_after = element_size * (old_length - hole_start - hole_length);
2092 tmpptr = GNUNET_malloc (size);
2093 memcpy (tmpptr, oldptr, bytes_before);
2094 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2100 *old = (void *) tmpptr;
2104 * Solver API functions
2105 * ---------------------------
2109 * Change relative preference for quality in solver
2111 * @param solver the solver handle
2112 * @param peer the peer to change the preference for
2113 * @param kind the kind to change the preference
2114 * @param pref_rel the normalized preference value for this kind over all clients
2117 GAS_ril_address_change_preference (void *solver,
2118 const struct GNUNET_PeerIdentity *peer,
2119 enum GNUNET_ATS_PreferenceKind kind,
2122 LOG(GNUNET_ERROR_TYPE_DEBUG,
2123 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2124 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2126 struct GAS_RIL_Handle *s = solver;
2128 s->parameters.temperature = s->parameters.temperature_init;
2129 s->parameters.epsilon = s->parameters.epsilon_init;
2135 * Add a new address for a peer to the solver
2137 * The address is already contained in the addresses hashmap!
2139 * @param solver the solver Handle
2140 * @param address the address to add
2141 * @param network network type of this address
2144 GAS_ril_address_add (void *solver,
2145 struct ATS_Address *address,
2148 struct GAS_RIL_Handle *s = solver;
2149 struct RIL_Peer_Agent *agent;
2150 struct RIL_Address_Wrapped *address_wrapped;
2151 struct RIL_Scope *net;
2159 LOG (GNUNET_ERROR_TYPE_DEBUG,
2160 "API_address_add()\n");
2162 net = ril_get_network (s, network);
2163 address->solver_information = net;
2165 if (!ril_network_is_active (s, network))
2167 LOG(GNUNET_ERROR_TYPE_DEBUG,
2168 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2169 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2173 s->parameters.temperature = s->parameters.temperature_init;
2174 s->parameters.epsilon = s->parameters.epsilon_init;
2176 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2179 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2180 address_wrapped->address_naked = address;
2181 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2183 //increase size of W
2184 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2186 n_new = agent->n + 1;
2189 GNUNET_array_grow(agent->W, agent->n, n_new);
2191 GNUNET_array_grow(agent->E, agent->n, n_new);
2192 for (i = 0; i < n_new; i++)
2197 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2199 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2204 GNUNET_array_grow(agent->W[i], zero, m_new);
2206 GNUNET_array_grow(agent->E[i], zero, m_new);
2210 //increase size of old state vector
2212 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2214 ril_try_unblock_agent(s, agent, GNUNET_NO);
2218 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2219 address->active ? "active" : "inactive", address->plugin, address->addr,
2220 GNUNET_i2s (&address->peer));
2224 * Delete an address in the solver
2226 * The address is not contained in the address hashmap anymore!
2228 * @param solver the solver handle
2229 * @param address the address to remove
2232 GAS_ril_address_delete (void *solver,
2233 struct ATS_Address *address)
2235 struct GAS_RIL_Handle *s = solver;
2236 struct RIL_Peer_Agent *agent;
2237 struct RIL_Address_Wrapped *address_wrapped;
2242 struct RIL_Scope *net;
2244 LOG (GNUNET_ERROR_TYPE_DEBUG,
2245 "API_address_delete() Delete %s %s address %s for peer '%s'\n",
2246 address->active ? "active" : "inactive",
2249 GNUNET_i2s (&address->peer));
2251 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2254 net = address->solver_information;
2255 GNUNET_assert(! ril_network_is_active (s, net->type));
2256 LOG (GNUNET_ERROR_TYPE_DEBUG,
2257 "No agent allocated for peer yet, since address was in inactive network\n");
2261 s->parameters.temperature = s->parameters.temperature_init;
2262 s->parameters.epsilon = s->parameters.epsilon_init;
2264 address_index = agent_address_get_index (agent, address);
2265 address_wrapped = agent_address_get_wrapped (agent, address);
2267 if (NULL == address_wrapped)
2269 net = address->solver_information;
2270 LOG (GNUNET_ERROR_TYPE_DEBUG,
2271 "Address not considered by agent, address was in inactive network\n");
2274 GNUNET_CONTAINER_DLL_remove (agent->addresses_head,
2275 agent->addresses_tail,
2277 GNUNET_free (address_wrapped);
2280 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2281 n_new = agent->n - 1;
2283 for (i = 0; i < agent->n; i++)
2285 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2286 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2287 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2288 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2289 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2290 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2292 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2293 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2294 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2296 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2298 //correct last action
2299 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2303 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2305 agent->a_old = RIL_ACTION_INVALID;
2307 //decrease old state vector
2308 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2309 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2310 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2314 if (agent->address_inuse == address)
2316 if (NULL != agent->addresses_head) //if peer has an address left, use it
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2319 "Active address died, suggesting alternative!\n");
2320 envi_set_active_suggestion (s,
2322 agent->addresses_head->address_naked,
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2330 "Active address died, suggesting disconnect!\n");
2331 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2335 if (agent->suggestion_address == address)
2337 agent->suggestion_issue = GNUNET_NO;
2338 agent->suggestion_address = NULL;
2340 GNUNET_assert (agent->address_inuse != address);
2345 * Update the properties of an address in the solver
2347 * @param solver solver handle
2348 * @param address the address
2351 GAS_ril_address_property_changed (void *solver,
2352 struct ATS_Address *address)
2354 struct GAS_RIL_Handle *s = solver;
2356 LOG(GNUNET_ERROR_TYPE_DEBUG,
2357 "Properties for peer '%s' address changed\n",
2358 GNUNET_i2s (&address->peer));
2359 s->parameters.temperature = s->parameters.temperature_init;
2360 s->parameters.epsilon = s->parameters.epsilon_init;
2366 * Give feedback about the current assignment
2368 * @param solver the solver handle
2369 * @param application the application
2370 * @param peer the peer to change the preference for
2371 * @param scope the time interval for this feedback: [now - scope .. now]
2372 * @param kind the kind to change the preference
2373 * @param score the score
2376 GAS_ril_address_preference_feedback (void *solver,
2377 struct GNUNET_SERVER_Client *application,
2378 const struct GNUNET_PeerIdentity *peer,
2379 const struct GNUNET_TIME_Relative scope,
2380 enum GNUNET_ATS_PreferenceKind kind,
2383 LOG (GNUNET_ERROR_TYPE_DEBUG,
2384 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2385 "preference %s for %d seconds\n",
2388 GNUNET_ATS_print_preference_type (kind),
2389 scope.rel_value_us / 1000000);
2394 * Start a bulk operation
2396 * @param solver the solver
2399 GAS_ril_bulk_start (void *solver)
2401 struct GAS_RIL_Handle *s = solver;
2403 LOG (GNUNET_ERROR_TYPE_DEBUG,
2404 "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2411 * Bulk operation done
2413 * @param solver the solver handle
2416 GAS_ril_bulk_stop (void *solver)
2418 struct GAS_RIL_Handle *s = solver;
2420 LOG(GNUNET_ERROR_TYPE_DEBUG,
2421 "API_bulk_stop() lock: %d\n",
2424 if (s->bulk_lock < 1)
2431 if (0 < s->bulk_changes)
2434 s->bulk_changes = 0;
2440 * Tell solver to notify ATS if the address to use changes for a specific
2441 * peer using the bandwidth changed callback
2443 * The solver must only notify about changes for peers with pending address
2446 * @param solver the solver handle
2447 * @param peer the identity of the peer
2450 GAS_ril_get_preferred_address (void *solver,
2451 const struct GNUNET_PeerIdentity *peer)
2453 struct GAS_RIL_Handle *s = solver;
2454 struct RIL_Peer_Agent *agent;
2456 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2458 agent = ril_get_agent (s, peer, GNUNET_YES);
2460 agent->is_active = GNUNET_YES;
2461 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2463 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2465 if (agent->address_inuse)
2467 LOG(GNUNET_ERROR_TYPE_DEBUG,
2468 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2469 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2473 LOG(GNUNET_ERROR_TYPE_DEBUG,
2474 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2476 s->parameters.temperature = s->parameters.temperature_init;
2477 s->parameters.epsilon = s->parameters.epsilon_init;
2479 if (NULL != agent->address_inuse)
2480 s->env->bandwidth_changed_cb (s->env->cls,
2481 agent->address_inuse);
2486 * Tell solver stop notifying ATS about changes for this peers
2488 * The solver must only notify about changes for peers with pending address
2491 * @param solver the solver handle
2492 * @param peer the peer
2495 GAS_ril_stop_get_preferred_address (void *solver,
2496 const struct GNUNET_PeerIdentity *peer)
2498 struct GAS_RIL_Handle *s = solver;
2499 struct RIL_Peer_Agent *agent;
2501 LOG (GNUNET_ERROR_TYPE_DEBUG,
2502 "API_stop_get_preferred_address()");
2504 agent = ril_get_agent (s, peer, GNUNET_NO);
2511 if (GNUNET_NO == agent->is_active)
2517 s->parameters.temperature = s->parameters.temperature_init;
2518 s->parameters.epsilon = s->parameters.epsilon_init;
2520 agent->is_active = GNUNET_NO;
2522 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2527 LOG(GNUNET_ERROR_TYPE_DEBUG,
2528 "API_stop_get_preferred_address() Paused agent for peer '%s'\n",
2534 * Entry point for the plugin
2536 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2539 libgnunet_plugin_ats_ril_init (void *cls)
2541 static struct GNUNET_ATS_SolverFunctions sf;
2542 struct GNUNET_ATS_PluginEnvironment *env = cls;
2543 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2544 struct RIL_Scope * cur;
2549 LOG (GNUNET_ERROR_TYPE_DEBUG,
2550 "API_init() Initializing RIL solver\n");
2552 GNUNET_assert (NULL != env);
2553 GNUNET_assert (NULL != env->cfg);
2554 GNUNET_assert (NULL != env->stats);
2555 GNUNET_assert (NULL != env->bandwidth_changed_cb);
2556 GNUNET_assert (NULL != env->get_preferences);
2558 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2560 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2564 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2565 &solver->parameters.step_time_min))
2567 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2571 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2572 &solver->parameters.step_time_max))
2574 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2577 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2579 GNUNET_STRINGS_utf8_toupper (string, string);
2580 if (0 == strcmp (string, "SARSA"))
2582 solver->parameters.algorithm = RIL_ALGO_SARSA;
2584 if (0 == strcmp (string, "Q-LEARNING"))
2586 solver->parameters.algorithm = RIL_ALGO_Q;
2589 GNUNET_free (string);
2593 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2596 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2598 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2599 GNUNET_free (string);
2603 solver->parameters.select = RIL_DEFAULT_SELECT;
2607 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2608 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2609 "RIL_DISCOUNT_BETA", &f_tmp))
2613 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2614 "RIL_DISCOUNT_BETA", f_tmp);
2618 solver->parameters.beta = f_tmp;
2619 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2620 "RIL_DISCOUNT_BETA", f_tmp);
2624 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2625 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2626 "RIL_DISCOUNT_GAMMA", &f_tmp))
2628 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2630 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2631 "RIL_DISCOUNT_GAMMA", f_tmp);
2635 solver->parameters.gamma = f_tmp;
2636 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2637 "RIL_DISCOUNT_GAMMA", f_tmp);
2641 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2642 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2643 "RIL_GRADIENT_STEP_SIZE", &f_tmp))
2645 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2647 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2648 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2652 solver->parameters.alpha = f_tmp;
2653 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2654 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2658 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2659 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2660 "RIL_TRACE_DECAY", &f_tmp))
2662 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2664 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2665 "RIL_TRACE_DECAY", f_tmp);
2669 solver->parameters.lambda = f_tmp;
2670 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2671 "RIL_TRACE_DECAY", f_tmp);
2675 solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
2676 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2677 "RIL_EXPLORE_RATIO", &f_tmp))
2679 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2681 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2682 "RIL_EXPLORE_RATIO", f_tmp);
2686 solver->parameters.epsilon_init = f_tmp;
2687 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2688 "RIL_EXPLORE_RATIO", f_tmp);
2692 solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
2693 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2694 "RIL_EXPLORE_DECAY", &f_tmp))
2696 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2698 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2699 "RIL_EXPLORE_DECAY", f_tmp);
2703 solver->parameters.epsilon_decay = f_tmp;
2704 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2705 "RIL_EXPLORE_DECAY", f_tmp);
2709 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2710 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2711 "RIL_TEMPERATURE", &f_tmp))
2715 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2716 "RIL_TEMPERATURE", f_tmp);
2720 solver->parameters.temperature_init = f_tmp;
2721 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2722 "RIL_TEMPERATURE", f_tmp);
2726 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
2727 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2728 "RIL_TEMPERATURE_DECAY", &f_tmp))
2730 if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1)
2732 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2733 "RIL_TEMPERATURE_DECAY", f_tmp);
2737 solver->parameters.temperature_decay = f_tmp;
2738 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2739 "RIL_TEMPERATURE_DECAY", f_tmp);
2743 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2745 solver->simulate = GNUNET_NO;
2748 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2750 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2754 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2757 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2759 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2760 GNUNET_free (string);
2764 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2769 sf.s_add = &GAS_ril_address_add;
2770 sf.s_address_update_property = &GAS_ril_address_property_changed;
2771 sf.s_get = &GAS_ril_get_preferred_address;
2772 sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2773 sf.s_pref = &GAS_ril_address_change_preference;
2774 sf.s_feedback = &GAS_ril_address_preference_feedback;
2775 sf.s_del = &GAS_ril_address_delete;
2776 sf.s_bulk_start = &GAS_ril_bulk_start;
2777 sf.s_bulk_stop = &GAS_ril_bulk_stop;
2779 solver->networks_count = env->network_count;
2780 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2781 solver->step_count = 0;
2782 solver->done = GNUNET_NO;
2784 for (c = 0; c < env->network_count; c++)
2786 cur = &solver->network_entries[c];
2788 cur->bw_in_available = env->in_quota[c];
2789 cur->bw_out_available = env->out_quota[c];
2790 LOG (GNUNET_ERROR_TYPE_DEBUG,
2791 "init() Quotas for %s network: IN %llu - OUT %llu\n",
2792 GNUNET_ATS_print_network_type(cur->type),
2793 cur->bw_in_available/1024,
2794 cur->bw_out_available/1024);
2797 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2798 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2799 solver->parameters.algorithm ? "Q" : "SARSA",
2800 solver->parameters.alpha,
2801 solver->parameters.beta,
2802 solver->parameters.lambda);
2803 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
2804 solver->parameters.epsilon,
2805 solver->parameters.temperature,
2806 solver->parameters.select ? "EGREEDY" : "SOFTMAX");
2807 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2808 solver->parameters.rbf_divisor);
2815 * Exit point for the plugin
2817 * @param cls the solver handle
2820 libgnunet_plugin_ats_ril_done (void *cls)
2822 struct GNUNET_ATS_SolverFunctions *sf = cls;
2823 struct GAS_RIL_Handle *s = sf->cls;
2824 struct RIL_Peer_Agent *cur_agent;
2825 struct RIL_Peer_Agent *next_agent;
2827 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2829 s->done = GNUNET_YES;
2831 cur_agent = s->agents_head;
2832 while (NULL != cur_agent)
2834 next_agent = cur_agent->next;
2835 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2836 agent_die (s, cur_agent);
2837 cur_agent = next_agent;
2840 if (NULL != s->step_next_task_id)
2842 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2844 GNUNET_free(s->network_entries);
2851 /* end of plugin_ats_ril.c */