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/libgnunet_plugin_ats_ril.c
23 * @brief ATS reinforcement learning solver
24 * @author Fabian Oehlmann
25 * @author Matthias Wachs
27 #include "libgnunet_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
49 * ATS reinforcement learning solver
55 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
56 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
57 * to a particular address. The action of switching to address with index i is depicted by the
58 * number (RIL_ACTION_TYPE_NUM + i).
62 RIL_ACTION_NOTHING = -1,
63 RIL_ACTION_BW_IN_DBL = -2, //TODO! put actions back
64 RIL_ACTION_BW_IN_HLV = -3,
65 RIL_ACTION_BW_IN_INC = 0,
66 RIL_ACTION_BW_IN_DEC = 1,
67 RIL_ACTION_BW_OUT_DBL = -4,
68 RIL_ACTION_BW_OUT_HLV = -5,
69 RIL_ACTION_BW_OUT_INC = -6,
70 RIL_ACTION_BW_OUT_DEC = -7,
71 RIL_ACTION_TYPE_NUM = 2
80 enum RIL_E_Modification
89 * Global learning parameters
91 struct RIL_Learning_Parameters
94 * The TD-algorithm to use
96 enum RIL_Algorithm algorithm;
99 * Gradient-descent step-size
104 * Learning discount variable in the TD-update for semi-MDPs
109 * Learning discount factor in the TD-update for MDPs
114 * Trace-decay factor for eligibility traces
119 * Ratio, with what probability an agent should explore in the e-greed policy
121 double explore_ratio;
124 * How big the share of the global part of the reward signal is
126 double reward_global_share;
129 * Minimal interval time between steps in milliseconds
131 struct GNUNET_TIME_Relative step_time_min;
134 * Maximum interval time between steps in milliseconds
136 struct GNUNET_TIME_Relative step_time_max;
140 * Wrapper for addresses to store them in agent's linked list
142 struct RIL_Address_Wrapped
147 struct RIL_Address_Wrapped *next;
152 struct RIL_Address_Wrapped *prev;
157 struct ATS_Address *address_naked;
160 struct RIL_Peer_Agent
163 * Next agent in solver's linked list
165 struct RIL_Peer_Agent *next;
168 * Previous agent in solver's linked list
170 struct RIL_Peer_Agent *prev;
175 struct GAS_RIL_Handle *envi;
180 struct GNUNET_PeerIdentity peer;
183 * Whether the agent is active or not
188 * Number of performed time-steps
190 unsigned long long step_count;
193 * Experience matrix W
198 * Number of rows of W / Number of state-vector features
203 * Number of columns of W / Number of actions
208 * Last perceived state feature vector
218 * Eligibility trace vector
225 struct ATS_Address * address_inuse;
228 * Head of addresses DLL
230 struct RIL_Address_Wrapped * addresses_head;
233 * Tail of addresses DLL
235 struct RIL_Address_Wrapped * addresses_tail;
238 * Inbound bandwidth assigned by the agent
240 unsigned long long bw_in;
243 * Outbound bandwidth assigned by the agent
245 unsigned long long bw_out;
248 * Flag whether a suggestion has to be issued
250 int suggestion_issue;
253 * The address which has to be issued
255 struct ATS_Address * suggestion_address;
263 enum GNUNET_ATS_Network_Type type;
266 * Total available inbound bandwidth
268 unsigned long long bw_in_available;
271 * Bandwidth inbound assigned in network after last step
273 unsigned long long bw_in_assigned;
276 * Total available outbound bandwidth
278 unsigned long long bw_out_available;
281 * * Bandwidth outbound assigned in network after last step
283 unsigned long long bw_out_assigned;
287 * A handle for the reinforcement learning solver
289 struct GAS_RIL_Handle
292 * The solver-plugin environment of the solver-plugin API
294 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
299 struct GNUNET_STATISTICS_Handle *stats;
302 * Number of performed steps
304 unsigned long long step_count;
307 * Timestamp for the last time-step
309 struct GNUNET_TIME_Absolute step_time_last;
312 * Task identifier of the next time-step to be executed
314 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
317 * Variable discount factor, dependent on time between steps
319 double global_discount_variable;
322 * Integrated variable discount factor, dependent on time between steps
324 double global_discount_integrated;
327 * State vector for networks for the current step
329 double *global_state_networks;
332 * Lock for bulk operations
337 * Number of changes during a lock
342 * Learning parameters
344 struct RIL_Learning_Parameters parameters;
347 * Array of networks with global assignment state
349 struct RIL_Network * network_entries;
354 unsigned int networks_count;
357 * List of active peer-agents
359 struct RIL_Peer_Agent * agents_head;
360 struct RIL_Peer_Agent * agents_tail;
370 * ---------------------------
374 ril_count_agents(struct GAS_RIL_Handle * solver);
377 * Estimate the current action-value for state s and action a
379 * @param agent agent performing the estimation
382 * @return estimation value
385 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
390 for (i = 0; i < agent->m; i++)
392 result += state[i] * agent->W[action][i];
395 GNUNET_assert(!isnan(result));
399 GNUNET_assert(GNUNET_NO);
400 return isinf(result) * (DBL_MAX / 2); //TODO! fix
407 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
408 * currently estimated best action) in the current step
410 * @param agent agent performing the step
411 * @return yes, if exploring
414 agent_decide_exploration (struct RIL_Peer_Agent *agent)
416 //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
417 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
418 UINT32_MAX) / (double) UINT32_MAX;
420 if (r < agent->envi->parameters.explore_ratio)
428 * Get the index of the address in the agent's list.
430 * @param agent agent handle
431 * @param address address handle
432 * @return the index, starting with zero
435 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
438 struct RIL_Address_Wrapped *cur;
441 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
444 if (cur->address_naked == address)
454 * Gets the wrapped address from the agent's list
456 * @param agent agent handle
457 * @param address address handle
458 * @return wrapped address
460 static struct RIL_Address_Wrapped *
461 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
463 struct RIL_Address_Wrapped *cur;
465 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
467 if (cur->address_naked == address)
477 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
478 * most reward in the future)
480 * @param agent agent performing the calculation
481 * @param state the state from which to take the action
482 * @return the action promising most future reward
485 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
488 int max_i = RIL_ACTION_INVALID;
490 double max_q = -DBL_MAX;
492 for (i = 0; i < agent->n; i++)
494 cur_q = agent_estimate_q (agent, state, i);
502 GNUNET_assert(RIL_ACTION_INVALID != max_i);
508 * Gets any action, to explore the action space from that state
510 * @param agent agent performing the calculation
511 * @param state the state from which to take the action
515 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
517 // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
518 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
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, "Y*r = %f y*Q(s+1,a+1) = %f Q(s,a) = %f\n, y = %f\n",
541 // agent->envi->global_discount_integrated * reward,
542 // agent->envi->global_discount_variable * agent_estimate_q (agent, s_next, a_prime),
543 // agent_estimate_q (agent, agent->s_old, agent->a_old),
544 // agent->envi->global_discount_variable);
546 // LOG(GNUNET_ERROR_TYPE_INFO, "delta = %f\n", delta);
548 for (i = 0; i < agent->m; i++)
550 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
551 // agent->envi->parameters.alpha,
555 theta[i] += agent->envi->parameters.alpha * delta * agent->s_old[i];// * agent->e[i];
560 * Changes the eligibility trace vector e in various manners:
561 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
562 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
563 * RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
564 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
566 * @param agent the agent handle
567 * @param mod the kind of modification
570 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod, double *f)
573 double *e = agent->e;
575 for (i = 0; i < agent->m; i++)
579 case RIL_E_ACCUMULATE:
586 e[i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
596 ril_inform (struct GAS_RIL_Handle *solver,
597 enum GAS_Solver_Operation op,
598 enum GAS_Solver_Status stat)
600 if (NULL != solver->plugin_envi->info_cb)
601 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
605 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
606 * notify ATS of its new decision
608 * @param solver solver handle
609 * @param agent agent handle
610 * @param new_address the address which is to be used
611 * @param new_bw_in the new amount of inbound bandwidth set for this address
612 * @param new_bw_out the new amount of outbound bandwidth set for this address
613 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
616 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
617 struct RIL_Peer_Agent *agent,
618 struct ATS_Address *new_address,
619 unsigned long long new_bw_in,
620 unsigned long long new_bw_out,
623 int notify = GNUNET_NO;
625 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
628 if (agent->address_inuse != new_address)
630 if (NULL != agent->address_inuse)
632 agent->address_inuse->active = GNUNET_NO;
633 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
634 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
636 if (NULL != new_address)
638 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
639 new_address->active = agent->is_active;
640 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
641 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
643 notify |= GNUNET_YES;
649 if (new_address->active != agent->is_active)
651 new_address->active = agent->is_active;
652 notify |= GNUNET_YES;
656 if (agent->bw_in != new_bw_in)
658 agent->bw_in = new_bw_in;
659 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
660 notify |= GNUNET_YES;
662 if (agent->bw_out != new_bw_out)
664 agent->bw_out = new_bw_out;
665 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
666 notify |= GNUNET_YES;
670 if (notify && agent->is_active && (GNUNET_NO == silent))
674 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
675 agent->suggestion_issue = GNUNET_YES;
676 agent->suggestion_address = new_address;
678 else if (agent->address_inuse)
680 //disconnect case, no new address
681 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
682 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
686 agent->suggestion_issue = GNUNET_YES;
687 agent->suggestion_address = agent->address_inuse;
690 agent->address_inuse = new_address;
693 static unsigned long long
694 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
696 struct RIL_Peer_Agent *cur;
697 struct RIL_Network *net;
698 unsigned long long sum = 0;
700 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
702 if (cur->is_active && cur->address_inuse)
704 net = cur->address_inuse->solver_information;
705 if (net->type == type)
719 envi_state_networks (struct GAS_RIL_Handle *solver)
722 struct RIL_Network net;
724 int overutilized_out;
726 for (i = 0; i < solver->networks_count; i++)
728 net = solver->network_entries[i];
730 overutilized_in = net.bw_in_assigned > net.bw_in_available;
731 overutilized_out = net.bw_out_assigned > net.bw_out_available;
733 solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 0] = ((double) net.bw_in_assigned / (double) net.bw_in_available)*10;
734 solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) overutilized_in;
735 solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 2] = ((double) net.bw_out_assigned / (double) net.bw_out_available)*10;
736 solver->global_state_networks[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) overutilized_out;
741 * Allocates a state vector and fills it with the features present
742 * @param solver the solver handle
743 * @param agent the agent handle
744 * @return pointer to the state vector
747 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
751 double *state = GNUNET_malloc (sizeof (double) * agent->m);
752 struct RIL_Address_Wrapped *cur_address;
753 // const double *preferences;
754 // const double *properties;
755 struct RIL_Network *net;
757 //copy global networks state
758 for (i = 0; i < solver->networks_count * RIL_FEATURES_NETWORK_COUNT; i++)
760 // state[i] = solver->global_state_networks[i];
762 net = agent->address_inuse->solver_information;
764 state[0] = (double) net->bw_in_assigned / 1024; //(double) net->bw_in_available;
765 if (net->bw_in_assigned > net->bw_in_available)
767 state[1] = (double)(net->bw_in_assigned - net->bw_in_available) / 1024;// net->bw_in_available;
773 LOG(GNUNET_ERROR_TYPE_INFO, "state[0] = %f\n", state[0]);
774 LOG(GNUNET_ERROR_TYPE_INFO, "state[1] = %f\n", state[1]);
776 LOG(GNUNET_ERROR_TYPE_INFO, "W / %08.3f %08.3f \\ \n", agent->W[0][0], agent->W[1][0]);
777 LOG(GNUNET_ERROR_TYPE_INFO, "W \\ %08.3f %08.3f / \n", agent->W[0][1], agent->W[1][1]);
781 // preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
783 // for (k = 0; k < GNUNET_ATS_PreferenceCount; k++)
785 // state[i++] = preferences[k];
788 //get address specific features
789 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
791 // //when changing the number of address specific state features, change RIL_FEATURES_ADDRESS_COUNT macro
792 // state[i++] = cur_address->address_naked->active;
793 // state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
794 // state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
795 // properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
796 // cur_address->address_naked);
797 // for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
799 // state[i++] = properties[k];
807 * For all networks a peer has an address in, this gets the maximum bandwidth which could
808 * theoretically be available in one of the networks. This is used for bandwidth normalization.
810 * @param agent the agent handle
811 * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
813 static unsigned long long
814 ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
817 * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
818 * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
820 unsigned long long max = 0;
821 struct RIL_Address_Wrapped *cur;
822 struct RIL_Network *net;
824 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
826 net = cur->address_naked->solver_information;
829 if (net->bw_in_available > max)
831 max = net->bw_in_available;
836 if (net->bw_out_available > max)
838 max = net->bw_out_available;
846 * Get the index of the quality-property in question
848 * @param type the quality property type
852 ril_find_property_index (uint32_t type)
854 int existing_types[] = GNUNET_ATS_QualityProperties;
856 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
857 if (existing_types[c] == type)
859 return GNUNET_SYSERR;
863 ril_get_atsi (struct ATS_Address *address, uint32_t type)
866 GNUNET_assert(NULL != address);
868 if ((NULL == address->atsi) || (0 == address->atsi_count))
871 for (c1 = 0; c1 < address->atsi_count; c1++)
873 if (ntohl (address->atsi[c1].type) == type)
874 return ntohl (address->atsi[c1].value);
880 envi_reward_global (struct GAS_RIL_Handle *solver)
883 struct RIL_Network net;
884 unsigned int sum_in_available = 0;
885 unsigned int sum_out_available = 0;
886 unsigned int sum_in_assigned = 0;
887 unsigned int sum_out_assigned = 0;
891 for (i = 0; i < solver->networks_count; i++)
893 net = solver->network_entries[i];
894 sum_in_available += net.bw_in_available;
895 sum_in_assigned += net.bw_in_assigned;
896 sum_out_available += net.bw_out_available;
897 sum_out_assigned += net.bw_out_assigned;
900 ratio_in = ((double) sum_in_assigned) / ((double) sum_in_available);
901 ratio_out = ((double) sum_out_assigned) / ((double) sum_out_available);
903 // global reward in [1,2]
905 return ((ratio_in + ratio_out) / 2) + 1;
909 envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
911 const double *preferences;
912 const double *properties;
914 double pref_match = 0;
918 preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
920 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
921 agent->address_inuse);
924 prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
925 dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
927 // utilization in [0,1]
928 bw_norm = (((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
929 / (double) ril_get_max_bw (agent, GNUNET_YES))
930 + ((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
931 / (double) ril_get_max_bw (agent, GNUNET_NO))) / 2;
933 // preference matching in [0,4]
934 pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
935 pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
937 // local reward in [1,2]
938 return (pref_match / 4) +1;
942 * Gets the reward for the last performed step, which is calculated in equal
943 * parts from the local (the peer specific) and the global (for all peers
946 * @param solver the solver handle
947 * @param agent the agent handle
951 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
953 struct RIL_Network *net;
954 // double reward = 0;
955 long long overutilized_in = 0;
956 // long long overutilized_out;
957 long long assigned_in = 0;
958 // long long assigned_out = 0;
961 //punish overutilization
962 net = agent->address_inuse->solver_information;
964 if (net->bw_in_assigned > net->bw_in_available)
966 overutilized_in = (net->bw_in_assigned - net->bw_in_available);
967 assigned_in = net->bw_in_available;
971 assigned_in = net->bw_in_assigned;
973 // if (net->bw_out_assigned > net->bw_out_available)
975 // overutilized_out = (net->bw_out_assigned - net->bw_out_available);
976 // assigned_out = net->bw_out_available;
980 // assigned_out = net->bw_out_assigned;
983 // unused = net->bw_in_available - net->bw_in_assigned;
984 // unused = unused < 0 ? unused : -unused;
986 return (double) (assigned_in - overutilized_in) / 1024;
988 // reward += envi_reward_global (solver) * (solver->parameters.reward_global_share);
989 // reward += envi_reward_local (solver, agent) * (1 - solver->parameters.reward_global_share);
991 // return (reward - 1.) * 100;
995 * Doubles the bandwidth for the active address
997 * @param solver solver handle
998 * @param agent agent handle
999 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1002 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1003 struct RIL_Peer_Agent *agent,
1006 unsigned long long new_bw;
1010 new_bw = agent->bw_in * 2;
1011 if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
1012 new_bw = GNUNET_ATS_MaxBandwidth;
1013 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1014 agent->bw_out, GNUNET_NO);
1018 new_bw = agent->bw_out * 2;
1019 if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth)
1020 new_bw = GNUNET_ATS_MaxBandwidth;
1021 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1027 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1028 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1030 * @param solver solver handle
1031 * @param agent agent handle
1032 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1036 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1037 struct RIL_Peer_Agent *agent,
1040 unsigned long long new_bw;
1044 new_bw = agent->bw_in / 2;
1045 if (new_bw < MIN_BW || new_bw > agent->bw_in)
1047 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1052 new_bw = agent->bw_out / 2;
1053 if (new_bw < MIN_BW || new_bw > agent->bw_out)
1055 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1061 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1063 * @param solver solver handle
1064 * @param agent agent handle
1065 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1069 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1071 unsigned long long new_bw;
1075 new_bw = agent->bw_in + (1 * MIN_BW);
1076 if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth)
1077 new_bw = GNUNET_ATS_MaxBandwidth;
1078 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1079 agent->bw_out, GNUNET_NO);
1083 new_bw = agent->bw_out + (1 * MIN_BW);
1084 if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth)
1085 new_bw = GNUNET_ATS_MaxBandwidth;
1086 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1092 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1093 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1095 * @param solver solver handle
1096 * @param agent agent handle
1097 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1101 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1103 unsigned long long new_bw;
1107 new_bw = agent->bw_in - (1 * MIN_BW);
1108 if (new_bw < MIN_BW || new_bw > agent->bw_in)
1110 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1115 new_bw = agent->bw_out - (1 * MIN_BW);
1116 if (new_bw < MIN_BW || new_bw > agent->bw_out)
1118 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1124 * Switches to the address given by its index
1126 * @param solver solver handle
1127 * @param agent agent handle
1128 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1131 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1132 struct RIL_Peer_Agent *agent,
1133 unsigned int address_index)
1135 struct RIL_Address_Wrapped *cur;
1138 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1140 if (i == address_index)
1142 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1150 //no address with address_index exists, in this case this action should not be callable
1151 GNUNET_assert(GNUNET_NO);
1155 * Puts the action into effect by calling the according function
1157 * @param solver the solver handle
1158 * @param agent the action handle
1159 * @param action the action to perform by the solver
1162 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1168 case RIL_ACTION_NOTHING:
1170 case RIL_ACTION_BW_IN_DBL:
1171 envi_action_bw_double (solver, agent, GNUNET_YES);
1173 case RIL_ACTION_BW_IN_HLV:
1174 envi_action_bw_halven (solver, agent, GNUNET_YES);
1176 case RIL_ACTION_BW_IN_INC:
1177 envi_action_bw_inc (solver, agent, GNUNET_YES);
1179 case RIL_ACTION_BW_IN_DEC:
1180 envi_action_bw_dec (solver, agent, GNUNET_YES);
1182 case RIL_ACTION_BW_OUT_DBL:
1183 envi_action_bw_double (solver, agent, GNUNET_NO);
1185 case RIL_ACTION_BW_OUT_HLV:
1186 envi_action_bw_halven (solver, agent, GNUNET_NO);
1188 case RIL_ACTION_BW_OUT_INC:
1189 envi_action_bw_inc (solver, agent, GNUNET_NO);
1191 case RIL_ACTION_BW_OUT_DEC:
1192 envi_action_bw_dec (solver, agent, GNUNET_NO);
1195 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1197 address_index = action - RIL_ACTION_TYPE_NUM;
1199 GNUNET_assert(address_index >= 0);
1201 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1203 envi_action_address_switch (solver, agent, address_index);
1206 // error - action does not exist
1207 GNUNET_assert(GNUNET_NO);
1212 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1213 * after having done the last action a_old. It observes the new state s_next and the reward
1214 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1215 * next action is put into effect.
1217 * @param agent the agent performing the step
1220 agent_step (struct RIL_Peer_Agent *agent)
1222 int a_next = RIL_ACTION_INVALID;
1226 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1227 GNUNET_i2s (&agent->peer),
1228 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1230 s_next = envi_get_state (agent->envi, agent);
1231 reward = envi_get_reward (agent->envi, agent);
1233 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Agent step %llu: A: %d R: %f IN %llu OUT %llu\n",
1238 agent->bw_out/1024);
1239 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Agent step %llu: Best A: %d Q(s,a): %f \n",
1241 agent_get_action_best (agent, s_next),
1242 agent_estimate_q(agent, s_next, agent_get_action_best (agent, s_next)));
1244 switch (agent->envi->parameters.algorithm)
1246 case RIL_ALGO_SARSA:
1247 if (agent_decide_exploration (agent))
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);
1270 if (agent_decide_exploration (agent))
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 envi_do_action (agent->envi, agent, a_next);
1289 GNUNET_free(agent->s_old);
1290 agent->s_old = s_next;
1291 agent->a_old = a_next;
1293 agent->step_count += 1;
1297 ril_step (struct GAS_RIL_Handle *solver);
1300 * Task for the scheduler, which performs one step and lets the solver know that
1301 * no further step is scheduled.
1303 * @param cls the solver handle
1304 * @param tc the task context for the scheduler
1307 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1309 struct GAS_RIL_Handle *solver = cls;
1311 solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1316 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1319 struct RIL_Network net;
1320 unsigned long long sum_assigned = 0;
1321 unsigned long long sum_available = 0;
1324 for (i = 0; i < solver->networks_count; i++)
1326 net = solver->network_entries[i];
1327 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1329 sum_assigned += net.bw_in_assigned;
1330 sum_assigned += net.bw_out_assigned;
1331 sum_available += net.bw_in_available;
1332 sum_available += net.bw_out_available;
1335 if (sum_available > 0)
1337 ratio = ((double) sum_assigned) / ((double) sum_available);
1344 return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
1348 * Lookup network struct by type
1350 * @param s the solver handle
1351 * @param type the network type
1352 * @return the network struct
1354 static struct RIL_Network *
1355 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1359 for (i = 0; i < s->networks_count; i++)
1361 if (s->network_entries[i].type == type)
1363 return &s->network_entries[i];
1370 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1372 struct RIL_Network *net;
1373 struct RIL_Peer_Agent *agent;
1374 unsigned long long address_count = 0;
1376 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1378 if (agent->address_inuse && agent->is_active)
1380 net = agent->address_inuse->solver_information;
1381 if (net->type == network)
1388 net = ril_get_network (solver, network);
1389 return (net->bw_in_available > MIN_BW * address_count) && (net->bw_out_available > MIN_BW * address_count);
1393 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1395 struct RIL_Address_Wrapped *addr_wrap;
1396 struct RIL_Network *net;
1398 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1400 net = addr_wrap->address_naked->solver_information;
1401 if (ril_network_is_not_full(solver, net->type))
1403 if (NULL == agent->address_inuse)
1404 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, MIN_BW, MIN_BW, silent);
1408 agent->address_inuse = NULL;
1412 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1414 struct GNUNET_TIME_Absolute time_now;
1415 struct GNUNET_TIME_Relative time_delta;
1418 // MDP case - remove when debugged
1419 if (solver->parameters.step_time_min.rel_value_us == solver->parameters.step_time_max.rel_value_us)
1421 solver->global_discount_variable = solver->parameters.gamma;
1422 solver->global_discount_integrated = 1;
1428 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1429 time_now = GNUNET_TIME_absolute_get ();
1430 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1431 solver->step_time_last = time_now;
1432 tau = (double) time_delta.rel_value_us
1433 / (double) solver->parameters.step_time_min.rel_value_us;
1435 //calculate reward discounts (once per step for all agents)
1436 solver->global_discount_variable = pow (M_E, ((-1.) * ((double) solver->parameters.beta) * tau));
1437 solver->global_discount_integrated = (1. - solver->global_discount_variable)
1438 / (double) solver->parameters.beta;
1442 ril_calculate_assigned_bwnet (struct GAS_RIL_Handle *solver)
1445 struct RIL_Network *net;
1447 for (c = 0; c < solver->networks_count; c++)
1449 net = &solver->network_entries[c];
1450 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1451 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1456 * Schedules the next global step in an adaptive way. The more resources are
1457 * left, the earlier the next step is scheduled. This serves the reactivity of
1458 * the solver to changed inputs.
1460 * @param solver the solver handle
1463 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1469 struct GNUNET_TIME_Relative time_next;
1471 used_ratio = ril_get_used_resource_ratio (solver);
1474 solver->parameters.step_time_min.rel_value_us
1475 <= solver->parameters.step_time_max.rel_value_us);
1477 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1478 solver->parameters.step_time_min).rel_value_us;
1479 offset = (double) solver->parameters.step_time_min.rel_value_us;
1480 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1482 GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1483 GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1485 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1487 if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1489 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1495 * Triggers one step per agent
1499 ril_step (struct GAS_RIL_Handle *solver)
1501 struct RIL_Peer_Agent *cur;
1503 if (GNUNET_YES == solver->bulk_lock)
1505 solver->bulk_changes++;
1509 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1511 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1513 if (0 == solver->step_count)
1515 solver->step_time_last = GNUNET_TIME_absolute_get ();
1518 ril_calculate_discount (solver);
1519 ril_calculate_assigned_bwnet (solver);
1521 //calculate network state vector
1522 // envi_state_networks(solver);
1524 //trigger one step per active, unblocked agent
1525 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1529 if (NULL == cur->address_inuse)
1531 ril_try_unblock_agent(solver, cur, GNUNET_NO);
1533 if (cur->address_inuse)
1540 ril_calculate_assigned_bwnet (solver);
1542 solver->step_count += 1;
1543 ril_step_schedule_next (solver);
1545 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1547 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1548 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1550 if (cur->suggestion_issue) {
1551 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1552 cur->suggestion_issue = GNUNET_NO;
1555 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1559 ril_count_agents (struct GAS_RIL_Handle *solver)
1562 struct RIL_Peer_Agent *cur_agent;
1564 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1572 agent_w_start (struct RIL_Peer_Agent *agent)
1575 struct RIL_Peer_Agent *other;
1579 count = ril_count_agents(agent->envi);
1581 for (i = 0; i < agent->n; i++)
1583 for (k = 0; k < agent->m; k++)
1586 agent->W[i][k] = 1.1 - ((double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX/5)/(double)UINT32_MAX);
1589 for (other = agent->envi->agents_head; NULL != other; other = other->next)
1591 agent->W[i][k] += (other->W[i][k] / (double) count);
1595 GNUNET_assert(!isinf(agent->W[i][k]));
1601 * Initialize an agent without addresses and its knowledge base
1603 * @param s ril solver
1604 * @param peer the one in question
1605 * @return handle to the new agent
1607 static struct RIL_Peer_Agent *
1608 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1611 struct GAS_RIL_Handle * solver = s;
1612 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
1614 agent->envi = solver;
1615 agent->peer = *peer;
1616 agent->step_count = 0;
1617 agent->is_active = GNUNET_NO;
1618 agent->bw_in = MIN_BW;
1619 agent->bw_out = MIN_BW;
1620 agent->suggestion_issue = GNUNET_NO;
1621 agent->n = RIL_ACTION_TYPE_NUM;
1622 agent->m = (RIL_FEATURES_NETWORK_COUNT);// + GNUNET_ATS_PreferenceCount;
1623 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1624 for (i = 0; i < agent->n; i++)
1626 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1628 agent_w_start(agent);
1629 agent->a_old = RIL_ACTION_INVALID;
1630 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1631 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1632 agent_modify_eligibility (agent, RIL_E_ZERO, NULL);
1640 * @param solver the solver handle
1641 * @param agent the agent to retire
1644 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1648 for (i = 0; i < agent->n; i++)
1650 GNUNET_free(agent->W[i]);
1652 GNUNET_free(agent->W);
1653 GNUNET_free(agent->e);
1654 GNUNET_free(agent->s_old);
1659 * Returns the agent for a peer
1661 * @param solver the solver handle
1662 * @param peer the identity of the peer
1663 * @param create whether or not to create an agent, if none is allocated yet
1666 static struct RIL_Peer_Agent *
1667 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1669 struct RIL_Peer_Agent *cur;
1671 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1673 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1681 cur = agent_init (solver, peer);
1682 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
1689 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1690 * considered inactive and not used. Addresses in an inactive network are ignored.
1692 * @param solver solver handle
1693 * @param network the network type
1694 * @return whether or not the network is considered active
1697 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1699 struct RIL_Network *net;
1701 net = ril_get_network (solver, network);
1702 return net->bw_out_available >= MIN_BW;
1706 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1707 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1708 * frees the memory of the old vector, and redirects the pointer to the new one.
1710 * @param old pointer to the pointer to the first element of the vector
1711 * @param element_size byte size of the vector elements
1712 * @param hole_start the first element to cut out
1713 * @param hole_length the number of elements to cut out
1714 * @param old_length the length of the old vector
1717 ril_cut_from_vector (void **old,
1718 size_t element_size,
1719 unsigned int hole_start,
1720 unsigned int hole_length,
1721 unsigned int old_length)
1724 char *oldptr = (char *) *old;
1726 unsigned int bytes_before;
1727 unsigned int bytes_hole;
1728 unsigned int bytes_after;
1730 GNUNET_assert(old_length > hole_length);
1731 GNUNET_assert(old_length >= (hole_start + hole_length));
1733 size = element_size * (old_length - hole_length);
1735 bytes_before = element_size * hole_start;
1736 bytes_hole = element_size * hole_length;
1737 bytes_after = element_size * (old_length - hole_start - hole_length);
1745 tmpptr = GNUNET_malloc (size);
1746 memcpy (tmpptr, oldptr, bytes_before);
1747 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1753 *old = (void *) tmpptr;
1757 * Solver API functions
1758 * ---------------------------
1762 * Change relative preference for quality in solver
1764 * @param solver the solver handle
1765 * @param peer the peer to change the preference for
1766 * @param kind the kind to change the preference
1767 * @param pref_rel the normalized preference value for this kind over all clients
1770 GAS_ril_address_change_preference (void *solver,
1771 const struct GNUNET_PeerIdentity *peer,
1772 enum GNUNET_ATS_PreferenceKind kind,
1775 LOG(GNUNET_ERROR_TYPE_DEBUG,
1776 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1777 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1783 * Entry point for the plugin
1785 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1788 libgnunet_plugin_ats_ril_init (void *cls)
1790 struct GNUNET_ATS_PluginEnvironment *env = cls;
1791 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1792 struct RIL_Network * cur;
1796 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1798 GNUNET_assert(NULL != env);
1799 GNUNET_assert(NULL != env->cfg);
1800 GNUNET_assert(NULL != env->stats);
1801 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1802 GNUNET_assert(NULL != env->get_preferences);
1803 GNUNET_assert(NULL != env->get_property);
1806 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
1807 &solver->parameters.step_time_min))
1809 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
1812 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
1813 &solver->parameters.step_time_max))
1815 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
1817 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
1819 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
1820 GNUNET_free (string);
1824 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1826 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
1828 solver->parameters.beta = strtod (string, NULL);
1829 GNUNET_free (string);
1833 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1835 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
1837 solver->parameters.gamma = strtod (string, NULL);
1838 GNUNET_free (string);
1842 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
1845 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
1847 solver->parameters.alpha = strtod (string, NULL);
1848 GNUNET_free (string);
1852 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1854 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
1856 solver->parameters.lambda = strtod (string, NULL);
1857 GNUNET_free (string);
1861 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1863 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
1865 solver->parameters.explore_ratio = strtod (string, NULL);
1866 GNUNET_free (string);
1870 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
1872 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
1874 solver->parameters.reward_global_share = strtod (string, NULL);
1875 GNUNET_free (string);
1879 solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
1882 env->sf.s_add = &GAS_ril_address_add;
1883 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1884 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1885 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
1886 env->sf.s_address_update_network = &GAS_ril_address_change_network;
1887 env->sf.s_get = &GAS_ril_get_preferred_address;
1888 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
1889 env->sf.s_pref = &GAS_ril_address_change_preference;
1890 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
1891 env->sf.s_del = &GAS_ril_address_delete;
1892 env->sf.s_bulk_start = &GAS_ril_bulk_start;
1893 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
1895 solver->plugin_envi = env;
1896 solver->networks_count = env->network_count;
1897 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
1898 solver->step_count = 0;
1899 solver->global_state_networks = GNUNET_malloc (solver->networks_count * RIL_FEATURES_NETWORK_COUNT * sizeof (double));
1900 solver->done = GNUNET_NO;
1902 for (c = 0; c < env->network_count; c++)
1904 cur = &solver->network_entries[c];
1905 cur->type = env->networks[c];
1906 cur->bw_in_available = env->in_quota[c];
1907 cur->bw_out_available = env->out_quota[c];
1908 LOG(GNUNET_ERROR_TYPE_INFO, "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);
1911 LOG(GNUNET_ERROR_TYPE_INFO, "Parameters:\n");
1912 LOG(GNUNET_ERROR_TYPE_INFO, "Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
1913 solver->parameters.algorithm ? "Q" : "SARSA",
1914 solver->parameters.alpha,
1915 solver->parameters.beta,
1916 solver->parameters.lambda);
1917 LOG(GNUNET_ERROR_TYPE_INFO, "explore = %f, global_share = %f\n",
1918 solver->parameters.explore_ratio,
1919 solver->parameters.reward_global_share);
1925 * Exit point for the plugin
1927 * @param cls the solver handle
1930 libgnunet_plugin_ats_ril_done (void *cls)
1932 struct GAS_RIL_Handle *s = cls;
1933 struct RIL_Peer_Agent *cur_agent;
1934 struct RIL_Peer_Agent *next_agent;
1936 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1938 s->done = GNUNET_YES;
1940 cur_agent = s->agents_head;
1941 while (NULL != cur_agent)
1943 next_agent = cur_agent->next;
1944 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1945 agent_die (s, cur_agent);
1946 cur_agent = next_agent;
1949 if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
1951 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
1953 GNUNET_free(s->network_entries);
1954 GNUNET_free(s->global_state_networks);
1961 * Add a new address for a peer to the solver
1963 * The address is already contained in the addresses hashmap!
1965 * @param solver the solver Handle
1966 * @param address the address to add
1967 * @param network network type of this address
1970 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1972 struct GAS_RIL_Handle *s = solver;
1973 struct RIL_Peer_Agent *agent;
1974 struct RIL_Address_Wrapped *address_wrapped;
1975 struct RIL_Network *net;
1983 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
1985 net = ril_get_network (s, network);
1986 address->solver_information = net;
1988 if (!ril_network_is_active (s, network))
1990 LOG(GNUNET_ERROR_TYPE_DEBUG,
1991 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
1992 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1996 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1999 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
2000 address_wrapped->address_naked = address;
2001 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2003 //increase size of W
2004 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
2006 n_new = agent->n + 1;
2009 GNUNET_array_grow(agent->W, agent->n, n_new);
2010 for (i = 0; i < n_new; i++)
2015 GNUNET_array_grow(agent->W[i], agent->m, m_new);
2020 GNUNET_array_grow(agent->W[i], zero, m_new);
2024 //increase size of old state vector
2026 GNUNET_array_grow(agent->s_old, agent->m, m_new);
2029 GNUNET_array_grow(agent->e, agent->m, m_new);
2031 ril_try_unblock_agent(s, agent, GNUNET_NO);
2035 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2036 address->active ? "active" : "inactive", address->plugin, address->addr,
2037 GNUNET_i2s (&address->peer));
2041 * Delete an address in the solver
2043 * The address is not contained in the address hashmap anymore!
2045 * @param solver the solver handle
2046 * @param address the address to remove
2047 * @param session_only delete only session not whole address
2050 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
2052 struct GAS_RIL_Handle *s = solver;
2053 struct RIL_Peer_Agent *agent;
2054 struct RIL_Address_Wrapped *address_wrapped;
2055 int address_was_used = address->active;
2060 struct RIL_Network *net;
2062 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2063 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2064 address->addr, GNUNET_i2s (&address->peer));
2066 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2069 net = address->solver_information;
2070 GNUNET_assert(!ril_network_is_active (s, net->type));
2071 LOG(GNUNET_ERROR_TYPE_DEBUG,
2072 "No agent allocated for peer yet, since address was in inactive network\n");
2076 address_index = agent_address_get_index (agent, address);
2077 address_wrapped = agent_address_get (agent, address);
2079 if (NULL == address_wrapped)
2081 net = address->solver_information;
2082 GNUNET_assert(!ril_network_is_active (s, net->type));
2083 LOG(GNUNET_ERROR_TYPE_DEBUG,
2084 "Address not considered by agent, address was in inactive network\n");
2088 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2089 GNUNET_free(address_wrapped);
2092 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
2093 n_new = agent->n - 1;
2095 for (i = 0; i < agent->n; i++)
2097 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2098 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
2099 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
2101 GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2102 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2104 //correct last action
2105 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2109 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2111 agent->a_old = RIL_ACTION_INVALID;
2113 //decrease old state vector and eligibility vector
2114 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2115 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
2116 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
2117 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
2118 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
2119 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
2123 if (address_was_used)
2125 if (NULL != agent->addresses_head) //if peer has an address left, use it
2127 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, MIN_BW, MIN_BW,
2132 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2140 * Update the properties of an address in the solver
2142 * @param solver solver handle
2143 * @param address the address
2144 * @param type the ATSI type in HBO
2145 * @param abs_value the absolute value of the property
2146 * @param rel_value the normalized value
2149 GAS_ril_address_property_changed (void *solver,
2150 struct ATS_Address *address,
2155 LOG(GNUNET_ERROR_TYPE_DEBUG,
2156 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2157 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2158 address->addr, rel_value);
2164 * Update the session of an address in the solver
2166 * NOTE: values in addresses are already updated
2168 * @param solver solver handle
2169 * @param address the address
2170 * @param cur_session the current session
2171 * @param new_session the new session
2174 GAS_ril_address_session_changed (void *solver,
2175 struct ATS_Address *address,
2176 uint32_t cur_session,
2177 uint32_t new_session)
2180 * TODO? Future Work: Potentially add session activity as a feature in state vector
2182 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2186 * Notify the solver that an address is (not) actively used by transport
2187 * to communicate with a remote peer
2189 * NOTE: values in addresses are already updated
2191 * @param solver solver handle
2192 * @param address the address
2193 * @param in_use usage state
2196 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2199 * TODO? Future Work: Potentially add usage variable to state vector
2201 LOG(GNUNET_ERROR_TYPE_DEBUG,
2202 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2203 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2207 * Notify solver that the network an address is located in has changed
2209 * NOTE: values in addresses are already updated
2211 * @param solver solver handle
2212 * @param address the address
2213 * @param current_network the current network
2214 * @param new_network the new network
2217 GAS_ril_address_change_network (void *solver,
2218 struct ATS_Address *address,
2219 uint32_t current_network,
2220 uint32_t new_network)
2222 struct GAS_RIL_Handle *s = solver;
2223 struct RIL_Peer_Agent *agent;
2225 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2226 "%s address of peer %s from '%s' to '%s'\n",
2227 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2228 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2230 if (address->active && !ril_network_is_active (solver, new_network))
2232 GAS_ril_address_delete (solver, address, GNUNET_NO);
2236 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2239 GNUNET_assert(!ril_network_is_active (solver, current_network));
2241 GAS_ril_address_add (s, address, new_network);
2245 address->solver_information = ril_get_network(solver, new_network);
2249 * Give feedback about the current assignment
2251 * @param solver the solver handle
2252 * @param application the application
2253 * @param peer the peer to change the preference for
2254 * @param scope the time interval for this feedback: [now - scope .. now]
2255 * @param kind the kind to change the preference
2256 * @param score the score
2259 GAS_ril_address_preference_feedback (void *solver,
2261 const struct GNUNET_PeerIdentity *peer,
2262 const struct GNUNET_TIME_Relative scope,
2263 enum GNUNET_ATS_PreferenceKind kind,
2266 LOG(GNUNET_ERROR_TYPE_DEBUG,
2267 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2268 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2269 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2273 * Start a bulk operation
2275 * @param solver the solver
2278 GAS_ril_bulk_start (void *solver)
2280 struct GAS_RIL_Handle *s = solver;
2282 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2288 * Bulk operation done
2290 * @param solver the solver handle
2293 GAS_ril_bulk_stop (void *solver)
2295 struct GAS_RIL_Handle *s = solver;
2297 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2299 if (s->bulk_lock < 1)
2306 if (0 < s->bulk_changes)
2309 s->bulk_changes = 0;
2314 * Tell solver to notify ATS if the address to use changes for a specific
2315 * peer using the bandwidth changed callback
2317 * The solver must only notify about changes for peers with pending address
2320 * @param solver the solver handle
2321 * @param peer the identity of the peer
2323 const struct ATS_Address *
2324 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2327 * activate agent, return currently chosen address
2329 struct GAS_RIL_Handle *s = solver;
2330 struct RIL_Peer_Agent *agent;
2332 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2334 agent = ril_get_agent (s, peer, GNUNET_YES);
2336 agent->is_active = GNUNET_YES;
2337 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2339 ril_try_unblock_agent(solver, agent, GNUNET_YES);
2341 if (agent->address_inuse)
2343 LOG(GNUNET_ERROR_TYPE_DEBUG,
2344 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2345 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2349 LOG(GNUNET_ERROR_TYPE_DEBUG,
2350 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2354 return agent->address_inuse;
2358 * Tell solver stop notifying ATS about changes for this peers
2360 * The solver must only notify about changes for peers with pending address
2363 * @param solver the solver handle
2364 * @param peer the peer
2367 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2369 struct GAS_RIL_Handle *s = solver;
2370 struct RIL_Peer_Agent *agent;
2372 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2374 agent = ril_get_agent (s, peer, GNUNET_NO);
2381 if (GNUNET_NO == agent->is_active)
2387 agent->is_active = GNUNET_NO;
2389 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2394 LOG(GNUNET_ERROR_TYPE_DEBUG,
2395 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2396 GNUNET_i2s (peer), agent->address_inuse->plugin);
2399 /* end of libgnunet_plugin_ats_ril.c */