GNUnet  0.10.x
plugin_ats_ril.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2011-2014 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "platform.h"
28 #include <float.h>
29 #include <math.h>
30 #include "gnunet_ats_plugin.h"
32 
33 
34 
35 #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
36 
37 #define RIL_MIN_BW (5 * ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__))
38 #define RIL_MAX_BW GNUNET_ATS_MaxBandwidth
39 
40 #define RIL_ACTION_INVALID -1
41 #define RIL_INTERVAL_EXPONENT 10
42 #define RIL_UTILITY_DELAY_MAX 1000
43 
44 #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200)
45 #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 2000)
46 #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
47 #define RIL_DEFAULT_SELECT RIL_SELECT_SOFTMAX
48 #define RIL_DEFAULT_WELFARE RIL_WELFARE_NASH
49 #define RIL_DEFAULT_DISCOUNT_BETA 0.6
50 #define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
51 #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.01
52 #define RIL_DEFAULT_TRACE_DECAY 0.5
53 #define RIL_DEFAULT_EXPLORE_RATIO 1
54 #define RIL_DEFAULT_EXPLORE_DECAY 0.95
55 #define RIL_DEFAULT_RBF_DIVISOR 50
56 #define RIL_DEFAULT_TEMPERATURE 0.1
57 #define RIL_DEFAULT_TEMPERATURE_DECAY 1
58 
59 #define RIL_INC_DEC_STEP_SIZE 1
60 #define RIL_NOP_DECAY 0.5
61 
75 {
77  RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions
86 };
87 
89 {
92 };
93 
95 {
98 };
99 
101 {
104 };
105 
107 {
112 };
113 
118 {
123 
127  double alpha;
128 
132  double beta;
133 
137  double gamma;
138 
142  double lambda;
143 
148 
153 
157  double temperature;
158 
163 
168 
172  unsigned long long rbf_divisor;
173 
178 
182  double epsilon_init;
183 
187  double epsilon;
188 
193 
198 
203 };
204 
209 {
214 
219 
224 };
225 
226 
228 {
233 
238 
243 
248 
253 
257  unsigned long long step_count;
258 
262  double ** W;
263 
267  unsigned int m;
268 
272  unsigned int n;
273 
277  double *s_old;
278 
282  int a_old;
283 
287  double ** E;
288 
293 
298 
303 
308 
312  uint32_t bw_in;
313 
317  uint32_t bw_out;
318 
323 
328 
333 
337  double nop_bonus;
338 };
339 
340 struct RIL_Scope
341 {
346 
350  uint32_t bw_in_available;
351 
355  uint32_t bw_in_assigned;
356 
360  uint32_t bw_in_utilized;
361 
366 
370  unsigned long long bw_out_assigned;
371 
375  unsigned long long bw_out_utilized;
376 
380  unsigned int active_agent_count;
381 
386 };
387 
392 {
397 
401  unsigned long long step_count;
402 
406  struct GNUNET_TIME_Absolute step_time_last;
407 
412 
417 
422 
427 
432 
436  struct RIL_Learning_Parameters parameters;
437 
442 
446  unsigned int networks_count;
447 
453 
457  int done;
458 
462  unsigned long long simulate;
463 };
464 
465 /*
466  * "Private" functions
467  * ---------------------------
468  */
469 
478 static double
479 agent_q (struct RIL_Peer_Agent *agent,
480  const double *state,
481  int action)
482 {
483  unsigned int i;
484  double result = 0.0;
485 
486  for (i = 0; i < agent->m; i++)
487  result += state[i] * agent->W[action][i];
488 
489  /* prevent crashes if learning diverges */
490  if (isnan(result))
491  return isnan(result) * UINT32_MAX;
492  if (isinf(result))
493  return isinf(result) * UINT32_MAX;
494  return result;
495 }
496 
497 
505 static int
507 {
508  int i;
509  struct RIL_Address_Wrapped *cur;
510 
511  i = -1;
512  for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
513  {
514  i++;
515  if (cur->address_naked == address)
516  return i;
517  }
518  return i;
519 }
520 
521 
529 static struct RIL_Address_Wrapped *
531 {
532  struct RIL_Address_Wrapped *cur;
533 
534  for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
535  if (cur->address_naked == address)
536  return cur;
537  return NULL;
538 }
539 
540 
541 static int
542 agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
543 {
544  int address_index;
545 
546  switch (action)
547  {
548  case RIL_ACTION_NOTHING:
549  return GNUNET_YES;
550  break;
553  if (agent->bw_in >= RIL_MAX_BW)
554  return GNUNET_NO;
555  else
556  return GNUNET_YES;
557  break;
560  if (agent->bw_in <= 0)
561  return GNUNET_NO;
562  else
563  return GNUNET_YES;
564  break;
567  if (agent->bw_out >= RIL_MAX_BW)
568  return GNUNET_NO;
569  else
570  return GNUNET_YES;
571  break;
574  if (agent->bw_out <= 0)
575  return GNUNET_NO;
576  else
577  return GNUNET_YES;
578  break;
579  default:
580  if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
581  {
582  address_index = action - RIL_ACTION_TYPE_NUM;
583 
584  GNUNET_assert(address_index >= 0);
586  address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
587 
588  if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
589  agent->address_inuse->active)
590  return GNUNET_NO;
591  else
592  return GNUNET_YES;
593  break;
594  }
595  // error - action does not exist
597  }
598 }
599 
600 
609 static int
610 agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
611 {
612  int i;
613  int max_i = RIL_ACTION_INVALID;
614  double cur_q;
615  double max_q = -DBL_MAX;
616 
617  for (i = 0; i < agent->n; i++)
618  {
619  if (agent_action_is_possible(agent, i))
620  {
621  cur_q = agent_q (agent, state, i);
622  if (cur_q > max_q)
623  {
624  max_q = cur_q;
625  max_i = i;
626  }
627  }
628  }
629 
631 
632  return max_i;
633 }
634 
641 static int
643 {
644  int i;
645  int is_possible[agent->n];
646  int sum = 0;
647  int r;
648 
649  for (i = 0; i<agent->n; i++)
650  {
651  if (agent_action_is_possible(agent, i))
652  {
653  is_possible[i] = GNUNET_YES;
654  sum++;
655  }
656  else
657  {
658  is_possible[i] = GNUNET_NO;
659  }
660  }
661 
663 
664  sum = -1;
665  for (i = 0; i<agent->n; i++)
666  {
667  if (is_possible[i])
668  {
669  sum++;
670  if (sum == r)
671  return i;
672  }
673  }
674 
676  return RIL_ACTION_INVALID;
677 }
678 
679 
688 static void
689 agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
690 {
691  int i;
692  int k;
693  double delta;
694  double **theta = agent->W;
695 
696  delta = agent->envi->global_discount_integrated * reward; //reward
697  delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
698  delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
699 
700 // 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",
701 // agent->step_count,
702 // agent_q (agent, agent->s_old, agent->a_old),
703 // agent->envi->parameters.alpha,
704 // reward,
705 // agent->envi->global_discount_variable,
706 // agent_q (agent, s_next, a_prime),
707 // delta);
708 
709  for (k = 0; k < agent->n; k++)
710  {
711  for (i = 0; i < agent->m; i++)
712  {
713  // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
714  // agent->envi->parameters.alpha,
715  // delta,
716  // i,
717  // agent->e[i]);
718  theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
719  }
720  }
721 }
722 
723 
736 static void
738  enum RIL_E_Modification mod,
739  double *feature,
740  int action)
741 {
742  int i;
743  int k;
744 
745  for (i = 0; i < agent->m; i++)
746  {
747  switch (mod)
748  {
749  case RIL_E_ACCUMULATE:
750  agent->E[action][i] += feature[i];
751  break;
752  case RIL_E_REPLACE:
753  agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
754  break;
755  case RIL_E_DECAY:
756  for (k = 0; k < agent->n; k++)
757  {
758  agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
759  }
760  break;
761  case RIL_E_ZERO:
762  for (k = 0; k < agent->n; k++)
763  {
764  agent->E[k][i] = 0;
765  }
766  break;
767  }
768  }
769 }
770 
778 static void
779 ril_inform (struct GAS_RIL_Handle *solver,
781  enum GAS_Solver_Status stat)
782 {
783  solver->env->info_cb (solver->env->cls,
784  op,
785  stat,
786  GAS_INFO_NONE);
787 }
788 
794 static unsigned long long
796 {
798 }
799 
811 static void
813  struct RIL_Peer_Agent *agent,
814  struct ATS_Address *new_address,
815  unsigned long long new_bw_in,
816  unsigned long long new_bw_out,
817  int silent)
818 {
819  int notify = GNUNET_NO;
820 
822  " set_active_suggestion() for peer '%s'\n",
823  GNUNET_i2s (&agent->peer));
824 
825  //address change
826  if (agent->address_inuse != new_address)
827  {
828  if (NULL != agent->address_inuse)
829  {
830  agent->address_inuse->active = GNUNET_NO;
831  agent->address_inuse->assigned_bw_in = 0;
832  agent->address_inuse->assigned_bw_out = 0;
833  }
834  if (NULL != new_address)
835  {
836  LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
837  new_address->active = agent->is_active;
838  new_address->assigned_bw_in = agent->bw_in;
839  new_address->assigned_bw_out = agent->bw_out;
840  }
841  notify |= GNUNET_YES;
842  }
843 
844  if (new_address)
845  {
846  //activity change
847  if (new_address->active != agent->is_active)
848  {
849  new_address->active = agent->is_active;
850  notify |= GNUNET_YES;
851  }
852 
853  //bw change
854  if (agent->bw_in != new_bw_in)
855  {
856  agent->bw_in = new_bw_in;
857  new_address->assigned_bw_in = new_bw_in;
858  notify |= GNUNET_YES;
859  }
860  if (agent->bw_out != new_bw_out)
861  {
862  agent->bw_out = new_bw_out;
863  new_address->assigned_bw_out = new_bw_out;
864  notify |= GNUNET_YES;
865  }
866  }
867 
868  if (notify && agent->is_active && (GNUNET_NO == silent))
869  {
870  if (new_address)
871  {
872  LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
873  agent->suggestion_issue = GNUNET_YES;
874  agent->suggestion_address = new_address;
875  }
876  else if (agent->address_inuse)
877  {
878  /* disconnect case, no new address */
881  agent->bw_in = 0;
882  agent->bw_out = 0;
883 
884  agent->suggestion_issue = GNUNET_YES;
885  agent->suggestion_address = agent->address_inuse;
886  }
887  }
888  agent->address_inuse = new_address;
889 }
890 
891 
898 static double *
899 envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
900 {
901  double *state;
902  double y[2];
903  double x[2];
904  double d[2];
905  double sigma;
906  double f;
907  int m;
908  int i;
909  int k;
910  unsigned long long max_bw;
911 
912  state = GNUNET_malloc (sizeof(double) * agent->m);
913 
914  max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
915 
916  y[0] = (double) agent->bw_out;
917  y[1] = (double) agent->bw_in;
918 
919  m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
920  for (i = 0; i <= solver->parameters.rbf_divisor; i++)
921  {
922  for (k = 0; k <= solver->parameters.rbf_divisor; k++)
923  {
924  x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
925  x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
926  d[0] = x[0]-y[0];
927  d[1] = x[1]-y[1];
928  sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
929  f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
930  state[m++] = f;
931  }
932  }
933 
934  return state;
935 }
936 
937 
944 static double
946 {
947  const double *preferences;
948  double delay_atsi;
949  double delay_norm;
950  double pref_match;
951 
952  preferences = agent->envi->env->get_preferences (agent->envi->env->cls,
953  &agent->peer);
954 
955  delay_atsi = agent->address_inuse->norm_delay.norm;
956  delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
957 
958  pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
959  pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
960  sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
961  return pref_match;
962 }
963 
972 static double
974 {
975  struct RIL_Peer_Agent *cur;
976  double result;
977 
978  switch (solver->parameters.social_welfare)
979  {
981  result = DBL_MAX;
982  for (cur = solver->agents_head; NULL != cur; cur = cur->next)
983  {
984  if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
985  {
986  result = GNUNET_MIN(result, agent_get_utility(cur));
987  }
988  }
989  return result;
990 
991  case RIL_WELFARE_NASH:
992  result = 0;
993  for (cur = solver->agents_head; NULL != cur; cur = cur->next)
994  {
995  if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
996  {
997  result *= pow(agent_get_utility(cur), 1.0 / (double) scope->active_agent_count);
998  }
999  }
1000  return result;
1001  }
1003  return 1;
1004 }
1005 
1006 static double
1007 envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1008 {
1009  struct RIL_Scope *net;
1010  unsigned long long over_max;
1011  unsigned long long over_in = 0;
1012  unsigned long long over_out = 0;
1013 
1014  net = agent->address_inuse->solver_information;
1015 
1016  if (net->bw_in_utilized > net->bw_in_available)
1017  {
1018  over_in = net->bw_in_utilized - net->bw_in_available;
1019  if (RIL_ACTION_BW_IN_INC == agent->a_old)
1020  {
1021  /* increase quadratically */
1022  over_in *= over_in;
1023  }
1024  }
1025  if (net->bw_out_utilized > net->bw_out_available)
1026  {
1027  over_out = net->bw_out_utilized - net->bw_out_available;
1028  if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1029  {
1030  /* increase quadratically */
1031  over_out *= over_out;
1032  }
1033  }
1034  over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
1035 
1036  return -1.0 * (double) over_max;
1037 }
1038 
1048 static double
1049 envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1050 {
1051  struct RIL_Scope *net;
1052  double objective;
1053  double delta;
1054  double steady;
1055  double penalty;
1056  double reward;
1057 
1058  net = agent->address_inuse->solver_information;
1059 
1060  penalty = envi_get_penalty(solver, agent);
1061  objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1062  delta = objective - agent->objective_old;
1063  agent->objective_old = objective;
1064 
1065  if (delta != 0 && penalty == 0)
1066  {
1067  agent->nop_bonus = delta * RIL_NOP_DECAY;
1068  }
1069  else
1070  {
1071  agent->nop_bonus *= RIL_NOP_DECAY;
1072  }
1073 
1074  steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1075 
1076  reward = delta + steady;
1077  return reward + penalty;
1078 }
1079 
1087 static void
1089  struct RIL_Peer_Agent *agent,
1090  int direction_in)
1091 {
1092  unsigned long long new_bw;
1093  unsigned long long max_bw;
1094 
1095  max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1096 
1097  if (direction_in)
1098  {
1099  new_bw = agent->bw_in * 2;
1100  if (new_bw < agent->bw_in || new_bw > max_bw)
1101  new_bw = max_bw;
1102  envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1103  agent->bw_out, GNUNET_NO);
1104  }
1105  else
1106  {
1107  new_bw = agent->bw_out * 2;
1108  if (new_bw < agent->bw_out || new_bw > max_bw)
1109  new_bw = max_bw;
1110  envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1111  new_bw, GNUNET_NO);
1112  }
1113 }
1114 
1124 static void
1126  struct RIL_Peer_Agent *agent,
1127  int direction_in)
1128 {
1129  unsigned long long new_bw;
1130 
1131  if (direction_in)
1132  {
1133  new_bw = agent->bw_in / 2;
1134  if (new_bw <= 0 || new_bw > agent->bw_in)
1135  new_bw = 0;
1136  envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1137  GNUNET_NO);
1138  }
1139  else
1140  {
1141  new_bw = agent->bw_out / 2;
1142  if (new_bw <= 0 || new_bw > agent->bw_out)
1143  new_bw = 0;
1144  envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1145  GNUNET_NO);
1146  }
1147 }
1148 
1157 static void
1158 envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1159 {
1160  unsigned long long new_bw;
1161  unsigned long long max_bw;
1162 
1163  max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
1164 
1165  if (direction_in)
1166  {
1167  new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1168  if (new_bw < agent->bw_in || new_bw > max_bw)
1169  new_bw = max_bw;
1170  envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1171  agent->bw_out, GNUNET_NO);
1172  }
1173  else
1174  {
1175  new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1176  if (new_bw < agent->bw_out || new_bw > max_bw)
1177  new_bw = max_bw;
1178  envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1179  new_bw, GNUNET_NO);
1180  }
1181 }
1182 
1192 static void
1193 envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
1194 {
1195  unsigned long long new_bw;
1196 
1197  if (direction_in)
1198  {
1199  new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1200  if (new_bw <= 0 || new_bw > agent->bw_in)
1201  new_bw = 0;
1202  envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1203  GNUNET_NO);
1204  }
1205  else
1206  {
1207  new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1208  if (new_bw <= 0 || new_bw > agent->bw_out)
1209  new_bw = 0;
1210  envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1211  GNUNET_NO);
1212  }
1213 }
1214 
1222 static void
1224  struct RIL_Peer_Agent *agent,
1225  unsigned int address_index)
1226 {
1227  struct RIL_Address_Wrapped *cur;
1228  int i = 0;
1229 
1230  //cur = agent_address_get_wrapped(agent, agent->address_inuse);
1231 
1232  for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1233  {
1234  if (i == address_index)
1235  {
1236  envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
1237  GNUNET_NO);
1238  return;
1239  }
1240 
1241  i++;
1242  }
1243 
1244  //no address with address_index exists, in this case this action should not be callable
1246 }
1247 
1255 static void
1256 envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
1257 {
1258  int address_index;
1259 
1260  switch (action)
1261  {
1262  case RIL_ACTION_NOTHING:
1263  break;
1264  case RIL_ACTION_BW_IN_DBL:
1265  envi_action_bw_double (solver, agent, GNUNET_YES);
1266  break;
1267  case RIL_ACTION_BW_IN_HLV:
1268  envi_action_bw_halven (solver, agent, GNUNET_YES);
1269  break;
1270  case RIL_ACTION_BW_IN_INC:
1271  envi_action_bw_inc (solver, agent, GNUNET_YES);
1272  break;
1273  case RIL_ACTION_BW_IN_DEC:
1274  envi_action_bw_dec (solver, agent, GNUNET_YES);
1275  break;
1276  case RIL_ACTION_BW_OUT_DBL:
1277  envi_action_bw_double (solver, agent, GNUNET_NO);
1278  break;
1279  case RIL_ACTION_BW_OUT_HLV:
1280  envi_action_bw_halven (solver, agent, GNUNET_NO);
1281  break;
1282  case RIL_ACTION_BW_OUT_INC:
1283  envi_action_bw_inc (solver, agent, GNUNET_NO);
1284  break;
1285  case RIL_ACTION_BW_OUT_DEC:
1286  envi_action_bw_dec (solver, agent, GNUNET_NO);
1287  break;
1288  default:
1289  if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
1290  {
1291  address_index = action - RIL_ACTION_TYPE_NUM;
1292 
1293  GNUNET_assert(address_index >= 0);
1294  GNUNET_assert(
1295  address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
1296 
1297  envi_action_address_switch (solver, agent, address_index);
1298  break;
1299  }
1300  // error - action does not exist
1302  }
1303 }
1304 
1316 static int
1318 {
1319  int action;
1321  UINT32_MAX) / (double) UINT32_MAX;
1322 
1323  if (r < agent->envi->parameters.epsilon) //explore
1324  {
1325  action = agent_get_action_random(agent);
1326  if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1327  {
1328  agent->eligibility_reset = GNUNET_YES;
1329  }
1330  agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
1331  return action;
1332  }
1333  else //exploit
1334  {
1335  action = agent_get_action_max(agent, state);
1336  return action;
1337  }
1338 }
1339 
1350 static int
1352 {
1353  int i;
1354  int a_max;
1355  double eqt[agent->n];
1356  double p[agent->n];
1357  double sum = 0;
1358  double r;
1359 
1360  a_max = agent_get_action_max(agent, state);
1361 
1362  for (i=0; i<agent->n; i++)
1363  {
1364  if (agent_action_is_possible(agent, i))
1365  {
1366  eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
1367  if (isinf (eqt[i]))
1368  eqt[i] = isinf(eqt[i]) * UINT32_MAX;
1369  sum += eqt[i];
1370  }
1371  }
1372  for (i=0; i<agent->n; i++)
1373  {
1374  if (agent_action_is_possible(agent, i))
1375  {
1376  p[i] = eqt[i]/sum;
1377  }
1378  else
1379  {
1380  p[i] = 0;
1381  }
1382  }
1384  UINT32_MAX) / (double) UINT32_MAX;
1385  sum = 0;
1386  for (i=0; i<agent->n; i++)
1387  {
1388  if (sum + p[i] > r)
1389  {
1390  if (i != a_max)
1391  {
1392  if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1393  agent->eligibility_reset = GNUNET_YES;
1395  }
1396  return i;
1397  }
1398  sum += p[i];
1399  }
1401  return RIL_ACTION_INVALID;
1402 }
1403 
1412 static int
1413 agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1414 {
1415  if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1416  {
1417  return agent_select_egreedy(agent, state);
1418  }
1419  else
1420  {
1421  return agent_select_softmax(agent, state);
1422  }
1423 }
1424 
1433 static void
1435 {
1436  int a_next = RIL_ACTION_INVALID;
1437  int a_max;
1438  double *s_next;
1439  double reward;
1440 
1441  LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1442  GNUNET_i2s (&agent->peer),
1443  agent->envi->parameters.algorithm ? "Q" : "SARSA");
1444 
1445  s_next = envi_get_state (agent->envi, agent);
1446  reward = envi_get_reward (agent->envi, agent);
1447 
1448  if (agent->eligibility_reset)
1449  {
1450  agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
1451  agent->eligibility_reset = GNUNET_NO;
1452  }
1453  else
1454  {
1455  agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
1456  }
1457  if (RIL_ACTION_INVALID != agent->a_old)
1458  {
1459  agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
1460  }
1461 
1462  switch (agent->envi->parameters.algorithm)
1463  {
1464  case RIL_ALGO_SARSA:
1465  a_next = agent_select_action (agent, s_next);
1466  if (RIL_ACTION_INVALID != agent->a_old)
1467  {
1468  //updates weights with selected action (on-policy), if not first step
1469  agent_update (agent, reward, s_next, a_next);
1470  }
1471  break;
1472 
1473  case RIL_ALGO_Q:
1474  a_max = agent_get_action_max (agent, s_next);
1475  if (RIL_ACTION_INVALID != agent->a_old)
1476  {
1477  //updates weights with best action, disregarding actually selected action (off-policy), if not first step
1478  agent_update (agent, reward, s_next, a_max);
1479  }
1480  a_next = agent_select_action (agent, s_next);
1481  break;
1482  }
1483 
1484  GNUNET_assert(RIL_ACTION_INVALID != a_next);
1485 
1486  LOG (GNUNET_ERROR_TYPE_DEBUG, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1487  agent->step_count,
1488  reward,
1489  agent->bw_in/1024,
1490  agent->bw_out/1024,
1491  a_next);
1492 
1493  envi_do_action (agent->envi, agent, a_next);
1494 
1495  GNUNET_free(agent->s_old);
1496  agent->s_old = s_next;
1497  agent->a_old = a_next;
1498 
1499  agent->step_count += 1;
1500 }
1501 
1507 static void
1508 ril_step (struct GAS_RIL_Handle *solver);
1509 
1510 
1517 static void
1519 {
1520  struct GAS_RIL_Handle *solver = cls;
1521 
1522  solver->step_next_task_id = NULL;
1523  ril_step (solver);
1524 }
1525 
1534 static double
1536 {
1537  int i;
1538  struct RIL_Scope net;
1539  unsigned long long sum_assigned = 0;
1540  unsigned long long sum_available = 0;
1541  double ratio;
1542 
1543  for (i = 0; i < solver->networks_count; i++)
1544  {
1545  net = solver->network_entries[i];
1546  if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
1547  {
1548  sum_assigned += net.bw_in_utilized;
1549  sum_assigned += net.bw_out_utilized;
1550  sum_available += net.bw_in_available;
1551  sum_available += net.bw_out_available;
1552  }
1553  }
1554  if (sum_available > 0)
1555  {
1556  ratio = ((double) sum_assigned) / ((double) sum_available);
1557  }
1558  else
1559  {
1560  ratio = 0;
1561  }
1562 
1563  return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
1564 }
1565 
1573 static struct RIL_Scope *
1574 ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1575 {
1576  int i;
1577 
1578  for (i = 0; i < s->networks_count; i++)
1579  {
1580  if (s->network_entries[i].type == type)
1581  {
1582  return &s->network_entries[i];
1583  }
1584  }
1585  return NULL ;
1586 }
1587 
1597 static int
1599 {
1600  struct RIL_Scope *net;
1601  struct RIL_Peer_Agent *agent;
1602  unsigned long long address_count = 0;
1603 
1604  for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1605  {
1606  if (agent->address_inuse && agent->is_active)
1607  {
1608  net = agent->address_inuse->solver_information;
1609  if (net->type == network)
1610  {
1611  address_count++;
1612  }
1613  }
1614  }
1615 
1616  net = ril_get_network (solver, network);
1617  return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1618 }
1619 
1630 static void
1631 ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
1632 {
1633  struct RIL_Address_Wrapped *addr_wrap;
1634  struct RIL_Scope *net;
1635  unsigned long long start_in;
1636  unsigned long long start_out;
1637 
1638  for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
1639  {
1640  net = addr_wrap->address_naked->solver_information;
1641  if (ril_network_is_not_full(solver, net->type))
1642  {
1643  if (NULL == agent->address_inuse)
1644  {
1645  start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
1646  start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
1647  envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, start_in, start_out, silent);
1648  }
1649  return;
1650  }
1651  }
1652  agent->address_inuse = NULL;
1653 }
1654 
1661 static void
1663 {
1664  struct GNUNET_TIME_Absolute time_now;
1665  struct GNUNET_TIME_Relative time_delta;
1666  double tau;
1667 
1668  // MDP case only for debugging purposes
1669  if (solver->simulate)
1670  {
1671  solver->global_discount_variable = solver->parameters.gamma;
1672  solver->global_discount_integrated = 1;
1673  return;
1674  }
1675 
1676  // semi-MDP case
1677 
1678  //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1679  time_now = GNUNET_TIME_absolute_get ();
1680  time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
1681  solver->step_time_last = time_now;
1682  tau = (double) time_delta.rel_value_us
1683  / (double) solver->parameters.step_time_min.rel_value_us;
1684 
1685  //calculate reward discounts (once per step for all agents)
1686  solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
1687  solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1688  / (double) solver->parameters.beta;
1689 }
1690 
1698 static int
1700 {
1701  int c = 0;
1702  struct RIL_Peer_Agent *cur_agent;
1703 
1704  for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1705  {
1706  if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1707  {
1708  c++;
1709  }
1710  }
1711  return c;
1712 }
1713 
1724 static unsigned long long
1725 ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType type, int direction_in)
1726 {
1727  struct RIL_Peer_Agent *cur;
1728  struct RIL_Scope *net;
1729  unsigned long long sum = 0;
1730 
1731  for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1732  {
1733  if (cur->is_active && cur->address_inuse)
1734  {
1735  net = cur->address_inuse->solver_information;
1736  if (net->type == type)
1737  {
1738  if (direction_in)
1739  sum += cur->bw_in;
1740  else
1741  sum += cur->bw_out;
1742  }
1743  }
1744  }
1745 
1746  return sum;
1747 }
1748 
1759 static unsigned long long
1760 ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType type, int direction_in)
1761 {
1762  struct RIL_Peer_Agent *cur;
1763  struct RIL_Scope *net;
1764  unsigned long long sum = 0;
1765 
1766  for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1767  {
1768  if (cur->is_active && cur->address_inuse)
1769  {
1770  net = cur->address_inuse->solver_information;
1771  if (net->type == type)
1772  {
1773  if (direction_in)
1774  sum += cur->address_inuse->norm_utilization_in.norm;
1775  else
1777  }
1778  }
1779  }
1780 
1781  return sum;
1782 }
1783 
1790 static void
1792 {
1793  int c;
1794  struct RIL_Scope *net;
1795 
1796  for (c = 0; c < solver->networks_count; c++)
1797  {
1798  net = &solver->network_entries[c];
1804  net->social_welfare = ril_network_get_social_welfare(solver, net);
1805  }
1806 }
1807 
1815 static void
1817 {
1818  double used_ratio;
1819  double factor;
1820  double y;
1821  double offset;
1822  struct GNUNET_TIME_Relative time_next;
1823 
1824  used_ratio = ril_get_used_resource_ratio (solver);
1825 
1826  GNUNET_assert(
1829 
1830  factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1832  offset = (double) solver->parameters.step_time_min.rel_value_us;
1833  y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1834 
1835  GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
1836  GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
1837 
1838  time_next = GNUNET_TIME_relative_saturating_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1839 
1840 // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1841 // used_ratio,
1842 // factor,
1843 // offset,
1844 // y);
1845 
1846  if (solver->simulate)
1847  {
1848  time_next = GNUNET_TIME_UNIT_ZERO;
1849  }
1850 
1851  if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1852  {
1854  solver);
1855  }
1856 }
1857 
1863 static void
1864 ril_step (struct GAS_RIL_Handle *solver)
1865 {
1866  struct RIL_Peer_Agent *cur;
1867 
1868  if (GNUNET_YES == solver->bulk_lock)
1869  {
1870  solver->bulk_changes++;
1871  return;
1872  }
1873 
1875 
1876  LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1877 
1878  if (0 == solver->step_count)
1879  {
1881  }
1882 
1883  ril_calculate_discount (solver);
1884  ril_networks_update_state (solver);
1885 
1886  //trigger one step per active, unblocked agent
1887  for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1888  {
1889  if (cur->is_active)
1890  {
1891  if (NULL == cur->address_inuse)
1892  {
1893  ril_try_unblock_agent(solver, cur, GNUNET_NO);
1894  }
1895  if (cur->address_inuse)
1896  {
1897  agent_step (cur);
1898  }
1899  }
1900  }
1901 
1902  ril_networks_update_state (solver);
1903 
1904  solver->step_count++;
1905  ril_step_schedule_next (solver);
1906 
1908 
1910  for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1911  {
1912  if (cur->suggestion_issue) {
1913  solver->env->bandwidth_changed_cb (solver->env->cls,
1914  cur->suggestion_address);
1915  cur->suggestion_issue = GNUNET_NO;
1916  }
1917  }
1919 }
1920 
1926 static void
1928 {
1929  int i;
1930  int k;
1931 
1932  for (i = 0; i < agent->n; i++)
1933  {
1934  for (k = 0; k < agent->m; k++)
1935  {
1936  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));
1937  }
1938  }
1939 }
1940 
1948 static struct RIL_Peer_Agent *
1949 agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1950 {
1951  int i;
1952  struct GAS_RIL_Handle * solver = s;
1953  struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
1954 
1955  agent->envi = solver;
1956  agent->peer = *peer;
1957  agent->step_count = 0;
1958  agent->is_active = GNUNET_NO;
1959  agent->bw_in = RIL_MIN_BW;
1960  agent->bw_out = RIL_MIN_BW;
1961  agent->suggestion_issue = GNUNET_NO;
1962  agent->n = RIL_ACTION_TYPE_NUM;
1963  agent->m = 0;
1964  agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1965  agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
1966  for (i = 0; i < agent->n; i++)
1967  {
1968  agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1969  agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1970  }
1971  agent_w_init(agent);
1972  agent->eligibility_reset = GNUNET_NO;
1973  agent->a_old = RIL_ACTION_INVALID;
1974  agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1975  agent->address_inuse = NULL;
1976  agent->objective_old = 0;
1977  agent->nop_bonus = 0;
1978 
1979  return agent;
1980 }
1981 
1988 static void
1989 agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1990 {
1991  int i;
1992 
1993  for (i = 0; i < agent->n; i++)
1994  {
1995  GNUNET_free_non_null(agent->W[i]);
1996  GNUNET_free_non_null(agent->E[i]);
1997  }
1998  GNUNET_free_non_null(agent->W);
1999  GNUNET_free_non_null(agent->E);
2000  GNUNET_free_non_null(agent->s_old);
2001  GNUNET_free(agent);
2002 }
2003 
2012 static struct RIL_Peer_Agent *
2013 ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
2014 {
2015  struct RIL_Peer_Agent *cur;
2016 
2017  for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2018  {
2019  if (0 == GNUNET_memcmp (peer, &cur->peer))
2020  {
2021  return cur;
2022  }
2023  }
2024 
2025  if (create)
2026  {
2027  cur = agent_init (solver, peer);
2029  return cur;
2030  }
2031  return NULL ;
2032 }
2033 
2042 static int
2044 {
2045  struct RIL_Scope *net;
2046 
2047  net = ril_get_network (solver, network);
2048  return net->bw_out_available >= RIL_MIN_BW;
2049 }
2050 
2062 static void
2064  size_t element_size,
2065  unsigned int hole_start,
2066  unsigned int hole_length,
2067  unsigned int old_length)
2068 {
2069  char *tmpptr;
2070  char *oldptr = (char *) *old;
2071  size_t size;
2072  unsigned int bytes_before;
2073  unsigned int bytes_hole;
2074  unsigned int bytes_after;
2075 
2076  GNUNET_assert(old_length >= hole_length);
2077  GNUNET_assert(old_length >= (hole_start + hole_length));
2078 
2079  size = element_size * (old_length - hole_length);
2080 
2081  bytes_before = element_size * hole_start;
2082  bytes_hole = element_size * hole_length;
2083  bytes_after = element_size * (old_length - hole_start - hole_length);
2084 
2085  if (0 == size)
2086  {
2087  tmpptr = NULL;
2088  }
2089  else
2090  {
2091  tmpptr = GNUNET_malloc (size);
2092  GNUNET_memcpy (tmpptr, oldptr, bytes_before);
2093  GNUNET_memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
2094  }
2095  if (NULL != *old)
2096  {
2097  GNUNET_free(*old);
2098  }
2099  *old = (void *) tmpptr;
2100 }
2101 
2102 /*
2103  * Solver API functions
2104  * ---------------------------
2105  */
2106 
2115 static void
2117  const struct GNUNET_PeerIdentity *peer,
2118  enum GNUNET_ATS_PreferenceKind kind,
2119  double pref_rel)
2120 {
2122  "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2123  GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2124 
2125  struct GAS_RIL_Handle *s = solver;
2126 
2129  ril_step (s);
2130 }
2131 
2132 
2142 static void
2143 GAS_ril_address_add (void *solver,
2144  struct ATS_Address *address,
2145  uint32_t network)
2146 {
2147  struct GAS_RIL_Handle *s = solver;
2148  struct RIL_Peer_Agent *agent;
2149  struct RIL_Address_Wrapped *address_wrapped;
2150  struct RIL_Scope *net;
2151  unsigned int m_new;
2152  unsigned int m_old;
2153  unsigned int n_new;
2154  unsigned int n_old;
2155  int i;
2156  unsigned int zero;
2157 
2159  "API_address_add()\n");
2160 
2161  net = ril_get_network (s, network);
2162  address->solver_information = net;
2163 
2164  if (!ril_network_is_active (s, network))
2165  {
2167  "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2168  address->plugin, address->addr, GNUNET_i2s (&address->peer));
2169  return;
2170  }
2171 
2174 
2175  agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2176 
2177  //add address
2178  address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2179  address_wrapped->address_naked = address;
2180  GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2181 
2182  //increase size of W
2183  m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2184  m_old = agent->m;
2185  n_new = agent->n + 1;
2186  n_old = agent->n;
2187 
2188  GNUNET_array_grow(agent->W, agent->n, n_new);
2189  agent->n = n_old;
2190  GNUNET_array_grow(agent->E, agent->n, n_new);
2191  for (i = 0; i < n_new; i++)
2192  {
2193  if (i < n_old)
2194  {
2195  agent->m = m_old;
2196  GNUNET_array_grow(agent->W[i], agent->m, m_new);
2197  agent->m = m_old;
2198  GNUNET_array_grow(agent->E[i], agent->m, m_new);
2199  }
2200  else
2201  {
2202  zero = 0;
2203  GNUNET_array_grow(agent->W[i], zero, m_new);
2204  zero = 0;
2205  GNUNET_array_grow(agent->E[i], zero, m_new);
2206  }
2207  }
2208 
2209  //increase size of old state vector
2210  agent->m = m_old;
2211  GNUNET_array_grow(agent->s_old, agent->m, m_new);
2212 
2213  ril_try_unblock_agent(s, agent, GNUNET_NO);
2214 
2215  ril_step (s);
2216 
2217  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
2218  address->active ? "active" : "inactive", address->plugin, address->addr,
2219  GNUNET_i2s (&address->peer));
2220 }
2221 
2230 static void
2232  struct ATS_Address *address)
2233 {
2234  struct GAS_RIL_Handle *s = solver;
2235  struct RIL_Peer_Agent *agent;
2236  struct RIL_Address_Wrapped *address_wrapped;
2237  int address_index;
2238  unsigned int m_new;
2239  unsigned int n_new;
2240  int i;
2241  struct RIL_Scope *net;
2242 
2244  "API_address_delete() Delete %s %s address %s for peer '%s'\n",
2245  address->active ? "active" : "inactive",
2246  address->plugin,
2247  address->addr,
2248  GNUNET_i2s (&address->peer));
2249 
2250  agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2251  if (NULL == agent)
2252  {
2253  net = address->solver_information;
2256  "No agent allocated for peer yet, since address was in inactive network\n");
2257  return;
2258  }
2259 
2262 
2263  address_index = agent_address_get_index (agent, address);
2264  address_wrapped = agent_address_get_wrapped (agent, address);
2265 
2266  if (NULL == address_wrapped)
2267  {
2268  net = address->solver_information;
2270  "Address not considered by agent, address was in inactive network\n");
2271  return;
2272  }
2274  agent->addresses_tail,
2275  address_wrapped);
2276  GNUNET_free (address_wrapped);
2277 
2278  //decrease W
2279  m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2280  n_new = agent->n - 1;
2281 
2282  for (i = 0; i < agent->n; i++)
2283  {
2284  ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2285  address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2286  ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2287  ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2288  address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2289  ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2290  }
2291  GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2292  GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2293  ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2294  1, agent->n);
2295  ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2296  1, agent->n);
2297  //correct last action
2298  if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2299  {
2300  agent->a_old -= 1;
2301  }
2302  else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2303  {
2304  agent->a_old = RIL_ACTION_INVALID;
2305  }
2306  //decrease old state vector
2307  ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2308  address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2309  ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2310  agent->m = m_new;
2311  agent->n = n_new;
2312 
2313  if (agent->address_inuse == address)
2314  {
2315  if (NULL != agent->addresses_head) //if peer has an address left, use it
2316  {
2318  "Active address died, suggesting alternative!\n");
2320  agent,
2321  agent->addresses_head->address_naked,
2322  agent->bw_in,
2323  agent->bw_out,
2324  GNUNET_YES);
2325  }
2326  else
2327  {
2329  "Active address died, suggesting disconnect!\n");
2330  envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2331  }
2332  }
2333  ril_step (solver);
2334  if (agent->suggestion_address == address)
2335  {
2336  agent->suggestion_issue = GNUNET_NO;
2337  agent->suggestion_address = NULL;
2338  }
2339  GNUNET_assert (agent->address_inuse != address);
2340 }
2341 
2342 
2349 static void
2351  struct ATS_Address *address)
2352 {
2353  struct GAS_RIL_Handle *s = solver;
2354 
2356  "Properties for peer '%s' address changed\n",
2357  GNUNET_i2s (&address->peer));
2360  ril_step (s);
2361 }
2362 
2363 
2374 static void
2376  struct GNUNET_SERVICE_Client *application,
2377  const struct GNUNET_PeerIdentity *peer,
2378  const struct GNUNET_TIME_Relative scope,
2379  enum GNUNET_ATS_PreferenceKind kind,
2380  double score)
2381 {
2383  "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2384  "preference %s for %d seconds\n",
2385  GNUNET_i2s (peer),
2386  "UNKNOWN",
2388  scope.rel_value_us / 1000000);
2389 }
2390 
2391 
2397 static void
2398 GAS_ril_bulk_start (void *solver)
2399 {
2400  struct GAS_RIL_Handle *s = solver;
2401 
2403  "API_bulk_start() lock: %d\n", s->bulk_lock+1);
2404 
2405  s->bulk_lock++;
2406 }
2407 
2408 
2414 static void
2415 GAS_ril_bulk_stop (void *solver)
2416 {
2417  struct GAS_RIL_Handle *s = solver;
2418 
2420  "API_bulk_stop() lock: %d\n",
2421  s->bulk_lock - 1);
2422 
2423  if (s->bulk_lock < 1)
2424  {
2425  GNUNET_break(0);
2426  return;
2427  }
2428  s->bulk_lock--;
2429 
2430  if (0 < s->bulk_changes)
2431  {
2432  ril_step (solver);
2433  s->bulk_changes = 0;
2434  }
2435 }
2436 
2437 
2448 static void
2450  const struct GNUNET_PeerIdentity *peer)
2451 {
2452  struct GAS_RIL_Handle *s = solver;
2453  struct RIL_Peer_Agent *agent;
2454 
2455  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2456 
2457  agent = ril_get_agent (s, peer, GNUNET_YES);
2458 
2459  agent->is_active = GNUNET_YES;
2460  envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
2461 
2462  ril_try_unblock_agent(solver, agent, GNUNET_YES);
2463 
2464  if (agent->address_inuse)
2465  {
2467  "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2468  GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
2469  }
2470  else
2471  {
2473  "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2474  GNUNET_i2s (peer));
2477  }
2478  if (NULL != agent->address_inuse)
2479  s->env->bandwidth_changed_cb (s->env->cls,
2480  agent->address_inuse);
2481 }
2482 
2483 
2493 static void
2495  const struct GNUNET_PeerIdentity *peer)
2496 {
2497  struct GAS_RIL_Handle *s = solver;
2498  struct RIL_Peer_Agent *agent;
2499 
2501  "API_stop_get_preferred_address()");
2502 
2503  agent = ril_get_agent (s, peer, GNUNET_NO);
2504 
2505  if (NULL == agent)
2506  {
2507  GNUNET_break(0);
2508  return;
2509  }
2510  if (GNUNET_NO == agent->is_active)
2511  {
2512  GNUNET_break(0);
2513  return;
2514  }
2515 
2518 
2519  agent->is_active = GNUNET_NO;
2520 
2521  envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
2522  GNUNET_YES);
2523 
2524  ril_step (s);
2525 
2527  "API_stop_get_preferred_address() Paused agent for peer '%s'\n",
2528  GNUNET_i2s (peer));
2529 }
2530 
2531 
2537 void *
2539 {
2540  static struct GNUNET_ATS_SolverFunctions sf;
2542  struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2543  struct RIL_Scope * cur;
2544  int c;
2545  char *string;
2546  float f_tmp;
2547 
2549  "API_init() Initializing RIL solver\n");
2550 
2551  GNUNET_assert (NULL != env);
2552  GNUNET_assert (NULL != env->cfg);
2553  GNUNET_assert (NULL != env->stats);
2554  GNUNET_assert (NULL != env->bandwidth_changed_cb);
2555  GNUNET_assert (NULL != env->get_preferences);
2556 
2557  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
2558  {
2560  }
2561 
2562  if (GNUNET_OK
2563  != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
2564  &solver->parameters.step_time_min))
2565  {
2567  }
2568 
2569  if (GNUNET_OK
2570  != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
2571  &solver->parameters.step_time_max))
2572  {
2574  }
2575 
2576  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
2577  {
2578  GNUNET_STRINGS_utf8_toupper (string, string);
2579  if (0 == strcmp (string, "SARSA"))
2580  {
2582  }
2583  if (0 == strcmp (string, "Q-LEARNING"))
2584  {
2585  solver->parameters.algorithm = RIL_ALGO_Q;
2586  }
2587 
2588  GNUNET_free (string);
2589  }
2590  else
2591  {
2593  }
2594 
2595  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
2596  {
2597  solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2598  GNUNET_free (string);
2599  }
2600  else
2601  {
2603  }
2604 
2605 
2608  "RIL_DISCOUNT_BETA", &f_tmp))
2609  {
2610  if (f_tmp < 0.0)
2611  {
2612  LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2613  "RIL_DISCOUNT_BETA", f_tmp);
2614  }
2615  else
2616  {
2617  solver->parameters.beta = f_tmp;
2618  LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2619  "RIL_DISCOUNT_BETA", f_tmp);
2620  }
2621  }
2622 
2625  "RIL_DISCOUNT_GAMMA", &f_tmp))
2626  {
2627  if ((f_tmp < 0.0) || (f_tmp > 1.0))
2628  {
2629  LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2630  "RIL_DISCOUNT_GAMMA", f_tmp);
2631  }
2632  else
2633  {
2634  solver->parameters.gamma = f_tmp;
2635  LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2636  "RIL_DISCOUNT_GAMMA", f_tmp);
2637  }
2638  }
2639 
2642  "RIL_GRADIENT_STEP_SIZE", &f_tmp))
2643  {
2644  if ((f_tmp < 0.0) || (f_tmp > 1.0))
2645  {
2646  LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2647  "RIL_GRADIENT_STEP_SIZE", f_tmp);
2648  }
2649  else
2650  {
2651  solver->parameters.alpha = f_tmp;
2652  LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2653  "RIL_GRADIENT_STEP_SIZE", f_tmp);
2654  }
2655  }
2656 
2659  "RIL_TRACE_DECAY", &f_tmp))
2660  {
2661  if ((f_tmp < 0.0) || (f_tmp > 1.0))
2662  {
2663  LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2664  "RIL_TRACE_DECAY", f_tmp);
2665  }
2666  else
2667  {
2668  solver->parameters.lambda = f_tmp;
2669  LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2670  "RIL_TRACE_DECAY", f_tmp);
2671  }
2672  }
2673 
2676  "RIL_EXPLORE_RATIO", &f_tmp))
2677  {
2678  if ((f_tmp < 0.0) || (f_tmp > 1.0))
2679  {
2680  LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2681  "RIL_EXPLORE_RATIO", f_tmp);
2682  }
2683  else
2684  {
2685  solver->parameters.epsilon_init = f_tmp;
2686  LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2687  "RIL_EXPLORE_RATIO", f_tmp);
2688  }
2689  }
2690 
2693  "RIL_EXPLORE_DECAY", &f_tmp))
2694  {
2695  if ((f_tmp < 0.0) || (f_tmp > 1.0))
2696  {
2697  LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2698  "RIL_EXPLORE_DECAY", f_tmp);
2699  }
2700  else
2701  {
2702  solver->parameters.epsilon_decay = f_tmp;
2703  LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2704  "RIL_EXPLORE_DECAY", f_tmp);
2705  }
2706  }
2707 
2710  "RIL_TEMPERATURE", &f_tmp))
2711  {
2712  if (f_tmp <= 0.0)
2713  {
2714  LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2715  "RIL_TEMPERATURE", f_tmp);
2716  }
2717  else
2718  {
2719  solver->parameters.temperature_init = f_tmp;
2720  LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2721  "RIL_TEMPERATURE", f_tmp);
2722  }
2723  }
2724 
2727  "RIL_TEMPERATURE_DECAY", &f_tmp))
2728  {
2729  if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1)
2730  {
2731  LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2732  "RIL_TEMPERATURE_DECAY", f_tmp);
2733  }
2734  else
2735  {
2736  solver->parameters.temperature_decay = f_tmp;
2737  LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2738  "RIL_TEMPERATURE_DECAY", f_tmp);
2739  }
2740  }
2741 
2742  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2743  {
2744  solver->simulate = GNUNET_NO;
2745  }
2746 
2747  if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
2748  {
2750  }
2751  else
2752  {
2754  }
2755 
2756  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2757  {
2758  solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2759  GNUNET_free (string);
2760  }
2761  else
2762  {
2764  }
2765 
2766  solver->env = env;
2767  sf.cls = solver;
2768  sf.s_add = &GAS_ril_address_add;
2777 
2778  solver->networks_count = env->network_count;
2779  solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
2780  solver->step_count = 0;
2781  solver->done = GNUNET_NO;
2782 
2783  for (c = 0; c < env->network_count; c++)
2784  {
2785  cur = &solver->network_entries[c];
2786  cur->type = c;
2787  cur->bw_in_available = env->in_quota[c];
2788  cur->bw_out_available = env->out_quota[c];
2790  "init() Quotas for %s network: IN %llu - OUT %llu\n",
2791  GNUNET_NT_to_string(cur->type),
2792  cur->bw_in_available/1024,
2793  cur->bw_out_available/1024);
2794  }
2795 
2796  LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2797  LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2798  solver->parameters.algorithm ? "Q" : "SARSA",
2799  solver->parameters.alpha,
2800  solver->parameters.beta,
2801  solver->parameters.lambda);
2802  LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
2803  solver->parameters.epsilon,
2804  solver->parameters.temperature,
2805  solver->parameters.select ? "EGREEDY" : "SOFTMAX");
2806  LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2807  solver->parameters.rbf_divisor);
2808 
2809  return &sf;
2810 }
2811 
2812 
2818 void *
2820 {
2821  struct GNUNET_ATS_SolverFunctions *sf = cls;
2822  struct GAS_RIL_Handle *s = sf->cls;
2823  struct RIL_Peer_Agent *cur_agent;
2824  struct RIL_Peer_Agent *next_agent;
2825 
2826  LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2827 
2828  s->done = GNUNET_YES;
2829 
2830  cur_agent = s->agents_head;
2831  while (NULL != cur_agent)
2832  {
2833  next_agent = cur_agent->next;
2835  agent_die (s, cur_agent);
2836  cur_agent = next_agent;
2837  }
2838 
2839  if (NULL != s->step_next_task_id)
2840  {
2842  }
2844  GNUNET_free(s);
2845 
2846  return NULL;
2847 }
2848 
2849 
2850 /* end of plugin_ats_ril.c */
struct RIL_Address_Wrapped * prev
Previous in DLL.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct GNUNET_SCHEDULER_Task * step_next_task_id
Task identifier of the next time-step to be executed.
static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero
Public key of all zeros.
GAS_solver_stop_get_preferred_address s_get_stop
Tell solver stop notifying ATS about changes for this peers.
static struct RIL_Scope * ril_get_network(struct GAS_RIL_Handle *s, uint32_t type)
Lookup network struct by type.
static void GAS_ril_address_change_preference(void *solver, const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind, double pref_rel)
Change relative preference for quality in solver.
static void GAS_ril_address_delete(void *solver, struct ATS_Address *address)
Delete an address in the solver.
After the problem was finished, start notifications about changes to addresses.
#define RIL_MAX_BW
struct RIL_Peer_Agent * agents_head
List of active peer-agents.
uint32_t bw_in
Inbound bandwidth assigned by the agent.
int is_active
Whether the agent is active or not.
struct GNUNET_ATS_PluginEnvironment * env
The solver-plugin environment of the solver-plugin API.
Global learning parameters.
int GNUNET_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
GAS_solver_address_add s_add
Add a new address for a peer to the solver.
double global_discount_integrated
Integrated variable discount factor, dependent on time between steps.
int GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
static void envi_action_bw_halven(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
Cuts the bandwidth for the active address in half.
uint64_t rel_value_us
The actual value.
const char * GNUNET_NT_to_string(enum GNUNET_NetworkType net)
Convert a enum GNUNET_NetworkType to a string.
Definition: nt.c:43
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:35
#define RIL_DEFAULT_SELECT
static unsigned int element_size
double lambda
Trace-decay factor for eligibility traces.
#define RIL_UTILITY_DELAY_MAX
static int agent_select_softmax(struct RIL_Peer_Agent *agent, double *state)
Selects the next action with a probability corresponding to its value.
struct GNUNET_STATISTICS_Handle * stats
Statistics handle to be used by the solver.
static void agent_die(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
Deallocate agent.
#define RIL_DEFAULT_DISCOUNT_GAMMA
#define RIL_DEFAULT_EXPLORE_DECAY
#define RIL_DEFAULT_RBF_DIVISOR
int bulk_changes
Number of changes during a lock.
static void GAS_ril_address_add(void *solver, struct ATS_Address *address, uint32_t network)
Add a new address for a peer to the solver.
int active
Is this the active address for this peer?
GAS_get_preferences get_preferences
ATS addresses function to obtain preference values.
unsigned int m
Number of rows of W / Number of state-vector features.
static void GAS_ril_bulk_stop(void *solver)
Bulk operation done.
struct GNUNET_PeerIdentity peer
Peer ID this address is for.
GAS_Solver_Status
Status of a GAS_Solver_Operation operation.
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
static double agent_q(struct RIL_Peer_Agent *agent, const double *state, int action)
Estimate the current action-value for state s and action a.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void ril_inform(struct GAS_RIL_Handle *solver, enum GAS_Solver_Operation op, enum GAS_Solver_Status stat)
Informs the environment about the status of the solver.
static void GAS_ril_stop_get_preferred_address(void *solver, const struct GNUNET_PeerIdentity *peer)
Tell solver stop notifying ATS about changes for this peers.
static void envi_action_bw_dec(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
Decreases the bandwidth by 5 times the minimum bandwidth for the active address.
No more specific information.
A handle for the reinforcement learning solver.
const void * addr
Address (in plugin-specific binary format).
static int agent_get_action_max(struct RIL_Peer_Agent *agent, double *state)
Gets the action, with the maximal estimated Q-value (i.e.
struct RIL_Address_Wrapped * addresses_head
Head of addresses DLL.
int eligibility_reset
Whether to reset the eligibility traces to 0 after a Q-exploration step.
double gamma
Learning discount factor in the TD-update for MDPs.
struct GNUNET_TIME_Relative step_time_max
Maximum interval time between steps in milliseconds.
#define RIL_DEFAULT_GRADIENT_STEP_SIZE
GAS_solver_address_feedback_preference s_feedback
Give feedback about the current assignment.
int a_old
Last chosen action.
int GNUNET_CONFIGURATION_get_value_float(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, float *number)
Get a configuration value that should be a floating point number.
static void ril_cut_from_vector(void **old, size_t element_size, unsigned int hole_start, unsigned int hole_length, unsigned int old_length)
Cuts a slice out of a vector of elements.
GAS_solver_address_property_changed s_address_update_property
Update the properties of an address in the solver.
double temperature_decay
Decay factor of the temperature value.
#define RIL_DEFAULT_WELFARE
#define GNUNET_NO
Definition: gnunet_common.h:81
struct RIL_Scope * network_entries
Array of networks with global assignment state.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
char * plugin
Plugin name.
int suggestion_issue
Flag whether a suggestion has to be issued.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct RIL_Peer_Agent * agents_tail
#define RIL_INTERVAL_EXPONENT
static void ril_try_unblock_agent(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
Unblocks an agent for which a connection request is there, that could not be satisfied.
void * libgnunet_plugin_ats_ril_init(void *cls)
Entry point for the plugin.
double epsilon
Ratio, with what probability an agent should explore in the e-greed policy.
static int ril_network_is_active(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType network)
Determine whether at least the minimum bandwidth is set for the network.
static int ril_network_is_not_full(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType network)
Determines whether more connections are allocated in a network scope, than they would theoretically f...
static void envi_set_active_suggestion(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, struct ATS_Address *new_address, unsigned long long new_bw_in, unsigned long long new_bw_out, int silent)
Changes the active assignment suggestion of the handler and invokes the bw_changed callback to notify...
static void GAS_ril_address_property_changed(void *solver, struct ATS_Address *address)
Update the properties of an address in the solver.
struct ATS_Address * address_naked
The address.
RIL_E_Modification
static void envi_action_address_switch(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, unsigned int address_index)
Switches to the address given by its index.
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:35
static double * envi_get_state(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
Allocates a state vector and fills it with the features present.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
unsigned long long step_count
Number of performed steps.
RIL_Welfare
double temperature
Softmax action-selection temperature.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static void ril_step_scheduler_task(void *cls)
Task for the scheduler, which performs one step and lets the solver know that no further step is sche...
GAS_Solver_Operation
Operation codes for solver information callback.
Handle to a client that is connected to a service.
Definition: service.c:249
struct GNUNET_TIME_Absolute step_time_last
Timestamp for the last time-step.
uint32_t assigned_bw_in
Inbound bandwidth assigned by solver.
static int agent_get_action_random(struct RIL_Peer_Agent *agent)
Chooses a random action from the set of possible ones.
struct GNUNET_PeerIdentity peer
Peer ID.
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:99
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1246
#define RIL_DEFAULT_TRACE_DECAY
unsigned long long simulate
Simulate steps, i.e.
A solution iteration has been started.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
void GNUNET_STRINGS_utf8_toupper(const char *input, char *output)
Convert the utf-8 input string to upper case.
Definition: strings.c:580
enum State state
current state of profiling
static void GAS_ril_get_preferred_address(void *solver, const struct GNUNET_PeerIdentity *peer)
Tell solver to notify ATS if the address to use changes for a specific peer using the bandwidth chang...
#define RIL_DEFAULT_ALGORITHM
#define GNUNET_memcpy(dst, src, n)
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
enum RIL_Select select
Action selection strategy;.
static void envi_do_action(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
Puts the action into effect by calling the according function.
static void ril_step_schedule_next(struct GAS_RIL_Handle *solver)
Schedules the next global step in an adaptive way.
unsigned long long bw_out_utilized
Bandwidth outbound actually utilized in the network.
static struct RIL_Peer_Agent * agent_init(void *s, const struct GNUNET_PeerIdentity *peer)
Initialize an agent without addresses and its knowledge base.
static int silent
–silent option
Definition: gnunet-qr.c:49
enum GNUNET_NetworkType type
ATS network type.
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:85
static enum GNUNET_NetworkType scope
Which network scope do we belong to?
enum RIL_Welfare social_welfare
Which measure of social welfare should be used.
static void agent_w_init(struct RIL_Peer_Agent *agent)
Initializes the matrix W of parameter vectors theta with small random numbers.
double beta
Learning discount variable in the TD-update for semi-MDPs.
static unsigned long long ril_network_get_utilized(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType type, int direction_in)
Calculates how much bandwidth is actually utilized in sum in a network scope, either in the inbound o...
static unsigned long long ril_get_max_bw(struct RIL_Scope *net)
Calculates the maximum bandwidth an agent can assign in a network scope.
static struct GNUNET_PEERINFO_NotifyContext * notify
Handle to the peerinfo notify service (NULL until we&#39;ve connected to it).
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:83
GAS_solver_address_delete s_del
Delete an address in the solver.
static int agent_address_get_index(struct RIL_Peer_Agent *agent, struct ATS_Address *address)
Get the index of the address in the agent&#39;s list.
#define RIL_DEFAULT_TEMPERATURE_DECAY
static int result
Global testing status.
enum RIL_Algorithm algorithm
The TD-algorithm to use.
#define RIL_DEFAULT_STEP_TIME_MAX
double ** W
Experience matrix W.
static int agent_select_egreedy(struct RIL_Peer_Agent *agent, double *state)
Selects the next action using the e-greedy strategy.
struct GAS_NormalizationInfo norm_utilization_out
Normalized utilization outbound for this address.
const char * GNUNET_ATS_print_preference_type(enum GNUNET_ATS_PreferenceKind type)
Convert a enum GNUNET_ATS_PreferenceType to a string.
static int address_count
Definition: gnunet-hello.c:50
struct GNUNET_TIME_Relative GNUNET_TIME_relative_subtract(struct GNUNET_TIME_Relative a1, struct GNUNET_TIME_Relative a2)
Subtract relative timestamp from the other.
Definition: time.c:601
struct RIL_Learning_Parameters parameters
Learning parameters.
GAS_solver_address_change_preference s_pref
Change relative preference for quality in solver.
#define RIL_DEFAULT_TEMPERATURE
static void envi_action_bw_double(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
Doubles the bandwidth for the active address.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
uint32_t bw_in_utilized
Bandwidth inbound actually utilized in the network.
double social_welfare
The social welfare achieved in the scope.
int GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
#define GNUNET_ATS_MaxBandwidth
Maximum bandwidth assigned to a network : 4095 MB/s.
#define RIL_DEFAULT_EXPLORE_RATIO
Address with additional information.
static struct RIL_Peer_Agent * ril_get_agent(struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
Returns the agent for a peer.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static void ril_calculate_discount(struct GAS_RIL_Handle *solver)
Determines how much the reward needs to be discounted depending on the amount of time, which has passed since the last time-step.
static unsigned int size
Size of the "table".
Definition: peer.c:67
void * solver_information
Solver-specific information for this address.
double ** E
Eligibility traces.
void * libgnunet_plugin_ats_ril_done(void *cls)
Exit point for the plugin.
uint32_t bw_out
Outbound bandwidth assigned by the agent.
static struct RIL_Address_Wrapped * agent_address_get_wrapped(struct RIL_Peer_Agent *agent, struct ATS_Address *address)
Gets the wrapped address from the agent&#39;s list.
static int ril_network_count_active_agents(struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
Count the number of active agents/connections in a network scope.
unsigned int networks_count
Networks count.
struct ATS_Address * address_inuse
Address in use.
double epsilon_decay
Decay factor of the explore ratio.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
void * cls
Closure to pass to all solver functions in this struct.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
Change the peer&#39;s latency value to the given amount.
#define RIL_DEFAULT_STEP_TIME_MIN
double epsilon_init
Initial exploration ratio value.
ats service address management
RIL_Select
#define GNUNET_TIME_UNIT_ZERO
Relative time zero.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
GAS_solver_get_preferred_address s_get
Tell solver to notify ATS if the address to use changes for a specific peer using the bandwidth chang...
unsigned long long in_quota[6]
Array of configured inbound quotas Order according to networks in network array.
A solution iteration has been finished.
#define RIL_DEFAULT_DISCOUNT_BETA
static unsigned long long ril_network_get_assigned(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType type, int direction_in)
Calculates how much bandwidth is assigned in sum in a network scope, either in the inbound or in the ...
unsigned int active_agent_count
Number of active agents in scope.
uint32_t bw_out_available
Total available outbound bandwidth.
GAS_solver_bulk_start s_bulk_start
Start a bulk operation.
struct RIL_Address_Wrapped * next
Next in DLL.
double global_discount_variable
Variable discount factor, dependent on time between steps.
The identity of the host (wraps the signing key of the peer).
unsigned int n
Number of columns of W / Number of actions.
int done
Shutdown.
static void GAS_ril_address_preference_feedback(void *solver, struct GNUNET_SERVICE_Client *application, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_TIME_Relative scope, enum GNUNET_ATS_PreferenceKind kind, double score)
Give feedback about the current assignment.
uint32_t assigned_bw_out
Outbound bandwidth assigned by solver.
struct GAS_NormalizationInfo norm_utilization_in
Normalized utilization inbound for this address.
static double ril_get_used_resource_ratio(struct GAS_RIL_Handle *solver)
Determines how much of the available bandwidth is assigned.
#define RIL_ACTION_INVALID
#define RIL_NOP_DECAY
struct RIL_Peer_Agent * prev
Previous agent in solver&#39;s linked list.
#define RIL_INC_DEC_STEP_SIZE
static void agent_update(struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
Updates the weights (i.e.
static void GAS_ril_bulk_start(void *solver)
Start a bulk operation.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration handle to be used by the solver.
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
static double envi_get_reward(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
Gets the reward for the last performed step, which is calculated in equal parts from the local (the p...
GAS_solver_information_callback info_cb
Callback for solver to call with status information, can be NULL.
Change the peer&#39;s bandwidth value (value per byte of bandwidth in the goal function) to the given amo...
static int agent_select_action(struct RIL_Peer_Agent *agent, double *state)
Select the next action of an agent either according to the e-greedy strategy or the softmax strategy...
static void agent_step(struct RIL_Peer_Agent *agent)
Performs one step of the Markov Decision Process.
static void ril_networks_update_state(struct GAS_RIL_Handle *solver)
Retrieves the state of the network scope, so that its attributes are up-to- date. ...
struct GAS_NormalizationInfo norm_delay
Normalized delay information for this address.
unsigned long long out_quota[6]
Array of configured outbound quotas Order according to networks in network array. ...
static double agent_get_utility(struct RIL_Peer_Agent *agent)
Returns the utility value of the connection an agent manages.
double * s_old
Last perceived state feature vector.
uint32_t bw_in_assigned
Bandwidth inbound assigned in network after last step.
unsigned long long bw_out_assigned
Bandwidth outbound assigned in network after last step.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
void * cls
Closure to pass to all callbacks in this struct.
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:80
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Absolute end)
Compute the time difference between the given start and end times.
Definition: time.c:353
struct RIL_Address_Wrapped * addresses_tail
Tail of addresses DLL.
static struct GNUNET_ATS_SolverFunctions * sf
Solver handle.
GNUNET_ATS_PreferenceKind
Enum defining all known preference categories.
int bulk_lock
Lock for bulk operations.
static void envi_action_bw_inc(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
Increases the bandwidth by 5 times the minimum bandwidth for the active address.
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:139
The ATS plugin will pass a pointer to a struct of this type as to the initialization function of the ...
struct RIL_Peer_Agent * next
Next agent in solver&#39;s linked list.
static double envi_get_penalty(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
int GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
static double ril_network_get_social_welfare(struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
Calculates the social welfare within a network scope according to what social welfare measure is set ...
unsigned long long rbf_divisor
State space divisor.
struct GNUNET_TIME_Relative step_time_min
Minimal interval time between steps in milliseconds.
GAS_solver_bulk_stop s_bulk_stop
Bulk operation done.
#define LOG(kind,...)
static void ril_step(struct GAS_RIL_Handle *solver)
Prototype of the ril_step() procedure.
static char * address
GNS address for this phone.
GAS_bandwidth_changed_cb bandwidth_changed_cb
ATS addresses callback to be notified about bandwidth assignment changes.
After the problem was finished, notifications about changes to addresses are done.
#define GNUNET_TIME_UNIT_MICROSECONDS
One microsecond, our basic time unit.
static void agent_modify_eligibility(struct RIL_Peer_Agent *agent, enum RIL_E_Modification mod, double *feature, int action)
Changes the eligibility trace vector e in various manners: RIL_E_ACCUMULATE - adds feature to each co...
struct ATS_Address * suggestion_address
The address which has to be issued.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_saturating_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Saturating multiply relative time by a given factor.
Definition: time.c:499
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
double norm
Normalized values from queue to a range of values [1.0...2.0].
static int agent_action_is_possible(struct RIL_Peer_Agent *agent, int action)
struct GAS_RIL_Handle * envi
Environment handle.
unsigned int network_count
Number of networks available, size of the out_quota and in_quota arrays.
RIL_Algorithm
Wrapper for addresses to store them in agent&#39;s linked list.
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
#define GNUNET_malloc(size)
Wrapper around malloc.
double temperature_init
Initial softmax action-selection temperature.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
double nop_bonus
NOP bonus.
double objective_old
The agent&#39;s last objective value.
double alpha
Gradient-descent step-size.
uint32_t bw_in_available
Total available inbound bandwidth.
unsigned long long step_count
Number of performed time-steps.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965
#define RIL_MIN_BW
enum RIL_E_Modification eligibility_trace_mode
Whether to accumulate or replace eligibility traces.
RIL_Action_Type
ATS reinforcement learning solver.