better experience initialization
[oweals/gnunet.git] / src / ats / libgnunet_plugin_ats_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/libgnunet_plugin_ats_ril.c
23  * @brief ATS reinforcement learning solver
24  * @author Fabian Oehlmann
25  * @author Matthias Wachs
26  */
27 #include "libgnunet_plugin_ats_ril.h"
28
29 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
30
31 #define RIL_ACTION_INVALID -1
32 #define RIL_FEATURES_ADDRESS_COUNT (3 + GNUNET_ATS_QualityPropertiesCount)
33 #define RIL_FEATURES_NETWORK_COUNT 4
34 #define RIL_INTERVAL_EXPONENT 10
35
36 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
37 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
38 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
39 #define RIL_DEFAULT_DISCOUNT_BETA 0.7
40 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.3
41 #define RIL_DEFAULT_TRACE_DECAY 0.5
42 #define RIL_DEFAULT_EXPLORE_RATIO 0.1
43
44 /**
45  * ATS reinforcement learning solver
46  *
47  * General description
48  */
49
50 /**
51  * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
52  * changed for the currently chosen address. Not depicted in the enum are the actions of switching
53  * to a particular address. The action of switching to address with index i is depicted by the
54  * number (RIL_ACTION_TYPE_NUM + i).
55  */
56 enum RIL_Action_Type
57 {
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
68 };
69
70 enum RIL_Algorithm
71 {
72   RIL_ALGO_SARSA = 0,
73   RIL_ALGO_Q = 1
74 };
75
76 enum RIL_E_Modification
77 {
78   RIL_E_SET,
79   RIL_E_ZERO,
80   RIL_E_ACCUMULATE,
81   RIL_E_REPLACE
82 };
83
84 /**
85  * Global learning parameters
86  */
87 struct RIL_Learning_Parameters
88 {
89   /**
90    * The TD-algorithm to use
91    */
92   enum RIL_Algorithm algorithm;
93
94   /**
95    * Learning discount factor in the TD-update
96    */
97   double beta;
98
99   /**
100    * Gradient-descent step-size
101    */
102   double alpha;
103
104   /**
105    * Trace-decay factor for eligibility traces
106    */
107   double lambda;
108
109   /**
110    *
111    */
112   double explore_ratio;
113
114   /**
115    * Minimal interval time between steps in milliseconds
116    */
117   struct GNUNET_TIME_Relative step_time_min;
118
119   /**
120    * Maximum interval time between steps in milliseconds
121    */
122   struct GNUNET_TIME_Relative step_time_max;
123 };
124
125 /**
126  * Wrapper for addresses to store them in agent's linked list
127  */
128 struct RIL_Address_Wrapped
129 {
130   /**
131    * Next in DLL
132    */
133   struct RIL_Address_Wrapped *next;
134
135   /**
136    * Previous in DLL
137    */
138   struct RIL_Address_Wrapped *prev;
139
140   /**
141    * The address
142    */
143   struct ATS_Address *address_naked;
144 };
145
146 struct RIL_Peer_Agent
147 {
148   /**
149    * Next agent in solver's linked list
150    */
151   struct RIL_Peer_Agent *next;
152
153   /**
154    * Previous agent in solver's linked list
155    */
156   struct RIL_Peer_Agent *prev;
157
158   /**
159    * Environment handle
160    */
161   struct GAS_RIL_Handle *envi;
162
163   /**
164    * Peer ID
165    */
166   struct GNUNET_PeerIdentity peer;
167
168   /**
169    * Whether the agent is active or not
170    */
171   int is_active;
172
173   /**
174    * Number of performed time-steps
175    */
176   unsigned long long step_count;
177
178   /**
179    * Experience matrix W
180    */
181   double ** W;
182
183   /**
184    * Number of rows of W / Number of state-vector features
185    */
186   unsigned int m;
187
188   /**
189    * Number of columns of W / Number of actions
190    */
191   unsigned int n;
192
193   /**
194    * Last perceived state feature vector
195    */
196   double * s_old;
197
198   /**
199    * Last chosen action
200    */
201   int a_old;
202
203   /**
204    * Eligibility trace vector
205    */
206   double * e;
207
208   /**
209    * Address in use
210    */
211   struct ATS_Address * address_inuse;
212
213   /**
214    * Head of addresses DLL
215    */
216   struct RIL_Address_Wrapped * addresses_head;
217
218   /**
219    * Tail of addresses DLL
220    */
221   struct RIL_Address_Wrapped * addresses_tail;
222
223   /**
224    * Inbound bandwidth assigned by the agent
225    */
226   unsigned long long bw_in;
227
228   /**
229    * Outbound bandwidth assigned by the agent
230    */
231   unsigned long long bw_out;
232
233   /**
234    * Flag whether a suggestion has to be issued
235    */
236   int suggestion_issue;
237
238   /**
239    * The address which has to be issued ()
240    */
241   struct ATS_Address * suggestion_address;
242 };
243
244 struct RIL_Network
245 {
246   /**
247    * ATS network type
248    */
249   enum GNUNET_ATS_Network_Type type;
250
251   /**
252    * Total available inbound bandwidth
253    */
254   unsigned long long bw_in_available;
255
256   /**
257    * Total available outbound bandwidth
258    */
259   unsigned long long bw_out_available;
260 };
261
262 /**
263  * A handle for the reinforcement learning solver
264  */
265 struct GAS_RIL_Handle
266 {
267   /**
268    * The solver-plugin environment of the solver-plugin API
269    */
270   struct GNUNET_ATS_PluginEnvironment *plugin_envi;
271
272   /**
273    * Statistics handle
274    */
275   struct GNUNET_STATISTICS_Handle *stats;
276
277   /**
278    * Number of performed steps
279    */
280   unsigned long long step_count;
281
282   /**
283    * Timestamp for the last time-step
284    */
285   struct GNUNET_TIME_Absolute step_time_last;
286
287   /**
288    * Task identifier of the next time-step to be executed
289    */
290   GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
291
292   /**
293    * Whether a step is already scheduled
294    */
295   int task_pending;
296
297   /**
298    * Variable discount factor, dependent on time between steps
299    */
300   double discount_variable;
301
302   /**
303    * Integrated variable discount factor, dependent on time between steps
304    */
305   double discount_integrated;
306
307   /**
308    * Lock for bulk operations
309    */
310   int bulk_lock;
311
312   /**
313    * Number of changes during a lock
314    */
315   int bulk_changes;
316
317   /**
318    * Learning parameters
319    */
320   struct RIL_Learning_Parameters parameters;
321
322   /**
323    * Array of networks with global assignment state
324    */
325   struct RIL_Network * network_entries;
326
327   /**
328    * Networks count
329    */
330   unsigned int networks_count;
331
332   /**
333    * List of active peer-agents
334    */
335   struct RIL_Peer_Agent * agents_head;
336   struct RIL_Peer_Agent * agents_tail;
337 };
338
339 /*
340  *  Private functions
341  *  ---------------------------
342  */
343
344 /**
345  * Estimate the current action-value for state s and action a
346  *
347  * @param agent agent performing the estimation
348  * @param state s
349  * @param action a
350  * @return estimation value
351  */
352 static double
353 agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
354 {
355   int i;
356   double result = 0;
357
358   for (i = 0; i < agent->m; i++)
359   {
360     result += state[i] * agent->W[action][i];
361   }
362
363   return result;
364 }
365
366 /**
367  * Decide whether to do exploration (i.e. taking a new action) or exploitation (i.e. taking the
368  * currently estimated best action) in the current step
369  *
370  * @param agent agent performing the step
371  * @return yes, if exploring
372  */
373 static int
374 agent_decide_exploration (struct RIL_Peer_Agent *agent)
375 {
376   //TODO? Future Work: Improve exploration/exploitation trade-off by different mechanisms than e-greedy
377   double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
378       UINT32_MAX) / (double) UINT32_MAX;
379
380   if (r < agent->envi->parameters.explore_ratio)
381   {
382     return GNUNET_YES;
383   }
384   return GNUNET_NO;
385 }
386
387   /**
388    * Get the index of the address in the agent's list.
389    *
390    * @param agent agent handle
391    * @param address address handle
392    * @return the index, starting with zero
393    */
394 static int
395 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
396 {
397   int i;
398   struct RIL_Address_Wrapped *cur;
399
400   i = -1;
401   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
402   {
403     i++;
404     if (cur->address_naked == address)
405     {
406       return i;
407     }
408   }
409
410   return i;
411 }
412
413 /**
414  * Gets the wrapped address from the agent's list
415  *
416  * @param agent agent handle
417  * @param address address handle
418  * @return wrapped address
419  */
420 static struct RIL_Address_Wrapped *
421 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
422 {
423   struct RIL_Address_Wrapped *cur;
424
425   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
426   {
427     if (cur->address_naked == address)
428     {
429       return cur;
430     }
431   }
432
433   return NULL ;
434 }
435
436 /**
437  * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
438  * most reward in the future)
439  *
440  * @param agent agent performing the calculation
441  * @param state the state from which to take the action
442  * @return the action promising most future reward
443  */
444 static int
445 agent_get_action_best (struct RIL_Peer_Agent *agent, double *state)
446 {
447   int i;
448   int max_i = RIL_ACTION_INVALID;
449   double cur_q;
450   double max_q = -DBL_MAX;
451
452   for (i = 0; i < agent->n; i++)
453   {
454     cur_q = agent_estimate_q (agent, state, i);
455     if (cur_q > max_q)
456     {
457       max_q = cur_q;
458       max_i = i;
459     }
460   }
461
462   GNUNET_assert(RIL_ACTION_INVALID != max_i);
463
464   return max_i;
465 }
466
467 /**
468  * Gets any action, to explore the action space from that state
469  *
470  * @param agent agent performing the calculation
471  * @param state the state from which to take the action
472  * @return any action
473  */
474 static int
475 agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state)
476 {
477   // TODO?: Future Work: Choose the action for exploration, which has been explored the least in this state
478   return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n);
479 }
480
481 /**
482  * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
483  *
484  * @param agent the agent performing the update
485  * @param reward the reward received for the last action
486  * @param s_next the new state, the last step got the agent into
487  * @param a_prime the new
488  */
489 static void
490 agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
491 {
492   int i;
493   double delta;
494   double *theta = agent->W[agent->a_old];
495
496   delta = agent->envi->discount_integrated * reward; //reward
497   delta += agent->envi->discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
498   delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
499   for (i = 0; i < agent->m; i++)
500   {
501     theta[i] += agent->envi->parameters.alpha * delta * agent->e[i];
502   }
503 }
504
505 /**
506  * Changes the eligibility trace vector e in various manners:
507  * RIL_E_ACCUMULATE - adds 1 to each component as in accumulating eligibility traces
508  * RIL_E_REPLACE - resets each component to 1 as in replacing traces
509  * RIL_E_SET - multiplies e with discount factor and lambda as in the update rule
510  * RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
511  *
512  * @param agent the agent handle
513  * @param mod the kind of modification
514  */
515 static void
516 agent_modify_eligibility (struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod)
517 {
518   int i;
519   double *e = agent->e;
520
521   for (i = 0; i < agent->m; i++)
522   {
523     switch (mod)
524     {
525     case RIL_E_ACCUMULATE:
526       e[i] += 1;
527       break;
528     case RIL_E_REPLACE:
529       e[i] = 1;
530       break;
531     case RIL_E_SET:
532       e[i] *= agent->envi->discount_variable * agent->envi->parameters.lambda;
533       break;
534     case RIL_E_ZERO:
535       e[i] = 0;
536       break;
537     }
538   }
539 }
540
541 static void
542 ril_inform (struct GAS_RIL_Handle *solver,
543     enum GAS_Solver_Operation op,
544     enum GAS_Solver_Status stat)
545 {
546   if (NULL != solver->plugin_envi->info_cb)
547     solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
548 }
549
550 /**
551  * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
552  * notify ATS of its new decision
553  *
554  * @param solver solver handle
555  * @param agent agent handle
556  * @param new_address the address which is to be used
557  * @param new_bw_in the new amount of inbound bandwidth set for this address
558  * @param new_bw_out the new amount of outbound bandwidth set for this address
559  * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
560  */
561 static void
562 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
563     struct RIL_Peer_Agent *agent,
564     struct ATS_Address *new_address,
565     unsigned long long new_bw_in,
566     unsigned long long new_bw_out,
567     int silent)
568 {
569   int notify = GNUNET_NO;
570
571   LOG(GNUNET_ERROR_TYPE_DEBUG, "    set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
572
573   //address change
574   if (agent->address_inuse != new_address)
575   {
576     if (NULL != agent->address_inuse)
577     {
578       agent->address_inuse->active = GNUNET_NO;
579       agent->address_inuse->assigned_bw_in.value__ = htonl (0);
580       agent->address_inuse->assigned_bw_out.value__ = htonl (0);
581     }
582     if (NULL != new_address)
583     {
584       LOG(GNUNET_ERROR_TYPE_DEBUG, "    set address active: %s\n", agent->is_active ? "yes" : "no");
585       new_address->active = agent->is_active;
586       new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
587       new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
588     }
589     notify |= GNUNET_YES;
590   }
591
592   if (new_address)
593   {
594     //activity change
595     if (new_address->active != agent->is_active)
596     {
597       new_address->active = agent->is_active;
598       notify |= GNUNET_YES;
599     }
600
601     //bw change
602     if (agent->bw_in != new_bw_in)
603     {
604       agent->bw_in = new_bw_in;
605       new_address->assigned_bw_in.value__ = htonl (new_bw_in);
606       notify |= GNUNET_YES;
607     }
608     if (agent->bw_out != new_bw_out)
609     {
610       agent->bw_out = new_bw_out;
611       new_address->assigned_bw_out.value__ = htonl (new_bw_out);
612       notify |= GNUNET_YES;
613     }
614   }
615
616   if (notify && agent->is_active && (GNUNET_NO == silent))
617   {
618     if (new_address)
619     {
620       LOG(GNUNET_ERROR_TYPE_DEBUG, "    envi_set_active_suggestion() notify\n");
621       agent->suggestion_issue = GNUNET_YES;
622       agent->suggestion_address = new_address;
623     }
624     else if (agent->address_inuse)
625     {
626       //disconnect case, no new address
627       GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
628       GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
629       agent->bw_in = 0;
630       agent->bw_out = 0;
631
632       agent->suggestion_issue = GNUNET_YES;
633       agent->suggestion_address = agent->address_inuse;
634     }
635   }
636   agent->address_inuse = new_address;
637 }
638
639 static unsigned long long
640 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
641 {
642   struct RIL_Peer_Agent *cur;
643   struct RIL_Network *net;
644   unsigned long long sum = 0;
645
646   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
647   {
648     if (cur->is_active && cur->address_inuse)
649     {
650       net = cur->address_inuse->solver_information;
651       if (net->type == type)
652       {
653         if (direction_in)
654           sum += cur->bw_in;
655         else
656           sum += cur->bw_out;
657       }
658     }
659   }
660
661   return sum;
662 }
663
664 /**
665  * Allocates a state vector and fills it with the features present
666  * @param solver the solver handle
667  * @param agent the agent handle
668  * @return pointer to the state vector
669  */
670 static double *
671 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
672 {
673   int i;
674   int k;
675   struct RIL_Network net;
676   double *state = GNUNET_malloc (sizeof (double) * agent->m);
677   struct RIL_Address_Wrapped *cur_address;
678   const double *properties;
679
680   for (i = 0; i < solver->networks_count; i++)
681   {
682     net = solver->network_entries[i];
683     state[i * RIL_FEATURES_NETWORK_COUNT + 0] = (double) ril_network_get_assigned(solver, net.type, GNUNET_YES);
684     state[i * RIL_FEATURES_NETWORK_COUNT + 1] = (double) net.bw_in_available;
685     state[i * RIL_FEATURES_NETWORK_COUNT + 2] = (double) ril_network_get_assigned(solver, net.type, GNUNET_NO);
686     state[i * RIL_FEATURES_NETWORK_COUNT + 3] = (double) net.bw_out_available;
687   }
688
689   i = i * RIL_FEATURES_NETWORK_COUNT; //first address feature
690
691   for (cur_address = agent->addresses_head; NULL != cur_address; cur_address = cur_address->next)
692   {
693     state[i++] = cur_address->address_naked->active;
694     state[i++] = cur_address->address_naked->active ? agent->bw_in : 0;
695     state[i++] = cur_address->address_naked->active ? agent->bw_out : 0;
696     properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
697         cur_address->address_naked);
698     for (k = 0; k < GNUNET_ATS_QualityPropertiesCount; k++)
699     {
700       state[i++] = properties[k];
701     }
702   }
703
704   return state;
705 }
706
707 /**
708  * For all networks a peer has an address in, this gets the maximum bandwidth which could
709  * theoretically be available in one of the networks. This is used for bandwidth normalization.
710  *
711  * @param agent the agent handle
712  * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
713  */
714 static long long unsigned
715 ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
716 {
717   /*
718    * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
719    * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
720    */
721   int max = 0;
722   struct RIL_Address_Wrapped *cur;
723   struct RIL_Network *net;
724
725   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
726   {
727     net = cur->address_naked->solver_information;
728     if (direction_in)
729     {
730       if (net->bw_in_available > max)
731       {
732         max = net->bw_in_available;
733       }
734     }
735     else
736     {
737       if (net->bw_out_available > max)
738       {
739         max = net->bw_out_available;
740       }
741     }
742   }
743   return max;
744 }
745
746 /**
747  * Get the index of the quality-property in question
748  *
749  * @param type the quality property type
750  * @return the index
751  */
752 static int
753 ril_find_property_index (uint32_t type)
754 {
755   int existing_types[] = GNUNET_ATS_QualityProperties;
756   int c;
757   for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
758     if (existing_types[c] == type)
759       return c;
760   return GNUNET_SYSERR;
761 }
762
763 static int
764 ril_get_atsi (struct ATS_Address *address, uint32_t type)
765 {
766   int c1;
767   GNUNET_assert(NULL != address);
768
769   if ((NULL == address->atsi) || (0 == address->atsi_count))
770     return 0;
771
772   for (c1 = 0; c1 < address->atsi_count; c1++)
773   {
774     if (ntohl (address->atsi[c1].type) == type)
775       return ntohl (address->atsi[c1].value);
776   }
777   return 0;
778 }
779
780 static double
781 envi_reward_global (struct GAS_RIL_Handle *solver)
782 {
783   int i;
784   struct RIL_Network net;
785   unsigned int sum_in_available = 0;
786   unsigned int sum_out_available = 0;
787   unsigned int sum_in_assigned = 0;
788   unsigned int sum_out_assigned = 0;
789   double ratio_in;
790   double ratio_out;
791
792   for (i = 0; i < solver->networks_count; i++)
793   {
794     net = solver->network_entries[i];
795     sum_in_available += net.bw_in_available;
796     sum_in_assigned += ril_network_get_assigned(solver, net.type, GNUNET_YES);
797     sum_out_available += net.bw_out_available;
798     sum_out_assigned += ril_network_get_assigned(solver, net.type, GNUNET_NO);
799   }
800
801   ratio_in = ((double) sum_in_assigned) / ((double) sum_in_available);
802   ratio_out = ((double) sum_out_assigned) / ((double) sum_out_available);
803
804   // global reward in [1,2]
805   return ((ratio_in + ratio_out) / 2) + 1;
806 }
807
808 static double
809 envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
810 {
811   const double *preferences;
812   const double *properties;
813   int prop_index;
814   double pref_match = 0;
815   double bw_norm;
816   double dl_norm;
817
818   preferences = solver->plugin_envi->get_preferences (solver->plugin_envi->get_preference_cls,
819       &agent->peer);
820   properties = solver->plugin_envi->get_property (solver->plugin_envi->get_property_cls,
821       agent->address_inuse);
822
823   // delay in [0,1]
824   prop_index = ril_find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
825   dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
826
827   // utilization in [0,1]
828   bw_norm = ((ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
829       / ril_get_max_bw (agent, GNUNET_YES))
830       + (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
831           / ril_get_max_bw (agent, GNUNET_NO))) / 2;
832
833   // preference matching in [0,4]
834   pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
835   pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * bw_norm);
836
837   // local reward in [1,2]
838   return (pref_match / 4) +1;
839 }
840
841 /**
842  * Gets the reward for the last performed step, which is calculated in equal
843  * parts from the local (the peer specific) and the global (for all peers
844  * identical) reward.
845  *
846  * @param solver the solver handle
847  * @param agent the agent handle
848  * @return the reward
849  */
850 static double
851 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
852 {
853   struct RIL_Network *net;
854   double reward = 0;
855
856   //punish overutilization
857   net = agent->address_inuse->solver_information;
858   if ((ril_network_get_assigned(solver, net->type, GNUNET_YES) > net->bw_in_available)
859       || (ril_network_get_assigned(solver, net->type, GNUNET_NO) > net->bw_out_available))
860   {
861     return 0;
862   }
863
864   reward += envi_reward_global (solver);
865   reward += envi_reward_local (solver, agent);
866   return reward * 0.5;
867 }
868
869 /**
870  * Doubles the bandwidth for the active address
871  *
872  * @param solver solver handle
873  * @param agent agent handle
874  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
875  */
876 static void
877 envi_action_bw_double (struct GAS_RIL_Handle *solver,
878     struct RIL_Peer_Agent *agent,
879     int direction_in)
880 {
881   unsigned long long new_bw;
882
883   if (direction_in)
884   {
885     new_bw = agent->bw_in * 2;
886     if (new_bw < agent->bw_in)
887       new_bw = ULLONG_MAX;
888     envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
889         agent->bw_out, GNUNET_NO);
890   }
891   else
892   {
893     new_bw = agent->bw_out * 2;
894     if (new_bw < agent->bw_out)
895       new_bw = ULLONG_MAX;
896     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
897         new_bw, GNUNET_NO);
898   }
899 }
900
901 /**
902  * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
903  * the minimum bandwidth for a peer, in order to not invoke a disconnect.
904  *
905  * @param solver solver handle
906  * @param agent agent handle
907  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
908  * bandwidth
909  */
910 static void
911 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
912     struct RIL_Peer_Agent *agent,
913     int direction_in)
914 {
915   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
916   unsigned long long new_bw;
917
918   if (direction_in)
919   {
920     new_bw = agent->bw_in / 2;
921     if (new_bw < min_bw || new_bw > agent->bw_in)
922       new_bw = min_bw;
923     envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
924         GNUNET_NO);
925   }
926   else
927   {
928     new_bw = agent->bw_out / 2;
929     if (new_bw < min_bw || new_bw > agent->bw_out)
930       new_bw = min_bw;
931     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
932         GNUNET_NO);
933   }
934 }
935
936 /**
937  * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
938  *
939  * @param solver solver handle
940  * @param agent agent handle
941  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
942  * bandwidth
943  */
944 static void
945 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
946 {
947   unsigned long long new_bw;
948   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
949
950   if (direction_in)
951   {
952     new_bw = agent->bw_in + (5 * min_bw);
953     if (new_bw < agent->bw_in)
954       new_bw = ULLONG_MAX;
955     envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
956         agent->bw_out, GNUNET_NO);
957   }
958   else
959   {
960     new_bw = agent->bw_out + (5 * min_bw);
961     if (new_bw < agent->bw_out)
962       new_bw = ULLONG_MAX;
963     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
964         new_bw, GNUNET_NO);
965   }
966 }
967
968 /**
969  * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
970  * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
971  *
972  * @param solver solver handle
973  * @param agent agent handle
974  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
975  * bandwidth
976  */
977 static void
978 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
979 {
980   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
981   unsigned long long new_bw;
982
983   if (direction_in)
984   {
985     new_bw = agent->bw_in - (5 * min_bw);
986     if (new_bw < min_bw || new_bw > agent->bw_in)
987       new_bw = min_bw;
988     envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
989         GNUNET_NO);
990   }
991   else
992   {
993     new_bw = agent->bw_out - (5 * min_bw);
994     if (new_bw < min_bw || new_bw > agent->bw_in)
995       new_bw = min_bw;
996     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
997         GNUNET_NO);
998   }
999 }
1000
1001 /**
1002  * Switches to the address given by its index
1003  *
1004  * @param solver solver handle
1005  * @param agent agent handle
1006  * @param address_index index of the address as it is saved in the agent's list, starting with zero
1007  */
1008 static void
1009 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1010     struct RIL_Peer_Agent *agent,
1011     unsigned int address_index)
1012 {
1013   struct RIL_Address_Wrapped *cur;
1014   int i = 0;
1015
1016   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1017   {
1018     if (i == address_index)
1019     {
1020       envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1021           GNUNET_NO);
1022       return;
1023     }
1024
1025     i++;
1026   }
1027
1028   //no address with address_index exists, in this case this action should not be callable
1029   GNUNET_assert(GNUNET_NO);
1030 }
1031
1032 /**
1033  * Puts the action into effect by calling the according function
1034  *
1035  * @param solver the solver handle
1036  * @param agent the action handle
1037  * @param action the action to perform by the solver
1038  */
1039 static void
1040 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1041 {
1042   int address_index;
1043
1044   switch (action)
1045   {
1046   case RIL_ACTION_NOTHING:
1047     break;
1048   case RIL_ACTION_BW_IN_DBL:
1049     envi_action_bw_double (solver, agent, GNUNET_YES);
1050     break;
1051   case RIL_ACTION_BW_IN_HLV:
1052     envi_action_bw_halven (solver, agent, GNUNET_YES);
1053     break;
1054   case RIL_ACTION_BW_IN_INC:
1055     envi_action_bw_inc (solver, agent, GNUNET_YES);
1056     break;
1057   case RIL_ACTION_BW_IN_DEC:
1058     envi_action_bw_dec (solver, agent, GNUNET_YES);
1059     break;
1060   case RIL_ACTION_BW_OUT_DBL:
1061     envi_action_bw_double (solver, agent, GNUNET_NO);
1062     break;
1063   case RIL_ACTION_BW_OUT_HLV:
1064     envi_action_bw_halven (solver, agent, GNUNET_NO);
1065     break;
1066   case RIL_ACTION_BW_OUT_INC:
1067     envi_action_bw_inc (solver, agent, GNUNET_NO);
1068     break;
1069   case RIL_ACTION_BW_OUT_DEC:
1070     envi_action_bw_dec (solver, agent, GNUNET_NO);
1071     break;
1072   default:
1073     if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1074     {
1075       address_index = action - RIL_ACTION_TYPE_NUM;
1076
1077       GNUNET_assert(address_index >= 0);
1078       GNUNET_assert(
1079           address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1080
1081       envi_action_address_switch (solver, agent, address_index);
1082       break;
1083     }
1084     // error - action does not exist
1085     GNUNET_assert(GNUNET_NO);
1086   }
1087 }
1088
1089 /**
1090  * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1091  * after having done the last action a_old. It observes the new state s_next and the reward
1092  * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1093  * next action is put into effect.
1094  *
1095  * @param agent the agent performing the step
1096  */
1097 static void
1098 agent_step (struct RIL_Peer_Agent *agent)
1099 {
1100   int a_next = RIL_ACTION_INVALID;
1101   double *s_next;
1102   double reward;
1103
1104   LOG(GNUNET_ERROR_TYPE_DEBUG, "    agent_step() Peer '%s', algorithm %s\n",
1105       GNUNET_i2s (&agent->peer),
1106       agent->envi->parameters.algorithm ? "Q" : "SARSA");
1107
1108   s_next = envi_get_state (agent->envi, agent);
1109   reward = envi_get_reward (agent->envi, agent);
1110
1111   switch (agent->envi->parameters.algorithm)
1112   {
1113   case RIL_ALGO_SARSA:
1114     agent_modify_eligibility (agent, RIL_E_SET);
1115     if (agent_decide_exploration (agent))
1116     {
1117       a_next = agent_get_action_explore (agent, s_next);
1118     }
1119     else
1120     {
1121       a_next = agent_get_action_best (agent, s_next);
1122     }
1123     if (RIL_ACTION_INVALID != agent->a_old)
1124     {
1125       //updates weights with selected action (on-policy), if not first step
1126       agent_update_weights (agent, reward, s_next, a_next);
1127     }
1128     break;
1129
1130   case RIL_ALGO_Q:
1131     a_next = agent_get_action_best (agent, s_next);
1132     if (RIL_ACTION_INVALID != agent->a_old)
1133     {
1134       //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1135       agent_update_weights (agent, reward, s_next, a_next);
1136     }
1137     if (agent_decide_exploration (agent))
1138     {
1139       a_next = agent_get_action_explore (agent, s_next);
1140       agent_modify_eligibility (agent, RIL_E_ZERO);
1141     }
1142     else
1143     {
1144       a_next = agent_get_action_best (agent, s_next);
1145       agent_modify_eligibility (agent, RIL_E_SET);
1146     }
1147     break;
1148   }
1149
1150   GNUNET_assert(RIL_ACTION_INVALID != a_next);
1151
1152   agent_modify_eligibility (agent, RIL_E_ACCUMULATE);
1153
1154   envi_do_action (agent->envi, agent, a_next);
1155
1156   GNUNET_free(agent->s_old);
1157   agent->s_old = s_next;
1158   agent->a_old = a_next;
1159
1160   agent->step_count += 1;
1161 }
1162
1163 static int
1164 ril_step (struct GAS_RIL_Handle *solver);
1165
1166 /**
1167  * Task for the scheduler, which performs one step and lets the solver know that
1168  * no further step is scheduled.
1169  *
1170  * @param cls the solver handle
1171  * @param tc the task context for the scheduler
1172  */
1173 static void
1174 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1175 {
1176   struct GAS_RIL_Handle *solver = cls;
1177
1178   solver->task_pending = GNUNET_NO;
1179   ril_step (solver);
1180 }
1181
1182 static double
1183 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1184 {
1185   int i;
1186   struct RIL_Network net;
1187   unsigned long long sum_assigned = 0;
1188   unsigned long long sum_available = 0;
1189   double ratio;
1190
1191   for (i = 0; i < solver->networks_count; i++)
1192   {
1193     net = solver->network_entries[i];
1194     if (ril_network_get_assigned(solver, net.type, GNUNET_YES) > 0) //only consider scopes with an active address
1195     {
1196       sum_assigned += ril_network_get_assigned(solver, net.type, GNUNET_YES);
1197       sum_assigned += ril_network_get_assigned(solver, net.type, GNUNET_NO);
1198       sum_available += net.bw_in_available;
1199       sum_available += net.bw_out_available;
1200     }
1201   }
1202   if (sum_available > 0)
1203   {
1204     ratio = ((double) sum_assigned) / ((double) sum_available);
1205   }
1206   else
1207   {
1208     ratio = 0;
1209   }
1210
1211   return ratio > 1 ? 1 : ratio; //overutilization possible, cap at 1
1212 }
1213
1214 /**
1215  * Schedules the next global step in an adaptive way. The more resources are
1216  * left, the earlier the next step is scheduled. This serves the reactivity of
1217  * the solver to changed inputs.
1218  *
1219  * @param solver the solver handle
1220  */
1221 static void
1222 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1223 {
1224   double used_ratio;
1225   double factor;
1226   double y;
1227   double offset;
1228   struct GNUNET_TIME_Relative time_next;
1229
1230   if (solver->task_pending)
1231   {
1232     GNUNET_SCHEDULER_cancel (solver->step_next_task_id);
1233   }
1234
1235   used_ratio = ril_get_used_resource_ratio (solver);
1236
1237   GNUNET_assert(
1238       solver->parameters.step_time_min.rel_value_us
1239           < solver->parameters.step_time_max.rel_value_us);
1240
1241   factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1242       solver->parameters.step_time_min).rel_value_us;
1243   offset = (double) solver->parameters.step_time_min.rel_value_us;
1244   y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1245
1246   GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1247   GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1248
1249   time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned int) y);
1250
1251   solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1252       solver);
1253   solver->task_pending = GNUNET_YES;
1254 }
1255
1256 /**
1257  * Triggers one step per agent
1258  * @param solver
1259  */
1260 static int
1261 ril_step (struct GAS_RIL_Handle *solver)
1262 {
1263   struct RIL_Peer_Agent *cur;
1264   struct GNUNET_TIME_Absolute time_now;
1265   struct GNUNET_TIME_Relative time_delta;
1266   double tau;
1267
1268   if (GNUNET_YES == solver->bulk_lock)
1269   {
1270     solver->bulk_changes++;
1271     return GNUNET_NO;
1272   }
1273
1274   ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1275
1276   LOG(GNUNET_ERROR_TYPE_DEBUG, "    RIL step number %d\n", solver->step_count);
1277
1278   if (0 == solver->step_count)
1279   {
1280     solver->step_time_last = GNUNET_TIME_absolute_get ();
1281   }
1282
1283   //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1284   time_now = GNUNET_TIME_absolute_get ();
1285   time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1286   tau = ((double) time_delta.rel_value_us)
1287       / ((double) solver->parameters.step_time_min.rel_value_us);
1288   solver->step_time_last = time_now;
1289
1290   //calculate reward discounts (once per step for all agents)
1291   solver->discount_variable = pow (M_E, ((-1.) * ((double) solver->parameters.beta) * tau));
1292   solver->discount_integrated = (1 - solver->discount_variable)
1293       / ((double) solver->parameters.beta);
1294
1295   //trigger one step per active agent
1296   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1297   {
1298     if (cur->is_active && cur->address_inuse)
1299     {
1300       agent_step (cur);
1301     }
1302   }
1303
1304   solver->step_count += 1;
1305   ril_step_schedule_next (solver);
1306
1307   ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1308
1309   ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1310   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1311   {
1312     if (cur->suggestion_issue) {
1313       solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1314       LOG(GNUNET_ERROR_TYPE_DEBUG, "\n    Suggest address: %s\n\n",
1315                             cur->suggestion_address->addr);
1316       cur->suggestion_issue = GNUNET_NO;
1317     }
1318   }
1319   ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1320
1321   return GNUNET_YES;
1322 }
1323
1324 static int
1325 ril_count_agents (struct GAS_RIL_Handle *solver)
1326 {
1327   int c = 0;
1328   struct RIL_Peer_Agent *cur_agent;
1329
1330   for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1331   {
1332     c++;
1333   }
1334   return c;
1335 }
1336
1337 static void
1338 agent_w_start (struct RIL_Peer_Agent *agent)
1339 {
1340   int count;
1341   struct RIL_Peer_Agent *other;
1342   int i;
1343   int k;
1344
1345   count = ril_count_agents(agent->envi);
1346
1347   if (0 == count)
1348     return;
1349
1350   for (other = agent->envi->agents_head; NULL != other; other = other->next)
1351   {
1352     for (i = 0; i < agent->n; i++)
1353     {
1354       for (k = 0; k < agent->m; k++)
1355       {
1356         agent->W[i][k] += (other->W[i][k] / (double) count);
1357       }
1358     }
1359   }
1360 }
1361
1362 /**
1363  * Initialize an agent without addresses and its knowledge base
1364  *
1365  * @param s ril solver
1366  * @param peer the one in question
1367  * @return handle to the new agent
1368  */
1369 static struct RIL_Peer_Agent *
1370 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1371 {
1372   int i;
1373   struct GAS_RIL_Handle * solver = s;
1374   struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent));
1375
1376   agent->envi = solver;
1377   agent->peer = *peer;
1378   agent->step_count = 0;
1379   agent->is_active = GNUNET_NO;
1380   agent->bw_in = 0;
1381   agent->bw_out = 0;
1382   agent->suggestion_issue = GNUNET_NO;
1383   agent->n = RIL_ACTION_TYPE_NUM;
1384   agent->m = solver->networks_count * RIL_FEATURES_NETWORK_COUNT;
1385   agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1386   for (i = 0; i < agent->n; i++)
1387   {
1388     agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1389   }
1390   agent_w_start(agent);
1391   agent->a_old = RIL_ACTION_INVALID;
1392   agent->s_old = envi_get_state (solver, agent);
1393   agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1394   agent_modify_eligibility (agent, RIL_E_ZERO);
1395
1396   GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, agent);
1397
1398   return agent;
1399 }
1400
1401 /**
1402  * Deallocate agent
1403  *
1404  * @param solver the solver handle
1405  * @param agent the agent to retire
1406  */
1407 static void
1408 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1409 {
1410   int i;
1411
1412   for (i = 0; i < agent->n; i++)
1413   {
1414     GNUNET_free(agent->W[i]);
1415   }
1416   GNUNET_free(agent->W);
1417   GNUNET_free(agent->e);
1418   GNUNET_free(agent->s_old);
1419   GNUNET_free(agent);
1420 }
1421
1422 /**
1423  * Returns the agent for a peer
1424  *
1425  * @param solver the solver handle
1426  * @param peer the identity of the peer
1427  * @param create whether or not to create an agent, if none is allocated yet
1428  * @return the agent
1429  */
1430 static struct RIL_Peer_Agent *
1431 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1432 {
1433   struct RIL_Peer_Agent *cur;
1434
1435   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1436   {
1437     if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1438     {
1439       return cur;
1440     }
1441   }
1442
1443   if (create)
1444   {
1445     return agent_init (solver, peer);
1446   }
1447   return NULL ;
1448 }
1449
1450 /**
1451  * Lookup network struct by type
1452  *
1453  * @param s the solver handle
1454  * @param type the network type
1455  * @return the network struct
1456  */
1457 static struct RIL_Network *
1458 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1459 {
1460   int i;
1461
1462   for (i = 0; i < s->networks_count; i++)
1463   {
1464     if (s->network_entries[i].type == type)
1465     {
1466       return &s->network_entries[i];
1467     }
1468   }
1469   return NULL ;
1470 }
1471
1472 /**
1473  * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1474  * considered inactive and not used. Addresses in an inactive network are ignored.
1475  *
1476  * @param solver solver handle
1477  * @param network the network type
1478  * @return whether or not the network is considered active
1479  */
1480 static int
1481 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1482 {
1483   struct RIL_Network *net;
1484   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1485
1486   net = ril_get_network (solver, network);
1487   if (net->bw_out_available < min_bw)
1488     return GNUNET_NO;
1489   return GNUNET_YES;
1490 }
1491
1492 /**
1493  * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1494  * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1495  * frees the memory of the old vector, and redirects the pointer to the new one.
1496  *
1497  * @param old pointer to the pointer to the first element of the vector
1498  * @param element_size byte size of the vector elements
1499  * @param hole_start the first element to cut out
1500  * @param hole_length the number of elements to cut out
1501  * @param old_length the length of the old vector
1502  */
1503 static void
1504 ril_cut_from_vector (void **old,
1505     size_t element_size,
1506     unsigned int hole_start,
1507     unsigned int hole_length,
1508     unsigned int old_length)
1509 {
1510   char *tmpptr;
1511   char *oldptr = (char *) *old;
1512   size_t size;
1513   unsigned int bytes_before;
1514   unsigned int bytes_hole;
1515   unsigned int bytes_after;
1516
1517   GNUNET_assert(old_length > hole_length);
1518   GNUNET_assert(old_length >= (hole_start + hole_length));
1519
1520   size = element_size * (old_length - hole_length);
1521
1522   bytes_before = element_size * hole_start;
1523   bytes_hole = element_size * hole_length;
1524   bytes_after = element_size * (old_length - hole_start - hole_length);
1525
1526   if (0 == size)
1527   {
1528     tmpptr = NULL;
1529   }
1530   else
1531   {
1532     tmpptr = GNUNET_malloc (size);
1533     memcpy (tmpptr, oldptr, bytes_before);
1534     memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
1535   }
1536   if (NULL != *old)
1537   {
1538     GNUNET_free(*old);
1539   }
1540   *old = (void *) tmpptr;
1541 }
1542
1543 /*
1544  *  Solver API functions
1545  *  ---------------------------
1546  */
1547
1548 /**
1549  * Change relative preference for quality in solver
1550  *
1551  * @param solver the solver handle
1552  * @param peer the peer to change the preference for
1553  * @param kind the kind to change the preference
1554  * @param pref_rel the normalized preference value for this kind over all clients
1555  */
1556 void
1557 GAS_ril_address_change_preference (void *solver,
1558     const struct GNUNET_PeerIdentity *peer,
1559     enum GNUNET_ATS_PreferenceKind kind,
1560     double pref_rel)
1561 {
1562   LOG(GNUNET_ERROR_TYPE_DEBUG,
1563       "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
1564       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
1565
1566   ril_step (solver);
1567 }
1568
1569 /**
1570  * Entry point for the plugin
1571  *
1572  * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
1573  */
1574 void *
1575 libgnunet_plugin_ats_ril_init (void *cls)
1576 {
1577   struct GNUNET_ATS_PluginEnvironment *env = cls;
1578   struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
1579   struct RIL_Network * cur;
1580   int c;
1581   char *string;
1582
1583   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
1584
1585   GNUNET_assert(NULL != env);
1586   GNUNET_assert(NULL != env->cfg);
1587   GNUNET_assert(NULL != env->stats);
1588   GNUNET_assert(NULL != env->bandwidth_changed_cb);
1589   GNUNET_assert(NULL != env->get_preferences);
1590   GNUNET_assert(NULL != env->get_property);
1591
1592   if (GNUNET_OK
1593       != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
1594           &solver->parameters.step_time_min))
1595   {
1596     solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
1597   }
1598   if (GNUNET_OK
1599       != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
1600           &solver->parameters.step_time_max))
1601   {
1602     solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
1603   }
1604   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
1605   {
1606     solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
1607     GNUNET_free (string);
1608   }
1609   else
1610   {
1611     solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
1612   }
1613   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
1614   {
1615     solver->parameters.beta = strtod (string, NULL);
1616     GNUNET_free (string);
1617   }
1618   else
1619   {
1620     solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
1621   }
1622   if (GNUNET_OK
1623       == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
1624   {
1625     solver->parameters.alpha = strtod (string, NULL);
1626     GNUNET_free (string);
1627   }
1628   else
1629   {
1630     solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
1631   }
1632   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
1633   {
1634     solver->parameters.lambda = strtod (string, NULL);
1635     GNUNET_free (string);
1636   }
1637   else
1638   {
1639     solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
1640   }
1641   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
1642   {
1643     solver->parameters.explore_ratio = strtod (string, NULL);
1644     GNUNET_free (string);
1645   }
1646   else
1647   {
1648     solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
1649   }
1650
1651   env->sf.s_add = &GAS_ril_address_add;
1652   env->sf.s_address_update_property = &GAS_ril_address_property_changed;
1653   env->sf.s_address_update_session = &GAS_ril_address_session_changed;
1654   env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
1655   env->sf.s_address_update_network = &GAS_ril_address_change_network;
1656   env->sf.s_get = &GAS_ril_get_preferred_address;
1657   env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
1658   env->sf.s_pref = &GAS_ril_address_change_preference;
1659   env->sf.s_feedback = &GAS_ril_address_preference_feedback;
1660   env->sf.s_del = &GAS_ril_address_delete;
1661   env->sf.s_bulk_start = &GAS_ril_bulk_start;
1662   env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
1663
1664   solver->plugin_envi = env;
1665   solver->networks_count = env->network_count;
1666   solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Network));
1667   solver->step_count = 0;
1668
1669   for (c = 0; c < env->network_count; c++)
1670   {
1671     cur = &solver->network_entries[c];
1672     cur->type = env->networks[c];
1673     cur->bw_in_available = env->in_quota[c];
1674     cur->bw_out_available = env->out_quota[c];
1675   }
1676
1677   solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (
1678       GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000),
1679       &ril_step_scheduler_task, solver);
1680   solver->task_pending = GNUNET_YES;
1681
1682   return solver;
1683 }
1684
1685 /**
1686  * Exit point for the plugin
1687  *
1688  * @param cls the solver handle
1689  */
1690 void *
1691 libgnunet_plugin_ats_ril_done (void *cls)
1692 {
1693   struct GAS_RIL_Handle *s = cls;
1694   struct RIL_Peer_Agent *cur_agent;
1695   struct RIL_Peer_Agent *next_agent;
1696
1697   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
1698
1699   cur_agent = s->agents_head;
1700   while (NULL != cur_agent)
1701   {
1702     next_agent = cur_agent->next;
1703     GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
1704     agent_die (s, cur_agent);
1705     cur_agent = next_agent;
1706   }
1707
1708   if (s->task_pending)
1709   {
1710     GNUNET_SCHEDULER_cancel (s->step_next_task_id);
1711   }
1712   GNUNET_free(s->network_entries);
1713   GNUNET_free(s);
1714
1715   return NULL ;
1716 }
1717
1718 /**
1719  * Add a new address for a peer to the solver
1720  *
1721  * The address is already contained in the addresses hashmap!
1722  *
1723  * @param solver the solver Handle
1724  * @param address the address to add
1725  * @param network network type of this address
1726  */
1727 void
1728 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
1729 {
1730   struct GAS_RIL_Handle *s = solver;
1731   struct RIL_Peer_Agent *agent;
1732   struct RIL_Address_Wrapped *address_wrapped;
1733   struct RIL_Network *net;
1734   unsigned int m_new;
1735   unsigned int m_old;
1736   unsigned int n_new;
1737   unsigned int n_old;
1738   int i;
1739   unsigned int zero;
1740   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1741
1742   LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
1743
1744   net = ril_get_network (s, network);
1745   address->solver_information = net;
1746
1747   if (!ril_network_is_active (s, network))
1748   {
1749     LOG(GNUNET_ERROR_TYPE_DEBUG,
1750         "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
1751         address->plugin, address->addr, GNUNET_i2s (&address->peer));
1752     return;
1753   }
1754
1755   agent = ril_get_agent (s, &address->peer, GNUNET_YES);
1756
1757   //add address
1758   address_wrapped = GNUNET_malloc (sizeof (struct RIL_Address_Wrapped));
1759   address_wrapped->address_naked = address;
1760   GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
1761
1762   //increase size of W
1763   m_new = agent->m + RIL_FEATURES_ADDRESS_COUNT;
1764   m_old = agent->m;
1765   n_new = agent->n + 1;
1766   n_old = agent->n;
1767
1768   GNUNET_array_grow(agent->W, agent->n, n_new);
1769   for (i = 0; i < n_new; i++)
1770   {
1771     if (i < n_old)
1772     {
1773       agent->m = m_old;
1774       GNUNET_array_grow(agent->W[i], agent->m, m_new);
1775     }
1776     else
1777     {
1778       zero = 0;
1779       GNUNET_array_grow(agent->W[i], zero, m_new);
1780     }
1781   }
1782
1783   //increase size of old state vector
1784   agent->m = m_old;
1785   GNUNET_array_grow(agent->s_old, agent->m, m_new);
1786
1787   agent->m = m_old;
1788   GNUNET_array_grow(agent->e, agent->m, m_new);
1789
1790   if (NULL == agent->address_inuse)
1791   {
1792     envi_set_active_suggestion (s, agent, address, min_bw, min_bw, GNUNET_NO);
1793   }
1794
1795   ril_step (s);
1796
1797   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
1798       address->active ? "active" : "inactive", address->plugin, address->addr,
1799       GNUNET_i2s (&address->peer));
1800 }
1801
1802 /**
1803  * Delete an address in the solver
1804  *
1805  * The address is not contained in the address hashmap anymore!
1806  *
1807  * @param solver the solver handle
1808  * @param address the address to remove
1809  * @param session_only delete only session not whole address
1810  */
1811 void
1812 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
1813 {
1814   struct GAS_RIL_Handle *s = solver;
1815   struct RIL_Peer_Agent *agent;
1816   struct RIL_Address_Wrapped *address_wrapped;
1817   int address_was_used = address->active;
1818   int address_index;
1819   unsigned int m_new;
1820   unsigned int n_new;
1821   int i;
1822   struct RIL_Network *net;
1823   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1824
1825   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
1826       session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
1827       address->addr, GNUNET_i2s (&address->peer));
1828
1829   agent = ril_get_agent (s, &address->peer, GNUNET_NO);
1830   if (NULL == agent)
1831   {
1832     net = address->solver_information;
1833     GNUNET_assert(!ril_network_is_active (s, net->type));
1834     LOG(GNUNET_ERROR_TYPE_DEBUG,
1835         "No agent allocated for peer yet, since address was in inactive network\n");
1836     return;
1837   }
1838
1839   address_index = agent_address_get_index (agent, address);
1840   address_wrapped = agent_address_get (agent, address);
1841
1842   if (NULL == address_wrapped)
1843   {
1844     net = address->solver_information;
1845     GNUNET_assert(!ril_network_is_active (s, net->type));
1846     LOG(GNUNET_ERROR_TYPE_DEBUG,
1847         "Address not considered by agent, address was in inactive network\n");
1848     return;
1849   }
1850
1851   GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
1852   GNUNET_free(address_wrapped);
1853
1854   //decrease W
1855   m_new = agent->m - RIL_FEATURES_ADDRESS_COUNT;
1856   n_new = agent->n - 1;
1857
1858   for (i = 0; i < agent->n; i++)
1859   {
1860     ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
1861         ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1862             + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1863   }
1864   GNUNET_free(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
1865   ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
1866       1, agent->n);
1867   //correct last action
1868   if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
1869   {
1870     agent->a_old -= 1;
1871   }
1872   else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
1873   {
1874     agent->a_old = RIL_ACTION_INVALID;
1875   }
1876   //decrease old state vector and eligibility vector
1877   ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
1878       ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1879           + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1880   ril_cut_from_vector ((void **) &agent->e, sizeof(double),
1881       ((s->networks_count * RIL_FEATURES_NETWORK_COUNT)
1882           + (address_index * RIL_FEATURES_ADDRESS_COUNT)), RIL_FEATURES_ADDRESS_COUNT, agent->m);
1883   agent->m = m_new;
1884   agent->n = n_new;
1885
1886   LOG(GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
1887
1888   if (address_was_used)
1889   {
1890     if (NULL != agent->addresses_head) //if peer has an address left, use it
1891     {
1892       LOG(GNUNET_ERROR_TYPE_DEBUG, "address left: %s\n",
1893           agent->addresses_head->address_naked->addr);
1894
1895       envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw,
1896           GNUNET_NO);
1897     }
1898     else
1899     {
1900       LOG(GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
1901
1902       envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
1903     }
1904   }
1905
1906   LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted: %s\n", address->addr);
1907
1908   ril_step (solver);
1909 }
1910
1911 /**
1912  * Update the properties of an address in the solver
1913  *
1914  * @param solver solver handle
1915  * @param address the address
1916  * @param type the ATSI type in HBO
1917  * @param abs_value the absolute value of the property
1918  * @param rel_value the normalized value
1919  */
1920 void
1921 GAS_ril_address_property_changed (void *solver,
1922     struct ATS_Address *address,
1923     uint32_t type,
1924     uint32_t abs_value,
1925     double rel_value)
1926 {
1927   LOG(GNUNET_ERROR_TYPE_DEBUG,
1928       "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
1929           "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1930       address->addr, rel_value);
1931
1932   ril_step (solver);
1933 }
1934
1935 /**
1936  * Update the session of an address in the solver
1937  *
1938  * NOTE: values in addresses are already updated
1939  *
1940  * @param solver solver handle
1941  * @param address the address
1942  * @param cur_session the current session
1943  * @param new_session the new session
1944  */
1945 void
1946 GAS_ril_address_session_changed (void *solver,
1947     struct ATS_Address *address,
1948     uint32_t cur_session,
1949     uint32_t new_session)
1950 {
1951   /*
1952    * TODO? Future Work: Potentially add session activity as a feature in state vector
1953    */
1954   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
1955 }
1956
1957 /**
1958  * Notify the solver that an address is (not) actively used by transport
1959  * to communicate with a remote peer
1960  *
1961  * NOTE: values in addresses are already updated
1962  *
1963  * @param solver solver handle
1964  * @param address the address
1965  * @param in_use usage state
1966  */
1967 void
1968 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
1969 {
1970   /* Nothing to do here.
1971    * Possible TODO? Future Work: Potentially add usage variable to state vector
1972    */
1973   LOG(GNUNET_ERROR_TYPE_DEBUG,
1974       "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
1975       address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1976 }
1977
1978 /**
1979  * Notify solver that the network an address is located in has changed
1980  *
1981  * NOTE: values in addresses are already updated
1982  *
1983  * @param solver solver handle
1984  * @param address the address
1985  * @param current_network the current network
1986  * @param new_network the new network
1987  */
1988 void
1989 GAS_ril_address_change_network (void *solver,
1990     struct ATS_Address *address,
1991     uint32_t current_network,
1992     uint32_t new_network)
1993 {
1994   struct GAS_RIL_Handle *s = solver;
1995   struct RIL_Peer_Agent *agent;
1996
1997   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
1998       "%s address of peer %s from '%s' to '%s'\n",
1999       (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2000       GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2001
2002   if (address->active && !ril_network_is_active (solver, new_network))
2003   {
2004     GAS_ril_address_delete (solver, address, GNUNET_NO);
2005     return;
2006   }
2007
2008   agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2009   if (NULL == agent)
2010   {
2011     GNUNET_assert(!ril_network_is_active (solver, current_network));
2012
2013     GAS_ril_address_add (s, address, new_network);
2014     return;
2015   }
2016
2017   address->solver_information = ril_get_network(solver, new_network);
2018 }
2019
2020 /**
2021  * Give feedback about the current assignment
2022  *
2023  * @param solver the solver handle
2024  * @param application the application
2025  * @param peer the peer to change the preference for
2026  * @param scope the time interval for this feedback: [now - scope .. now]
2027  * @param kind the kind to change the preference
2028  * @param score the score
2029  */
2030 void
2031 GAS_ril_address_preference_feedback (void *solver,
2032     void *application,
2033     const struct GNUNET_PeerIdentity *peer,
2034     const struct GNUNET_TIME_Relative scope,
2035     enum GNUNET_ATS_PreferenceKind kind,
2036     double score)
2037 {
2038   LOG(GNUNET_ERROR_TYPE_DEBUG,
2039       "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2040           "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2041       GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2042 }
2043
2044 /**
2045  * Start a bulk operation
2046  *
2047  * @param solver the solver
2048  */
2049 void
2050 GAS_ril_bulk_start (void *solver)
2051 {
2052   struct GAS_RIL_Handle *s = solver;
2053
2054   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2055
2056   s->bulk_lock++;
2057 }
2058
2059 /**
2060  * Bulk operation done
2061  *
2062  * @param solver the solver handle
2063  */
2064 void
2065 GAS_ril_bulk_stop (void *solver)
2066 {
2067   struct GAS_RIL_Handle *s = solver;
2068
2069   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2070
2071   if (s->bulk_lock < 1)
2072   {
2073     GNUNET_break(0);
2074     return;
2075   }
2076   s->bulk_lock--;
2077
2078   if (0 < s->bulk_changes)
2079   {
2080     ril_step (solver);
2081     s->bulk_changes = 0;
2082   }
2083 }
2084
2085 /**
2086  * Tell solver to notify ATS if the address to use changes for a specific
2087  * peer using the bandwidth changed callback
2088  *
2089  * The solver must only notify about changes for peers with pending address
2090  * requests!
2091  *
2092  * @param solver the solver handle
2093  * @param peer the identity of the peer
2094  */
2095 const struct ATS_Address *
2096 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2097 {
2098   /*
2099    * activate agent, return currently chosen address
2100    */
2101   struct GAS_RIL_Handle *s = solver;
2102   struct RIL_Peer_Agent *agent;
2103
2104   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2105
2106   agent = ril_get_agent (s, peer, GNUNET_YES);
2107
2108   agent->is_active = GNUNET_YES;
2109
2110   envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2111       GNUNET_YES);
2112
2113   if (agent->address_inuse)
2114   {
2115     LOG(GNUNET_ERROR_TYPE_DEBUG,
2116         "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2117         GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2118   }
2119   else
2120   {
2121     LOG(GNUNET_ERROR_TYPE_DEBUG,
2122         "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2123         GNUNET_i2s (peer));
2124   }
2125
2126   return agent->address_inuse;
2127 }
2128
2129 /**
2130  * Tell solver stop notifying ATS about changes for this peers
2131  *
2132  * The solver must only notify about changes for peers with pending address
2133  * requests!
2134  *
2135  * @param solver the solver handle
2136  * @param peer the peer
2137  */
2138 void
2139 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2140 {
2141   struct GAS_RIL_Handle *s = solver;
2142   struct RIL_Peer_Agent *agent;
2143
2144   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2145
2146   agent = ril_get_agent (s, peer, GNUNET_NO);
2147
2148   if (NULL == agent)
2149   {
2150     GNUNET_break(0);
2151     return;
2152   }
2153   if (GNUNET_NO == agent->is_active)
2154   {
2155     GNUNET_break(0);
2156     return;
2157   }
2158
2159   agent->is_active = GNUNET_NO;
2160
2161   envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2162       GNUNET_YES);
2163
2164   ril_step (s);
2165
2166   LOG(GNUNET_ERROR_TYPE_DEBUG,
2167       "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2168       GNUNET_i2s (peer), agent->address_inuse->plugin);
2169 }
2170
2171 /* end of libgnunet_plugin_ats_ril.c */