aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFabian Oehlmann <oehlmann@in.tum.de>2014-01-16 17:35:08 +0000
committerFabian Oehlmann <oehlmann@in.tum.de>2014-01-16 17:35:08 +0000
commit5650ff38f1263a52c29511673aee1c849ae1fd8e (patch)
tree8747fd8ca85ce699a23a230593cb116c01516c4c /src
parent2134d1a1eaa421d42e93ce0be1f718758c4a6e4b (diff)
downloadgnunet-5650ff38f1263a52c29511673aee1c849ae1fd8e.tar.gz
gnunet-5650ff38f1263a52c29511673aee1c849ae1fd8e.zip
- state-feature fix
- minor refactorings
Diffstat (limited to 'src')
-rwxr-xr-xsrc/ats/plugin_ats_ril.c366
1 files changed, 206 insertions, 160 deletions
diff --git a/src/ats/plugin_ats_ril.c b/src/ats/plugin_ats_ril.c
index da974dd72..be42c9dc7 100755
--- a/src/ats/plugin_ats_ril.c
+++ b/src/ats/plugin_ats_ril.c
@@ -28,26 +28,29 @@
28 28
29#define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__) 29#define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
30 30
31#define MIN_BW ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__) 31#define RIL_MIN_BW ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)
32#define RIL_MAX_BW GNUNET_ATS_MaxBandwidth
32 33
33#define RIL_ACTION_INVALID -1 34#define RIL_ACTION_INVALID -1
34#define RIL_INTERVAL_EXPONENT 10 35#define RIL_INTERVAL_EXPONENT 10
35#define RIL_UTILITY_MAX (double) GNUNET_ATS_MaxBandwidth 36#define RIL_UTILITY_MAX (double) RIL_MAX_BW
36 37
37#define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500) 38#define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
38#define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000) 39#define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
39#define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA 40#define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q
40#define RIL_DEFAULT_SELECT RIL_SELECT_EGREEDY 41#define RIL_DEFAULT_SELECT RIL_SELECT_EGREEDY
42#define RIL_DEFAULT_WELFARE RIL_WELFARE_EGALITARIAN
41#define RIL_DEFAULT_DISCOUNT_BETA 1.0 43#define RIL_DEFAULT_DISCOUNT_BETA 1.0
42#define RIL_DEFAULT_DISCOUNT_GAMMA 0.5 44#define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
43#define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.1 45#define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.1
44#define RIL_DEFAULT_TRACE_DECAY 0.5 46#define RIL_DEFAULT_TRACE_DECAY 0.5
45#define RIL_DEFAULT_EXPLORE_RATIO 0.1 47#define RIL_DEFAULT_EXPLORE_RATIO 0.1
46#define RIL_DEFAULT_RBF_DIVISOR 10 48#define RIL_DEFAULT_RBF_DIVISOR 10
47#define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5 49#define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5
48#define RIL_DEFAULT_TEMPERATURE 1.0 50#define RIL_DEFAULT_TEMPERATURE 1.0
49 51
50#define RIL_INC_DEC_STEP_SIZE 1 52#define RIL_INC_DEC_STEP_SIZE 1
53#define RIL_NOP_BONUS 0.5
51 54
52/** 55/**
53 * ATS reinforcement learning solver 56 * ATS reinforcement learning solver
@@ -70,9 +73,9 @@ enum RIL_Action_Type
70 RIL_ACTION_BW_IN_DEC = 2, 73 RIL_ACTION_BW_IN_DEC = 2,
71 RIL_ACTION_BW_OUT_DBL = -4, 74 RIL_ACTION_BW_OUT_DBL = -4,
72 RIL_ACTION_BW_OUT_HLV = -5, 75 RIL_ACTION_BW_OUT_HLV = -5,
73 RIL_ACTION_BW_OUT_INC = 3, 76 RIL_ACTION_BW_OUT_INC = -6,
74 RIL_ACTION_BW_OUT_DEC = 4, 77 RIL_ACTION_BW_OUT_DEC = -7,
75 RIL_ACTION_TYPE_NUM = 5 78 RIL_ACTION_TYPE_NUM = 3
76}; 79};
77 80
78enum RIL_Algorithm 81enum RIL_Algorithm
@@ -83,8 +86,14 @@ enum RIL_Algorithm
83 86
84enum RIL_Select 87enum RIL_Select
85{ 88{
86 RIL_SELECT_EGREEDY, 89 RIL_SELECT_SOFTMAX = 0,
87 RIL_SELECT_SOFTMAX 90 RIL_SELECT_EGREEDY = 1
91};
92
93enum RIL_Welfare
94{
95 RIL_WELFARE_NASH,
96 RIL_WELFARE_EGALITARIAN
88}; 97};
89 98
90enum RIL_E_Modification 99enum RIL_E_Modification
@@ -136,9 +145,14 @@ struct RIL_Learning_Parameters
136 double temperature; 145 double temperature;
137 146
138 /** 147 /**
148 * Which measure of social welfare should be used
149 */
150 enum RIL_Welfare social_welfare;
151
152 /**
139 * State space divisor 153 * State space divisor
140 */ 154 */
141 unsigned long long int divisor; 155 unsigned long long rbf_divisor;
142 156
143 /** 157 /**
144 * Action selection strategy; 158 * Action selection strategy;
@@ -283,6 +297,16 @@ struct RIL_Peer_Agent
283 * The address which has to be issued 297 * The address which has to be issued
284 */ 298 */
285 struct ATS_Address * suggestion_address; 299 struct ATS_Address * suggestion_address;
300
301 /**
302 * The agent's last objective value
303 */
304 double objective_old;
305
306 /**
307 * NOP bonus
308 */
309 double nop_bonus;
286}; 310};
287 311
288struct RIL_Scope 312struct RIL_Scope
@@ -311,6 +335,16 @@ struct RIL_Scope
311 * * Bandwidth outbound assigned in network after last step 335 * * Bandwidth outbound assigned in network after last step
312 */ 336 */
313 unsigned long long bw_out_assigned; 337 unsigned long long bw_out_assigned;
338
339 /**
340 * Number of active agents in scope
341 */
342 unsigned int agent_count;
343
344 /**
345 * The social welfare achieved in the scope
346 */
347 double social_welfare;
314}; 348};
315 349
316/** 350/**
@@ -400,16 +434,6 @@ struct GAS_RIL_Handle
400 * --------------------------- 434 * ---------------------------
401 */ 435 */
402 436
403static int
404ril_count_agents(struct GAS_RIL_Handle * solver);
405
406static double
407agent_get_utility (struct RIL_Peer_Agent *agent)
408{
409 //TODO get utilitiy
410 return (double) agent->bw_in;
411}
412
413/** 437/**
414 * Estimate the current action-value for state s and action a 438 * Estimate the current action-value for state s and action a
415 * 439 *
@@ -553,7 +577,7 @@ agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_nex
553// delta, 577// delta,
554// i, 578// i,
555// agent->e[i]); 579// agent->e[i]);
556 theta[i] += agent->envi->parameters.alpha * delta * agent->s_old[i];// * agent->E[a_prime][i]; 580 theta[i] += agent->envi->parameters.alpha * delta * agent->s_old[i] * agent->E[a_prime][i];
557 } 581 }
558} 582}
559 583
@@ -745,22 +769,25 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
745 int m; 769 int m;
746 int i; 770 int i;
747 int k; 771 int k;
772 unsigned long long max_bw;
748 773
749 state = GNUNET_malloc (sizeof(double) * agent->m); 774 state = GNUNET_malloc (sizeof(double) * agent->m);
750 775
776 max_bw = RIL_MAX_BW;
777
751 y[0] = (double) agent->bw_out; 778 y[0] = (double) agent->bw_out;
752 y[1] = (double) agent->bw_in; 779 y[1] = (double) agent->bw_in;
753 780
754 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.divisor+1) * (solver->parameters.divisor+1); 781 m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
755 for (i = 0; i <= solver->parameters.divisor; i++) 782 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
756 { 783 {
757 for (k = 0; k <= solver->parameters.divisor; k++) 784 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
758 { 785 {
759 x[0] = i * GNUNET_ATS_MaxBandwidth / solver->parameters.divisor; 786 x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
760 x[1] = k * GNUNET_ATS_MaxBandwidth / solver->parameters.divisor; 787 x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
761 d[0] = x[0]-y[0]; 788 d[0] = x[0]-y[0];
762 d[1] = x[1]-y[1]; 789 d[1] = x[1]-y[1];
763 sigma = ((double) GNUNET_ATS_MaxBandwidth / 2) * M_SQRT2; 790 sigma = (((double) max_bw / 2) * M_SQRT2) / (double) solver->parameters.rbf_divisor;
764 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma))); 791 f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
765 state[m++] = f; 792 state[m++] = f;
766 } 793 }
@@ -807,7 +834,7 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
807// } 834// }
808// return max; 835// return max;
809//} 836//}
810 837//
811///* 838///*
812// * Get the index of the quality-property in question 839// * Get the index of the quality-property in question
813// * 840// *
@@ -824,7 +851,7 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
824// return c; 851// return c;
825// return GNUNET_SYSERR; 852// return GNUNET_SYSERR;
826//} 853//}
827 854//
828//static int 855//static int
829//ril_get_atsi (struct ATS_Address *address, uint32_t type) 856//ril_get_atsi (struct ATS_Address *address, uint32_t type)
830//{ 857//{
@@ -841,36 +868,8 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
841// } 868// }
842// return 0; 869// return 0;
843//} 870//}
844
845//static double
846//envi_reward_global (struct GAS_RIL_Handle *solver)
847//{
848// int i;
849// struct RIL_Scope net;
850// unsigned int sum_in_available = 0;
851// unsigned int sum_out_available = 0;
852// unsigned int sum_in_assigned = 0;
853// unsigned int sum_out_assigned = 0;
854// double ratio_in;
855// double ratio_out;
856// 871//
857// for (i = 0; i < solver->networks_count; i++)
858// {
859// net = solver->network_entries[i];
860// sum_in_available += net.bw_in_available;
861// sum_in_assigned += net.bw_in_assigned;
862// sum_out_available += net.bw_out_available;
863// sum_out_assigned += net.bw_out_assigned;
864// }
865//
866// ratio_in = ((double) sum_in_assigned) / ((double) sum_in_available);
867// ratio_out = ((double) sum_out_assigned) / ((double) sum_out_available);
868// 872//
869// // global reward in [1,2]
870// return ratio_in +1;
871// return ((ratio_in + ratio_out) / 2) + 1;
872//}
873
874//static double 873//static double
875//envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent) 874//envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
876//{ 875//{
@@ -905,21 +904,44 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
905//} 904//}
906 905
907static double 906static double
908envi_get_collective_utility (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope) 907agent_get_utility (struct RIL_Peer_Agent *agent)
908{
909 return (double) (agent->bw_in/RIL_MIN_BW);
910// return sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
911}
912
913static double
914ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
909{ 915{
910 //TODO! add nash product
911 struct RIL_Peer_Agent *cur; 916 struct RIL_Peer_Agent *cur;
912 double result = RIL_UTILITY_MAX; 917 double result;
913 918
914 for (cur = solver->agents_head; NULL != cur; cur = cur->next) 919 switch (solver->parameters.social_welfare)
915 { 920 {
916 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope)) 921 case RIL_WELFARE_EGALITARIAN:
922 result = RIL_UTILITY_MAX;
923 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
917 { 924 {
918 result = GNUNET_MIN(result, agent_get_utility(cur)); 925 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
926 {
927 result = GNUNET_MIN(result, agent_get_utility(cur));
928 }
919 } 929 }
920 } 930 return result;
921 931
922 return result; 932 case RIL_WELFARE_NASH:
933 result = 0;
934 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
935 {
936 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
937 {
938 result *= agent_get_utility(cur);
939 }
940 }
941 return pow(result, 1.0 / (double) scope->agent_count);
942 }
943 GNUNET_assert(GNUNET_NO);
944 return 1;
923} 945}
924 946
925/** 947/**
@@ -935,21 +957,42 @@ static double
935envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent) 957envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
936{ 958{
937 struct RIL_Scope *net; 959 struct RIL_Scope *net;
960 unsigned long long overutilization;
961 unsigned long long over_in = 0;
962 unsigned long long over_out = 0;
963 double objective;
964 double delta;
965 double steady;
938 966
939 unsigned long long objective;
940
941 LOG(GNUNET_ERROR_TYPE_INFO, "address: %x\n", agent->address_inuse);
942 net = agent->address_inuse->solver_information; 967 net = agent->address_inuse->solver_information;
968
943 if (net->bw_in_assigned > net->bw_in_available) 969 if (net->bw_in_assigned > net->bw_in_available)
970 over_in = net->bw_in_assigned - net->bw_in_available;
971 if (net->bw_out_assigned > net->bw_out_available)
972 over_out = net->bw_out_assigned - net->bw_out_available;
973 overutilization = GNUNET_MAX(over_in, over_out) / RIL_MIN_BW;
974
975 objective = agent_get_utility (agent) + net->social_welfare;
976 delta = objective - agent->objective_old;
977 agent->objective_old = objective;
978
979 if (delta != 0)
944 { 980 {
945 objective = net->bw_in_available - net->bw_in_assigned; 981 agent->nop_bonus = abs(delta) * 0;
982 }
983
984 LOG(GNUNET_ERROR_TYPE_DEBUG, "utility: %f, welfare: %f, objective, overutilization: %d\n", agent_get_utility (agent), net->social_welfare, objective, overutilization);
985
986 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
987
988 if (0 != overutilization)
989 {
990 return -1.0 * (double) overutilization;
946 } 991 }
947 else 992 else
948 { 993 {
949 objective = envi_get_collective_utility(solver, agent->address_inuse->solver_information); 994 return delta + steady;
950 } 995 }
951
952 return objective;
953} 996}
954 997
955/** 998/**
@@ -969,16 +1012,16 @@ envi_action_bw_double (struct GAS_RIL_Handle *solver,
969 if (direction_in) 1012 if (direction_in)
970 { 1013 {
971 new_bw = agent->bw_in * 2; 1014 new_bw = agent->bw_in * 2;
972 if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth) 1015 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
973 new_bw = GNUNET_ATS_MaxBandwidth; 1016 new_bw = RIL_MAX_BW;
974 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, 1017 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
975 agent->bw_out, GNUNET_NO); 1018 agent->bw_out, GNUNET_NO);
976 } 1019 }
977 else 1020 else
978 { 1021 {
979 new_bw = agent->bw_out * 2; 1022 new_bw = agent->bw_out * 2;
980 if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth) 1023 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
981 new_bw = GNUNET_ATS_MaxBandwidth; 1024 new_bw = RIL_MAX_BW;
982 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, 1025 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
983 new_bw, GNUNET_NO); 1026 new_bw, GNUNET_NO);
984 } 1027 }
@@ -1003,16 +1046,16 @@ envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1003 if (direction_in) 1046 if (direction_in)
1004 { 1047 {
1005 new_bw = agent->bw_in / 2; 1048 new_bw = agent->bw_in / 2;
1006 if (new_bw < MIN_BW || new_bw > agent->bw_in) 1049 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1007 new_bw = MIN_BW; 1050 new_bw = RIL_MIN_BW;
1008 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out, 1051 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1009 GNUNET_NO); 1052 GNUNET_NO);
1010 } 1053 }
1011 else 1054 else
1012 { 1055 {
1013 new_bw = agent->bw_out / 2; 1056 new_bw = agent->bw_out / 2;
1014 if (new_bw < MIN_BW || new_bw > agent->bw_out) 1057 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1015 new_bw = MIN_BW; 1058 new_bw = RIL_MIN_BW;
1016 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw, 1059 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1017 GNUNET_NO); 1060 GNUNET_NO);
1018 } 1061 }
@@ -1033,17 +1076,17 @@ envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
1033 1076
1034 if (direction_in) 1077 if (direction_in)
1035 { 1078 {
1036 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * MIN_BW); 1079 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1037 if (new_bw < agent->bw_in || new_bw > GNUNET_ATS_MaxBandwidth) 1080 if (new_bw < agent->bw_in || new_bw > RIL_MAX_BW)
1038 new_bw = GNUNET_ATS_MaxBandwidth; 1081 new_bw = RIL_MAX_BW;
1039 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, 1082 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1040 agent->bw_out, GNUNET_NO); 1083 agent->bw_out, GNUNET_NO);
1041 } 1084 }
1042 else 1085 else
1043 { 1086 {
1044 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * MIN_BW); 1087 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1045 if (new_bw < agent->bw_out || new_bw > GNUNET_ATS_MaxBandwidth) 1088 if (new_bw < agent->bw_out || new_bw > RIL_MAX_BW)
1046 new_bw = GNUNET_ATS_MaxBandwidth; 1089 new_bw = RIL_MAX_BW;
1047 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, 1090 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
1048 new_bw, GNUNET_NO); 1091 new_bw, GNUNET_NO);
1049 } 1092 }
@@ -1065,17 +1108,17 @@ envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
1065 1108
1066 if (direction_in) 1109 if (direction_in)
1067 { 1110 {
1068 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * MIN_BW); 1111 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1069 if (new_bw < MIN_BW || new_bw > agent->bw_in) 1112 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_in)
1070 new_bw = MIN_BW; 1113 new_bw = RIL_MIN_BW;
1071 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out, 1114 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
1072 GNUNET_NO); 1115 GNUNET_NO);
1073 } 1116 }
1074 else 1117 else
1075 { 1118 {
1076 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * MIN_BW); 1119 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1077 if (new_bw < MIN_BW || new_bw > agent->bw_out) 1120 if (new_bw < RIL_MIN_BW || new_bw > agent->bw_out)
1078 new_bw = MIN_BW; 1121 new_bw = RIL_MIN_BW;
1079 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw, 1122 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1080 GNUNET_NO); 1123 GNUNET_NO);
1081 } 1124 }
@@ -1317,12 +1360,12 @@ agent_step (struct RIL_Peer_Agent *agent)
1317 1360
1318 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, s_next, a_next); 1361 agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, s_next, a_next);
1319 1362
1320// GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n", 1363 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1321// agent->step_count, 1364 agent->step_count,
1322// reward, 1365 reward,
1323// agent->bw_in/1024, 1366 agent->bw_in/1024,
1324// agent->bw_out/1024, 1367 agent->bw_out/1024,
1325// a_next); 1368 a_next);
1326 1369
1327 envi_do_action (agent->envi, agent, a_next); 1370 envi_do_action (agent->envi, agent, a_next);
1328 1371
@@ -1426,7 +1469,7 @@ ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_
1426 } 1469 }
1427 1470
1428 net = ril_get_network (solver, network); 1471 net = ril_get_network (solver, network);
1429 return (net->bw_in_available > MIN_BW * address_count) && (net->bw_out_available > MIN_BW * address_count); 1472 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
1430} 1473}
1431 1474
1432static void 1475static void
@@ -1441,7 +1484,7 @@ ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *age
1441 if (ril_network_is_not_full(solver, net->type)) 1484 if (ril_network_is_not_full(solver, net->type))
1442 { 1485 {
1443 if (NULL == agent->address_inuse) 1486 if (NULL == agent->address_inuse)
1444 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, MIN_BW, MIN_BW, silent); 1487 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, RIL_MIN_BW, RIL_MIN_BW, silent);
1445 return; 1488 return;
1446 } 1489 }
1447 } 1490 }
@@ -1455,7 +1498,7 @@ ril_calculate_discount (struct GAS_RIL_Handle *solver)
1455 struct GNUNET_TIME_Relative time_delta; 1498 struct GNUNET_TIME_Relative time_delta;
1456 double tau; 1499 double tau;
1457 1500
1458 // MDP case - remove when debugged 1501 // MDP case - TODO remove when debugged
1459 if (solver->simulate) 1502 if (solver->simulate)
1460 { 1503 {
1461 solver->global_discount_variable = solver->parameters.gamma; 1504 solver->global_discount_variable = solver->parameters.gamma;
@@ -1478,8 +1521,24 @@ ril_calculate_discount (struct GAS_RIL_Handle *solver)
1478 / (double) solver->parameters.beta; 1521 / (double) solver->parameters.beta;
1479} 1522}
1480 1523
1524static int
1525ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
1526{
1527 int c = 0;
1528 struct RIL_Peer_Agent *cur_agent;
1529
1530 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1531 {
1532 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
1533 {
1534 c++;
1535 }
1536 }
1537 return c;
1538}
1539
1481static void 1540static void
1482ril_calculate_assigned_bwnet (struct GAS_RIL_Handle *solver) 1541ril_networks_update_state (struct GAS_RIL_Handle *solver)
1483{ 1542{
1484 int c; 1543 int c;
1485 struct RIL_Scope *net; 1544 struct RIL_Scope *net;
@@ -1489,6 +1548,8 @@ ril_calculate_assigned_bwnet (struct GAS_RIL_Handle *solver)
1489 net = &solver->network_entries[c]; 1548 net = &solver->network_entries[c];
1490 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES); 1549 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
1491 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO); 1550 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
1551 net->agent_count = ril_network_count_active_agents(solver, net);
1552 net->social_welfare = ril_network_get_social_welfare(solver, net);
1492 } 1553 }
1493} 1554}
1494 1555
@@ -1538,6 +1599,7 @@ ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1538 1599
1539/** 1600/**
1540 * Triggers one step per agent 1601 * Triggers one step per agent
1602 *
1541 * @param solver 1603 * @param solver
1542 */ 1604 */
1543static void 1605static void
@@ -1561,10 +1623,7 @@ ril_step (struct GAS_RIL_Handle *solver)
1561 } 1623 }
1562 1624
1563 ril_calculate_discount (solver); 1625 ril_calculate_discount (solver);
1564 ril_calculate_assigned_bwnet (solver); 1626 ril_networks_update_state (solver);
1565
1566 //calculate network state vector
1567// envi_state_networks(solver);
1568 1627
1569 //trigger one step per active, unblocked agent 1628 //trigger one step per active, unblocked agent
1570 for (cur = solver->agents_head; NULL != cur; cur = cur->next) 1629 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
@@ -1582,7 +1641,7 @@ ril_step (struct GAS_RIL_Handle *solver)
1582 } 1641 }
1583 } 1642 }
1584 1643
1585 ril_calculate_assigned_bwnet (solver); 1644 ril_networks_update_state (solver);
1586 1645
1587 solver->step_count += 1; 1646 solver->step_count += 1;
1588 ril_step_schedule_next (solver); 1647 ril_step_schedule_next (solver);
@@ -1600,44 +1659,22 @@ ril_step (struct GAS_RIL_Handle *solver)
1600 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS); 1659 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1601} 1660}
1602 1661
1603static int 1662/**
1604ril_count_agents (struct GAS_RIL_Handle *solver) 1663 * Initializes the matrix W of parameter vectors theta with small random numbers.
1605{ 1664 *
1606 int c = 0; 1665 * @param agent The respective agent
1607 struct RIL_Peer_Agent *cur_agent; 1666 */
1608
1609 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
1610 {
1611 c++;
1612 }
1613 return c;
1614}
1615
1616static void 1667static void
1617agent_w_start (struct RIL_Peer_Agent *agent) 1668agent_w_init (struct RIL_Peer_Agent *agent)
1618{ 1669{
1619 int count;
1620 struct RIL_Peer_Agent *other;
1621 int i; 1670 int i;
1622 int k; 1671 int k;
1623 1672
1624 count = ril_count_agents(agent->envi);
1625
1626 for (i = 0; i < agent->n; i++) 1673 for (i = 0; i < agent->n; i++)
1627 { 1674 {
1628 for (k = 0; k < agent->m; k++) 1675 for (k = 0; k < agent->m; k++)
1629 { 1676 {
1630 if (0 == count) { 1677 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));
1631 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));
1632 }
1633 else {
1634 for (other = agent->envi->agents_head; NULL != other; other = other->next)
1635 {
1636 agent->W[i][k] += (other->W[i][k] / (double) count);
1637 }
1638 }
1639
1640 GNUNET_assert(!isinf(agent->W[i][k]));
1641 } 1678 }
1642 } 1679 }
1643} 1680}
@@ -1660,8 +1697,8 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1660 agent->peer = *peer; 1697 agent->peer = *peer;
1661 agent->step_count = 0; 1698 agent->step_count = 0;
1662 agent->is_active = GNUNET_NO; 1699 agent->is_active = GNUNET_NO;
1663 agent->bw_in = MIN_BW; 1700 agent->bw_in = RIL_MIN_BW;
1664 agent->bw_out = MIN_BW; 1701 agent->bw_out = RIL_MIN_BW;
1665 agent->suggestion_issue = GNUNET_NO; 1702 agent->suggestion_issue = GNUNET_NO;
1666 agent->n = RIL_ACTION_TYPE_NUM; 1703 agent->n = RIL_ACTION_TYPE_NUM;
1667 agent->m = 0; 1704 agent->m = 0;
@@ -1672,10 +1709,11 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1672 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m); 1709 agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1673 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m); 1710 agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
1674 } 1711 }
1675 agent_w_start(agent); 1712 agent_w_init(agent);
1676 agent->a_old = RIL_ACTION_INVALID; 1713 agent->a_old = RIL_ACTION_INVALID;
1677 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m); 1714 agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
1678 agent->address_inuse = NULL; 1715 agent->address_inuse = NULL;
1716 agent->objective_old = 0;
1679 1717
1680 return agent; 1718 return agent;
1681} 1719}
@@ -1746,7 +1784,7 @@ ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Ty
1746 struct RIL_Scope *net; 1784 struct RIL_Scope *net;
1747 1785
1748 net = ril_get_network (solver, network); 1786 net = ril_get_network (solver, network);
1749 return net->bw_out_available >= MIN_BW; 1787 return net->bw_out_available >= RIL_MIN_BW;
1750} 1788}
1751 1789
1752/** 1790/**
@@ -1849,9 +1887,9 @@ libgnunet_plugin_ats_ril_init (void *cls)
1849 GNUNET_assert(NULL != env->get_preferences); 1887 GNUNET_assert(NULL != env->get_preferences);
1850 GNUNET_assert(NULL != env->get_property); 1888 GNUNET_assert(NULL != env->get_property);
1851 1889
1852 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.divisor)) 1890 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
1853 { 1891 {
1854 solver->parameters.divisor = RIL_DEFAULT_RBF_DIVISOR; 1892 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
1855 } 1893 }
1856 if (GNUNET_OK 1894 if (GNUNET_OK
1857 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN", 1895 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
@@ -1994,6 +2032,15 @@ libgnunet_plugin_ats_ril_init (void *cls)
1994 { 2032 {
1995 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE; 2033 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
1996 } 2034 }
2035 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
2036 {
2037 solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
2038 GNUNET_free (string);
2039 }
2040 else
2041 {
2042 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2043 }
1997 2044
1998 env->sf.s_add = &GAS_ril_address_add; 2045 env->sf.s_add = &GAS_ril_address_add;
1999 env->sf.s_address_update_property = &GAS_ril_address_property_changed; 2046 env->sf.s_address_update_property = &GAS_ril_address_property_changed;
@@ -2020,18 +2067,22 @@ libgnunet_plugin_ats_ril_init (void *cls)
2020 cur->type = env->networks[c]; 2067 cur->type = env->networks[c];
2021 cur->bw_in_available = env->in_quota[c]; 2068 cur->bw_in_available = env->in_quota[c];
2022 cur->bw_out_available = env->out_quota[c]; 2069 cur->bw_out_available = env->out_quota[c];
2023 LOG(GNUNET_ERROR_TYPE_INFO, "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); 2070 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);
2024 } 2071 }
2025 2072
2026 LOG(GNUNET_ERROR_TYPE_INFO, "init() Parameters:\n"); 2073 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2027 LOG(GNUNET_ERROR_TYPE_INFO, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n", 2074 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2028 solver->parameters.algorithm ? "Q" : "SARSA", 2075 solver->parameters.algorithm ? "Q" : "SARSA",
2029 solver->parameters.alpha, 2076 solver->parameters.alpha,
2030 solver->parameters.beta, 2077 solver->parameters.beta,
2031 solver->parameters.lambda); 2078 solver->parameters.lambda);
2032 LOG(GNUNET_ERROR_TYPE_INFO, "init() explore = %f, global_share = %f\n", 2079 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s, global_share = %f\n",
2033 solver->parameters.explore_ratio, 2080 solver->parameters.explore_ratio,
2081 solver->parameters.temperature,
2082 solver->parameters.select ? "EGREEDY" : "SOFTMAX",
2034 solver->parameters.reward_global_share); 2083 solver->parameters.reward_global_share);
2084 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2085 solver->parameters.rbf_divisor);
2035 2086
2036 return solver; 2087 return solver;
2037} 2088}
@@ -2115,7 +2166,7 @@ GAS_ril_address_add (void *solver, struct ATS_Address *address, uint32_t network
2115 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped); 2166 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
2116 2167
2117 //increase size of W 2168 //increase size of W
2118 m_new = agent->m + ((s->parameters.divisor+1) * (s->parameters.divisor+1)); 2169 m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2119 m_old = agent->m; 2170 m_old = agent->m;
2120 n_new = agent->n + 1; 2171 n_new = agent->n + 1;
2121 n_old = agent->n; 2172 n_old = agent->n;
@@ -2206,26 +2257,22 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
2206 GNUNET_free(address_wrapped); 2257 GNUNET_free(address_wrapped);
2207 2258
2208 //decrease W 2259 //decrease W
2209 m_new = agent->m - ((s->parameters.divisor+1) * (s->parameters.divisor+1)); 2260 m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
2210 n_new = agent->n - 1; 2261 n_new = agent->n - 1;
2211 2262
2212 for (i = 0; i < agent->n; i++) 2263 for (i = 0; i < agent->n; i++)
2213 { 2264 {
2214 LOG(GNUNET_ERROR_TYPE_DEBUG, "first\n");
2215 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double), 2265 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2216 address_index * ((s->parameters.divisor+1) * (s->parameters.divisor+1)), 2266 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2217 ((s->parameters.divisor+1) * (s->parameters.divisor+1)), agent->m); 2267 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2218 LOG(GNUNET_ERROR_TYPE_DEBUG, "sec\n");
2219 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double), 2268 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2220 address_index * ((s->parameters.divisor+1) * (s->parameters.divisor+1)), 2269 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2221 ((s->parameters.divisor+1) * (s->parameters.divisor+1)), agent->m); 2270 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2222 } 2271 }
2223 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]); 2272 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2224 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]); 2273 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2225 LOG(GNUNET_ERROR_TYPE_DEBUG, "third\n");
2226 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index, 2274 ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2227 1, agent->n); 2275 1, agent->n);
2228 LOG(GNUNET_ERROR_TYPE_DEBUG, "fourth\n");
2229 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index, 2276 ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
2230 1, agent->n); 2277 1, agent->n);
2231 //correct last action 2278 //correct last action
@@ -2238,10 +2285,9 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
2238 agent->a_old = RIL_ACTION_INVALID; 2285 agent->a_old = RIL_ACTION_INVALID;
2239 } 2286 }
2240 //decrease old state vector 2287 //decrease old state vector
2241 LOG(GNUNET_ERROR_TYPE_DEBUG, "fifth\n");
2242 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double), 2288 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2243 address_index * ((s->parameters.divisor+1) * (s->parameters.divisor+1)), 2289 address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
2244 ((s->parameters.divisor+1) * (s->parameters.divisor+1)), agent->m); 2290 ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
2245 agent->m = m_new; 2291 agent->m = m_new;
2246 agent->n = n_new; 2292 agent->n = n_new;
2247 2293
@@ -2249,7 +2295,7 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
2249 { 2295 {
2250 if (NULL != agent->addresses_head) //if peer has an address left, use it 2296 if (NULL != agent->addresses_head) //if peer has an address left, use it
2251 { 2297 {
2252 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, MIN_BW, MIN_BW, 2298 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, RIL_MIN_BW, RIL_MIN_BW,
2253 GNUNET_NO); 2299 GNUNET_NO);
2254 } 2300 }
2255 else 2301 else