aboutsummaryrefslogtreecommitdiff
path: root/src/ats
diff options
context:
space:
mode:
authorFabian Oehlmann <oehlmann@in.tum.de>2013-11-21 19:26:24 +0000
committerFabian Oehlmann <oehlmann@in.tum.de>2013-11-21 19:26:24 +0000
commit0d9ce999faff935fe81b5b4696cc2f831c80d821 (patch)
treeacef1dc209ddcde75b643f3455d6550c6b22c298 /src/ats
parent6bd6580bfd79a61d86977c8a39c993a087bfcf61 (diff)
downloadgnunet-0d9ce999faff935fe81b5b4696cc2f831c80d821.tar.gz
gnunet-0d9ce999faff935fe81b5b4696cc2f831c80d821.zip
- bugfixes
- changed reward calculation
Diffstat (limited to 'src/ats')
-rwxr-xr-xsrc/ats/libgnunet_plugin_ats_ril.c116
1 files changed, 77 insertions, 39 deletions
diff --git a/src/ats/libgnunet_plugin_ats_ril.c b/src/ats/libgnunet_plugin_ats_ril.c
index 8a7bd5771..b2e90e77c 100755
--- a/src/ats/libgnunet_plugin_ats_ril.c
+++ b/src/ats/libgnunet_plugin_ats_ril.c
@@ -36,8 +36,8 @@
36#define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500) 36#define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
37#define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000) 37#define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000)
38#define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA 38#define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
39#define RIL_DEFAULT_DISCOUNT_BETA 0.7 39#define RIL_DEFAULT_DISCOUNT_BETA 1
40#define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.3 40#define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.1
41#define RIL_DEFAULT_TRACE_DECAY 0.5 41#define RIL_DEFAULT_TRACE_DECAY 0.5
42#define RIL_DEFAULT_EXPLORE_RATIO 0.1 42#define RIL_DEFAULT_EXPLORE_RATIO 0.1
43#define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5 43#define RIL_DEFAULT_GLOBAL_REWARD_SHARE 0.5
@@ -57,15 +57,15 @@
57enum RIL_Action_Type 57enum RIL_Action_Type
58{ 58{
59 RIL_ACTION_NOTHING = 0, 59 RIL_ACTION_NOTHING = 0,
60 RIL_ACTION_BW_IN_DBL = 1, 60 RIL_ACTION_BW_IN_DBL = -1, //TODO! put actions back
61 RIL_ACTION_BW_IN_HLV = 2, 61 RIL_ACTION_BW_IN_HLV = -2,
62 RIL_ACTION_BW_IN_INC = 3, 62 RIL_ACTION_BW_IN_INC = 1,
63 RIL_ACTION_BW_IN_DEC = 4, 63 RIL_ACTION_BW_IN_DEC = 2,
64 RIL_ACTION_BW_OUT_DBL = 5, 64 RIL_ACTION_BW_OUT_DBL = -3,
65 RIL_ACTION_BW_OUT_HLV = 6, 65 RIL_ACTION_BW_OUT_HLV = -4,
66 RIL_ACTION_BW_OUT_INC = 7, 66 RIL_ACTION_BW_OUT_INC = 3,
67 RIL_ACTION_BW_OUT_DEC = 8, 67 RIL_ACTION_BW_OUT_DEC = 4,
68 RIL_ACTION_TYPE_NUM = 9 68 RIL_ACTION_TYPE_NUM = 5
69}; 69};
70 70
71enum RIL_Algorithm 71enum RIL_Algorithm
@@ -352,6 +352,9 @@ struct GAS_RIL_Handle
352 * --------------------------- 352 * ---------------------------
353 */ 353 */
354 354
355static int
356ril_count_agents(struct GAS_RIL_Handle * solver);
357
355/** 358/**
356 * Estimate the current action-value for state s and action a 359 * Estimate the current action-value for state s and action a
357 * 360 *
@@ -371,6 +374,13 @@ agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action)
371 result += state[i] * agent->W[action][i]; 374 result += state[i] * agent->W[action][i];
372 } 375 }
373 376
377 GNUNET_assert(!isnan(result));
378
379 if (isinf(result))
380 {
381 return isinf(result) * (DBL_MAX / 2); //is still big enough
382 }
383
374 return result; 384 return result;
375} 385}
376 386
@@ -507,6 +517,7 @@ agent_update_weights (struct RIL_Peer_Agent *agent, double reward, double *s_nex
507 delta = agent->envi->global_discount_integrated * reward; //reward 517 delta = agent->envi->global_discount_integrated * reward; //reward
508 delta += agent->envi->global_discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value 518 delta += agent->envi->global_discount_variable * agent_estimate_q (agent, s_next, a_prime); //discounted future value
509 delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step 519 delta -= agent_estimate_q (agent, agent->s_old, agent->a_old); //one step
520
510 for (i = 0; i < agent->m; i++) 521 for (i = 0; i < agent->m; i++)
511 { 522 {
512 theta[i] += agent->envi->parameters.alpha * delta * agent->e[i]; 523 theta[i] += agent->envi->parameters.alpha * delta * agent->e[i];
@@ -755,14 +766,14 @@ envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
755 * @param agent the agent handle 766 * @param agent the agent handle
756 * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO 767 * @param direction_in whether the inbound bandwidth should be considered. Returns the maximum outbound bandwidth if GNUNET_NO
757 */ 768 */
758static long long unsigned 769static unsigned long long
759ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in) 770ril_get_max_bw (struct RIL_Peer_Agent *agent, int direction_in)
760{ 771{
761 /* 772 /*
762 * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses' 773 * get the maximum bandwidth possible for a peer, e.g. among all addresses which addresses'
763 * network could provide the maximum bandwidth if all that bandwidth was used on that one peer. 774 * network could provide the maximum bandwidth if all that bandwidth was used on that one peer.
764 */ 775 */
765 int max = 0; 776 unsigned long long max = 0;
766 struct RIL_Address_Wrapped *cur; 777 struct RIL_Address_Wrapped *cur;
767 struct RIL_Network *net; 778 struct RIL_Network *net;
768 779
@@ -869,10 +880,10 @@ envi_reward_local (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
869 dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies 880 dl_norm = 2 - properties[prop_index]; //invert property as we want to maximize for lower latencies
870 881
871 // utilization in [0,1] 882 // utilization in [0,1]
872 bw_norm = ((ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN) 883 bw_norm = (((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_IN)
873 / ril_get_max_bw (agent, GNUNET_YES)) 884 / (double) ril_get_max_bw (agent, GNUNET_YES))
874 + (ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT) 885 + ((double) ril_get_atsi (agent->address_inuse, GNUNET_ATS_UTILIZATION_OUT)
875 / ril_get_max_bw (agent, GNUNET_NO))) / 2; 886 / (double) ril_get_max_bw (agent, GNUNET_NO))) / 2;
876 887
877 // preference matching in [0,4] 888 // preference matching in [0,4]
878 pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm); 889 pref_match += (preferences[GNUNET_ATS_PREFERENCE_LATENCY] * dl_norm);
@@ -896,13 +907,27 @@ envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
896{ 907{
897 struct RIL_Network *net; 908 struct RIL_Network *net;
898 double reward = 0; 909 double reward = 0;
910 unsigned long long assigned_in;
911 unsigned long long assigned_out;
912 unsigned long long overutilized = 0;
899 913
900 //punish overutilization 914 //punish overutilization
901 net = agent->address_inuse->solver_information; 915 net = agent->address_inuse->solver_information;
902 if ((ril_network_get_assigned(solver, net->type, GNUNET_YES) > net->bw_in_available) 916 assigned_in = ril_network_get_assigned(solver, net->type, GNUNET_YES);
903 || (ril_network_get_assigned(solver, net->type, GNUNET_NO) > net->bw_out_available)) 917 assigned_out = ril_network_get_assigned(solver, net->type, GNUNET_NO);
918
919 if (assigned_in > net->bw_in_available)
904 { 920 {
905 return 0; 921 overutilized += assigned_in - net->bw_in_available;
922 }
923 if (assigned_out > net->bw_out_available)
924 {
925 overutilized += assigned_out - net->bw_out_available;
926 }
927 if (overutilized > 0)
928 {
929 //return -1. * (double) overutilized;
930 return -1;
906 } 931 }
907 932
908 reward += envi_reward_global (solver) * (solver->parameters.reward_global_share); 933 reward += envi_reward_global (solver) * (solver->parameters.reward_global_share);
@@ -1036,7 +1061,7 @@ envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
1036 else 1061 else
1037 { 1062 {
1038 new_bw = agent->bw_out - (5 * min_bw); 1063 new_bw = agent->bw_out - (5 * min_bw);
1039 if (new_bw < min_bw || new_bw > agent->bw_in) 1064 if (new_bw < min_bw || new_bw > agent->bw_out)
1040 new_bw = min_bw; 1065 new_bw = min_bw;
1041 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw, 1066 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
1042 GNUNET_NO); 1067 GNUNET_NO);
@@ -1198,6 +1223,13 @@ agent_step (struct RIL_Peer_Agent *agent)
1198 1223
1199 envi_do_action (agent->envi, agent, a_next); 1224 envi_do_action (agent->envi, agent, a_next);
1200 1225
1226 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Agent step %llu: Action: %d Reward: %f Result: IN %llu OUT %llu\n",
1227 agent->step_count,
1228 a_next,
1229 reward,
1230 agent->bw_in/1024,
1231 agent->bw_out/1024);
1232
1201 GNUNET_free(agent->s_old); 1233 GNUNET_free(agent->s_old);
1202 agent->s_old = s_next; 1234 agent->s_old = s_next;
1203 agent->a_old = a_next; 1235 agent->a_old = a_next;
@@ -1236,7 +1268,7 @@ ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1236 for (i = 0; i < solver->networks_count; i++) 1268 for (i = 0; i < solver->networks_count; i++)
1237 { 1269 {
1238 net = solver->network_entries[i]; 1270 net = solver->network_entries[i];
1239 if (ril_network_get_assigned(solver, net.type, GNUNET_YES) > 0) //only consider scopes with an active address 1271 if (ril_network_get_assigned(solver, net.type, GNUNET_YES) > 0) //only consider scopes where an address is actually active
1240 { 1272 {
1241 sum_assigned += ril_network_get_assigned(solver, net.type, GNUNET_YES); 1273 sum_assigned += ril_network_get_assigned(solver, net.type, GNUNET_YES);
1242 sum_assigned += ril_network_get_assigned(solver, net.type, GNUNET_NO); 1274 sum_assigned += ril_network_get_assigned(solver, net.type, GNUNET_NO);
@@ -1347,7 +1379,7 @@ ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1347 1379
1348 GNUNET_assert( 1380 GNUNET_assert(
1349 solver->parameters.step_time_min.rel_value_us 1381 solver->parameters.step_time_min.rel_value_us
1350 < solver->parameters.step_time_max.rel_value_us); 1382 <= solver->parameters.step_time_max.rel_value_us);
1351 1383
1352 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max, 1384 factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
1353 solver->parameters.step_time_min).rel_value_us; 1385 solver->parameters.step_time_min).rel_value_us;
@@ -1406,7 +1438,7 @@ ril_step (struct GAS_RIL_Handle *solver)
1406 //calculate network state vector 1438 //calculate network state vector
1407 envi_state_networks(solver); 1439 envi_state_networks(solver);
1408 1440
1409 //trigger one step per active agent 1441 //trigger one step per active, unblocked agent
1410 for (cur = solver->agents_head; NULL != cur; cur = cur->next) 1442 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1411 { 1443 {
1412 if (cur->is_active) 1444 if (cur->is_active)
@@ -1463,16 +1495,20 @@ agent_w_start (struct RIL_Peer_Agent *agent)
1463 1495
1464 count = ril_count_agents(agent->envi); 1496 count = ril_count_agents(agent->envi);
1465 1497
1466 if (0 == count)
1467 return;
1468
1469 for (other = agent->envi->agents_head; NULL != other; other = other->next) 1498 for (other = agent->envi->agents_head; NULL != other; other = other->next)
1470 { 1499 {
1471 for (i = 0; i < agent->n; i++) 1500 for (i = 0; i < agent->n; i++)
1472 { 1501 {
1473 for (k = 0; k < agent->m; k++) 1502 for (k = 0; k < agent->m; k++)
1474 { 1503 {
1475 agent->W[i][k] += (other->W[i][k] / (double) count); 1504 if (0 == count) {
1505 agent->W[i][k] = 1;
1506 }
1507 else {
1508 agent->W[i][k] += (other->W[i][k] / (double) count);
1509 }
1510
1511 GNUNET_assert(!isinf(agent->W[i][k]));
1476 } 1512 }
1477 } 1513 }
1478 } 1514 }
@@ -1496,8 +1532,8 @@ agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1496 agent->peer = *peer; 1532 agent->peer = *peer;
1497 agent->step_count = 0; 1533 agent->step_count = 0;
1498 agent->is_active = GNUNET_NO; 1534 agent->is_active = GNUNET_NO;
1499 agent->bw_in = 0; 1535 agent->bw_in = 1024;
1500 agent->bw_out = 0; 1536 agent->bw_out = 1024;
1501 agent->suggestion_issue = GNUNET_NO; 1537 agent->suggestion_issue = GNUNET_NO;
1502 agent->n = RIL_ACTION_TYPE_NUM; 1538 agent->n = RIL_ACTION_TYPE_NUM;
1503 agent->m = (solver->networks_count * RIL_FEATURES_NETWORK_COUNT) + GNUNET_ATS_PreferenceCount; 1539 agent->m = (solver->networks_count * RIL_FEATURES_NETWORK_COUNT) + GNUNET_ATS_PreferenceCount;
@@ -1777,6 +1813,7 @@ libgnunet_plugin_ats_ril_init (void *cls)
1777 cur->type = env->networks[c]; 1813 cur->type = env->networks[c];
1778 cur->bw_in_available = env->in_quota[c]; 1814 cur->bw_in_available = env->in_quota[c];
1779 cur->bw_out_available = env->out_quota[c]; 1815 cur->bw_out_available = env->out_quota[c];
1816 LOG(GNUNET_ERROR_TYPE_INFO, "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);
1780 } 1817 }
1781 1818
1782 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed ( 1819 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (
@@ -1784,6 +1821,16 @@ libgnunet_plugin_ats_ril_init (void *cls)
1784 &ril_step_scheduler_task, solver); 1821 &ril_step_scheduler_task, solver);
1785 solver->step_task_pending = GNUNET_YES; 1822 solver->step_task_pending = GNUNET_YES;
1786 1823
1824 LOG(GNUNET_ERROR_TYPE_INFO, "Parameters:\n");
1825 LOG(GNUNET_ERROR_TYPE_INFO, "Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
1826 solver->parameters.algorithm ? "Q" : "SARSA",
1827 solver->parameters.alpha,
1828 solver->parameters.beta,
1829 solver->parameters.lambda);
1830 LOG(GNUNET_ERROR_TYPE_INFO, "explore = %f, global_share = %f\n",
1831 solver->parameters.explore_ratio,
1832 solver->parameters.reward_global_share);
1833
1787 return solver; 1834 return solver;
1788} 1835}
1789 1836
@@ -1985,28 +2032,19 @@ GAS_ril_address_delete (void *solver, struct ATS_Address *address, int session_o
1985 agent->m = m_new; 2032 agent->m = m_new;
1986 agent->n = n_new; 2033 agent->n = n_new;
1987 2034
1988 LOG(GNUNET_ERROR_TYPE_DEBUG, "address was used: %s\n", address_was_used ? "yes" : "no");
1989
1990 if (address_was_used) 2035 if (address_was_used)
1991 { 2036 {
1992 if (NULL != agent->addresses_head) //if peer has an address left, use it 2037 if (NULL != agent->addresses_head) //if peer has an address left, use it
1993 { 2038 {
1994 LOG(GNUNET_ERROR_TYPE_DEBUG, "address left: %s\n",
1995 agent->addresses_head->address_naked->addr);
1996
1997 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw, 2039 envi_set_active_suggestion (s, agent, agent->addresses_head->address_naked, min_bw, min_bw,
1998 GNUNET_NO); 2040 GNUNET_NO);
1999 } 2041 }
2000 else 2042 else
2001 { 2043 {
2002 LOG(GNUNET_ERROR_TYPE_DEBUG, "no address left => disconnect\n");
2003
2004 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO); 2044 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2005 } 2045 }
2006 } 2046 }
2007 2047
2008 LOG(GNUNET_ERROR_TYPE_DEBUG, "Address deleted: %s\n", address->addr);
2009
2010 ril_step (solver); 2048 ril_step (solver);
2011} 2049}
2012 2050