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_RBF_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 = 0,
67 RIL_ACTION_BW_IN_DBL = -2, //TODO! put actions back
68 RIL_ACTION_BW_IN_HLV = -3,
69 RIL_ACTION_BW_IN_INC = 1,
70 RIL_ACTION_BW_IN_DEC = 2,
71 RIL_ACTION_BW_OUT_DBL = -4,
72 RIL_ACTION_BW_OUT_HLV = -5,
73 RIL_ACTION_BW_OUT_INC = 3,
74 RIL_ACTION_BW_OUT_DEC = 4,
75 RIL_ACTION_TYPE_NUM = 5
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 * Whether to accumulate or replace eligibility traces
131 enum RIL_E_Modification eligibility_trace_mode;
134 * Softmax action-selection temperature
139 * State space divisor
141 unsigned long long int divisor;
144 * Action selection strategy;
146 enum RIL_Select select;
149 * Ratio, with what probability an agent should explore in the e-greed policy
151 double explore_ratio;
154 * How big the share of the global part of the reward signal is
156 double reward_global_share;
159 * Minimal interval time between steps in milliseconds
161 struct GNUNET_TIME_Relative step_time_min;
164 * Maximum interval time between steps in milliseconds
166 struct GNUNET_TIME_Relative step_time_max;
170 * Wrapper for addresses to store them in agent's linked list
172 struct RIL_Address_Wrapped
177 struct RIL_Address_Wrapped *next;
182 struct RIL_Address_Wrapped *prev;
187 struct ATS_Address *address_naked;
190 struct RIL_Peer_Agent
193 * Next agent in solver's linked list
195 struct RIL_Peer_Agent *next;
198 * Previous agent in solver's linked list
200 struct RIL_Peer_Agent *prev;
205 struct GAS_RIL_Handle *envi;
210 struct GNUNET_PeerIdentity peer;
213 * Whether the agent is active or not
218 * Number of performed time-steps
220 unsigned long long step_count;
223 * Experience matrix W
228 * Number of rows of W / Number of state-vector features
233 * Number of columns of W / Number of actions
238 * Last perceived state feature vector
255 struct ATS_Address * address_inuse;
258 * Head of addresses DLL
260 struct RIL_Address_Wrapped * addresses_head;
263 * Tail of addresses DLL
265 struct RIL_Address_Wrapped * addresses_tail;
268 * Inbound bandwidth assigned by the agent
270 unsigned long long bw_in;
273 * Outbound bandwidth assigned by the agent
275 unsigned long long bw_out;
278 * Flag whether a suggestion has to be issued
280 int suggestion_issue;
283 * The address which has to be issued
285 struct ATS_Address * suggestion_address;
293 enum GNUNET_ATS_Network_Type type;
296 * Total available inbound bandwidth
298 unsigned long long bw_in_available;
301 * Bandwidth inbound assigned in network after last step
303 unsigned long long bw_in_assigned;
306 * Total available outbound bandwidth
308 unsigned long long bw_out_available;
311 * * Bandwidth outbound assigned in network after last step
313 unsigned long long bw_out_assigned;
317 * A handle for the reinforcement learning solver
319 struct GAS_RIL_Handle
322 * The solver-plugin environment of the solver-plugin API
324 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
329 struct GNUNET_STATISTICS_Handle *stats;
332 * Number of performed steps
334 unsigned long long step_count;
337 * Timestamp for the last time-step
339 struct GNUNET_TIME_Absolute step_time_last;
342 * Task identifier of the next time-step to be executed
344 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
347 * Variable discount factor, dependent on time between steps
349 double global_discount_variable;
352 * Integrated variable discount factor, dependent on time between steps
354 double global_discount_integrated;
357 * Lock for bulk operations
362 * Number of changes during a lock
367 * Learning parameters
369 struct RIL_Learning_Parameters parameters;
372 * Array of networks with global assignment state
374 struct RIL_Scope * network_entries;
379 unsigned int networks_count;
382 * List of active peer-agents
384 struct RIL_Peer_Agent * agents_head;
385 struct RIL_Peer_Agent * agents_tail;
393 * Simulate steps, i.e. schedule steps immediately
395 unsigned long long simulate;
400 * ---------------------------
404 ril_count_agents(struct GAS_RIL_Handle * solver);
407 agent_get_utility (struct RIL_Peer_Agent *agent)
410 return (double) agent->bw_in;
414 * Estimate the current action-value for state s and action a
416 * @param agent agent performing the estimation
419 * @return estimation value
422 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
427 for (i = 0; i < agent->m; i++)
429 result += state[i] * agent->W[action][i];
432 GNUNET_assert(!isnan(result));
434 //prevent crash when learning diverges
437 return isinf(result) * UINT32_MAX;
444 * Get the index of the address in the agent's list.
446 * @param agent agent handle
447 * @param address address handle
448 * @return the index, starting with zero
451 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
454 struct RIL_Address_Wrapped *cur;
457 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
460 if (cur->address_naked == address)
468 * Gets the wrapped address from the agent's list
470 * @param agent agent handle
471 * @param address address handle
472 * @return wrapped address
474 static struct RIL_Address_Wrapped *
475 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
477 struct RIL_Address_Wrapped *cur;
479 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
480 if (cur->address_naked == address)
487 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
488 * most reward in the future)
490 * @param agent agent performing the calculation
491 * @param state the state from which to take the action
492 * @return the action promising most future reward
495 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
499 int max_i = RIL_ACTION_INVALID;
501 double max_q = -DBL_MAX;
503 num_actions = agent->address_inuse->used ? RIL_ACTION_TYPE_NUM : agent->n;
505 for (i = 0; i < num_actions; i++)
507 cur_q = agent_estimate_q (agent, state, i);
515 GNUNET_assert(RIL_ACTION_INVALID != max_i);
522 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
524 * @param agent the agent performing the update
525 * @param reward the reward received for the last action
526 * @param s_next the new state, the last step got the agent into
527 * @param a_prime the new
530 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
534 double *theta = agent->W[agent->a_old];
536 delta = agent->envi->global_discount_integrated * reward; //reward
537 delta += agent->envi->global_discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
538 delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
540 // 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",
541 // agent->step_count,
542 // agent_estimate_q (agent, agent->s_old, agent->a_old),
543 // agent->envi->parameters.alpha,
545 // agent->envi->global_discount_variable,
546 // agent_estimate_q (agent, s_next, a_prime),
549 for (i = 0; i < agent->m; i++)
551 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
552 // agent->envi->parameters.alpha,
556 theta[i] += agent->envi->parameters.alpha * delta * agent->s_old[i];// * agent->E[a_prime][i];
562 * Changes the eligibility trace vector e in various manners:
563 * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
564 * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
565 * #RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
566 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
568 * @param agent the agent handle
569 * @param mod the kind of modification
570 * @param feature the feature vector
573 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
574 enum RIL_E_Modification mod,
581 for (i = 0; i < agent->m; i++)
585 case RIL_E_ACCUMULATE:
586 agent->E[action][i] += feature[i];
589 agent->E[action][i] = (agent->envi->global_discount_variable * agent->envi->parameters.lambda * agent->E[action][i]) > feature[i] ? agent->E[action][i] : feature[i];
592 agent->E[action][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
595 for (k = 0; k < agent->n; k++)
606 ril_inform (struct GAS_RIL_Handle *solver,
607 enum GAS_Solver_Operation op,
608 enum GAS_Solver_Status stat)
610 if (NULL != solver->plugin_envi->info_cb)
611 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
616 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
617 * notify ATS of its new decision
619 * @param solver solver handle
620 * @param agent agent handle
621 * @param new_address the address which is to be used
622 * @param new_bw_in the new amount of inbound bandwidth set for this address
623 * @param new_bw_out the new amount of outbound bandwidth set for this address
624 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
627 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
628 struct RIL_Peer_Agent *agent,
629 struct ATS_Address *new_address,
630 unsigned long long new_bw_in,
631 unsigned long long new_bw_out,
634 int notify = GNUNET_NO;
636 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
639 if (agent->address_inuse != new_address)
641 if (NULL != agent->address_inuse)
643 agent->address_inuse->active = GNUNET_NO;
644 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
645 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
647 if (NULL != new_address)
649 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
650 new_address->active = agent->is_active;
651 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
652 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
654 notify |= GNUNET_YES;
660 if (new_address->active != agent->is_active)
662 new_address->active = agent->is_active;
663 notify |= GNUNET_YES;
667 if (agent->bw_in != new_bw_in)
669 agent->bw_in = new_bw_in;
670 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
671 notify |= GNUNET_YES;
673 if (agent->bw_out != new_bw_out)
675 agent->bw_out = new_bw_out;
676 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
677 notify |= GNUNET_YES;
681 if (notify && agent->is_active && (GNUNET_NO == silent))
685 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
686 agent->suggestion_issue = GNUNET_YES;
687 agent->suggestion_address = new_address;
689 else if (agent->address_inuse)
691 //disconnect case, no new address
692 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
693 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
697 agent->suggestion_issue = GNUNET_YES;
698 agent->suggestion_address = agent->address_inuse;
701 agent->address_inuse = new_address;
705 static unsigned long long
706 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
708 struct RIL_Peer_Agent *cur;
709 struct RIL_Scope *net;
710 unsigned long long sum = 0;
712 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
714 if (cur->is_active && cur->address_inuse)
716 net = cur->address_inuse->solver_information;
717 if (net->type == type)
731 * Allocates a state vector and fills it with the features present
732 * @param solver the solver handle
733 * @param agent the agent handle
734 * @return pointer to the state vector
737 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
749 state = GNUNET_malloc (sizeof(double) * agent->m);
751 y[0] = (double) agent->bw_out;
752 y[1] = (double) agent->bw_in;
754 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.divisor+1) * (solver->parameters.divisor+1);
755 for (i = 0; i <= solver->parameters.divisor; i++)
757 for (k = 0; k <= solver->parameters.divisor; k++)
759 x[0] = i * GNUNET_ATS_MaxBandwidth / solver->parameters.divisor;
760 x[1] = k * GNUNET_ATS_MaxBandwidth / solver->parameters.divisor;
763 sigma = ((double) GNUNET_ATS_MaxBandwidth / 2) * M_SQRT2;
764 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
773 // * For all networks a peer has an address in, this gets the maximum bandwidth which could
774 // * theoretically be available in one of the networks. This is used for bandwidth normalization.
776 // * @param agent the agent handle
777 // * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
779 //static unsigned long long
780 //ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
783 // * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
784 // * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
786 // unsigned long long max = 0;
787 // struct RIL_Address_Wrapped *cur;
788 // struct RIL_Scope *net;
790 // for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
792 // net = cur->address_naked->solver_information;
795 // if (net->bw_in_available > max)
797 // max = net->bw_in_available;
802 // if (net->bw_out_available > max)
804 // max = net->bw_out_available;
812 // * Get the index of the quality-property in question
814 // * @param type the quality property type
815 // * @return the index
818 //ril_find_property_index (uint32_t type)
820 // int existing_types[] = GNUNET_ATS_QualityProperties;
822 // for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
823 // if (existing_types[c] == type)
825 // return GNUNET_SYSERR;
829 //ril_get_atsi (struct ATS_Address *address, uint32_t type)
832 // GNUNET_assert(NULL != address);
834 // if ((NULL == address->atsi) || (0 == address->atsi_count))
837 // for (c1 = 0; c1 < address->atsi_count; c1++)
839 // if (ntohl (address->atsi[c1].type) == type)
840 // return ntohl (address->atsi[c1].value);
846 //envi_reward_global (struct GAS_RIL_Handle *solver)
849 // struct RIL_Scope net;
850 // unsigned int sum_in_available = 0;
851 // unsigned int sum_out_available = 0;
852 // unsigned int sum_in_assigned = 0;
853 // unsigned int sum_out_assigned = 0;
857 // for (i = 0; i < solver->networks_count; i++)
859 // net = solver->network_entries[i];
860 // sum_in_available += net.bw_in_available;
861 // sum_in_assigned += net.bw_in_assigned;
862 // sum_out_available += net.bw_out_available;
863 // sum_out_assigned += net.bw_out_assigned;
866 // ratio_in = ((double) sum_in_assigned) / ((double) sum_in_available);
867 // ratio_out = ((double) sum_out_assigned) / ((double) sum_out_available);
869 // // global reward in [1,2]
870 // return ratio_in +1;
871 // return ((ratio_in + ratio_out) / 2) + 1;
875 //envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
877 // const double *preferences;
878 // const double *properties;
880 // double pref_match = 0;
884 // preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
886 // properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
887 // agent->address_inuse);
890 // prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
891 // dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
893 // // utilization in [0,1]
894 // bw_norm = (((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
895 // / (double) ril_get_max_bw (agent, GNUNET_YES))
896 // + ((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
897 // / (double) ril_get_max_bw (agent, GNUNET_NO))) / 2;
899 // // preference matching in [0,4]
900 // pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
901 // pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
903 // // local reward in [1,2]
904 // return (pref_match / 4) +1;
908 envi_get_collective_utility (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
910 //TODO! add nash product
911 struct RIL_Peer_Agent *cur;
912 double result = RIL_UTILITY_MAX;
914 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
916 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
918 result = GNUNET_MIN(result, agent_get_utility(cur));
926 * Gets the reward for the last performed step, which is calculated in equal
927 * parts from the local (the peer specific) and the global (for all peers
930 * @param solver the solver handle
931 * @param agent the agent handle
935 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
937 struct RIL_Scope *net;
939 unsigned long long objective;
941 LOG(GNUNET_ERROR_TYPE_INFO, "address: %x\n", agent->address_inuse);
942 net = agent->address_inuse->solver_information;
943 if (net->bw_in_assigned > net->bw_in_available)
945 objective = net->bw_in_available - net->bw_in_assigned;
949 objective = envi_get_collective_utility(solver, agent->address_inuse->solver_information);
956 * Doubles the bandwidth for the active address
958 * @param solver solver handle
959 * @param agent agent handle
960 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
963 envi_action_bw_double (struct GAS_RIL_Handle *solver,
964 struct RIL_Peer_Agent *agent,
967 unsigned long long new_bw;
971 new_bw = agent->bw_in * 2;
972 if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
973 new_bw = GNUNET_ATS_MaxBandwidth;
974 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
975 agent->bw_out, GNUNET_NO);
979 new_bw = agent->bw_out * 2;
980 if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth)
981 new_bw = GNUNET_ATS_MaxBandwidth;
982 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
988 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
989 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
991 * @param solver solver handle
992 * @param agent agent handle
993 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
997 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
998 struct RIL_Peer_Agent *agent,
1001 unsigned long long new_bw;
1005 new_bw = agent->bw_in / 2;
1006 if (new_bw < MIN_BW || new_bw > agent->bw_in)
1008 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1013 new_bw = agent->bw_out / 2;
1014 if (new_bw < MIN_BW || new_bw > agent->bw_out)
1016 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1022 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1024 * @param solver solver handle
1025 * @param agent agent handle
1026 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1030 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1032 unsigned long long new_bw;
1036 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1037 if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
1038 new_bw = GNUNET_ATS_MaxBandwidth;
1039 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1040 agent->bw_out, GNUNET_NO);
1044 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1045 if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth)
1046 new_bw = GNUNET_ATS_MaxBandwidth;
1047 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1053 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1054 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1056 * @param solver solver handle
1057 * @param agent agent handle
1058 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1062 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1064 unsigned long long new_bw;
1068 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1069 if (new_bw < MIN_BW || new_bw > agent->bw_in)
1071 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1076 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1077 if (new_bw < MIN_BW || new_bw > agent->bw_out)
1079 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1085 * Switches to the address given by its index
1087 * @param solver solver handle
1088 * @param agent agent handle
1089 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1092 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1093 struct RIL_Peer_Agent *agent,
1094 unsigned int address_index)
1096 struct RIL_Address_Wrapped *cur;
1099 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1101 if (i == address_index)
1103 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1111 //no address with address_index exists, in this case this action should not be callable
1112 GNUNET_assert(GNUNET_NO);
1116 * Puts the action into effect by calling the according function
1118 * @param solver the solver handle
1119 * @param agent the action handle
1120 * @param action the action to perform by the solver
1123 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1129 case RIL_ACTION_NOTHING:
1131 case RIL_ACTION_BW_IN_DBL:
1132 envi_action_bw_double (solver, agent, GNUNET_YES);
1134 case RIL_ACTION_BW_IN_HLV:
1135 envi_action_bw_halven (solver, agent, GNUNET_YES);
1137 case RIL_ACTION_BW_IN_INC:
1138 envi_action_bw_inc (solver, agent, GNUNET_YES);
1140 case RIL_ACTION_BW_IN_DEC:
1141 envi_action_bw_dec (solver, agent, GNUNET_YES);
1143 case RIL_ACTION_BW_OUT_DBL:
1144 envi_action_bw_double (solver, agent, GNUNET_NO);
1146 case RIL_ACTION_BW_OUT_HLV:
1147 envi_action_bw_halven (solver, agent, GNUNET_NO);
1149 case RIL_ACTION_BW_OUT_INC:
1150 envi_action_bw_inc (solver, agent, GNUNET_NO);
1152 case RIL_ACTION_BW_OUT_DEC:
1153 envi_action_bw_dec (solver, agent, GNUNET_NO);
1156 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1158 address_index = action - RIL_ACTION_TYPE_NUM;
1160 GNUNET_assert(address_index >= 0);
1162 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1164 envi_action_address_switch (solver, agent, address_index);
1167 // error - action does not exist
1168 GNUNET_assert(GNUNET_NO);
1173 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1177 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1178 UINT32_MAX) / (double) UINT32_MAX;
1180 num_actions = agent->address_inuse->used ? RIL_ACTION_TYPE_NUM : agent->n;
1182 if (r < agent->envi->parameters.explore_ratio) //explore
1184 action = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_actions);
1185 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1187 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, action);
1193 action = agent_get_action_max(agent, state);
1194 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1196 agent_modify_eligibility(agent, RIL_E_DISCOUNT, NULL, action);
1203 * Selects the next action with a probability corresponding to its value. The
1204 * probability is calculated using a Boltzmann distribution with a temperature
1205 * value. The higher the temperature, the more are the action selection
1206 * probabilities the same. With a temperature of 0, the selection is greedy,
1207 * i.e. always the action with the highest value is chosen.
1213 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1218 double eqt[agent->n];
1223 num_actions = agent->address_inuse->used ? RIL_ACTION_TYPE_NUM : agent->n;
1225 a_max = agent_get_action_max(agent, state);
1227 for (i=0; i<num_actions; i++)
1229 eqt[i] = exp(agent_estimate_q(agent,state,i) / agent->envi->parameters.temperature);
1232 for (i=0; i<num_actions; i++)
1236 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1237 UINT32_MAX) / (double) UINT32_MAX;
1239 for (i=0; i<num_actions; i++)
1243 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1246 agent_modify_eligibility(agent, RIL_E_DISCOUNT, NULL, i);
1248 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1254 GNUNET_assert(GNUNET_NO);
1258 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1260 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1262 return agent_select_egreedy(agent, state);
1266 return agent_select_softmax(agent, state);
1271 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1272 * after having done the last action a_old. It observes the new state s_next and the reward
1273 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1274 * next action is put into effect.
1276 * @param agent the agent performing the step
1279 agent_step (struct RIL_Peer_Agent *agent)
1281 int a_next = RIL_ACTION_INVALID;
1286 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1287 GNUNET_i2s (&agent->peer),
1288 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1290 s_next = envi_get_state (agent->envi, agent);
1291 reward = envi_get_reward (agent->envi, agent);
1293 switch (agent->envi->parameters.algorithm)
1295 case RIL_ALGO_SARSA:
1296 a_next = agent_select_action (agent, s_next);
1297 if (RIL_ACTION_INVALID != agent->a_old)
1299 //updates weights with selected action (on-policy), if not first step
1300 agent_update_weights (agent, reward, s_next, a_next);
1302 agent_modify_eligibility (agent, RIL_E_DISCOUNT, s_next, a_next);
1306 a_max = agent_get_action_max (agent, s_next);
1307 if (RIL_ACTION_INVALID != agent->a_old)
1309 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1310 agent_update_weights (agent, reward, s_next, a_max);
1312 a_next = agent_select_action (agent, s_next);
1316 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1318 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, s_next, a_next);
1320 // GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1321 // agent->step_count,
1323 // agent->bw_in/1024,
1324 // agent->bw_out/1024,
1327 envi_do_action (agent->envi, agent, a_next);
1329 GNUNET_free(agent->s_old);
1330 agent->s_old = s_next;
1331 agent->a_old = a_next;
1333 agent->step_count += 1;
1337 ril_step (struct GAS_RIL_Handle *solver);
1340 * Task for the scheduler, which performs one step and lets the solver know that
1341 * no further step is scheduled.
1343 * @param cls the solver handle
1344 * @param tc the task context for the scheduler
1347 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1349 struct GAS_RIL_Handle *solver = cls;
1351 solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1356 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1359 struct RIL_Scope net;
1360 unsigned long long sum_assigned = 0;
1361 unsigned long long sum_available = 0;
1364 for (i = 0; i < solver->networks_count; i++)
1366 net = solver->network_entries[i];
1367 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1369 sum_assigned += net.bw_in_assigned;
1370 sum_assigned += net.bw_out_assigned;
1371 sum_available += net.bw_in_available;
1372 sum_available += net.bw_out_available;
1375 if (sum_available > 0)
1377 ratio = ((double) sum_assigned) / ((double) sum_available);
1384 return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
1388 * Lookup network struct by type
1390 * @param s the solver handle
1391 * @param type the network type
1392 * @return the network struct
1394 static struct RIL_Scope *
1395 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1399 for (i = 0; i < s->networks_count; i++)
1401 if (s->network_entries[i].type == type)
1403 return &s->network_entries[i];
1410 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1412 struct RIL_Scope *net;
1413 struct RIL_Peer_Agent *agent;
1414 unsigned long long address_count = 0;
1416 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1418 if (agent->address_inuse && agent->is_active)
1420 net = agent->address_inuse->solver_information;
1421 if (net->type == network)
1428 net = ril_get_network (solver, network);
1429 return (net->bw_in_available > MIN_BW * address_count) && (net->bw_out_available > MIN_BW * address_count);
1433 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1435 struct RIL_Address_Wrapped *addr_wrap;
1436 struct RIL_Scope *net;
1438 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1440 net = addr_wrap->address_naked->solver_information;
1441 if (ril_network_is_not_full(solver, net->type))
1443 if (NULL == agent->address_inuse)
1444 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, MIN_BW, MIN_BW, silent);
1448 agent->address_inuse = NULL;
1452 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1454 struct GNUNET_TIME_Absolute time_now;
1455 struct GNUNET_TIME_Relative time_delta;
1458 // MDP case - remove when debugged
1459 if (solver->simulate)
1461 solver->global_discount_variable = solver->parameters.gamma;
1462 solver->global_discount_integrated = 1;
1468 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1469 time_now = GNUNET_TIME_absolute_get ();
1470 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1471 solver->step_time_last = time_now;
1472 tau = (double) time_delta.rel_value_us
1473 / (double) solver->parameters.step_time_min.rel_value_us;
1475 //calculate reward discounts (once per step for all agents)
1476 solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1477 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1478 / (double) solver->parameters.beta;
1482 ril_calculate_assigned_bwnet (struct GAS_RIL_Handle *solver)
1485 struct RIL_Scope *net;
1487 for (c = 0; c < solver->networks_count; c++)
1489 net = &solver->network_entries[c];
1490 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1491 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1496 * Schedules the next global step in an adaptive way. The more resources are
1497 * left, the earlier the next step is scheduled. This serves the reactivity of
1498 * the solver to changed inputs.
1500 * @param solver the solver handle
1503 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1509 struct GNUNET_TIME_Relative time_next;
1511 used_ratio = ril_get_used_resource_ratio (solver);
1514 solver->parameters.step_time_min.rel_value_us
1515 <= solver->parameters.step_time_max.rel_value_us);
1517 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1518 solver->parameters.step_time_min).rel_value_us;
1519 offset = (double) solver->parameters.step_time_min.rel_value_us;
1520 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1522 GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1523 GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1525 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1527 if (solver->simulate)
1529 time_next = GNUNET_TIME_UNIT_ZERO;
1532 if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1534 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1540 * Triggers one step per agent
1544 ril_step (struct GAS_RIL_Handle *solver)
1546 struct RIL_Peer_Agent *cur;
1548 if (GNUNET_YES == solver->bulk_lock)
1550 solver->bulk_changes++;
1554 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1556 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1558 if (0 == solver->step_count)
1560 solver->step_time_last = GNUNET_TIME_absolute_get ();
1563 ril_calculate_discount (solver);
1564 ril_calculate_assigned_bwnet (solver);
1566 //calculate network state vector
1567 // envi_state_networks(solver);
1569 //trigger one step per active, unblocked agent
1570 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1574 if (NULL == cur->address_inuse)
1576 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1578 if (cur->address_inuse)
1585 ril_calculate_assigned_bwnet (solver);
1587 solver->step_count += 1;
1588 ril_step_schedule_next (solver);
1590 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1592 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1593 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1595 if (cur->suggestion_issue) {
1596 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1597 cur->suggestion_issue = GNUNET_NO;
1600 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1604 ril_count_agents (struct GAS_RIL_Handle *solver)
1607 struct RIL_Peer_Agent *cur_agent;
1609 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1617 agent_w_start (struct RIL_Peer_Agent *agent)
1620 struct RIL_Peer_Agent *other;
1624 count = ril_count_agents(agent->envi);
1626 for (i = 0; i < agent->n; i++)
1628 for (k = 0; k < agent->m; k++)
1631 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));
1634 for (other = agent->envi->agents_head; NULL != other; other = other->next)
1636 agent->W[i][k] += (other->W[i][k] / (double) count);
1640 GNUNET_assert(!isinf(agent->W[i][k]));
1646 * Initialize an agent without addresses and its knowledge base
1648 * @param s ril solver
1649 * @param peer the one in question
1650 * @return handle to the new agent
1652 static struct RIL_Peer_Agent *
1653 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1656 struct GAS_RIL_Handle * solver = s;
1657 struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1659 agent->envi = solver;
1660 agent->peer = *peer;
1661 agent->step_count = 0;
1662 agent->is_active = GNUNET_NO;
1663 agent->bw_in = MIN_BW;
1664 agent->bw_out = MIN_BW;
1665 agent->suggestion_issue = GNUNET_NO;
1666 agent->n = RIL_ACTION_TYPE_NUM;
1668 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1669 agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1670 for (i = 0; i < agent->n; i++)
1672 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1673 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1675 agent_w_start(agent);
1676 agent->a_old = RIL_ACTION_INVALID;
1677 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1678 agent->address_inuse = NULL;
1686 * @param solver the solver handle
1687 * @param agent the agent to retire
1690 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1694 for (i = 0; i < agent->n; i++)
1696 GNUNET_free_non_null(agent->W[i]);
1697 GNUNET_free_non_null(agent->E[i]);
1699 GNUNET_free_non_null(agent->W);
1700 GNUNET_free_non_null(agent->E);
1701 GNUNET_free_non_null(agent->s_old);
1706 * Returns the agent for a peer
1708 * @param solver the solver handle
1709 * @param peer the identity of the peer
1710 * @param create whether or not to create an agent, if none is allocated yet
1713 static struct RIL_Peer_Agent *
1714 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1716 struct RIL_Peer_Agent *cur;
1718 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1720 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1728 cur = agent_init (solver, peer);
1729 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
1736 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1737 * considered inactive and not used. Addresses in an inactive network are ignored.
1739 * @param solver solver handle
1740 * @param network the network type
1741 * @return whether or not the network is considered active
1744 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1746 struct RIL_Scope *net;
1748 net = ril_get_network (solver, network);
1749 return net->bw_out_available >= MIN_BW;
1753 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1754 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1755 * frees the memory of the old vector, and redirects the pointer to the new one.
1757 * @param old pointer to the pointer to the first element of the vector
1758 * @param element_size byte size of the vector elements
1759 * @param hole_start the first element to cut out
1760 * @param hole_length the number of elements to cut out
1761 * @param old_length the length of the old vector
1764 ril_cut_from_vector (void **old,
1765 size_t element_size,
1766 unsigned int hole_start,
1767 unsigned int hole_length,
1768 unsigned int old_length)
1771 char *oldptr = (char *) *old;
1773 unsigned int bytes_before;
1774 unsigned int bytes_hole;
1775 unsigned int bytes_after;
1777 GNUNET_assert(old_length >= hole_length);
1778 GNUNET_assert(old_length >= (hole_start + hole_length));
1780 size = element_size * (old_length - hole_length);
1782 bytes_before = element_size * hole_start;
1783 bytes_hole = element_size * hole_length;
1784 bytes_after = element_size * (old_length - hole_start - hole_length);
1792 tmpptr = GNUNET_malloc (size);
1793 memcpy (tmpptr, oldptr, bytes_before);
1794 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1800 *old = (void *) tmpptr;
1804 * Solver API functions
1805 * ---------------------------
1809 * Change relative preference for quality in solver
1811 * @param solver the solver handle
1812 * @param peer the peer to change the preference for
1813 * @param kind the kind to change the preference
1814 * @param pref_rel the normalized preference value for this kind over all clients
1817 GAS_ril_address_change_preference (void *solver,
1818 const struct GNUNET_PeerIdentity *peer,
1819 enum GNUNET_ATS_PreferenceKind kind,
1822 LOG(GNUNET_ERROR_TYPE_DEBUG,
1823 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1824 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1830 * Entry point for the plugin
1832 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1835 libgnunet_plugin_ats_ril_init (void *cls)
1837 struct GNUNET_ATS_PluginEnvironment *env = cls;
1838 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1839 struct RIL_Scope * cur;
1843 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1845 GNUNET_assert(NULL != env);
1846 GNUNET_assert(NULL != env->cfg);
1847 GNUNET_assert(NULL != env->stats);
1848 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1849 GNUNET_assert(NULL != env->get_preferences);
1850 GNUNET_assert(NULL != env->get_property);
1852 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.divisor))
1854 solver->parameters.divisor = RIL_DEFAULT_RBF_DIVISOR;
1857 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
1858 &solver->parameters.step_time_min))
1860 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
1863 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
1864 &solver->parameters.step_time_max))
1866 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
1868 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
1870 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
1871 GNUNET_free (string);
1875 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1877 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
1879 solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
1880 GNUNET_free (string);
1884 solver->parameters.select = RIL_DEFAULT_SELECT;
1886 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
1888 solver->parameters.beta = strtod (string, NULL);
1889 GNUNET_free (string);
1890 if (!(solver->parameters.beta > 0))
1892 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);
1893 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1898 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1900 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
1902 solver->parameters.gamma = strtod (string, NULL);
1903 GNUNET_free (string);
1904 if (!(solver->parameters.gamma < 1) || (solver->parameters.gamma < 0))
1906 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);
1907 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
1912 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
1915 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
1917 solver->parameters.alpha = strtod (string, NULL);
1918 GNUNET_free (string);
1919 if (!(solver->parameters.alpha > 0) || solver->parameters.alpha > 1)
1921 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);
1922 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1927 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1929 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
1931 solver->parameters.lambda = strtod (string, NULL);
1932 GNUNET_free (string);
1933 if (solver->parameters.lambda < 0 || solver->parameters.lambda > 1)
1935 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);
1936 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1941 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1943 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
1945 solver->parameters.explore_ratio = strtod (string, NULL);
1946 GNUNET_free (string);
1947 if (solver->parameters.explore_ratio < 0 || solver->parameters.explore_ratio > 1)
1949 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);
1950 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
1955 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
1957 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
1959 solver->parameters.reward_global_share = strtod (string, NULL);
1960 GNUNET_free (string);
1961 if (solver->parameters.reward_global_share < 0 || solver->parameters.reward_global_share > 1)
1963 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);
1964 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
1969 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
1971 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE", &string))
1973 solver->parameters.temperature = strtod (string, NULL);
1974 GNUNET_free (string);
1975 if (solver->parameters.temperature <= 0)
1977 LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE not positive. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE);
1978 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
1983 solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
1985 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
1987 solver->simulate = GNUNET_NO;
1989 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
1991 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
1995 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
1998 env->sf.s_add = &GAS_ril_address_add;
1999 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
2000 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
2001 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
2002 env->sf.s_address_update_network = &GAS_ril_address_change_network;
2003 env->sf.s_get = &GAS_ril_get_preferred_address;
2004 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2005 env->sf.s_pref = &GAS_ril_address_change_preference;
2006 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
2007 env->sf.s_del = &GAS_ril_address_delete;
2008 env->sf.s_bulk_start = &GAS_ril_bulk_start;
2009 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
2011 solver->plugin_envi = env;
2012 solver->networks_count = env->network_count;
2013 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2014 solver->step_count = 0;
2015 solver->done = GNUNET_NO;
2017 for (c = 0; c < env->network_count; c++)
2019 cur = &solver->network_entries[c];
2020 cur->type = env->networks[c];
2021 cur->bw_in_available = env->in_quota[c];
2022 cur->bw_out_available = env->out_quota[c];
2023 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);
2026 LOG(GNUNET_ERROR_TYPE_INFO, "init() Parameters:\n");
2027 LOG(GNUNET_ERROR_TYPE_INFO, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2028 solver->parameters.algorithm ? "Q" : "SARSA",
2029 solver->parameters.alpha,
2030 solver->parameters.beta,
2031 solver->parameters.lambda);
2032 LOG(GNUNET_ERROR_TYPE_INFO, "init() explore = %f, global_share = %f\n",
2033 solver->parameters.explore_ratio,
2034 solver->parameters.reward_global_share);
2040 * Exit point for the plugin
2042 * @param cls the solver handle
2045 libgnunet_plugin_ats_ril_done (void *cls)
2047 struct GAS_RIL_Handle *s = cls;
2048 struct RIL_Peer_Agent *cur_agent;
2049 struct RIL_Peer_Agent *next_agent;
2051 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2053 s->done = GNUNET_YES;
2055 cur_agent = s->agents_head;
2056 while (NULL != cur_agent)
2058 next_agent = cur_agent->next;
2059 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2060 agent_die (s, cur_agent);
2061 cur_agent = next_agent;
2064 if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
2066 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2068 GNUNET_free(s->network_entries);
2075 * Add a new address for a peer to the solver
2077 * The address is already contained in the addresses hashmap!
2079 * @param solver the solver Handle
2080 * @param address the address to add
2081 * @param network network type of this address
2084 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
2086 struct GAS_RIL_Handle *s = solver;
2087 struct RIL_Peer_Agent *agent;
2088 struct RIL_Address_Wrapped *address_wrapped;
2089 struct RIL_Scope *net;
2097 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
2099 net = ril_get_network (s, network);
2100 address->solver_information = net;
2102 if (!ril_network_is_active (s, network))
2104 LOG(GNUNET_ERROR_TYPE_DEBUG,
2105 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2106 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2110 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2113 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2114 address_wrapped->address_naked = address;
2115 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2117 //increase size of W
2118 m_new = agent->m + ((s->parameters.divisor+1) * (s->parameters.divisor+1));
2120 n_new = agent->n + 1;
2123 GNUNET_array_grow(agent->W, agent->n, n_new);
2125 GNUNET_array_grow(agent->E, agent->n, n_new);
2126 for (i = 0; i < n_new; i++)
2131 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2133 GNUNET_array_grow(agent->E[i], agent->m, m_new);
2138 GNUNET_array_grow(agent->W[i], zero, m_new);
2140 GNUNET_array_grow(agent->E[i], zero, m_new);
2144 //increase size of old state vector
2146 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2148 ril_try_unblock_agent(s, agent, GNUNET_NO);
2152 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2153 address->active ? "active" : "inactive", address->plugin, address->addr,
2154 GNUNET_i2s (&address->peer));
2158 * Delete an address in the solver
2160 * The address is not contained in the address hashmap anymore!
2162 * @param solver the solver handle
2163 * @param address the address to remove
2164 * @param session_only delete only session not whole address
2167 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
2169 struct GAS_RIL_Handle *s = solver;
2170 struct RIL_Peer_Agent *agent;
2171 struct RIL_Address_Wrapped *address_wrapped;
2172 int address_was_used = address->active;
2177 struct RIL_Scope *net;
2179 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2180 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2181 address->addr, GNUNET_i2s (&address->peer));
2183 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2186 net = address->solver_information;
2187 GNUNET_assert(!ril_network_is_active (s, net->type));
2188 LOG(GNUNET_ERROR_TYPE_DEBUG,
2189 "No agent allocated for peer yet, since address was in inactive network\n");
2193 address_index = agent_address_get_index (agent, address);
2194 address_wrapped = agent_address_get (agent, address);
2196 if (NULL == address_wrapped)
2198 net = address->solver_information;
2199 GNUNET_assert(!ril_network_is_active (s, net->type));
2200 LOG(GNUNET_ERROR_TYPE_DEBUG,
2201 "Address not considered by agent, address was in inactive network\n");
2205 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2206 GNUNET_free(address_wrapped);
2209 m_new = agent->m - ((s->parameters.divisor+1) * (s->parameters.divisor+1));
2210 n_new = agent->n - 1;
2212 for (i = 0; i < agent->n; i++)
2214 LOG(GNUNET_ERROR_TYPE_DEBUG, "first\n");
2215 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2216 address_index * ((s->parameters.divisor+1) * (s->parameters.divisor+1)),
2217 ((s->parameters.divisor+1) * (s->parameters.divisor+1)), agent->m);
2218 LOG(GNUNET_ERROR_TYPE_DEBUG, "sec\n");
2219 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2220 address_index * ((s->parameters.divisor+1) * (s->parameters.divisor+1)),
2221 ((s->parameters.divisor+1) * (s->parameters.divisor+1)), agent->m);
2223 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2224 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2225 LOG(GNUNET_ERROR_TYPE_DEBUG, "third\n");
2226 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2228 LOG(GNUNET_ERROR_TYPE_DEBUG, "fourth\n");
2229 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2231 //correct last action
2232 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2236 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2238 agent->a_old = RIL_ACTION_INVALID;
2240 //decrease old state vector
2241 LOG(GNUNET_ERROR_TYPE_DEBUG, "fifth\n");
2242 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2243 address_index * ((s->parameters.divisor+1) * (s->parameters.divisor+1)),
2244 ((s->parameters.divisor+1) * (s->parameters.divisor+1)), agent->m);
2248 if (address_was_used)
2250 if (NULL != agent->addresses_head) //if peer has an address left, use it
2252 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, MIN_BW, MIN_BW,
2257 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2265 * Update the properties of an address in the solver
2267 * @param solver solver handle
2268 * @param address the address
2269 * @param type the ATSI type in HBO
2270 * @param abs_value the absolute value of the property
2271 * @param rel_value the normalized value
2274 GAS_ril_address_property_changed (void *solver,
2275 struct ATS_Address *address,
2280 LOG(GNUNET_ERROR_TYPE_DEBUG,
2281 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2282 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2283 address->addr, rel_value);
2289 * Update the session of an address in the solver
2291 * NOTE: values in addresses are already updated
2293 * @param solver solver handle
2294 * @param address the address
2295 * @param cur_session the current session
2296 * @param new_session the new session
2299 GAS_ril_address_session_changed (void *solver,
2300 struct ATS_Address *address,
2301 uint32_t cur_session,
2302 uint32_t new_session)
2304 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2308 * Notify the solver that an address is (not) actively used by transport
2309 * to communicate with a remote peer
2311 * NOTE: values in addresses are already updated
2313 * @param solver solver handle
2314 * @param address the address
2315 * @param in_use usage state
2318 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2320 LOG(GNUNET_ERROR_TYPE_DEBUG,
2321 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2322 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2326 * Notify solver that the network an address is located in has changed
2328 * NOTE: values in addresses are already updated
2330 * @param solver solver handle
2331 * @param address the address
2332 * @param current_network the current network
2333 * @param new_network the new network
2336 GAS_ril_address_change_network (void *solver,
2337 struct ATS_Address *address,
2338 uint32_t current_network,
2339 uint32_t new_network)
2341 struct GAS_RIL_Handle *s = solver;
2342 struct RIL_Peer_Agent *agent;
2344 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2345 "%s address of peer %s from '%s' to '%s'\n",
2346 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2347 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2349 if (address->active && !ril_network_is_active (solver, new_network))
2351 GAS_ril_address_delete (solver, address, GNUNET_NO);
2355 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2358 GNUNET_assert(!ril_network_is_active (solver, current_network));
2360 GAS_ril_address_add (s, address, new_network);
2364 address->solver_information = ril_get_network(solver, new_network);
2368 * Give feedback about the current assignment
2370 * @param solver the solver handle
2371 * @param application the application
2372 * @param peer the peer to change the preference for
2373 * @param scope the time interval for this feedback: [now - scope .. now]
2374 * @param kind the kind to change the preference
2375 * @param score the score
2378 GAS_ril_address_preference_feedback (void *solver,
2380 const struct GNUNET_PeerIdentity *peer,
2381 const struct GNUNET_TIME_Relative scope,
2382 enum GNUNET_ATS_PreferenceKind kind,
2385 LOG(GNUNET_ERROR_TYPE_DEBUG,
2386 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2387 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2388 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2392 * Start a bulk operation
2394 * @param solver the solver
2397 GAS_ril_bulk_start (void *solver)
2399 struct GAS_RIL_Handle *s = solver;
2401 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2407 * Bulk operation done
2409 * @param solver the solver handle
2412 GAS_ril_bulk_stop (void *solver)
2414 struct GAS_RIL_Handle *s = solver;
2416 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2418 if (s->bulk_lock < 1)
2425 if (0 < s->bulk_changes)
2428 s->bulk_changes = 0;
2433 * Tell solver to notify ATS if the address to use changes for a specific
2434 * peer using the bandwidth changed callback
2436 * The solver must only notify about changes for peers with pending address
2439 * @param solver the solver handle
2440 * @param peer the identity of the peer
2442 const struct ATS_Address *
2443 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2446 * activate agent, return currently chosen address
2448 struct GAS_RIL_Handle *s = solver;
2449 struct RIL_Peer_Agent *agent;
2451 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2453 agent = ril_get_agent (s, peer, GNUNET_YES);
2455 agent->is_active = GNUNET_YES;
2456 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2458 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2460 if (agent->address_inuse)
2462 LOG(GNUNET_ERROR_TYPE_DEBUG,
2463 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2464 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2468 LOG(GNUNET_ERROR_TYPE_DEBUG,
2469 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2473 return agent->address_inuse;
2477 * Tell solver stop notifying ATS about changes for this peers
2479 * The solver must only notify about changes for peers with pending address
2482 * @param solver the solver handle
2483 * @param peer the peer
2486 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2488 struct GAS_RIL_Handle *s = solver;
2489 struct RIL_Peer_Agent *agent;
2491 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2493 agent = ril_get_agent (s, peer, GNUNET_NO);
2500 if (GNUNET_NO == agent->is_active)
2506 agent->is_active = GNUNET_NO;
2508 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2513 LOG(GNUNET_ERROR_TYPE_DEBUG,
2514 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2515 GNUNET_i2s (peer), agent->address_inuse->plugin);
2518 /* end of plugin_ats_ril.c */