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
35 #define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
36 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
37 #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
38 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
39 #define RIL_DEFAULT_TRACE_DECAY 0.6
40 #define RIL_EXPLORE_RATIO 0.1
43 * ATS reinforcement learning solver
49 * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
50 * changed for the currently chosen address. Not depicted in the enum are the actions of switching
51 * to a particular address. The action of switching to address with index i is depicted by the
52 * number (RIL_ACTION_TYPE_NUM + i).
56 RIL_ACTION_NOTHING = 0,
57 RIL_ACTION_BW_IN_DBL = 1,
58 RIL_ACTION_BW_IN_HLV = 2,
59 RIL_ACTION_BW_IN_INC = 3,
60 RIL_ACTION_BW_IN_DEC = 4,
61 RIL_ACTION_BW_OUT_DBL = 5,
62 RIL_ACTION_BW_OUT_HLV = 6,
63 RIL_ACTION_BW_OUT_INC = 7,
64 RIL_ACTION_BW_OUT_DEC = 8,
65 RIL_ACTION_TYPE_NUM = 9
74 enum RIL_E_Modification
83 * Global learning parameters
85 struct RIL_Learning_Parameters
88 * The TD-algorithm to use
90 enum RIL_Algorithm algorithm;
93 * Learning discount factor in the TD-update
98 * Gradient-descent step-size
103 * Trace-decay factor for eligibility traces
109 * Wrapper for addresses to store them in agent's linked list
111 struct RIL_Address_Wrapped
116 struct RIL_Address_Wrapped *next;
121 struct RIL_Address_Wrapped *prev;
126 struct ATS_Address *address_naked;
129 struct RIL_Peer_Agent
132 * Next agent in solver's linked list
134 struct RIL_Peer_Agent *next;
137 * Previous agent in solver's linked list
139 struct RIL_Peer_Agent *prev;
144 struct GAS_RIL_Handle *envi;
149 struct GNUNET_PeerIdentity peer;
152 * Whether the agent is active or not
157 * Number of performed time-steps
159 unsigned long long step_count;
162 * Experience matrix W
167 * Number of rows of W / Number of state-vector features
172 * Number of columns of W / Number of actions
177 * Last perceived state feature vector
187 * Eligibility trace vector
194 struct ATS_Address * address_inuse;
197 * Head of addresses DLL
199 struct RIL_Address_Wrapped * addresses_head;
202 * Tail of addresses DLL
204 struct RIL_Address_Wrapped * addresses_tail;
207 * Inbound bandwidth assigned by the agent
209 unsigned long long bw_in;
212 * Outbound bandwidth assigned by the agent
214 unsigned long long bw_out;
222 enum GNUNET_ATS_Network_Type type;
225 * Total available inbound bandwidth
227 unsigned long long bw_in_available;
230 * Total assigned outbound bandwidth
232 unsigned long long bw_in_assigned;
235 * Total available outbound bandwidth
237 unsigned long long bw_out_available;
240 * Total assigned outbound bandwidth
242 unsigned long long bw_out_assigned;
246 * A handle for the reinforcement learning solver
248 struct GAS_RIL_Handle
253 struct GNUNET_ATS_PluginEnvironment *plugin_envi;
258 struct GNUNET_STATISTICS_Handle *stats;
261 * Number of performed epochs
263 unsigned long long epoch_count;
266 * Number of performed steps
268 unsigned long long step_count;
271 * Interval time between steps in milliseconds //TODO? Future Work: Heterogeneous stepping among agents
273 struct GNUNET_TIME_Relative step_time;
276 * Task identifier of the next time-step to be executed
278 GNUNET_SCHEDULER_TaskIdentifier next_step;
281 * Lock for bulk operations
286 * Number of changes during a lock
291 * Learning parameters
293 struct RIL_Learning_Parameters parameters;
296 * Array of networks with global assignment state
298 struct RIL_Network * network_entries;
303 unsigned int networks_count;
306 * List of active peer-agents
308 struct RIL_Peer_Agent * agents_head;
309 struct RIL_Peer_Agent * agents_tail;
314 * ---------------------------
318 * Estimate the current action-value for state s and action a
320 * @param agent agent performing the estimation
323 * @return estimation value
326 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
331 for (i = 0; i < agent->m; i++)
333 result += state[i] * agent->W[action][i];
340 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
341 * currently estimated best action) in the current step
343 * @param agent agent performing the step
344 * @return yes, if exploring
347 agent_decide_exploration (struct RIL_Peer_Agent *agent)
349 //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
351 * An e-greedy replacement could be based on the accuracy of the prediction of the Q-value
353 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
354 UINT32_MAX) / (double) UINT32_MAX;
356 if (r < RIL_EXPLORE_RATIO)
364 * Get the index of the address in the agent's list.
366 * @param agent agent handle
367 * @param address address handle
368 * @return the index, starting with zero
371 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
374 struct RIL_Address_Wrapped *cur;
377 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
380 if (cur->address_naked == address)
390 * Gets the wrapped address from the agent's list
392 * @param agent agent handle
393 * @param address address handle
394 * @return wrapped address
396 static struct RIL_Address_Wrapped *
397 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
399 struct RIL_Address_Wrapped *cur;
401 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
403 if (cur->address_naked == address)
413 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
414 * most reward in the future)
416 * @param agent agent performing the calculation
417 * @param state the state from which to take the action
418 * @return the action promising most future reward
421 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
424 int max_i = RIL_ACTION_INVALID;
426 double max_q = -DBL_MAX;
428 for (i = 0; i < agent->n; i++)
430 cur_q = agent_estimate_q (agent, state, i);
438 GNUNET_assert(RIL_ACTION_INVALID != max_i);
444 * Gets any action, to explore the action space from that state
446 * @param agent agent performing the calculation
447 * @param state the state from which to take the action
451 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
453 // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
454 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
458 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
460 * @param agent the agent performing the update
461 * @param reward the reward received for the last action
462 * @param s_next the new state, the last step got the agent into
463 * @param a_prime the new
466 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
470 double *theta = agent->W[agent->a_old];
472 delta = reward + agent_estimate_q (agent, s_next, a_prime)
473 - agent_estimate_q (agent, agent->s_old, agent->a_old);
474 for (i = 0; i < agent->m; i++)
476 theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
481 * Changes the eligibility trace vector e in various manners:
482 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
483 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
484 * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
485 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
487 * @param agent the agent handle
488 * @param mod the kind of modification
491 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
494 double *e = agent->e;
495 double gamma = agent->envi->parameters.gamma;
496 double lambda = agent->envi->parameters.lambda;
498 for (i = 0; i < agent->m; i++)
502 case RIL_E_ACCUMULATE:
509 e[i] = gamma * lambda;
518 static void ril_inform (struct GAS_RIL_Handle *solver,
519 enum GAS_Solver_Operation op, enum GAS_Solver_Status stat)
521 if (NULL != solver->plugin_envi->info_cb)
522 solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
526 * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
527 * notify ATS of its new decision
529 * @param solver solver handle
530 * @param agent agent handle
531 * @param new_address the address which is to be used
532 * @param new_bw_in the new amount of inbound bandwidth set for this address
533 * @param new_bw_out the new amount of outbound bandwidth set for this address
534 * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
537 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
538 struct RIL_Peer_Agent *agent,
539 struct ATS_Address *new_address,
540 unsigned long long new_bw_in,
541 unsigned long long new_bw_out,
544 int notify = GNUNET_NO;
546 LOG(GNUNET_ERROR_TYPE_DEBUG, "set_active_suggestion()\n");
549 if (agent->address_inuse != new_address)
551 if (NULL != agent->address_inuse)
553 agent->address_inuse->active = GNUNET_NO;
554 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
555 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
557 if (NULL != new_address)
559 LOG(GNUNET_ERROR_TYPE_DEBUG, "set address active: %s\n", agent->is_active ? "yes" : "no");
560 new_address->active = agent->is_active;
561 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
562 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
564 notify |= GNUNET_YES;
570 if (new_address->active != agent->is_active)
572 new_address->active = agent->is_active;
576 if (agent->bw_in != new_bw_in)
578 agent->bw_in = new_bw_in;
579 new_address->assigned_bw_in.value__ = htonl (new_bw_out);
580 notify |= GNUNET_YES;
582 if (agent->bw_out != new_bw_out)
584 agent->bw_out = new_bw_out;
585 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
586 notify |= GNUNET_YES;
590 if (notify && agent->is_active && (GNUNET_NO == silent))
594 solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
597 else if (agent->address_inuse)
599 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
600 GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
604 solver->plugin_envi->bandwidth_changed_cb (solver->plugin_envi->bw_changed_cb_cls,
605 agent->address_inuse);
608 agent->address_inuse = new_address;
612 * Allocates a state vector and fills it with the features present
613 * @param solver the solver handle
614 * @param agent the agent handle
615 * @return pointer to the state vector
618 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
622 struct RIL_Network *net;
623 double *state = GNUNET_malloc (sizeof (double) * agent->m);
624 struct RIL_Address_Wrapped *cur_address;
625 const double *properties;
627 for (i = 0; i < solver->networks_count; i++)
629 net = &solver->network_entries[i];
630 state[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) net->bw_in_assigned;
631 state[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net->bw_in_available;
632 state[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) net->bw_out_assigned;
633 state[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net->bw_out_available;
636 i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
638 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
640 state[i++] = cur_address->address_naked->active;
641 state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
642 state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
643 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
644 cur_address->address_naked);
645 for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
647 state[i++] = properties[k];
655 * For all networks a peer has an address in, this gets the maximum bandwidth which could
656 * theoretically be available in one of the networks. This is used for bandwidth normalization.
658 * @param agent the agent handle
659 * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
661 static long long unsigned
662 ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
665 * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
666 * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
669 struct RIL_Address_Wrapped *cur;
670 struct RIL_Network *net;
672 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
674 net = cur->address_naked->solver_information;
677 if (net->bw_in_available > max)
679 max = net->bw_in_available;
684 if (net->bw_out_available > max)
686 max = net->bw_out_available;
694 * Get the index of the quality-property in question
696 * @param type the quality property type
700 ril_find_property_index (uint32_t type)
702 int existing_types[] = GNUNET_ATS_QualityProperties;
704 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
705 if (existing_types[c] == type)
707 return GNUNET_SYSERR;
711 envi_reward_global (struct GAS_RIL_Handle *solver)
714 unsigned int in_available = 0;
715 unsigned int out_available = 0;
716 unsigned int in_assigned = 0;
717 unsigned int out_assigned = 0;
721 for (i = 0; i < solver->networks_count; i++)
723 in_available += solver->network_entries[i].bw_in_available;
724 in_assigned += solver->network_entries[i].bw_in_assigned;
725 out_available += solver->network_entries[i].bw_out_available;
726 out_assigned += solver->network_entries[i].bw_out_assigned;
729 ratio_in = ((double) in_assigned) / ((double) in_available);
730 ratio_out = ((double) out_assigned) / ((double) out_available);
732 return ((ratio_in + ratio_out) * 0.5) + 1;
736 envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
738 //TODO! add utilization
740 const double *preferences;
741 const double *properties;
743 double pref_match = 0;
746 preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
748 properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
749 agent->address_inuse);
751 //preference matching from latency and bandwidth
752 prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
753 pref_match += 1 - (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * (3 - properties[prop_index])); //invert property as we want to maximize for lower latencies
754 bw_norm = GNUNET_MAX(2, (((
755 ((double) agent->bw_in / (double) ril_get_max_bw(agent, GNUNET_YES)) +
756 ((double) agent->bw_out / (double) ril_get_max_bw(agent, GNUNET_NO))
760 pref_match += 1 - (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
762 return pref_match * 0.5;
766 * Gets the reward for the last performed step, which is calculated in equal
767 * parts from the local (the peer specific) and the global (for all peers
770 * @param solver the solver handle
771 * @param agent the agent handle
775 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
777 struct RIL_Network *net;
780 //punish overutilization
781 net = agent->address_inuse->solver_information;
782 if ((net->bw_in_assigned > net->bw_in_available) || (net->bw_out_assigned > net->bw_out_available))
787 reward += envi_reward_global (solver);
788 reward += envi_reward_local (solver, agent);
793 * Doubles the bandwidth for the active address
795 * @param solver solver handle
796 * @param agent agent handle
797 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
800 envi_action_bw_double (struct GAS_RIL_Handle *solver,
801 struct RIL_Peer_Agent *agent,
806 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2,
807 agent->bw_out, GNUNET_NO);
811 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
812 agent->bw_out * 2, GNUNET_NO);
817 * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
818 * the minimum bandwidth for a peer, in order to not invoke a disconnect.
820 * @param solver solver handle
821 * @param agent agent handle
822 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
826 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
827 struct RIL_Peer_Agent *agent,
830 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
831 unsigned long long new_bw;
835 new_bw = agent->bw_in / 2;
838 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
843 new_bw = agent->bw_out / 2;
846 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
852 * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
854 * @param solver solver handle
855 * @param agent agent handle
856 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
860 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
862 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
866 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
867 agent->bw_out, GNUNET_NO);
871 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
872 agent->bw_out + (5 * min_bw), GNUNET_NO);
877 * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
878 * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
880 * @param solver solver handle
881 * @param agent agent handle
882 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
886 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
888 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
889 unsigned long long new_bw;
893 new_bw = agent->bw_in - (5 * min_bw);
896 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
901 new_bw = agent->bw_out - (5 * min_bw);
904 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
910 * Switches to the address given by its index
912 * @param solver solver handle
913 * @param agent agent handle
914 * @param address_index index of the address as it is saved in the agent's list, starting with zero
917 envi_action_address_switch (struct GAS_RIL_Handle *solver,
918 struct RIL_Peer_Agent *agent,
919 unsigned int address_index)
921 struct RIL_Address_Wrapped *cur;
924 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
926 if (i == address_index)
928 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
936 //no address with address_index exists, in this case this action should not be callable
937 GNUNET_assert(GNUNET_NO);
941 * Puts the action into effect by calling the according function
943 * @param solver the solver handle
944 * @param agent the action handle
945 * @param action the action to perform by the solver
948 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
954 case RIL_ACTION_NOTHING:
956 case RIL_ACTION_BW_IN_DBL:
957 envi_action_bw_double (solver, agent, GNUNET_YES);
959 case RIL_ACTION_BW_IN_HLV:
960 envi_action_bw_halven (solver, agent, GNUNET_YES);
962 case RIL_ACTION_BW_IN_INC:
963 envi_action_bw_inc (solver, agent, GNUNET_YES);
965 case RIL_ACTION_BW_IN_DEC:
966 envi_action_bw_dec (solver, agent, GNUNET_YES);
968 case RIL_ACTION_BW_OUT_DBL:
969 envi_action_bw_double (solver, agent, GNUNET_NO);
971 case RIL_ACTION_BW_OUT_HLV:
972 envi_action_bw_halven (solver, agent, GNUNET_NO);
974 case RIL_ACTION_BW_OUT_INC:
975 envi_action_bw_inc (solver, agent, GNUNET_NO);
977 case RIL_ACTION_BW_OUT_DEC:
978 envi_action_bw_dec (solver, agent, GNUNET_NO);
981 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
983 address_index = action - RIL_ACTION_TYPE_NUM;
985 GNUNET_assert(address_index >= 0);
987 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
989 envi_action_address_switch (solver, agent, address_index);
992 // error - action does not exist
993 GNUNET_assert(GNUNET_NO);
998 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
999 * after having done the last action a_old. It observes the new state s_next and the reward
1000 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1001 * next action is put into effect.
1003 * @param agent the agent performing the step
1006 agent_step (struct RIL_Peer_Agent *agent)
1008 int a_next = RIL_ACTION_INVALID;
1012 s_next = envi_get_state (agent->envi, agent);
1013 reward = envi_get_reward (agent->envi, agent);
1015 LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
1016 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1018 switch (agent->envi->parameters.algorithm)
1020 case RIL_ALGO_SARSA:
1021 agent_modify_eligibility (agent, RIL_E_SET);
1022 if (agent_decide_exploration (agent))
1024 a_next = agent_get_action_explore (agent, s_next);
1028 a_next = agent_get_action_best (agent, s_next);
1030 if (RIL_ACTION_INVALID != agent->a_old)
1032 //updates weights with selected action (on-policy), if not first step
1033 agent_update_weights (agent, reward, s_next, a_next);
1038 a_next = agent_get_action_best (agent, s_next);
1039 if (RIL_ACTION_INVALID != agent->a_old)
1041 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1042 agent_update_weights (agent, reward, s_next, a_next);
1044 if (agent_decide_exploration (agent))
1046 a_next = agent_get_action_explore (agent, s_next);
1047 agent_modify_eligibility (agent, RIL_E_ZERO);
1051 a_next = agent_get_action_best (agent, s_next);
1052 agent_modify_eligibility (agent, RIL_E_SET);
1057 GNUNET_assert(RIL_ACTION_INVALID != a_next);
1059 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
1061 envi_do_action (agent->envi, agent, a_next);
1063 GNUNET_free(agent->s_old);
1064 agent->s_old = s_next;
1065 agent->a_old = a_next;
1067 agent->step_count += 1;
1071 * Triggers one epoch of agent decisions
1075 ril_epoch (struct GAS_RIL_Handle *solver)
1077 //TODO! add multiple steps per epoch
1078 struct RIL_Peer_Agent *cur;
1080 if (GNUNET_YES == solver->bulk_lock)
1082 solver->bulk_changes ++;
1086 ril_inform(solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1087 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1089 if (cur->is_active && cur->address_inuse)
1094 ril_inform(solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1100 * Cycles through all agents and lets the active ones do a step. Schedules the next step.
1102 * @param cls the solver handle
1103 * @param tc the task context for the scheduler
1106 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1108 struct GAS_RIL_Handle *solver = cls;
1110 LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
1114 solver->epoch_count += 1;
1115 solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, &ril_periodic_step, solver);
1119 * Initialize an agent without addresses and its knowledge base
1121 * @param s ril solver
1122 * @param peer the one in question
1123 * @return handle to the new agent
1125 static struct RIL_Peer_Agent *
1126 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1129 struct GAS_RIL_Handle * solver = s;
1130 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
1132 agent->envi = solver;
1133 agent->peer = *peer;
1134 agent->step_count = 0;
1135 agent->is_active = GNUNET_NO;
1136 agent->n = RIL_ACTION_TYPE_NUM;
1137 agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
1138 agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1139 for (i = 0; i < agent->n; i++)
1141 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1143 agent->a_old = RIL_ACTION_INVALID;
1144 agent->s_old = envi_get_state (solver, agent);
1145 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1146 agent_modify_eligibility (agent, RIL_E_ZERO);
1148 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
1156 * @param solver the solver handle
1157 * @param agent the agent to retire
1160 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1164 for (i = 0; i < agent->n; i++)
1166 GNUNET_free(agent->W[i]);
1168 GNUNET_free(agent->W);
1169 GNUNET_free(agent->e);
1170 GNUNET_free(agent->s_old);
1175 * Returns the agent for a peer
1177 * @param solver the solver handle
1178 * @param peer the identity of the peer
1179 * @param create whether or not to create an agent, if none is allocated yet
1182 static struct RIL_Peer_Agent *
1183 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1185 struct RIL_Peer_Agent *cur;
1187 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1189 if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1197 return agent_init (solver, peer);
1203 * Lookup network struct by type
1205 * @param s the solver handle
1206 * @param type the network type
1207 * @return the network struct
1209 static struct RIL_Network *
1210 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1214 for (i = 0; i < s->networks_count; i++)
1216 if (s->network_entries[i].type == type)
1218 return &s->network_entries[i];
1225 * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1226 * considered inactive and not used. Addresses in an inactive network are ignored.
1228 * @param solver solver handle
1229 * @param network the network type
1230 * @return whether or not the network is considered active
1233 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1235 struct RIL_Network *net;
1236 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1238 net = ril_get_network (solver, network);
1239 if (net->bw_out_available < min_bw)
1245 * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1246 * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1247 * frees the memory of the old vector, and redirects the pointer to the new one.
1249 * @param old pointer to the pointer to the first element of the vector
1250 * @param element_size byte size of the vector elements
1251 * @param hole_start the first element to cut out
1252 * @param hole_length the number of elements to cut out
1253 * @param old_length the length of the old vector
1256 ril_cut_from_vector (void **old,
1257 size_t element_size,
1258 unsigned int hole_start,
1259 unsigned int hole_length,
1260 unsigned int old_length)
1263 char *oldptr = (char *) *old;
1265 unsigned int bytes_before;
1266 unsigned int bytes_hole;
1267 unsigned int bytes_after;
1269 GNUNET_assert(old_length > hole_length);
1270 GNUNET_assert(old_length >= (hole_start + hole_length));
1272 size = element_size * (old_length - hole_length);
1274 bytes_before = element_size * hole_start;
1275 bytes_hole = element_size * hole_length;
1276 bytes_after = element_size * (old_length - hole_start - hole_length);
1284 tmpptr = GNUNET_malloc (size);
1285 memcpy (tmpptr, oldptr, bytes_before);
1286 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1292 *old = (void *) tmpptr;
1296 * Solver API functions
1297 * ---------------------------
1301 * Change relative preference for quality in solver
1303 * @param solver the solver handle
1304 * @param peer the peer to change the preference for
1305 * @param kind the kind to change the preference
1306 * @param pref_rel the normalized preference value for this kind over all clients
1309 GAS_ril_address_change_preference (void *solver,
1310 const struct GNUNET_PeerIdentity *peer,
1311 enum GNUNET_ATS_PreferenceKind kind,
1314 LOG(GNUNET_ERROR_TYPE_DEBUG,
1315 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1316 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1322 * Entry point for the plugin
1324 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1327 libgnunet_plugin_ats_ril_init (void *cls)
1329 struct GNUNET_ATS_PluginEnvironment *env = cls;
1330 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1331 struct RIL_Network * cur;
1333 unsigned long long tmp;
1336 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1338 GNUNET_assert(NULL != env);
1339 GNUNET_assert(NULL != env->cfg);
1340 GNUNET_assert(NULL != env->stats);
1341 GNUNET_assert(NULL != env->bandwidth_changed_cb);
1342 GNUNET_assert(NULL != env->get_preferences);
1343 GNUNET_assert(NULL != env->get_property);
1346 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
1348 solver->step_time = RIL_DEFAULT_STEP_TIME;
1350 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string)
1351 && NULL != string && 0 == strcmp (string, "SARSA"))
1353 solver->parameters.algorithm = RIL_ALGO_SARSA;
1357 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1360 == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
1362 solver->parameters.gamma = (double) tmp / 100;
1366 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
1369 == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
1371 solver->parameters.alpha = (double) tmp / 100;
1375 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1377 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats", "RIL_TRACE_DECAY", &tmp))
1379 solver->parameters.lambda = (double) tmp / 100;
1383 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1386 env->sf.s_add = &GAS_ril_address_add;
1387 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1388 env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1389 env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
1390 env->sf.s_address_update_network = &GAS_ril_address_change_network;
1391 env->sf.s_get = &GAS_ril_get_preferred_address;
1392 env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
1393 env->sf.s_pref = &GAS_ril_address_change_preference;
1394 env->sf.s_feedback = &GAS_ril_address_preference_feedback;
1395 env->sf.s_del = &GAS_ril_address_delete;
1396 env->sf.s_bulk_start = &GAS_ril_bulk_start;
1397 env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
1399 solver->plugin_envi = env;
1400 solver->networks_count = env->network_count;
1401 solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
1402 solver->step_count = 0;
1404 for (c = 0; c < env->network_count; c++)
1406 cur = &solver->network_entries[c];
1407 cur->type = env->networks[c];
1408 cur->bw_in_available = env->in_quota[c];
1409 cur->bw_in_assigned = 0;
1410 cur->bw_out_available = env->out_quota[c];
1411 cur->bw_out_assigned = 0;
1414 solver->next_step = GNUNET_SCHEDULER_add_delayed (
1415 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1416 &ril_periodic_step, solver);
1422 * Exit point for the plugin
1424 * @param cls the solver handle
1427 libgnunet_plugin_ats_ril_done (void *cls)
1429 struct GAS_RIL_Handle *s = cls;
1430 struct RIL_Peer_Agent *cur_agent;
1431 struct RIL_Peer_Agent *next_agent;
1433 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1435 cur_agent = s->agents_head;
1436 while (NULL != cur_agent)
1438 next_agent = cur_agent->next;
1439 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1440 agent_die (s, cur_agent);
1441 cur_agent = next_agent;
1444 GNUNET_SCHEDULER_cancel (s->next_step);
1445 GNUNET_free(s->network_entries);
1452 * Add a new address for a peer to the solver
1454 * The address is already contained in the addresses hashmap!
1456 * @param solver the solver Handle
1457 * @param address the address to add
1458 * @param network network type of this address
1461 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1463 struct GAS_RIL_Handle *s = solver;
1464 struct RIL_Peer_Agent *agent;
1465 struct RIL_Address_Wrapped *address_wrapped;
1466 struct RIL_Network *net;
1473 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1475 net = ril_get_network (s, network);
1476 address->solver_information = net;
1478 if (!ril_network_is_active (s, network))
1480 LOG(GNUNET_ERROR_TYPE_DEBUG,
1481 "API_address_add() Did not add %s address %p for peer '%s', network does not have enough bandwidth\n",
1482 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1486 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1489 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1490 address_wrapped->address_naked = address;
1491 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1493 //increase size of W
1494 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
1496 n_new = agent->n + 1;
1499 GNUNET_array_grow(agent->W, agent->n, n_new);
1500 for (i = 0; i < n_new; i++)
1505 GNUNET_array_grow(agent->W[i], agent->m, m_new);
1510 GNUNET_array_grow(agent->W[i], zero, m_new);
1514 //increase size of old state vector
1516 GNUNET_array_grow(agent->s_old, agent->m, m_new);
1519 GNUNET_array_grow(agent->e, agent->m, m_new);
1521 if (NULL == agent->address_inuse)
1523 net->bw_in_assigned += min_bw;
1524 net->bw_out_assigned += min_bw;
1525 envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
1530 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %p for peer '%s'\n",
1531 address->active ? "active" : "inactive", address->plugin, address->addr,
1532 GNUNET_i2s (&address->peer));
1536 * Delete an address in the solver
1538 * The address is not contained in the address hashmap anymore!
1540 * @param solver the solver handle
1541 * @param address the address to remove
1542 * @param session_only delete only session not whole address
1545 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1547 struct GAS_RIL_Handle *s = solver;
1548 struct RIL_Peer_Agent *agent;
1549 struct RIL_Address_Wrapped *address_wrapped;
1550 int address_was_used = address->active;
1555 struct RIL_Network *net;
1556 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1558 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
1559 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
1560 address->addr, GNUNET_i2s (&address->peer));
1562 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1565 net = address->solver_information;
1566 GNUNET_assert(!ril_network_is_active (s, net->type));
1567 LOG(GNUNET_ERROR_TYPE_DEBUG,
1568 "No agent allocated for peer yet, since address was in inactive network\n");
1572 address_index = agent_address_get_index (agent, address);
1573 address_wrapped = agent_address_get (agent, address);
1575 if (NULL == address_wrapped)
1577 net = address->solver_information;
1578 GNUNET_assert(!ril_network_is_active (s, net->type));
1579 LOG(GNUNET_ERROR_TYPE_DEBUG,
1580 "Address not considered by agent, address was in inactive network\n");
1584 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1585 GNUNET_free(address_wrapped);
1588 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
1589 n_new = agent->n - 1;
1591 for (i = 0; i < agent->n; i++)
1593 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
1594 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1595 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1597 GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
1598 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
1600 //correct last action
1601 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1605 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1607 agent->a_old = RIL_ACTION_INVALID;
1609 //decrease old state vector and eligibility vector
1610 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
1611 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1612 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1613 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
1614 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1615 + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1619 LOG(GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
1621 if (address_was_used)
1623 net = address->solver_information;
1624 net->bw_in_assigned -= agent->bw_in;
1625 net->bw_out_assigned -= agent->bw_out;
1627 if (NULL != agent->addresses_head) //if peer has an address left, use it
1629 LOG(GNUNET_ERROR_TYPE_DEBUG, "address left: %p\n",
1630 agent->addresses_head->address_naked->addr);
1631 //TODO? check if network/bandwidth update can be done more clever/elegant at different function
1632 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw,
1634 net = agent->addresses_head->address_naked->solver_information;
1635 net->bw_in_assigned -= min_bw;
1636 net->bw_out_assigned -= min_bw;
1640 LOG(GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
1642 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
1648 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
1652 * Update the properties of an address in the solver
1654 * @param solver solver handle
1655 * @param address the address
1656 * @param type the ATSI type in HBO
1657 * @param abs_value the absolute value of the property
1658 * @param rel_value the normalized value
1661 GAS_ril_address_property_changed (void *solver,
1662 struct ATS_Address *address,
1667 LOG(GNUNET_ERROR_TYPE_DEBUG,
1668 "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1669 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1670 address->addr, rel_value);
1676 * Update the session of an address in the solver
1678 * NOTE: values in addresses are already updated
1680 * @param solver solver handle
1681 * @param address the address
1682 * @param cur_session the current session
1683 * @param new_session the new session
1686 GAS_ril_address_session_changed (void *solver,
1687 struct ATS_Address *address,
1688 uint32_t cur_session,
1689 uint32_t new_session)
1692 * TODO? Future Work: Potentially add session activity as a feature in state vector
1694 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1698 * Notify the solver that an address is (not) actively used by transport
1699 * to communicate with a remote peer
1701 * NOTE: values in addresses are already updated
1703 * @param solver solver handle
1704 * @param address the address
1705 * @param in_use usage state
1708 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1710 /* Nothing to do here.
1711 * Possible TODO? Future Work: Use usage as state vector
1713 LOG(GNUNET_ERROR_TYPE_DEBUG,
1714 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1715 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1719 * Notify solver that the network an address is located in has changed
1721 * NOTE: values in addresses are already updated
1723 * @param solver solver handle
1724 * @param address the address
1725 * @param current_network the current network
1726 * @param new_network the new network
1729 GAS_ril_address_change_network (void *solver,
1730 struct ATS_Address *address,
1731 uint32_t current_network,
1732 uint32_t new_network)
1734 struct GAS_RIL_Handle *s = solver;
1735 struct RIL_Peer_Agent *agent;
1736 struct RIL_Network *net;
1737 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1739 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
1740 "%s address of peer %s from '%s' to '%s'\n",
1741 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
1742 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
1744 if (address->active && !ril_network_is_active (solver, new_network))
1746 GAS_ril_address_delete (solver, address, GNUNET_NO);
1750 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1753 GNUNET_assert(!ril_network_is_active (solver, current_network));
1755 GAS_ril_address_add (s, address, new_network);
1759 net = ril_get_network (s, current_network);
1760 net->bw_in_assigned -= agent->bw_in;
1761 net->bw_out_assigned -= agent->bw_out;
1763 net = ril_get_network (s, new_network);
1764 net->bw_in_assigned -= min_bw;
1765 net->bw_out_assigned -= min_bw;
1766 address->solver_information = net;
1770 * Give feedback about the current assignment
1772 * @param solver the solver handle
1773 * @param application the application
1774 * @param peer the peer to change the preference for
1775 * @param scope the time interval for this feedback: [now - scope .. now]
1776 * @param kind the kind to change the preference
1777 * @param score the score
1780 GAS_ril_address_preference_feedback (void *solver,
1782 const struct GNUNET_PeerIdentity *peer,
1783 const struct GNUNET_TIME_Relative scope,
1784 enum GNUNET_ATS_PreferenceKind kind,
1787 LOG(GNUNET_ERROR_TYPE_DEBUG,
1788 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1789 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1790 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1794 * Start a bulk operation
1796 * @param solver the solver
1799 GAS_ril_bulk_start (void *solver)
1801 struct GAS_RIL_Handle *s = solver;
1803 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() Locking solver for bulk operation ...\n");
1809 * Bulk operation done
1811 * @param solver the solver handle
1814 GAS_ril_bulk_stop (void *solver)
1816 /* TODO! trigger "recalculation" after bulk / trigger "recalculation" after changes (address or preference)
1817 * that has to be properly thought through, not when your mind is blocked completely
1819 struct GAS_RIL_Handle *s = solver;
1821 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() Releasing solver from bulk operation ...\n");
1823 if (s->bulk_lock < 1)
1830 if (0 < s->bulk_changes)
1833 s->bulk_changes = 0;
1838 * Tell solver to notify ATS if the address to use changes for a specific
1839 * peer using the bandwidth changed callback
1841 * The solver must only notify about changes for peers with pending address
1844 * @param solver the solver handle
1845 * @param peer the identity of the peer
1847 const struct ATS_Address *
1848 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1851 * activate agent, return currently chosen address
1853 struct GAS_RIL_Handle *s = solver;
1854 struct RIL_Peer_Agent *agent;
1855 struct RIL_Network *net;
1857 agent = ril_get_agent (s, peer, GNUNET_YES);
1859 agent->is_active = GNUNET_YES;
1861 if (agent->address_inuse)
1863 net = agent->address_inuse->solver_information;
1864 net->bw_in_assigned += agent->bw_in;
1865 net->bw_out_assigned += agent->bw_out;
1867 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
1870 if (agent->address_inuse)
1872 LOG(GNUNET_ERROR_TYPE_DEBUG,
1873 "API_get_preferred_address() Activated agent for peer '%s' with %s address %p\n",
1874 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
1878 LOG(GNUNET_ERROR_TYPE_DEBUG,
1879 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
1885 return agent->address_inuse;
1889 * Tell solver stop notifying ATS about changes for this peers
1891 * The solver must only notify about changes for peers with pending address
1894 * @param solver the solver handle
1895 * @param peer the peer
1898 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1900 struct GAS_RIL_Handle *s = solver;
1901 struct RIL_Peer_Agent *agent;
1902 struct RIL_Network *net;
1904 agent = ril_get_agent (s, peer, GNUNET_NO);
1911 if (GNUNET_NO == agent->is_active)
1917 agent->is_active = GNUNET_NO;
1918 if (agent->address_inuse)
1920 net = agent->address_inuse->solver_information;
1921 net->bw_in_assigned -= agent->bw_in;
1922 net->bw_out_assigned -= agent->bw_out;
1924 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
1929 LOG(GNUNET_ERROR_TYPE_DEBUG,
1930 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1931 GNUNET_i2s (peer), agent->address_inuse->plugin);
1934 /* end of libgnunet_plugin_ats_ril.c */