ats_ril: fix: set address only active if address request for peer active
[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 LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
34
35 #define RIL_ACTION_INVALID -1
36
37 #define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
38 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
39 #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5
40 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4
41 #define RIL_DEFAULT_TRACE_DECAY 0.6
42 #define RIL_EXPLORE_RATIO 0.1
43
44 /**
45  * ATS reinforcement learning solver
46  *
47  * General description
48  */
49
50 /**
51  * TODO! implement reward calculation 1 and 2 (i.e. meeting preferences and taking scores)
52  */
53
54 enum RIL_Action_Type
55 {
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
66 };
67
68 enum RIL_Algorithm
69 {
70   RIL_ALGO_SARSA = 0,
71   RIL_ALGO_Q = 1
72 };
73
74 enum RIL_E_Modification
75 {
76   RIL_E_SET,
77   RIL_E_ZERO,
78   RIL_E_ACCUMULATE,
79   RIL_E_REPLACE
80 };
81
82 /**
83  * Global learning parameters
84  */
85 struct RIL_Learning_Parameters
86 {
87   /**
88    * The TD-algorithm to use
89    */
90   enum RIL_Algorithm algorithm;
91
92   /**
93    * Learning discount factor in the TD-update
94    */
95   float gamma;
96
97   /**
98    * Gradient-descent step-size
99    */
100   float alpha;
101
102   /**
103    * Trace-decay factor for eligibility traces
104    */
105   float lambda;
106 };
107
108 /**
109  * Wrapper for addresses to store them in agent's linked list
110  */
111 struct RIL_Address_Wrapped
112 {
113   /**
114    * Next in DLL
115    */
116   struct RIL_Address_Wrapped *next;
117
118   /**
119    * Previous in DLL
120    */
121   struct RIL_Address_Wrapped *prev;
122
123   /**
124    * The address
125    */
126   struct ATS_Address *address_naked;
127 };
128
129 struct RIL_Peer_Agent
130 {
131   /**
132    * Next agent in solver's linked list
133    */
134   struct RIL_Peer_Agent *next;
135
136   /**
137    * Previous agent in solver's linked list
138    */
139   struct RIL_Peer_Agent *prev;
140
141   /**
142    * Environment handle
143    */
144   struct GAS_RIL_Handle *envi;
145
146   /**
147    * Peer ID
148    */
149   struct GNUNET_PeerIdentity peer;
150
151   /**
152    * Whether the agent is active or not
153    */
154   int active;
155
156   /**
157    * Number of performed time-steps
158    */
159   unsigned long long step_count;
160
161   /**
162    * Experience matrix W
163    */
164   double ** W;
165
166   /**
167    * Number of rows of W / Number of state-vector features
168    */
169   unsigned int m;
170
171   /**
172    * Number of columns of W / Number of actions
173    */
174   unsigned int n;
175
176   /**
177    * Last perceived state feature vector
178    */
179   double * s_old;
180
181   /**
182    * Last chosen action
183    */
184   int a_old;
185
186   /**
187    * Eligibility trace vector
188    */
189   double * e;
190
191   /**
192    * Address in use
193    */
194   struct ATS_Address * address_inuse;
195
196   /**
197    * Head of addresses DLL
198    */
199   struct RIL_Address_Wrapped * addresses_head;
200
201   /**
202    * Tail of addresses DLL
203    */
204   struct RIL_Address_Wrapped * addresses_tail;
205
206   /**
207    * Inbound bandwidth assigned by the agent
208    */
209   unsigned long long bw_in;
210
211   /**
212    * Outbound bandwidth assigned by the agent
213    */
214   unsigned long long bw_out;
215 };
216
217 struct RIL_Network
218 {
219   /**
220    * ATS network type
221    */
222   enum GNUNET_ATS_Network_Type type;
223
224   /**
225    * Total available inbound bandwidth
226    */
227   unsigned long long bw_in_available;
228
229   /**
230    * Total assigned outbound bandwidth
231    */
232   unsigned long long bw_in_assigned;
233
234   /**
235    * Total available outbound bandwidth
236    */
237   unsigned long long bw_out_available;
238
239   /**
240    * Total assigned outbound bandwidth
241    */
242   unsigned long long bw_out_assigned;
243 };
244
245 struct RIL_Callbacks
246 {
247   /**
248    * Bandwidth changed callback
249    */
250   GAS_bandwidth_changed_cb bw_changed;
251
252   /**
253    * Bandwidth changed callback cls
254    */
255   void *bw_changed_cls;
256
257   /**
258    * ATS function to get preferences
259    */
260   GAS_get_preferences get_preferences;
261
262   /**
263    * Closure for ATS function to get preferences
264    */
265   void *get_preferences_cls;
266
267   /**
268    * ATS function to get properties
269    */
270   GAS_get_properties get_properties;
271
272   /**
273    * Closure for ATS function to get properties
274    */
275   void *get_properties_cls;
276 };
277
278 /**
279  * A handle for the reinforcement learning solver
280  */
281 struct GAS_RIL_Handle
282 {
283   /**
284    * Statistics handle
285    */
286   struct GNUNET_STATISTICS_Handle *stats;
287
288   /**
289    * Hashmap containing all valid addresses
290    */
291   const struct GNUNET_CONTAINER_MultiHashMap *addresses;
292
293   /**
294    * Callbacks for the solver
295    */
296   struct RIL_Callbacks *callbacks;
297
298   /**
299    * Bulk lock
300    */
301   int bulk_lock;
302
303   /**
304    * Number of changes while solver was locked
305    */
306   int bulk_requests;
307
308   /**
309    * Number of performed time-steps
310    */
311   unsigned long long step_count;
312
313   /**
314    * Interval time between steps in milliseconds //TODO? put in agent
315    */
316   struct GNUNET_TIME_Relative step_time;
317
318   /**
319    * Task identifier of the next time-step to be executed //TODO? put in agent
320    */
321   GNUNET_SCHEDULER_TaskIdentifier next_step;
322
323   /**
324    * Learning parameters
325    */
326   struct RIL_Learning_Parameters parameters;
327
328   /**
329    * Array of networks with global assignment state
330    */
331   struct RIL_Network * network_entries;
332
333   /**
334    * Networks count
335    */
336   unsigned int networks_count;
337
338   /**
339    * List of active peer-agents
340    */
341   struct RIL_Peer_Agent * agents_head;
342   struct RIL_Peer_Agent * agents_tail;
343 };
344
345 /**
346  *  Private functions
347  *  ---------------------------
348  */
349
350 /**
351  * Estimate the current action-value for state s and action a
352  * @param agent agent performing the estimation
353  * @param state s
354  * @param action a
355  * @return estimation value
356  */
357 static double
358 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
359 {
360   int i;
361   double result = 0;
362
363   for (i = 0; i < agent->m; i++)
364   {
365     result += state[i] * agent->W[action][i];
366   }
367
368   return result;
369 }
370
371 /**
372  * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
373  * currently estimated best action) in the current step
374  * @param agent agent performing the step
375  * @return yes, if exploring
376  */
377 static int
378 agent_decide_exploration (struct RIL_Peer_Agent *agent)
379 {
380   double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
381       UINT32_MAX) / (double) UINT32_MAX;
382
383 if  (r < RIL_EXPLORE_RATIO)
384   {
385     return GNUNET_YES;
386   }
387   return GNUNET_NO;
388 }
389
390 static int
391 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
392 {
393   int i;
394   struct RIL_Address_Wrapped *cur;
395
396   i = 0;
397   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
398   {
399     if (cur->address_naked == address)
400     {
401       return i;
402     }
403     i++;
404   }
405
406   return -1;
407 }
408
409 static struct RIL_Address_Wrapped *
410 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
411 {
412   struct RIL_Address_Wrapped *cur;
413
414   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
415   {
416     if (cur->address_naked == address)
417     {
418       return cur;
419     }
420   }
421
422   return NULL;
423 }
424
425   /**
426    * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
427    * most reward in the future)
428    * @param agent agent performing the calculation
429    * @param state the state from which to take the action
430    * @return the action promising most future reward
431    */
432 static int
433 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
434 {
435   int i;
436   int max_i = RIL_ACTION_INVALID;
437   double cur_q;
438   double max_q = -DBL_MAX;
439
440   for (i = 0; i < agent->n; i++)
441   {
442     cur_q = agent_estimate_q (agent, state, i);
443     if (cur_q > max_q)
444     {
445       max_q = cur_q;
446       max_i = i;
447     }
448   }
449
450   GNUNET_assert(RIL_ACTION_INVALID != max_i);
451
452   return max_i;
453 }
454
455 /**
456  * Gets any action, to explore the action space from that state
457  * @param agent agent performing the calculation
458  * @param state the state from which to take the action
459  * @return any action
460  */
461 static int
462 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
463 {
464   return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
465 }
466
467 /**
468  * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
469  * @param agent the agent performing the update
470  * @param reward the reward received for the last action
471  * @param s_next the new state, the last step got the agent into
472  * @param a_prime the new
473  */
474 static void
475 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
476 {
477   int i;
478   double delta;
479   double *theta = agent->W[agent->a_old];
480
481   delta = reward + agent_estimate_q (agent, s_next, a_prime)
482       - agent_estimate_q (agent, agent->s_old, agent->a_old);
483   for (i = 0; i < agent->m; i++)
484   {
485     theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i];
486   }
487 }
488
489 /**
490  * Changes the eligibility trace vector e in various manners:
491  * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
492  * RIL_E_REPLACE - resets each component to 1 as in replacing traces
493  * RIL_E_SET - multiplies e with gamma and lambda as in the update rule
494  * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
495  * @param agent
496  * @param mod
497  */
498 static void
499 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
500 {
501   int i;
502   double *e = agent->e;
503   double gamma = agent->envi->parameters.gamma;
504   double lambda = agent->envi->parameters.lambda;
505
506   for (i = 0; i < agent->m; i++)
507   {
508     switch (mod)
509     {
510     case RIL_E_ACCUMULATE:
511       e[i] += 1;
512       break;
513     case RIL_E_REPLACE:
514       e[i] = 1;
515       break;
516     case RIL_E_SET:
517       e[i] = gamma * lambda;
518       break;
519     case RIL_E_ZERO:
520       e[i] = 0;
521       break;
522     }
523   }
524 }
525
526 static void
527 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
528     struct RIL_Peer_Agent *agent,
529     struct ATS_Address *new_address,
530     unsigned long long new_bw_in,
531     unsigned long long new_bw_out)
532 {
533   int notify = GNUNET_NO;
534
535   if (agent->address_inuse != new_address)
536   {
537     if (NULL != agent->address_inuse)
538     {
539       agent->address_inuse->active = GNUNET_NO;
540     }
541     agent->address_inuse = new_address;
542     if (agent->active)
543     {
544       agent->address_inuse->active = GNUNET_YES;
545     }
546     agent->address_inuse->assigned_bw_in.value__ = htonl (agent->bw_in);
547     agent->address_inuse->assigned_bw_out.value__ = htonl (agent->bw_out);
548     notify |= GNUNET_YES;
549   }
550
551   if (agent->bw_in != new_bw_in)
552   {
553     agent->bw_in = new_bw_in;
554     agent->address_inuse->assigned_bw_in.value__ = htonl (new_bw_out);
555     notify |= GNUNET_YES;
556   }
557   if (agent->bw_out != new_bw_out)
558   {
559     agent->bw_out = new_bw_out;
560     agent->address_inuse->assigned_bw_out.value__ = htonl (new_bw_out);
561     notify |= GNUNET_YES;
562   }
563
564   if (notify && agent->active)
565   {
566     solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, agent->address_inuse);
567   }
568 }
569
570 /**
571  * Allocates a state vector and fills it with the features present
572  * @param solver the solver handle
573  * @return pointer to the state vector
574  */
575 static double *
576 envi_get_state (struct GAS_RIL_Handle *solver)
577 {
578   int i;
579   struct RIL_Network *net;
580   double *state = GNUNET_malloc (sizeof (double) * solver->networks_count * 4);
581
582   for (i = 0; i < solver->networks_count; i++)
583   {
584     net = &solver->network_entries[i];
585     state[i * 4 + 0] = (double) net->bw_in_assigned;
586     state[i * 4 + 1] = (double) net->bw_in_available;
587     state[i * 4 + 2] = (double) net->bw_out_assigned;
588     state[i * 4 + 3] = (double) net->bw_out_available;
589   }
590
591   return state;
592 }
593
594 /**
595  * Gets the reward of the last performed step
596  * @param solver solver handle
597  * @return the reward
598  */
599 static double
600 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
601 {
602   //TODO! implement reward calculation
603
604   return (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX)
605       / (double) UINT32_MAX;
606 }
607
608 static void
609 envi_action_bw_double (struct GAS_RIL_Handle *solver,
610     struct RIL_Peer_Agent *agent,
611     int direction_in)
612 {
613   if (direction_in)
614   {
615     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in * 2, agent->bw_out);
616   }
617   else
618   {
619     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out * 2);
620   }
621 }
622
623 static void
624 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
625     struct RIL_Peer_Agent *agent,
626     int direction_in)
627 {
628   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
629   unsigned long long new_bw;
630
631   if (direction_in)
632   {
633     new_bw = agent->bw_in / 2;
634     if (new_bw < min_bw)
635       new_bw = min_bw;
636     envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out);
637   }
638   else
639   {
640     new_bw = agent->bw_out / 2;
641     if (new_bw < min_bw)
642       new_bw = min_bw;
643     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw);
644   }
645 }
646
647 static void
648 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
649 {
650   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
651
652   if (direction_in)
653   {
654     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in + (5 * min_bw),
655         agent->bw_out);
656   }
657   else
658   {
659     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
660         agent->bw_out + (5 * min_bw));
661   }
662 }
663
664 static void
665 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
666 {
667   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
668   unsigned long long new_bw;
669
670   if (direction_in)
671   {
672     new_bw = agent->bw_in - (5 * min_bw);
673     if (new_bw < min_bw)
674       new_bw = min_bw;
675     envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out);
676   }
677   else
678   {
679     new_bw = agent->bw_out - (5 * min_bw);
680     if (new_bw < min_bw)
681       new_bw = min_bw;
682     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw);
683   }
684 }
685
686 static void
687 envi_action_address_switch (struct GAS_RIL_Handle *solver,
688     struct RIL_Peer_Agent *agent,
689     unsigned int address_index)
690 {
691   struct RIL_Address_Wrapped *cur;
692   int i = 0;
693
694   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
695   {
696     if (i == address_index) {
697       envi_set_active_suggestion(solver, agent, cur->address_naked, agent->bw_in, agent->bw_out);
698       return;
699     }
700
701     i++;
702   }
703
704   //no address with address_index exists
705   GNUNET_assert (GNUNET_NO);
706 }
707
708 /**
709  * Puts the action into effect
710  * @param solver solver handle
711  * @param action action to perform by the solver
712  */
713 static void
714 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
715 {
716   unsigned int address_index;
717
718   switch (action)
719   {
720   case RIL_ACTION_NOTHING:
721     break;
722   case RIL_ACTION_BW_IN_DBL:
723     envi_action_bw_double (solver, agent, GNUNET_YES);
724     break;
725   case RIL_ACTION_BW_IN_HLV:
726     envi_action_bw_halven (solver, agent, GNUNET_YES);
727     break;
728   case RIL_ACTION_BW_IN_INC:
729     envi_action_bw_inc (solver, agent, GNUNET_YES);
730     break;
731   case RIL_ACTION_BW_IN_DEC:
732     envi_action_bw_dec (solver, agent, GNUNET_YES);
733     break;
734   case RIL_ACTION_BW_OUT_DBL:
735     envi_action_bw_double (solver, agent, GNUNET_NO);
736     break;
737   case RIL_ACTION_BW_OUT_HLV:
738     envi_action_bw_halven (solver, agent, GNUNET_NO);
739     break;
740   case RIL_ACTION_BW_OUT_INC:
741     envi_action_bw_inc (solver, agent, GNUNET_NO);
742     break;
743   case RIL_ACTION_BW_OUT_DEC:
744     envi_action_bw_dec (solver, agent, GNUNET_NO);
745     break;
746   default:
747     if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n))
748     {
749       address_index = agent->n - RIL_ACTION_TYPE_NUM;
750
751       GNUNET_assert (address_index >= 0);
752       GNUNET_assert (address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
753
754       envi_action_address_switch (solver, agent, address_index);
755       break;
756     }
757     // error - action does not exist
758     GNUNET_assert(GNUNET_NO);
759   }
760 }
761
762 /**
763  * Performs one step of the Markov Decision Process. Other than in the literature the step starts
764  * after having done the last action a_old. It observes the new state s_next and the reward
765  * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
766  * next action is put into effect.
767  * @param agent the agent performing the step
768  */
769 static void
770 agent_step (struct RIL_Peer_Agent *agent)
771 {
772   int a_next = RIL_ACTION_INVALID;
773   double *s_next;
774   double reward;
775
776   s_next = envi_get_state (agent->envi);
777   reward = envi_get_reward (agent->envi, agent);
778
779   LOG(GNUNET_ERROR_TYPE_DEBUG, "agent_step() with algorithm %s\n",
780       agent->envi->parameters.algorithm ? "Q" : "SARSA");
781
782   switch (agent->envi->parameters.algorithm)
783   {
784   case RIL_ALGO_SARSA:
785     agent_modify_eligibility (agent, RIL_E_SET);
786     if (agent_decide_exploration (agent))
787     {
788       a_next = agent_get_action_explore (agent, s_next);
789     }
790     else
791     {
792       a_next = agent_get_action_best (agent, s_next);
793     }
794     if (RIL_ACTION_INVALID != agent->a_old)
795     {
796       //updates weights with selected action (on-policy), if not first step
797       agent_update_weights (agent, reward, s_next, a_next);
798     }
799     break;
800
801   case RIL_ALGO_Q:
802     a_next = agent_get_action_best (agent, s_next);
803     if (RIL_ACTION_INVALID != agent->a_old)
804     {
805       //updates weights with best action, disregarding actually selected action (off-policy), if not first step
806       agent_update_weights (agent, reward, s_next, a_next);
807     }
808     if (agent_decide_exploration (agent))
809     {
810       a_next = agent_get_action_explore (agent, s_next);
811       agent_modify_eligibility (agent, RIL_E_ZERO);
812     }
813     else
814     {
815       a_next = agent_get_action_best (agent, s_next);
816       agent_modify_eligibility (agent, RIL_E_SET);
817     }
818     break;
819   }
820
821   GNUNET_assert(RIL_ACTION_INVALID != a_next);
822
823   agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
824
825   envi_do_action (agent->envi, agent, a_next);
826
827   GNUNET_free(agent->s_old);
828   agent->s_old = s_next;
829   agent->a_old = a_next;
830
831   agent->step_count += 1;
832 }
833
834 /**
835  * Cycles through all agents and lets the active ones do a step. Schedules the next step.
836  * @param solver the solver handle
837  * @param tc task context for the scheduler
838  */
839 static void
840 ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
841 {
842   struct GAS_RIL_Handle *solver = cls;
843   struct RIL_Peer_Agent *cur;
844
845   LOG(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count);
846
847   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
848   {
849     if (cur->active)
850     {
851       agent_step (cur);
852     }
853   }
854
855   solver->step_count += 1;
856   solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, &ril_periodic_step, solver);
857 }
858
859 /**
860  * Initialize an agent without addresses and its knowledge base
861  * @param s ril solver
862  * @param peer the one in question
863  * @return handle to the new agent
864  */
865 static struct RIL_Peer_Agent *
866 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
867 {
868   int i;
869   struct GAS_RIL_Handle * solver = s;
870   struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
871
872   agent->envi = solver;
873   agent->peer = *peer;
874   agent->step_count = 0;
875   agent->active = GNUNET_NO;
876   agent->s_old = envi_get_state (solver);
877   agent->n = RIL_ACTION_TYPE_NUM;
878   agent->m = solver->networks_count * 4;
879   agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n);
880   for (i = 0; i < agent->n; i++)
881   {
882     agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
883   }
884   agent->a_old = RIL_ACTION_INVALID;
885   agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
886   agent_modify_eligibility (agent, RIL_E_ZERO);
887
888   GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
889
890   return agent;
891 }
892
893 /**
894  * Deallocate agent
895  * @param s solver handle
896  * @param agent the agent to retire
897  */
898 static void
899 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
900 {
901   int i;
902
903   for (i = 0; i < agent->n; i++)
904   {
905     GNUNET_free(agent->W[i]);
906   }
907   GNUNET_free(agent->W);
908   GNUNET_free(agent->e);
909   GNUNET_free(agent->s_old);
910 }
911
912 /**
913  * Returns the agent for a peer
914  * @param s solver handle
915  * @param peer identity of the peer
916  * @param create whether to create an agent if none is allocated yet
917  * @return agent
918  */
919 static struct RIL_Peer_Agent *
920 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
921 {
922   struct RIL_Peer_Agent *cur;
923
924   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
925   {
926     if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey))
927     {
928       return cur;
929     }
930   }
931
932   if (create)
933     return agent_init (solver, peer);
934   return NULL ;
935 }
936
937 /**
938  * Lookup network struct by type
939  *
940  * @param s the solver handle
941  * @param type the network type
942  * @return the network struct
943  */
944 static struct RIL_Network *
945 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
946 {
947   int i;
948
949   for (i = 0; i < s->networks_count; i++)
950   {
951     if (s->network_entries[i].type == type)
952     {
953       return &s->network_entries[i];
954     }
955   }
956   return NULL ;
957 }
958
959 static int
960 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
961 {
962   struct RIL_Network *net;
963   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
964
965   net = ril_get_network(solver, network);
966   if (net->bw_out_available < min_bw)
967     return GNUNET_NO;
968   return GNUNET_YES;
969 }
970
971 static void
972 ril_cut_from_vector (void **old, size_t element_size, unsigned int hole_start, unsigned int hole_length, unsigned int old_length)
973 {
974   char *tmpptr;
975   char *oldptr = (char *) *old;
976   size_t size;
977   unsigned int bytes_before;
978   unsigned int bytes_hole;
979   unsigned int bytes_after;
980
981 //  LOG(GNUNET_ERROR_TYPE_DEBUG, "hole_start = %d, hole_length = %d, old_length = %d\n", hole_start, hole_length, old_length);
982   GNUNET_assert(old_length > hole_length);
983   GNUNET_assert(old_length >= (hole_start + hole_length));
984
985   size = (old_length - hole_length) * element_size;
986
987   bytes_before = element_size * hole_start;
988   bytes_hole   = element_size * hole_length;
989   bytes_after  = element_size * (old_length - hole_start - hole_length);
990
991   if (0 == size)
992   {
993     tmpptr = NULL;
994   }
995   else
996   {
997     tmpptr = GNUNET_malloc (size);
998     memcpy (tmpptr, oldptr, bytes_before);
999     memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1000   }
1001   if (NULL != *old)
1002   {
1003     GNUNET_free(*old);
1004   }
1005   *old = (void *) tmpptr;
1006 }
1007
1008 /**
1009  *  Solver API functions
1010  *  ---------------------------
1011  */
1012
1013 /**
1014  * Changes the preferences for a peer in the problem
1015  *
1016  * @param solver the solver handle
1017  * @param peer the peer to change the preference for
1018  * @param kind the kind to change the preference
1019  * @param pref_rel the normalized preference value for this kind over all clients
1020  */
1021 void
1022 GAS_ril_address_change_preference (void *s,
1023     const struct GNUNET_PeerIdentity *peer,
1024     enum GNUNET_ATS_PreferenceKind kind,
1025     double pref_rel)
1026 {
1027   LOG(GNUNET_ERROR_TYPE_DEBUG,
1028       "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1029       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1030   /*
1031    * Nothing to do here. Preferences are considered during reward calculation.
1032    */
1033 }
1034
1035 /**
1036  * Init the reinforcement learning problem solver
1037  *
1038  * Quotas:
1039  * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1040  * out_quota[i] contains outbound quota for network type i
1041  * in_quota[i] contains inbound quota for network type i
1042  *
1043  * Example
1044  * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1045  * network[2]   == GNUNET_ATS_NET_LAN
1046  * out_quota[2] == 65353
1047  * in_quota[2]  == 65353
1048  *
1049  * @param cfg configuration handle
1050  * @param stats the GNUNET_STATISTICS handle
1051  * @param network array of GNUNET_ATS_NetworkType with length dest_length
1052  * @param addresses hashmap containing all addresses
1053  * @param out_quota array of outbound quotas
1054  * @param in_quota array of outbound quota
1055  * @param dest_length array length for quota arrays
1056  * @param bw_changed_cb callback for changed bandwidth amounts
1057  * @param bw_changed_cb_cls cls for callback
1058  * @param get_preference callback to get relative preferences for a peer
1059  * @param get_preference_cls cls for callback to get relative preferences
1060  * @param get_properties_cls for callback to get relative properties
1061  * @param get_properties_cls cls for callback to get relative properties
1062  * @return handle for the solver on success, NULL on fail
1063  */
1064 void *
1065 GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1066     const struct GNUNET_STATISTICS_Handle *stats,
1067     const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1068     int *network,
1069     unsigned long long *out_quota,
1070     unsigned long long *in_quota,
1071     int dest_length,
1072     GAS_bandwidth_changed_cb bw_changed_cb,
1073     void *bw_changed_cb_cls,
1074     GAS_get_preferences get_preference,
1075     void *get_preference_cls,
1076     GAS_get_properties get_properties,
1077     void *get_properties_cls)
1078 {
1079   int c;
1080   unsigned long long tmp;
1081   char *string;
1082   struct RIL_Network * cur;
1083   struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle));
1084
1085   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1086
1087   GNUNET_assert(NULL != cfg);
1088   GNUNET_assert(NULL != stats);
1089   GNUNET_assert(NULL != network);
1090   GNUNET_assert(NULL != bw_changed_cb);
1091   GNUNET_assert(NULL != get_preference);
1092   GNUNET_assert(NULL != get_properties);
1093
1094   if (GNUNET_OK
1095       != GNUNET_CONFIGURATION_get_value_time (cfg, "ats", "RIL_STEP_TIME", &solver->step_time))
1096   {
1097     solver->step_time = RIL_DEFAULT_STEP_TIME;
1098   }
1099   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "RIL_ALGORITHM", &string)
1100       && NULL != string && 0 == strcmp (string, "SARSA"))
1101   {
1102     solver->parameters.algorithm = RIL_ALGO_SARSA;
1103   }
1104   else
1105   {
1106     solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1107   }
1108   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp))
1109   {
1110     solver->parameters.gamma = (double) tmp / 100;
1111   }
1112   else
1113   {
1114     solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR;
1115   }
1116   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp))
1117   {
1118     solver->parameters.alpha = (double) tmp / 100;
1119   }
1120   else
1121   {
1122     solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1123   }
1124   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_TRACE_DECAY", &tmp))
1125   {
1126     solver->parameters.lambda = (double) tmp / 100;
1127   }
1128   else
1129   {
1130     solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1131   }
1132
1133   solver->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1134   solver->callbacks = GNUNET_malloc (sizeof (struct RIL_Callbacks));
1135   solver->callbacks->bw_changed = bw_changed_cb;
1136   solver->callbacks->bw_changed_cls = bw_changed_cb_cls;
1137   solver->callbacks->get_preferences = get_preference;
1138   solver->callbacks->get_preferences_cls = get_preference_cls;
1139   solver->callbacks->get_properties = get_properties;
1140   solver->callbacks->get_properties_cls = get_properties_cls;
1141   solver->networks_count = dest_length;
1142   solver->network_entries = GNUNET_malloc (dest_length * sizeof (struct RIL_Network));
1143   solver->bulk_lock = GNUNET_NO;
1144   solver->addresses = addresses;
1145   solver->step_count = 0;
1146
1147   for (c = 0; c < dest_length; c++)
1148   {
1149     cur = &solver->network_entries[c];
1150     cur->type = network[c];
1151     cur->bw_in_available = in_quota[c];
1152     cur->bw_in_assigned = 0;
1153     cur->bw_out_available = out_quota[c];
1154     cur->bw_out_assigned = 0;
1155   }
1156
1157   solver->next_step = GNUNET_SCHEDULER_add_delayed (
1158       GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1159       &ril_periodic_step, solver);
1160
1161   return solver;
1162 }
1163
1164 /**
1165  * Shutdown the reinforcement learning problem solver
1166  *
1167  * @param solver the respective handle to shutdown
1168  */
1169 void
1170 GAS_ril_done (void * solver)
1171 {
1172   struct GAS_RIL_Handle *s = solver;
1173   struct RIL_Peer_Agent *cur_agent;
1174   struct RIL_Peer_Agent *next_agent;
1175
1176   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1177
1178   cur_agent = s->agents_head;
1179   while (NULL != cur_agent)
1180   {
1181     next_agent = cur_agent->next;
1182     GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1183     agent_die (s, cur_agent);
1184     cur_agent = next_agent;
1185   }
1186
1187   GNUNET_SCHEDULER_cancel (s->next_step);
1188   GNUNET_free(s->callbacks);
1189   GNUNET_free(s->network_entries);
1190   GNUNET_free(s);
1191 }
1192
1193 /**
1194  * Add a single address within a network to the solver
1195  *
1196  * @param solver the solver Handle
1197  * @param address the address to add
1198  * @param network network type of this address
1199  */
1200 void
1201 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1202 {
1203   struct GAS_RIL_Handle *s = solver;
1204   struct RIL_Peer_Agent *agent;
1205   struct RIL_Address_Wrapped *address_wrapped;
1206   unsigned int m_new;
1207   unsigned int m_old;
1208   unsigned int n_new;
1209   unsigned int n_old;
1210   int i;
1211   unsigned int zero;
1212   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1213
1214   address->solver_information = ril_get_network (s, network);
1215
1216   if (!ril_network_is_active(s, network))
1217   {
1218     LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Did not add %s address %p for peer '%s', network does not have enough bandwidth\n",
1219         address->plugin, address->addr, GNUNET_i2s (&address->peer));
1220     return;
1221   }
1222
1223   agent = ril_get_agent(s, &address->peer, GNUNET_YES);
1224
1225   //add address
1226   address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1227   address_wrapped->address_naked = address;
1228   GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1229
1230   //increase size of W
1231   m_new = agent->m + 5; //TODO! make size of features from address variable (Note to self: ctrl+f for "5" or I kill you!)
1232   m_old = agent->m;
1233   n_new = agent->n + 1;
1234   n_old = agent->n;
1235
1236   GNUNET_array_grow (agent->W, agent->n, n_new);
1237   for (i = 0; i < n_new; i++)
1238   {
1239     if (i < n_old)
1240     {
1241       agent->m = m_old;
1242       GNUNET_array_grow(agent->W[i], agent->m, m_new);
1243     }
1244     else
1245     {
1246       zero = 0;
1247       GNUNET_array_grow(agent->W[i], zero, m_new);
1248     }
1249   }
1250
1251   //increase size of old state vector if there is one
1252   if (RIL_ACTION_INVALID != agent->a_old)
1253   {
1254     agent->m = m_old;
1255     GNUNET_array_grow(agent->s_old, agent->m, m_new); //TODO initialize new state features?
1256   }
1257
1258   agent->m = m_old;
1259   GNUNET_array_grow(agent->e, agent->m, m_new);
1260
1261   if (NULL == agent->address_inuse)
1262   {
1263     envi_set_active_suggestion(s, agent, address, min_bw, min_bw);
1264   }
1265
1266   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s address %p for peer '%s'\n",
1267       address->plugin, address->addr, GNUNET_i2s (&address->peer));
1268 }
1269
1270 /**
1271  * Remove an address from the solver
1272  *
1273  * @param solver the solver handle
1274  * @param address the address to remove
1275  * @param session_only delete only session not whole address
1276  */
1277 void
1278 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1279 {
1280   //TODO! delete session only
1281   struct GAS_RIL_Handle *s = solver;
1282   struct RIL_Peer_Agent *agent;
1283   struct RIL_Address_Wrapped *address_wrapped;
1284   int address_was_used = address->active;
1285   int address_index;
1286   unsigned int m_new;
1287   unsigned int n_new;
1288   int i;
1289   struct RIL_Network *net;
1290   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1291
1292   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %p for peer '%s'\n",
1293         session_only ? "session for " : "",
1294             address->active ? "active" : "inactive",
1295         address->plugin,
1296         address->addr,
1297         GNUNET_i2s (&address->peer));
1298
1299   agent = ril_get_agent(s, &address->peer, GNUNET_NO);
1300   if (NULL == agent)
1301   {
1302     net = address->solver_information;
1303     GNUNET_assert (!ril_network_is_active(s, net->type));
1304     LOG (GNUNET_ERROR_TYPE_DEBUG, "No agent allocated for peer yet, since address was in inactive network\n");
1305     return;
1306   }
1307
1308   address_index = agent_address_get_index(agent, address);
1309   address_wrapped = agent_address_get(agent, address);
1310
1311   if (NULL == address_wrapped)
1312   {
1313     net = address->solver_information;
1314     GNUNET_assert (!ril_network_is_active(s, net->type));
1315     LOG (GNUNET_ERROR_TYPE_DEBUG, "Address not considered by agent, address was in inactive network\n");
1316     return;
1317   }
1318
1319   GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1320
1321   //decrease W
1322   m_new = agent->m - 5;
1323   n_new = agent->n - 1;
1324
1325   for (i = 0; i < agent->n; i++)
1326   {
1327 //    LOG (GNUNET_ERROR_TYPE_DEBUG, "first - cut vectors in W\n");
1328     ril_cut_from_vector((void **) &agent->W[i], sizeof (double), ((s->networks_count * 4) + (address_index * 5)), 5, agent->m);
1329   }
1330 //  LOG (GNUNET_ERROR_TYPE_DEBUG, "second - cut action vector out of W\n");
1331   ril_cut_from_vector((void **) &agent->W, sizeof (double *), RIL_ACTION_TYPE_NUM + address_index, 1, agent->n);
1332   //correct last action
1333   if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1334   {
1335     agent->a_old -= 1;
1336   }
1337   else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1338   {
1339     agent->a_old = RIL_ACTION_INVALID;
1340   }
1341   //decrease old state vector and eligibility vector
1342 //  LOG (GNUNET_ERROR_TYPE_DEBUG, "third - cut state vector\n");
1343   ril_cut_from_vector((void **) &agent->s_old, sizeof (double), ((s->networks_count * 4) + (address_index * 5)), 5, agent->m);
1344 //  LOG (GNUNET_ERROR_TYPE_DEBUG, "fourth - cut eligibility vector\n");
1345   ril_cut_from_vector((void **) &agent->e,     sizeof (double), ((s->networks_count * 4) + (address_index * 5)), 5, agent->m);
1346   agent->m = m_new;
1347   agent->n = n_new;
1348
1349   if (address_was_used)
1350   {
1351     net = address->solver_information;
1352     net->bw_in_assigned -= agent->bw_in;
1353     net->bw_out_assigned -= agent->bw_out;
1354
1355     if (NULL != agent->addresses_head) //if peer has an address left, use it
1356     {
1357     //TODO? check if network/bandwidth update can be done more clever/elegant at different function
1358       envi_set_active_suggestion(s, agent, agent->addresses_head->address_naked, min_bw, min_bw);
1359       net = agent->addresses_head->address_naked->solver_information;
1360       net->bw_in_assigned -= min_bw;
1361       net->bw_out_assigned -= min_bw;
1362     }
1363   }
1364
1365   LOG (GNUNET_ERROR_TYPE_DEBUG, "Address deleted\n");
1366 }
1367
1368 /**
1369  * Transport properties for this address have changed
1370  *
1371  * @param solver solver handle
1372  * @param address the address
1373  * @param type the ATSI type in HBO
1374  * @param abs_value the absolute value of the property
1375  * @param rel_value the normalized value
1376  */
1377 void
1378 GAS_ril_address_property_changed (void *solver,
1379     struct ATS_Address *address,
1380     uint32_t type,
1381     uint32_t abs_value,
1382     double rel_value)
1383 {
1384   LOG(GNUNET_ERROR_TYPE_DEBUG,
1385       "API_address_property_changed() Property '%s' for peer '%s' address %p changed "
1386           "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer), address->addr,
1387       rel_value);
1388   /*
1389    * Nothing to do here, properties are considered in every reward calculation
1390    */
1391 }
1392
1393 /**
1394  * Transport session for this address has changed
1395  *
1396  * NOTE: values in addresses are already updated
1397  *
1398  * @param solver solver handle
1399  * @param address the address
1400  * @param cur_session the current session
1401  * @param new_session the new session
1402  */
1403 void
1404 GAS_ril_address_session_changed (void *solver,
1405     struct ATS_Address *address,
1406     uint32_t cur_session,
1407     uint32_t new_session)
1408 {
1409   //TODO? consider session changed in solver behaviour
1410   /*
1411    * Potentially add session activity as a feature in state vector
1412    */
1413   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1414 }
1415
1416 /**
1417  * Usage for this address has changed
1418  *
1419  * NOTE: values in addresses are already updated
1420  *
1421  * @param solver solver handle
1422  * @param address the address
1423  * @param in_use usage state
1424  */
1425 void
1426 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1427 {
1428   //TODO! consider address_inuse_changed according to matthias' email
1429   /**
1430    * See matthias' email
1431    */
1432   LOG(GNUNET_ERROR_TYPE_DEBUG,
1433       "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1434       address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1435 }
1436
1437 /**
1438  * Network scope for this address has changed
1439  *
1440  * NOTE: values in addresses are already updated
1441  *
1442  * @param solver solver handle
1443  * @param address the address
1444  * @param current_network the current network
1445  * @param new_network the new network
1446  */
1447 void
1448 GAS_ril_address_change_network (void *solver,
1449     struct ATS_Address *address,
1450     uint32_t current_network,
1451     uint32_t new_network)
1452 {
1453   struct GAS_RIL_Handle *s = solver;
1454   struct RIL_Peer_Agent *agent;
1455   struct RIL_Network *net;
1456   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1457
1458   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
1459       "%s address of peer %s from '%s' to '%s'\n",
1460       (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
1461       GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
1462
1463   if (address->active && !ril_network_is_active(solver, new_network))
1464   {
1465     GAS_ril_address_delete(solver, address, GNUNET_NO);
1466     return;
1467   }
1468
1469   agent = ril_get_agent(s, &address->peer, GNUNET_NO);
1470   if (NULL == agent)
1471   {
1472     //no agent there yet, so add as if address is new
1473     address->solver_information = ril_get_network (s, new_network);
1474     GAS_ril_address_add(s,address,new_network);
1475     return;
1476   }
1477
1478   net = ril_get_network (s, current_network);
1479   net->bw_in_assigned -= agent->bw_in;
1480   net->bw_out_assigned -= agent->bw_out;
1481
1482   net = ril_get_network (s, new_network);
1483   net->bw_in_assigned -= min_bw;
1484   net->bw_out_assigned -= min_bw;
1485   address->solver_information = net;
1486 }
1487
1488 /**
1489  * Get application feedback for a peer
1490  *
1491  * @param solver the solver handle
1492  * @param application the application
1493  * @param peer the peer to change the preference for
1494  * @param scope the time interval for this feedback: [now - scope .. now]
1495  * @param kind the kind to change the preference
1496  * @param score the score
1497  */
1498 void
1499 GAS_ril_address_preference_feedback (void *solver,
1500     void *application,
1501     const struct GNUNET_PeerIdentity *peer,
1502     const struct GNUNET_TIME_Relative scope,
1503     enum GNUNET_ATS_PreferenceKind kind,
1504     double score)
1505 {
1506   //TODO! collect reward until next reward calculation
1507   LOG(GNUNET_ERROR_TYPE_DEBUG,
1508       "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
1509           "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
1510       GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
1511 }
1512
1513 /**
1514  * Start a bulk operation
1515  *
1516  * @param solver the solver
1517  */
1518 void
1519 GAS_ril_bulk_start (void *solver)
1520 {
1521   //TODO? consideration: keep bulk counter and stop agents during bulk
1522   /*
1523    * bulk counter up, but not really relevant, because there is no complete calculation of the
1524    * bandwidth assignment triggered anyway. Therefore, changes to addresses can come and go as
1525    * they want. Consideration: Step-pause during bulk-start-stop period...
1526    */
1527
1528   //LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n");
1529 }
1530
1531 /**
1532  * Bulk operation done
1533  */
1534 void
1535 GAS_ril_bulk_stop (void *solver)
1536 {
1537   //TODO? consideration: keep bulk counter and stop agents during bulk
1538   /*
1539    * bulk counter down, see bulk_start()
1540    */
1541
1542   //LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n");
1543 }
1544
1545 /**
1546  * Get the preferred address for a specific peer
1547  *
1548  * @param solver the solver handle
1549  * @param peer the identity of the peer
1550  */
1551 const struct ATS_Address *
1552 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1553 {
1554   /*
1555    * activate agent, return currently chosen address
1556    */
1557   struct GAS_RIL_Handle *s = solver;
1558   struct RIL_Peer_Agent *agent;
1559
1560   agent = ril_get_agent (s, peer, GNUNET_NO);
1561
1562   if (NULL == agent)
1563   {
1564     LOG(GNUNET_ERROR_TYPE_DEBUG,
1565         "API_get_preferred_address() No agent for peer '%s' do not suggest address\n",
1566         GNUNET_i2s (peer));
1567     return NULL ;
1568   }
1569
1570   agent->active = GNUNET_YES;
1571
1572   GNUNET_assert(NULL != agent->address_inuse);
1573
1574   LOG(GNUNET_ERROR_TYPE_DEBUG,
1575       "API_get_preferred_address() Activated agent for peer '%s' with %s address\n",
1576       GNUNET_i2s (peer), agent->address_inuse->plugin);
1577
1578   return agent->address_inuse;
1579 }
1580
1581 /**
1582  * Stop notifying about address and bandwidth changes for this peer
1583  *
1584  * @param solver the solver handle
1585  * @param peer the peer
1586  */
1587 void
1588 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
1589 {
1590   struct GAS_RIL_Handle *s = solver;
1591   struct RIL_Peer_Agent *agent;
1592
1593   agent = ril_get_agent (s, peer, GNUNET_NO);
1594   agent->active = GNUNET_NO;
1595
1596   LOG(GNUNET_ERROR_TYPE_DEBUG,
1597       "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
1598       GNUNET_i2s (peer), agent->address_inuse->plugin);
1599 }
1600
1601 /* end of gnunet-service-ats-solver_ril.c */