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/gnunet-service-ats-solver_ril.c
23 * @brief ATS reinforcement learning solver
24 * @author Fabian Oehlmann
25 * @author Matthias Wachs
29 #include "gnunet_util_lib.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet_statistics_service.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
35 #define RIL_ACTION_INVALID -1
36 #define RIL_FEATURES_ADDRESS_COUNT (3 + GNUNET_ATS_QualityPropertiesCount)
37 #define RIL_FEATURES_NETWORK_COUNT 4
39 #define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
40 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
41 #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
42 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
43 #define RIL_DEFAULT_TRACE_DECAY 0.6
44 #define RIL_EXPLORE_RATIO 0.1
47 * ATS reinforcement learning solver
53 * TODO! implement reward calculation 1 and 2 (i.e. meeting preferences and taking scores)
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
111 * Wrapper for addresses to store them in agent's linked list
113 struct RIL_Address_Wrapped
118 struct RIL_Address_Wrapped *next;
123 struct RIL_Address_Wrapped *prev;
128 struct ATS_Address *address_naked;
131 struct RIL_Peer_Agent
134 * Next agent in solver's linked list
136 struct RIL_Peer_Agent *next;
139 * Previous agent in solver's linked list
141 struct RIL_Peer_Agent *prev;
146 struct GAS_RIL_Handle *envi;
151 struct GNUNET_PeerIdentity peer;
154 * Whether the agent is active or not
156 int active; //TODO? rename into "requested", since it rather depicts whether there is a request pending for it
159 * Number of performed time-steps
161 unsigned long long step_count;
164 * Experience matrix W
169 * Number of rows of W / Number of state-vector features
174 * Number of columns of W / Number of actions
179 * Last perceived state feature vector
189 * Eligibility trace vector
196 struct ATS_Address * address_inuse;
199 * Head of addresses DLL
201 struct RIL_Address_Wrapped * addresses_head;
204 * Tail of addresses DLL
206 struct RIL_Address_Wrapped * addresses_tail;
209 * Inbound bandwidth assigned by the agent
211 unsigned long long bw_in;
214 * Outbound bandwidth assigned by the agent
216 unsigned long long bw_out;
224 enum GNUNET_ATS_Network_Type type;
227 * Total available inbound bandwidth
229 unsigned long long bw_in_available;
232 * Total assigned outbound bandwidth
234 unsigned long long bw_in_assigned;
237 * Total available outbound bandwidth
239 unsigned long long bw_out_available;
242 * Total assigned outbound bandwidth
244 unsigned long long bw_out_assigned;
250 * Bandwidth changed callback
252 GAS_bandwidth_changed_cb bw_changed;
255 * Bandwidth changed callback cls
257 void *bw_changed_cls;
260 * ATS function to get preferences
262 GAS_get_preferences get_preferences;
265 * Closure for ATS function to get preferences
267 void *get_preferences_cls;
270 * ATS function to get properties
272 GAS_get_properties get_properties;
275 * Closure for ATS function to get properties
277 void *get_properties_cls;
281 * A handle for the reinforcement learning solver
283 struct GAS_RIL_Handle
288 struct GNUNET_STATISTICS_Handle *stats;
291 * Hashmap containing all valid addresses
293 const struct GNUNET_CONTAINER_MultiHashMap *addresses;
296 * Callbacks for the solver
298 struct RIL_Callbacks *callbacks;
306 * Number of changes while solver was locked
311 * Number of performed time-steps
313 unsigned long long step_count;
316 * Interval time between steps in milliseconds //TODO? put in agent
318 struct GNUNET_TIME_Relative step_time;
321 * Task identifier of the next time-step to be executed //TODO? put in agent
323 GNUNET_SCHEDULER_TaskIdentifier next_step;
326 * Learning parameters
328 struct RIL_Learning_Parameters parameters;
331 * Array of networks with global assignment state
333 struct RIL_Network * network_entries;
338 unsigned int networks_count;
341 * List of active peer-agents
343 struct RIL_Peer_Agent * agents_head;
344 struct RIL_Peer_Agent * agents_tail;
349 * ---------------------------
353 * Estimate the current action-value for state s and action a
354 * @param agent agent performing the estimation
357 * @return estimation value
360 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
365 for (i = 0; i < agent->m; i++)
367 result += state[i] * agent->W[action][i];
374 * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
375 * currently estimated best action) in the current step
376 * @param agent agent performing the step
377 * @return yes, if exploring
380 agent_decide_exploration (struct RIL_Peer_Agent *agent)
382 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
383 UINT32_MAX) / (double) UINT32_MAX;
385 if (r < RIL_EXPLORE_RATIO)
393 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
396 struct RIL_Address_Wrapped *cur;
399 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
402 if (cur->address_naked == address)
411 static struct RIL_Address_Wrapped *
412 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
414 struct RIL_Address_Wrapped *cur;
416 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
418 if (cur->address_naked == address)
428 * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
429 * most reward in the future)
430 * @param agent agent performing the calculation
431 * @param state the state from which to take the action
432 * @return the action promising most future reward
435 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
438 int max_i = RIL_ACTION_INVALID;
440 double max_q = -DBL_MAX;
442 for (i = 0; i < agent->n; i++)
444 cur_q = agent_estimate_q (agent, state, i);
452 GNUNET_assert(RIL_ACTION_INVALID != max_i);
458 * Gets any action, to explore the action space from that state
459 * @param agent agent performing the calculation
460 * @param state the state from which to take the action
464 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
466 return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
470 * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
471 * @param agent the agent performing the update
472 * @param reward the reward received for the last action
473 * @param s_next the new state, the last step got the agent into
474 * @param a_prime the new
477 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
481 double *theta = agent->W[agent->a_old];
483 delta = reward + agent_estimate_q (agent, s_next, a_prime)
484 - agent_estimate_q (agent, agent->s_old, agent->a_old);
485 for (i = 0; i < agent->m; i++)
487 theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
492 * Changes the eligibility trace vector e in various manners:
493 * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
494 * RIL_E_REPLACE - resets each component to 1 as in replacing traces
495 * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
496 * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
501 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
504 double *e = agent->e;
505 double gamma = agent->envi->parameters.gamma;
506 double lambda = agent->envi->parameters.lambda;
508 for (i = 0; i < agent->m; i++)
512 case RIL_E_ACCUMULATE:
519 e[i] = gamma * lambda;
529 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
530 struct RIL_Peer_Agent *agent,
531 struct ATS_Address *new_address,
532 unsigned long long new_bw_in,
533 unsigned long long new_bw_out,
536 int notify = GNUNET_NO;
538 LOG (GNUNET_ERROR_TYPE_DEBUG, "set_active_suggestion()\n");
541 if (agent->address_inuse != new_address)
543 if (NULL != agent->address_inuse)
545 agent->address_inuse->active = GNUNET_NO;
546 agent->address_inuse->assigned_bw_in.value__ = htonl (0);
547 agent->address_inuse->assigned_bw_out.value__ = htonl (0);
549 if (NULL != new_address)
551 LOG (GNUNET_ERROR_TYPE_DEBUG, "set address active: %s\n", agent->active ? "yes" : "no");
552 new_address->active = agent->active;
553 new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
554 new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
556 notify |= GNUNET_YES;
562 if (new_address->active != agent->active)
564 new_address->active = agent->active;
568 if (agent->bw_in != new_bw_in)
570 agent->bw_in = new_bw_in;
571 new_address->assigned_bw_in.value__ = htonl (new_bw_out);
572 notify |= GNUNET_YES;
574 if (agent->bw_out != new_bw_out)
576 agent->bw_out = new_bw_out;
577 new_address->assigned_bw_out.value__ = htonl (new_bw_out);
578 notify |= GNUNET_YES;
582 if (notify && agent->active && (GNUNET_NO == silent))
586 solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, new_address);
590 GNUNET_assert (0 == ntohl(agent->address_inuse->assigned_bw_in.value__));
591 GNUNET_assert (0 == ntohl(agent->address_inuse->assigned_bw_out.value__));
595 solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, agent->address_inuse);
598 agent->address_inuse = new_address;
602 * Allocates a state vector and fills it with the features present
603 * @param solver the solver handle
604 * @return pointer to the state vector
607 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
611 struct RIL_Network *net;
612 double *state = GNUNET_malloc (sizeof (double) * agent->m);
613 struct RIL_Address_Wrapped *cur_address;
614 const double *properties;
616 for (i = 0; i < solver->networks_count; i++)
618 net = &solver->network_entries[i];
619 state[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) net->bw_in_assigned;
620 state[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net->bw_in_available;
621 state[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) net->bw_out_assigned;
622 state[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net->bw_out_available;
625 i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
627 for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
629 state[i++] = cur_address->address_naked->active;
630 state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
631 state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
632 properties = solver->callbacks->get_properties (solver->callbacks->get_properties_cls, cur_address->address_naked);
633 for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
635 state[i++] = properties[k];
643 * Gets the reward of the last performed step
644 * @param solver solver handle
648 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
650 //TODO! implement reward calculation
652 return (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX)
653 / (double) UINT32_MAX;
657 envi_action_bw_double (struct GAS_RIL_Handle *solver,
658 struct RIL_Peer_Agent *agent,
663 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2,
664 agent->bw_out, GNUNET_NO);
668 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
669 agent->bw_out * 2, GNUNET_NO);
674 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
675 struct RIL_Peer_Agent *agent,
678 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
679 unsigned long long new_bw;
683 new_bw = agent->bw_in / 2;
686 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out, GNUNET_NO);
690 new_bw = agent->bw_out / 2;
693 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw, GNUNET_NO);
698 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
700 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
704 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
705 agent->bw_out, GNUNET_NO);
709 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
710 agent->bw_out + (5 * min_bw), GNUNET_NO);
715 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
717 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
718 unsigned long long new_bw;
722 new_bw = agent->bw_in - (5 * min_bw);
725 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out, GNUNET_NO);
729 new_bw = agent->bw_out - (5 * min_bw);
732 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw, GNUNET_NO);
737 envi_action_address_switch (struct GAS_RIL_Handle *solver,
738 struct RIL_Peer_Agent *agent,
739 unsigned int address_index)
741 struct RIL_Address_Wrapped *cur;
744 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
746 if (i == address_index)
748 envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out, GNUNET_NO);
755 //no address with address_index exists
756 GNUNET_assert(GNUNET_NO);
760 * Puts the action into effect
761 * @param solver solver handle
762 * @param action action to perform by the solver
765 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
767 unsigned int address_index;
771 case RIL_ACTION_NOTHING:
773 case RIL_ACTION_BW_IN_DBL:
774 envi_action_bw_double (solver, agent, GNUNET_YES);
776 case RIL_ACTION_BW_IN_HLV:
777 envi_action_bw_halven (solver, agent, GNUNET_YES);
779 case RIL_ACTION_BW_IN_INC:
780 envi_action_bw_inc (solver, agent, GNUNET_YES);
782 case RIL_ACTION_BW_IN_DEC:
783 envi_action_bw_dec (solver, agent, GNUNET_YES);
785 case RIL_ACTION_BW_OUT_DBL:
786 envi_action_bw_double (solver, agent, GNUNET_NO);
788 case RIL_ACTION_BW_OUT_HLV:
789 envi_action_bw_halven (solver, agent, GNUNET_NO);
791 case RIL_ACTION_BW_OUT_INC:
792 envi_action_bw_inc (solver, agent, GNUNET_NO);
794 case RIL_ACTION_BW_OUT_DEC:
795 envi_action_bw_dec (solver, agent, GNUNET_NO);
798 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n))
800 address_index = agent->n - RIL_ACTION_TYPE_NUM;
802 GNUNET_assert(address_index >= 0);
804 address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
806 envi_action_address_switch (solver, agent, address_index);
809 // error - action does not exist
810 GNUNET_assert(GNUNET_NO);
815 * Performs one step of the Markov Decision Process. Other than in the literature the step starts
816 * after having done the last action a_old. It observes the new state s_next and the reward
817 * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
818 * next action is put into effect.
819 * @param agent the agent performing the step
822 agent_step (struct RIL_Peer_Agent *agent)
824 int a_next = RIL_ACTION_INVALID;
828 s_next = envi_get_state (agent->envi, agent);
829 reward = envi_get_reward (agent->envi, agent);
831 LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
832 agent->envi->parameters.algorithm ? "Q" : "SARSA");
834 switch (agent->envi->parameters.algorithm)
837 agent_modify_eligibility (agent, RIL_E_SET);
838 if (agent_decide_exploration (agent))
840 a_next = agent_get_action_explore (agent, s_next);
844 a_next = agent_get_action_best (agent, s_next);
846 if (RIL_ACTION_INVALID != agent->a_old)
848 //updates weights with selected action (on-policy), if not first step
849 agent_update_weights (agent, reward, s_next, a_next);
854 a_next = agent_get_action_best (agent, s_next);
855 if (RIL_ACTION_INVALID != agent->a_old)
857 //updates weights with best action, disregarding actually selected action (off-policy), if not first step
858 agent_update_weights (agent, reward, s_next, a_next);
860 if (agent_decide_exploration (agent))
862 a_next = agent_get_action_explore (agent, s_next);
863 agent_modify_eligibility (agent, RIL_E_ZERO);
867 a_next = agent_get_action_best (agent, s_next);
868 agent_modify_eligibility (agent, RIL_E_SET);
873 GNUNET_assert(RIL_ACTION_INVALID != a_next);
875 agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
877 envi_do_action (agent->envi, agent, a_next);
879 GNUNET_free(agent->s_old);
880 agent->s_old = s_next;
881 agent->a_old = a_next;
883 agent->step_count += 1;
887 * Cycles through all agents and lets the active ones do a step. Schedules the next step.
888 * @param solver the solver handle
889 * @param tc task context for the scheduler
892 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
894 struct GAS_RIL_Handle *solver = cls;
895 struct RIL_Peer_Agent *cur;
897 LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
899 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
901 if (cur->active && cur->address_inuse)
907 solver->step_count += 1;
908 solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, &ril_periodic_step, solver);
912 * Initialize an agent without addresses and its knowledge base
913 * @param s ril solver
914 * @param peer the one in question
915 * @return handle to the new agent
917 static struct RIL_Peer_Agent *
918 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
921 struct GAS_RIL_Handle * solver = s;
922 struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
924 agent->envi = solver;
926 agent->step_count = 0;
927 agent->active = GNUNET_NO;
928 agent->n = RIL_ACTION_TYPE_NUM;
929 agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
930 agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
931 for (i = 0; i < agent->n; i++)
933 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
935 agent->a_old = RIL_ACTION_INVALID;
936 agent->s_old = envi_get_state (solver, agent);
937 agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
938 agent_modify_eligibility (agent, RIL_E_ZERO);
940 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
947 * @param s solver handle
948 * @param agent the agent to retire
951 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
955 for (i = 0; i < agent->n; i++)
957 GNUNET_free(agent->W[i]);
959 GNUNET_free(agent->W);
960 GNUNET_free(agent->e);
961 GNUNET_free(agent->s_old);
966 * Returns the agent for a peer
967 * @param s solver handle
968 * @param peer identity of the peer
969 * @param create whether to create an agent if none is allocated yet
972 static struct RIL_Peer_Agent *
973 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
975 struct RIL_Peer_Agent *cur;
977 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
979 if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey))
987 return agent_init (solver, peer);
993 * Lookup network struct by type
995 * @param s the solver handle
996 * @param type the network type
997 * @return the network struct
999 static struct RIL_Network *
1000 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1004 for (i = 0; i < s->networks_count; i++)
1006 if (s->network_entries[i].type == type)
1008 return &s->network_entries[i];
1015 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1017 struct RIL_Network *net;
1018 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1020 net = ril_get_network (solver, network);
1021 if (net->bw_out_available < min_bw)
1027 ril_cut_from_vector (void **old,
1028 size_t element_size,
1029 unsigned int hole_start,
1030 unsigned int hole_length,
1031 unsigned int old_length)
1034 char *oldptr = (char *) *old;
1036 unsigned int bytes_before;
1037 unsigned int bytes_hole;
1038 unsigned int bytes_after;
1041 GNUNET_assert(old_length > hole_length);
1042 GNUNET_assert(old_length >= (hole_start + hole_length));
1044 size = element_size * (old_length - hole_length);
1046 bytes_before = element_size * hole_start;
1047 bytes_hole = element_size * hole_length;
1048 bytes_after = element_size * (old_length - hole_start - hole_length);
1056 // LOG(GNUNET_ERROR_TYPE_DEBUG, "hole_start = %d, hole_length = %d, old_length = %d\n", hole_start, hole_length, old_length);
1057 // LOG(GNUNET_ERROR_TYPE_DEBUG, "bytes_before = %d, bytes_hole = %d, bytes_after = %d\n", bytes_before, bytes_hole, bytes_after);
1058 // LOG(GNUNET_ERROR_TYPE_DEBUG, "element_size = %d, bytes_old = %d, bytes_new = %d\n", element_size, old_length * element_size, size);
1060 tmpptr = GNUNET_malloc (size);
1061 // LOG(GNUNET_ERROR_TYPE_DEBUG, "first\n");
1062 memcpy (tmpptr, oldptr, bytes_before);
1063 // LOG(GNUNET_ERROR_TYPE_DEBUG, "second\n");
1064 memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1070 *old = (void *) tmpptr;
1074 * Solver API functions
1075 * ---------------------------
1079 * Changes the preferences for a peer in the problem
1081 * @param solver the solver handle
1082 * @param peer the peer to change the preference for
1083 * @param kind the kind to change the preference
1084 * @param pref_rel the normalized preference value for this kind over all clients
1087 GAS_ril_address_change_preference (void *s,
1088 const struct GNUNET_PeerIdentity *peer,
1089 enum GNUNET_ATS_PreferenceKind kind,
1092 LOG(GNUNET_ERROR_TYPE_DEBUG,
1093 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1094 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1096 * Nothing to do here. Preferences are considered during reward calculation.
1101 * Init the reinforcement learning problem solver
1104 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1105 * out_quota[i] contains outbound quota for network type i
1106 * in_quota[i] contains inbound quota for network type i
1109 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1110 * network[2] == GNUNET_ATS_NET_LAN
1111 * out_quota[2] == 65353
1112 * in_quota[2] == 65353
1114 * @param cfg configuration handle
1115 * @param stats the GNUNET_STATISTICS handle
1116 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1117 * @param addresses hashmap containing all addresses
1118 * @param out_quota array of outbound quotas
1119 * @param in_quota array of outbound quota
1120 * @param dest_length array length for quota arrays
1121 * @param bw_changed_cb callback for changed bandwidth amounts
1122 * @param bw_changed_cb_cls cls for callback
1123 * @param get_preference callback to get relative preferences for a peer
1124 * @param get_preference_cls cls for callback to get relative preferences
1125 * @param get_properties_cls for callback to get relative properties
1126 * @param get_properties_cls cls for callback to get relative properties
1127 * @return handle for the solver on success, NULL on fail
1130 GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1131 const struct GNUNET_STATISTICS_Handle *stats,
1132 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1134 unsigned long long *out_quota,
1135 unsigned long long *in_quota,
1137 GAS_bandwidth_changed_cb bw_changed_cb,
1138 void *bw_changed_cb_cls,
1139 GAS_get_preferences get_preference,
1140 void *get_preference_cls,
1141 GAS_get_properties get_properties,
1142 void *get_properties_cls)
1145 unsigned long long tmp;
1147 struct RIL_Network * cur;
1148 struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle));
1150 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1152 GNUNET_assert(NULL != cfg);
1153 GNUNET_assert(NULL != stats);
1154 GNUNET_assert(NULL != network);
1155 GNUNET_assert(NULL != bw_changed_cb);
1156 GNUNET_assert(NULL != get_preference);
1157 GNUNET_assert(NULL != get_properties);
1160 != GNUNET_CONFIGURATION_get_value_time (cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
1162 solver->step_time = RIL_DEFAULT_STEP_TIME;
1164 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "RIL_ALGORITHM", &string)
1165 && NULL != string && 0 == strcmp (string, "SARSA"))
1167 solver->parameters.algorithm = RIL_ALGO_SARSA;
1171 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1173 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
1175 solver->parameters.gamma = (double) tmp / 100;
1179 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
1181 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
1183 solver->parameters.alpha = (double) tmp / 100;
1187 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1189 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_TRACE_DECAY", &tmp))
1191 solver->parameters.lambda = (double) tmp / 100;
1195 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1198 solver->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1199 solver->callbacks = GNUNET_malloc (sizeof (struct RIL_Callbacks));
1200 solver->callbacks->bw_changed = bw_changed_cb;
1201 solver->callbacks->bw_changed_cls = bw_changed_cb_cls;
1202 solver->callbacks->get_preferences = get_preference;
1203 solver->callbacks->get_preferences_cls = get_preference_cls;
1204 solver->callbacks->get_properties = get_properties;
1205 solver->callbacks->get_properties_cls = get_properties_cls;
1206 solver->networks_count = dest_length;
1207 solver->network_entries = GNUNET_malloc (dest_length * sizeof (struct RIL_Network));
1208 solver->bulk_lock = GNUNET_NO;
1209 solver->addresses = addresses;
1210 solver->step_count = 0;
1212 for (c = 0; c < dest_length; c++)
1214 cur = &solver->network_entries[c];
1215 cur->type = network[c];
1216 cur->bw_in_available = in_quota[c];
1217 cur->bw_in_assigned = 0;
1218 cur->bw_out_available = out_quota[c];
1219 cur->bw_out_assigned = 0;
1222 solver->next_step = GNUNET_SCHEDULER_add_delayed (
1223 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1224 &ril_periodic_step, solver);
1230 * Shutdown the reinforcement learning problem solver
1232 * @param solver the respective handle to shutdown
1235 GAS_ril_done (void * solver)
1237 struct GAS_RIL_Handle *s = solver;
1238 struct RIL_Peer_Agent *cur_agent;
1239 struct RIL_Peer_Agent *next_agent;
1241 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1243 cur_agent = s->agents_head;
1244 while (NULL != cur_agent)
1246 next_agent = cur_agent->next;
1247 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1248 agent_die (s, cur_agent);
1249 cur_agent = next_agent;
1252 GNUNET_SCHEDULER_cancel (s->next_step);
1253 GNUNET_free(s->callbacks);
1254 GNUNET_free(s->network_entries);
1259 * Add a single address within a network to the solver
1261 * @param solver the solver Handle
1262 * @param address the address to add
1263 * @param network network type of this address
1266 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1268 struct GAS_RIL_Handle *s = solver;
1269 struct RIL_Peer_Agent *agent;
1270 struct RIL_Address_Wrapped *address_wrapped;
1277 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1279 address->solver_information = ril_get_network (s, network);
1281 if (!ril_network_is_active (s, network))
1283 LOG(GNUNET_ERROR_TYPE_DEBUG,
1284 "API_address_add() Did not add %s address %p for peer '%s', network does not have enough bandwidth\n",
1285 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1289 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1292 address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1293 address_wrapped->address_naked = address;
1294 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1296 //increase size of W
1297 m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
1299 n_new = agent->n + 1;
1302 GNUNET_array_grow(agent->W, agent->n, n_new);
1303 for (i = 0; i < n_new; i++)
1308 GNUNET_array_grow(agent->W[i], agent->m, m_new);
1313 GNUNET_array_grow(agent->W[i], zero, m_new);
1317 //increase size of old state vector
1319 GNUNET_array_grow(agent->s_old, agent->m, m_new); //TODO initialize new state features?
1322 GNUNET_array_grow(agent->e, agent->m, m_new);
1324 if (NULL == agent->address_inuse)
1326 envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
1329 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %p for peer '%s'\n",
1330 address->active ? "active" : "inactive",
1331 address->plugin, address->addr, GNUNET_i2s (&address->peer));
1335 * Remove an address from the solver
1337 * @param solver the solver handle
1338 * @param address the address to remove
1339 * @param session_only delete only session not whole address
1342 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1344 //TODO! delete session only
1345 struct GAS_RIL_Handle *s = solver;
1346 struct RIL_Peer_Agent *agent;
1347 struct RIL_Address_Wrapped *address_wrapped;
1348 int address_was_used = address->active;
1353 struct RIL_Network *net;
1354 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1356 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
1357 session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
1358 address->addr, GNUNET_i2s (&address->peer));
1360 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1363 net = address->solver_information;
1364 GNUNET_assert(!ril_network_is_active (s, net->type));
1365 LOG(GNUNET_ERROR_TYPE_DEBUG,
1366 "No agent allocated for peer yet, since address was in inactive network\n");
1370 address_index = agent_address_get_index (agent, address);
1371 address_wrapped = agent_address_get (agent, address);
1373 if (NULL == address_wrapped)
1375 net = address->solver_information;
1376 GNUNET_assert(!ril_network_is_active (s, net->type));
1377 LOG(GNUNET_ERROR_TYPE_DEBUG,
1378 "Address not considered by agent, address was in inactive network\n");
1382 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1383 GNUNET_free(address_wrapped);
1386 m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
1387 n_new = agent->n - 1;
1389 for (i = 0; i < agent->n; i++)
1391 // LOG (GNUNET_ERROR_TYPE_DEBUG, "first - cut vectors in W\n");
1392 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
1393 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT) + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1395 // LOG (GNUNET_ERROR_TYPE_DEBUG, "second - cut action vector out of W\n");
1396 GNUNET_free (agent->W[RIL_ACTION_TYPE_NUM + address_index]);
1397 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
1399 //correct last action
1400 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1404 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1406 agent->a_old = RIL_ACTION_INVALID;
1408 //decrease old state vector and eligibility vector
1409 // LOG (GNUNET_ERROR_TYPE_DEBUG, "third - cut state vector\n");
1410 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
1411 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT) + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1412 // LOG (GNUNET_ERROR_TYPE_DEBUG, "fourth - cut eligibility vector\n");
1413 ril_cut_from_vector ((void **) &agent->e, sizeof(double),
1414 ((s->networks_count * RIL_FEATURES_NETWORK_COUNT) + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1418 LOG (GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
1420 if (address_was_used)
1422 net = address->solver_information;
1423 net->bw_in_assigned -= agent->bw_in;
1424 net->bw_out_assigned -= agent->bw_out;
1426 if (NULL != agent->addresses_head) //if peer has an address left, use it
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, "address left: %p\n", agent->addresses_head->address_naked->addr);
1429 //TODO? check if network/bandwidth update can be done more clever/elegant at different function
1430 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw, GNUNET_NO);
1431 net = agent->addresses_head->address_naked->solver_information;
1432 net->bw_in_assigned -= min_bw;
1433 net->bw_out_assigned -= min_bw;
1437 LOG (GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
1439 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
1443 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
1447 * Transport properties for this address have changed
1449 * @param solver solver handle
1450 * @param address the address
1451 * @param type the ATSI type in HBO
1452 * @param abs_value the absolute value of the property
1453 * @param rel_value the normalized value
1456 GAS_ril_address_property_changed (void *solver,
1457 struct ATS_Address *address,
1462 LOG(GNUNET_ERROR_TYPE_DEBUG,
1463 "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1464 "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1465 address->addr, rel_value);
1467 * Nothing to do here, properties are considered in every reward calculation
1472 * Transport session for this address has changed
1474 * NOTE: values in addresses are already updated
1476 * @param solver solver handle
1477 * @param address the address
1478 * @param cur_session the current session
1479 * @param new_session the new session
1482 GAS_ril_address_session_changed (void *solver,
1483 struct ATS_Address *address,
1484 uint32_t cur_session,
1485 uint32_t new_session)
1487 //TODO? consider session changed in solver behaviour
1489 * Potentially add session activity as a feature in state vector
1491 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1495 * Usage for this address has changed
1497 * NOTE: values in addresses are already updated
1499 * @param solver solver handle
1500 * @param address the address
1501 * @param in_use usage state
1504 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1506 //TODO! consider address_inuse_changed according to matthias' email
1508 * See matthias' email
1510 LOG(GNUNET_ERROR_TYPE_DEBUG,
1511 "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1512 address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1516 * Network scope for this address has changed
1518 * NOTE: values in addresses are already updated
1520 * @param solver solver handle
1521 * @param address the address
1522 * @param current_network the current network
1523 * @param new_network the new network
1526 GAS_ril_address_change_network (void *solver,
1527 struct ATS_Address *address,
1528 uint32_t current_network,
1529 uint32_t new_network)
1531 struct GAS_RIL_Handle *s = solver;
1532 struct RIL_Peer_Agent *agent;
1533 struct RIL_Network *net;
1534 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1536 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
1537 "%s address of peer %s from '%s' to '%s'\n",
1538 (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
1539 GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
1541 if (address->active && !ril_network_is_active (solver, new_network))
1543 GAS_ril_address_delete (solver, address, GNUNET_NO);
1547 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1550 //no agent there yet, so add as if address is new
1551 address->solver_information = ril_get_network (s, new_network);
1552 GAS_ril_address_add (s, address, new_network);
1556 net = ril_get_network (s, current_network);
1557 net->bw_in_assigned -= agent->bw_in;
1558 net->bw_out_assigned -= agent->bw_out;
1560 net = ril_get_network (s, new_network);
1561 net->bw_in_assigned -= min_bw;
1562 net->bw_out_assigned -= min_bw;
1563 address->solver_information = net;
1567 * Get application feedback for a peer
1569 * @param solver the solver handle
1570 * @param application the application
1571 * @param peer the peer to change the preference for
1572 * @param scope the time interval for this feedback: [now - scope .. now]
1573 * @param kind the kind to change the preference
1574 * @param score the score
1577 GAS_ril_address_preference_feedback (void *solver,
1579 const struct GNUNET_PeerIdentity *peer,
1580 const struct GNUNET_TIME_Relative scope,
1581 enum GNUNET_ATS_PreferenceKind kind,
1584 //TODO! collect reward until next reward calculation
1585 LOG(GNUNET_ERROR_TYPE_DEBUG,
1586 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1587 "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1588 GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1592 * Start a bulk operation
1594 * @param solver the solver
1597 GAS_ril_bulk_start (void *solver)
1599 //TODO? consideration: keep bulk counter and stop agents during bulk
1601 * bulk counter up, but not really relevant, because there is no complete calculation of the
1602 * bandwidth assignment triggered anyway. Therefore, changes to addresses can come and go as
1603 * they want. Consideration: Step-pause during bulk-start-stop period...
1606 //LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n");
1610 * Bulk operation done
1613 GAS_ril_bulk_stop (void *solver)
1615 //TODO? consideration: keep bulk counter and stop agents during bulk
1617 * bulk counter down, see bulk_start()
1620 //LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n");
1624 * Get the preferred address for a specific peer
1626 * @param solver the solver handle
1627 * @param peer the identity of the peer
1629 const struct ATS_Address *
1630 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1633 * activate agent, return currently chosen address
1635 struct GAS_RIL_Handle *s = solver;
1636 struct RIL_Peer_Agent *agent;
1638 agent = ril_get_agent (s, peer, GNUNET_YES);
1640 agent->active = GNUNET_YES;
1642 envi_set_active_suggestion(s, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
1644 if (agent->address_inuse)
1646 LOG(GNUNET_ERROR_TYPE_DEBUG,
1647 "API_get_preferred_address() Activated agent for peer '%s' with %s address %p\n",
1648 GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
1652 LOG(GNUNET_ERROR_TYPE_DEBUG,
1653 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n", GNUNET_i2s (peer));
1656 return agent->address_inuse;
1660 * Stop notifying about address and bandwidth changes for this peer
1662 * @param solver the solver handle
1663 * @param peer the peer
1666 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1668 struct GAS_RIL_Handle *s = solver;
1669 struct RIL_Peer_Agent *agent;
1671 agent = ril_get_agent (s, peer, GNUNET_NO);
1678 if (GNUNET_NO == agent->active)
1684 agent->active = GNUNET_NO;
1685 envi_set_active_suggestion(s, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
1687 LOG(GNUNET_ERROR_TYPE_DEBUG,
1688 "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1689 GNUNET_i2s (peer), agent->address_inuse->plugin);
1692 /* end of gnunet-service-ats-solver_ril.c */