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 RIL_ACTION_INVALID -1
32 #define RIL_FEATURES_ADDRESS_COUNT (3 + GNUNET_ATS_QualityPropertiesCount)
33 #define RIL_FEATURES_NETWORK_COUNT 4
34 #define RIL_INTERVAL_EXPONENT 10
36 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
37 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
38 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
39 #define RIL_DEFAULT_DISCOUNT_BETA 0.7
40 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.3
41 #define RIL_DEFAULT_TRACE_DECAY 0.5
42 #define RIL_DEFAULT_EXPLORE_RATIO 0.1
45 * ATS reinforcement learning solver
51 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
52 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
53 * to a particular address. The action of switching to address with index i is depicted by the
54 * number (RIL_ACTION_TYPE_NUM + i).
58 RIL_ACTION_NOTHING = 0,
59 RIL_ACTION_BW_IN_DBL = 1,
60 RIL_ACTION_BW_IN_HLV = 2,
61 RIL_ACTION_BW_IN_INC = 3,
62 RIL_ACTION_BW_IN_DEC = 4,
63 RIL_ACTION_BW_OUT_DBL = 5,
64 RIL_ACTION_BW_OUT_HLV = 6,
65 RIL_ACTION_BW_OUT_INC = 7,
66 RIL_ACTION_BW_OUT_DEC = 8,
67 RIL_ACTION_TYPE_NUM = 9
76 enum RIL_E_Modification
85 * Global learning parameters
87 struct RIL_Learning_Parameters
90 * The TD-algorithm to use
92 enum RIL_Algorithm algorithm;
95 * Learning discount factor in the TD-update
100 * Gradient-descent step-size
105 * Trace-decay factor for eligibility traces
112 double explore_ratio;
115 * Minimal interval time between steps in milliseconds
117 struct GNUNET_TIME_Relative step_time_min;
120 * Maximum interval time between steps in milliseconds
122 struct GNUNET_TIME_Relative step_time_max;
126 * Wrapper for addresses to store them in agent's linked list
128 struct RIL_Address_Wrapped
133 struct RIL_Address_Wrapped *next;
138 struct RIL_Address_Wrapped *prev;
143 struct ATS_Address *address_naked;
146 struct RIL_Peer_Agent
149 * Next agent in solver's linked list
151 struct RIL_Peer_Agent *next;
154 * Previous agent in solver's linked list
156 struct RIL_Peer_Agent *prev;
161 struct GAS_RIL_Handle *envi;
166 struct GNUNET_PeerIdentity peer;
169 * Whether the agent is active or not
174 * Number of performed time-steps
176 unsigned long long step_count;
179 * Experience matrix W
184 * Number of rows of W / Number of state-vector features
189 * Number of columns of W / Number of actions
194 * Last perceived state feature vector
204 * Eligibility trace vector
211 struct ATS_Address * address_inuse;
214 * Head of addresses DLL
216 struct RIL_Address_Wrapped * addresses_head;
219 * Tail of addresses DLL
221 struct RIL_Address_Wrapped * addresses_tail;
224 * Inbound bandwidth assigned by the agent
226 unsigned long long bw_in;
229 * Outbound bandwidth assigned by the agent
231 unsigned long long bw_out;
234 * Flag whether a suggestion has to be issued
236 int suggestion_issue;
239 * The address which has to be issued ()
241 struct ATS_Address * suggestion_address;
249 enum GNUNET_ATS_Network_Type type;
252 * Total available inbound bandwidth
254 unsigned long long bw_in_available;
257 * Total available outbound bandwidth
259 unsigned long long bw_out_available;
263 * A handle for the reinforcement learning solver
265 struct GAS_RIL_Handle
268 * The solver-plugin environment of the solver-plugin API
270 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
275 struct GNUNET_STATISTICS_Handle *stats;
278 * Number of performed steps
280 unsigned long long step_count;
283 * Timestamp for the last time-step
285 struct GNUNET_TIME_Absolute step_time_last;
288 * Task identifier of the next time-step to be executed
290 GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
293 * Whether a step is already scheduled
298 * Variable discount factor, dependent on time between steps
300 double discount_variable;
303 * Integrated variable discount factor, dependent on time between steps
305 double discount_integrated;
308 * State vector for networks for the current step
310 double *state_networks;
313 * Lock for bulk operations
318 * Number of changes during a lock
323 * Learning parameters
325 struct RIL_Learning_Parameters parameters;
328 * Array of networks with global assignment state
330 struct RIL_Network * network_entries;
335 unsigned int networks_count;
338 * List of active peer-agents
340 struct RIL_Peer_Agent * agents_head;
341 struct RIL_Peer_Agent * agents_tail;
346 * ---------------------------
350 * Estimate the current action-value for state s and action a
352 * @param agent agent performing the estimation
355 * @return estimation value
358 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
363 for (i = 0; i < agent->m; i++)
365 result += state[i] * agent->W[action][i];
372 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
373 * currently estimated best action) in the current step
375 * @param agent agent performing the step
376 * @return yes, if exploring
379 agent_decide_exploration (struct RIL_Peer_Agent *agent)
381 //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
382 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
383 UINT32_MAX) / (double) UINT32_MAX;
385 if (r < agent->envi->parameters.explore_ratio)
393 * Get the index of the address in the agent's list.
395 * @param agent agent handle
396 * @param address address handle
397 * @return the index, starting with zero
400 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
403 struct RIL_Address_Wrapped *cur;
406 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
409 if (cur->address_naked == address)
419 * Gets the wrapped address from the agent's list
421 * @param agent agent handle
422 * @param address address handle
423 * @return wrapped address
425 static struct RIL_Address_Wrapped *
426 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
428 struct RIL_Address_Wrapped *cur;
430 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
432 if (cur->address_naked == address)
442 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
443 * most reward in the future)
445 * @param agent agent performing the calculation
446 * @param state the state from which to take the action
447 * @return the action promising most future reward
450 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
453 int max_i = RIL_ACTION_INVALID;
455 double max_q = -DBL_MAX;
457 for (i = 0; i < agent->n; i++)
459 cur_q = agent_estimate_q (agent, state, i);
467 GNUNET_assert(RIL_ACTION_INVALID != max_i);
473 * Gets any action, to explore the action space from that state
475 * @param agent agent performing the calculation
476 * @param state the state from which to take the action
480 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
482 // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
483 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
487 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
489 * @param agent the agent performing the update
490 * @param reward the reward received for the last action
491 * @param s_next the new state, the last step got the agent into
492 * @param a_prime the new
495 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
499 double *theta = agent->W[agent->a_old];
501 delta = agent->envi->discount_integrated * reward; //reward
502 delta += agent->envi->discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
503 delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
504 for (i = 0; i < agent->m; i++)
506 theta[i] += agent->envi->parameters.alpha * delta * agent->e[i];
511 * Changes the eligibility trace vector e in various manners:
512 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
513 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
514 * RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
515 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
517 * @param agent the agent handle
518 * @param mod the kind of modification
521 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
524 double *e = agent->e;
526 for (i = 0; i < agent->m; i++)
530 case RIL_E_ACCUMULATE:
537 e[i] *= agent->envi->discount_variable * agent->envi->parameters.lambda;
547 ril_inform (struct GAS_RIL_Handle *solver,
548 enum GAS_Solver_Operation op,
549 enum GAS_Solver_Status stat)
551 if (NULL != solver->plugin_envi->info_cb)
552 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
556 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
557 * notify ATS of its new decision
559 * @param solver solver handle
560 * @param agent agent handle
561 * @param new_address the address which is to be used
562 * @param new_bw_in the new amount of inbound bandwidth set for this address
563 * @param new_bw_out the new amount of outbound bandwidth set for this address
564 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
567 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
568 struct RIL_Peer_Agent *agent,
569 struct ATS_Address *new_address,
570 unsigned long long new_bw_in,
571 unsigned long long new_bw_out,
574 int notify = GNUNET_NO;
576 LOG(GNUNET_ERROR_TYPE_DEBUG, " set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
579 if (agent->address_inuse != new_address)
581 if (NULL != agent->address_inuse)
583 agent->address_inuse->active = GNUNET_NO;
584 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
585 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
587 if (NULL != new_address)
589 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
590 new_address->active = agent->is_active;
591 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
592 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
594 notify |= GNUNET_YES;
600 if (new_address->active != agent->is_active)
602 new_address->active = agent->is_active;
603 notify |= GNUNET_YES;
607 if (agent->bw_in != new_bw_in)
609 agent->bw_in = new_bw_in;
610 new_address->assigned_bw_in.value__ = htonl (new_bw_in);
611 notify |= GNUNET_YES;
613 if (agent->bw_out != new_bw_out)
615 agent->bw_out = new_bw_out;
616 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
617 notify |= GNUNET_YES;
621 if (notify && agent->is_active && (GNUNET_NO == silent))
625 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
626 agent->suggestion_issue = GNUNET_YES;
627 agent->suggestion_address = new_address;
629 else if (agent->address_inuse)
631 //disconnect case, no new address
632 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
633 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
637 agent->suggestion_issue = GNUNET_YES;
638 agent->suggestion_address = agent->address_inuse;
641 agent->address_inuse = new_address;
644 static unsigned long long
645 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
647 struct RIL_Peer_Agent *cur;
648 struct RIL_Network *net;
649 unsigned long long sum = 0;
651 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
653 if (cur->is_active && cur->address_inuse)
655 net = cur->address_inuse->solver_information;
656 if (net->type == type)
670 envi_state_networks (struct GAS_RIL_Handle *solver)
673 struct RIL_Network net;
675 for (i = 0; i < solver->networks_count; i++)
677 net = solver->network_entries[i];
678 solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) ril_network_get_assigned(solver, net.type, GNUNET_YES);
679 solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net.bw_in_available;
680 solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) ril_network_get_assigned(solver, net.type, GNUNET_NO);
681 solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net.bw_out_available;
686 * Allocates a state vector and fills it with the features present
687 * @param solver the solver handle
688 * @param agent the agent handle
689 * @return pointer to the state vector
692 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
696 double *state = GNUNET_malloc (sizeof (double) * agent->m);
697 struct RIL_Address_Wrapped *cur_address;
698 const double *properties;
700 //copy global networks state
701 for (i = 0; i < solver->networks_count; i++)
703 state[i * RIL_FEATURES_NETWORK_COUNT + 0] = solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 0];
704 state[i * RIL_FEATURES_NETWORK_COUNT + 1] = solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 1];
705 state[i * RIL_FEATURES_NETWORK_COUNT + 2] = solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 2];
706 state[i * RIL_FEATURES_NETWORK_COUNT + 3] = solver->state_networks[i * RIL_FEATURES_NETWORK_COUNT + 3];
709 i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
711 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
713 state[i++] = cur_address->address_naked->active;
714 state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
715 state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
716 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
717 cur_address->address_naked);
718 for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
720 state[i++] = properties[k];
728 * For all networks a peer has an address in, this gets the maximum bandwidth which could
729 * theoretically be available in one of the networks. This is used for bandwidth normalization.
731 * @param agent the agent handle
732 * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
734 static long long unsigned
735 ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
738 * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
739 * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
742 struct RIL_Address_Wrapped *cur;
743 struct RIL_Network *net;
745 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
747 net = cur->address_naked->solver_information;
750 if (net->bw_in_available > max)
752 max = net->bw_in_available;
757 if (net->bw_out_available > max)
759 max = net->bw_out_available;
767 * Get the index of the quality-property in question
769 * @param type the quality property type
773 ril_find_property_index (uint32_t type)
775 int existing_types[] = GNUNET_ATS_QualityProperties;
777 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
778 if (existing_types[c] == type)
780 return GNUNET_SYSERR;
784 ril_get_atsi (struct ATS_Address *address, uint32_t type)
787 GNUNET_assert(NULL != address);
789 if ((NULL == address->atsi) || (0 == address->atsi_count))
792 for (c1 = 0; c1 < address->atsi_count; c1++)
794 if (ntohl (address->atsi[c1].type) == type)
795 return ntohl (address->atsi[c1].value);
801 envi_reward_global (struct GAS_RIL_Handle *solver)
804 struct RIL_Network net;
805 unsigned int sum_in_available = 0;
806 unsigned int sum_out_available = 0;
807 unsigned int sum_in_assigned = 0;
808 unsigned int sum_out_assigned = 0;
812 for (i = 0; i < solver->networks_count; i++)
814 net = solver->network_entries[i];
815 sum_in_available += net.bw_in_available;
816 sum_in_assigned += ril_network_get_assigned(solver, net.type, GNUNET_YES);
817 sum_out_available += net.bw_out_available;
818 sum_out_assigned += ril_network_get_assigned(solver, net.type, GNUNET_NO);
821 ratio_in = ((double) sum_in_assigned) / ((double) sum_in_available);
822 ratio_out = ((double) sum_out_assigned) / ((double) sum_out_available);
824 // global reward in [1,2]
825 return ((ratio_in + ratio_out) / 2) + 1;
829 envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
831 const double *preferences;
832 const double *properties;
834 double pref_match = 0;
838 preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
840 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
841 agent->address_inuse);
844 prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
845 dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
847 // utilization in [0,1]
848 bw_norm = ((ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
849 / ril_get_max_bw (agent, GNUNET_YES))
850 + (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
851 / ril_get_max_bw (agent, GNUNET_NO))) / 2;
853 // preference matching in [0,4]
854 pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
855 pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
857 // local reward in [1,2]
858 return (pref_match / 4) +1;
862 * Gets the reward for the last performed step, which is calculated in equal
863 * parts from the local (the peer specific) and the global (for all peers
866 * @param solver the solver handle
867 * @param agent the agent handle
871 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
873 struct RIL_Network *net;
876 //punish overutilization
877 net = agent->address_inuse->solver_information;
878 if ((ril_network_get_assigned(solver, net->type, GNUNET_YES) > net->bw_in_available)
879 || (ril_network_get_assigned(solver, net->type, GNUNET_NO) > net->bw_out_available))
884 reward += envi_reward_global (solver);
885 reward += envi_reward_local (solver, agent);
890 * Doubles the bandwidth for the active address
892 * @param solver solver handle
893 * @param agent agent handle
894 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
897 envi_action_bw_double (struct GAS_RIL_Handle *solver,
898 struct RIL_Peer_Agent *agent,
901 unsigned long long new_bw;
905 new_bw = agent->bw_in * 2;
906 if (new_bw < agent->bw_in)
908 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
909 agent->bw_out, GNUNET_NO);
913 new_bw = agent->bw_out * 2;
914 if (new_bw < agent->bw_out)
916 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
922 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
923 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
925 * @param solver solver handle
926 * @param agent agent handle
927 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
931 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
932 struct RIL_Peer_Agent *agent,
935 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
936 unsigned long long new_bw;
940 new_bw = agent->bw_in / 2;
941 if (new_bw < min_bw || new_bw > agent->bw_in)
943 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
948 new_bw = agent->bw_out / 2;
949 if (new_bw < min_bw || new_bw > agent->bw_out)
951 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
957 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
959 * @param solver solver handle
960 * @param agent agent handle
961 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
965 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
967 unsigned long long new_bw;
968 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
972 new_bw = agent->bw_in + (5 * min_bw);
973 if (new_bw < agent->bw_in)
975 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
976 agent->bw_out, GNUNET_NO);
980 new_bw = agent->bw_out + (5 * min_bw);
981 if (new_bw < agent->bw_out)
983 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
989 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
990 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
992 * @param solver solver handle
993 * @param agent agent handle
994 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
998 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1000 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1001 unsigned long long new_bw;
1005 new_bw = agent->bw_in - (5 * min_bw);
1006 if (new_bw < min_bw || new_bw > agent->bw_in)
1008 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1013 new_bw = agent->bw_out - (5 * min_bw);
1014 if (new_bw < min_bw || new_bw > agent->bw_in)
1016 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1022 * Switches to the address given by its index
1024 * @param solver solver handle
1025 * @param agent agent handle
1026 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1029 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1030 struct RIL_Peer_Agent *agent,
1031 unsigned int address_index)
1033 struct RIL_Address_Wrapped *cur;
1036 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1038 if (i == address_index)
1040 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1048 //no address with address_index exists, in this case this action should not be callable
1049 GNUNET_assert(GNUNET_NO);
1053 * Puts the action into effect by calling the according function
1055 * @param solver the solver handle
1056 * @param agent the action handle
1057 * @param action the action to perform by the solver
1060 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1066 case RIL_ACTION_NOTHING:
1068 case RIL_ACTION_BW_IN_DBL:
1069 envi_action_bw_double (solver, agent, GNUNET_YES);
1071 case RIL_ACTION_BW_IN_HLV:
1072 envi_action_bw_halven (solver, agent, GNUNET_YES);
1074 case RIL_ACTION_BW_IN_INC:
1075 envi_action_bw_inc (solver, agent, GNUNET_YES);
1077 case RIL_ACTION_BW_IN_DEC:
1078 envi_action_bw_dec (solver, agent, GNUNET_YES);
1080 case RIL_ACTION_BW_OUT_DBL:
1081 envi_action_bw_double (solver, agent, GNUNET_NO);
1083 case RIL_ACTION_BW_OUT_HLV:
1084 envi_action_bw_halven (solver, agent, GNUNET_NO);
1086 case RIL_ACTION_BW_OUT_INC:
1087 envi_action_bw_inc (solver, agent, GNUNET_NO);
1089 case RIL_ACTION_BW_OUT_DEC:
1090 envi_action_bw_dec (solver, agent, GNUNET_NO);
1093 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1095 address_index = action - RIL_ACTION_TYPE_NUM;
1097 GNUNET_assert(address_index >= 0);
1099 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1101 envi_action_address_switch (solver, agent, address_index);
1104 // error - action does not exist
1105 GNUNET_assert(GNUNET_NO);
1110 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1111 * after having done the last action a_old. It observes the new state s_next and the reward
1112 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1113 * next action is put into effect.
1115 * @param agent the agent performing the step
1118 agent_step (struct RIL_Peer_Agent *agent)
1120 int a_next = RIL_ACTION_INVALID;
1124 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1125 GNUNET_i2s (&agent->peer),
1126 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1128 s_next = envi_get_state (agent->envi, agent);
1129 reward = envi_get_reward (agent->envi, agent);
1131 switch (agent->envi->parameters.algorithm)
1133 case RIL_ALGO_SARSA:
1134 agent_modify_eligibility (agent, RIL_E_SET);
1135 if (agent_decide_exploration (agent))
1137 a_next = agent_get_action_explore (agent, s_next);
1141 a_next = agent_get_action_best (agent, s_next);
1143 if (RIL_ACTION_INVALID != agent->a_old)
1145 //updates weights with selected action (on-policy), if not first step
1146 agent_update_weights (agent, reward, s_next, a_next);
1151 a_next = agent_get_action_best (agent, s_next);
1152 if (RIL_ACTION_INVALID != agent->a_old)
1154 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1155 agent_update_weights (agent, reward, s_next, a_next);
1157 if (agent_decide_exploration (agent))
1159 a_next = agent_get_action_explore (agent, s_next);
1160 agent_modify_eligibility (agent, RIL_E_ZERO);
1164 a_next = agent_get_action_best (agent, s_next);
1165 agent_modify_eligibility (agent, RIL_E_SET);
1170 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1172 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
1174 envi_do_action (agent->envi, agent, a_next);
1176 GNUNET_free(agent->s_old);
1177 agent->s_old = s_next;
1178 agent->a_old = a_next;
1180 agent->step_count += 1;
1184 ril_step (struct GAS_RIL_Handle *solver);
1187 * Task for the scheduler, which performs one step and lets the solver know that
1188 * no further step is scheduled.
1190 * @param cls the solver handle
1191 * @param tc the task context for the scheduler
1194 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1196 struct GAS_RIL_Handle *solver = cls;
1198 solver->task_pending = GNUNET_NO;
1203 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1206 struct RIL_Network net;
1207 unsigned long long sum_assigned = 0;
1208 unsigned long long sum_available = 0;
1211 for (i = 0; i < solver->networks_count; i++)
1213 net = solver->network_entries[i];
1214 if (ril_network_get_assigned(solver, net.type, GNUNET_YES) > 0) //only consider scopes with an active address
1216 sum_assigned += ril_network_get_assigned(solver, net.type, GNUNET_YES);
1217 sum_assigned += ril_network_get_assigned(solver, net.type, GNUNET_NO);
1218 sum_available += net.bw_in_available;
1219 sum_available += net.bw_out_available;
1222 if (sum_available > 0)
1224 ratio = ((double) sum_assigned) / ((double) sum_available);
1231 return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
1235 * Schedules the next global step in an adaptive way. The more resources are
1236 * left, the earlier the next step is scheduled. This serves the reactivity of
1237 * the solver to changed inputs.
1239 * @param solver the solver handle
1242 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1248 struct GNUNET_TIME_Relative time_next;
1250 if (solver->task_pending)
1252 GNUNET_SCHEDULER_cancel (solver->step_next_task_id);
1255 used_ratio = ril_get_used_resource_ratio (solver);
1258 solver->parameters.step_time_min.rel_value_us
1259 < solver->parameters.step_time_max.rel_value_us);
1261 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1262 solver->parameters.step_time_min).rel_value_us;
1263 offset = (double) solver->parameters.step_time_min.rel_value_us;
1264 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1266 GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1267 GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1269 time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned int) y);
1271 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1273 solver->task_pending = GNUNET_YES;
1277 * Triggers one step per agent
1281 ril_step (struct GAS_RIL_Handle *solver)
1283 struct RIL_Peer_Agent *cur;
1284 struct GNUNET_TIME_Absolute time_now;
1285 struct GNUNET_TIME_Relative time_delta;
1288 if (GNUNET_YES == solver->bulk_lock)
1290 solver->bulk_changes++;
1294 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1296 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1298 if (0 == solver->step_count)
1300 solver->step_time_last = GNUNET_TIME_absolute_get ();
1303 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1304 time_now = GNUNET_TIME_absolute_get ();
1305 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1306 tau = ((double) time_delta.rel_value_us)
1307 / ((double) solver->parameters.step_time_min.rel_value_us);
1308 solver->step_time_last = time_now;
1310 //calculate reward discounts (once per step for all agents)
1311 solver->discount_variable = pow (M_E, ((-1.) * ((double) solver->parameters.beta) * tau));
1312 solver->discount_integrated = (1 - solver->discount_variable)
1313 / ((double) solver->parameters.beta);
1315 //calculate network state vector
1316 envi_state_networks(solver);
1318 //trigger one step per active agent
1319 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1321 if (cur->is_active && cur->address_inuse)
1327 solver->step_count += 1;
1328 ril_step_schedule_next (solver);
1330 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1332 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1333 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1335 if (cur->suggestion_issue) {
1336 solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1337 LOG(GNUNET_ERROR_TYPE_DEBUG, "\n Suggest address: %s\n\n",
1338 cur->suggestion_address->addr);
1339 cur->suggestion_issue = GNUNET_NO;
1342 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1348 ril_count_agents (struct GAS_RIL_Handle *solver)
1351 struct RIL_Peer_Agent *cur_agent;
1353 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1361 agent_w_start (struct RIL_Peer_Agent *agent)
1364 struct RIL_Peer_Agent *other;
1368 count = ril_count_agents(agent->envi);
1373 for (other = agent->envi->agents_head; NULL != other; other = other->next)
1375 for (i = 0; i < agent->n; i++)
1377 for (k = 0; k < agent->m; k++)
1379 agent->W[i][k] += (other->W[i][k] / (double) count);
1386 * Initialize an agent without addresses and its knowledge base
1388 * @param s ril solver
1389 * @param peer the one in question
1390 * @return handle to the new agent
1392 static struct RIL_Peer_Agent *
1393 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1396 struct GAS_RIL_Handle * solver = s;
1397 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
1399 agent->envi = solver;
1400 agent->peer = *peer;
1401 agent->step_count = 0;
1402 agent->is_active = GNUNET_NO;
1405 agent->suggestion_issue = GNUNET_NO;
1406 agent->n = RIL_ACTION_TYPE_NUM;
1407 agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
1408 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1409 for (i = 0; i < agent->n; i++)
1411 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1413 agent_w_start(agent);
1414 agent->a_old = RIL_ACTION_INVALID;
1415 agent->s_old = envi_get_state (solver, agent);
1416 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1417 agent_modify_eligibility (agent, RIL_E_ZERO);
1419 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
1427 * @param solver the solver handle
1428 * @param agent the agent to retire
1431 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1435 for (i = 0; i < agent->n; i++)
1437 GNUNET_free(agent->W[i]);
1439 GNUNET_free(agent->W);
1440 GNUNET_free(agent->e);
1441 GNUNET_free(agent->s_old);
1446 * Returns the agent for a peer
1448 * @param solver the solver handle
1449 * @param peer the identity of the peer
1450 * @param create whether or not to create an agent, if none is allocated yet
1453 static struct RIL_Peer_Agent *
1454 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1456 struct RIL_Peer_Agent *cur;
1458 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1460 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1468 return agent_init (solver, peer);
1474 * Lookup network struct by type
1476 * @param s the solver handle
1477 * @param type the network type
1478 * @return the network struct
1480 static struct RIL_Network *
1481 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1485 for (i = 0; i < s->networks_count; i++)
1487 if (s->network_entries[i].type == type)
1489 return &s->network_entries[i];
1496 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1497 * considered inactive and not used. Addresses in an inactive network are ignored.
1499 * @param solver solver handle
1500 * @param network the network type
1501 * @return whether or not the network is considered active
1504 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1506 struct RIL_Network *net;
1507 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1509 net = ril_get_network (solver, network);
1510 if (net->bw_out_available < min_bw)
1516 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1517 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1518 * frees the memory of the old vector, and redirects the pointer to the new one.
1520 * @param old pointer to the pointer to the first element of the vector
1521 * @param element_size byte size of the vector elements
1522 * @param hole_start the first element to cut out
1523 * @param hole_length the number of elements to cut out
1524 * @param old_length the length of the old vector
1527 ril_cut_from_vector (void **old,
1528 size_t element_size,
1529 unsigned int hole_start,
1530 unsigned int hole_length,
1531 unsigned int old_length)
1534 char *oldptr = (char *) *old;
1536 unsigned int bytes_before;
1537 unsigned int bytes_hole;
1538 unsigned int bytes_after;
1540 GNUNET_assert(old_length > hole_length);
1541 GNUNET_assert(old_length >= (hole_start + hole_length));
1543 size = element_size * (old_length - hole_length);
1545 bytes_before = element_size * hole_start;
1546 bytes_hole = element_size * hole_length;
1547 bytes_after = element_size * (old_length - hole_start - hole_length);
1555 tmpptr = GNUNET_malloc (size);
1556 memcpy (tmpptr, oldptr, bytes_before);
1557 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1563 *old = (void *) tmpptr;
1567 * Solver API functions
1568 * ---------------------------
1572 * Change relative preference for quality in solver
1574 * @param solver the solver handle
1575 * @param peer the peer to change the preference for
1576 * @param kind the kind to change the preference
1577 * @param pref_rel the normalized preference value for this kind over all clients
1580 GAS_ril_address_change_preference (void *solver,
1581 const struct GNUNET_PeerIdentity *peer,
1582 enum GNUNET_ATS_PreferenceKind kind,
1585 LOG(GNUNET_ERROR_TYPE_DEBUG,
1586 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1587 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1593 * Entry point for the plugin
1595 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1598 libgnunet_plugin_ats_ril_init (void *cls)
1600 struct GNUNET_ATS_PluginEnvironment *env = cls;
1601 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1602 struct RIL_Network * cur;
1606 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1608 GNUNET_assert(NULL != env);
1609 GNUNET_assert(NULL != env->cfg);
1610 GNUNET_assert(NULL != env->stats);
1611 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1612 GNUNET_assert(NULL != env->get_preferences);
1613 GNUNET_assert(NULL != env->get_property);
1616 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
1617 &solver->parameters.step_time_min))
1619 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
1622 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
1623 &solver->parameters.step_time_max))
1625 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
1627 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
1629 solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
1630 GNUNET_free (string);
1634 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1636 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
1638 solver->parameters.beta = strtod (string, NULL);
1639 GNUNET_free (string);
1643 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1646 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
1648 solver->parameters.alpha = strtod (string, NULL);
1649 GNUNET_free (string);
1653 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1655 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
1657 solver->parameters.lambda = strtod (string, NULL);
1658 GNUNET_free (string);
1662 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1664 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
1666 solver->parameters.explore_ratio = strtod (string, NULL);
1667 GNUNET_free (string);
1671 solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
1674 env->sf.s_add = &GAS_ril_address_add;
1675 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1676 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1677 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
1678 env->sf.s_address_update_network = &GAS_ril_address_change_network;
1679 env->sf.s_get = &GAS_ril_get_preferred_address;
1680 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
1681 env->sf.s_pref = &GAS_ril_address_change_preference;
1682 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
1683 env->sf.s_del = &GAS_ril_address_delete;
1684 env->sf.s_bulk_start = &GAS_ril_bulk_start;
1685 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
1687 solver->plugin_envi = env;
1688 solver->networks_count = env->network_count;
1689 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
1690 solver->step_count = 0;
1691 solver->state_networks = GNUNET_malloc (solver->networks_count * RIL_FEATURES_NETWORK_COUNT * sizeof (double));
1693 for (c = 0; c < env->network_count; c++)
1695 cur = &solver->network_entries[c];
1696 cur->type = env->networks[c];
1697 cur->bw_in_available = env->in_quota[c];
1698 cur->bw_out_available = env->out_quota[c];
1701 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (
1702 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1703 &ril_step_scheduler_task, solver);
1704 solver->task_pending = GNUNET_YES;
1710 * Exit point for the plugin
1712 * @param cls the solver handle
1715 libgnunet_plugin_ats_ril_done (void *cls)
1717 struct GAS_RIL_Handle *s = cls;
1718 struct RIL_Peer_Agent *cur_agent;
1719 struct RIL_Peer_Agent *next_agent;
1721 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1723 cur_agent = s->agents_head;
1724 while (NULL != cur_agent)
1726 next_agent = cur_agent->next;
1727 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1728 agent_die (s, cur_agent);
1729 cur_agent = next_agent;
1732 if (s->task_pending)
1734 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
1736 GNUNET_free(s->network_entries);
1737 GNUNET_free(s->state_networks);
1744 * Add a new address for a peer to the solver
1746 * The address is already contained in the addresses hashmap!
1748 * @param solver the solver Handle
1749 * @param address the address to add
1750 * @param network network type of this address
1753 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1755 struct GAS_RIL_Handle *s = solver;
1756 struct RIL_Peer_Agent *agent;
1757 struct RIL_Address_Wrapped *address_wrapped;
1758 struct RIL_Network *net;
1765 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1767 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
1769 net = ril_get_network (s, network);
1770 address->solver_information = net;
1772 if (!ril_network_is_active (s, network))
1774 LOG(GNUNET_ERROR_TYPE_DEBUG,
1775 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
1776 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1780 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1783 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1784 address_wrapped->address_naked = address;
1785 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1787 //increase size of W
1788 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
1790 n_new = agent->n + 1;
1793 GNUNET_array_grow(agent->W, agent->n, n_new);
1794 for (i = 0; i < n_new; i++)
1799 GNUNET_array_grow(agent->W[i], agent->m, m_new);
1804 GNUNET_array_grow(agent->W[i], zero, m_new);
1808 //increase size of old state vector
1810 GNUNET_array_grow(agent->s_old, agent->m, m_new);
1813 GNUNET_array_grow(agent->e, agent->m, m_new);
1815 if (NULL == agent->address_inuse)
1817 envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
1822 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
1823 address->active ? "active" : "inactive", address->plugin, address->addr,
1824 GNUNET_i2s (&address->peer));
1828 * Delete an address in the solver
1830 * The address is not contained in the address hashmap anymore!
1832 * @param solver the solver handle
1833 * @param address the address to remove
1834 * @param session_only delete only session not whole address
1837 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1839 struct GAS_RIL_Handle *s = solver;
1840 struct RIL_Peer_Agent *agent;
1841 struct RIL_Address_Wrapped *address_wrapped;
1842 int address_was_used = address->active;
1847 struct RIL_Network *net;
1848 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1850 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
1851 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
1852 address->addr, GNUNET_i2s (&address->peer));
1854 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1857 net = address->solver_information;
1858 GNUNET_assert(!ril_network_is_active (s, net->type));
1859 LOG(GNUNET_ERROR_TYPE_DEBUG,
1860 "No agent allocated for peer yet, since address was in inactive network\n");
1864 address_index = agent_address_get_index (agent, address);
1865 address_wrapped = agent_address_get (agent, address);
1867 if (NULL == address_wrapped)
1869 net = address->solver_information;
1870 GNUNET_assert(!ril_network_is_active (s, net->type));
1871 LOG(GNUNET_ERROR_TYPE_DEBUG,
1872 "Address not considered by agent, address was in inactive network\n");
1876 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1877 GNUNET_free(address_wrapped);
1880 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
1881 n_new = agent->n - 1;
1883 for (i = 0; i < agent->n; i++)
1885 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
1886 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1887 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1889 GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
1890 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
1892 //correct last action
1893 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1897 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1899 agent->a_old = RIL_ACTION_INVALID;
1901 //decrease old state vector and eligibility vector
1902 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
1903 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1904 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1905 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
1906 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1907 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1911 LOG(GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
1913 if (address_was_used)
1915 if (NULL != agent->addresses_head) //if peer has an address left, use it
1917 LOG(GNUNET_ERROR_TYPE_DEBUG, "address left: %s\n",
1918 agent->addresses_head->address_naked->addr);
1920 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw,
1925 LOG(GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
1927 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
1931 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted: %s\n", address->addr);
1937 * Update the properties of an address in the solver
1939 * @param solver solver handle
1940 * @param address the address
1941 * @param type the ATSI type in HBO
1942 * @param abs_value the absolute value of the property
1943 * @param rel_value the normalized value
1946 GAS_ril_address_property_changed (void *solver,
1947 struct ATS_Address *address,
1952 LOG(GNUNET_ERROR_TYPE_DEBUG,
1953 "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
1954 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1955 address->addr, rel_value);
1961 * Update the session of an address in the solver
1963 * NOTE: values in addresses are already updated
1965 * @param solver solver handle
1966 * @param address the address
1967 * @param cur_session the current session
1968 * @param new_session the new session
1971 GAS_ril_address_session_changed (void *solver,
1972 struct ATS_Address *address,
1973 uint32_t cur_session,
1974 uint32_t new_session)
1977 * TODO? Future Work: Potentially add session activity as a feature in state vector
1979 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1983 * Notify the solver that an address is (not) actively used by transport
1984 * to communicate with a remote peer
1986 * NOTE: values in addresses are already updated
1988 * @param solver solver handle
1989 * @param address the address
1990 * @param in_use usage state
1993 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1995 /* Nothing to do here.
1996 * Possible TODO? Future Work: Potentially add usage variable to state vector
1998 LOG(GNUNET_ERROR_TYPE_DEBUG,
1999 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2000 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2004 * Notify solver that the network an address is located in has changed
2006 * NOTE: values in addresses are already updated
2008 * @param solver solver handle
2009 * @param address the address
2010 * @param current_network the current network
2011 * @param new_network the new network
2014 GAS_ril_address_change_network (void *solver,
2015 struct ATS_Address *address,
2016 uint32_t current_network,
2017 uint32_t new_network)
2019 struct GAS_RIL_Handle *s = solver;
2020 struct RIL_Peer_Agent *agent;
2022 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2023 "%s address of peer %s from '%s' to '%s'\n",
2024 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2025 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2027 if (address->active && !ril_network_is_active (solver, new_network))
2029 GAS_ril_address_delete (solver, address, GNUNET_NO);
2033 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2036 GNUNET_assert(!ril_network_is_active (solver, current_network));
2038 GAS_ril_address_add (s, address, new_network);
2042 address->solver_information = ril_get_network(solver, new_network);
2046 * Give feedback about the current assignment
2048 * @param solver the solver handle
2049 * @param application the application
2050 * @param peer the peer to change the preference for
2051 * @param scope the time interval for this feedback: [now - scope .. now]
2052 * @param kind the kind to change the preference
2053 * @param score the score
2056 GAS_ril_address_preference_feedback (void *solver,
2058 const struct GNUNET_PeerIdentity *peer,
2059 const struct GNUNET_TIME_Relative scope,
2060 enum GNUNET_ATS_PreferenceKind kind,
2063 LOG(GNUNET_ERROR_TYPE_DEBUG,
2064 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2065 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2066 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2070 * Start a bulk operation
2072 * @param solver the solver
2075 GAS_ril_bulk_start (void *solver)
2077 struct GAS_RIL_Handle *s = solver;
2079 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2085 * Bulk operation done
2087 * @param solver the solver handle
2090 GAS_ril_bulk_stop (void *solver)
2092 struct GAS_RIL_Handle *s = solver;
2094 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2096 if (s->bulk_lock < 1)
2103 if (0 < s->bulk_changes)
2106 s->bulk_changes = 0;
2111 * Tell solver to notify ATS if the address to use changes for a specific
2112 * peer using the bandwidth changed callback
2114 * The solver must only notify about changes for peers with pending address
2117 * @param solver the solver handle
2118 * @param peer the identity of the peer
2120 const struct ATS_Address *
2121 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2124 * activate agent, return currently chosen address
2126 struct GAS_RIL_Handle *s = solver;
2127 struct RIL_Peer_Agent *agent;
2129 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2131 agent = ril_get_agent (s, peer, GNUNET_YES);
2133 agent->is_active = GNUNET_YES;
2135 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2138 if (agent->address_inuse)
2140 LOG(GNUNET_ERROR_TYPE_DEBUG,
2141 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2142 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2146 LOG(GNUNET_ERROR_TYPE_DEBUG,
2147 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2151 return agent->address_inuse;
2155 * Tell solver stop notifying ATS about changes for this peers
2157 * The solver must only notify about changes for peers with pending address
2160 * @param solver the solver handle
2161 * @param peer the peer
2164 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2166 struct GAS_RIL_Handle *s = solver;
2167 struct RIL_Peer_Agent *agent;
2169 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2171 agent = ril_get_agent (s, peer, GNUNET_NO);
2178 if (GNUNET_NO == agent->is_active)
2184 agent->is_active = GNUNET_NO;
2186 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2191 LOG(GNUNET_ERROR_TYPE_DEBUG,
2192 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2193 GNUNET_i2s (peer), agent->address_inuse->plugin);
2196 /* end of libgnunet_plugin_ats_ril.c */