2 This file is part of GNUnet.
3 (C) 2011 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
27 #include "plugin_ats_ril.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
31 #define MIN_BW ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)
33 #define RIL_ACTION_INVALID -1
34 #define RIL_INTERVAL_EXPONENT 10
35 #define RIL_UTILITY_MAX (double) GNUNET_ATS_MaxBandwidth
37 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
38 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
39 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
40 #define RIL_DEFAULT_SELECT RIL_SELECT_EGREEDY
41 #define RIL_DEFAULT_DISCOUNT_BETA 1.0
42 #define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
43 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.1
44 #define RIL_DEFAULT_TRACE_DECAY 0.5
45 #define RIL_DEFAULT_EXPLORE_RATIO 0.1
46 #define RIL_DEFAULT_DIVISOR 10
47 #define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5
48 #define RIL_DEFAULT_TEMPERATURE 1.0
50 #define RIL_INC_DEC_STEP_SIZE 1
53 * ATS reinforcement learning solver
59 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
60 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
61 * to a particular address. The action of switching to address with index i is depicted by the
62 * number (RIL_ACTION_TYPE_NUM + i).
66 RIL_ACTION_NOTHING = -1,
67 RIL_ACTION_BW_IN_DBL = -2, //TODO! put actions back
68 RIL_ACTION_BW_IN_HLV = -3,
69 RIL_ACTION_BW_IN_INC = 0,
70 RIL_ACTION_BW_IN_DEC = 1,
71 RIL_ACTION_BW_OUT_DBL = -4,
72 RIL_ACTION_BW_OUT_HLV = -5,
73 RIL_ACTION_BW_OUT_INC = -6,
74 RIL_ACTION_BW_OUT_DEC = -7,
75 RIL_ACTION_TYPE_NUM = 1
90 enum RIL_E_Modification
99 * Global learning parameters
101 struct RIL_Learning_Parameters
104 * The TD-algorithm to use
106 enum RIL_Algorithm algorithm;
109 * Gradient-descent step-size
114 * Learning discount variable in the TD-update for semi-MDPs
119 * Learning discount factor in the TD-update for MDPs
124 * Trace-decay factor for eligibility traces
129 * Softmax action-selection temperature
134 * State space divisor
136 unsigned long long int divisor;
139 * Action selection strategy;
141 enum RIL_Select select;
144 * Ratio, with what probability an agent should explore in the e-greed policy
146 double explore_ratio;
149 * How big the share of the global part of the reward signal is
151 double reward_global_share;
154 * Minimal interval time between steps in milliseconds
156 struct GNUNET_TIME_Relative step_time_min;
159 * Maximum interval time between steps in milliseconds
161 struct GNUNET_TIME_Relative step_time_max;
165 * Wrapper for addresses to store them in agent's linked list
167 struct RIL_Address_Wrapped
172 struct RIL_Address_Wrapped *next;
177 struct RIL_Address_Wrapped *prev;
182 struct ATS_Address *address_naked;
185 struct RIL_Peer_Agent
188 * Next agent in solver's linked list
190 struct RIL_Peer_Agent *next;
193 * Previous agent in solver's linked list
195 struct RIL_Peer_Agent *prev;
200 struct GAS_RIL_Handle *envi;
205 struct GNUNET_PeerIdentity peer;
208 * Whether the agent is active or not
213 * Number of performed time-steps
215 unsigned long long step_count;
218 * Experience matrix W
223 * Number of rows of W / Number of state-vector features
228 * Number of columns of W / Number of actions
233 * Last perceived state feature vector
243 * Eligibility trace vector
250 struct ATS_Address * address_inuse;
253 * Head of addresses DLL
255 struct RIL_Address_Wrapped * addresses_head;
258 * Tail of addresses DLL
260 struct RIL_Address_Wrapped * addresses_tail;
263 * Inbound bandwidth assigned by the agent
265 unsigned long long bw_in;
268 * Outbound bandwidth assigned by the agent
270 unsigned long long bw_out;
273 * Flag whether a suggestion has to be issued
275 int suggestion_issue;
278 * The address which has to be issued
280 struct ATS_Address * suggestion_address;
288 enum GNUNET_ATS_Network_Type type;
291 * Total available inbound bandwidth
293 unsigned long long bw_in_available;
296 * Bandwidth inbound assigned in network after last step
298 unsigned long long bw_in_assigned;
301 * Total available outbound bandwidth
303 unsigned long long bw_out_available;
306 * * Bandwidth outbound assigned in network after last step
308 unsigned long long bw_out_assigned;
312 * A handle for the reinforcement learning solver
314 struct GAS_RIL_Handle
317 * The solver-plugin environment of the solver-plugin API
319 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
324 struct GNUNET_STATISTICS_Handle *stats;
327 * Number of performed steps
329 unsigned long long step_count;
332 * Timestamp for the last time-step
334 struct GNUNET_TIME_Absolute step_time_last;
337 * Task identifier of the next time-step to be executed
339 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
342 * Variable discount factor, dependent on time between steps
344 double global_discount_variable;
347 * Integrated variable discount factor, dependent on time between steps
349 double global_discount_integrated;
352 * Lock for bulk operations
357 * Number of changes during a lock
362 * Learning parameters
364 struct RIL_Learning_Parameters parameters;
367 * Array of networks with global assignment state
369 struct RIL_Network * network_entries;
374 unsigned int networks_count;
377 * List of active peer-agents
379 struct RIL_Peer_Agent * agents_head;
380 struct RIL_Peer_Agent * agents_tail;
388 * Simulate steps, i.e. schedule steps immediately
390 unsigned long long simulate;
395 * ---------------------------
399 ril_count_agents(struct GAS_RIL_Handle * solver);
402 agent_get_utility (struct RIL_Peer_Agent *agent)
404 return (double) agent->bw_in;
408 * Estimate the current action-value for state s and action a
410 * @param agent agent performing the estimation
413 * @return estimation value
416 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
421 for (i = 0; i < agent->m; i++)
423 result += state[i] * agent->W[action][i];
426 GNUNET_assert(!isnan(result));
430 return isinf(result) * UINT32_MAX; //TODO! prevent crash when learning diverges
437 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
438 * currently estimated best action) in the current step
440 * @param agent agent performing the step
441 * @return yes, if exploring
444 agent_decide_exploration (struct RIL_Peer_Agent *agent)
446 //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
447 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
448 UINT32_MAX) / (double) UINT32_MAX;
450 if (r < agent->envi->parameters.explore_ratio)
459 * Get the index of the address in the agent's list.
461 * @param agent agent handle
462 * @param address address handle
463 * @return the index, starting with zero
466 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
469 struct RIL_Address_Wrapped *cur;
472 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
475 if (cur->address_naked == address)
483 * Gets the wrapped address from the agent's list
485 * @param agent agent handle
486 * @param address address handle
487 * @return wrapped address
489 static struct RIL_Address_Wrapped *
490 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
492 struct RIL_Address_Wrapped *cur;
494 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
495 if (cur->address_naked == address)
502 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
503 * most reward in the future)
505 * @param agent agent performing the calculation
506 * @param state the state from which to take the action
507 * @return the action promising most future reward
510 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
513 int max_i = RIL_ACTION_INVALID;
515 double max_q = -DBL_MAX;
517 for (i = 0; i < agent->n; i++)
519 cur_q = agent_estimate_q (agent, state, i);
527 GNUNET_assert(RIL_ACTION_INVALID != max_i);
534 * Gets any action, to explore the action space from that state
536 * @param agent agent performing the calculation
537 * @param state the state from which to take the action
541 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
543 // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
544 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
549 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
551 * @param agent the agent performing the update
552 * @param reward the reward received for the last action
553 * @param s_next the new state, the last step got the agent into
554 * @param a_prime the new
557 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
561 double *theta = agent->W[agent->a_old];
563 delta = agent->envi->global_discount_integrated * reward; //reward
564 delta += agent->envi->global_discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
565 delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
567 // 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",
568 // agent->step_count,
569 // agent_estimate_q (agent, agent->s_old, agent->a_old),
570 // agent->envi->parameters.alpha,
572 // agent->envi->global_discount_variable,
573 // agent_estimate_q (agent, s_next, a_prime),
576 for (i = 0; i < agent->m; i++)
578 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
579 // agent->envi->parameters.alpha,
583 theta[i] += agent->envi->parameters.alpha * delta * agent->s_old[i];// * agent->e[i];
589 * Changes the eligibility trace vector e in various manners:
590 * #RIL_E_ACCUMULATE - adds @a f to each component as in accumulating eligibility traces
591 * #RIL_E_REPLACE - resets each component to @a f as in replacing traces
592 * #RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
593 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
595 * @param agent the agent handle
596 * @param mod the kind of modification
597 * @param f how much to change
600 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
601 enum RIL_E_Modification mod,
605 double *e = agent->e;
607 for (i = 0; i < agent->m; i++)
611 case RIL_E_ACCUMULATE:
618 e[i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
629 ril_inform (struct GAS_RIL_Handle *solver,
630 enum GAS_Solver_Operation op,
631 enum GAS_Solver_Status stat)
633 if (NULL != solver->plugin_envi->info_cb)
634 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
639 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
640 * notify ATS of its new decision
642 * @param solver solver handle
643 * @param agent agent handle
644 * @param new_address the address which is to be used
645 * @param new_bw_in the new amount of inbound bandwidth set for this address
646 * @param new_bw_out the new amount of outbound bandwidth set for this address
647 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
650 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
651 struct RIL_Peer_Agent *agent,
652 struct ATS_Address *new_address,
653 unsigned long long new_bw_in,
654 unsigned long long new_bw_out,
657 int notify = GNUNET_NO;
659 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
662 if (agent->address_inuse != new_address)
664 if (NULL != agent->address_inuse)
666 agent->address_inuse->active = GNUNET_NO;
667 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
668 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
670 if (NULL != new_address)
672 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
673 new_address->active = agent->is_active;
674 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
675 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
677 notify |= GNUNET_YES;
683 if (new_address->active != agent->is_active)
685 new_address->active = agent->is_active;
686 notify |= GNUNET_YES;
690 if (agent->bw_in != new_bw_in)
692 agent->bw_in = new_bw_in;
693 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
694 notify |= GNUNET_YES;
696 if (agent->bw_out != new_bw_out)
698 agent->bw_out = new_bw_out;
699 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
700 notify |= GNUNET_YES;
704 if (notify && agent->is_active && (GNUNET_NO == silent))
708 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
709 agent->suggestion_issue = GNUNET_YES;
710 agent->suggestion_address = new_address;
712 else if (agent->address_inuse)
714 //disconnect case, no new address
715 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
716 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
720 agent->suggestion_issue = GNUNET_YES;
721 agent->suggestion_address = agent->address_inuse;
724 agent->address_inuse = new_address;
728 static unsigned long long
729 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
731 struct RIL_Peer_Agent *cur;
732 struct RIL_Network *net;
733 unsigned long long sum = 0;
735 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
737 if (cur->is_active && cur->address_inuse)
739 net = cur->address_inuse->solver_information;
740 if (net->type == type)
754 * Allocates a state vector and fills it with the features present
755 * @param solver the solver handle
756 * @param agent the agent handle
757 * @return pointer to the state vector
760 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
772 state = GNUNET_malloc (sizeof(agent->m));
774 y[0] = (double) agent->bw_out;
775 y[1] = (double) agent->bw_in;
777 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.divisor+1) * (solver->parameters.divisor+1);
778 for (i = 0; i <= solver->parameters.divisor; i++)
780 for (k = 0; k <= solver->parameters.divisor; k++)
782 x[0] = i * GNUNET_ATS_MaxBandwidth / solver->parameters.divisor;
783 x[1] = k * GNUNET_ATS_MaxBandwidth / solver->parameters.divisor;
786 sigma = ((double) GNUNET_ATS_MaxBandwidth / 2) * M_SQRT2;
787 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
796 // * For all networks a peer has an address in, this gets the maximum bandwidth which could
797 // * theoretically be available in one of the networks. This is used for bandwidth normalization.
799 // * @param agent the agent handle
800 // * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
802 //static unsigned long long
803 //ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
806 // * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
807 // * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
809 // unsigned long long max = 0;
810 // struct RIL_Address_Wrapped *cur;
811 // struct RIL_Network *net;
813 // for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
815 // net = cur->address_naked->solver_information;
818 // if (net->bw_in_available > max)
820 // max = net->bw_in_available;
825 // if (net->bw_out_available > max)
827 // max = net->bw_out_available;
835 // * Get the index of the quality-property in question
837 // * @param type the quality property type
838 // * @return the index
841 //ril_find_property_index (uint32_t type)
843 // int existing_types[] = GNUNET_ATS_QualityProperties;
845 // for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
846 // if (existing_types[c] == type)
848 // return GNUNET_SYSERR;
852 //ril_get_atsi (struct ATS_Address *address, uint32_t type)
855 // GNUNET_assert(NULL != address);
857 // if ((NULL == address->atsi) || (0 == address->atsi_count))
860 // for (c1 = 0; c1 < address->atsi_count; c1++)
862 // if (ntohl (address->atsi[c1].type) == type)
863 // return ntohl (address->atsi[c1].value);
869 //envi_reward_global (struct GAS_RIL_Handle *solver)
872 // struct RIL_Network net;
873 // unsigned int sum_in_available = 0;
874 // unsigned int sum_out_available = 0;
875 // unsigned int sum_in_assigned = 0;
876 // unsigned int sum_out_assigned = 0;
880 // for (i = 0; i < solver->networks_count; i++)
882 // net = solver->network_entries[i];
883 // sum_in_available += net.bw_in_available;
884 // sum_in_assigned += net.bw_in_assigned;
885 // sum_out_available += net.bw_out_available;
886 // sum_out_assigned += net.bw_out_assigned;
889 // ratio_in = ((double) sum_in_assigned) / ((double) sum_in_available);
890 // ratio_out = ((double) sum_out_assigned) / ((double) sum_out_available);
892 // // global reward in [1,2]
893 // return ratio_in +1;
894 // return ((ratio_in + ratio_out) / 2) + 1;
898 //envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
900 // const double *preferences;
901 // const double *properties;
903 // double pref_match = 0;
907 // preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
909 // properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
910 // agent->address_inuse);
913 // prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
914 // dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
916 // // utilization in [0,1]
917 // bw_norm = (((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
918 // / (double) ril_get_max_bw (agent, GNUNET_YES))
919 // + ((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
920 // / (double) ril_get_max_bw (agent, GNUNET_NO))) / 2;
922 // // preference matching in [0,4]
923 // pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
924 // pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
926 // // local reward in [1,2]
927 // return (pref_match / 4) +1;
931 envi_get_collective_utility (struct GAS_RIL_Handle *solver)
933 //TODO! add nash product
934 struct RIL_Peer_Agent *cur;
935 double result = RIL_UTILITY_MAX;
937 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
941 if (cur->address_inuse)
943 result = GNUNET_MIN(result, agent_get_utility(cur));
952 * Gets the reward for the last performed step, which is calculated in equal
953 * parts from the local (the peer specific) and the global (for all peers
956 * @param solver the solver handle
957 * @param agent the agent handle
961 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
963 struct RIL_Network *net;
965 unsigned long long objective;
967 net = agent->address_inuse->solver_information;
968 if (net->bw_in_assigned > net->bw_in_available)
970 objective = net->bw_in_available - net->bw_in_assigned;
974 objective = envi_get_collective_utility(solver);
981 * Doubles the bandwidth for the active address
983 * @param solver solver handle
984 * @param agent agent handle
985 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
988 envi_action_bw_double (struct GAS_RIL_Handle *solver,
989 struct RIL_Peer_Agent *agent,
992 unsigned long long new_bw;
996 new_bw = agent->bw_in * 2;
997 if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
998 new_bw = GNUNET_ATS_MaxBandwidth;
999 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1000 agent->bw_out, GNUNET_NO);
1004 new_bw = agent->bw_out * 2;
1005 if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth)
1006 new_bw = GNUNET_ATS_MaxBandwidth;
1007 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1013 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1014 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1016 * @param solver solver handle
1017 * @param agent agent handle
1018 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1022 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1023 struct RIL_Peer_Agent *agent,
1026 unsigned long long new_bw;
1030 new_bw = agent->bw_in / 2;
1031 if (new_bw < MIN_BW || new_bw > agent->bw_in)
1033 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1038 new_bw = agent->bw_out / 2;
1039 if (new_bw < MIN_BW || new_bw > agent->bw_out)
1041 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1047 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1049 * @param solver solver handle
1050 * @param agent agent handle
1051 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1055 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1057 unsigned long long new_bw;
1061 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1062 if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
1063 new_bw = GNUNET_ATS_MaxBandwidth;
1064 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1065 agent->bw_out, GNUNET_NO);
1069 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1070 if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth)
1071 new_bw = GNUNET_ATS_MaxBandwidth;
1072 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1078 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1079 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1081 * @param solver solver handle
1082 * @param agent agent handle
1083 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1087 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1089 unsigned long long new_bw;
1093 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1094 if (new_bw < MIN_BW || new_bw > agent->bw_in)
1096 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1101 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1102 if (new_bw < MIN_BW || new_bw > agent->bw_out)
1104 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1110 * Switches to the address given by its index
1112 * @param solver solver handle
1113 * @param agent agent handle
1114 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1117 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1118 struct RIL_Peer_Agent *agent,
1119 unsigned int address_index)
1121 struct RIL_Address_Wrapped *cur;
1124 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1126 if (i == address_index)
1128 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1136 //no address with address_index exists, in this case this action should not be callable
1137 GNUNET_assert(GNUNET_NO);
1141 * Puts the action into effect by calling the according function
1143 * @param solver the solver handle
1144 * @param agent the action handle
1145 * @param action the action to perform by the solver
1148 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1154 case RIL_ACTION_NOTHING:
1156 case RIL_ACTION_BW_IN_DBL:
1157 envi_action_bw_double (solver, agent, GNUNET_YES);
1159 case RIL_ACTION_BW_IN_HLV:
1160 envi_action_bw_halven (solver, agent, GNUNET_YES);
1162 case RIL_ACTION_BW_IN_INC:
1163 envi_action_bw_inc (solver, agent, GNUNET_YES);
1165 case RIL_ACTION_BW_IN_DEC:
1166 envi_action_bw_dec (solver, agent, GNUNET_YES);
1168 case RIL_ACTION_BW_OUT_DBL:
1169 envi_action_bw_double (solver, agent, GNUNET_NO);
1171 case RIL_ACTION_BW_OUT_HLV:
1172 envi_action_bw_halven (solver, agent, GNUNET_NO);
1174 case RIL_ACTION_BW_OUT_INC:
1175 envi_action_bw_inc (solver, agent, GNUNET_NO);
1177 case RIL_ACTION_BW_OUT_DEC:
1178 envi_action_bw_dec (solver, agent, GNUNET_NO);
1181 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1183 address_index = action - RIL_ACTION_TYPE_NUM;
1185 GNUNET_assert(address_index >= 0);
1187 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1189 envi_action_address_switch (solver, agent, address_index);
1192 // error - action does not exist
1193 GNUNET_assert(GNUNET_NO);
1198 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1200 if (agent_decide_exploration(agent))
1202 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1204 agent_modify_eligibility(agent, RIL_E_ZERO, NULL);
1206 return agent_get_action_explore(agent, state);
1210 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1212 agent_modify_eligibility(agent, RIL_E_SET, NULL);
1214 return agent_get_action_best(agent, state);
1219 * Selects the next action with a probability corresponding to its value. The
1220 * probability is calculated using a Boltzmann distribution with a temperature
1221 * value. The higher the temperature, the more are the action selection
1222 * probabilities the same. With a temperature of 0, the selection is greedy,
1223 * i.e. always the action with the highest value is chosen.
1229 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1232 double eqt[agent->n];
1237 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1239 agent_modify_eligibility(agent, RIL_E_SET, NULL);
1242 for (i=0; i<agent->n; i++)
1244 eqt[i] = exp(agent_estimate_q(agent,state,i) / agent->envi->parameters.temperature);
1247 for (i=0; i<agent->n; i++)
1251 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1252 UINT32_MAX) / (double) UINT32_MAX;
1254 for (i=0; i<agent->n; i++)
1262 GNUNET_assert(GNUNET_NO);
1266 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1268 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1270 return agent_select_egreedy(agent, state);
1274 return agent_select_softmax(agent, state);
1279 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1280 * after having done the last action a_old. It observes the new state s_next and the reward
1281 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1282 * next action is put into effect.
1284 * @param agent the agent performing the step
1287 agent_step (struct RIL_Peer_Agent *agent)
1289 int a_next = RIL_ACTION_INVALID;
1294 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1295 GNUNET_i2s (&agent->peer),
1296 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1298 s_next = envi_get_state (agent->envi, agent);
1299 reward = envi_get_reward (agent->envi, agent);
1301 switch (agent->envi->parameters.algorithm)
1303 case RIL_ALGO_SARSA:
1304 a_next = agent_select_action (agent, s_next);
1305 if (RIL_ACTION_INVALID != agent->a_old)
1307 //updates weights with selected action (on-policy), if not first step
1308 agent_update_weights (agent, reward, s_next, a_next);
1310 agent_modify_eligibility (agent, RIL_E_SET, s_next);
1314 a_max = agent_get_action_best (agent, s_next);
1315 if (RIL_ACTION_INVALID != agent->a_old)
1317 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1318 agent_update_weights (agent, reward, s_next, a_max);
1320 a_next = agent_select_action (agent, s_next);
1324 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1326 agent_modify_eligibility (agent, RIL_E_ACCUMULATE, s_next);
1328 // GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1329 // agent->step_count,
1331 // agent->bw_in/1024,
1332 // agent->bw_out/1024,
1335 envi_do_action (agent->envi, agent, a_next);
1337 GNUNET_free(agent->s_old);
1338 agent->s_old = s_next;
1339 agent->a_old = a_next;
1341 agent->step_count += 1;
1345 ril_step (struct GAS_RIL_Handle *solver);
1348 * Task for the scheduler, which performs one step and lets the solver know that
1349 * no further step is scheduled.
1351 * @param cls the solver handle
1352 * @param tc the task context for the scheduler
1355 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1357 struct GAS_RIL_Handle *solver = cls;
1359 solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1364 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1367 struct RIL_Network net;
1368 unsigned long long sum_assigned = 0;
1369 unsigned long long sum_available = 0;
1372 for (i = 0; i < solver->networks_count; i++)
1374 net = solver->network_entries[i];
1375 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1377 sum_assigned += net.bw_in_assigned;
1378 sum_assigned += net.bw_out_assigned;
1379 sum_available += net.bw_in_available;
1380 sum_available += net.bw_out_available;
1383 if (sum_available > 0)
1385 ratio = ((double) sum_assigned) / ((double) sum_available);
1392 return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
1396 * Lookup network struct by type
1398 * @param s the solver handle
1399 * @param type the network type
1400 * @return the network struct
1402 static struct RIL_Network *
1403 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1407 for (i = 0; i < s->networks_count; i++)
1409 if (s->network_entries[i].type == type)
1411 return &s->network_entries[i];
1418 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1420 struct RIL_Network *net;
1421 struct RIL_Peer_Agent *agent;
1422 unsigned long long address_count = 0;
1424 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1426 if (agent->address_inuse && agent->is_active)
1428 net = agent->address_inuse->solver_information;
1429 if (net->type == network)
1436 net = ril_get_network (solver, network);
1437 return (net->bw_in_available > MIN_BW * address_count) && (net->bw_out_available > MIN_BW * address_count);
1441 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1443 struct RIL_Address_Wrapped *addr_wrap;
1444 struct RIL_Network *net;
1446 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1448 net = addr_wrap->address_naked->solver_information;
1449 if (ril_network_is_not_full(solver, net->type))
1451 if (NULL == agent->address_inuse)
1452 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, MIN_BW, MIN_BW, silent);
1456 agent->address_inuse = NULL;
1460 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1462 struct GNUNET_TIME_Absolute time_now;
1463 struct GNUNET_TIME_Relative time_delta;
1466 // MDP case - remove when debugged
1467 if (solver->simulate)
1469 solver->global_discount_variable = solver->parameters.gamma;
1470 solver->global_discount_integrated = 1;
1476 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1477 time_now = GNUNET_TIME_absolute_get ();
1478 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1479 solver->step_time_last = time_now;
1480 tau = (double) time_delta.rel_value_us
1481 / (double) solver->parameters.step_time_min.rel_value_us;
1483 //calculate reward discounts (once per step for all agents)
1484 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1485 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1486 / (double) solver->parameters.beta;
1490 ril_calculate_assigned_bwnet (struct GAS_RIL_Handle *solver)
1493 struct RIL_Network *net;
1495 for (c = 0; c < solver->networks_count; c++)
1497 net = &solver->network_entries[c];
1498 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1499 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1504 * Schedules the next global step in an adaptive way. The more resources are
1505 * left, the earlier the next step is scheduled. This serves the reactivity of
1506 * the solver to changed inputs.
1508 * @param solver the solver handle
1511 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1517 struct GNUNET_TIME_Relative time_next;
1519 used_ratio = ril_get_used_resource_ratio (solver);
1522 solver->parameters.step_time_min.rel_value_us
1523 <= solver->parameters.step_time_max.rel_value_us);
1525 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1526 solver->parameters.step_time_min).rel_value_us;
1527 offset = (double) solver->parameters.step_time_min.rel_value_us;
1528 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1530 GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1531 GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1533 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1535 if (solver->simulate)
1537 time_next = GNUNET_TIME_UNIT_ZERO;
1540 if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1542 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1548 * Triggers one step per agent
1552 ril_step (struct GAS_RIL_Handle *solver)
1554 struct RIL_Peer_Agent *cur;
1556 if (GNUNET_YES == solver->bulk_lock)
1558 solver->bulk_changes++;
1562 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1564 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1566 if (0 == solver->step_count)
1568 solver->step_time_last = GNUNET_TIME_absolute_get ();
1571 ril_calculate_discount (solver);
1572 ril_calculate_assigned_bwnet (solver);
1574 //calculate network state vector
1575 // envi_state_networks(solver);
1577 //trigger one step per active, unblocked agent
1578 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1582 if (NULL == cur->address_inuse)
1584 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1586 if (cur->address_inuse)
1593 ril_calculate_assigned_bwnet (solver);
1595 solver->step_count += 1;
1596 ril_step_schedule_next (solver);
1598 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1600 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1601 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1603 if (cur->suggestion_issue) {
1604 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1605 cur->suggestion_issue = GNUNET_NO;
1608 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1612 ril_count_agents (struct GAS_RIL_Handle *solver)
1615 struct RIL_Peer_Agent *cur_agent;
1617 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1625 agent_w_start (struct RIL_Peer_Agent *agent)
1628 struct RIL_Peer_Agent *other;
1632 count = ril_count_agents(agent->envi);
1634 for (i = 0; i < agent->n; i++)
1636 for (k = 0; k < agent->m; k++)
1639 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));
1642 for (other = agent->envi->agents_head; NULL != other; other = other->next)
1644 agent->W[i][k] += (other->W[i][k] / (double) count);
1648 GNUNET_assert(!isinf(agent->W[i][k]));
1654 * Initialize an agent without addresses and its knowledge base
1656 * @param s ril solver
1657 * @param peer the one in question
1658 * @return handle to the new agent
1660 static struct RIL_Peer_Agent *
1661 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1664 struct GAS_RIL_Handle * solver = s;
1665 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1667 agent->envi = solver;
1668 agent->peer = *peer;
1669 agent->step_count = 0;
1670 agent->is_active = GNUNET_NO;
1671 agent->bw_in = MIN_BW;
1672 agent->bw_out = MIN_BW;
1673 agent->suggestion_issue = GNUNET_NO;
1674 agent->n = RIL_ACTION_TYPE_NUM;
1676 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1677 for (i = 0; i < agent->n; i++)
1679 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1681 agent_w_start(agent);
1682 agent->a_old = RIL_ACTION_INVALID;
1683 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1684 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1685 agent_modify_eligibility (agent, RIL_E_ZERO, NULL);
1693 * @param solver the solver handle
1694 * @param agent the agent to retire
1697 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1701 for (i = 0; i < agent->n; i++)
1703 GNUNET_free(agent->W[i]);
1705 GNUNET_free(agent->W);
1706 GNUNET_free(agent->e);
1707 GNUNET_free(agent->s_old);
1712 * Returns the agent for a peer
1714 * @param solver the solver handle
1715 * @param peer the identity of the peer
1716 * @param create whether or not to create an agent, if none is allocated yet
1719 static struct RIL_Peer_Agent *
1720 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1722 struct RIL_Peer_Agent *cur;
1724 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1726 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1734 cur = agent_init (solver, peer);
1735 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
1742 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1743 * considered inactive and not used. Addresses in an inactive network are ignored.
1745 * @param solver solver handle
1746 * @param network the network type
1747 * @return whether or not the network is considered active
1750 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1752 struct RIL_Network *net;
1754 net = ril_get_network (solver, network);
1755 return net->bw_out_available >= MIN_BW;
1759 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1760 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1761 * frees the memory of the old vector, and redirects the pointer to the new one.
1763 * @param old pointer to the pointer to the first element of the vector
1764 * @param element_size byte size of the vector elements
1765 * @param hole_start the first element to cut out
1766 * @param hole_length the number of elements to cut out
1767 * @param old_length the length of the old vector
1770 ril_cut_from_vector (void **old,
1771 size_t element_size,
1772 unsigned int hole_start,
1773 unsigned int hole_length,
1774 unsigned int old_length)
1777 char *oldptr = (char *) *old;
1779 unsigned int bytes_before;
1780 unsigned int bytes_hole;
1781 unsigned int bytes_after;
1783 GNUNET_assert(old_length > hole_length);
1784 GNUNET_assert(old_length >= (hole_start + hole_length));
1786 size = element_size * (old_length - hole_length);
1788 bytes_before = element_size * hole_start;
1789 bytes_hole = element_size * hole_length;
1790 bytes_after = element_size * (old_length - hole_start - hole_length);
1798 tmpptr = GNUNET_malloc (size);
1799 memcpy (tmpptr, oldptr, bytes_before);
1800 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1806 *old = (void *) tmpptr;
1810 * Solver API functions
1811 * ---------------------------
1815 * Change relative preference for quality in solver
1817 * @param solver the solver handle
1818 * @param peer the peer to change the preference for
1819 * @param kind the kind to change the preference
1820 * @param pref_rel the normalized preference value for this kind over all clients
1823 GAS_ril_address_change_preference (void *solver,
1824 const struct GNUNET_PeerIdentity *peer,
1825 enum GNUNET_ATS_PreferenceKind kind,
1828 LOG(GNUNET_ERROR_TYPE_DEBUG,
1829 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1830 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1836 * Entry point for the plugin
1838 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1841 libgnunet_plugin_ats_ril_init (void *cls)
1843 struct GNUNET_ATS_PluginEnvironment *env = cls;
1844 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1845 struct RIL_Network * cur;
1849 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1851 GNUNET_assert(NULL != env);
1852 GNUNET_assert(NULL != env->cfg);
1853 GNUNET_assert(NULL != env->stats);
1854 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1855 GNUNET_assert(NULL != env->get_preferences);
1856 GNUNET_assert(NULL != env->get_property);
1858 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_DIVISOR", &solver->parameters.divisor))
1860 solver->parameters.divisor = RIL_DEFAULT_DIVISOR;
1863 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
1864 &solver->parameters.step_time_min))
1866 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
1869 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
1870 &solver->parameters.step_time_max))
1872 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
1874 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
1876 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
1877 GNUNET_free (string);
1881 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1883 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
1885 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
1886 GNUNET_free (string);
1890 solver->parameters.select = RIL_DEFAULT_SELECT;
1892 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
1894 solver->parameters.beta = strtod (string, NULL);
1895 GNUNET_free (string);
1896 if (!(solver->parameters.beta > 0))
1898 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_DISCOUNT_BETA not configured as positive number. Set to default value of %f instead.\n", RIL_DEFAULT_DISCOUNT_BETA);
1899 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1904 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1906 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
1908 solver->parameters.gamma = strtod (string, NULL);
1909 GNUNET_free (string);
1910 if (!(solver->parameters.gamma < 1) || (solver->parameters.gamma < 0))
1912 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_DISCOUNT_GAMMA not configured in range [0,1[. Set to default value of %f instead.\n", RIL_DEFAULT_DISCOUNT_GAMMA);
1913 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
1918 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
1921 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
1923 solver->parameters.alpha = strtod (string, NULL);
1924 GNUNET_free (string);
1925 if (!(solver->parameters.alpha > 0) || solver->parameters.alpha > 1)
1927 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_GRADIENT_STEP_SIZE not configured in range ]0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_GRADIENT_STEP_SIZE);
1928 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1933 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1935 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
1937 solver->parameters.lambda = strtod (string, NULL);
1938 GNUNET_free (string);
1939 if (solver->parameters.lambda < 0 || solver->parameters.lambda > 1)
1941 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TRACE_DECAY not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_TRACE_DECAY);
1942 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1947 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1949 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
1951 solver->parameters.explore_ratio = strtod (string, NULL);
1952 GNUNET_free (string);
1953 if (solver->parameters.explore_ratio < 0 || solver->parameters.explore_ratio > 1)
1955 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_EXPLORE_RATIO not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_EXPLORE_RATIO);
1956 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
1961 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
1963 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
1965 solver->parameters.reward_global_share = strtod (string, NULL);
1966 GNUNET_free (string);
1967 if (solver->parameters.reward_global_share < 0 || solver->parameters.reward_global_share > 1)
1969 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_GLOBAL_REWARD_SHARE not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_GLOBAL_REWARD_SHARE);
1970 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
1975 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
1977 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE", &string))
1979 solver->parameters.temperature = strtod (string, NULL);
1980 GNUNET_free (string);
1981 if (solver->parameters.temperature <= 0)
1983 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE not positive. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE);
1984 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
1989 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
1991 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
1993 solver->simulate = GNUNET_NO;
1996 env->sf.s_add = &GAS_ril_address_add;
1997 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1998 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1999 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
2000 env->sf.s_address_update_network = &GAS_ril_address_change_network;
2001 env->sf.s_get = &GAS_ril_get_preferred_address;
2002 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2003 env->sf.s_pref = &GAS_ril_address_change_preference;
2004 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
2005 env->sf.s_del = &GAS_ril_address_delete;
2006 env->sf.s_bulk_start = &GAS_ril_bulk_start;
2007 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
2009 solver->plugin_envi = env;
2010 solver->networks_count = env->network_count;
2011 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
2012 solver->step_count = 0;
2013 solver->done = GNUNET_NO;
2015 for (c = 0; c < env->network_count; c++)
2017 cur = &solver->network_entries[c];
2018 cur->type = env->networks[c];
2019 cur->bw_in_available = env->in_quota[c];
2020 cur->bw_out_available = env->out_quota[c];
2021 LOG(GNUNET_ERROR_TYPE_INFO, "init() Quotas for %s network: IN %llu - OUT %llu\n", GNUNET_ATS_print_network_type(cur->type), cur->bw_in_available/1024, cur->bw_out_available/1024);
2024 LOG(GNUNET_ERROR_TYPE_INFO, "init() Parameters:\n");
2025 LOG(GNUNET_ERROR_TYPE_INFO, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2026 solver->parameters.algorithm ? "Q" : "SARSA",
2027 solver->parameters.alpha,
2028 solver->parameters.beta,
2029 solver->parameters.lambda);
2030 LOG(GNUNET_ERROR_TYPE_INFO, "init() explore = %f, global_share = %f\n",
2031 solver->parameters.explore_ratio,
2032 solver->parameters.reward_global_share);
2038 * Exit point for the plugin
2040 * @param cls the solver handle
2043 libgnunet_plugin_ats_ril_done (void *cls)
2045 struct GAS_RIL_Handle *s = cls;
2046 struct RIL_Peer_Agent *cur_agent;
2047 struct RIL_Peer_Agent *next_agent;
2049 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2051 s->done = GNUNET_YES;
2053 cur_agent = s->agents_head;
2054 while (NULL != cur_agent)
2056 next_agent = cur_agent->next;
2057 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2058 agent_die (s, cur_agent);
2059 cur_agent = next_agent;
2062 if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
2064 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2066 GNUNET_free(s->network_entries);
2073 * Add a new address for a peer to the solver
2075 * The address is already contained in the addresses hashmap!
2077 * @param solver the solver Handle
2078 * @param address the address to add
2079 * @param network network type of this address
2082 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
2084 struct GAS_RIL_Handle *s = solver;
2085 struct RIL_Peer_Agent *agent;
2086 struct RIL_Address_Wrapped *address_wrapped;
2087 struct RIL_Network *net;
2095 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
2097 net = ril_get_network (s, network);
2098 address->solver_information = net;
2100 if (!ril_network_is_active (s, network))
2102 LOG(GNUNET_ERROR_TYPE_DEBUG,
2103 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2104 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2108 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2111 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2112 address_wrapped->address_naked = address;
2113 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2115 //increase size of W
2116 m_new = agent->m + ((s->parameters.divisor+1) * (s->parameters.divisor+1));
2118 n_new = agent->n + 1;
2121 GNUNET_array_grow(agent->W, agent->n, n_new);
2122 for (i = 0; i < n_new; i++)
2127 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2132 GNUNET_array_grow(agent->W[i], zero, m_new);
2136 //increase size of old state vector
2138 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2141 GNUNET_array_grow(agent->e, agent->m, m_new);
2143 ril_try_unblock_agent(s, agent, GNUNET_NO);
2147 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2148 address->active ? "active" : "inactive", address->plugin, address->addr,
2149 GNUNET_i2s (&address->peer));
2153 * Delete an address in the solver
2155 * The address is not contained in the address hashmap anymore!
2157 * @param solver the solver handle
2158 * @param address the address to remove
2159 * @param session_only delete only session not whole address
2162 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
2164 struct GAS_RIL_Handle *s = solver;
2165 struct RIL_Peer_Agent *agent;
2166 struct RIL_Address_Wrapped *address_wrapped;
2167 int address_was_used = address->active;
2172 struct RIL_Network *net;
2174 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2175 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2176 address->addr, GNUNET_i2s (&address->peer));
2178 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2181 net = address->solver_information;
2182 GNUNET_assert(!ril_network_is_active (s, net->type));
2183 LOG(GNUNET_ERROR_TYPE_DEBUG,
2184 "No agent allocated for peer yet, since address was in inactive network\n");
2188 address_index = agent_address_get_index (agent, address);
2189 address_wrapped = agent_address_get (agent, address);
2191 if (NULL == address_wrapped)
2193 net = address->solver_information;
2194 GNUNET_assert(!ril_network_is_active (s, net->type));
2195 LOG(GNUNET_ERROR_TYPE_DEBUG,
2196 "Address not considered by agent, address was in inactive network\n");
2200 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2201 GNUNET_free(address_wrapped);
2204 m_new = agent->m - ((s->parameters.divisor+1) * (s->parameters.divisor+1));
2205 n_new = agent->n - 1;
2207 LOG(GNUNET_ERROR_TYPE_DEBUG, "first\n");
2209 for (i = 0; i < agent->n; i++)
2211 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2212 address_index * ((s->parameters.divisor+1) * (s->parameters.divisor+1)),
2213 ((s->parameters.divisor+1) * (s->parameters.divisor+1)), agent->m);
2215 GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2216 LOG(GNUNET_ERROR_TYPE_DEBUG, "second\n");
2217 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2219 //correct last action
2220 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2224 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2226 agent->a_old = RIL_ACTION_INVALID;
2228 //decrease old state vector and eligibility vector
2229 LOG(GNUNET_ERROR_TYPE_DEBUG, "third\n");
2230 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2231 address_index * ((s->parameters.divisor+1) * (s->parameters.divisor+1)),
2232 ((s->parameters.divisor+1) * (s->parameters.divisor+1)), agent->m);
2233 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
2234 address_index * ((s->parameters.divisor+1) * (s->parameters.divisor+1)),
2235 ((s->parameters.divisor+1) * (s->parameters.divisor+1)), agent->m);
2239 if (address_was_used)
2241 if (NULL != agent->addresses_head) //if peer has an address left, use it
2243 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, MIN_BW, MIN_BW,
2248 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2256 * Update the properties of an address in the solver
2258 * @param solver solver handle
2259 * @param address the address
2260 * @param type the ATSI type in HBO
2261 * @param abs_value the absolute value of the property
2262 * @param rel_value the normalized value
2265 GAS_ril_address_property_changed (void *solver,
2266 struct ATS_Address *address,
2271 LOG(GNUNET_ERROR_TYPE_DEBUG,
2272 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2273 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2274 address->addr, rel_value);
2280 * Update the session of an address in the solver
2282 * NOTE: values in addresses are already updated
2284 * @param solver solver handle
2285 * @param address the address
2286 * @param cur_session the current session
2287 * @param new_session the new session
2290 GAS_ril_address_session_changed (void *solver,
2291 struct ATS_Address *address,
2292 uint32_t cur_session,
2293 uint32_t new_session)
2296 * TODO? Future Work: Potentially add session activity as a feature in state vector
2298 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2302 * Notify the solver that an address is (not) actively used by transport
2303 * to communicate with a remote peer
2305 * NOTE: values in addresses are already updated
2307 * @param solver solver handle
2308 * @param address the address
2309 * @param in_use usage state
2312 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2315 * TODO? Future Work: Potentially add usage variable to state vector
2317 LOG(GNUNET_ERROR_TYPE_DEBUG,
2318 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2319 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2323 * Notify solver that the network an address is located in has changed
2325 * NOTE: values in addresses are already updated
2327 * @param solver solver handle
2328 * @param address the address
2329 * @param current_network the current network
2330 * @param new_network the new network
2333 GAS_ril_address_change_network (void *solver,
2334 struct ATS_Address *address,
2335 uint32_t current_network,
2336 uint32_t new_network)
2338 struct GAS_RIL_Handle *s = solver;
2339 struct RIL_Peer_Agent *agent;
2341 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2342 "%s address of peer %s from '%s' to '%s'\n",
2343 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2344 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2346 if (address->active && !ril_network_is_active (solver, new_network))
2348 GAS_ril_address_delete (solver, address, GNUNET_NO);
2352 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2355 GNUNET_assert(!ril_network_is_active (solver, current_network));
2357 GAS_ril_address_add (s, address, new_network);
2361 address->solver_information = ril_get_network(solver, new_network);
2365 * Give feedback about the current assignment
2367 * @param solver the solver handle
2368 * @param application the application
2369 * @param peer the peer to change the preference for
2370 * @param scope the time interval for this feedback: [now - scope .. now]
2371 * @param kind the kind to change the preference
2372 * @param score the score
2375 GAS_ril_address_preference_feedback (void *solver,
2377 const struct GNUNET_PeerIdentity *peer,
2378 const struct GNUNET_TIME_Relative scope,
2379 enum GNUNET_ATS_PreferenceKind kind,
2382 LOG(GNUNET_ERROR_TYPE_DEBUG,
2383 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2384 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2385 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2389 * Start a bulk operation
2391 * @param solver the solver
2394 GAS_ril_bulk_start (void *solver)
2396 struct GAS_RIL_Handle *s = solver;
2398 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2404 * Bulk operation done
2406 * @param solver the solver handle
2409 GAS_ril_bulk_stop (void *solver)
2411 struct GAS_RIL_Handle *s = solver;
2413 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2415 if (s->bulk_lock < 1)
2422 if (0 < s->bulk_changes)
2425 s->bulk_changes = 0;
2430 * Tell solver to notify ATS if the address to use changes for a specific
2431 * peer using the bandwidth changed callback
2433 * The solver must only notify about changes for peers with pending address
2436 * @param solver the solver handle
2437 * @param peer the identity of the peer
2439 const struct ATS_Address *
2440 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2443 * activate agent, return currently chosen address
2445 struct GAS_RIL_Handle *s = solver;
2446 struct RIL_Peer_Agent *agent;
2448 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2450 agent = ril_get_agent (s, peer, GNUNET_YES);
2452 agent->is_active = GNUNET_YES;
2453 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2455 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2457 if (agent->address_inuse)
2459 LOG(GNUNET_ERROR_TYPE_DEBUG,
2460 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2461 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2465 LOG(GNUNET_ERROR_TYPE_DEBUG,
2466 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2470 return agent->address_inuse;
2474 * Tell solver stop notifying ATS about changes for this peers
2476 * The solver must only notify about changes for peers with pending address
2479 * @param solver the solver handle
2480 * @param peer the peer
2483 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2485 struct GAS_RIL_Handle *s = solver;
2486 struct RIL_Peer_Agent *agent;
2488 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2490 agent = ril_get_agent (s, peer, GNUNET_NO);
2497 if (GNUNET_NO == agent->is_active)
2503 agent->is_active = GNUNET_NO;
2505 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2510 LOG(GNUNET_ERROR_TYPE_DEBUG,
2511 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2512 GNUNET_i2s (peer), agent->address_inuse->plugin);
2515 /* end of plugin_ats_ril.c */