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_FEATURES_ADDRESS_COUNT (0)// + GNUNET_ATS_QualityPropertiesCount)
35 #define RIL_FEATURES_NETWORK_COUNT 2
36 #define RIL_INTERVAL_EXPONENT 10
38 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
39 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
40 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
41 #define RIL_DEFAULT_DISCOUNT_BETA 1
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_GLOBAL_REWARD_SHARE 0.5
48 #define RIL_INC_DEC_STEP_SIZE 1
51 * ATS reinforcement learning solver
57 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
58 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
59 * to a particular address. The action of switching to address with index i is depicted by the
60 * number (RIL_ACTION_TYPE_NUM + i).
64 RIL_ACTION_NOTHING = -1,
65 RIL_ACTION_BW_IN_DBL = -2, //TODO! put actions back
66 RIL_ACTION_BW_IN_HLV = -3,
67 RIL_ACTION_BW_IN_INC = 0,
68 RIL_ACTION_BW_IN_DEC = 1,
69 RIL_ACTION_BW_OUT_DBL = -4,
70 RIL_ACTION_BW_OUT_HLV = -5,
71 RIL_ACTION_BW_OUT_INC = -6,
72 RIL_ACTION_BW_OUT_DEC = -7,
73 RIL_ACTION_TYPE_NUM = 1
82 enum RIL_E_Modification
91 * Global learning parameters
93 struct RIL_Learning_Parameters
96 * The TD-algorithm to use
98 enum RIL_Algorithm algorithm;
101 * Gradient-descent step-size
106 * Learning discount variable in the TD-update for semi-MDPs
111 * Learning discount factor in the TD-update for MDPs
116 * Trace-decay factor for eligibility traces
121 * Ratio, with what probability an agent should explore in the e-greed policy
123 double explore_ratio;
126 * How big the share of the global part of the reward signal is
128 double reward_global_share;
131 * Minimal interval time between steps in milliseconds
133 struct GNUNET_TIME_Relative step_time_min;
136 * Maximum interval time between steps in milliseconds
138 struct GNUNET_TIME_Relative step_time_max;
142 * Wrapper for addresses to store them in agent's linked list
144 struct RIL_Address_Wrapped
149 struct RIL_Address_Wrapped *next;
154 struct RIL_Address_Wrapped *prev;
159 struct ATS_Address *address_naked;
162 struct RIL_Peer_Agent
165 * Next agent in solver's linked list
167 struct RIL_Peer_Agent *next;
170 * Previous agent in solver's linked list
172 struct RIL_Peer_Agent *prev;
177 struct GAS_RIL_Handle *envi;
182 struct GNUNET_PeerIdentity peer;
185 * Whether the agent is active or not
190 * Number of performed time-steps
192 unsigned long long step_count;
195 * Experience matrix W
200 * Number of rows of W / Number of state-vector features
205 * Number of columns of W / Number of actions
210 * Last perceived state feature vector
220 * Eligibility trace vector
227 struct ATS_Address * address_inuse;
230 * Head of addresses DLL
232 struct RIL_Address_Wrapped * addresses_head;
235 * Tail of addresses DLL
237 struct RIL_Address_Wrapped * addresses_tail;
240 * Inbound bandwidth assigned by the agent
242 unsigned long long bw_in;
245 * Outbound bandwidth assigned by the agent
247 unsigned long long bw_out;
250 * Flag whether a suggestion has to be issued
252 int suggestion_issue;
255 * The address which has to be issued
257 struct ATS_Address * suggestion_address;
265 enum GNUNET_ATS_Network_Type type;
268 * Total available inbound bandwidth
270 unsigned long long bw_in_available;
273 * Bandwidth inbound assigned in network after last step
275 unsigned long long bw_in_assigned;
278 * Total available outbound bandwidth
280 unsigned long long bw_out_available;
283 * * Bandwidth outbound assigned in network after last step
285 unsigned long long bw_out_assigned;
289 * A handle for the reinforcement learning solver
291 struct GAS_RIL_Handle
294 * The solver-plugin environment of the solver-plugin API
296 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
301 struct GNUNET_STATISTICS_Handle *stats;
304 * Number of performed steps
306 unsigned long long step_count;
309 * Timestamp for the last time-step
311 struct GNUNET_TIME_Absolute step_time_last;
314 * Task identifier of the next time-step to be executed
316 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
319 * Variable discount factor, dependent on time between steps
321 double global_discount_variable;
324 * Integrated variable discount factor, dependent on time between steps
326 double global_discount_integrated;
329 * State vector for networks for the current step
331 double *global_state_networks;
334 * Lock for bulk operations
339 * Number of changes during a lock
344 * Learning parameters
346 struct RIL_Learning_Parameters parameters;
349 * Array of networks with global assignment state
351 struct RIL_Network * network_entries;
356 unsigned int networks_count;
359 * List of active peer-agents
361 struct RIL_Peer_Agent * agents_head;
362 struct RIL_Peer_Agent * agents_tail;
370 * Simulate steps, i.e. schedule steps immediately
372 unsigned long long simulate;
377 * ---------------------------
381 ril_count_agents(struct GAS_RIL_Handle * solver);
384 * Estimate the current action-value for state s and action a
386 * @param agent agent performing the estimation
389 * @return estimation value
392 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
397 for (i = 0; i < agent->m; i++)
399 result += state[i] * agent->W[action][i];
402 GNUNET_assert(!isnan(result));
406 return isinf(result) * UINT32_MAX; //TODO! fix
413 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
414 * currently estimated best action) in the current step
416 * @param agent agent performing the step
417 * @return yes, if exploring
420 agent_decide_exploration (struct RIL_Peer_Agent *agent)
422 //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
423 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
424 UINT32_MAX) / (double) UINT32_MAX;
426 if (r < agent->envi->parameters.explore_ratio)
434 * Get the index of the address in the agent's list.
436 * @param agent agent handle
437 * @param address address handle
438 * @return the index, starting with zero
441 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
444 struct RIL_Address_Wrapped *cur;
447 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
450 if (cur->address_naked == address)
460 * Gets the wrapped address from the agent's list
462 * @param agent agent handle
463 * @param address address handle
464 * @return wrapped address
466 static struct RIL_Address_Wrapped *
467 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
469 struct RIL_Address_Wrapped *cur;
471 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
473 if (cur->address_naked == address)
483 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
484 * most reward in the future)
486 * @param agent agent performing the calculation
487 * @param state the state from which to take the action
488 * @return the action promising most future reward
491 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
494 int max_i = RIL_ACTION_INVALID;
496 double max_q = -DBL_MAX;
498 for (i = 0; i < agent->n; i++)
500 cur_q = agent_estimate_q (agent, state, i);
508 GNUNET_assert(RIL_ACTION_INVALID != max_i);
514 * Gets any action, to explore the action space from that state
516 * @param agent agent performing the calculation
517 * @param state the state from which to take the action
521 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
523 // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
524 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
528 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
530 * @param agent the agent performing the update
531 * @param reward the reward received for the last action
532 * @param s_next the new state, the last step got the agent into
533 * @param a_prime the new
536 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
540 double *theta = agent->W[agent->a_old];
542 delta = agent->envi->global_discount_integrated * reward; //reward
543 delta += agent->envi->global_discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
544 delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
546 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",
548 agent_estimate_q (agent, agent->s_old, agent->a_old),
549 agent->envi->parameters.alpha,
551 agent->envi->global_discount_variable,
552 agent_estimate_q (agent, s_next, a_prime),
555 for (i = 0; i < agent->m; i++)
557 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
558 // agent->envi->parameters.alpha,
562 theta[i] += agent->envi->parameters.alpha * delta * agent->s_old[i];// * agent->e[i];
567 * Changes the eligibility trace vector e in various manners:
568 * #RIL_E_ACCUMULATE - adds @a f to each component as in accumulating eligibility traces
569 * #RIL_E_REPLACE - resets each component to @f as in replacing traces
570 * #RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
571 * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
573 * @param agent the agent handle
574 * @param mod the kind of modification
575 * @param f how much to change
578 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod, double *f)
581 double *e = agent->e;
583 for (i = 0; i < agent->m; i++)
587 case RIL_E_ACCUMULATE:
594 e[i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
605 ril_inform (struct GAS_RIL_Handle *solver,
606 enum GAS_Solver_Operation op,
607 enum GAS_Solver_Status stat)
609 if (NULL != solver->plugin_envi->info_cb)
610 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
614 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
615 * notify ATS of its new decision
617 * @param solver solver handle
618 * @param agent agent handle
619 * @param new_address the address which is to be used
620 * @param new_bw_in the new amount of inbound bandwidth set for this address
621 * @param new_bw_out the new amount of outbound bandwidth set for this address
622 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
625 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
626 struct RIL_Peer_Agent *agent,
627 struct ATS_Address *new_address,
628 unsigned long long new_bw_in,
629 unsigned long long new_bw_out,
632 int notify = GNUNET_NO;
634 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
637 if (agent->address_inuse != new_address)
639 if (NULL != agent->address_inuse)
641 agent->address_inuse->active = GNUNET_NO;
642 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
643 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
645 if (NULL != new_address)
647 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
648 new_address->active = agent->is_active;
649 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
650 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
652 notify |= GNUNET_YES;
658 if (new_address->active != agent->is_active)
660 new_address->active = agent->is_active;
661 notify |= GNUNET_YES;
665 if (agent->bw_in != new_bw_in)
667 agent->bw_in = new_bw_in;
668 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
669 notify |= GNUNET_YES;
671 if (agent->bw_out != new_bw_out)
673 agent->bw_out = new_bw_out;
674 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
675 notify |= GNUNET_YES;
679 if (notify && agent->is_active && (GNUNET_NO == silent))
683 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
684 agent->suggestion_issue = GNUNET_YES;
685 agent->suggestion_address = new_address;
687 else if (agent->address_inuse)
689 //disconnect case, no new address
690 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
691 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
695 agent->suggestion_issue = GNUNET_YES;
696 agent->suggestion_address = agent->address_inuse;
699 agent->address_inuse = new_address;
702 static unsigned long long
703 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
705 struct RIL_Peer_Agent *cur;
706 struct RIL_Network *net;
707 unsigned long long sum = 0;
709 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
711 if (cur->is_active && cur->address_inuse)
713 net = cur->address_inuse->solver_information;
714 if (net->type == type)
728 //envi_state_networks (struct GAS_RIL_Handle *solver)
731 // struct RIL_Network net;
732 // int overutilized_in;
733 // int overutilized_out;
735 // for (i = 0; i < solver->networks_count; i++)
737 // net = solver->network_entries[i];
739 // overutilized_in = net.bw_in_assigned > net.bw_in_available;
740 // overutilized_out = net.bw_out_assigned > net.bw_out_available;
742 // solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 0] = ((double) net.bw_in_assigned / (double) net.bw_in_available)*10;
743 // solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) overutilized_in;
744 // solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 2] = ((double) net.bw_out_assigned / (double) net.bw_out_available)*10;
745 // solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) overutilized_out;
750 * Allocates a state vector and fills it with the features present
751 * @param solver the solver handle
752 * @param agent the agent handle
753 * @return pointer to the state vector
756 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
760 double *state = GNUNET_malloc (sizeof (double) * agent->m);
761 struct RIL_Address_Wrapped *cur_address;
762 // const double *preferences;
763 // const double *properties;
764 struct RIL_Network *net;
766 //copy global networks state
767 for (i = 0; i < solver->networks_count * RIL_FEATURES_NETWORK_COUNT; i++)
769 // state[i] = solver->global_state_networks[i];
771 net = agent->address_inuse->solver_information;
773 state[0] = (double) net->bw_in_assigned / 1024; //(double) net->bw_in_available;
774 if (net->bw_in_assigned > net->bw_in_available)
776 state[1] = 1;// net->bw_in_available;
782 LOG(GNUNET_ERROR_TYPE_INFO, "get_state() state[0] = %f\n", state[0]);
783 LOG(GNUNET_ERROR_TYPE_INFO, "get_state() state[1] = %f\n", state[1]);
785 LOG(GNUNET_ERROR_TYPE_INFO, "get_state() W / %08.3f %08.3f \\ \n", agent->W[0][0], agent->W[1][0]);
786 LOG(GNUNET_ERROR_TYPE_INFO, "get_state() W \\ %08.3f %08.3f / \n", agent->W[0][1], agent->W[1][1]);
790 // preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
792 // for (k = 0; k < GNUNET_ATS_PreferenceCount; k++)
794 // state[i++] = preferences[k];
797 //get address specific features
798 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
800 // //when changing the number of address specific state features, change RIL_FEATURES_ADDRESS_COUNT macro
801 // state[i++] = cur_address->address_naked->active;
802 // state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
803 // state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
804 // properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
805 // cur_address->address_naked);
806 // for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
808 // state[i++] = properties[k];
816 // * For all networks a peer has an address in, this gets the maximum bandwidth which could
817 // * theoretically be available in one of the networks. This is used for bandwidth normalization.
819 // * @param agent the agent handle
820 // * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
822 //static unsigned long long
823 //ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
826 // * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
827 // * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
829 // unsigned long long max = 0;
830 // struct RIL_Address_Wrapped *cur;
831 // struct RIL_Network *net;
833 // for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
835 // net = cur->address_naked->solver_information;
838 // if (net->bw_in_available > max)
840 // max = net->bw_in_available;
845 // if (net->bw_out_available > max)
847 // max = net->bw_out_available;
855 // * Get the index of the quality-property in question
857 // * @param type the quality property type
858 // * @return the index
861 //ril_find_property_index (uint32_t type)
863 // int existing_types[] = GNUNET_ATS_QualityProperties;
865 // for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
866 // if (existing_types[c] == type)
868 // return GNUNET_SYSERR;
872 //ril_get_atsi (struct ATS_Address *address, uint32_t type)
875 // GNUNET_assert(NULL != address);
877 // if ((NULL == address->atsi) || (0 == address->atsi_count))
880 // for (c1 = 0; c1 < address->atsi_count; c1++)
882 // if (ntohl (address->atsi[c1].type) == type)
883 // return ntohl (address->atsi[c1].value);
889 //envi_reward_global (struct GAS_RIL_Handle *solver)
892 // struct RIL_Network net;
893 // unsigned int sum_in_available = 0;
894 // unsigned int sum_out_available = 0;
895 // unsigned int sum_in_assigned = 0;
896 // unsigned int sum_out_assigned = 0;
900 // for (i = 0; i < solver->networks_count; i++)
902 // net = solver->network_entries[i];
903 // sum_in_available += net.bw_in_available;
904 // sum_in_assigned += net.bw_in_assigned;
905 // sum_out_available += net.bw_out_available;
906 // sum_out_assigned += net.bw_out_assigned;
909 // ratio_in = ((double) sum_in_assigned) / ((double) sum_in_available);
910 // ratio_out = ((double) sum_out_assigned) / ((double) sum_out_available);
912 // // global reward in [1,2]
913 // return ratio_in +1;
914 // return ((ratio_in + ratio_out) / 2) + 1;
918 //envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
920 // const double *preferences;
921 // const double *properties;
923 // double pref_match = 0;
927 // preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
929 // properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
930 // agent->address_inuse);
933 // prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
934 // dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
936 // // utilization in [0,1]
937 // bw_norm = (((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
938 // / (double) ril_get_max_bw (agent, GNUNET_YES))
939 // + ((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
940 // / (double) ril_get_max_bw (agent, GNUNET_NO))) / 2;
942 // // preference matching in [0,4]
943 // pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
944 // pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
946 // // local reward in [1,2]
947 // return (pref_match / 4) +1;
951 * Gets the reward for the last performed step, which is calculated in equal
952 * parts from the local (the peer specific) and the global (for all peers
955 * @param solver the solver handle
956 * @param agent the agent handle
960 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
962 struct RIL_Network *net;
963 // double reward = 0;
964 long long overutilized_in = 0;
965 // long long overutilized_out;
966 long long assigned_in = 0;
967 // long long assigned_out = 0;
970 //punish overutilization
971 net = agent->address_inuse->solver_information;
973 if (net->bw_in_assigned > net->bw_in_available)
975 overutilized_in = (net->bw_in_assigned - net->bw_in_available);
976 assigned_in = net->bw_in_available;
980 assigned_in = net->bw_in_assigned;
982 // if (net->bw_out_assigned > net->bw_out_available)
984 // overutilized_out = (net->bw_out_assigned - net->bw_out_available);
985 // assigned_out = net->bw_out_available;
989 // assigned_out = net->bw_out_assigned;
992 // unused = net->bw_in_available - net->bw_in_assigned;
993 // unused = unused < 0 ? unused : -unused;
995 return (double) (assigned_in - overutilized_in) / 1024;
997 // reward += envi_reward_global (solver) * (solver->parameters.reward_global_share);
998 // reward += envi_reward_local (solver, agent) * (1 - solver->parameters.reward_global_share);
1000 // return (reward - 1.) * 100;
1004 * Doubles the bandwidth for the active address
1006 * @param solver solver handle
1007 * @param agent agent handle
1008 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1011 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1012 struct RIL_Peer_Agent *agent,
1015 unsigned long long new_bw;
1019 new_bw = agent->bw_in * 2;
1020 if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
1021 new_bw = GNUNET_ATS_MaxBandwidth;
1022 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1023 agent->bw_out, GNUNET_NO);
1027 new_bw = agent->bw_out * 2;
1028 if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth)
1029 new_bw = GNUNET_ATS_MaxBandwidth;
1030 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1036 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1037 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1039 * @param solver solver handle
1040 * @param agent agent handle
1041 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1045 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1046 struct RIL_Peer_Agent *agent,
1049 unsigned long long new_bw;
1053 new_bw = agent->bw_in / 2;
1054 if (new_bw < MIN_BW || new_bw > agent->bw_in)
1056 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1061 new_bw = agent->bw_out / 2;
1062 if (new_bw < MIN_BW || new_bw > agent->bw_out)
1064 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1070 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1072 * @param solver solver handle
1073 * @param agent agent handle
1074 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1078 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1080 unsigned long long new_bw;
1084 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1085 if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
1086 new_bw = GNUNET_ATS_MaxBandwidth;
1087 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1088 agent->bw_out, GNUNET_NO);
1092 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1093 if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth)
1094 new_bw = GNUNET_ATS_MaxBandwidth;
1095 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1101 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1102 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1104 * @param solver solver handle
1105 * @param agent agent handle
1106 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1110 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1112 unsigned long long new_bw;
1116 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1117 if (new_bw < MIN_BW || new_bw > agent->bw_in)
1119 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1124 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * MIN_BW);
1125 if (new_bw < MIN_BW || new_bw > agent->bw_out)
1127 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1133 * Switches to the address given by its index
1135 * @param solver solver handle
1136 * @param agent agent handle
1137 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1140 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1141 struct RIL_Peer_Agent *agent,
1142 unsigned int address_index)
1144 struct RIL_Address_Wrapped *cur;
1147 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1149 if (i == address_index)
1151 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1159 //no address with address_index exists, in this case this action should not be callable
1160 GNUNET_assert(GNUNET_NO);
1164 * Puts the action into effect by calling the according function
1166 * @param solver the solver handle
1167 * @param agent the action handle
1168 * @param action the action to perform by the solver
1171 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1177 case RIL_ACTION_NOTHING:
1179 case RIL_ACTION_BW_IN_DBL:
1180 envi_action_bw_double (solver, agent, GNUNET_YES);
1182 case RIL_ACTION_BW_IN_HLV:
1183 envi_action_bw_halven (solver, agent, GNUNET_YES);
1185 case RIL_ACTION_BW_IN_INC:
1186 envi_action_bw_inc (solver, agent, GNUNET_YES);
1188 case RIL_ACTION_BW_IN_DEC:
1189 envi_action_bw_dec (solver, agent, GNUNET_YES);
1191 case RIL_ACTION_BW_OUT_DBL:
1192 envi_action_bw_double (solver, agent, GNUNET_NO);
1194 case RIL_ACTION_BW_OUT_HLV:
1195 envi_action_bw_halven (solver, agent, GNUNET_NO);
1197 case RIL_ACTION_BW_OUT_INC:
1198 envi_action_bw_inc (solver, agent, GNUNET_NO);
1200 case RIL_ACTION_BW_OUT_DEC:
1201 envi_action_bw_dec (solver, agent, GNUNET_NO);
1204 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1206 address_index = action - RIL_ACTION_TYPE_NUM;
1208 GNUNET_assert(address_index >= 0);
1210 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1212 envi_action_address_switch (solver, agent, address_index);
1215 // error - action does not exist
1216 GNUNET_assert(GNUNET_NO);
1221 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1222 * after having done the last action a_old. It observes the new state s_next and the reward
1223 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1224 * next action is put into effect.
1226 * @param agent the agent performing the step
1229 agent_step (struct RIL_Peer_Agent *agent)
1231 int a_next = RIL_ACTION_INVALID;
1236 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1237 GNUNET_i2s (&agent->peer),
1238 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1240 s_next = envi_get_state (agent->envi, agent);
1241 reward = envi_get_reward (agent->envi, agent);
1242 explore = agent_decide_exploration (agent);
1244 switch (agent->envi->parameters.algorithm)
1246 case RIL_ALGO_SARSA:
1249 a_next = agent_get_action_explore (agent, s_next);
1253 a_next = agent_get_action_best (agent, s_next);
1255 if (RIL_ACTION_INVALID != agent->a_old)
1257 //updates weights with selected action (on-policy), if not first step
1258 agent_update_weights (agent, reward, s_next, a_next);
1259 agent_modify_eligibility (agent, RIL_E_SET, s_next);
1264 a_next = agent_get_action_best (agent, s_next);
1265 if (RIL_ACTION_INVALID != agent->a_old)
1267 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1268 agent_update_weights (agent, reward, s_next, a_next);
1272 a_next = agent_get_action_explore (agent, s_next);
1273 agent_modify_eligibility (agent, RIL_E_ZERO, NULL);
1277 a_next = agent_get_action_best (agent, s_next);
1278 agent_modify_eligibility (agent, RIL_E_SET, s_next);
1283 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1285 agent_modify_eligibility (agent, RIL_E_ACCUMULATE, s_next);
1287 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1294 envi_do_action (agent->envi, agent, a_next);
1296 GNUNET_free(agent->s_old);
1297 agent->s_old = s_next;
1298 agent->a_old = a_next;
1300 agent->step_count += 1;
1304 ril_step (struct GAS_RIL_Handle *solver);
1307 * Task for the scheduler, which performs one step and lets the solver know that
1308 * no further step is scheduled.
1310 * @param cls the solver handle
1311 * @param tc the task context for the scheduler
1314 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1316 struct GAS_RIL_Handle *solver = cls;
1318 solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1323 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1326 struct RIL_Network net;
1327 unsigned long long sum_assigned = 0;
1328 unsigned long long sum_available = 0;
1331 for (i = 0; i < solver->networks_count; i++)
1333 net = solver->network_entries[i];
1334 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1336 sum_assigned += net.bw_in_assigned;
1337 sum_assigned += net.bw_out_assigned;
1338 sum_available += net.bw_in_available;
1339 sum_available += net.bw_out_available;
1342 if (sum_available > 0)
1344 ratio = ((double) sum_assigned) / ((double) sum_available);
1351 return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
1355 * Lookup network struct by type
1357 * @param s the solver handle
1358 * @param type the network type
1359 * @return the network struct
1361 static struct RIL_Network *
1362 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1366 for (i = 0; i < s->networks_count; i++)
1368 if (s->network_entries[i].type == type)
1370 return &s->network_entries[i];
1377 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1379 struct RIL_Network *net;
1380 struct RIL_Peer_Agent *agent;
1381 unsigned long long address_count = 0;
1383 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1385 if (agent->address_inuse && agent->is_active)
1387 net = agent->address_inuse->solver_information;
1388 if (net->type == network)
1395 net = ril_get_network (solver, network);
1396 return (net->bw_in_available > MIN_BW * address_count) && (net->bw_out_available > MIN_BW * address_count);
1400 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1402 struct RIL_Address_Wrapped *addr_wrap;
1403 struct RIL_Network *net;
1405 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1407 net = addr_wrap->address_naked->solver_information;
1408 if (ril_network_is_not_full(solver, net->type))
1410 if (NULL == agent->address_inuse)
1411 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, MIN_BW, MIN_BW, silent);
1415 agent->address_inuse = NULL;
1419 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1421 struct GNUNET_TIME_Absolute time_now;
1422 struct GNUNET_TIME_Relative time_delta;
1425 // MDP case - remove when debugged
1426 if (solver->simulate)
1428 solver->global_discount_variable = solver->parameters.gamma;
1429 solver->global_discount_integrated = 1;
1435 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1436 time_now = GNUNET_TIME_absolute_get ();
1437 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1438 solver->step_time_last = time_now;
1439 tau = (double) time_delta.rel_value_us
1440 / (double) solver->parameters.step_time_min.rel_value_us;
1442 //calculate reward discounts (once per step for all agents)
1443 solver->global_discount_variable = pow (M_E, ((-1.) * ((double) solver->parameters.beta) * tau));
1444 solver->global_discount_integrated = (1. - solver->global_discount_variable)
1445 / (double) solver->parameters.beta;
1449 ril_calculate_assigned_bwnet (struct GAS_RIL_Handle *solver)
1452 struct RIL_Network *net;
1454 for (c = 0; c < solver->networks_count; c++)
1456 net = &solver->network_entries[c];
1457 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1458 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1463 * Schedules the next global step in an adaptive way. The more resources are
1464 * left, the earlier the next step is scheduled. This serves the reactivity of
1465 * the solver to changed inputs.
1467 * @param solver the solver handle
1470 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1476 struct GNUNET_TIME_Relative time_next;
1478 used_ratio = ril_get_used_resource_ratio (solver);
1481 solver->parameters.step_time_min.rel_value_us
1482 <= solver->parameters.step_time_max.rel_value_us);
1484 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1485 solver->parameters.step_time_min).rel_value_us;
1486 offset = (double) solver->parameters.step_time_min.rel_value_us;
1487 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1489 GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1490 GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1492 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1494 if (solver->simulate)
1496 time_next = GNUNET_TIME_UNIT_ZERO;
1499 if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1501 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1507 * Triggers one step per agent
1511 ril_step (struct GAS_RIL_Handle *solver)
1513 struct RIL_Peer_Agent *cur;
1515 if (GNUNET_YES == solver->bulk_lock)
1517 solver->bulk_changes++;
1521 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1523 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1525 if (0 == solver->step_count)
1527 solver->step_time_last = GNUNET_TIME_absolute_get ();
1530 ril_calculate_discount (solver);
1531 ril_calculate_assigned_bwnet (solver);
1533 //calculate network state vector
1534 // envi_state_networks(solver);
1536 //trigger one step per active, unblocked agent
1537 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1541 if (NULL == cur->address_inuse)
1543 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1545 if (cur->address_inuse)
1552 ril_calculate_assigned_bwnet (solver);
1554 solver->step_count += 1;
1555 ril_step_schedule_next (solver);
1557 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1559 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1560 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1562 if (cur->suggestion_issue) {
1563 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1564 cur->suggestion_issue = GNUNET_NO;
1567 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1571 ril_count_agents (struct GAS_RIL_Handle *solver)
1574 struct RIL_Peer_Agent *cur_agent;
1576 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1584 agent_w_start (struct RIL_Peer_Agent *agent)
1587 struct RIL_Peer_Agent *other;
1591 count = ril_count_agents(agent->envi);
1593 for (i = 0; i < agent->n; i++)
1595 for (k = 0; k < agent->m; k++)
1598 agent->W[i][k] = 1;//.1 - ((double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX/5)/(double)UINT32_MAX);
1601 for (other = agent->envi->agents_head; NULL != other; other = other->next)
1603 agent->W[i][k] += (other->W[i][k] / (double) count);
1607 GNUNET_assert(!isinf(agent->W[i][k]));
1613 * Initialize an agent without addresses and its knowledge base
1615 * @param s ril solver
1616 * @param peer the one in question
1617 * @return handle to the new agent
1619 static struct RIL_Peer_Agent *
1620 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1623 struct GAS_RIL_Handle * solver = s;
1624 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
1626 agent->envi = solver;
1627 agent->peer = *peer;
1628 agent->step_count = 0;
1629 agent->is_active = GNUNET_NO;
1630 agent->bw_in = MIN_BW;
1631 agent->bw_out = MIN_BW;
1632 agent->suggestion_issue = GNUNET_NO;
1633 agent->n = RIL_ACTION_TYPE_NUM;
1634 agent->m = (RIL_FEATURES_NETWORK_COUNT);// + GNUNET_ATS_PreferenceCount;
1635 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1636 for (i = 0; i < agent->n; i++)
1638 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1640 agent_w_start(agent);
1641 agent->a_old = RIL_ACTION_INVALID;
1642 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1643 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1644 agent_modify_eligibility (agent, RIL_E_ZERO, NULL);
1652 * @param solver the solver handle
1653 * @param agent the agent to retire
1656 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1660 for (i = 0; i < agent->n; i++)
1662 GNUNET_free(agent->W[i]);
1664 GNUNET_free(agent->W);
1665 GNUNET_free(agent->e);
1666 GNUNET_free(agent->s_old);
1671 * Returns the agent for a peer
1673 * @param solver the solver handle
1674 * @param peer the identity of the peer
1675 * @param create whether or not to create an agent, if none is allocated yet
1678 static struct RIL_Peer_Agent *
1679 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1681 struct RIL_Peer_Agent *cur;
1683 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1685 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1693 cur = agent_init (solver, peer);
1694 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
1701 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1702 * considered inactive and not used. Addresses in an inactive network are ignored.
1704 * @param solver solver handle
1705 * @param network the network type
1706 * @return whether or not the network is considered active
1709 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1711 struct RIL_Network *net;
1713 net = ril_get_network (solver, network);
1714 return net->bw_out_available >= MIN_BW;
1718 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1719 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1720 * frees the memory of the old vector, and redirects the pointer to the new one.
1722 * @param old pointer to the pointer to the first element of the vector
1723 * @param element_size byte size of the vector elements
1724 * @param hole_start the first element to cut out
1725 * @param hole_length the number of elements to cut out
1726 * @param old_length the length of the old vector
1729 ril_cut_from_vector (void **old,
1730 size_t element_size,
1731 unsigned int hole_start,
1732 unsigned int hole_length,
1733 unsigned int old_length)
1736 char *oldptr = (char *) *old;
1738 unsigned int bytes_before;
1739 unsigned int bytes_hole;
1740 unsigned int bytes_after;
1742 GNUNET_assert(old_length > hole_length);
1743 GNUNET_assert(old_length >= (hole_start + hole_length));
1745 size = element_size * (old_length - hole_length);
1747 bytes_before = element_size * hole_start;
1748 bytes_hole = element_size * hole_length;
1749 bytes_after = element_size * (old_length - hole_start - hole_length);
1757 tmpptr = GNUNET_malloc (size);
1758 memcpy (tmpptr, oldptr, bytes_before);
1759 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1765 *old = (void *) tmpptr;
1769 * Solver API functions
1770 * ---------------------------
1774 * Change relative preference for quality in solver
1776 * @param solver the solver handle
1777 * @param peer the peer to change the preference for
1778 * @param kind the kind to change the preference
1779 * @param pref_rel the normalized preference value for this kind over all clients
1782 GAS_ril_address_change_preference (void *solver,
1783 const struct GNUNET_PeerIdentity *peer,
1784 enum GNUNET_ATS_PreferenceKind kind,
1787 LOG(GNUNET_ERROR_TYPE_DEBUG,
1788 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1789 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1795 * Entry point for the plugin
1797 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1800 libgnunet_plugin_ats_ril_init (void *cls)
1802 struct GNUNET_ATS_PluginEnvironment *env = cls;
1803 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1804 struct RIL_Network * cur;
1808 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1810 GNUNET_assert(NULL != env);
1811 GNUNET_assert(NULL != env->cfg);
1812 GNUNET_assert(NULL != env->stats);
1813 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1814 GNUNET_assert(NULL != env->get_preferences);
1815 GNUNET_assert(NULL != env->get_property);
1818 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
1819 &solver->parameters.step_time_min))
1821 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
1824 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
1825 &solver->parameters.step_time_max))
1827 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
1829 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
1831 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
1832 GNUNET_free (string);
1836 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1838 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
1840 solver->parameters.beta = strtod (string, NULL);
1841 GNUNET_free (string);
1845 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1847 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
1849 solver->parameters.gamma = strtod (string, NULL);
1850 GNUNET_free (string);
1854 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
1857 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
1859 solver->parameters.alpha = strtod (string, NULL);
1860 GNUNET_free (string);
1864 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1866 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
1868 solver->parameters.lambda = strtod (string, NULL);
1869 GNUNET_free (string);
1873 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1875 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
1877 solver->parameters.explore_ratio = strtod (string, NULL);
1878 GNUNET_free (string);
1882 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
1884 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
1886 solver->parameters.reward_global_share = strtod (string, NULL);
1887 GNUNET_free (string);
1891 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
1893 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
1895 solver->simulate = GNUNET_NO;
1898 env->sf.s_add = &GAS_ril_address_add;
1899 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1900 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1901 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
1902 env->sf.s_address_update_network = &GAS_ril_address_change_network;
1903 env->sf.s_get = &GAS_ril_get_preferred_address;
1904 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
1905 env->sf.s_pref = &GAS_ril_address_change_preference;
1906 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
1907 env->sf.s_del = &GAS_ril_address_delete;
1908 env->sf.s_bulk_start = &GAS_ril_bulk_start;
1909 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
1911 solver->plugin_envi = env;
1912 solver->networks_count = env->network_count;
1913 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
1914 solver->step_count = 0;
1915 solver->global_state_networks = GNUNET_malloc (solver->networks_count * RIL_FEATURES_NETWORK_COUNT * sizeof (double));
1916 solver->done = GNUNET_NO;
1918 for (c = 0; c < env->network_count; c++)
1920 cur = &solver->network_entries[c];
1921 cur->type = env->networks[c];
1922 cur->bw_in_available = env->in_quota[c];
1923 cur->bw_out_available = env->out_quota[c];
1924 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);
1927 LOG(GNUNET_ERROR_TYPE_INFO, "init() Parameters:\n");
1928 LOG(GNUNET_ERROR_TYPE_INFO, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
1929 solver->parameters.algorithm ? "Q" : "SARSA",
1930 solver->parameters.alpha,
1931 solver->parameters.beta,
1932 solver->parameters.lambda);
1933 LOG(GNUNET_ERROR_TYPE_INFO, "init() explore = %f, global_share = %f\n",
1934 solver->parameters.explore_ratio,
1935 solver->parameters.reward_global_share);
1941 * Exit point for the plugin
1943 * @param cls the solver handle
1946 libgnunet_plugin_ats_ril_done (void *cls)
1948 struct GAS_RIL_Handle *s = cls;
1949 struct RIL_Peer_Agent *cur_agent;
1950 struct RIL_Peer_Agent *next_agent;
1952 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1954 s->done = GNUNET_YES;
1956 cur_agent = s->agents_head;
1957 while (NULL != cur_agent)
1959 next_agent = cur_agent->next;
1960 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1961 agent_die (s, cur_agent);
1962 cur_agent = next_agent;
1965 if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
1967 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
1969 GNUNET_free(s->network_entries);
1970 GNUNET_free(s->global_state_networks);
1977 * Add a new address for a peer to the solver
1979 * The address is already contained in the addresses hashmap!
1981 * @param solver the solver Handle
1982 * @param address the address to add
1983 * @param network network type of this address
1986 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1988 struct GAS_RIL_Handle *s = solver;
1989 struct RIL_Peer_Agent *agent;
1990 struct RIL_Address_Wrapped *address_wrapped;
1991 struct RIL_Network *net;
1999 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
2001 net = ril_get_network (s, network);
2002 address->solver_information = net;
2004 if (!ril_network_is_active (s, network))
2006 LOG(GNUNET_ERROR_TYPE_DEBUG,
2007 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2008 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2012 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2015 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
2016 address_wrapped->address_naked = address;
2017 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2019 //increase size of W
2020 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
2022 n_new = agent->n + 1;
2025 GNUNET_array_grow(agent->W, agent->n, n_new);
2026 for (i = 0; i < n_new; i++)
2031 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2036 GNUNET_array_grow(agent->W[i], zero, m_new);
2040 //increase size of old state vector
2042 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2045 GNUNET_array_grow(agent->e, agent->m, m_new);
2047 ril_try_unblock_agent(s, agent, GNUNET_NO);
2051 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2052 address->active ? "active" : "inactive", address->plugin, address->addr,
2053 GNUNET_i2s (&address->peer));
2057 * Delete an address in the solver
2059 * The address is not contained in the address hashmap anymore!
2061 * @param solver the solver handle
2062 * @param address the address to remove
2063 * @param session_only delete only session not whole address
2066 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
2068 struct GAS_RIL_Handle *s = solver;
2069 struct RIL_Peer_Agent *agent;
2070 struct RIL_Address_Wrapped *address_wrapped;
2071 int address_was_used = address->active;
2076 struct RIL_Network *net;
2078 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2079 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2080 address->addr, GNUNET_i2s (&address->peer));
2082 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2085 net = address->solver_information;
2086 GNUNET_assert(!ril_network_is_active (s, net->type));
2087 LOG(GNUNET_ERROR_TYPE_DEBUG,
2088 "No agent allocated for peer yet, since address was in inactive network\n");
2092 address_index = agent_address_get_index (agent, address);
2093 address_wrapped = agent_address_get (agent, address);
2095 if (NULL == address_wrapped)
2097 net = address->solver_information;
2098 GNUNET_assert(!ril_network_is_active (s, net->type));
2099 LOG(GNUNET_ERROR_TYPE_DEBUG,
2100 "Address not considered by agent, address was in inactive network\n");
2104 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2105 GNUNET_free(address_wrapped);
2108 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
2109 n_new = agent->n - 1;
2111 LOG(GNUNET_ERROR_TYPE_DEBUG, "first\n");
2113 for (i = 0; i < agent->n; i++)
2115 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2116 //((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
2117 ((RIL_FEATURES_NETWORK_COUNT) //TODO! replace, when adding more networks
2118 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
2120 GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2121 LOG(GNUNET_ERROR_TYPE_DEBUG, "second\n");
2122 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2124 //correct last action
2125 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2129 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2131 agent->a_old = RIL_ACTION_INVALID;
2133 //decrease old state vector and eligibility vector
2134 LOG(GNUNET_ERROR_TYPE_DEBUG, "third\n");
2135 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2136 //((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
2137 ((RIL_FEATURES_NETWORK_COUNT) //TODO! replace when adding more networks
2138 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
2139 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
2140 //((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
2141 ((RIL_FEATURES_NETWORK_COUNT) //TODO! replace when adding more networks
2142 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
2146 if (address_was_used)
2148 if (NULL != agent->addresses_head) //if peer has an address left, use it
2150 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, MIN_BW, MIN_BW,
2155 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2163 * Update the properties of an address in the solver
2165 * @param solver solver handle
2166 * @param address the address
2167 * @param type the ATSI type in HBO
2168 * @param abs_value the absolute value of the property
2169 * @param rel_value the normalized value
2172 GAS_ril_address_property_changed (void *solver,
2173 struct ATS_Address *address,
2178 LOG(GNUNET_ERROR_TYPE_DEBUG,
2179 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2180 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2181 address->addr, rel_value);
2187 * Update the session of an address in the solver
2189 * NOTE: values in addresses are already updated
2191 * @param solver solver handle
2192 * @param address the address
2193 * @param cur_session the current session
2194 * @param new_session the new session
2197 GAS_ril_address_session_changed (void *solver,
2198 struct ATS_Address *address,
2199 uint32_t cur_session,
2200 uint32_t new_session)
2203 * TODO? Future Work: Potentially add session activity as a feature in state vector
2205 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2209 * Notify the solver that an address is (not) actively used by transport
2210 * to communicate with a remote peer
2212 * NOTE: values in addresses are already updated
2214 * @param solver solver handle
2215 * @param address the address
2216 * @param in_use usage state
2219 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2222 * TODO? Future Work: Potentially add usage variable to state vector
2224 LOG(GNUNET_ERROR_TYPE_DEBUG,
2225 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2226 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2230 * Notify solver that the network an address is located in has changed
2232 * NOTE: values in addresses are already updated
2234 * @param solver solver handle
2235 * @param address the address
2236 * @param current_network the current network
2237 * @param new_network the new network
2240 GAS_ril_address_change_network (void *solver,
2241 struct ATS_Address *address,
2242 uint32_t current_network,
2243 uint32_t new_network)
2245 struct GAS_RIL_Handle *s = solver;
2246 struct RIL_Peer_Agent *agent;
2248 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2249 "%s address of peer %s from '%s' to '%s'\n",
2250 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2251 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2253 if (address->active && !ril_network_is_active (solver, new_network))
2255 GAS_ril_address_delete (solver, address, GNUNET_NO);
2259 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2262 GNUNET_assert(!ril_network_is_active (solver, current_network));
2264 GAS_ril_address_add (s, address, new_network);
2268 address->solver_information = ril_get_network(solver, new_network);
2272 * Give feedback about the current assignment
2274 * @param solver the solver handle
2275 * @param application the application
2276 * @param peer the peer to change the preference for
2277 * @param scope the time interval for this feedback: [now - scope .. now]
2278 * @param kind the kind to change the preference
2279 * @param score the score
2282 GAS_ril_address_preference_feedback (void *solver,
2284 const struct GNUNET_PeerIdentity *peer,
2285 const struct GNUNET_TIME_Relative scope,
2286 enum GNUNET_ATS_PreferenceKind kind,
2289 LOG(GNUNET_ERROR_TYPE_DEBUG,
2290 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2291 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2292 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2296 * Start a bulk operation
2298 * @param solver the solver
2301 GAS_ril_bulk_start (void *solver)
2303 struct GAS_RIL_Handle *s = solver;
2305 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2311 * Bulk operation done
2313 * @param solver the solver handle
2316 GAS_ril_bulk_stop (void *solver)
2318 struct GAS_RIL_Handle *s = solver;
2320 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2322 if (s->bulk_lock < 1)
2329 if (0 < s->bulk_changes)
2332 s->bulk_changes = 0;
2337 * Tell solver to notify ATS if the address to use changes for a specific
2338 * peer using the bandwidth changed callback
2340 * The solver must only notify about changes for peers with pending address
2343 * @param solver the solver handle
2344 * @param peer the identity of the peer
2346 const struct ATS_Address *
2347 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2350 * activate agent, return currently chosen address
2352 struct GAS_RIL_Handle *s = solver;
2353 struct RIL_Peer_Agent *agent;
2355 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2357 agent = ril_get_agent (s, peer, GNUNET_YES);
2359 agent->is_active = GNUNET_YES;
2360 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2362 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2364 if (agent->address_inuse)
2366 LOG(GNUNET_ERROR_TYPE_DEBUG,
2367 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2368 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2372 LOG(GNUNET_ERROR_TYPE_DEBUG,
2373 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2377 return agent->address_inuse;
2381 * Tell solver stop notifying ATS about changes for this peers
2383 * The solver must only notify about changes for peers with pending address
2386 * @param solver the solver handle
2387 * @param peer the peer
2390 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2392 struct GAS_RIL_Handle *s = solver;
2393 struct RIL_Peer_Agent *agent;
2395 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2397 agent = ril_get_agent (s, peer, GNUNET_NO);
2404 if (GNUNET_NO == agent->is_active)
2410 agent->is_active = GNUNET_NO;
2412 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2417 LOG(GNUNET_ERROR_TYPE_DEBUG,
2418 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2419 GNUNET_i2s (peer), agent->address_inuse->plugin);
2422 /* end of plugin_ats_ril.c */