several fixes and comments
[oweals/gnunet.git] / src / ats / 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/plugin_ats_ril.c
23  * @brief ATS reinforcement learning solver
24  * @author Fabian Oehlmann
25  * @author Matthias Wachs
26  */
27 #include "plugin_ats_ril.h"
28
29 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
30
31 #define RIL_MIN_BW                      ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)
32 #define RIL_MAX_BW                      1024 * 250 //GNUNET_ATS_MaxBandwidth
33
34 #define RIL_ACTION_INVALID              -1
35 #define RIL_INTERVAL_EXPONENT           10
36 #define RIL_UTILITY_DELAY_MAX           100
37
38 #define RIL_DEFAULT_STEP_TIME_MIN       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
39 #define RIL_DEFAULT_STEP_TIME_MAX       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
40 #define RIL_DEFAULT_ALGORITHM           RIL_ALGO_Q
41 #define RIL_DEFAULT_SELECT              RIL_SELECT_EGREEDY
42 #define RIL_DEFAULT_WELFARE             RIL_WELFARE_EGALITARIAN
43 #define RIL_DEFAULT_DISCOUNT_BETA       1.0
44 #define RIL_DEFAULT_DISCOUNT_GAMMA      0.5
45 #define RIL_DEFAULT_GRADIENT_STEP_SIZE  0.1
46 #define RIL_DEFAULT_TRACE_DECAY         0.5
47 #define RIL_DEFAULT_EXPLORE_RATIO       0.1
48 #define RIL_DEFAULT_RBF_DIVISOR         10
49 #define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5
50 #define RIL_DEFAULT_TEMPERATURE         1.0
51
52 #define RIL_INC_DEC_STEP_SIZE           1
53 #define RIL_NOP_BONUS                   0.5
54
55 /**
56  * ATS reinforcement learning solver
57  *
58  * General description
59  */
60
61 /**
62  * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
63  * changed for the currently chosen address. Not depicted in the enum are the actions of switching
64  * to a particular address. The action of switching to address with index i is depicted by the
65  * number (RIL_ACTION_TYPE_NUM + i).
66  */
67 enum RIL_Action_Type
68 {
69   RIL_ACTION_NOTHING = 0,
70   RIL_ACTION_BW_IN_DBL = -2, //TODO! put actions back
71   RIL_ACTION_BW_IN_HLV = -3,
72   RIL_ACTION_BW_IN_INC = 1,
73   RIL_ACTION_BW_IN_DEC = 2,
74   RIL_ACTION_BW_OUT_DBL = -4,
75   RIL_ACTION_BW_OUT_HLV = -5,
76   RIL_ACTION_BW_OUT_INC = 3,
77   RIL_ACTION_BW_OUT_DEC = 4,
78   RIL_ACTION_TYPE_NUM = 5
79 };
80
81 enum RIL_Algorithm
82 {
83   RIL_ALGO_SARSA = 0,
84   RIL_ALGO_Q = 1
85 };
86
87 enum RIL_Select
88 {
89   RIL_SELECT_SOFTMAX = 0,
90   RIL_SELECT_EGREEDY = 1
91 };
92
93 enum RIL_Welfare
94 {
95   RIL_WELFARE_NASH,
96   RIL_WELFARE_EGALITARIAN
97 };
98
99 enum RIL_E_Modification
100 {
101   RIL_E_DISCOUNT,
102   RIL_E_ZERO,
103   RIL_E_ACCUMULATE,
104   RIL_E_REPLACE
105 };
106
107 /**
108  * Global learning parameters
109  */
110 struct RIL_Learning_Parameters
111 {
112   /**
113    * The TD-algorithm to use
114    */
115   enum RIL_Algorithm algorithm;
116
117   /**
118    * Gradient-descent step-size
119    */
120   double alpha;
121
122   /**
123    * Learning discount variable in the TD-update for semi-MDPs
124    */
125   double beta;
126
127   /**
128    * Learning discount factor in the TD-update for MDPs
129    */
130   double gamma;
131
132   /**
133    * Trace-decay factor for eligibility traces
134    */
135   double lambda;
136
137   /**
138    * Whether to accumulate or replace eligibility traces
139    */
140   enum RIL_E_Modification eligibility_trace_mode;
141
142   /**
143    * Softmax action-selection temperature
144    */
145   double temperature;
146
147   /**
148    * Which measure of social welfare should be used
149    */
150   enum RIL_Welfare social_welfare;
151
152   /**
153    * State space divisor
154    */
155   unsigned long long rbf_divisor;
156
157   /**
158    * Action selection strategy;
159    */
160   enum RIL_Select select;
161
162   /**
163    * Ratio, with what probability an agent should explore in the e-greed policy
164    */
165   double explore_ratio;
166
167   /**
168    * How big the share of the global part of the reward signal is
169    */
170   double reward_global_share;
171
172   /**
173    * Minimal interval time between steps in milliseconds
174    */
175   struct GNUNET_TIME_Relative step_time_min;
176
177   /**
178    * Maximum interval time between steps in milliseconds
179    */
180   struct GNUNET_TIME_Relative step_time_max;
181 };
182
183 /**
184  * Wrapper for addresses to store them in agent's linked list
185  */
186 struct RIL_Address_Wrapped
187 {
188   /**
189    * Next in DLL
190    */
191   struct RIL_Address_Wrapped *next;
192
193   /**
194    * Previous in DLL
195    */
196   struct RIL_Address_Wrapped *prev;
197
198   /**
199    * The address
200    */
201   struct ATS_Address *address_naked;
202 };
203
204 struct RIL_Peer_Agent
205 {
206   /**
207    * Next agent in solver's linked list
208    */
209   struct RIL_Peer_Agent *next;
210
211   /**
212    * Previous agent in solver's linked list
213    */
214   struct RIL_Peer_Agent *prev;
215
216   /**
217    * Environment handle
218    */
219   struct GAS_RIL_Handle *envi;
220
221   /**
222    * Peer ID
223    */
224   struct GNUNET_PeerIdentity peer;
225
226   /**
227    * Whether the agent is active or not
228    */
229   int is_active;
230
231   /**
232    * Number of performed time-steps
233    */
234   unsigned long long step_count;
235
236   /**
237    * Experience matrix W
238    */
239   double ** W;
240
241   /**
242    * Number of rows of W / Number of state-vector features
243    */
244   unsigned int m;
245
246   /**
247    * Number of columns of W / Number of actions
248    */
249   unsigned int n;
250
251   /**
252    * Last perceived state feature vector
253    */
254   double * s_old;
255
256   /**
257    * Last chosen action
258    */
259   int a_old;
260
261   /**
262    * Eligibility traces
263    */
264   double ** E;
265
266   /**
267    * Whether to reset the eligibility traces to 0 after a Q-exploration step
268    */
269   int eligibility_reset;
270
271   /**
272    * Address in use
273    */
274   struct ATS_Address * address_inuse;
275
276   /**
277    * Head of addresses DLL
278    */
279   struct RIL_Address_Wrapped * addresses_head;
280
281   /**
282    * Tail of addresses DLL
283    */
284   struct RIL_Address_Wrapped * addresses_tail;
285
286   /**
287    * Inbound bandwidth assigned by the agent
288    */
289   unsigned long long bw_in;
290
291   /**
292    * Outbound bandwidth assigned by the agent
293    */
294   unsigned long long bw_out;
295
296   /**
297    * Flag whether a suggestion has to be issued
298    */
299   int suggestion_issue;
300
301   /**
302    * The address which has to be issued
303    */
304   struct ATS_Address * suggestion_address;
305
306   /**
307    * The agent's last objective value
308    */
309   double objective_old;
310
311   /**
312    * NOP bonus
313    */
314   double nop_bonus;
315 };
316
317 struct RIL_Scope
318 {
319   /**
320    * ATS network type
321    */
322   enum GNUNET_ATS_Network_Type type;
323
324   /**
325    * Total available inbound bandwidth
326    */
327   unsigned long long bw_in_available;
328
329   /**
330    * Bandwidth inbound assigned in network after last step
331    */
332   unsigned long long bw_in_assigned;
333
334   /**
335    * Bandwidth inbound actually utilized in the network
336    */
337   unsigned long long bw_in_utilized;
338
339   /**
340    * Total available outbound bandwidth
341    */
342   unsigned long long bw_out_available;
343
344   /**
345    * Bandwidth outbound assigned in network after last step
346    */
347   unsigned long long bw_out_assigned;
348
349   /**
350    * Bandwidth outbound actually utilized in the network
351    */
352   unsigned long long bw_out_utilized;
353
354   /**
355    * Number of active agents in scope
356    */
357   unsigned int agent_count;
358
359   /**
360    * The social welfare achieved in the scope
361    */
362   double social_welfare;
363 };
364
365 /**
366  * A handle for the reinforcement learning solver
367  */
368 struct GAS_RIL_Handle
369 {
370   /**
371    * The solver-plugin environment of the solver-plugin API
372    */
373   struct GNUNET_ATS_PluginEnvironment *plugin_envi;
374
375   /**
376    * Statistics handle
377    */
378   struct GNUNET_STATISTICS_Handle *stats;
379
380   /**
381    * Number of performed steps
382    */
383   unsigned long long step_count;
384
385   /**
386    * Timestamp for the last time-step
387    */
388   struct GNUNET_TIME_Absolute step_time_last;
389
390   /**
391    * Task identifier of the next time-step to be executed
392    */
393   GNUNET_SCHEDULER_TaskIdentifier step_next_task_id;
394
395   /**
396    * Variable discount factor, dependent on time between steps
397    */
398   double global_discount_variable;
399
400   /**
401    * Integrated variable discount factor, dependent on time between steps
402    */
403   double global_discount_integrated;
404
405   /**
406    * Lock for bulk operations
407    */
408   int bulk_lock;
409
410   /**
411    * Number of changes during a lock
412    */
413   int bulk_changes;
414
415   /**
416    * Learning parameters
417    */
418   struct RIL_Learning_Parameters parameters;
419
420   /**
421    * Array of networks with global assignment state
422    */
423   struct RIL_Scope * network_entries;
424
425   /**
426    * Networks count
427    */
428   unsigned int networks_count;
429
430   /**
431    * List of active peer-agents
432    */
433   struct RIL_Peer_Agent * agents_head;
434   struct RIL_Peer_Agent * agents_tail;
435
436   /**
437    * Shutdown
438    */
439   int done;
440
441   /**
442    * Simulate steps, i.e. schedule steps immediately
443    */
444   unsigned long long simulate;
445 };
446
447 /*
448  *  "Private" functions
449  *  ---------------------------
450  */
451
452 /**
453  * Estimate the current action-value for state s and action a
454  *
455  * @param agent agent performing the estimation
456  * @param state s
457  * @param action a
458  * @return estimation value
459  */
460 static double
461 agent_q (struct RIL_Peer_Agent *agent, double *state, int action)
462 {
463   int i;
464   double result = 0;
465
466   for (i = 0; i < agent->m; i++)
467   {
468     result += state[i] * agent->W[action][i];
469   }
470
471   GNUNET_assert(!isnan(result));
472
473   //prevent crash when learning diverges
474   if (isinf(result))
475   {
476     return isinf(result) * UINT32_MAX;
477   }
478   return result;
479 }
480
481
482 /**
483  * Get the index of the address in the agent's list.
484  *
485  * @param agent agent handle
486  * @param address address handle
487  * @return the index, starting with zero
488  */
489 static int
490 agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
491 {
492   int i;
493   struct RIL_Address_Wrapped *cur;
494
495   i = -1;
496   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
497   {
498     i++;
499     if (cur->address_naked == address)
500       return i;
501   }
502   return i;
503 }
504
505
506 /**
507  * Gets the wrapped address from the agent's list
508  *
509  * @param agent agent handle
510  * @param address address handle
511  * @return wrapped address
512  */
513 static struct RIL_Address_Wrapped *
514 agent_address_get (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
515 {
516   struct RIL_Address_Wrapped *cur;
517
518   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
519     if (cur->address_naked == address)
520       return cur;
521   return NULL;
522 }
523
524
525 static int
526 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
527 {
528   int address_index;
529
530   switch (action)
531   {
532   case RIL_ACTION_NOTHING:
533     return GNUNET_YES;
534     break;
535   case RIL_ACTION_BW_IN_INC:
536   case RIL_ACTION_BW_IN_DBL:
537     if (agent->bw_in >= RIL_MAX_BW)
538       return GNUNET_NO;
539     else
540       return GNUNET_YES;
541     break;
542   case RIL_ACTION_BW_IN_DEC:
543   case RIL_ACTION_BW_IN_HLV:
544     if (agent->bw_in <= RIL_MIN_BW)
545       return GNUNET_NO;
546     else
547       return GNUNET_YES;
548     break;
549   case RIL_ACTION_BW_OUT_INC:
550   case RIL_ACTION_BW_OUT_DBL:
551     if (agent->bw_out >= RIL_MAX_BW)
552       return GNUNET_NO;
553     else
554       return GNUNET_YES;
555     break;
556   case RIL_ACTION_BW_OUT_DEC:
557   case RIL_ACTION_BW_OUT_HLV:
558     if (agent->bw_out <= RIL_MIN_BW)
559       return GNUNET_NO;
560     else
561       return GNUNET_YES;
562     break;
563   default:
564     if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
565     {
566       address_index = action - RIL_ACTION_TYPE_NUM;
567
568       GNUNET_assert(address_index >= 0);
569       GNUNET_assert(
570           address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
571
572       if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
573           agent->address_inuse->active)
574         return GNUNET_NO;
575       else
576         return GNUNET_YES;
577       break;
578     }
579     // error - action does not exist
580     GNUNET_assert(GNUNET_NO);
581   }
582 }
583
584
585 /**
586  * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
587  * most reward in the future)
588  *
589  * @param agent agent performing the calculation
590  * @param state the state from which to take the action
591  * @return the action promising most future reward
592  */
593 static int
594 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
595 {
596   int i;
597   int max_i = RIL_ACTION_INVALID;
598   double cur_q;
599   double max_q = -DBL_MAX;
600
601   for (i = 0; i < agent->n; i++)
602   {
603     if (agent_action_is_possible(agent, i))
604     {
605       cur_q = agent_q (agent, state, i);
606       if (cur_q > max_q)
607       {
608         max_q = cur_q;
609         max_i = i;
610       }
611     }
612   }
613
614   GNUNET_assert(RIL_ACTION_INVALID != max_i);
615
616   return max_i;
617 }
618
619 /**
620  * Chooses a random action from the set of possible ones
621  *
622  * @param agent the agent performing the action
623  * @return the action index
624  */
625 static int
626 agent_get_action_random (struct RIL_Peer_Agent *agent)
627 {
628   int i;
629   int is_possible[agent->n];
630   int sum = 0;
631   int r;
632
633   for (i = 0; i<agent->n; i++)
634   {
635     if (agent_action_is_possible(agent, i))
636     {
637       is_possible[i] = GNUNET_YES;
638       sum++;
639     }
640     else
641     {
642       is_possible[i] = GNUNET_NO;
643     }
644   }
645
646   r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
647
648   sum = -1;
649   for (i = 0; i<agent->n; i++)
650   {
651     if (is_possible[i])
652     {
653       sum++;
654       if (sum == r)
655         return i;
656     }
657   }
658
659   GNUNET_assert(GNUNET_NO);
660 }
661
662
663 /**
664  * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
665  *
666  * @param agent the agent performing the update
667  * @param reward the reward received for the last action
668  * @param s_next the new state, the last step got the agent into
669  * @param a_prime the new
670  */
671 static void
672 agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
673 {
674   int i;
675   double delta;
676   double *theta = agent->W[agent->a_old];
677
678   delta = agent->envi->global_discount_integrated * reward; //reward
679   delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
680   delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
681
682 //  LOG(GNUNET_ERROR_TYPE_INFO, "update()   Step# %llu  Q(s,a): %f  a: %f  r: %f  y: %f  Q(s+1,a+1) = %f  delta: %f\n",
683 //      agent->step_count,
684 //      agent_q (agent, agent->s_old, agent->a_old),
685 //      agent->envi->parameters.alpha,
686 //      reward,
687 //      agent->envi->global_discount_variable,
688 //      agent_q (agent, s_next, a_prime),
689 //      delta);
690
691   for (i = 0; i < agent->m; i++)
692   {
693 //    LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f   delta = %f   e[%d] = %f\n",
694 //        agent->envi->parameters.alpha,
695 //        delta,
696 //        i,
697 //        agent->e[i]);
698     theta[i] += agent->envi->parameters.alpha * delta * agent->E[agent->a_old][i];
699   }
700 }
701
702
703 /**
704  * Changes the eligibility trace vector e in various manners:
705  * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
706  * #RIL_E_REPLACE - resets each component to @a feature  as in replacing traces
707  * #RIL_E_DISCOUNT - multiplies e with discount factor and lambda as in the update rule
708  * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
709  *
710  * @param agent the agent handle
711  * @param mod the kind of modification
712  * @param feature the feature vector
713  */
714 static void
715 agent_modify_eligibility (struct RIL_Peer_Agent *agent,
716                           enum RIL_E_Modification mod,
717                           double *feature,
718                           int action)
719 {
720   int i;
721   int k;
722
723   for (i = 0; i < agent->m; i++)
724   {
725     switch (mod)
726     {
727     case RIL_E_ACCUMULATE:
728       agent->E[action][i] += feature[i];
729       break;
730     case RIL_E_REPLACE:
731       agent->E[action][i] =  (agent->envi->global_discount_variable * agent->envi->parameters.lambda * agent->E[action][i]) > feature[i] ? agent->E[action][i] : feature[i];
732       break;
733     case RIL_E_DISCOUNT:
734       for (k = 0; k < agent->n; k++)
735       {
736         agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
737       }
738       break;
739     case RIL_E_ZERO:
740       for (k = 0; k < agent->n; k++)
741       {
742         agent->E[k][i] = 0;
743       }
744       break;
745     }
746   }
747 }
748
749 /**
750  * Informs the environment about the status of the solver
751  *
752  * @param solver
753  * @param op
754  * @param stat
755  */
756 static void
757 ril_inform (struct GAS_RIL_Handle *solver,
758             enum GAS_Solver_Operation op,
759             enum GAS_Solver_Status stat)
760 {
761   if (NULL != solver->plugin_envi->info_cb)
762     solver->plugin_envi->info_cb (solver->plugin_envi->info_cb_cls, op, stat, GAS_INFO_NONE);
763 }
764
765
766 /**
767  * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
768  * notify ATS of its new decision
769  *
770  * @param solver solver handle
771  * @param agent agent handle
772  * @param new_address the address which is to be used
773  * @param new_bw_in the new amount of inbound bandwidth set for this address
774  * @param new_bw_out the new amount of outbound bandwidth set for this address
775  * @param silent disables invocation of the bw_changed callback, if GNUNET_YES
776  */
777 static void
778 envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
779     struct RIL_Peer_Agent *agent,
780     struct ATS_Address *new_address,
781     unsigned long long new_bw_in,
782     unsigned long long new_bw_out,
783     int silent)
784 {
785   int notify = GNUNET_NO;
786
787   LOG(GNUNET_ERROR_TYPE_DEBUG, "    set_active_suggestion() for peer '%s'\n", GNUNET_i2s (&agent->peer));
788
789   //address change
790   if (agent->address_inuse != new_address)
791   {
792     if (NULL != agent->address_inuse)
793     {
794       agent->address_inuse->active = GNUNET_NO;
795       agent->address_inuse->assigned_bw_in.value__ = htonl (0);
796       agent->address_inuse->assigned_bw_out.value__ = htonl (0);
797     }
798     if (NULL != new_address)
799     {
800       LOG(GNUNET_ERROR_TYPE_DEBUG, "    set address active: %s\n", agent->is_active ? "yes" : "no");
801       new_address->active = agent->is_active;
802       new_address->assigned_bw_in.value__ = htonl (agent->bw_in);
803       new_address->assigned_bw_out.value__ = htonl (agent->bw_out);
804     }
805     notify |= GNUNET_YES;
806   }
807
808   if (new_address)
809   {
810     //activity change
811     if (new_address->active != agent->is_active)
812     {
813       new_address->active = agent->is_active;
814       notify |= GNUNET_YES;
815     }
816
817     //bw change
818     if (agent->bw_in != new_bw_in)
819     {
820       agent->bw_in = new_bw_in;
821       new_address->assigned_bw_in.value__ = htonl (new_bw_in);
822       notify |= GNUNET_YES;
823     }
824     if (agent->bw_out != new_bw_out)
825     {
826       agent->bw_out = new_bw_out;
827       new_address->assigned_bw_out.value__ = htonl (new_bw_out);
828       notify |= GNUNET_YES;
829     }
830   }
831
832   if (notify && agent->is_active && (GNUNET_NO == silent))
833   {
834     if (new_address)
835     {
836       LOG(GNUNET_ERROR_TYPE_DEBUG, "    envi_set_active_suggestion() notify\n");
837       agent->suggestion_issue = GNUNET_YES;
838       agent->suggestion_address = new_address;
839     }
840     else if (agent->address_inuse)
841     {
842       //disconnect case, no new address
843       GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_in.value__));
844       GNUNET_assert(0 == ntohl (agent->address_inuse->assigned_bw_out.value__));
845       agent->bw_in = 0;
846       agent->bw_out = 0;
847
848       agent->suggestion_issue = GNUNET_YES;
849       agent->suggestion_address = agent->address_inuse;
850     }
851   }
852   agent->address_inuse = new_address;
853 }
854
855
856 /**
857  * Allocates a state vector and fills it with the features present
858  * @param solver the solver handle
859  * @param agent the agent handle
860  * @return pointer to the state vector
861  */
862 static double *
863 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
864 {
865   double *state;
866   double y[2];
867   double x[2];
868   double d[2];
869   double sigma;
870   double f;
871   int m;
872   int i;
873   int k;
874   unsigned long long max_bw;
875
876   state = GNUNET_malloc (sizeof(double) * agent->m);
877
878   max_bw = RIL_MAX_BW;
879
880   y[0] = (double) agent->bw_out;
881   y[1] = (double) agent->bw_in;
882
883   m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
884   for (i = 0; i <= solver->parameters.rbf_divisor; i++)
885   {
886     for (k = 0; k <= solver->parameters.rbf_divisor; k++)
887     {
888       x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
889       x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
890       d[0] = x[0]-y[0];
891       d[1] = x[1]-y[1];
892       sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
893       f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
894       state[m++] = f;
895     }
896   }
897
898   return state;
899 }
900
901 /**
902  * Retrieves an ATS information value of an address
903  *
904  * @param address the address in question
905  * @param type the ATS information type
906  * @return the value
907  */
908 static unsigned int
909 ril_get_atsi (struct ATS_Address *address, uint32_t type)
910 {
911   int c1;
912   GNUNET_assert(NULL != address);
913
914   if ((NULL == address->atsi) || (0 == address->atsi_count))
915     return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
916
917   for (c1 = 0; c1 < address->atsi_count; c1++)
918   {
919     if (ntohl (address->atsi[c1].type) == type)
920       return ntohl (address->atsi[c1].value);
921   }
922   return GNUNET_ATS_QUALITY_NET_DELAY == type ? UINT32_MAX : 1;
923 }
924
925 /**
926  * Returns the utility value of the connection an agent manages
927  *
928  * @param agent the agent in question
929  * @return the utility value
930  */
931 static double
932 agent_get_utility (struct RIL_Peer_Agent *agent)
933 {
934   const double *preferences;
935   double delay_atsi;
936   double delay_norm;
937   double pref_match;
938
939   preferences = agent->envi->plugin_envi->get_preferences (agent->envi->plugin_envi->get_preference_cls,
940       &agent->peer);
941
942   delay_atsi = (double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_QUALITY_NET_DELAY);
943   delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
944
945   pref_match = (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm);
946   pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * (double) (agent->bw_in/RIL_MIN_BW));
947   pref_match += (preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * (double) (agent->bw_out/RIL_MIN_BW));
948
949 //  return (double) (agent->bw_in/RIL_MIN_BW);
950 //  return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
951   return pref_match;
952 }
953
954 /**
955  * Calculates the social welfare within a network scope according to what social
956  * welfare measure is set in the configuration.
957  *
958  * @param solver the solver handle
959  * @param scope the network scope in question
960  * @return the social welfare value
961  */
962 static double
963 ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
964 {
965   struct RIL_Peer_Agent *cur;
966   double result;
967
968   switch (solver->parameters.social_welfare)
969   {
970   case RIL_WELFARE_EGALITARIAN:
971     result = DBL_MAX;
972     for (cur = solver->agents_head; NULL != cur; cur = cur->next)
973     {
974       if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
975       {
976         result = GNUNET_MIN(result, agent_get_utility(cur));
977       }
978     }
979     return result;
980
981   case RIL_WELFARE_NASH:
982     result = 0;
983     for (cur = solver->agents_head; NULL != cur; cur = cur->next)
984     {
985       if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
986       {
987         result *= agent_get_utility(cur);
988       }
989     }
990     return pow(result, 1.0 / (double) scope->agent_count);
991   }
992   GNUNET_assert(GNUNET_NO);
993   return 1;
994 }
995
996 /**
997  * Gets the reward for the last performed step, which is calculated in equal
998  * parts from the local (the peer specific) and the global (for all peers
999  * identical) reward.
1000  *
1001  * @param solver the solver handle
1002  * @param agent the agent handle
1003  * @return the reward
1004  */
1005 static double
1006 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1007 {
1008   struct RIL_Scope *net;
1009   unsigned long long overutilization;
1010   unsigned long long over_in = 0;
1011   unsigned long long over_out = 0;
1012   double objective;
1013   double delta;
1014   double steady;
1015
1016   net = agent->address_inuse->solver_information;
1017
1018   //TODO make sure in tests to have utilization property updated
1019   if (net->bw_in_assigned > net->bw_in_available)
1020     over_in = net->bw_in_assigned - net->bw_in_available;
1021   if (net->bw_out_assigned > net->bw_out_available)
1022     over_out = net->bw_out_assigned - net->bw_out_available;
1023   overutilization = GNUNET_MAX(over_in, over_out) / RIL_MIN_BW;
1024
1025   objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1026   delta = objective - agent->objective_old;
1027   agent->objective_old = objective;
1028
1029   if (delta != 0)
1030   {
1031     agent->nop_bonus = delta * 0.5;
1032   }
1033
1034   LOG(GNUNET_ERROR_TYPE_DEBUG, "agent->nop_bonus: %f\n", agent->nop_bonus);
1035
1036   steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1037
1038   if (0 != overutilization)
1039   {
1040     return -1.0 * (double) overutilization;
1041   }
1042   else
1043   {
1044     return delta + steady;
1045   }
1046 }
1047
1048 /**
1049  * Doubles the bandwidth for the active address
1050  *
1051  * @param solver solver handle
1052  * @param agent agent handle
1053  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1054  */
1055 static void
1056 envi_action_bw_double (struct GAS_RIL_Handle *solver,
1057     struct RIL_Peer_Agent *agent,
1058     int direction_in)
1059 {
1060   unsigned long long new_bw;
1061
1062   if (direction_in)
1063   {
1064     new_bw = agent->bw_in * 2;
1065     if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1066       new_bw = RIL_MAX_BW;
1067     envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1068         agent->bw_out, GNUNET_NO);
1069   }
1070   else
1071   {
1072     new_bw = agent->bw_out * 2;
1073     if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1074       new_bw = RIL_MAX_BW;
1075     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1076         new_bw, GNUNET_NO);
1077   }
1078 }
1079
1080 /**
1081  * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
1082  * the minimum bandwidth for a peer, in order to not invoke a disconnect.
1083  *
1084  * @param solver solver handle
1085  * @param agent agent handle
1086  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1087  * bandwidth
1088  */
1089 static void
1090 envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1091     struct RIL_Peer_Agent *agent,
1092     int direction_in)
1093 {
1094   unsigned long long new_bw;
1095
1096   if (direction_in)
1097   {
1098     new_bw = agent->bw_in / 2;
1099     if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1100       new_bw = RIL_MIN_BW;
1101     envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1102         GNUNET_NO);
1103   }
1104   else
1105   {
1106     new_bw = agent->bw_out / 2;
1107     if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1108       new_bw = RIL_MIN_BW;
1109     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1110         GNUNET_NO);
1111   }
1112 }
1113
1114 /**
1115  * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
1116  *
1117  * @param solver solver handle
1118  * @param agent agent handle
1119  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1120  * bandwidth
1121  */
1122 static void
1123 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1124 {
1125   unsigned long long new_bw;
1126
1127   if (direction_in)
1128   {
1129     new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1130     if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1131       new_bw = RIL_MAX_BW;
1132     envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1133         agent->bw_out, GNUNET_NO);
1134   }
1135   else
1136   {
1137     new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1138     if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1139       new_bw = RIL_MAX_BW;
1140     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1141         new_bw, GNUNET_NO);
1142   }
1143 }
1144
1145 /**
1146  * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
1147  * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
1148  *
1149  * @param solver solver handle
1150  * @param agent agent handle
1151  * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
1152  * bandwidth
1153  */
1154 static void
1155 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1156 {
1157   unsigned long long new_bw;
1158
1159   if (direction_in)
1160   {
1161     new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1162     if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1163       new_bw = RIL_MIN_BW;
1164     envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1165         GNUNET_NO);
1166   }
1167   else
1168   {
1169     new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1170     if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1171       new_bw = RIL_MIN_BW;
1172     envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1173         GNUNET_NO);
1174   }
1175 }
1176
1177 /**
1178  * Switches to the address given by its index
1179  *
1180  * @param solver solver handle
1181  * @param agent agent handle
1182  * @param address_index index of the address as it is saved in the agent's list, starting with zero
1183  */
1184 static void
1185 envi_action_address_switch (struct GAS_RIL_Handle *solver,
1186     struct RIL_Peer_Agent *agent,
1187     unsigned int address_index)
1188 {
1189   struct RIL_Address_Wrapped *cur;
1190   int i = 0;
1191
1192   for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1193   {
1194     if (i == address_index)
1195     {
1196       envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1197           GNUNET_NO);
1198       return;
1199     }
1200
1201     i++;
1202   }
1203
1204   //no address with address_index exists, in this case this action should not be callable
1205   GNUNET_assert(GNUNET_NO);
1206 }
1207
1208 /**
1209  * Puts the action into effect by calling the according function
1210  *
1211  * @param solver the solver handle
1212  * @param agent the action handle
1213  * @param action the action to perform by the solver
1214  */
1215 static void
1216 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1217 {
1218   int address_index;
1219
1220   switch (action)
1221   {
1222   case RIL_ACTION_NOTHING:
1223     break;
1224   case RIL_ACTION_BW_IN_DBL:
1225     envi_action_bw_double (solver, agent, GNUNET_YES);
1226     break;
1227   case RIL_ACTION_BW_IN_HLV:
1228     envi_action_bw_halven (solver, agent, GNUNET_YES);
1229     break;
1230   case RIL_ACTION_BW_IN_INC:
1231     envi_action_bw_inc (solver, agent, GNUNET_YES);
1232     break;
1233   case RIL_ACTION_BW_IN_DEC:
1234     envi_action_bw_dec (solver, agent, GNUNET_YES);
1235     break;
1236   case RIL_ACTION_BW_OUT_DBL:
1237     envi_action_bw_double (solver, agent, GNUNET_NO);
1238     break;
1239   case RIL_ACTION_BW_OUT_HLV:
1240     envi_action_bw_halven (solver, agent, GNUNET_NO);
1241     break;
1242   case RIL_ACTION_BW_OUT_INC:
1243     envi_action_bw_inc (solver, agent, GNUNET_NO);
1244     break;
1245   case RIL_ACTION_BW_OUT_DEC:
1246     envi_action_bw_dec (solver, agent, GNUNET_NO);
1247     break;
1248   default:
1249     if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1250     {
1251       address_index = action - RIL_ACTION_TYPE_NUM;
1252
1253       GNUNET_assert(address_index >= 0);
1254       GNUNET_assert(
1255           address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1256
1257       envi_action_address_switch (solver, agent, address_index);
1258       break;
1259     }
1260     // error - action does not exist
1261     GNUNET_assert(GNUNET_NO);
1262   }
1263 }
1264
1265 /**
1266  * Selects the next action using the e-greedy strategy. I.e. with a probability
1267  * of (1-e) the action with the maximum expected return will be chosen
1268  * (=> exploitation) and with probability (e) a random action will be chosen.
1269  * In case the Q-learning rule is set, the function also resets the eligibility
1270  * traces in the exploration case (after Watkin's Q-learning).
1271  *
1272  * @param agent the agent selecting an action
1273  * @param state the current state-feature vector
1274  * @return the action index
1275  */
1276 static int
1277 agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1278 {
1279   int action;
1280   double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1281         UINT32_MAX) / (double) UINT32_MAX;
1282
1283   if (r < agent->envi->parameters.explore_ratio) //explore
1284   {
1285     action = agent_get_action_random(agent);
1286     if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1287     {
1288       agent->eligibility_reset = GNUNET_YES;
1289     }
1290     return action;
1291   }
1292   else //exploit
1293   {
1294     action = agent_get_action_max(agent, state);
1295     return action;
1296   }
1297 }
1298
1299 /**
1300  * Selects the next action with a probability corresponding to its value. The
1301  * probability is calculated using a Boltzmann distribution with a temperature
1302  * value. The higher the temperature, the more are the action selection
1303  * probabilities the same. With a temperature of 0, the selection is greedy,
1304  * i.e. always the action with the highest value is chosen.
1305  * @param agent
1306  * @param state
1307  * @return
1308  */
1309 static int
1310 agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1311 {
1312   int i;
1313   int a_max;
1314   double eqt[agent->n];
1315   double p[agent->n];
1316   double sum = 0;
1317   double r;
1318
1319   a_max = agent_get_action_max(agent, state);
1320
1321   for (i=0; i<agent->n; i++)
1322   {
1323     if (agent_action_is_possible(agent, i))
1324     {
1325       eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1326       sum += eqt[i];
1327     }
1328   }
1329   for (i=0; i<agent->n; i++)
1330   {
1331     if (agent_action_is_possible(agent, i))
1332     {
1333       p[i] = eqt[i]/sum;
1334     }
1335     else
1336     {
1337       p[i] = 0;
1338     }
1339   }
1340   r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1341       UINT32_MAX) / (double) UINT32_MAX;
1342   sum = 0;
1343   for (i=0; i<agent->n; i++)
1344   {
1345     if (sum + p[i] > r)
1346     {
1347       if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1348       {
1349         if (i != a_max)
1350           agent->eligibility_reset = GNUNET_YES;
1351       }
1352       return i;
1353     }
1354     sum += p[i];
1355   }
1356   GNUNET_assert(GNUNET_NO);
1357 }
1358
1359 /**
1360  * Select the next action of an agent either according to the e-greedy strategy
1361  * or the softmax strategy.
1362  *
1363  * @param agent the agent in question
1364  * @param state the current state-feature vector
1365  * @return the action index
1366  */
1367 static int
1368 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1369 {
1370   if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1371   {
1372     return agent_select_egreedy(agent, state);
1373   }
1374   else
1375   {
1376     return agent_select_softmax(agent, state);
1377   }
1378 }
1379
1380 /**
1381  * Performs one step of the Markov Decision Process. Other than in the literature the step starts
1382  * after having done the last action a_old. It observes the new state s_next and the reward
1383  * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
1384  * next action is put into effect.
1385  *
1386  * @param agent the agent performing the step
1387  */
1388 static void
1389 agent_step (struct RIL_Peer_Agent *agent)
1390 {
1391   int a_next = RIL_ACTION_INVALID;
1392   int a_max;
1393   double *s_next;
1394   double reward;
1395
1396   LOG(GNUNET_ERROR_TYPE_DEBUG, "    agent_step() Peer '%s', algorithm %s\n",
1397       GNUNET_i2s (&agent->peer),
1398       agent->envi->parameters.algorithm ? "Q" : "SARSA");
1399
1400   s_next = envi_get_state (agent->envi, agent);
1401   reward = envi_get_reward (agent->envi, agent);
1402
1403   if (agent->eligibility_reset)
1404   {
1405     agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1406     agent->eligibility_reset = GNUNET_NO;
1407   }
1408   else
1409   {
1410     agent_modify_eligibility (agent, RIL_E_DISCOUNT, NULL, -1);
1411   }
1412   if (RIL_ACTION_INVALID != agent->a_old)
1413   {
1414     agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1415   }
1416
1417   switch (agent->envi->parameters.algorithm)
1418   {
1419   case RIL_ALGO_SARSA:
1420     a_next = agent_select_action (agent, s_next);
1421     if (RIL_ACTION_INVALID != agent->a_old)
1422     {
1423       //updates weights with selected action (on-policy), if not first step
1424       agent_update (agent, reward, s_next, a_next);
1425     }
1426     break;
1427
1428   case RIL_ALGO_Q:
1429     a_max = agent_get_action_max (agent, s_next);
1430     if (RIL_ACTION_INVALID != agent->a_old)
1431     {
1432       //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1433       agent_update (agent, reward, s_next, a_max);
1434     }
1435     a_next = agent_select_action (agent, s_next);
1436     break;
1437   }
1438
1439   GNUNET_assert(RIL_ACTION_INVALID != a_next);
1440
1441   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step()  Step# %llu  R: %f  IN %llu  OUT %llu  A: %d\n",
1442         agent->step_count,
1443         reward,
1444         agent->bw_in/1024,
1445         agent->bw_out/1024,
1446         a_next);
1447
1448   envi_do_action (agent->envi, agent, a_next);
1449
1450   GNUNET_free(agent->s_old);
1451   agent->s_old = s_next;
1452   agent->a_old = a_next;
1453
1454   agent->step_count += 1;
1455 }
1456
1457 /**
1458  * Prototype of the ril_step() procedure
1459  *
1460  * @param solver the solver handle
1461  */
1462 static void
1463 ril_step (struct GAS_RIL_Handle *solver);
1464
1465 /**
1466  * Task for the scheduler, which performs one step and lets the solver know that
1467  * no further step is scheduled.
1468  *
1469  * @param cls the solver handle
1470  * @param tc the task context for the scheduler
1471  */
1472 static void
1473 ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1474 {
1475   struct GAS_RIL_Handle *solver = cls;
1476
1477   solver->step_next_task_id = GNUNET_SCHEDULER_NO_TASK;
1478   ril_step (solver);
1479 }
1480
1481 /**
1482  * Determines how much of the available bandwidth is assigned. If more is
1483  * assigned than available it returns 1. The function is used to determine the
1484  * step size of the adaptive stepping.
1485  *
1486  * @param solver the solver handle
1487  * @return the ratio
1488  */
1489 static double
1490 ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1491 {
1492   int i;
1493   struct RIL_Scope net;
1494   unsigned long long sum_assigned = 0;
1495   unsigned long long sum_available = 0;
1496   double ratio;
1497
1498   for (i = 0; i < solver->networks_count; i++)
1499   {
1500     net = solver->network_entries[i];
1501     if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1502     {
1503       sum_assigned += net.bw_in_assigned;
1504       sum_assigned += net.bw_out_assigned;
1505       sum_available += net.bw_in_available;
1506       sum_available += net.bw_out_available;
1507     }
1508   }
1509   if (sum_available > 0)
1510   {
1511     ratio = ((double) sum_assigned) / ((double) sum_available);
1512   }
1513   else
1514   {
1515     ratio = 0;
1516   }
1517
1518   return ratio > 1 ? 1 : ratio; //overassignment is possible, cap at 1
1519 }
1520
1521 /**
1522  * Lookup network struct by type
1523  *
1524  * @param s the solver handle
1525  * @param type the network type
1526  * @return the network struct
1527  */
1528 static struct RIL_Scope *
1529 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1530 {
1531   int i;
1532
1533   for (i = 0; i < s->networks_count; i++)
1534   {
1535     if (s->network_entries[i].type == type)
1536     {
1537       return &s->network_entries[i];
1538     }
1539   }
1540   return NULL ;
1541 }
1542
1543 /**
1544  * Determines whether more connections are allocated in a network scope, than
1545  * they would theoretically fit. This is used as a heuristic to determine,
1546  * whether a new connection can be allocated or not.
1547  *
1548  * @param solver the solver handle
1549  * @param network the network scope in question
1550  * @return GNUNET_YES if there are theoretically enough resources left
1551  */
1552 static int
1553 ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1554 {
1555   struct RIL_Scope *net;
1556   struct RIL_Peer_Agent *agent;
1557   unsigned long long address_count = 0;
1558
1559   for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1560   {
1561     if (agent->address_inuse && agent->is_active)
1562     {
1563       net = agent->address_inuse->solver_information;
1564       if (net->type == network)
1565       {
1566         address_count++;
1567       }
1568     }
1569   }
1570
1571   net = ril_get_network (solver, network);
1572   return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1573 }
1574
1575 /**
1576  * Unblocks an agent for which a connection request is there, that could not
1577  * be satisfied. Iterates over the addresses of the agent, if one of its
1578  * addresses can now be allocated in its scope the agent is unblocked,
1579  * otherwise it remains unchanged.
1580  *
1581  * @param solver the solver handle
1582  * @param agent the agent in question
1583  * @param silent
1584  */
1585 static void
1586 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1587 {
1588   struct RIL_Address_Wrapped *addr_wrap;
1589   struct RIL_Scope *net;
1590
1591   for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1592   {
1593     net = addr_wrap->address_naked->solver_information;
1594     if (ril_network_is_not_full(solver, net->type))
1595     {
1596       if (NULL == agent->address_inuse)
1597         envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, RIL_MIN_BW, RIL_MIN_BW, silent);
1598       return;
1599     }
1600   }
1601   agent->address_inuse = NULL;
1602 }
1603
1604 /**
1605  * Determines how much the reward needs to be discounted depending on the amount
1606  * of time, which has passed since the last time-step.
1607  *
1608  * @param solver the solver handle
1609  */
1610 static void
1611 ril_calculate_discount (struct GAS_RIL_Handle *solver)
1612 {
1613   struct GNUNET_TIME_Absolute time_now;
1614   struct GNUNET_TIME_Relative time_delta;
1615   double tau;
1616
1617   // MDP case - TODO remove when debugged
1618   if (solver->simulate)
1619   {
1620     solver->global_discount_variable = solver->parameters.gamma;
1621     solver->global_discount_integrated = 1;
1622     return;
1623   }
1624
1625   // semi-MDP case
1626
1627   //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1628   time_now = GNUNET_TIME_absolute_get ();
1629   time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1630   solver->step_time_last = time_now;
1631   tau = (double) time_delta.rel_value_us
1632       / (double) solver->parameters.step_time_min.rel_value_us;
1633
1634   //calculate reward discounts (once per step for all agents)
1635   solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1636   solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1637       / (double) solver->parameters.beta;
1638 }
1639
1640 /**
1641  * Count the number of active agents/connections in a network scope
1642  *
1643  * @param solver the solver handle
1644  * @param scope the network scope in question
1645  * @return the number of allocated connections
1646  */
1647 static int
1648 ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1649 {
1650   int c = 0;
1651   struct RIL_Peer_Agent *cur_agent;
1652
1653   for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1654   {
1655     if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1656     {
1657       c++;
1658     }
1659   }
1660   return c;
1661 }
1662
1663 /**
1664  * Calculates how much bandwidth is assigned in sum in a network scope, either
1665  * in the inbound or in the outbound direction.
1666  *
1667  * @param solver the solver handle
1668  * @param type the type of the network scope in question
1669  * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1670  *   otherwise the outbound direction will be summed up
1671  * @return the sum of the assigned bandwidths
1672  */
1673 static unsigned long long
1674 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1675 {
1676   struct RIL_Peer_Agent *cur;
1677   struct RIL_Scope *net;
1678   unsigned long long sum = 0;
1679
1680   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1681   {
1682     if (cur->is_active && cur->address_inuse)
1683     {
1684       net = cur->address_inuse->solver_information;
1685       if (net->type == type)
1686       {
1687         if (direction_in)
1688           sum += cur->bw_in;
1689         else
1690           sum += cur->bw_out;
1691       }
1692     }
1693   }
1694
1695   return sum;
1696 }
1697
1698 /**
1699  * Calculates how much bandwidth is actually utilized in sum in a network scope,
1700  * either in the inbound or in the outbound direction.
1701  *
1702  * @param solver the solver handle
1703  * @param type the type of the network scope in question
1704  * @param direction_in GNUNET_YES if the inbound direction should be summed up,
1705  *   otherwise the outbound direction will be summed up
1706  * @return the sum of the utilized bandwidths (in bytes/second)
1707  */
1708 static unsigned long long
1709 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
1710 {
1711   struct RIL_Peer_Agent *cur;
1712   struct RIL_Scope *net;
1713   unsigned long long sum = 0;
1714
1715   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1716   {
1717     if (cur->is_active && cur->address_inuse)
1718     {
1719       net = cur->address_inuse->solver_information;
1720       if (net->type == type)
1721       {
1722         if (direction_in)
1723           sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_IN);
1724         else
1725           sum += ril_get_atsi (cur->address_inuse, GNUNET_ATS_UTILIZATION_OUT);
1726       }
1727     }
1728   }
1729
1730   return sum;
1731 }
1732
1733 /**
1734  * Retrieves the state of the network scope, so that its attributes are up-to-
1735  * date.
1736  *
1737  * @param solver the solver handle
1738  */
1739 static void
1740 ril_networks_update_state (struct GAS_RIL_Handle *solver)
1741 {
1742   int c;
1743   struct RIL_Scope *net;
1744
1745   for (c = 0; c < solver->networks_count; c++)
1746   {
1747     net = &solver->network_entries[c];
1748     net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1749     net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
1750     net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1751     net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
1752     net->agent_count = ril_network_count_active_agents(solver, net);
1753     net->social_welfare = ril_network_get_social_welfare(solver, net);
1754   }
1755 }
1756
1757 /**
1758  * Schedules the next global step in an adaptive way. The more resources are
1759  * left, the earlier the next step is scheduled. This serves the reactivity of
1760  * the solver to changed inputs.
1761  *
1762  * @param solver the solver handle
1763  */
1764 static void
1765 ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1766 {
1767   double used_ratio;
1768   double factor;
1769   double y;
1770   double offset;
1771   struct GNUNET_TIME_Relative time_next;
1772
1773   used_ratio = ril_get_used_resource_ratio (solver);
1774
1775   GNUNET_assert(
1776       solver->parameters.step_time_min.rel_value_us
1777           <= solver->parameters.step_time_max.rel_value_us);
1778
1779   factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1780       solver->parameters.step_time_min).rel_value_us;
1781   offset = (double) solver->parameters.step_time_min.rel_value_us;
1782   y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1783
1784   GNUNET_assert(y <= (double ) solver->parameters.step_time_max.rel_value_us);
1785   GNUNET_assert(y >= (double ) solver->parameters.step_time_min.rel_value_us);
1786
1787   time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1788
1789   if (solver->simulate)
1790   {
1791     time_next = GNUNET_TIME_UNIT_ZERO;
1792   }
1793
1794   if ((GNUNET_SCHEDULER_NO_TASK == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1795   {
1796     solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
1797           solver);
1798   }
1799 }
1800
1801 /**
1802  * Triggers one step per agent
1803  *
1804  * @param solver
1805  */
1806 static void
1807 ril_step (struct GAS_RIL_Handle *solver)
1808 {
1809   struct RIL_Peer_Agent *cur;
1810
1811   if (GNUNET_YES == solver->bulk_lock)
1812   {
1813     solver->bulk_changes++;
1814     return;
1815   }
1816
1817   ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1818
1819   LOG(GNUNET_ERROR_TYPE_DEBUG, "    RIL step number %d\n", solver->step_count);
1820
1821   if (0 == solver->step_count)
1822   {
1823     solver->step_time_last = GNUNET_TIME_absolute_get ();
1824   }
1825
1826   ril_calculate_discount (solver);
1827   ril_networks_update_state (solver);
1828
1829   //trigger one step per active, unblocked agent
1830   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1831   {
1832     if (cur->is_active)
1833     {
1834       if (NULL == cur->address_inuse)
1835       {
1836         ril_try_unblock_agent(solver, cur, GNUNET_NO);
1837       }
1838       if (cur->address_inuse)
1839       {
1840         agent_step (cur);
1841       }
1842     }
1843   }
1844
1845   ril_networks_update_state (solver);
1846
1847   solver->step_count += 1;
1848   ril_step_schedule_next (solver);
1849
1850   ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1851
1852   ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1853   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1854   {
1855     if (cur->suggestion_issue) {
1856       solver->plugin_envi->bandwidth_changed_cb(solver->plugin_envi->bw_changed_cb_cls, cur->suggestion_address);
1857       cur->suggestion_issue = GNUNET_NO;
1858     }
1859   }
1860   ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1861 }
1862
1863 /**
1864  * Initializes the matrix W of parameter vectors theta with small random numbers.
1865  *
1866  * @param agent The respective agent
1867  */
1868 static void
1869 agent_w_init (struct RIL_Peer_Agent *agent)
1870 {
1871   int i;
1872   int k;
1873
1874   for (i = 0; i < agent->n; i++)
1875   {
1876     for (k = 0; k < agent->m; k++)
1877     {
1878       agent->W[i][k] = agent->envi->parameters.alpha * (1.0 - 2.0 * ((double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX)/(double) UINT32_MAX));
1879     }
1880   }
1881 }
1882
1883 /**
1884  * Initialize an agent without addresses and its knowledge base
1885  *
1886  * @param s ril solver
1887  * @param peer the one in question
1888  * @return handle to the new agent
1889  */
1890 static struct RIL_Peer_Agent *
1891 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1892 {
1893   int i;
1894   struct GAS_RIL_Handle * solver = s;
1895   struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1896
1897   agent->envi = solver;
1898   agent->peer = *peer;
1899   agent->step_count = 0;
1900   agent->is_active = GNUNET_NO;
1901   agent->bw_in = RIL_MIN_BW;
1902   agent->bw_out = RIL_MIN_BW;
1903   agent->suggestion_issue = GNUNET_NO;
1904   agent->n = RIL_ACTION_TYPE_NUM;
1905   agent->m = 0;
1906   agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1907   agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1908   for (i = 0; i < agent->n; i++)
1909   {
1910     agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1911     agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1912   }
1913   agent_w_init(agent);
1914   agent->eligibility_reset = GNUNET_NO;
1915   agent->a_old = RIL_ACTION_INVALID;
1916   agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1917   agent->address_inuse = NULL;
1918   agent->objective_old = 0;
1919
1920   return agent;
1921 }
1922
1923 /**
1924  * Deallocate agent
1925  *
1926  * @param solver the solver handle
1927  * @param agent the agent to retire
1928  */
1929 static void
1930 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1931 {
1932   int i;
1933
1934   for (i = 0; i < agent->n; i++)
1935   {
1936     GNUNET_free_non_null(agent->W[i]);
1937     GNUNET_free_non_null(agent->E[i]);
1938   }
1939   GNUNET_free_non_null(agent->W);
1940   GNUNET_free_non_null(agent->E);
1941   GNUNET_free_non_null(agent->s_old);
1942   GNUNET_free(agent);
1943 }
1944
1945 /**
1946  * Returns the agent for a peer
1947  *
1948  * @param solver the solver handle
1949  * @param peer the identity of the peer
1950  * @param create whether or not to create an agent, if none is allocated yet
1951  * @return the agent
1952  */
1953 static struct RIL_Peer_Agent *
1954 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
1955 {
1956   struct RIL_Peer_Agent *cur;
1957
1958   for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1959   {
1960     if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
1961     {
1962       return cur;
1963     }
1964   }
1965
1966   if (create)
1967   {
1968     cur = agent_init (solver, peer);
1969     GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
1970     return cur;
1971   }
1972   return NULL ;
1973 }
1974
1975 /**
1976  * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
1977  * considered inactive and not used. Addresses in an inactive network are ignored.
1978  *
1979  * @param solver solver handle
1980  * @param network the network type
1981  * @return whether or not the network is considered active
1982  */
1983 static int
1984 ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
1985 {
1986   struct RIL_Scope *net;
1987
1988   net = ril_get_network (solver, network);
1989   return net->bw_out_available >= RIL_MIN_BW;
1990 }
1991
1992 /**
1993  * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
1994  * the reward function approximation. It copies the memory, which is not cut, to the new vector,
1995  * frees the memory of the old vector, and redirects the pointer to the new one.
1996  *
1997  * @param old pointer to the pointer to the first element of the vector
1998  * @param element_size byte size of the vector elements
1999  * @param hole_start the first element to cut out
2000  * @param hole_length the number of elements to cut out
2001  * @param old_length the length of the old vector
2002  */
2003 static void
2004 ril_cut_from_vector (void **old,
2005     size_t element_size,
2006     unsigned int hole_start,
2007     unsigned int hole_length,
2008     unsigned int old_length)
2009 {
2010   char *tmpptr;
2011   char *oldptr = (char *) *old;
2012   size_t size;
2013   unsigned int bytes_before;
2014   unsigned int bytes_hole;
2015   unsigned int bytes_after;
2016
2017   GNUNET_assert(old_length >= hole_length);
2018   GNUNET_assert(old_length >= (hole_start + hole_length));
2019
2020   size = element_size * (old_length - hole_length);
2021
2022   bytes_before = element_size * hole_start;
2023   bytes_hole = element_size * hole_length;
2024   bytes_after = element_size * (old_length - hole_start - hole_length);
2025
2026   if (0 == size)
2027   {
2028     tmpptr = NULL;
2029   }
2030   else
2031   {
2032     tmpptr = GNUNET_malloc (size);
2033     memcpy (tmpptr, oldptr, bytes_before);
2034     memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2035   }
2036   if (NULL != *old)
2037   {
2038     GNUNET_free(*old);
2039   }
2040   *old = (void *) tmpptr;
2041 }
2042
2043 /*
2044  *  Solver API functions
2045  *  ---------------------------
2046  */
2047
2048 /**
2049  * Change relative preference for quality in solver
2050  *
2051  * @param solver the solver handle
2052  * @param peer the peer to change the preference for
2053  * @param kind the kind to change the preference
2054  * @param pref_rel the normalized preference value for this kind over all clients
2055  */
2056 void
2057 GAS_ril_address_change_preference (void *solver,
2058     const struct GNUNET_PeerIdentity *peer,
2059     enum GNUNET_ATS_PreferenceKind kind,
2060     double pref_rel)
2061 {
2062   LOG(GNUNET_ERROR_TYPE_DEBUG,
2063       "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2064       GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2065
2066   ril_step (solver);
2067 }
2068
2069 /**
2070  * Entry point for the plugin
2071  *
2072  * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2073  */
2074 void *
2075 libgnunet_plugin_ats_ril_init (void *cls)
2076 {
2077   struct GNUNET_ATS_PluginEnvironment *env = cls;
2078   struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2079   struct RIL_Scope * cur;
2080   int c;
2081   char *string;
2082
2083   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n");
2084
2085   GNUNET_assert(NULL != env);
2086   GNUNET_assert(NULL != env->cfg);
2087   GNUNET_assert(NULL != env->stats);
2088   GNUNET_assert(NULL != env->bandwidth_changed_cb);
2089   GNUNET_assert(NULL != env->get_preferences);
2090   GNUNET_assert(NULL != env->get_property);
2091
2092   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2093   {
2094     solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2095   }
2096   if (GNUNET_OK
2097       != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2098           &solver->parameters.step_time_min))
2099   {
2100     solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2101   }
2102   if (GNUNET_OK
2103       != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2104           &solver->parameters.step_time_max))
2105   {
2106     solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2107   }
2108   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2109   {
2110     solver->parameters.algorithm = !strcmp (string, "SARSA") ? RIL_ALGO_SARSA : RIL_ALGO_Q;
2111     GNUNET_free (string);
2112   }
2113   else
2114   {
2115     solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2116   }
2117   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2118   {
2119     solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2120     GNUNET_free (string);
2121   }
2122   else
2123   {
2124     solver->parameters.select = RIL_DEFAULT_SELECT;
2125   }
2126   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_BETA", &string))
2127   {
2128     solver->parameters.beta = strtod (string, NULL);
2129     GNUNET_free (string);
2130     if (!(solver->parameters.beta > 0))
2131     {
2132       LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_DISCOUNT_BETA not configured as positive number. Set to default value of %f instead.\n", RIL_DEFAULT_DISCOUNT_BETA);
2133       solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2134     }
2135   }
2136   else
2137   {
2138     solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2139   }
2140   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_DISCOUNT_GAMMA", &string))
2141   {
2142     solver->parameters.gamma = strtod (string, NULL);
2143     GNUNET_free (string);
2144     if (!(solver->parameters.gamma < 1) || (solver->parameters.gamma < 0))
2145     {
2146       LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_DISCOUNT_GAMMA not configured in range [0,1[. Set to default value of %f instead.\n", RIL_DEFAULT_DISCOUNT_GAMMA);
2147       solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2148     }
2149   }
2150   else
2151   {
2152     solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2153   }
2154   if (GNUNET_OK
2155       == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &string))
2156   {
2157     solver->parameters.alpha = strtod (string, NULL);
2158     GNUNET_free (string);
2159     if (!(solver->parameters.alpha > 0) || solver->parameters.alpha > 1)
2160     {
2161       LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_GRADIENT_STEP_SIZE not configured in range ]0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_GRADIENT_STEP_SIZE);
2162       solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2163     }
2164   }
2165   else
2166   {
2167     solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2168   }
2169   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TRACE_DECAY", &string))
2170   {
2171     solver->parameters.lambda = strtod (string, NULL);
2172     GNUNET_free (string);
2173     if (solver->parameters.lambda < 0 || solver->parameters.lambda > 1)
2174     {
2175       LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TRACE_DECAY not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_TRACE_DECAY);
2176       solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2177     }
2178   }
2179   else
2180   {
2181     solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2182   }
2183   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_EXPLORE_RATIO", &string))
2184   {
2185     solver->parameters.explore_ratio = strtod (string, NULL);
2186     GNUNET_free (string);
2187     if (solver->parameters.explore_ratio < 0 || solver->parameters.explore_ratio > 1)
2188     {
2189       LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_EXPLORE_RATIO not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_EXPLORE_RATIO);
2190       solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
2191     }
2192   }
2193   else
2194   {
2195     solver->parameters.explore_ratio = RIL_DEFAULT_EXPLORE_RATIO;
2196   }
2197   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_GLOBAL_REWARD_SHARE", &string))
2198   {
2199     solver->parameters.reward_global_share = strtod (string, NULL);
2200     GNUNET_free (string);
2201     if (solver->parameters.reward_global_share < 0 || solver->parameters.reward_global_share > 1)
2202     {
2203       LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_GLOBAL_REWARD_SHARE not configured in range [0,1]. Set to default value of %f instead.\n", RIL_DEFAULT_GLOBAL_REWARD_SHARE);
2204       solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
2205     }
2206   }
2207   else
2208   {
2209     solver->parameters.reward_global_share = RIL_DEFAULT_GLOBAL_REWARD_SHARE;
2210   }
2211   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_TEMPERATURE", &string))
2212   {
2213     solver->parameters.temperature = strtod (string, NULL);
2214     GNUNET_free (string);
2215     if (solver->parameters.temperature <= 0)
2216     {
2217       LOG (GNUNET_ERROR_TYPE_WARNING, "RIL_TEMPERATURE not positive. Set to default value of %f instead.\n", RIL_DEFAULT_TEMPERATURE);
2218       solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
2219     }
2220   }
2221   else
2222   {
2223     solver->parameters.temperature = RIL_DEFAULT_TEMPERATURE;
2224   }
2225   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2226   {
2227     solver->simulate = GNUNET_NO;
2228   }
2229   if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2230   {
2231     solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2232   }
2233   else
2234   {
2235     solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2236   }
2237   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2238   {
2239     solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2240     GNUNET_free (string);
2241   }
2242   else
2243   {
2244     solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2245   }
2246
2247   env->sf.s_add = &GAS_ril_address_add;
2248   env->sf.s_address_update_property = &GAS_ril_address_property_changed;
2249   env->sf.s_address_update_session = &GAS_ril_address_session_changed;
2250   env->sf.s_address_update_inuse = &GAS_ril_address_inuse_changed;
2251   env->sf.s_address_update_network = &GAS_ril_address_change_network;
2252   env->sf.s_get = &GAS_ril_get_preferred_address;
2253   env->sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
2254   env->sf.s_pref = &GAS_ril_address_change_preference;
2255   env->sf.s_feedback = &GAS_ril_address_preference_feedback;
2256   env->sf.s_del = &GAS_ril_address_delete;
2257   env->sf.s_bulk_start = &GAS_ril_bulk_start;
2258   env->sf.s_bulk_stop = &GAS_ril_bulk_stop;
2259
2260   solver->plugin_envi = env;
2261   solver->networks_count = env->network_count;
2262   solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2263   solver->step_count = 0;
2264   solver->done = GNUNET_NO;
2265
2266   for (c = 0; c < env->network_count; c++)
2267   {
2268     cur = &solver->network_entries[c];
2269     cur->type = env->networks[c];
2270     cur->bw_in_available = env->in_quota[c];
2271     cur->bw_out_available = env->out_quota[c];
2272     LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  Quotas for %s network:  IN %llu - OUT %llu\n", GNUNET_ATS_print_network_type(cur->type), cur->bw_in_available/1024, cur->bw_out_available/1024);
2273   }
2274
2275   LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  Parameters:\n");
2276   LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2277       solver->parameters.algorithm ? "Q" : "SARSA",
2278       solver->parameters.alpha,
2279       solver->parameters.beta,
2280       solver->parameters.lambda);
2281   LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  exploration_ratio = %f, temperature = %f, ActionSelection = %s, global_share = %f\n",
2282       solver->parameters.explore_ratio,
2283       solver->parameters.temperature,
2284       solver->parameters.select ? "EGREEDY" : "SOFTMAX",
2285       solver->parameters.reward_global_share);
2286   LOG(GNUNET_ERROR_TYPE_DEBUG, "init()  RBF_DIVISOR = %llu\n",
2287       solver->parameters.rbf_divisor);
2288
2289   return solver;
2290 }
2291
2292 /**
2293  * Exit point for the plugin
2294  *
2295  * @param cls the solver handle
2296  */
2297 void *
2298 libgnunet_plugin_ats_ril_done (void *cls)
2299 {
2300   struct GAS_RIL_Handle *s = cls;
2301   struct RIL_Peer_Agent *cur_agent;
2302   struct RIL_Peer_Agent *next_agent;
2303
2304   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2305
2306   s->done = GNUNET_YES;
2307
2308   cur_agent = s->agents_head;
2309   while (NULL != cur_agent)
2310   {
2311     next_agent = cur_agent->next;
2312     GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
2313     agent_die (s, cur_agent);
2314     cur_agent = next_agent;
2315   }
2316
2317   if (GNUNET_SCHEDULER_NO_TASK != s->step_next_task_id)
2318   {
2319     GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2320   }
2321   GNUNET_free(s->network_entries);
2322   GNUNET_free(s);
2323
2324   return NULL;
2325 }
2326
2327 /**
2328  * Add a new address for a peer to the solver
2329  *
2330  * The address is already contained in the addresses hashmap!
2331  *
2332  * @param solver the solver Handle
2333  * @param address the address to add
2334  * @param network network type of this address
2335  */
2336 void
2337 GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network)
2338 {
2339   struct GAS_RIL_Handle *s = solver;
2340   struct RIL_Peer_Agent *agent;
2341   struct RIL_Address_Wrapped *address_wrapped;
2342   struct RIL_Scope *net;
2343   unsigned int m_new;
2344   unsigned int m_old;
2345   unsigned int n_new;
2346   unsigned int n_old;
2347   int i;
2348   unsigned int zero;
2349
2350   LOG (GNUNET_ERROR_TYPE_DEBUG, "API_address_add()\n");
2351
2352   net = ril_get_network (s, network);
2353   address->solver_information = net;
2354
2355   if (!ril_network_is_active (s, network))
2356   {
2357     LOG(GNUNET_ERROR_TYPE_DEBUG,
2358         "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2359         address->plugin, address->addr, GNUNET_i2s (&address->peer));
2360     return;
2361   }
2362
2363   agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2364
2365   //add address
2366   address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2367   address_wrapped->address_naked = address;
2368   GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2369
2370   //increase size of W
2371   m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2372   m_old = agent->m;
2373   n_new = agent->n + 1;
2374   n_old = agent->n;
2375
2376   GNUNET_array_grow(agent->W, agent->n, n_new);
2377   agent->n = n_old;
2378   GNUNET_array_grow(agent->E, agent->n, n_new);
2379   for (i = 0; i < n_new; i++)
2380   {
2381     if (i < n_old)
2382     {
2383       agent->m = m_old;
2384       GNUNET_array_grow(agent->W[i], agent->m, m_new);
2385       agent->m = m_old;
2386       GNUNET_array_grow(agent->E[i], agent->m, m_new);
2387     }
2388     else
2389     {
2390       zero = 0;
2391       GNUNET_array_grow(agent->W[i], zero, m_new);
2392       zero = 0;
2393       GNUNET_array_grow(agent->E[i], zero, m_new);
2394     }
2395   }
2396
2397   //increase size of old state vector
2398   agent->m = m_old;
2399   GNUNET_array_grow(agent->s_old, agent->m, m_new);
2400
2401   ril_try_unblock_agent(s, agent, GNUNET_NO);
2402
2403   ril_step (s);
2404
2405   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2406       address->active ? "active" : "inactive", address->plugin, address->addr,
2407       GNUNET_i2s (&address->peer));
2408 }
2409
2410 /**
2411  * Delete an address in the solver
2412  *
2413  * The address is not contained in the address hashmap anymore!
2414  *
2415  * @param solver the solver handle
2416  * @param address the address to remove
2417  * @param session_only delete only session not whole address
2418  */
2419 void
2420 GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_only)
2421 {
2422   struct GAS_RIL_Handle *s = solver;
2423   struct RIL_Peer_Agent *agent;
2424   struct RIL_Address_Wrapped *address_wrapped;
2425   int address_was_used = address->active;
2426   int address_index;
2427   unsigned int m_new;
2428   unsigned int n_new;
2429   int i;
2430   struct RIL_Scope *net;
2431
2432   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_delete() Delete %s%s %s address %s for peer '%s'\n",
2433       session_only ? "session for " : "", address->active ? "active" : "inactive", address->plugin,
2434       address->addr, GNUNET_i2s (&address->peer));
2435
2436   agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2437   if (NULL == agent)
2438   {
2439     net = address->solver_information;
2440     GNUNET_assert(!ril_network_is_active (s, net->type));
2441     LOG(GNUNET_ERROR_TYPE_DEBUG,
2442         "No agent allocated for peer yet, since address was in inactive network\n");
2443     return;
2444   }
2445
2446   address_index = agent_address_get_index (agent, address);
2447   address_wrapped = agent_address_get (agent, address);
2448
2449   if (NULL == address_wrapped)
2450   {
2451     net = address->solver_information;
2452     GNUNET_assert(!ril_network_is_active (s, net->type));
2453     LOG(GNUNET_ERROR_TYPE_DEBUG,
2454         "Address not considered by agent, address was in inactive network\n");
2455     return;
2456   }
2457
2458   GNUNET_CONTAINER_DLL_remove(agent->addresses_head, agent->addresses_tail, address_wrapped);
2459   GNUNET_free(address_wrapped);
2460
2461   //decrease W
2462   m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2463   n_new = agent->n - 1;
2464
2465   for (i = 0; i < agent->n; i++)
2466   {
2467     ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2468         address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2469         ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2470     ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2471         address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2472         ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2473   }
2474   GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2475   GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2476   ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2477       1, agent->n);
2478   ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2479       1, agent->n);
2480   //correct last action
2481   if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2482   {
2483     agent->a_old -= 1;
2484   }
2485   else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2486   {
2487     agent->a_old = RIL_ACTION_INVALID;
2488   }
2489   //decrease old state vector
2490   ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2491       address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2492       ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2493   agent->m = m_new;
2494   agent->n = n_new;
2495
2496   if (address_was_used)
2497   {
2498     if (NULL != agent->addresses_head) //if peer has an address left, use it
2499     {
2500       envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, RIL_MIN_BW, RIL_MIN_BW,
2501           GNUNET_NO);
2502     }
2503     else
2504     {
2505       envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2506     }
2507   }
2508
2509   ril_step (solver);
2510 }
2511
2512 /**
2513  * Update the properties of an address in the solver
2514  *
2515  * @param solver solver handle
2516  * @param address the address
2517  * @param type the ATSI type in HBO
2518  * @param abs_value the absolute value of the property
2519  * @param rel_value the normalized value
2520  */
2521 void
2522 GAS_ril_address_property_changed (void *solver,
2523     struct ATS_Address *address,
2524     uint32_t type,
2525     uint32_t abs_value,
2526     double rel_value)
2527 {
2528   LOG(GNUNET_ERROR_TYPE_DEBUG,
2529       "API_address_property_changed() Property '%s' for peer '%s' address %s changed "
2530           "to %.2f \n", GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
2531       address->addr, rel_value);
2532
2533   ril_step (solver);
2534 }
2535
2536 /**
2537  * Update the session of an address in the solver
2538  *
2539  * NOTE: values in addresses are already updated
2540  *
2541  * @param solver solver handle
2542  * @param address the address
2543  * @param cur_session the current session
2544  * @param new_session the new session
2545  */
2546 void
2547 GAS_ril_address_session_changed (void *solver,
2548     struct ATS_Address *address,
2549     uint32_t cur_session,
2550     uint32_t new_session)
2551 {
2552   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n");
2553 }
2554
2555 /**
2556  * Notify the solver that an address is (not) actively used by transport
2557  * to communicate with a remote peer
2558  *
2559  * NOTE: values in addresses are already updated
2560  *
2561  * @param solver solver handle
2562  * @param address the address
2563  * @param in_use usage state
2564  */
2565 void
2566 GAS_ril_address_inuse_changed (void *solver, struct ATS_Address *address, int in_use)
2567 {
2568   LOG(GNUNET_ERROR_TYPE_DEBUG,
2569       "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n",
2570       address->plugin, GNUNET_i2s (&address->peer), (GNUNET_YES == in_use) ? "USED" : "UNUSED");
2571 }
2572
2573 /**
2574  * Notify solver that the network an address is located in has changed
2575  *
2576  * NOTE: values in addresses are already updated
2577  *
2578  * @param solver solver handle
2579  * @param address the address
2580  * @param current_network the current network
2581  * @param new_network the new network
2582  */
2583 void
2584 GAS_ril_address_change_network (void *solver,
2585     struct ATS_Address *address,
2586     uint32_t current_network,
2587     uint32_t new_network)
2588 {
2589   struct GAS_RIL_Handle *s = solver;
2590   struct RIL_Peer_Agent *agent;
2591
2592   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving "
2593       "%s address of peer %s from '%s' to '%s'\n",
2594       (GNUNET_YES == address->active) ? "active" : "inactive", GNUNET_i2s (&address->peer),
2595       GNUNET_ATS_print_network_type (current_network), GNUNET_ATS_print_network_type (new_network));
2596
2597   if (address->active && !ril_network_is_active (solver, new_network))
2598   {
2599     GAS_ril_address_delete (solver, address, GNUNET_NO);
2600     return;
2601   }
2602
2603   agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2604   if (NULL == agent)
2605   {
2606     GNUNET_assert(!ril_network_is_active (solver, current_network));
2607
2608     GAS_ril_address_add (s, address, new_network);
2609     return;
2610   }
2611
2612   address->solver_information = ril_get_network(solver, new_network);
2613 }
2614
2615 /**
2616  * Give feedback about the current assignment
2617  *
2618  * @param solver the solver handle
2619  * @param application the application
2620  * @param peer the peer to change the preference for
2621  * @param scope the time interval for this feedback: [now - scope .. now]
2622  * @param kind the kind to change the preference
2623  * @param score the score
2624  */
2625 void
2626 GAS_ril_address_preference_feedback (void *solver,
2627     void *application,
2628     const struct GNUNET_PeerIdentity *peer,
2629     const struct GNUNET_TIME_Relative scope,
2630     enum GNUNET_ATS_PreferenceKind kind,
2631     double score)
2632 {
2633   LOG(GNUNET_ERROR_TYPE_DEBUG,
2634       "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2635           "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN",
2636       GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000);
2637 }
2638
2639 /**
2640  * Start a bulk operation
2641  *
2642  * @param solver the solver
2643  */
2644 void
2645 GAS_ril_bulk_start (void *solver)
2646 {
2647   struct GAS_RIL_Handle *s = solver;
2648
2649   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2650
2651   s->bulk_lock++;
2652 }
2653
2654 /**
2655  * Bulk operation done
2656  *
2657  * @param solver the solver handle
2658  */
2659 void
2660 GAS_ril_bulk_stop (void *solver)
2661 {
2662   struct GAS_RIL_Handle *s = solver;
2663
2664   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop() lock: %d\n", s->bulk_lock-1);
2665
2666   if (s->bulk_lock < 1)
2667   {
2668     GNUNET_break(0);
2669     return;
2670   }
2671   s->bulk_lock--;
2672
2673   if (0 < s->bulk_changes)
2674   {
2675     ril_step (solver);
2676     s->bulk_changes = 0;
2677   }
2678 }
2679
2680 /**
2681  * Tell solver to notify ATS if the address to use changes for a specific
2682  * peer using the bandwidth changed callback
2683  *
2684  * The solver must only notify about changes for peers with pending address
2685  * requests!
2686  *
2687  * @param solver the solver handle
2688  * @param peer the identity of the peer
2689  */
2690 const struct ATS_Address *
2691 GAS_ril_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2692 {
2693   /*
2694    * activate agent, return currently chosen address
2695    */
2696   struct GAS_RIL_Handle *s = solver;
2697   struct RIL_Peer_Agent *agent;
2698
2699   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2700
2701   agent = ril_get_agent (s, peer, GNUNET_YES);
2702
2703   agent->is_active = GNUNET_YES;
2704   envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2705
2706   ril_try_unblock_agent(solver, agent, GNUNET_YES);
2707
2708   if (agent->address_inuse)
2709   {
2710     LOG(GNUNET_ERROR_TYPE_DEBUG,
2711         "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2712         GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2713   }
2714   else
2715   {
2716     LOG(GNUNET_ERROR_TYPE_DEBUG,
2717         "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2718         GNUNET_i2s (peer));
2719   }
2720
2721   return agent->address_inuse;
2722 }
2723
2724 /**
2725  * Tell solver stop notifying ATS about changes for this peers
2726  *
2727  * The solver must only notify about changes for peers with pending address
2728  * requests!
2729  *
2730  * @param solver the solver handle
2731  * @param peer the peer
2732  */
2733 void
2734 GAS_ril_stop_get_preferred_address (void *solver, const struct GNUNET_PeerIdentity *peer)
2735 {
2736   struct GAS_RIL_Handle *s = solver;
2737   struct RIL_Peer_Agent *agent;
2738
2739   LOG(GNUNET_ERROR_TYPE_DEBUG, "API_stop_get_preferred_address()");
2740
2741   agent = ril_get_agent (s, peer, GNUNET_NO);
2742
2743   if (NULL == agent)
2744   {
2745     GNUNET_break(0);
2746     return;
2747   }
2748   if (GNUNET_NO == agent->is_active)
2749   {
2750     GNUNET_break(0);
2751     return;
2752   }
2753
2754   agent->is_active = GNUNET_NO;
2755
2756   envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2757       GNUNET_YES);
2758
2759   ril_step (s);
2760
2761   LOG(GNUNET_ERROR_TYPE_DEBUG,
2762       "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n",
2763       GNUNET_i2s (peer), agent->address_inuse->plugin);
2764 }
2765
2766 /* end of plugin_ats_ril.c */