- bulk deletion for all addresses
[oweals/gnunet.git] / src / ats / gnunet-service-ats-solver_ril.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file ats/gnunet-service-ats-solver_ril.c
23  * @brief ATS reinforcement learning solver
24  * @author Fabian Oehlmann
25  * @author Matthias Wachs
26  */
27 #include "platform.h"
28 #include "float.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet_statistics_service.h"
32
33 #define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
34 #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
35 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
36 #define RIL_DEFAULT_TRACE_DECAY 0.6
37 #define RIL_EXPLORE_RATIO 0.1
38
39 /**
40  * ATS reinforcement learning solver
41  *
42  * General description
43  */
44
45 enum RIL_Action
46 {
47         RIL_BW_DBL = 0,
48         RIL_BW_HLV = 1,
49         RIL_NUM_ACTIONS = 2
50 };
51 //TODO add the rest of the actions
52
53 enum RIL_Algorithm
54 {
55         RIL_ALGO_SARSA,
56         RIL_ALGO_Q
57 };
58
59 enum RIL_E_Modification
60 {
61         RIL_E_SET,
62         RIL_E_ZERO,
63         RIL_E_ACCUMULATE,
64         RIL_E_REPLACE
65 };
66
67 /**
68  * Global learning parameters
69  */
70 struct RIL_Learning_Parameters
71 {
72         /**
73          * The TD-algorithm to use
74          */
75         enum RIL_Algorithm algorithm;
76
77         /**
78          * Learning discount factor in the TD-update
79          */
80         float gamma;
81
82         /**
83          * Gradient-descent step-size
84          */
85         float alpha;
86
87         /**
88          * Trace-decay factor for eligibility traces
89          */
90         float lambda;
91 };
92
93 struct RIL_Peer_Agent
94 {
95         /**
96          * Next agent in solver's linked list
97          */
98         struct RIL_Peer_Agent *next;
99
100         /**
101          * Previous agent in solver's linked list
102          */
103         struct RIL_Peer_Agent *prev;
104
105         /**
106          * Environment handle
107          */
108         struct GAS_RIL_Handle *envi;
109
110         /**
111          * Peer ID
112          */
113         struct GNUNET_PeerIdentity peer;
114
115         /**
116          * Whether the agent is active or not
117          */
118         int active;
119
120         /**
121         * Number of performed time-steps
122         */
123         unsigned long long step_count;
124
125         /**
126          * Experience matrix W
127          */
128         double ** W;
129
130         /**
131          * Number of rows of W / Number of state-vector features
132          */
133         int m;
134
135         /**
136          * Number of columns of W / Number of actions
137          */
138         int n;
139
140         /**
141          * Last perceived state feature vector
142          */
143         double * s_old;
144
145         /**
146          * Last chosen action
147          */
148         int a_old;
149
150         /**
151          * Eligibility trace vector
152          */
153         double * e;
154
155         /**
156          * Address in use
157          */
158         struct ATS_Address * address;
159 };
160
161 struct RIL_Network
162 {
163           /**
164            * ATS network type
165            */
166           unsigned int type;
167
168           /**
169            * Network description
170            */
171           char *desc;
172
173           /**
174            * Total available inbound bandwidth
175            */
176           unsigned long long bw_in_available;
177
178           /**
179            * Total assigned outbound bandwidth
180            */
181           unsigned long long bw_in_assigned;
182
183           /**
184            * Total available outbound bandwidth
185            */
186           unsigned long long bw_out_available;
187
188           /**
189            * Total assigned outbound bandwidth
190            */
191           unsigned long long bw_out_assigned;
192 };
193
194 struct RIL_Callbacks
195 {
196           /**
197            * Bandwidth changed callback
198            */
199           GAS_bandwidth_changed_cb bw_changed;
200
201           /**
202            * Bandwidth changed callback cls
203            */
204           void *bw_changed_cls;
205
206           /**
207            * ATS function to get preferences
208            */
209           GAS_get_preferences get_preferences;
210
211           /**
212            * Closure for ATS function to get preferences
213            */
214           void *get_preferences_cls;
215
216           /**
217            * ATS function to get properties
218            */
219           GAS_get_properties get_properties;
220
221           /**
222            * Closure for ATS function to get properties
223            */
224           void *get_properties_cls;
225 };
226
227 /**
228  * A handle for the reinforcement learning solver
229  */
230 struct GAS_RIL_Handle
231 {
232         /**
233         * Statistics handle
234         */
235         struct GNUNET_STATISTICS_Handle *stats;
236
237         /**
238         * Hashmap containing all valid addresses
239         */
240         const struct GNUNET_CONTAINER_MultiHashMap *addresses;
241
242         /**
243         * Callbacks for the solver
244         */
245         struct RIL_Callbacks callbacks;
246
247         /**
248         * Bulk lock
249         */
250         int bulk_lock;
251
252         /**
253         * Number of changes while solver was locked
254         */
255         int bulk_requests;
256
257         /**
258         * Number of performed time-steps
259         */
260         unsigned long long step_count;
261
262         /**
263         * Interval time between steps in milliseconds //TODO put in agent
264         */
265         struct GNUNET_TIME_Relative step_time;
266
267         /**
268         * Task identifier of the next time-step to be executed //TODO put in agent
269         */
270         GNUNET_SCHEDULER_TaskIdentifier next_step;
271
272         /**
273         * Learning parameters
274         */
275         struct RIL_Learning_Parameters parameters;
276
277         /**
278         * Array of networks with global assignment state
279         */
280         struct RIL_Network * network_entries;
281
282         /**
283         * Networks count
284         */
285         unsigned int networks_count;
286
287         /**
288         * List of active peer-agents
289         */
290         struct RIL_Peer_Agent * agents_head;
291         struct RIL_Peer_Agent * agents_tail;
292 };
293
294
295
296
297 /**
298  *  Private functions
299  *  ---------------------------
300  */
301
302 /**
303  * Estimate the current action-value for state s and action a
304  * @param agent agent performing the estimation
305  * @param state s
306  * @param action a
307  * @return estimation value
308  */
309 double
310 agent_estimate_q (struct RIL_Peer_Agent *agent,
311                 double *state,
312                 int action)
313 {
314         int i;
315         double result = 0;
316
317         for (i = 0; i < agent->m; i++)
318         {
319                 result += state[i] * (agent->W)[agent->m][action];
320         }
321
322         return result;
323 }
324
325 /**
326  * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
327  * currently estimated best action) in the current step
328  * @param agent agent performing the step
329  * @return yes, if exploring
330  */
331 int
332 agent_decide_exploration (struct RIL_Peer_Agent *agent)
333 {
334         double r = (double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX) / (double) UINT32_MAX;
335
336         if (r < RIL_EXPLORE_RATIO)
337         {
338                 return GNUNET_YES;
339         }
340         return GNUNET_NO;
341 }
342
343 /**
344  * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
345  * most reward in the future)
346  * @param agent agent performing the calculation
347  * @param state the state from which to take the action
348  * @return the action promising most future reward
349  */
350 int
351 agent_get_action_best (struct RIL_Peer_Agent *agent,
352                 double *state)
353 {
354         int i;
355         int max_i = -1;
356         double cur_q;
357         double max_q = DBL_MIN;
358
359         for (i = 0; i < agent->m; i++)
360         {
361                 cur_q = agent_estimate_q (agent, state, i);
362                 if (cur_q > max_q)
363                 {
364                         max_q = cur_q;
365                         max_i = i;
366                 }
367         }
368
369         GNUNET_assert(-1 != max_i);
370
371         return max_i;
372 }
373
374 /**
375  * Gets any action, to explore the action space from that state
376  * @param agent agent performing the calculation
377  * @param state the state from which to take the action
378  * @return any action
379  */
380 int
381 agent_get_action_explore (struct RIL_Peer_Agent *agent,
382                 double *state)
383 {
384         return GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
385 }
386
387 /**
388  * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
389  * @param agent the agent performing the update
390  * @param reward the reward received for the last action
391  * @param s_next the new state, the last step got the agent into
392  * @param a_prime the new
393  */
394 void
395 agent_update_weights (struct RIL_Peer_Agent *agent,
396                 double reward,
397                 double *s_next,
398                 int a_prime)
399 {
400         int i;
401         double delta;
402         double *theta = (agent->W)[agent->a_old];
403
404         delta = reward + agent_estimate_q (agent, s_next, a_prime) -
405                         agent_estimate_q (agent, agent->s_old, agent->a_old);
406         for (i = 0; i < agent->m; i++)
407         {
408                 theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
409         }
410 }
411
412 /**
413  * Changes the eligibility trace vector e in various manners:
414  * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
415  * RIL_E_REPLACE - resets each component to 1 as in replacing traces
416  * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
417  * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
418  * @param agent
419  * @param mod
420  */
421 void
422 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
423                 enum RIL_E_Modification mod)
424 {
425         int i;
426         double *e = agent->e;
427         double gamma = agent->envi->parameters.gamma;
428         double lambda = agent->envi->parameters.lambda;
429
430         for (i = 0; i < agent->m; i++)
431         {
432                 switch (mod)
433                 {
434                         case RIL_E_ACCUMULATE:
435                                 e[i] += 1;
436                                 break;
437                         case RIL_E_REPLACE:
438                                 e[i] = 1;
439                                 break;
440                         case RIL_E_SET:
441                                 e[i] = gamma * lambda;
442                                 break;
443                         case RIL_E_ZERO:
444                                 e[i] = 0;
445                                 break;
446                 }
447         }
448 }
449
450 /**
451  * Allocates a state vector and fills it with the features present
452  * @param solver the solver handle
453  * @return pointer to the state vector
454  */
455 double *
456 envi_get_state (struct GAS_RIL_Handle *solver)
457 {
458         int i;
459         struct RIL_Network *net;
460         double *state = GNUNET_malloc (sizeof (double) * solver->networks_count * 4);
461
462         for (i = 0; i < solver->networks_count; i += 4)
463         {
464                 net = (&solver->network_entries)[i];
465                 state[i]   = (double) net->bw_in_assigned;
466                 state[i+1] = (double) net->bw_in_available;
467                 state[i+2] = (double) net->bw_out_assigned;
468                 state[i+3] = (double) net->bw_out_available;
469         }
470
471         return state;
472 }
473
474 /**
475  * Gets the reward of the last performed step
476  * @param solver solver handle
477  * @return the reward
478  */
479 double
480 envi_get_reward (struct GAS_RIL_Handle *solver)
481 {
482         //TODO implement
483         return (double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX) / (double) UINT32_MAX;
484 }
485
486 /**
487  * Puts the action into effect
488  * @param solver solver handle
489  * @param action action to perform by the solver
490  */
491 void
492 envi_do_action (struct GAS_RIL_Handle *solver,
493                 int action)
494 {
495
496 }
497
498 /**
499  * Performs one step of the Markov Decision Process. Other than in the literature the step starts
500  * after having done the last action a_old. It observes the new state s_next and the reward
501  * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
502  * next action is put into effect.
503  * @param agent the agent performing the step
504  */
505 void
506 agent_step (struct RIL_Peer_Agent *agent)
507 {
508         int a_next = -1;
509         double *s_next;
510         double reward;
511
512         s_next = envi_get_state(agent->envi);
513         reward = envi_get_reward(agent->envi);
514
515         switch (agent->envi->parameters.algorithm)
516         {
517                 case RIL_ALGO_SARSA:
518                         agent_modify_eligibility (agent, RIL_E_SET);
519                         if (agent_decide_exploration (agent))
520                         {
521                                 a_next = agent_get_action_explore (agent, s_next);
522                         }
523                         else
524                         {
525                                 a_next = agent_get_action_best (agent, s_next);
526                         }
527                         agent_update_weights (agent, reward, s_next, a_next); //update weights with next action
528                         break;
529
530                 case RIL_ALGO_Q:
531                         a_next = agent_get_action_best (agent, s_next); //update weights with best action
532                         agent_update_weights (agent, reward, s_next, a_next);
533                         if (agent_decide_exploration (agent))
534                         {
535                                 a_next = agent_get_action_explore (agent, s_next);
536                                 agent_modify_eligibility(agent, RIL_E_ZERO);
537                         }
538                         else
539                         {
540                                 a_next = agent_get_action_best (agent, s_next);
541                                 agent_modify_eligibility(agent, RIL_E_SET);
542                         }
543                         break;
544         }
545
546         GNUNET_assert (-1 != a_next);
547
548         agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
549
550         envi_do_action(agent->envi, a_next);
551
552         GNUNET_free(agent->s_old);
553         agent->s_old = s_next;
554         agent->a_old = a_next;
555
556         agent->step_count += 1;
557 }
558
559 /**
560  * Cycles through all agents and lets the active ones do a step. Schedules the next step.
561  * @param solver the solver handle
562  * @param tc task context for the scheduler
563  */
564 void
565 ril_periodic_step (void *cls,
566                                 const struct GNUNET_SCHEDULER_TaskContext *tc)
567 {
568         struct GAS_RIL_Handle *solver = cls;
569         struct RIL_Peer_Agent *cur;
570
571         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
572
573         for (cur = solver->agents_head; NULL != cur; cur = cur->next)
574         {
575                 if (cur->active)
576                 {
577                         agent_step (cur);
578                 }
579         }
580
581         solver->step_count += 1;
582         solver->next_step = GNUNET_SCHEDULER_add_delayed (
583                         solver->step_time,
584                         &ril_periodic_step,
585                         solver);
586 }
587
588 /**
589  * Initialize an agent without addresses and its knowledge base
590  * @param s ril solver
591  * @param peer the one in question
592  * @return handle to the new agent
593  */
594 struct RIL_Peer_Agent *
595 agent_init (void *s,
596                 const struct GNUNET_PeerIdentity *peer)
597 {
598         int i;
599         struct GAS_RIL_Handle * solver = s;
600         struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
601
602         agent->envi = solver;
603         agent->peer = *peer;
604         agent->step_count = 0;
605         agent->active = GNUNET_NO;
606         agent->s_old = NULL;
607         agent->n = solver->networks_count * 4;
608         agent->m = RIL_NUM_ACTIONS;
609         agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
610         for (i = 0; i < agent->n; i++)
611         {
612                 (agent->W)[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
613         }
614         agent->a_old = -1;
615         agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
616         agent_modify_eligibility (agent, RIL_E_ZERO);
617
618         GNUNET_CONTAINER_DLL_insert (solver->agents_head, solver->agents_tail, agent);
619
620         return agent;
621 }
622
623 /**
624  * Deallocate agent
625  * @param s solver handle
626  * @param agent the agent to retire
627  */
628 void
629 agent_die (struct GAS_RIL_Handle *solver,
630                 struct RIL_Peer_Agent *agent)
631 {
632         //TODO implement
633 }
634
635 /**
636  * Returns the agent for a peer
637  * @param s solver handle
638  * @param peer identity of the peer
639  * @return agent
640  */
641 struct RIL_Peer_Agent *
642 ril_get_agent (struct GAS_RIL_Handle *solver,
643                 const struct GNUNET_PeerIdentity *peer)
644 {
645         struct RIL_Peer_Agent *cur;
646
647         for (cur = solver->agents_head; NULL != cur; cur = cur->next)
648         {
649                 if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey))
650                 {
651                         return cur;
652                 }
653         }
654
655         return agent_init (solver, peer);
656 }
657
658 /**
659  * Iterator, which allocates one agent per peer
660  *
661  * @param cls solver
662  * @param key peer identity
663  * @param value address
664  * @return whether iterator should continue
665  */
666 int
667 init_agents_it (void *cls,
668                                 const struct GNUNET_HashCode *key,
669                                 void *value)
670 {
671         struct GAS_RIL_Handle *solver = cls;
672         struct ATS_Address *address = value;
673         struct RIL_Peer_Agent *agent;
674
675         agent = ril_get_agent (solver, &address->peer);
676
677         GNUNET_assert (agent != NULL);
678
679         if (NULL == agent->address)
680         {
681                 agent->address = address;
682         }
683
684         return GNUNET_YES;
685 }
686
687
688
689 /**
690  *  Solver API functions
691  *  ---------------------------
692  */
693
694 /**
695  * Changes the preferences for a peer in the problem
696  *
697  * @param solver the solver handle
698  * @param peer the peer to change the preference for
699  * @param kind the kind to change the preference
700  * @param pref_rel the normalized preference value for this kind over all clients
701  */
702 void
703 GAS_ril_address_change_preference (void *s,
704                 const struct GNUNET_PeerIdentity *peer,
705                 enum GNUNET_ATS_PreferenceKind kind,
706                 double pref_rel)
707 {
708           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709                       "Preference `%s' for peer `%s' changed to %.2f \n",
710                       GNUNET_ATS_print_preference_type (kind),
711                       GNUNET_i2s (peer),
712                       pref_rel);
713           /*
714            * Nothing to do here. Preferences are considered during reward calculation.
715            */
716 }
717
718
719 /**
720  * Init the reinforcement learning problem solver
721  *
722  * Quotas:
723  * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
724  * out_quota[i] contains outbound quota for network type i
725  * in_quota[i] contains inbound quota for network type i
726  *
727  * Example
728  * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
729  * network[2]   == GNUNET_ATS_NET_LAN
730  * out_quota[2] == 65353
731  * in_quota[2]  == 65353
732  *
733  * @param cfg configuration handle
734  * @param stats the GNUNET_STATISTICS handle
735  * @param network array of GNUNET_ATS_NetworkType with length dest_length
736  * @param addresses hashmap containing all addresses
737  * @param out_quota array of outbound quotas
738  * @param in_quota array of outbound quota
739  * @param dest_length array length for quota arrays
740  * @param bw_changed_cb callback for changed bandwidth amounts
741  * @param bw_changed_cb_cls cls for callback
742  * @param get_preference callback to get relative preferences for a peer
743  * @param get_preference_cls cls for callback to get relative preferences
744  * @param get_properties_cls for callback to get relative properties
745  * @param get_properties_cls cls for callback to get relative properties
746  * @return handle for the solver on success, NULL on fail
747  */
748 void *
749 GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
750                 const struct GNUNET_STATISTICS_Handle *stats,
751                 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
752                 int *network,
753                 unsigned long long *out_quota,
754                 unsigned long long *in_quota,
755                 int dest_length,
756                 GAS_bandwidth_changed_cb bw_changed_cb,
757                 void *bw_changed_cb_cls,
758                 GAS_get_preferences get_preference,
759                 void *get_preference_cls,
760                 GAS_get_properties get_properties,
761                 void *get_properties_cls)
762 {
763         //TODO implement
764         int c;
765         unsigned long long tmp;
766         struct RIL_Network * cur;
767         struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle));
768         char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
769
770         GNUNET_assert (NULL != cfg);
771         GNUNET_assert (NULL != stats);
772         GNUNET_assert (NULL != network);
773         GNUNET_assert (NULL != bw_changed_cb);
774         GNUNET_assert (NULL != get_preference);
775         GNUNET_assert (NULL != get_properties);
776
777         if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
778         {
779                 solver->step_time = RIL_DEFAULT_STEP_TIME;
780         }
781         if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
782         {
783                 solver->parameters.gamma = (double) tmp / 100;;
784         }
785         else
786         {
787                 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
788         }
789         if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
790         {
791                 solver->parameters.alpha = (double) tmp / 100;;
792         }
793         else
794         {
795                 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
796         }
797         if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "RIL_TRACE_DECAY", &tmp))
798         {
799                 solver->parameters.lambda = (double) tmp / 100;;
800         }
801         else
802         {
803                 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
804         }
805
806         solver->stats = (struct GNUNET_STATISTICS_Handle *) stats;
807         solver->callbacks.bw_changed = bw_changed_cb;
808         solver->callbacks.bw_changed_cls = bw_changed_cb_cls;
809         solver->callbacks.get_preferences = get_preference;
810         solver->callbacks.get_preferences_cls = get_preference_cls;
811         solver->callbacks.get_properties = get_properties;
812         solver->callbacks.get_properties_cls = get_properties_cls;
813         solver->networks_count = dest_length;
814         solver->network_entries = GNUNET_malloc (dest_length * sizeof (struct RIL_Network));
815         solver->bulk_lock = GNUNET_NO;
816         solver->addresses = addresses;
817         solver->step_count = 0;
818
819         for (c = 0; c < dest_length; c++)
820         {
821                 cur = &solver->network_entries[c];
822                 cur->type = network[c];
823                 cur->bw_in_available = in_quota[c];
824                 cur->bw_in_assigned = 0;
825                 cur->bw_out_available = out_quota[c];
826                 cur->bw_out_assigned = 0;
827                 cur->desc = net_str[c];
828         }
829
830         c = GNUNET_CONTAINER_multihashmap_iterate (addresses, &init_agents_it, solver);
831
832         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_init() has been called\n");
833         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RIL number of addresses: %d\n", c);
834
835         solver->next_step = GNUNET_SCHEDULER_add_delayed (
836                                 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
837                                 &ril_periodic_step,
838                                 solver);
839
840         return solver;
841 }
842
843 /**
844  * Shutdown the reinforcement learning problem solver
845  *
846  * @param solver the respective handle to shutdown
847  */
848 void
849 GAS_ril_done (void * solver)
850 {
851         //TODO implement
852         /*
853          * dealloc: agents, learning parameters, callbacks
854          */
855         struct GAS_RIL_Handle *s = solver;
856
857         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_done() has been called\n");
858
859         GNUNET_SCHEDULER_cancel (s->next_step);
860         GNUNET_free (s->network_entries);
861         GNUNET_free (s);
862 }
863
864
865 /**
866  * Add a single address within a network to the solver
867  *
868  * @param solver the solver Handle
869  * @param address the address to add
870  * @param network network type of this address
871  */
872 void
873 GAS_ril_address_add (void *solver,
874                 struct ATS_Address *address,
875                 uint32_t network)
876 {
877         //TODO implement
878         /*
879          * if (new peer)
880          *     initialize new agent
881          * Add address
882          * increase state vector
883          * knowledge matrix
884          * and action vector
885          */
886         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_address_add() has been called\n");
887 }
888
889 /**
890  * Remove an address from the solver
891  *
892  * @param solver the solver handle
893  * @param address the address to remove
894  * @param session_only delete only session not whole address
895  */
896 void
897 GAS_ril_address_delete (void *solver,
898                 struct ATS_Address *address,
899                 int session_only)
900 {
901         //TODO implement
902         /*
903          * remove address
904          * if (last address of peer)
905          *     remove agent
906          * else
907          *     decrease state vector
908          *     decrease knowledge matrix
909          *     decrease action vector
910          */
911         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_address_delete() has been called\n");
912 }
913
914 /**
915  * Transport properties for this address have changed
916  *
917  * @param solver solver handle
918  * @param address the address
919  * @param type the ATSI type in HBO
920  * @param abs_value the absolute value of the property
921  * @param rel_value the normalized value
922  */
923 void
924 GAS_ril_address_property_changed (void *solver,
925                 struct ATS_Address *address,
926                 uint32_t type,
927                 uint32_t abs_value,
928                 double rel_value)
929 {
930           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931                       "Property `%s' for peer `%s' address %p changed to %.2f \n",
932                       GNUNET_ATS_print_property_type (type),
933                       GNUNET_i2s (&address->peer),
934                       address, rel_value);
935           /*
936            * Nothing to do here, properties are considered in every reward calculation
937            */
938 }
939
940
941 /**
942  * Transport session for this address has changed
943  *
944  * NOTE: values in addresses are already updated
945  *
946  * @param solver solver handle
947  * @param address the address
948  * @param cur_session the current session
949  * @param new_session the new session
950  */
951 void
952 GAS_ril_address_session_changed (void *solver,
953                 struct ATS_Address *address,
954                 uint32_t cur_session,
955                 uint32_t new_session)
956 {
957         //TODO implement
958         /*
959          * Potentially add session activity as a feature in state vector
960          */
961         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_address_session_changed() has been called\n");
962 }
963
964
965 /**
966  * Usage for this address has changed
967  *
968  * NOTE: values in addresses are already updated
969  *
970  * @param solver solver handle
971  * @param address the address
972  * @param in_use usage state
973  */
974 void
975 GAS_ril_address_inuse_changed (void *solver,
976                 struct ATS_Address *address,
977                 int in_use)
978 {
979         //TODO implement
980         /**
981          * See matthias' email
982          */
983         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_address_inuse_changed() has been called\n");
984 }
985
986 /**
987  * Network scope for this address has changed
988  *
989  * NOTE: values in addresses are already updated
990  *
991  * @param solver solver handle
992  * @param address the address
993  * @param current_network the current network
994  * @param new_network the new network
995  */
996 void
997 GAS_ril_address_change_network (void *solver,
998                 struct ATS_Address *address,
999                 uint32_t current_network,
1000                 uint32_t new_network)
1001 {
1002         //TODO implement
1003         /*
1004          * update network
1005          */
1006         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_address_change_network() has been called\n");
1007 }
1008
1009 /**
1010  * Get application feedback for a peer
1011  *
1012  * @param solver the solver handle
1013  * @param application the application
1014  * @param peer the peer to change the preference for
1015  * @param scope the time interval for this feedback: [now - scope .. now]
1016  * @param kind the kind to change the preference
1017  * @param score the score
1018  */
1019 void
1020 GAS_ril_address_preference_feedback (void *solver,
1021                 void *application,
1022                 const struct GNUNET_PeerIdentity *peer,
1023                 const struct GNUNET_TIME_Relative scope,
1024                 enum GNUNET_ATS_PreferenceKind kind,
1025                 double score)
1026 {
1027         //TODO implement
1028         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_address_preference_feedback() has been called\n");
1029 }
1030
1031 /**
1032  * Start a bulk operation
1033  *
1034  * @param solver the solver
1035  */
1036 void
1037 GAS_ril_bulk_start (void *solver)
1038 {
1039         //TODO implement
1040         /*
1041          * bulk counter up, but not really relevant, because there is no complete calculation of the
1042          * bandwidth assignment triggered anyway. Therefore, changes to addresses can come and go as
1043          * they want. Consideration: Step-pause during bulk-start-stop period...
1044          */
1045         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_bulk_start() has been called\n");
1046 }
1047
1048
1049 /**
1050  * Bulk operation done
1051  */
1052 void
1053 GAS_ril_bulk_stop (void *solver)
1054 {
1055         //TODO implement
1056         /*
1057          * bulk counter down, see bulk_start()
1058          */
1059         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_bulk_stop() has been called\n");
1060 }
1061
1062 /**
1063  * Get the preferred address for a specific peer
1064  *
1065  * @param solver the solver handle
1066  * @param peer the identity of the peer
1067  */
1068 const struct ATS_Address *
1069 GAS_ril_get_preferred_address (void *solver,
1070                 const struct GNUNET_PeerIdentity *peer)
1071 {
1072         //TODO implement, gets only the first address for now
1073
1074         /*
1075          * connect-only for requested peers, move agent to active list
1076          */
1077         struct GAS_RIL_Handle *s = solver;
1078         struct RIL_Peer_Agent *agent;
1079
1080         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_get_preferred_address() has been called\n");
1081
1082         agent = ril_get_agent(s, peer);
1083         agent->active = GNUNET_YES;
1084
1085         if (0 == GNUNET_CONTAINER_multihashmap_contains(s->addresses, &peer->hashPubKey))
1086         {
1087                 return GNUNET_CONTAINER_multihashmap_get(s->addresses, &peer->hashPubKey);
1088         }
1089
1090         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No address for peer in addresses\n");
1091         return NULL;
1092 }
1093
1094 /**
1095  * Stop notifying about address and bandwidth changes for this peer
1096  *
1097  * @param solver the solver handle
1098  * @param peer the peer
1099  */
1100 void
1101 GAS_ril_stop_get_preferred_address (void *solver,
1102                 const struct GNUNET_PeerIdentity *peer)
1103 {
1104         //TODO implement
1105         /*
1106          * connect-only for requested peers, move agent to paused list
1107          */
1108         struct GAS_RIL_Handle *s = solver;
1109         struct RIL_Peer_Agent *agent;
1110
1111         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ril_stop_get_preferred_address() has been called\n");
1112
1113         agent = ril_get_agent(s, peer);
1114         agent->active = GNUNET_NO;
1115 }
1116
1117 /* end of gnunet-service-ats-solver_ril.c */