aboutsummaryrefslogtreecommitdiff
path: root/src/ats/plugin_ats_ril.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ats/plugin_ats_ril.c')
-rw-r--r--src/ats/plugin_ats_ril.c2423
1 files changed, 1281 insertions, 1142 deletions
diff --git a/src/ats/plugin_ats_ril.c b/src/ats/plugin_ats_ril.c
index abf265f19..7be3cb0a1 100644
--- a/src/ats/plugin_ats_ril.c
+++ b/src/ats/plugin_ats_ril.c
@@ -32,17 +32,21 @@
32 32
33 33
34 34
35#define LOG(kind, ...) GNUNET_log_from(kind, "ats-ril", __VA_ARGS__) 35#define LOG(kind, ...) GNUNET_log_from (kind, "ats-ril", __VA_ARGS__)
36 36
37#define RIL_MIN_BW (5 * ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) 37#define RIL_MIN_BW (5 * ntohl ( \
38 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT. \
39 value__))
38#define RIL_MAX_BW GNUNET_ATS_MaxBandwidth 40#define RIL_MAX_BW GNUNET_ATS_MaxBandwidth
39 41
40#define RIL_ACTION_INVALID -1 42#define RIL_ACTION_INVALID -1
41#define RIL_INTERVAL_EXPONENT 10 43#define RIL_INTERVAL_EXPONENT 10
42#define RIL_UTILITY_DELAY_MAX 1000 44#define RIL_UTILITY_DELAY_MAX 1000
43 45
44#define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 200) 46#define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply ( \
45#define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 2000) 47 GNUNET_TIME_UNIT_MILLISECONDS, 200)
48#define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply ( \
49 GNUNET_TIME_UNIT_MILLISECONDS, 2000)
46#define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA 50#define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
47#define RIL_DEFAULT_SELECT RIL_SELECT_SOFTMAX 51#define RIL_DEFAULT_SELECT RIL_SELECT_SOFTMAX
48#define RIL_DEFAULT_WELFARE RIL_WELFARE_NASH 52#define RIL_DEFAULT_WELFARE RIL_WELFARE_NASH
@@ -71,9 +75,10 @@
71 * to a particular address. The action of switching to address with index i is depicted by the 75 * to a particular address. The action of switching to address with index i is depicted by the
72 * number (RIL_ACTION_TYPE_NUM + i). 76 * number (RIL_ACTION_TYPE_NUM + i).
73 */ 77 */
74enum RIL_Action_Type { 78enum RIL_Action_Type
79{
75 RIL_ACTION_NOTHING = 0, 80 RIL_ACTION_NOTHING = 0,
76 RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions 81 RIL_ACTION_BW_IN_DBL = -2, // TODO? Potentially add more actions
77 RIL_ACTION_BW_IN_HLV = -3, 82 RIL_ACTION_BW_IN_HLV = -3,
78 RIL_ACTION_BW_IN_INC = 1, 83 RIL_ACTION_BW_IN_INC = 1,
79 RIL_ACTION_BW_IN_DEC = 2, 84 RIL_ACTION_BW_IN_DEC = 2,
@@ -84,22 +89,26 @@ enum RIL_Action_Type {
84 RIL_ACTION_TYPE_NUM = 5 89 RIL_ACTION_TYPE_NUM = 5
85}; 90};
86 91
87enum RIL_Algorithm { 92enum RIL_Algorithm
93{
88 RIL_ALGO_SARSA = 0, 94 RIL_ALGO_SARSA = 0,
89 RIL_ALGO_Q = 1 95 RIL_ALGO_Q = 1
90}; 96};
91 97
92enum RIL_Select { 98enum RIL_Select
99{
93 RIL_SELECT_SOFTMAX = 0, 100 RIL_SELECT_SOFTMAX = 0,
94 RIL_SELECT_EGREEDY = 1 101 RIL_SELECT_EGREEDY = 1
95}; 102};
96 103
97enum RIL_Welfare { 104enum RIL_Welfare
105{
98 RIL_WELFARE_NASH, 106 RIL_WELFARE_NASH,
99 RIL_WELFARE_EGALITARIAN 107 RIL_WELFARE_EGALITARIAN
100}; 108};
101 109
102enum RIL_E_Modification { 110enum RIL_E_Modification
111{
103 RIL_E_DECAY, 112 RIL_E_DECAY,
104 RIL_E_ZERO, 113 RIL_E_ZERO,
105 RIL_E_ACCUMULATE, 114 RIL_E_ACCUMULATE,
@@ -109,7 +118,8 @@ enum RIL_E_Modification {
109/** 118/**
110 * Global learning parameters 119 * Global learning parameters
111 */ 120 */
112struct RIL_Learning_Parameters { 121struct RIL_Learning_Parameters
122{
113 /** 123 /**
114 * The TD-algorithm to use 124 * The TD-algorithm to use
115 */ 125 */
@@ -199,7 +209,8 @@ struct RIL_Learning_Parameters {
199/** 209/**
200 * Wrapper for addresses to store them in agent's linked list 210 * Wrapper for addresses to store them in agent's linked list
201 */ 211 */
202struct RIL_Address_Wrapped { 212struct RIL_Address_Wrapped
213{
203 /** 214 /**
204 * Next in DLL 215 * Next in DLL
205 */ 216 */
@@ -217,7 +228,8 @@ struct RIL_Address_Wrapped {
217}; 228};
218 229
219 230
220struct RIL_Peer_Agent { 231struct RIL_Peer_Agent
232{
221 /** 233 /**
222 * Next agent in solver's linked list 234 * Next agent in solver's linked list
223 */ 235 */
@@ -251,7 +263,7 @@ struct RIL_Peer_Agent {
251 /** 263 /**
252 * Experience matrix W 264 * Experience matrix W
253 */ 265 */
254 double ** W; 266 double **W;
255 267
256 /** 268 /**
257 * Number of rows of W / Number of state-vector features 269 * Number of rows of W / Number of state-vector features
@@ -276,7 +288,7 @@ struct RIL_Peer_Agent {
276 /** 288 /**
277 * Eligibility traces 289 * Eligibility traces
278 */ 290 */
279 double ** E; 291 double **E;
280 292
281 /** 293 /**
282 * Whether to reset the eligibility traces to 0 after a Q-exploration step 294 * Whether to reset the eligibility traces to 0 after a Q-exploration step
@@ -329,7 +341,8 @@ struct RIL_Peer_Agent {
329 double nop_bonus; 341 double nop_bonus;
330}; 342};
331 343
332struct RIL_Scope { 344struct RIL_Scope
345{
333 /** 346 /**
334 * ATS network type 347 * ATS network type
335 */ 348 */
@@ -379,7 +392,8 @@ struct RIL_Scope {
379/** 392/**
380 * A handle for the reinforcement learning solver 393 * A handle for the reinforcement learning solver
381 */ 394 */
382struct GAS_RIL_Handle { 395struct GAS_RIL_Handle
396{
383 /** 397 /**
384 * The solver-plugin environment of the solver-plugin API 398 * The solver-plugin environment of the solver-plugin API
385 */ 399 */
@@ -398,7 +412,7 @@ struct GAS_RIL_Handle {
398 /** 412 /**
399 * Task identifier of the next time-step to be executed 413 * Task identifier of the next time-step to be executed
400 */ 414 */
401 struct GNUNET_SCHEDULER_Task * step_next_task_id; 415 struct GNUNET_SCHEDULER_Task *step_next_task_id;
402 416
403 /** 417 /**
404 * Variable discount factor, dependent on time between steps 418 * Variable discount factor, dependent on time between steps
@@ -428,7 +442,7 @@ struct GAS_RIL_Handle {
428 /** 442 /**
429 * Array of networks with global assignment state 443 * Array of networks with global assignment state
430 */ 444 */
431 struct RIL_Scope * network_entries; 445 struct RIL_Scope *network_entries;
432 446
433 /** 447 /**
434 * Networks count 448 * Networks count
@@ -438,8 +452,8 @@ struct GAS_RIL_Handle {
438 /** 452 /**
439 * List of active peer-agents 453 * List of active peer-agents
440 */ 454 */
441 struct RIL_Peer_Agent * agents_head; 455 struct RIL_Peer_Agent *agents_head;
442 struct RIL_Peer_Agent * agents_tail; 456 struct RIL_Peer_Agent *agents_tail;
443 457
444 /** 458 /**
445 * Shutdown 459 * Shutdown
@@ -466,9 +480,9 @@ struct GAS_RIL_Handle {
466 * @return estimation value 480 * @return estimation value
467 */ 481 */
468static double 482static double
469agent_q(struct RIL_Peer_Agent *agent, 483agent_q (struct RIL_Peer_Agent *agent,
470 const double *state, 484 const double *state,
471 int action) 485 int action)
472{ 486{
473 unsigned int i; 487 unsigned int i;
474 double result = 0.0; 488 double result = 0.0;
@@ -477,10 +491,10 @@ agent_q(struct RIL_Peer_Agent *agent,
477 result += state[i] * agent->W[action][i]; 491 result += state[i] * agent->W[action][i];
478 492
479 /* prevent crashes if learning diverges */ 493 /* prevent crashes if learning diverges */
480 if (isnan(result)) 494 if (isnan (result))
481 return isnan(result) * UINT32_MAX; 495 return isnan (result) * UINT32_MAX;
482 if (isinf(result)) 496 if (isinf (result))
483 return isinf(result) * UINT32_MAX; 497 return isinf (result) * UINT32_MAX;
484 return result; 498 return result;
485} 499}
486 500
@@ -493,18 +507,19 @@ agent_q(struct RIL_Peer_Agent *agent,
493 * @return the index, starting with zero 507 * @return the index, starting with zero
494 */ 508 */
495static int 509static int
496agent_address_get_index(struct RIL_Peer_Agent *agent, struct ATS_Address *address) 510agent_address_get_index (struct RIL_Peer_Agent *agent, struct
511 ATS_Address *address)
497{ 512{
498 int i; 513 int i;
499 struct RIL_Address_Wrapped *cur; 514 struct RIL_Address_Wrapped *cur;
500 515
501 i = -1; 516 i = -1;
502 for (cur = agent->addresses_head; NULL != cur; cur = cur->next) 517 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
503 { 518 {
504 i++; 519 i++;
505 if (cur->address_naked == address) 520 if (cur->address_naked == address)
506 return i; 521 return i;
507 } 522 }
508 return i; 523 return i;
509} 524}
510 525
@@ -517,7 +532,8 @@ agent_address_get_index(struct RIL_Peer_Agent *agent, struct ATS_Address *addres
517 * @return wrapped address 532 * @return wrapped address
518 */ 533 */
519static struct RIL_Address_Wrapped * 534static struct RIL_Address_Wrapped *
520agent_address_get_wrapped(struct RIL_Peer_Agent *agent, struct ATS_Address *address) 535agent_address_get_wrapped (struct RIL_Peer_Agent *agent, struct
536 ATS_Address *address)
521{ 537{
522 struct RIL_Address_Wrapped *cur; 538 struct RIL_Address_Wrapped *cur;
523 539
@@ -529,67 +545,70 @@ agent_address_get_wrapped(struct RIL_Peer_Agent *agent, struct ATS_Address *addr
529 545
530 546
531static int 547static int
532agent_action_is_possible(struct RIL_Peer_Agent *agent, int action) 548agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
533{ 549{
534 int address_index; 550 int address_index;
535 551
536 switch (action) 552 switch (action)
537 { 553 {
538 case RIL_ACTION_NOTHING: 554 case RIL_ACTION_NOTHING:
555 return GNUNET_YES;
556 break;
557
558 case RIL_ACTION_BW_IN_INC:
559 case RIL_ACTION_BW_IN_DBL:
560 if (agent->bw_in >= RIL_MAX_BW)
561 return GNUNET_NO;
562 else
539 return GNUNET_YES; 563 return GNUNET_YES;
540 break; 564 break;
541 565
542 case RIL_ACTION_BW_IN_INC: 566 case RIL_ACTION_BW_IN_DEC:
543 case RIL_ACTION_BW_IN_DBL: 567 case RIL_ACTION_BW_IN_HLV:
544 if (agent->bw_in >= RIL_MAX_BW) 568 if (agent->bw_in <= 0)
545 return GNUNET_NO; 569 return GNUNET_NO;
546 else 570 else
547 return GNUNET_YES; 571 return GNUNET_YES;
548 break; 572 break;
549 573
550 case RIL_ACTION_BW_IN_DEC: 574 case RIL_ACTION_BW_OUT_INC:
551 case RIL_ACTION_BW_IN_HLV: 575 case RIL_ACTION_BW_OUT_DBL:
552 if (agent->bw_in <= 0) 576 if (agent->bw_out >= RIL_MAX_BW)
553 return GNUNET_NO; 577 return GNUNET_NO;
554 else 578 else
555 return GNUNET_YES; 579 return GNUNET_YES;
556 break; 580 break;
557 581
558 case RIL_ACTION_BW_OUT_INC: 582 case RIL_ACTION_BW_OUT_DEC:
559 case RIL_ACTION_BW_OUT_DBL: 583 case RIL_ACTION_BW_OUT_HLV:
560 if (agent->bw_out >= RIL_MAX_BW) 584 if (agent->bw_out <= 0)
561 return GNUNET_NO; 585 return GNUNET_NO;
562 else 586 else
563 return GNUNET_YES; 587 return GNUNET_YES;
564 break; 588 break;
565 589
566 case RIL_ACTION_BW_OUT_DEC: 590 default:
567 case RIL_ACTION_BW_OUT_HLV: 591 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) // switch address action
568 if (agent->bw_out <= 0) 592 {
593 address_index = action - RIL_ACTION_TYPE_NUM;
594
595 GNUNET_assert (address_index >= 0);
596 GNUNET_assert (
597 address_index <= agent_address_get_index (agent,
598 agent->addresses_tail->
599 address_naked));
600
601 if ((agent_address_get_index (agent, agent->address_inuse) ==
602 address_index) ||
603 agent->address_inuse->active)
569 return GNUNET_NO; 604 return GNUNET_NO;
570 else 605 else
571 return GNUNET_YES; 606 return GNUNET_YES;
572 break; 607 break;
573
574 default:
575 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
576 {
577 address_index = action - RIL_ACTION_TYPE_NUM;
578
579 GNUNET_assert(address_index >= 0);
580 GNUNET_assert(
581 address_index <= agent_address_get_index(agent, agent->addresses_tail->address_naked));
582
583 if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
584 agent->address_inuse->active)
585 return GNUNET_NO;
586 else
587 return GNUNET_YES;
588 break;
589 }
590 // error - action does not exist
591 GNUNET_assert(GNUNET_NO);
592 } 608 }
609 // error - action does not exist
610 GNUNET_assert (GNUNET_NO);
611 }
593} 612}
594 613
595 614
@@ -602,7 +621,7 @@ agent_action_is_possible(struct RIL_Peer_Agent *agent, int action)
602 * @return the action promising most future reward 621 * @return the action promising most future reward
603 */ 622 */
604static int 623static int
605agent_get_action_max(struct RIL_Peer_Agent *agent, double *state) 624agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
606{ 625{
607 int i; 626 int i;
608 int max_i = RIL_ACTION_INVALID; 627 int max_i = RIL_ACTION_INVALID;
@@ -610,19 +629,19 @@ agent_get_action_max(struct RIL_Peer_Agent *agent, double *state)
610 double max_q = -DBL_MAX; 629 double max_q = -DBL_MAX;
611 630
612 for (i = 0; i < agent->n; i++) 631 for (i = 0; i < agent->n; i++)
632 {
633 if (agent_action_is_possible (agent, i))
613 { 634 {
614 if (agent_action_is_possible(agent, i)) 635 cur_q = agent_q (agent, state, i);
615 { 636 if (cur_q > max_q)
616 cur_q = agent_q(agent, state, i); 637 {
617 if (cur_q > max_q) 638 max_q = cur_q;
618 { 639 max_i = i;
619 max_q = cur_q; 640 }
620 max_i = i;
621 }
622 }
623 } 641 }
642 }
624 643
625 GNUNET_assert(RIL_ACTION_INVALID != max_i); 644 GNUNET_assert (RIL_ACTION_INVALID != max_i);
626 645
627 return max_i; 646 return max_i;
628} 647}
@@ -634,7 +653,7 @@ agent_get_action_max(struct RIL_Peer_Agent *agent, double *state)
634 * @return the action index 653 * @return the action index
635 */ 654 */
636static int 655static int
637agent_get_action_random(struct RIL_Peer_Agent *agent) 656agent_get_action_random (struct RIL_Peer_Agent *agent)
638{ 657{
639 int i; 658 int i;
640 int is_possible[agent->n]; 659 int is_possible[agent->n];
@@ -642,32 +661,32 @@ agent_get_action_random(struct RIL_Peer_Agent *agent)
642 int r; 661 int r;
643 662
644 for (i = 0; i < agent->n; i++) 663 for (i = 0; i < agent->n; i++)
664 {
665 if (agent_action_is_possible (agent, i))
645 { 666 {
646 if (agent_action_is_possible(agent, i)) 667 is_possible[i] = GNUNET_YES;
647 { 668 sum++;
648 is_possible[i] = GNUNET_YES; 669 }
649 sum++; 670 else
650 } 671 {
651 else 672 is_possible[i] = GNUNET_NO;
652 {
653 is_possible[i] = GNUNET_NO;
654 }
655 } 673 }
674 }
656 675
657 r = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, sum); 676 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
658 677
659 sum = -1; 678 sum = -1;
660 for (i = 0; i < agent->n; i++) 679 for (i = 0; i < agent->n; i++)
680 {
681 if (is_possible[i])
661 { 682 {
662 if (is_possible[i]) 683 sum++;
663 { 684 if (sum == r)
664 sum++; 685 return i;
665 if (sum == r)
666 return i;
667 }
668 } 686 }
687 }
669 688
670 GNUNET_assert(GNUNET_NO); 689 GNUNET_assert (GNUNET_NO);
671 return RIL_ACTION_INVALID; 690 return RIL_ACTION_INVALID;
672} 691}
673 692
@@ -681,16 +700,18 @@ agent_get_action_random(struct RIL_Peer_Agent *agent)
681 * @param a_prime the new 700 * @param a_prime the new
682 */ 701 */
683static void 702static void
684agent_update(struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime) 703agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int
704 a_prime)
685{ 705{
686 int i; 706 int i;
687 int k; 707 int k;
688 double delta; 708 double delta;
689 double **theta = agent->W; 709 double **theta = agent->W;
690 710
691 delta = agent->envi->global_discount_integrated * reward; //reward 711 delta = agent->envi->global_discount_integrated * reward; // reward
692 delta += agent->envi->global_discount_variable * agent_q(agent, s_next, a_prime); //discounted future value 712 delta += agent->envi->global_discount_variable * agent_q (agent, s_next,
693 delta -= agent_q(agent, agent->s_old, agent->a_old); //one step 713 a_prime); // discounted future value
714 delta -= agent_q (agent, agent->s_old, agent->a_old); // one step
694 715
695// 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", 716// 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",
696// agent->step_count, 717// agent->step_count,
@@ -702,17 +723,17 @@ agent_update(struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_
702// delta); 723// delta);
703 724
704 for (k = 0; k < agent->n; k++) 725 for (k = 0; k < agent->n; k++)
726 {
727 for (i = 0; i < agent->m; i++)
705 { 728 {
706 for (i = 0; i < agent->m; i++) 729 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
707 { 730 // agent->envi->parameters.alpha,
708 // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n", 731 // delta,
709 // agent->envi->parameters.alpha, 732 // i,
710 // delta, 733 // agent->e[i]);
711 // i, 734 theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
712 // agent->e[i]);
713 theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
714 }
715 } 735 }
736 }
716} 737}
717 738
718 739
@@ -729,41 +750,43 @@ agent_update(struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_
729 * @param action the action to take 750 * @param action the action to take
730 */ 751 */
731static void 752static void
732agent_modify_eligibility(struct RIL_Peer_Agent *agent, 753agent_modify_eligibility (struct RIL_Peer_Agent *agent,
733 enum RIL_E_Modification mod, 754 enum RIL_E_Modification mod,
734 double *feature, 755 double *feature,
735 int action) 756 int action)
736{ 757{
737 int i; 758 int i;
738 int k; 759 int k;
739 760
740 for (i = 0; i < agent->m; i++) 761 for (i = 0; i < agent->m; i++)
762 {
763 switch (mod)
741 { 764 {
742 switch (mod) 765 case RIL_E_ACCUMULATE:
743 { 766 agent->E[action][i] += feature[i];
744 case RIL_E_ACCUMULATE: 767 break;
745 agent->E[action][i] += feature[i]; 768
746 break; 769 case RIL_E_REPLACE:
747 770 agent->E[action][i] = agent->E[action][i] > feature[i] ?
748 case RIL_E_REPLACE: 771 agent->E[action][i] : feature[i];
749 agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i]; 772 break;
750 break; 773
751 774 case RIL_E_DECAY:
752 case RIL_E_DECAY: 775 for (k = 0; k < agent->n; k++)
753 for (k = 0; k < agent->n; k++) 776 {
754 { 777 agent->E[k][i] *= agent->envi->global_discount_variable
755 agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda; 778 * agent->envi->parameters.lambda;
756 } 779 }
757 break; 780 break;
758 781
759 case RIL_E_ZERO: 782 case RIL_E_ZERO:
760 for (k = 0; k < agent->n; k++) 783 for (k = 0; k < agent->n; k++)
761 { 784 {
762 agent->E[k][i] = 0; 785 agent->E[k][i] = 0;
763 } 786 }
764 break; 787 break;
765 }
766 } 788 }
789 }
767} 790}
768 791
769/** 792/**
@@ -774,14 +797,14 @@ agent_modify_eligibility(struct RIL_Peer_Agent *agent,
774 * @param stat 797 * @param stat
775 */ 798 */
776static void 799static void
777ril_inform(struct GAS_RIL_Handle *solver, 800ril_inform (struct GAS_RIL_Handle *solver,
778 enum GAS_Solver_Operation op, 801 enum GAS_Solver_Operation op,
779 enum GAS_Solver_Status stat) 802 enum GAS_Solver_Status stat)
780{ 803{
781 solver->env->info_cb(solver->env->cls, 804 solver->env->info_cb (solver->env->cls,
782 op, 805 op,
783 stat, 806 stat,
784 GAS_INFO_NONE); 807 GAS_INFO_NONE);
785} 808}
786 809
787/** 810/**
@@ -790,9 +813,11 @@ ril_inform(struct GAS_RIL_Handle *solver,
790 * @param net 813 * @param net
791 */ 814 */
792static unsigned long long 815static unsigned long long
793ril_get_max_bw(struct RIL_Scope *net) 816ril_get_max_bw (struct RIL_Scope *net)
794{ 817{
795 return GNUNET_MIN(2 * GNUNET_MAX(net->bw_in_available, net->bw_out_available), GNUNET_ATS_MaxBandwidth); 818 return GNUNET_MIN (2 * GNUNET_MAX (net->bw_in_available,
819 net->bw_out_available),
820 GNUNET_ATS_MaxBandwidth);
796} 821}
797 822
798/** 823/**
@@ -807,82 +832,84 @@ ril_get_max_bw(struct RIL_Scope *net)
807 * @param silent disables invocation of the bw_changed callback, if #GNUNET_YES 832 * @param silent disables invocation of the bw_changed callback, if #GNUNET_YES
808 */ 833 */
809static void 834static void
810envi_set_active_suggestion(struct GAS_RIL_Handle *solver, 835envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
811 struct RIL_Peer_Agent *agent, 836 struct RIL_Peer_Agent *agent,
812 struct ATS_Address *new_address, 837 struct ATS_Address *new_address,
813 unsigned long long new_bw_in, 838 unsigned long long new_bw_in,
814 unsigned long long new_bw_out, 839 unsigned long long new_bw_out,
815 int silent) 840 int silent)
816{ 841{
817 int notify = GNUNET_NO; 842 int notify = GNUNET_NO;
818 843
819 LOG(GNUNET_ERROR_TYPE_DEBUG, 844 LOG (GNUNET_ERROR_TYPE_DEBUG,
820 " set_active_suggestion() for peer '%s'\n", 845 " set_active_suggestion() for peer '%s'\n",
821 GNUNET_i2s(&agent->peer)); 846 GNUNET_i2s (&agent->peer));
822 847
823 //address change 848 // address change
824 if (agent->address_inuse != new_address) 849 if (agent->address_inuse != new_address)
850 {
851 if (NULL != agent->address_inuse)
825 { 852 {
826 if (NULL != agent->address_inuse) 853 agent->address_inuse->active = GNUNET_NO;
827 { 854 agent->address_inuse->assigned_bw_in = 0;
828 agent->address_inuse->active = GNUNET_NO; 855 agent->address_inuse->assigned_bw_out = 0;
829 agent->address_inuse->assigned_bw_in = 0; 856 }
830 agent->address_inuse->assigned_bw_out = 0; 857 if (NULL != new_address)
831 } 858 {
832 if (NULL != new_address) 859 LOG (GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n",
833 { 860 agent->is_active ? "yes" : "no");
834 LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no"); 861 new_address->active = agent->is_active;
835 new_address->active = agent->is_active; 862 new_address->assigned_bw_in = agent->bw_in;
836 new_address->assigned_bw_in = agent->bw_in; 863 new_address->assigned_bw_out = agent->bw_out;
837 new_address->assigned_bw_out = agent->bw_out;
838 }
839 notify |= GNUNET_YES;
840 } 864 }
865 notify |= GNUNET_YES;
866 }
841 867
842 if (new_address) 868 if (new_address)
869 {
870 // activity change
871 if (new_address->active != agent->is_active)
843 { 872 {
844 //activity change 873 new_address->active = agent->is_active;
845 if (new_address->active != agent->is_active) 874 notify |= GNUNET_YES;
846 {
847 new_address->active = agent->is_active;
848 notify |= GNUNET_YES;
849 }
850
851 //bw change
852 if (agent->bw_in != new_bw_in)
853 {
854 agent->bw_in = new_bw_in;
855 new_address->assigned_bw_in = new_bw_in;
856 notify |= GNUNET_YES;
857 }
858 if (agent->bw_out != new_bw_out)
859 {
860 agent->bw_out = new_bw_out;
861 new_address->assigned_bw_out = new_bw_out;
862 notify |= GNUNET_YES;
863 }
864 } 875 }
865 876
877 // bw change
878 if (agent->bw_in != new_bw_in)
879 {
880 agent->bw_in = new_bw_in;
881 new_address->assigned_bw_in = new_bw_in;
882 notify |= GNUNET_YES;
883 }
884 if (agent->bw_out != new_bw_out)
885 {
886 agent->bw_out = new_bw_out;
887 new_address->assigned_bw_out = new_bw_out;
888 notify |= GNUNET_YES;
889 }
890 }
891
866 if (notify && agent->is_active && (GNUNET_NO == silent)) 892 if (notify && agent->is_active && (GNUNET_NO == silent))
893 {
894 if (new_address)
867 { 895 {
868 if (new_address) 896 LOG (GNUNET_ERROR_TYPE_DEBUG,
869 { 897 " envi_set_active_suggestion() notify\n");
870 LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n"); 898 agent->suggestion_issue = GNUNET_YES;
871 agent->suggestion_issue = GNUNET_YES; 899 agent->suggestion_address = new_address;
872 agent->suggestion_address = new_address;
873 }
874 else if (agent->address_inuse)
875 {
876 /* disconnect case, no new address */
877 GNUNET_assert(0 == agent->address_inuse->assigned_bw_in);
878 GNUNET_assert(0 == agent->address_inuse->assigned_bw_out);
879 agent->bw_in = 0;
880 agent->bw_out = 0;
881
882 agent->suggestion_issue = GNUNET_YES;
883 agent->suggestion_address = agent->address_inuse;
884 }
885 } 900 }
901 else if (agent->address_inuse)
902 {
903 /* disconnect case, no new address */
904 GNUNET_assert (0 == agent->address_inuse->assigned_bw_in);
905 GNUNET_assert (0 == agent->address_inuse->assigned_bw_out);
906 agent->bw_in = 0;
907 agent->bw_out = 0;
908
909 agent->suggestion_issue = GNUNET_YES;
910 agent->suggestion_address = agent->address_inuse;
911 }
912 }
886 agent->address_inuse = new_address; 913 agent->address_inuse = new_address;
887} 914}
888 915
@@ -894,7 +921,7 @@ envi_set_active_suggestion(struct GAS_RIL_Handle *solver,
894 * @return pointer to the state vector 921 * @return pointer to the state vector
895 */ 922 */
896static double * 923static double *
897envi_get_state(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent) 924envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
898{ 925{
899 double *state; 926 double *state;
900 double y[2]; 927 double y[2];
@@ -907,27 +934,34 @@ envi_get_state(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
907 int k; 934 int k;
908 unsigned long long max_bw; 935 unsigned long long max_bw;
909 936
910 state = GNUNET_malloc(sizeof(double) * agent->m); 937 state = GNUNET_malloc (sizeof(double) * agent->m);
911 938
912 max_bw = ril_get_max_bw((struct RIL_Scope *)agent->address_inuse->solver_information); 939 max_bw = ril_get_max_bw ((struct
940 RIL_Scope *) agent->address_inuse->
941 solver_information);
913 942
914 y[0] = (double)agent->bw_out; 943 y[0] = (double) agent->bw_out;
915 y[1] = (double)agent->bw_in; 944 y[1] = (double) agent->bw_in;
916 945
917 m = agent_address_get_index(agent, agent->address_inuse) * (solver->parameters.rbf_divisor + 1) * (solver->parameters.rbf_divisor + 1); 946 m = agent_address_get_index (agent, agent->address_inuse)
947 * (solver->parameters.rbf_divisor + 1) * (solver->parameters.rbf_divisor
948 + 1);
918 for (i = 0; i <= solver->parameters.rbf_divisor; i++) 949 for (i = 0; i <= solver->parameters.rbf_divisor; i++)
919 { 950 {
920 for (k = 0; k <= solver->parameters.rbf_divisor; k++) 951 for (k = 0; k <= solver->parameters.rbf_divisor; k++)
921 { 952 {
922 x[0] = (double)i * (double)max_bw / (double)solver->parameters.rbf_divisor; 953 x[0] = (double) i * (double) max_bw
923 x[1] = (double)k * (double)max_bw / (double)solver->parameters.rbf_divisor; 954 / (double) solver->parameters.rbf_divisor;
924 d[0] = x[0] - y[0]; 955 x[1] = (double) k * (double) max_bw
925 d[1] = x[1] - y[1]; 956 / (double) solver->parameters.rbf_divisor;
926 sigma = (((double)max_bw / ((double)solver->parameters.rbf_divisor + 1)) * 0.5); 957 d[0] = x[0] - y[0];
927 f = exp(-((d[0] * d[0] + d[1] * d[1]) / (2 * sigma * sigma))); 958 d[1] = x[1] - y[1];
928 state[m++] = f; 959 sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor
929 } 960 + 1)) * 0.5);
930 } 961 f = exp (-((d[0] * d[0] + d[1] * d[1]) / (2 * sigma * sigma)));
962 state[m++] = f;
963 }
964 }
931 965
932 return state; 966 return state;
933} 967}
@@ -940,22 +974,23 @@ envi_get_state(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
940 * @return the utility value 974 * @return the utility value
941 */ 975 */
942static double 976static double
943agent_get_utility(struct RIL_Peer_Agent *agent) 977agent_get_utility (struct RIL_Peer_Agent *agent)
944{ 978{
945 const double *preferences; 979 const double *preferences;
946 double delay_atsi; 980 double delay_atsi;
947 double delay_norm; 981 double delay_norm;
948 double pref_match; 982 double pref_match;
949 983
950 preferences = agent->envi->env->get_preferences(agent->envi->env->cls, 984 preferences = agent->envi->env->get_preferences (agent->envi->env->cls,
951 &agent->peer); 985 &agent->peer);
952 986
953 delay_atsi = agent->address_inuse->norm_delay.norm; 987 delay_atsi = agent->address_inuse->norm_delay.norm;
954 delay_norm = RIL_UTILITY_DELAY_MAX * exp(-delay_atsi * 0.00001); 988 delay_norm = RIL_UTILITY_DELAY_MAX * exp (-delay_atsi * 0.00001);
955 989
956 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm; 990 pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
957 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] * 991 pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH]
958 sqrt((double)(agent->bw_in / RIL_MIN_BW) * (double)(agent->bw_out / RIL_MIN_BW)); 992 * sqrt ((double) (agent->bw_in / RIL_MIN_BW)
993 * (double) (agent->bw_out / RIL_MIN_BW));
959 return pref_match; 994 return pref_match;
960} 995}
961 996
@@ -968,41 +1003,45 @@ agent_get_utility(struct RIL_Peer_Agent *agent)
968 * @return the social welfare value 1003 * @return the social welfare value
969 */ 1004 */
970static double 1005static double
971ril_network_get_social_welfare(struct GAS_RIL_Handle *solver, struct RIL_Scope *scope) 1006ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct
1007 RIL_Scope *scope)
972{ 1008{
973 struct RIL_Peer_Agent *cur; 1009 struct RIL_Peer_Agent *cur;
974 double result; 1010 double result;
975 1011
976 switch (solver->parameters.social_welfare) 1012 switch (solver->parameters.social_welfare)
977 { 1013 {
978 case RIL_WELFARE_EGALITARIAN: 1014 case RIL_WELFARE_EGALITARIAN:
979 result = DBL_MAX; 1015 result = DBL_MAX;
980 for (cur = solver->agents_head; NULL != cur; cur = cur->next) 1016 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
981 { 1017 {
982 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope)) 1018 if (cur->is_active && cur->address_inuse &&
983 { 1019 (cur->address_inuse->solver_information == scope))
984 result = GNUNET_MIN(result, agent_get_utility(cur)); 1020 {
985 } 1021 result = GNUNET_MIN (result, agent_get_utility (cur));
986 } 1022 }
987 return result; 1023 }
988 1024 return result;
989 case RIL_WELFARE_NASH: 1025
990 result = 0; 1026 case RIL_WELFARE_NASH:
991 for (cur = solver->agents_head; NULL != cur; cur = cur->next) 1027 result = 0;
992 { 1028 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
993 if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope)) 1029 {
994 { 1030 if (cur->is_active && cur->address_inuse &&
995 result *= pow(agent_get_utility(cur), 1.0 / (double)scope->active_agent_count); 1031 (cur->address_inuse->solver_information == scope))
996 } 1032 {
997 } 1033 result *= pow (agent_get_utility (cur), 1.0
998 return result; 1034 / (double) scope->active_agent_count);
999 } 1035 }
1000 GNUNET_assert(GNUNET_NO); 1036 }
1037 return result;
1038 }
1039 GNUNET_assert (GNUNET_NO);
1001 return 1; 1040 return 1;
1002} 1041}
1003 1042
1004static double 1043static double
1005envi_get_penalty(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent) 1044envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1006{ 1045{
1007 struct RIL_Scope *net; 1046 struct RIL_Scope *net;
1008 unsigned long long over_max; 1047 unsigned long long over_max;
@@ -1012,26 +1051,26 @@ envi_get_penalty(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1012 net = agent->address_inuse->solver_information; 1051 net = agent->address_inuse->solver_information;
1013 1052
1014 if (net->bw_in_utilized > net->bw_in_available) 1053 if (net->bw_in_utilized > net->bw_in_available)
1054 {
1055 over_in = net->bw_in_utilized - net->bw_in_available;
1056 if (RIL_ACTION_BW_IN_INC == agent->a_old)
1015 { 1057 {
1016 over_in = net->bw_in_utilized - net->bw_in_available; 1058 /* increase quadratically */
1017 if (RIL_ACTION_BW_IN_INC == agent->a_old) 1059 over_in *= over_in;
1018 {
1019 /* increase quadratically */
1020 over_in *= over_in;
1021 }
1022 } 1060 }
1061 }
1023 if (net->bw_out_utilized > net->bw_out_available) 1062 if (net->bw_out_utilized > net->bw_out_available)
1063 {
1064 over_out = net->bw_out_utilized - net->bw_out_available;
1065 if (RIL_ACTION_BW_OUT_INC == agent->a_old)
1024 { 1066 {
1025 over_out = net->bw_out_utilized - net->bw_out_available; 1067 /* increase quadratically */
1026 if (RIL_ACTION_BW_OUT_INC == agent->a_old) 1068 over_out *= over_out;
1027 {
1028 /* increase quadratically */
1029 over_out *= over_out;
1030 }
1031 } 1069 }
1070 }
1032 over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW); 1071 over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
1033 1072
1034 return -1.0 * (double)over_max; 1073 return -1.0 * (double) over_max;
1035} 1074}
1036 1075
1037/** 1076/**
@@ -1044,7 +1083,7 @@ envi_get_penalty(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1044 * @return the reward 1083 * @return the reward
1045 */ 1084 */
1046static double 1085static double
1047envi_get_reward(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent) 1086envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1048{ 1087{
1049 struct RIL_Scope *net; 1088 struct RIL_Scope *net;
1050 double objective; 1089 double objective;
@@ -1055,19 +1094,19 @@ envi_get_reward(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1055 1094
1056 net = agent->address_inuse->solver_information; 1095 net = agent->address_inuse->solver_information;
1057 1096
1058 penalty = envi_get_penalty(solver, agent); 1097 penalty = envi_get_penalty (solver, agent);
1059 objective = (agent_get_utility(agent) + net->social_welfare) / 2; 1098 objective = (agent_get_utility (agent) + net->social_welfare) / 2;
1060 delta = objective - agent->objective_old; 1099 delta = objective - agent->objective_old;
1061 agent->objective_old = objective; 1100 agent->objective_old = objective;
1062 1101
1063 if (delta != 0 && penalty == 0) 1102 if ((delta != 0)&&(penalty == 0))
1064 { 1103 {
1065 agent->nop_bonus = delta * RIL_NOP_DECAY; 1104 agent->nop_bonus = delta * RIL_NOP_DECAY;
1066 } 1105 }
1067 else 1106 else
1068 { 1107 {
1069 agent->nop_bonus *= RIL_NOP_DECAY; 1108 agent->nop_bonus *= RIL_NOP_DECAY;
1070 } 1109 }
1071 1110
1072 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0; 1111 steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
1073 1112
@@ -1083,31 +1122,34 @@ envi_get_reward(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1083 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth 1122 * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
1084 */ 1123 */
1085static void 1124static void
1086envi_action_bw_double(struct GAS_RIL_Handle *solver, 1125envi_action_bw_double (struct GAS_RIL_Handle *solver,
1087 struct RIL_Peer_Agent *agent, 1126 struct RIL_Peer_Agent *agent,
1088 int direction_in) 1127 int direction_in)
1089{ 1128{
1090 unsigned long long new_bw; 1129 unsigned long long new_bw;
1091 unsigned long long max_bw; 1130 unsigned long long max_bw;
1092 1131
1093 max_bw = ril_get_max_bw((struct RIL_Scope *)agent->address_inuse->solver_information); 1132 max_bw = ril_get_max_bw ((struct
1133 RIL_Scope *) agent->address_inuse->
1134 solver_information);
1094 1135
1095 if (direction_in) 1136 if (direction_in)
1096 { 1137 {
1097 new_bw = agent->bw_in * 2; 1138 new_bw = agent->bw_in * 2;
1098 if (new_bw < agent->bw_in || new_bw > max_bw) 1139 if ((new_bw < agent->bw_in) ||(new_bw > max_bw) )
1099 new_bw = max_bw; 1140 new_bw = max_bw;
1100 envi_set_active_suggestion(solver, agent, agent->address_inuse, new_bw, 1141 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1101 agent->bw_out, GNUNET_NO); 1142 agent->bw_out, GNUNET_NO);
1102 } 1143 }
1103 else 1144 else
1104 { 1145 {
1105 new_bw = agent->bw_out * 2; 1146 new_bw = agent->bw_out * 2;
1106 if (new_bw < agent->bw_out || new_bw > max_bw) 1147 if ((new_bw < agent->bw_out) ||(new_bw > max_bw) )
1107 new_bw = max_bw; 1148 new_bw = max_bw;
1108 envi_set_active_suggestion(solver, agent, agent->address_inuse, agent->bw_in, 1149 envi_set_active_suggestion (solver, agent, agent->address_inuse,
1109 new_bw, GNUNET_NO); 1150 agent->bw_in,
1110 } 1151 new_bw, GNUNET_NO);
1152 }
1111} 1153}
1112 1154
1113/** 1155/**
@@ -1120,28 +1162,30 @@ envi_action_bw_double(struct GAS_RIL_Handle *solver,
1120 * bandwidth 1162 * bandwidth
1121 */ 1163 */
1122static void 1164static void
1123envi_action_bw_halven(struct GAS_RIL_Handle *solver, 1165envi_action_bw_halven (struct GAS_RIL_Handle *solver,
1124 struct RIL_Peer_Agent *agent, 1166 struct RIL_Peer_Agent *agent,
1125 int direction_in) 1167 int direction_in)
1126{ 1168{
1127 unsigned long long new_bw; 1169 unsigned long long new_bw;
1128 1170
1129 if (direction_in) 1171 if (direction_in)
1130 { 1172 {
1131 new_bw = agent->bw_in / 2; 1173 new_bw = agent->bw_in / 2;
1132 if (new_bw <= 0 || new_bw > agent->bw_in) 1174 if ((new_bw <= 0) ||(new_bw > agent->bw_in) )
1133 new_bw = 0; 1175 new_bw = 0;
1134 envi_set_active_suggestion(solver, agent, agent->address_inuse, new_bw, agent->bw_out, 1176 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1135 GNUNET_NO); 1177 agent->bw_out,
1136 } 1178 GNUNET_NO);
1179 }
1137 else 1180 else
1138 { 1181 {
1139 new_bw = agent->bw_out / 2; 1182 new_bw = agent->bw_out / 2;
1140 if (new_bw <= 0 || new_bw > agent->bw_out) 1183 if ((new_bw <= 0) ||(new_bw > agent->bw_out) )
1141 new_bw = 0; 1184 new_bw = 0;
1142 envi_set_active_suggestion(solver, agent, agent->address_inuse, agent->bw_in, new_bw, 1185 envi_set_active_suggestion (solver, agent, agent->address_inuse,
1143 GNUNET_NO); 1186 agent->bw_in, new_bw,
1144 } 1187 GNUNET_NO);
1188 }
1145} 1189}
1146 1190
1147/** 1191/**
@@ -1153,29 +1197,33 @@ envi_action_bw_halven(struct GAS_RIL_Handle *solver,
1153 * bandwidth 1197 * bandwidth
1154 */ 1198 */
1155static void 1199static void
1156envi_action_bw_inc(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in) 1200envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
1201 int direction_in)
1157{ 1202{
1158 unsigned long long new_bw; 1203 unsigned long long new_bw;
1159 unsigned long long max_bw; 1204 unsigned long long max_bw;
1160 1205
1161 max_bw = ril_get_max_bw((struct RIL_Scope *)agent->address_inuse->solver_information); 1206 max_bw = ril_get_max_bw ((struct
1207 RIL_Scope *) agent->address_inuse->
1208 solver_information);
1162 1209
1163 if (direction_in) 1210 if (direction_in)
1164 { 1211 {
1165 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW); 1212 new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1166 if (new_bw < agent->bw_in || new_bw > max_bw) 1213 if ((new_bw < agent->bw_in) ||(new_bw > max_bw) )
1167 new_bw = max_bw; 1214 new_bw = max_bw;
1168 envi_set_active_suggestion(solver, agent, agent->address_inuse, new_bw, 1215 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1169 agent->bw_out, GNUNET_NO); 1216 agent->bw_out, GNUNET_NO);
1170 } 1217 }
1171 else 1218 else
1172 { 1219 {
1173 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW); 1220 new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1174 if (new_bw < agent->bw_out || new_bw > max_bw) 1221 if ((new_bw < agent->bw_out) ||(new_bw > max_bw) )
1175 new_bw = max_bw; 1222 new_bw = max_bw;
1176 envi_set_active_suggestion(solver, agent, agent->address_inuse, agent->bw_in, 1223 envi_set_active_suggestion (solver, agent, agent->address_inuse,
1177 new_bw, GNUNET_NO); 1224 agent->bw_in,
1178 } 1225 new_bw, GNUNET_NO);
1226 }
1179} 1227}
1180 1228
1181/** 1229/**
@@ -1188,26 +1236,29 @@ envi_action_bw_inc(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
1188 * bandwidth 1236 * bandwidth
1189 */ 1237 */
1190static void 1238static void
1191envi_action_bw_dec(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in) 1239envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
1240 int direction_in)
1192{ 1241{
1193 unsigned long long new_bw; 1242 unsigned long long new_bw;
1194 1243
1195 if (direction_in) 1244 if (direction_in)
1196 { 1245 {
1197 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW); 1246 new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1198 if (new_bw <= 0 || new_bw > agent->bw_in) 1247 if ((new_bw <= 0) ||(new_bw > agent->bw_in) )
1199 new_bw = 0; 1248 new_bw = 0;
1200 envi_set_active_suggestion(solver, agent, agent->address_inuse, new_bw, agent->bw_out, 1249 envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
1201 GNUNET_NO); 1250 agent->bw_out,
1202 } 1251 GNUNET_NO);
1252 }
1203 else 1253 else
1204 { 1254 {
1205 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW); 1255 new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
1206 if (new_bw <= 0 || new_bw > agent->bw_out) 1256 if ((new_bw <= 0) ||(new_bw > agent->bw_out) )
1207 new_bw = 0; 1257 new_bw = 0;
1208 envi_set_active_suggestion(solver, agent, agent->address_inuse, agent->bw_in, new_bw, 1258 envi_set_active_suggestion (solver, agent, agent->address_inuse,
1209 GNUNET_NO); 1259 agent->bw_in, new_bw,
1210 } 1260 GNUNET_NO);
1261 }
1211} 1262}
1212 1263
1213/** 1264/**
@@ -1218,29 +1269,30 @@ envi_action_bw_dec(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent,
1218 * @param address_index index of the address as it is saved in the agent's list, starting with zero 1269 * @param address_index index of the address as it is saved in the agent's list, starting with zero
1219 */ 1270 */
1220static void 1271static void
1221envi_action_address_switch(struct GAS_RIL_Handle *solver, 1272envi_action_address_switch (struct GAS_RIL_Handle *solver,
1222 struct RIL_Peer_Agent *agent, 1273 struct RIL_Peer_Agent *agent,
1223 unsigned int address_index) 1274 unsigned int address_index)
1224{ 1275{
1225 struct RIL_Address_Wrapped *cur; 1276 struct RIL_Address_Wrapped *cur;
1226 int i = 0; 1277 int i = 0;
1227 1278
1228 //cur = agent_address_get_wrapped(agent, agent->address_inuse); 1279 // cur = agent_address_get_wrapped(agent, agent->address_inuse);
1229 1280
1230 for (cur = agent->addresses_head; NULL != cur; cur = cur->next) 1281 for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
1282 {
1283 if (i == address_index)
1231 { 1284 {
1232 if (i == address_index) 1285 envi_set_active_suggestion (solver, agent, cur->address_naked,
1233 { 1286 agent->bw_in, agent->bw_out,
1234 envi_set_active_suggestion(solver, agent, cur->address_naked, agent->bw_in, agent->bw_out, 1287 GNUNET_NO);
1235 GNUNET_NO); 1288 return;
1236 return;
1237 }
1238
1239 i++;
1240 } 1289 }
1241 1290
1242 //no address with address_index exists, in this case this action should not be callable 1291 i++;
1243 GNUNET_assert(GNUNET_NO); 1292 }
1293
1294 // no address with address_index exists, in this case this action should not be callable
1295 GNUNET_assert (GNUNET_NO);
1244} 1296}
1245 1297
1246/** 1298/**
@@ -1251,62 +1303,65 @@ envi_action_address_switch(struct GAS_RIL_Handle *solver,
1251 * @param action the action to perform by the solver 1303 * @param action the action to perform by the solver
1252 */ 1304 */
1253static void 1305static void
1254envi_do_action(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action) 1306envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int
1307 action)
1255{ 1308{
1256 int address_index; 1309 int address_index;
1257 1310
1258 switch (action) 1311 switch (action)
1259 { 1312 {
1260 case RIL_ACTION_NOTHING: 1313 case RIL_ACTION_NOTHING:
1261 break; 1314 break;
1262 1315
1263 case RIL_ACTION_BW_IN_DBL: 1316 case RIL_ACTION_BW_IN_DBL:
1264 envi_action_bw_double(solver, agent, GNUNET_YES); 1317 envi_action_bw_double (solver, agent, GNUNET_YES);
1265 break; 1318 break;
1266 1319
1267 case RIL_ACTION_BW_IN_HLV: 1320 case RIL_ACTION_BW_IN_HLV:
1268 envi_action_bw_halven(solver, agent, GNUNET_YES); 1321 envi_action_bw_halven (solver, agent, GNUNET_YES);
1269 break; 1322 break;
1270 1323
1271 case RIL_ACTION_BW_IN_INC: 1324 case RIL_ACTION_BW_IN_INC:
1272 envi_action_bw_inc(solver, agent, GNUNET_YES); 1325 envi_action_bw_inc (solver, agent, GNUNET_YES);
1273 break; 1326 break;
1274 1327
1275 case RIL_ACTION_BW_IN_DEC: 1328 case RIL_ACTION_BW_IN_DEC:
1276 envi_action_bw_dec(solver, agent, GNUNET_YES); 1329 envi_action_bw_dec (solver, agent, GNUNET_YES);
1277 break; 1330 break;
1278 1331
1279 case RIL_ACTION_BW_OUT_DBL: 1332 case RIL_ACTION_BW_OUT_DBL:
1280 envi_action_bw_double(solver, agent, GNUNET_NO); 1333 envi_action_bw_double (solver, agent, GNUNET_NO);
1281 break; 1334 break;
1282 1335
1283 case RIL_ACTION_BW_OUT_HLV: 1336 case RIL_ACTION_BW_OUT_HLV:
1284 envi_action_bw_halven(solver, agent, GNUNET_NO); 1337 envi_action_bw_halven (solver, agent, GNUNET_NO);
1285 break; 1338 break;
1286 1339
1287 case RIL_ACTION_BW_OUT_INC: 1340 case RIL_ACTION_BW_OUT_INC:
1288 envi_action_bw_inc(solver, agent, GNUNET_NO); 1341 envi_action_bw_inc (solver, agent, GNUNET_NO);
1289 break; 1342 break;
1290 1343
1291 case RIL_ACTION_BW_OUT_DEC: 1344 case RIL_ACTION_BW_OUT_DEC:
1292 envi_action_bw_dec(solver, agent, GNUNET_NO); 1345 envi_action_bw_dec (solver, agent, GNUNET_NO);
1293 break; 1346 break;
1294 1347
1295 default: 1348 default:
1296 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action 1349 if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) // switch address action
1297 { 1350 {
1298 address_index = action - RIL_ACTION_TYPE_NUM; 1351 address_index = action - RIL_ACTION_TYPE_NUM;
1299 1352
1300 GNUNET_assert(address_index >= 0); 1353 GNUNET_assert (address_index >= 0);
1301 GNUNET_assert( 1354 GNUNET_assert (
1302 address_index <= agent_address_get_index(agent, agent->addresses_tail->address_naked)); 1355 address_index <= agent_address_get_index (agent,
1356 agent->addresses_tail->
1357 address_naked));
1303 1358
1304 envi_action_address_switch(solver, agent, address_index); 1359 envi_action_address_switch (solver, agent, address_index);
1305 break; 1360 break;
1306 }
1307 // error - action does not exist
1308 GNUNET_assert(GNUNET_NO);
1309 } 1361 }
1362 // error - action does not exist
1363 GNUNET_assert (GNUNET_NO);
1364 }
1310} 1365}
1311 1366
1312/** 1367/**
@@ -1321,27 +1376,28 @@ envi_do_action(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int
1321 * @return the action index 1376 * @return the action index
1322 */ 1377 */
1323static int 1378static int
1324agent_select_egreedy(struct RIL_Peer_Agent *agent, double *state) 1379agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
1325{ 1380{
1326 int action; 1381 int action;
1327 double r = (double)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 1382 double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1328 UINT32_MAX) / (double)UINT32_MAX; 1383 UINT32_MAX)
1329 1384 / (double) UINT32_MAX;
1330 if (r < agent->envi->parameters.epsilon) //explore 1385
1331 { 1386 if (r < agent->envi->parameters.epsilon) // explore
1332 action = agent_get_action_random(agent); 1387 {
1333 if (RIL_ALGO_Q == agent->envi->parameters.algorithm) 1388 action = agent_get_action_random (agent);
1334 { 1389 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1335 agent->eligibility_reset = GNUNET_YES; 1390 {
1336 } 1391 agent->eligibility_reset = GNUNET_YES;
1337 agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay; 1392 }
1338 return action; 1393 agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
1339 } 1394 return action;
1340 else //exploit 1395 }
1341 { 1396 else // exploit
1342 action = agent_get_action_max(agent, state); 1397 {
1343 return action; 1398 action = agent_get_action_max (agent, state);
1344 } 1399 return action;
1400 }
1345} 1401}
1346 1402
1347/** 1403/**
@@ -1355,7 +1411,7 @@ agent_select_egreedy(struct RIL_Peer_Agent *agent, double *state)
1355 * @return 1411 * @return
1356 */ 1412 */
1357static int 1413static int
1358agent_select_softmax(struct RIL_Peer_Agent *agent, double *state) 1414agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
1359{ 1415{
1360 int i; 1416 int i;
1361 int a_max; 1417 int a_max;
@@ -1364,47 +1420,49 @@ agent_select_softmax(struct RIL_Peer_Agent *agent, double *state)
1364 double sum = 0; 1420 double sum = 0;
1365 double r; 1421 double r;
1366 1422
1367 a_max = agent_get_action_max(agent, state); 1423 a_max = agent_get_action_max (agent, state);
1368 1424
1369 for (i = 0; i < agent->n; i++) 1425 for (i = 0; i < agent->n; i++)
1426 {
1427 if (agent_action_is_possible (agent, i))
1370 { 1428 {
1371 if (agent_action_is_possible(agent, i)) 1429 eqt[i] = exp (agent_q (agent, state, i)
1372 { 1430 / agent->envi->parameters.temperature);
1373 eqt[i] = exp(agent_q(agent, state, i) / agent->envi->parameters.temperature); 1431 if (isinf (eqt[i]))
1374 if (isinf(eqt[i])) 1432 eqt[i] = isinf (eqt[i]) * UINT32_MAX;
1375 eqt[i] = isinf(eqt[i]) * UINT32_MAX; 1433 sum += eqt[i];
1376 sum += eqt[i];
1377 }
1378 } 1434 }
1435 }
1379 for (i = 0; i < agent->n; i++) 1436 for (i = 0; i < agent->n; i++)
1437 {
1438 if (agent_action_is_possible (agent, i))
1380 { 1439 {
1381 if (agent_action_is_possible(agent, i)) 1440 p[i] = eqt[i] / sum;
1382 { 1441 }
1383 p[i] = eqt[i] / sum; 1442 else
1384 } 1443 {
1385 else 1444 p[i] = 0;
1386 {
1387 p[i] = 0;
1388 }
1389 } 1445 }
1390 r = (double)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 1446 }
1391 UINT32_MAX) / (double)UINT32_MAX; 1447 r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1448 UINT32_MAX) / (double) UINT32_MAX;
1392 sum = 0; 1449 sum = 0;
1393 for (i = 0; i < agent->n; i++) 1450 for (i = 0; i < agent->n; i++)
1451 {
1452 if (sum + p[i] > r)
1394 { 1453 {
1395 if (sum + p[i] > r) 1454 if (i != a_max)
1396 { 1455 {
1397 if (i != a_max) 1456 if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
1398 { 1457 agent->eligibility_reset = GNUNET_YES;
1399 if (RIL_ALGO_Q == agent->envi->parameters.algorithm) 1458 agent->envi->parameters.temperature *=
1400 agent->eligibility_reset = GNUNET_YES; 1459 agent->envi->parameters.temperature_decay;
1401 agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay; 1460 }
1402 } 1461 return i;
1403 return i; 1462 }
1404 } 1463 sum += p[i];
1405 sum += p[i]; 1464 }
1406 } 1465 GNUNET_assert (GNUNET_NO);
1407 GNUNET_assert(GNUNET_NO);
1408 return RIL_ACTION_INVALID; 1466 return RIL_ACTION_INVALID;
1409} 1467}
1410 1468
@@ -1417,16 +1475,16 @@ agent_select_softmax(struct RIL_Peer_Agent *agent, double *state)
1417 * @return the action index 1475 * @return the action index
1418 */ 1476 */
1419static int 1477static int
1420agent_select_action(struct RIL_Peer_Agent *agent, double *state) 1478agent_select_action (struct RIL_Peer_Agent *agent, double *state)
1421{ 1479{
1422 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY) 1480 if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
1423 { 1481 {
1424 return agent_select_egreedy(agent, state); 1482 return agent_select_egreedy (agent, state);
1425 } 1483 }
1426 else 1484 else
1427 { 1485 {
1428 return agent_select_softmax(agent, state); 1486 return agent_select_softmax (agent, state);
1429 } 1487 }
1430} 1488}
1431 1489
1432/** 1490/**
@@ -1438,68 +1496,71 @@ agent_select_action(struct RIL_Peer_Agent *agent, double *state)
1438 * @param agent the agent performing the step 1496 * @param agent the agent performing the step
1439 */ 1497 */
1440static void 1498static void
1441agent_step(struct RIL_Peer_Agent *agent) 1499agent_step (struct RIL_Peer_Agent *agent)
1442{ 1500{
1443 int a_next = RIL_ACTION_INVALID; 1501 int a_next = RIL_ACTION_INVALID;
1444 int a_max; 1502 int a_max;
1445 double *s_next; 1503 double *s_next;
1446 double reward; 1504 double reward;
1447 1505
1448 LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n", 1506 LOG (GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
1449 GNUNET_i2s(&agent->peer), 1507 GNUNET_i2s (&agent->peer),
1450 agent->envi->parameters.algorithm ? "Q" : "SARSA"); 1508 agent->envi->parameters.algorithm ? "Q" : "SARSA");
1451 1509
1452 s_next = envi_get_state(agent->envi, agent); 1510 s_next = envi_get_state (agent->envi, agent);
1453 reward = envi_get_reward(agent->envi, agent); 1511 reward = envi_get_reward (agent->envi, agent);
1454 1512
1455 if (agent->eligibility_reset) 1513 if (agent->eligibility_reset)
1456 { 1514 {
1457 agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1); 1515 agent_modify_eligibility (agent, RIL_E_ZERO, NULL, -1);
1458 agent->eligibility_reset = GNUNET_NO; 1516 agent->eligibility_reset = GNUNET_NO;
1459 } 1517 }
1460 else 1518 else
1461 { 1519 {
1462 agent_modify_eligibility(agent, RIL_E_DECAY, NULL, -1); 1520 agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
1463 } 1521 }
1464 if (RIL_ACTION_INVALID != agent->a_old) 1522 if (RIL_ACTION_INVALID != agent->a_old)
1465 { 1523 {
1466 agent_modify_eligibility(agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old); 1524 agent_modify_eligibility (agent,
1467 } 1525 agent->envi->parameters.eligibility_trace_mode,
1526 agent->s_old, agent->a_old);
1527 }
1468 1528
1469 switch (agent->envi->parameters.algorithm) 1529 switch (agent->envi->parameters.algorithm)
1530 {
1531 case RIL_ALGO_SARSA:
1532 a_next = agent_select_action (agent, s_next);
1533 if (RIL_ACTION_INVALID != agent->a_old)
1470 { 1534 {
1471 case RIL_ALGO_SARSA: 1535 // updates weights with selected action (on-policy), if not first step
1472 a_next = agent_select_action(agent, s_next); 1536 agent_update (agent, reward, s_next, a_next);
1473 if (RIL_ACTION_INVALID != agent->a_old) 1537 }
1474 { 1538 break;
1475 //updates weights with selected action (on-policy), if not first step
1476 agent_update(agent, reward, s_next, a_next);
1477 }
1478 break;
1479 1539
1480 case RIL_ALGO_Q: 1540 case RIL_ALGO_Q:
1481 a_max = agent_get_action_max(agent, s_next); 1541 a_max = agent_get_action_max (agent, s_next);
1482 if (RIL_ACTION_INVALID != agent->a_old) 1542 if (RIL_ACTION_INVALID != agent->a_old)
1483 { 1543 {
1484 //updates weights with best action, disregarding actually selected action (off-policy), if not first step 1544 // updates weights with best action, disregarding actually selected action (off-policy), if not first step
1485 agent_update(agent, reward, s_next, a_max); 1545 agent_update (agent, reward, s_next, a_max);
1486 }
1487 a_next = agent_select_action(agent, s_next);
1488 break;
1489 } 1546 }
1547 a_next = agent_select_action (agent, s_next);
1548 break;
1549 }
1490 1550
1491 GNUNET_assert(RIL_ACTION_INVALID != a_next); 1551 GNUNET_assert (RIL_ACTION_INVALID != a_next);
1492 1552
1493 LOG(GNUNET_ERROR_TYPE_DEBUG, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n", 1553 LOG (GNUNET_ERROR_TYPE_DEBUG,
1494 agent->step_count, 1554 "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
1495 reward, 1555 agent->step_count,
1496 agent->bw_in / 1024, 1556 reward,
1497 agent->bw_out / 1024, 1557 agent->bw_in / 1024,
1498 a_next); 1558 agent->bw_out / 1024,
1559 a_next);
1499 1560
1500 envi_do_action(agent->envi, agent, a_next); 1561 envi_do_action (agent->envi, agent, a_next);
1501 1562
1502 GNUNET_free(agent->s_old); 1563 GNUNET_free (agent->s_old);
1503 agent->s_old = s_next; 1564 agent->s_old = s_next;
1504 agent->a_old = a_next; 1565 agent->a_old = a_next;
1505 1566
@@ -1512,7 +1573,7 @@ agent_step(struct RIL_Peer_Agent *agent)
1512 * @param solver the solver handle 1573 * @param solver the solver handle
1513 */ 1574 */
1514static void 1575static void
1515ril_step(struct GAS_RIL_Handle *solver); 1576ril_step (struct GAS_RIL_Handle *solver);
1516 1577
1517 1578
1518/** 1579/**
@@ -1522,12 +1583,12 @@ ril_step(struct GAS_RIL_Handle *solver);
1522 * @param cls the solver handle 1583 * @param cls the solver handle
1523 */ 1584 */
1524static void 1585static void
1525ril_step_scheduler_task(void *cls) 1586ril_step_scheduler_task (void *cls)
1526{ 1587{
1527 struct GAS_RIL_Handle *solver = cls; 1588 struct GAS_RIL_Handle *solver = cls;
1528 1589
1529 solver->step_next_task_id = NULL; 1590 solver->step_next_task_id = NULL;
1530 ril_step(solver); 1591 ril_step (solver);
1531} 1592}
1532 1593
1533/** 1594/**
@@ -1539,7 +1600,7 @@ ril_step_scheduler_task(void *cls)
1539 * @return the ratio 1600 * @return the ratio
1540 */ 1601 */
1541static double 1602static double
1542ril_get_used_resource_ratio(struct GAS_RIL_Handle *solver) 1603ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
1543{ 1604{
1544 int i; 1605 int i;
1545 struct RIL_Scope net; 1606 struct RIL_Scope net;
@@ -1548,26 +1609,26 @@ ril_get_used_resource_ratio(struct GAS_RIL_Handle *solver)
1548 double ratio; 1609 double ratio;
1549 1610
1550 for (i = 0; i < solver->networks_count; i++) 1611 for (i = 0; i < solver->networks_count; i++)
1612 {
1613 net = solver->network_entries[i];
1614 if (net.bw_in_assigned > 0) // only consider scopes where an address is actually active
1551 { 1615 {
1552 net = solver->network_entries[i]; 1616 sum_assigned += net.bw_in_utilized;
1553 if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active 1617 sum_assigned += net.bw_out_utilized;
1554 { 1618 sum_available += net.bw_in_available;
1555 sum_assigned += net.bw_in_utilized; 1619 sum_available += net.bw_out_available;
1556 sum_assigned += net.bw_out_utilized;
1557 sum_available += net.bw_in_available;
1558 sum_available += net.bw_out_available;
1559 }
1560 } 1620 }
1621 }
1561 if (sum_available > 0) 1622 if (sum_available > 0)
1562 { 1623 {
1563 ratio = ((double)sum_assigned) / ((double)sum_available); 1624 ratio = ((double) sum_assigned) / ((double) sum_available);
1564 } 1625 }
1565 else 1626 else
1566 { 1627 {
1567 ratio = 0; 1628 ratio = 0;
1568 } 1629 }
1569 1630
1570 return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1 1631 return ratio > 1 ? 1 : ratio; // overutilization is possible, cap at 1
1571} 1632}
1572 1633
1573/** 1634/**
@@ -1578,17 +1639,17 @@ ril_get_used_resource_ratio(struct GAS_RIL_Handle *solver)
1578 * @return the network struct 1639 * @return the network struct
1579 */ 1640 */
1580static struct RIL_Scope * 1641static struct RIL_Scope *
1581ril_get_network(struct GAS_RIL_Handle *s, uint32_t type) 1642ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
1582{ 1643{
1583 int i; 1644 int i;
1584 1645
1585 for (i = 0; i < s->networks_count; i++) 1646 for (i = 0; i < s->networks_count; i++)
1647 {
1648 if (s->network_entries[i].type == type)
1586 { 1649 {
1587 if (s->network_entries[i].type == type) 1650 return &s->network_entries[i];
1588 {
1589 return &s->network_entries[i];
1590 }
1591 } 1651 }
1652 }
1592 return NULL; 1653 return NULL;
1593} 1654}
1594 1655
@@ -1602,26 +1663,28 @@ ril_get_network(struct GAS_RIL_Handle *s, uint32_t type)
1602 * @return GNUNET_YES if there are theoretically enough resources left 1663 * @return GNUNET_YES if there are theoretically enough resources left
1603 */ 1664 */
1604static int 1665static int
1605ril_network_is_not_full(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType network) 1666ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType
1667 network)
1606{ 1668{
1607 struct RIL_Scope *net; 1669 struct RIL_Scope *net;
1608 struct RIL_Peer_Agent *agent; 1670 struct RIL_Peer_Agent *agent;
1609 unsigned long long address_count = 0; 1671 unsigned long long address_count = 0;
1610 1672
1611 for (agent = solver->agents_head; NULL != agent; agent = agent->next) 1673 for (agent = solver->agents_head; NULL != agent; agent = agent->next)
1674 {
1675 if (agent->address_inuse && agent->is_active)
1612 { 1676 {
1613 if (agent->address_inuse && agent->is_active) 1677 net = agent->address_inuse->solver_information;
1614 { 1678 if (net->type == network)
1615 net = agent->address_inuse->solver_information; 1679 {
1616 if (net->type == network) 1680 address_count++;
1617 { 1681 }
1618 address_count++;
1619 }
1620 }
1621 } 1682 }
1683 }
1622 1684
1623 net = ril_get_network(solver, network); 1685 net = ril_get_network (solver, network);
1624 return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count); 1686 return (net->bw_in_available > RIL_MIN_BW * address_count) &&
1687 (net->bw_out_available > RIL_MIN_BW * address_count);
1625} 1688}
1626 1689
1627/** 1690/**
@@ -1635,27 +1698,34 @@ ril_network_is_not_full(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType n
1635 * @param silent 1698 * @param silent
1636 */ 1699 */
1637static void 1700static void
1638ril_try_unblock_agent(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent) 1701ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct
1702 RIL_Peer_Agent *agent, int silent)
1639{ 1703{
1640 struct RIL_Address_Wrapped *addr_wrap; 1704 struct RIL_Address_Wrapped *addr_wrap;
1641 struct RIL_Scope *net; 1705 struct RIL_Scope *net;
1642 unsigned long long start_in; 1706 unsigned long long start_in;
1643 unsigned long long start_out; 1707 unsigned long long start_out;
1644 1708
1645 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next) 1709 for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap =
1646 { 1710 addr_wrap->next)
1647 net = addr_wrap->address_naked->solver_information; 1711 {
1648 if (ril_network_is_not_full(solver, net->type)) 1712 net = addr_wrap->address_naked->solver_information;
1649 { 1713 if (ril_network_is_not_full (solver, net->type))
1650 if (NULL == agent->address_inuse) 1714 {
1651 { 1715 if (NULL == agent->address_inuse)
1652 start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW; 1716 {
1653 start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW; 1717 start_in = net->bw_in_available < net->bw_in_utilized ?
1654 envi_set_active_suggestion(solver, agent, addr_wrap->address_naked, start_in, start_out, silent); 1718 (net->bw_in_available - net->bw_in_utilized) / 2 :
1655 } 1719 RIL_MIN_BW;
1656 return; 1720 start_out = net->bw_out_available < net->bw_out_utilized ?
1657 } 1721 (net->bw_out_available - net->bw_out_utilized) / 2 :
1722 RIL_MIN_BW;
1723 envi_set_active_suggestion (solver, agent, addr_wrap->address_naked,
1724 start_in, start_out, silent);
1725 }
1726 return;
1658 } 1727 }
1728 }
1659 agent->address_inuse = NULL; 1729 agent->address_inuse = NULL;
1660} 1730}
1661 1731
@@ -1666,7 +1736,7 @@ ril_try_unblock_agent(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agen
1666 * @param solver the solver handle 1736 * @param solver the solver handle
1667 */ 1737 */
1668static void 1738static void
1669ril_calculate_discount(struct GAS_RIL_Handle *solver) 1739ril_calculate_discount (struct GAS_RIL_Handle *solver)
1670{ 1740{
1671 struct GNUNET_TIME_Absolute time_now; 1741 struct GNUNET_TIME_Absolute time_now;
1672 struct GNUNET_TIME_Relative time_delta; 1742 struct GNUNET_TIME_Relative time_delta;
@@ -1674,25 +1744,28 @@ ril_calculate_discount(struct GAS_RIL_Handle *solver)
1674 1744
1675 // MDP case only for debugging purposes 1745 // MDP case only for debugging purposes
1676 if (solver->simulate) 1746 if (solver->simulate)
1677 { 1747 {
1678 solver->global_discount_variable = solver->parameters.gamma; 1748 solver->global_discount_variable = solver->parameters.gamma;
1679 solver->global_discount_integrated = 1; 1749 solver->global_discount_integrated = 1;
1680 return; 1750 return;
1681 } 1751 }
1682 1752
1683 // semi-MDP case 1753 // semi-MDP case
1684 1754
1685 //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step 1755 // calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
1686 time_now = GNUNET_TIME_absolute_get(); 1756 time_now = GNUNET_TIME_absolute_get ();
1687 time_delta = GNUNET_TIME_absolute_get_difference(solver->step_time_last, time_now); 1757 time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last,
1758 time_now);
1688 solver->step_time_last = time_now; 1759 solver->step_time_last = time_now;
1689 tau = (double)time_delta.rel_value_us 1760 tau = (double) time_delta.rel_value_us
1690 / (double)solver->parameters.step_time_min.rel_value_us; 1761 / (double) solver->parameters.step_time_min.rel_value_us;
1691 1762
1692 //calculate reward discounts (once per step for all agents) 1763 // calculate reward discounts (once per step for all agents)
1693 solver->global_discount_variable = pow(M_E, ((-1.0) * ((double)solver->parameters.beta) * tau)); 1764 solver->global_discount_variable = pow (M_E, ((-1.0)
1765 * ((double) solver->parameters.
1766 beta) * tau));
1694 solver->global_discount_integrated = (1.0 - solver->global_discount_variable) 1767 solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
1695 / (double)solver->parameters.beta; 1768 / (double) solver->parameters.beta;
1696} 1769}
1697 1770
1698/** 1771/**
@@ -1703,18 +1776,21 @@ ril_calculate_discount(struct GAS_RIL_Handle *solver)
1703 * @return the number of allocated connections 1776 * @return the number of allocated connections
1704 */ 1777 */
1705static int 1778static int
1706ril_network_count_active_agents(struct GAS_RIL_Handle *solver, struct RIL_Scope *scope) 1779ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct
1780 RIL_Scope *scope)
1707{ 1781{
1708 int c = 0; 1782 int c = 0;
1709 struct RIL_Peer_Agent *cur_agent; 1783 struct RIL_Peer_Agent *cur_agent;
1710 1784
1711 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next) 1785 for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent =
1786 cur_agent->next)
1787 {
1788 if (cur_agent->is_active && cur_agent->address_inuse &&
1789 (cur_agent->address_inuse->solver_information == scope))
1712 { 1790 {
1713 if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope)) 1791 c++;
1714 {
1715 c++;
1716 }
1717 } 1792 }
1793 }
1718 return c; 1794 return c;
1719} 1795}
1720 1796
@@ -1729,26 +1805,27 @@ ril_network_count_active_agents(struct GAS_RIL_Handle *solver, struct RIL_Scope
1729 * @return the sum of the assigned bandwidths 1805 * @return the sum of the assigned bandwidths
1730 */ 1806 */
1731static unsigned long long 1807static unsigned long long
1732ril_network_get_assigned(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType type, int direction_in) 1808ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType
1809 type, int direction_in)
1733{ 1810{
1734 struct RIL_Peer_Agent *cur; 1811 struct RIL_Peer_Agent *cur;
1735 struct RIL_Scope *net; 1812 struct RIL_Scope *net;
1736 unsigned long long sum = 0; 1813 unsigned long long sum = 0;
1737 1814
1738 for (cur = solver->agents_head; NULL != cur; cur = cur->next) 1815 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1816 {
1817 if (cur->is_active && cur->address_inuse)
1739 { 1818 {
1740 if (cur->is_active && cur->address_inuse) 1819 net = cur->address_inuse->solver_information;
1741 { 1820 if (net->type == type)
1742 net = cur->address_inuse->solver_information; 1821 {
1743 if (net->type == type) 1822 if (direction_in)
1744 { 1823 sum += cur->bw_in;
1745 if (direction_in) 1824 else
1746 sum += cur->bw_in; 1825 sum += cur->bw_out;
1747 else 1826 }
1748 sum += cur->bw_out;
1749 }
1750 }
1751 } 1827 }
1828 }
1752 1829
1753 return sum; 1830 return sum;
1754} 1831}
@@ -1764,26 +1841,27 @@ ril_network_get_assigned(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType
1764 * @return the sum of the utilized bandwidths (in bytes/second) 1841 * @return the sum of the utilized bandwidths (in bytes/second)
1765 */ 1842 */
1766static unsigned long long 1843static unsigned long long
1767ril_network_get_utilized(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType type, int direction_in) 1844ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType
1845 type, int direction_in)
1768{ 1846{
1769 struct RIL_Peer_Agent *cur; 1847 struct RIL_Peer_Agent *cur;
1770 struct RIL_Scope *net; 1848 struct RIL_Scope *net;
1771 unsigned long long sum = 0; 1849 unsigned long long sum = 0;
1772 1850
1773 for (cur = solver->agents_head; NULL != cur; cur = cur->next) 1851 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1852 {
1853 if (cur->is_active && cur->address_inuse)
1774 { 1854 {
1775 if (cur->is_active && cur->address_inuse) 1855 net = cur->address_inuse->solver_information;
1776 { 1856 if (net->type == type)
1777 net = cur->address_inuse->solver_information; 1857 {
1778 if (net->type == type) 1858 if (direction_in)
1779 { 1859 sum += cur->address_inuse->norm_utilization_in.norm;
1780 if (direction_in) 1860 else
1781 sum += cur->address_inuse->norm_utilization_in.norm; 1861 sum += cur->address_inuse->norm_utilization_out.norm;
1782 else 1862 }
1783 sum += cur->address_inuse->norm_utilization_out.norm;
1784 }
1785 }
1786 } 1863 }
1864 }
1787 1865
1788 return sum; 1866 return sum;
1789} 1867}
@@ -1795,21 +1873,25 @@ ril_network_get_utilized(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType
1795 * @param solver the solver handle 1873 * @param solver the solver handle
1796 */ 1874 */
1797static void 1875static void
1798ril_networks_update_state(struct GAS_RIL_Handle *solver) 1876ril_networks_update_state (struct GAS_RIL_Handle *solver)
1799{ 1877{
1800 int c; 1878 int c;
1801 struct RIL_Scope *net; 1879 struct RIL_Scope *net;
1802 1880
1803 for (c = 0; c < solver->networks_count; c++) 1881 for (c = 0; c < solver->networks_count; c++)
1804 { 1882 {
1805 net = &solver->network_entries[c]; 1883 net = &solver->network_entries[c];
1806 net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES); 1884 net->bw_in_assigned = ril_network_get_assigned (solver, net->type,
1807 net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES); 1885 GNUNET_YES);
1808 net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO); 1886 net->bw_in_utilized = ril_network_get_utilized (solver, net->type,
1809 net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO); 1887 GNUNET_YES);
1810 net->active_agent_count = ril_network_count_active_agents(solver, net); 1888 net->bw_out_assigned = ril_network_get_assigned (solver, net->type,
1811 net->social_welfare = ril_network_get_social_welfare(solver, net); 1889 GNUNET_NO);
1812 } 1890 net->bw_out_utilized = ril_network_get_utilized (solver, net->type,
1891 GNUNET_NO);
1892 net->active_agent_count = ril_network_count_active_agents (solver, net);
1893 net->social_welfare = ril_network_get_social_welfare (solver, net);
1894 }
1813} 1895}
1814 1896
1815/** 1897/**
@@ -1820,7 +1902,7 @@ ril_networks_update_state(struct GAS_RIL_Handle *solver)
1820 * @param solver the solver handle 1902 * @param solver the solver handle
1821 */ 1903 */
1822static void 1904static void
1823ril_step_schedule_next(struct GAS_RIL_Handle *solver) 1905ril_step_schedule_next (struct GAS_RIL_Handle *solver)
1824{ 1906{
1825 double used_ratio; 1907 double used_ratio;
1826 double factor; 1908 double factor;
@@ -1828,21 +1910,24 @@ ril_step_schedule_next(struct GAS_RIL_Handle *solver)
1828 double offset; 1910 double offset;
1829 struct GNUNET_TIME_Relative time_next; 1911 struct GNUNET_TIME_Relative time_next;
1830 1912
1831 used_ratio = ril_get_used_resource_ratio(solver); 1913 used_ratio = ril_get_used_resource_ratio (solver);
1832 1914
1833 GNUNET_assert( 1915 GNUNET_assert (
1834 solver->parameters.step_time_min.rel_value_us 1916 solver->parameters.step_time_min.rel_value_us
1835 <= solver->parameters.step_time_max.rel_value_us); 1917 <= solver->parameters.step_time_max.rel_value_us);
1836 1918
1837 factor = (double)GNUNET_TIME_relative_subtract(solver->parameters.step_time_max, 1919 factor = (double) GNUNET_TIME_relative_subtract (
1838 solver->parameters.step_time_min).rel_value_us; 1920 solver->parameters.step_time_max,
1839 offset = (double)solver->parameters.step_time_min.rel_value_us; 1921 solver->parameters.
1840 y = factor * pow(used_ratio, RIL_INTERVAL_EXPONENT) + offset; 1922 step_time_min).rel_value_us;
1923 offset = (double) solver->parameters.step_time_min.rel_value_us;
1924 y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
1841 1925
1842 GNUNET_assert(y <= (double)solver->parameters.step_time_max.rel_value_us); 1926 GNUNET_assert (y <= (double) solver->parameters.step_time_max.rel_value_us);
1843 GNUNET_assert(y >= (double)solver->parameters.step_time_min.rel_value_us); 1927 GNUNET_assert (y >= (double) solver->parameters.step_time_min.rel_value_us);
1844 1928
1845 time_next = GNUNET_TIME_relative_saturating_multiply(GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long)y); 1929 time_next = GNUNET_TIME_relative_saturating_multiply (
1930 GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
1846 1931
1847// LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n", 1932// LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
1848// used_ratio, 1933// used_ratio,
@@ -1851,15 +1936,17 @@ ril_step_schedule_next(struct GAS_RIL_Handle *solver)
1851// y); 1936// y);
1852 1937
1853 if (solver->simulate) 1938 if (solver->simulate)
1854 { 1939 {
1855 time_next = GNUNET_TIME_UNIT_ZERO; 1940 time_next = GNUNET_TIME_UNIT_ZERO;
1856 } 1941 }
1857 1942
1858 if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done)) 1943 if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done))
1859 { 1944 {
1860 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed(time_next, &ril_step_scheduler_task, 1945 solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next,
1861 solver); 1946 &
1862 } 1947 ril_step_scheduler_task,
1948 solver);
1949 }
1863} 1950}
1864 1951
1865/** 1952/**
@@ -1868,62 +1955,62 @@ ril_step_schedule_next(struct GAS_RIL_Handle *solver)
1868 * @param solver 1955 * @param solver
1869 */ 1956 */
1870static void 1957static void
1871ril_step(struct GAS_RIL_Handle *solver) 1958ril_step (struct GAS_RIL_Handle *solver)
1872{ 1959{
1873 struct RIL_Peer_Agent *cur; 1960 struct RIL_Peer_Agent *cur;
1874 1961
1875 if (GNUNET_YES == solver->bulk_lock) 1962 if (GNUNET_YES == solver->bulk_lock)
1876 { 1963 {
1877 solver->bulk_changes++; 1964 solver->bulk_changes++;
1878 return; 1965 return;
1879 } 1966 }
1880 1967
1881 ril_inform(solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS); 1968 ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
1882 1969
1883 LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count); 1970 LOG (GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
1884 1971
1885 if (0 == solver->step_count) 1972 if (0 == solver->step_count)
1886 { 1973 {
1887 solver->step_time_last = GNUNET_TIME_absolute_get(); 1974 solver->step_time_last = GNUNET_TIME_absolute_get ();
1888 } 1975 }
1889 1976
1890 ril_calculate_discount(solver); 1977 ril_calculate_discount (solver);
1891 ril_networks_update_state(solver); 1978 ril_networks_update_state (solver);
1892 1979
1893 //trigger one step per active, unblocked agent 1980 // trigger one step per active, unblocked agent
1894 for (cur = solver->agents_head; NULL != cur; cur = cur->next) 1981 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
1982 {
1983 if (cur->is_active)
1895 { 1984 {
1896 if (cur->is_active) 1985 if (NULL == cur->address_inuse)
1897 { 1986 {
1898 if (NULL == cur->address_inuse) 1987 ril_try_unblock_agent (solver, cur, GNUNET_NO);
1899 { 1988 }
1900 ril_try_unblock_agent(solver, cur, GNUNET_NO); 1989 if (cur->address_inuse)
1901 } 1990 {
1902 if (cur->address_inuse) 1991 agent_step (cur);
1903 { 1992 }
1904 agent_step(cur);
1905 }
1906 }
1907 } 1993 }
1994 }
1908 1995
1909 ril_networks_update_state(solver); 1996 ril_networks_update_state (solver);
1910 1997
1911 solver->step_count++; 1998 solver->step_count++;
1912 ril_step_schedule_next(solver); 1999 ril_step_schedule_next (solver);
1913 2000
1914 ril_inform(solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS); 2001 ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
1915 2002
1916 ril_inform(solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS); 2003 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
1917 for (cur = solver->agents_head; NULL != cur; cur = cur->next) 2004 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2005 {
2006 if (cur->suggestion_issue)
1918 { 2007 {
1919 if (cur->suggestion_issue) 2008 solver->env->bandwidth_changed_cb (solver->env->cls,
1920 { 2009 cur->suggestion_address);
1921 solver->env->bandwidth_changed_cb(solver->env->cls, 2010 cur->suggestion_issue = GNUNET_NO;
1922 cur->suggestion_address);
1923 cur->suggestion_issue = GNUNET_NO;
1924 }
1925 } 2011 }
1926 ril_inform(solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS); 2012 }
2013 ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
1927} 2014}
1928 2015
1929/** 2016/**
@@ -1932,18 +2019,23 @@ ril_step(struct GAS_RIL_Handle *solver)
1932 * @param agent The respective agent 2019 * @param agent The respective agent
1933 */ 2020 */
1934static void 2021static void
1935agent_w_init(struct RIL_Peer_Agent *agent) 2022agent_w_init (struct RIL_Peer_Agent *agent)
1936{ 2023{
1937 int i; 2024 int i;
1938 int k; 2025 int k;
1939 2026
1940 for (i = 0; i < agent->n; i++) 2027 for (i = 0; i < agent->n; i++)
2028 {
2029 for (k = 0; k < agent->m; k++)
1941 { 2030 {
1942 for (k = 0; k < agent->m; k++) 2031 agent->W[i][k] = agent->envi->parameters.alpha * (1.0 - 2.0
1943 { 2032 * ((double)
1944 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)); 2033 GNUNET_CRYPTO_random_u32 (
1945 } 2034 GNUNET_CRYPTO_QUALITY_WEAK,
2035 UINT32_MAX)
2036 / (double) UINT32_MAX));
1946 } 2037 }
2038 }
1947} 2039}
1948 2040
1949/** 2041/**
@@ -1954,11 +2046,11 @@ agent_w_init(struct RIL_Peer_Agent *agent)
1954 * @return handle to the new agent 2046 * @return handle to the new agent
1955 */ 2047 */
1956static struct RIL_Peer_Agent * 2048static struct RIL_Peer_Agent *
1957agent_init(void *s, const struct GNUNET_PeerIdentity *peer) 2049agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
1958{ 2050{
1959 int i; 2051 int i;
1960 struct GAS_RIL_Handle * solver = s; 2052 struct GAS_RIL_Handle *solver = s;
1961 struct RIL_Peer_Agent * agent = GNUNET_new(struct RIL_Peer_Agent); 2053 struct RIL_Peer_Agent *agent = GNUNET_new (struct RIL_Peer_Agent);
1962 2054
1963 agent->envi = solver; 2055 agent->envi = solver;
1964 agent->peer = *peer; 2056 agent->peer = *peer;
@@ -1969,17 +2061,17 @@ agent_init(void *s, const struct GNUNET_PeerIdentity *peer)
1969 agent->suggestion_issue = GNUNET_NO; 2061 agent->suggestion_issue = GNUNET_NO;
1970 agent->n = RIL_ACTION_TYPE_NUM; 2062 agent->n = RIL_ACTION_TYPE_NUM;
1971 agent->m = 0; 2063 agent->m = 0;
1972 agent->W = (double **)GNUNET_malloc(sizeof(double *) * agent->n); 2064 agent->W = (double **) GNUNET_malloc (sizeof(double *) * agent->n);
1973 agent->E = (double **)GNUNET_malloc(sizeof(double *) * agent->n); 2065 agent->E = (double **) GNUNET_malloc (sizeof(double *) * agent->n);
1974 for (i = 0; i < agent->n; i++) 2066 for (i = 0; i < agent->n; i++)
1975 { 2067 {
1976 agent->W[i] = (double *)GNUNET_malloc(sizeof(double) * agent->m); 2068 agent->W[i] = (double *) GNUNET_malloc (sizeof(double) * agent->m);
1977 agent->E[i] = (double *)GNUNET_malloc(sizeof(double) * agent->m); 2069 agent->E[i] = (double *) GNUNET_malloc (sizeof(double) * agent->m);
1978 } 2070 }
1979 agent_w_init(agent); 2071 agent_w_init (agent);
1980 agent->eligibility_reset = GNUNET_NO; 2072 agent->eligibility_reset = GNUNET_NO;
1981 agent->a_old = RIL_ACTION_INVALID; 2073 agent->a_old = RIL_ACTION_INVALID;
1982 agent->s_old = GNUNET_malloc(sizeof(double) * agent->m); 2074 agent->s_old = GNUNET_malloc (sizeof(double) * agent->m);
1983 agent->address_inuse = NULL; 2075 agent->address_inuse = NULL;
1984 agent->objective_old = 0; 2076 agent->objective_old = 0;
1985 agent->nop_bonus = 0; 2077 agent->nop_bonus = 0;
@@ -1994,19 +2086,19 @@ agent_init(void *s, const struct GNUNET_PeerIdentity *peer)
1994 * @param agent the agent to retire 2086 * @param agent the agent to retire
1995 */ 2087 */
1996static void 2088static void
1997agent_die(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent) 2089agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
1998{ 2090{
1999 int i; 2091 int i;
2000 2092
2001 for (i = 0; i < agent->n; i++) 2093 for (i = 0; i < agent->n; i++)
2002 { 2094 {
2003 GNUNET_free_non_null(agent->W[i]); 2095 GNUNET_free_non_null (agent->W[i]);
2004 GNUNET_free_non_null(agent->E[i]); 2096 GNUNET_free_non_null (agent->E[i]);
2005 } 2097 }
2006 GNUNET_free_non_null(agent->W); 2098 GNUNET_free_non_null (agent->W);
2007 GNUNET_free_non_null(agent->E); 2099 GNUNET_free_non_null (agent->E);
2008 GNUNET_free_non_null(agent->s_old); 2100 GNUNET_free_non_null (agent->s_old);
2009 GNUNET_free(agent); 2101 GNUNET_free (agent);
2010} 2102}
2011 2103
2012/** 2104/**
@@ -2018,24 +2110,26 @@ agent_die(struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
2018 * @return the agent 2110 * @return the agent
2019 */ 2111 */
2020static struct RIL_Peer_Agent * 2112static struct RIL_Peer_Agent *
2021ril_get_agent(struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create) 2113ril_get_agent (struct GAS_RIL_Handle *solver, const struct
2114 GNUNET_PeerIdentity *peer, int create)
2022{ 2115{
2023 struct RIL_Peer_Agent *cur; 2116 struct RIL_Peer_Agent *cur;
2024 2117
2025 for (cur = solver->agents_head; NULL != cur; cur = cur->next) 2118 for (cur = solver->agents_head; NULL != cur; cur = cur->next)
2119 {
2120 if (0 == GNUNET_memcmp (peer, &cur->peer))
2026 { 2121 {
2027 if (0 == GNUNET_memcmp(peer, &cur->peer)) 2122 return cur;
2028 {
2029 return cur;
2030 }
2031 } 2123 }
2124 }
2032 2125
2033 if (create) 2126 if (create)
2034 { 2127 {
2035 cur = agent_init(solver, peer); 2128 cur = agent_init (solver, peer);
2036 GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur); 2129 GNUNET_CONTAINER_DLL_insert_tail (solver->agents_head, solver->agents_tail,
2037 return cur; 2130 cur);
2038 } 2131 return cur;
2132 }
2039 return NULL; 2133 return NULL;
2040} 2134}
2041 2135
@@ -2048,11 +2142,12 @@ ril_get_agent(struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *p
2048 * @return whether or not the network is considered active 2142 * @return whether or not the network is considered active
2049 */ 2143 */
2050static int 2144static int
2051ril_network_is_active(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType network) 2145ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType
2146 network)
2052{ 2147{
2053 struct RIL_Scope *net; 2148 struct RIL_Scope *net;
2054 2149
2055 net = ril_get_network(solver, network); 2150 net = ril_get_network (solver, network);
2056 return net->bw_out_available >= RIL_MIN_BW; 2151 return net->bw_out_available >= RIL_MIN_BW;
2057} 2152}
2058 2153
@@ -2068,21 +2163,21 @@ ril_network_is_active(struct GAS_RIL_Handle *solver, enum GNUNET_NetworkType net
2068 * @param old_length the length of the old vector 2163 * @param old_length the length of the old vector
2069 */ 2164 */
2070static void 2165static void
2071ril_cut_from_vector(void **old, 2166ril_cut_from_vector (void **old,
2072 size_t element_size, 2167 size_t element_size,
2073 unsigned int hole_start, 2168 unsigned int hole_start,
2074 unsigned int hole_length, 2169 unsigned int hole_length,
2075 unsigned int old_length) 2170 unsigned int old_length)
2076{ 2171{
2077 char *tmpptr; 2172 char *tmpptr;
2078 char *oldptr = (char *)*old; 2173 char *oldptr = (char *) *old;
2079 size_t size; 2174 size_t size;
2080 unsigned int bytes_before; 2175 unsigned int bytes_before;
2081 unsigned int bytes_hole; 2176 unsigned int bytes_hole;
2082 unsigned int bytes_after; 2177 unsigned int bytes_after;
2083 2178
2084 GNUNET_assert(old_length >= hole_length); 2179 GNUNET_assert (old_length >= hole_length);
2085 GNUNET_assert(old_length >= (hole_start + hole_length)); 2180 GNUNET_assert (old_length >= (hole_start + hole_length));
2086 2181
2087 size = element_size * (old_length - hole_length); 2182 size = element_size * (old_length - hole_length);
2088 2183
@@ -2091,20 +2186,21 @@ ril_cut_from_vector(void **old,
2091 bytes_after = element_size * (old_length - hole_start - hole_length); 2186 bytes_after = element_size * (old_length - hole_start - hole_length);
2092 2187
2093 if (0 == size) 2188 if (0 == size)
2094 { 2189 {
2095 tmpptr = NULL; 2190 tmpptr = NULL;
2096 } 2191 }
2097 else 2192 else
2098 { 2193 {
2099 tmpptr = GNUNET_malloc(size); 2194 tmpptr = GNUNET_malloc (size);
2100 GNUNET_memcpy(tmpptr, oldptr, bytes_before); 2195 GNUNET_memcpy (tmpptr, oldptr, bytes_before);
2101 GNUNET_memcpy(tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after); 2196 GNUNET_memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole),
2102 } 2197 bytes_after);
2198 }
2103 if (NULL != *old) 2199 if (NULL != *old)
2104 { 2200 {
2105 GNUNET_free(*old); 2201 GNUNET_free (*old);
2106 } 2202 }
2107 *old = (void *)tmpptr; 2203 *old = (void *) tmpptr;
2108} 2204}
2109 2205
2110/* 2206/*
@@ -2121,20 +2217,20 @@ ril_cut_from_vector(void **old,
2121 * @param pref_rel the normalized preference value for this kind over all clients 2217 * @param pref_rel the normalized preference value for this kind over all clients
2122 */ 2218 */
2123static void 2219static void
2124GAS_ril_address_change_preference(void *solver, 2220GAS_ril_address_change_preference (void *solver,
2125 const struct GNUNET_PeerIdentity *peer, 2221 const struct GNUNET_PeerIdentity *peer,
2126 enum GNUNET_ATS_PreferenceKind kind, 2222 enum GNUNET_ATS_PreferenceKind kind,
2127 double pref_rel) 2223 double pref_rel)
2128{ 2224{
2129 LOG(GNUNET_ERROR_TYPE_DEBUG, 2225 LOG (GNUNET_ERROR_TYPE_DEBUG,
2130 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n", 2226 "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
2131 GNUNET_ATS_print_preference_type(kind), GNUNET_i2s(peer), pref_rel); 2227 GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
2132 2228
2133 struct GAS_RIL_Handle *s = solver; 2229 struct GAS_RIL_Handle *s = solver;
2134 2230
2135 s->parameters.temperature = s->parameters.temperature_init; 2231 s->parameters.temperature = s->parameters.temperature_init;
2136 s->parameters.epsilon = s->parameters.epsilon_init; 2232 s->parameters.epsilon = s->parameters.epsilon_init;
2137 ril_step(s); 2233 ril_step (s);
2138} 2234}
2139 2235
2140 2236
@@ -2148,9 +2244,9 @@ GAS_ril_address_change_preference(void *solver,
2148 * @param network network type of this address 2244 * @param network network type of this address
2149 */ 2245 */
2150static void 2246static void
2151GAS_ril_address_add(void *solver, 2247GAS_ril_address_add (void *solver,
2152 struct ATS_Address *address, 2248 struct ATS_Address *address,
2153 uint32_t network) 2249 uint32_t network)
2154{ 2250{
2155 struct GAS_RIL_Handle *s = solver; 2251 struct GAS_RIL_Handle *s = solver;
2156 struct RIL_Peer_Agent *agent; 2252 struct RIL_Peer_Agent *agent;
@@ -2163,68 +2259,71 @@ GAS_ril_address_add(void *solver,
2163 int i; 2259 int i;
2164 unsigned int zero; 2260 unsigned int zero;
2165 2261
2166 LOG(GNUNET_ERROR_TYPE_DEBUG, 2262 LOG (GNUNET_ERROR_TYPE_DEBUG,
2167 "API_address_add()\n"); 2263 "API_address_add()\n");
2168 2264
2169 net = ril_get_network(s, network); 2265 net = ril_get_network (s, network);
2170 address->solver_information = net; 2266 address->solver_information = net;
2171 2267
2172 if (!ril_network_is_active(s, network)) 2268 if (! ril_network_is_active (s, network))
2173 { 2269 {
2174 LOG(GNUNET_ERROR_TYPE_DEBUG, 2270 LOG (GNUNET_ERROR_TYPE_DEBUG,
2175 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n", 2271 "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
2176 address->plugin, address->addr, GNUNET_i2s(&address->peer)); 2272 address->plugin, address->addr, GNUNET_i2s (&address->peer));
2177 return; 2273 return;
2178 } 2274 }
2179 2275
2180 s->parameters.temperature = s->parameters.temperature_init; 2276 s->parameters.temperature = s->parameters.temperature_init;
2181 s->parameters.epsilon = s->parameters.epsilon_init; 2277 s->parameters.epsilon = s->parameters.epsilon_init;
2182 2278
2183 agent = ril_get_agent(s, &address->peer, GNUNET_YES); 2279 agent = ril_get_agent (s, &address->peer, GNUNET_YES);
2184 2280
2185 //add address 2281 // add address
2186 address_wrapped = GNUNET_new(struct RIL_Address_Wrapped); 2282 address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
2187 address_wrapped->address_naked = address; 2283 address_wrapped->address_naked = address;
2188 GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped); 2284 GNUNET_CONTAINER_DLL_insert_tail (agent->addresses_head,
2285 agent->addresses_tail, address_wrapped);
2189 2286
2190 //increase size of W 2287 // increase size of W
2191 m_new = agent->m + ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)); 2288 m_new = agent->m + ((s->parameters.rbf_divisor + 1)
2289 * (s->parameters.rbf_divisor + 1));
2192 m_old = agent->m; 2290 m_old = agent->m;
2193 n_new = agent->n + 1; 2291 n_new = agent->n + 1;
2194 n_old = agent->n; 2292 n_old = agent->n;
2195 2293
2196 GNUNET_array_grow(agent->W, agent->n, n_new); 2294 GNUNET_array_grow (agent->W, agent->n, n_new);
2197 agent->n = n_old; 2295 agent->n = n_old;
2198 GNUNET_array_grow(agent->E, agent->n, n_new); 2296 GNUNET_array_grow (agent->E, agent->n, n_new);
2199 for (i = 0; i < n_new; i++) 2297 for (i = 0; i < n_new; i++)
2298 {
2299 if (i < n_old)
2200 { 2300 {
2201 if (i < n_old) 2301 agent->m = m_old;
2202 { 2302 GNUNET_array_grow (agent->W[i], agent->m, m_new);
2203 agent->m = m_old; 2303 agent->m = m_old;
2204 GNUNET_array_grow(agent->W[i], agent->m, m_new); 2304 GNUNET_array_grow (agent->E[i], agent->m, m_new);
2205 agent->m = m_old; 2305 }
2206 GNUNET_array_grow(agent->E[i], agent->m, m_new); 2306 else
2207 } 2307 {
2208 else 2308 zero = 0;
2209 { 2309 GNUNET_array_grow (agent->W[i], zero, m_new);
2210 zero = 0; 2310 zero = 0;
2211 GNUNET_array_grow(agent->W[i], zero, m_new); 2311 GNUNET_array_grow (agent->E[i], zero, m_new);
2212 zero = 0;
2213 GNUNET_array_grow(agent->E[i], zero, m_new);
2214 }
2215 } 2312 }
2313 }
2216 2314
2217 //increase size of old state vector 2315 // increase size of old state vector
2218 agent->m = m_old; 2316 agent->m = m_old;
2219 GNUNET_array_grow(agent->s_old, agent->m, m_new); 2317 GNUNET_array_grow (agent->s_old, agent->m, m_new);
2220 2318
2221 ril_try_unblock_agent(s, agent, GNUNET_NO); 2319 ril_try_unblock_agent (s, agent, GNUNET_NO);
2222 2320
2223 ril_step(s); 2321 ril_step (s);
2224 2322
2225 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n", 2323 LOG (GNUNET_ERROR_TYPE_DEBUG,
2226 address->active ? "active" : "inactive", address->plugin, address->addr, 2324 "API_address_add() Added %s %s address %s for peer '%s'\n",
2227 GNUNET_i2s(&address->peer)); 2325 address->active ? "active" : "inactive", address->plugin, address->addr,
2326 GNUNET_i2s (&address->peer));
2228} 2327}
2229 2328
2230/** 2329/**
@@ -2236,8 +2335,8 @@ GAS_ril_address_add(void *solver,
2236 * @param address the address to remove 2335 * @param address the address to remove
2237 */ 2336 */
2238static void 2337static void
2239GAS_ril_address_delete(void *solver, 2338GAS_ril_address_delete (void *solver,
2240 struct ATS_Address *address) 2339 struct ATS_Address *address)
2241{ 2340{
2242 struct GAS_RIL_Handle *s = solver; 2341 struct GAS_RIL_Handle *s = solver;
2243 struct RIL_Peer_Agent *agent; 2342 struct RIL_Peer_Agent *agent;
@@ -2248,103 +2347,112 @@ GAS_ril_address_delete(void *solver,
2248 int i; 2347 int i;
2249 struct RIL_Scope *net; 2348 struct RIL_Scope *net;
2250 2349
2251 LOG(GNUNET_ERROR_TYPE_DEBUG, 2350 LOG (GNUNET_ERROR_TYPE_DEBUG,
2252 "API_address_delete() Delete %s %s address %s for peer '%s'\n", 2351 "API_address_delete() Delete %s %s address %s for peer '%s'\n",
2253 address->active ? "active" : "inactive", 2352 address->active ? "active" : "inactive",
2254 address->plugin, 2353 address->plugin,
2255 address->addr, 2354 address->addr,
2256 GNUNET_i2s(&address->peer)); 2355 GNUNET_i2s (&address->peer));
2257 2356
2258 agent = ril_get_agent(s, &address->peer, GNUNET_NO); 2357 agent = ril_get_agent (s, &address->peer, GNUNET_NO);
2259 if (NULL == agent) 2358 if (NULL == agent)
2260 { 2359 {
2261 net = address->solver_information; 2360 net = address->solver_information;
2262 GNUNET_assert(!ril_network_is_active(s, net->type)); 2361 GNUNET_assert (! ril_network_is_active (s, net->type));
2263 LOG(GNUNET_ERROR_TYPE_DEBUG, 2362 LOG (GNUNET_ERROR_TYPE_DEBUG,
2264 "No agent allocated for peer yet, since address was in inactive network\n"); 2363 "No agent allocated for peer yet, since address was in inactive network\n");
2265 return; 2364 return;
2266 } 2365 }
2267 2366
2268 s->parameters.temperature = s->parameters.temperature_init; 2367 s->parameters.temperature = s->parameters.temperature_init;
2269 s->parameters.epsilon = s->parameters.epsilon_init; 2368 s->parameters.epsilon = s->parameters.epsilon_init;
2270 2369
2271 address_index = agent_address_get_index(agent, address); 2370 address_index = agent_address_get_index (agent, address);
2272 address_wrapped = agent_address_get_wrapped(agent, address); 2371 address_wrapped = agent_address_get_wrapped (agent, address);
2273 2372
2274 if (NULL == address_wrapped) 2373 if (NULL == address_wrapped)
2275 { 2374 {
2276 net = address->solver_information; 2375 net = address->solver_information;
2277 LOG(GNUNET_ERROR_TYPE_DEBUG, 2376 LOG (GNUNET_ERROR_TYPE_DEBUG,
2278 "Address not considered by agent, address was in inactive network\n"); 2377 "Address not considered by agent, address was in inactive network\n");
2279 return; 2378 return;
2280 } 2379 }
2281 GNUNET_CONTAINER_DLL_remove(agent->addresses_head, 2380 GNUNET_CONTAINER_DLL_remove (agent->addresses_head,
2282 agent->addresses_tail, 2381 agent->addresses_tail,
2283 address_wrapped); 2382 address_wrapped);
2284 GNUNET_free(address_wrapped); 2383 GNUNET_free (address_wrapped);
2285 2384
2286 //decrease W 2385 // decrease W
2287 m_new = agent->m - ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)); 2386 m_new = agent->m - ((s->parameters.rbf_divisor + 1)
2387 * (s->parameters.rbf_divisor + 1));
2288 n_new = agent->n - 1; 2388 n_new = agent->n - 1;
2289 2389
2290 for (i = 0; i < agent->n; i++) 2390 for (i = 0; i < agent->n; i++)
2291 { 2391 {
2292 ril_cut_from_vector((void **)&agent->W[i], sizeof(double), 2392 ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
2293 address_index * ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)), 2393 address_index * ((s->parameters.rbf_divisor + 1)
2294 ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)), agent->m); 2394 * (s->parameters.rbf_divisor + 1)),
2295 ril_cut_from_vector((void **)&agent->E[i], sizeof(double), 2395 ((s->parameters.rbf_divisor + 1)
2296 address_index * ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)), 2396 * (s->parameters.rbf_divisor + 1)), agent->m);
2297 ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)), agent->m); 2397 ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
2298 } 2398 address_index * ((s->parameters.rbf_divisor + 1)
2299 GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]); 2399 * (s->parameters.rbf_divisor + 1)),
2300 GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]); 2400 ((s->parameters.rbf_divisor + 1)
2301 ril_cut_from_vector((void **)&agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index, 2401 * (s->parameters.rbf_divisor + 1)), agent->m);
2302 1, agent->n); 2402 }
2303 ril_cut_from_vector((void **)&agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index, 2403 GNUNET_free_non_null (agent->W[RIL_ACTION_TYPE_NUM + address_index]);
2304 1, agent->n); 2404 GNUNET_free_non_null (agent->E[RIL_ACTION_TYPE_NUM + address_index]);
2305 //correct last action 2405 ril_cut_from_vector ((void **) &agent->W, sizeof(double *),
2406 RIL_ACTION_TYPE_NUM + address_index,
2407 1, agent->n);
2408 ril_cut_from_vector ((void **) &agent->E, sizeof(double *),
2409 RIL_ACTION_TYPE_NUM + address_index,
2410 1, agent->n);
2411 // correct last action
2306 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index)) 2412 if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
2307 { 2413 {
2308 agent->a_old -= 1; 2414 agent->a_old -= 1;
2309 } 2415 }
2310 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index)) 2416 else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
2311 { 2417 {
2312 agent->a_old = RIL_ACTION_INVALID; 2418 agent->a_old = RIL_ACTION_INVALID;
2313 } 2419 }
2314 //decrease old state vector 2420 // decrease old state vector
2315 ril_cut_from_vector((void **)&agent->s_old, sizeof(double), 2421 ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
2316 address_index * ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)), 2422 address_index * ((s->parameters.rbf_divisor + 1)
2317 ((s->parameters.rbf_divisor + 1) * (s->parameters.rbf_divisor + 1)), agent->m); 2423 * (s->parameters.rbf_divisor + 1)),
2424 ((s->parameters.rbf_divisor + 1)
2425 * (s->parameters.rbf_divisor + 1)), agent->m);
2318 agent->m = m_new; 2426 agent->m = m_new;
2319 agent->n = n_new; 2427 agent->n = n_new;
2320 2428
2321 if (agent->address_inuse == address) 2429 if (agent->address_inuse == address)
2322 { 2430 {
2323 if (NULL != agent->addresses_head) //if peer has an address left, use it 2431 if (NULL != agent->addresses_head) // if peer has an address left, use it
2324 { 2432 {
2325 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 2433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326 "Active address died, suggesting alternative!\n"); 2434 "Active address died, suggesting alternative!\n");
2327 envi_set_active_suggestion(s, 2435 envi_set_active_suggestion (s,
2328 agent, 2436 agent,
2329 agent->addresses_head->address_naked, 2437 agent->addresses_head->address_naked,
2330 agent->bw_in, 2438 agent->bw_in,
2331 agent->bw_out, 2439 agent->bw_out,
2332 GNUNET_YES); 2440 GNUNET_YES);
2333 } 2441 }
2334 else 2442 else
2335 { 2443 {
2336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 2444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2337 "Active address died, suggesting disconnect!\n"); 2445 "Active address died, suggesting disconnect!\n");
2338 envi_set_active_suggestion(s, agent, NULL, 0, 0, GNUNET_NO); 2446 envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
2339 } 2447 }
2340 } 2448 }
2341 ril_step(solver); 2449 ril_step (solver);
2342 if (agent->suggestion_address == address) 2450 if (agent->suggestion_address == address)
2343 { 2451 {
2344 agent->suggestion_issue = GNUNET_NO; 2452 agent->suggestion_issue = GNUNET_NO;
2345 agent->suggestion_address = NULL; 2453 agent->suggestion_address = NULL;
2346 } 2454 }
2347 GNUNET_assert(agent->address_inuse != address); 2455 GNUNET_assert (agent->address_inuse != address);
2348} 2456}
2349 2457
2350 2458
@@ -2355,17 +2463,17 @@ GAS_ril_address_delete(void *solver,
2355 * @param address the address 2463 * @param address the address
2356 */ 2464 */
2357static void 2465static void
2358GAS_ril_address_property_changed(void *solver, 2466GAS_ril_address_property_changed (void *solver,
2359 struct ATS_Address *address) 2467 struct ATS_Address *address)
2360{ 2468{
2361 struct GAS_RIL_Handle *s = solver; 2469 struct GAS_RIL_Handle *s = solver;
2362 2470
2363 LOG(GNUNET_ERROR_TYPE_DEBUG, 2471 LOG (GNUNET_ERROR_TYPE_DEBUG,
2364 "Properties for peer '%s' address changed\n", 2472 "Properties for peer '%s' address changed\n",
2365 GNUNET_i2s(&address->peer)); 2473 GNUNET_i2s (&address->peer));
2366 s->parameters.temperature = s->parameters.temperature_init; 2474 s->parameters.temperature = s->parameters.temperature_init;
2367 s->parameters.epsilon = s->parameters.epsilon_init; 2475 s->parameters.epsilon = s->parameters.epsilon_init;
2368 ril_step(s); 2476 ril_step (s);
2369} 2477}
2370 2478
2371 2479
@@ -2380,20 +2488,20 @@ GAS_ril_address_property_changed(void *solver,
2380 * @param score the score 2488 * @param score the score
2381 */ 2489 */
2382static void 2490static void
2383GAS_ril_address_preference_feedback(void *solver, 2491GAS_ril_address_preference_feedback (void *solver,
2384 struct GNUNET_SERVICE_Client *application, 2492 struct GNUNET_SERVICE_Client *application,
2385 const struct GNUNET_PeerIdentity *peer, 2493 const struct GNUNET_PeerIdentity *peer,
2386 const struct GNUNET_TIME_Relative scope, 2494 const struct GNUNET_TIME_Relative scope,
2387 enum GNUNET_ATS_PreferenceKind kind, 2495 enum GNUNET_ATS_PreferenceKind kind,
2388 double score) 2496 double score)
2389{ 2497{
2390 LOG(GNUNET_ERROR_TYPE_DEBUG, 2498 LOG (GNUNET_ERROR_TYPE_DEBUG,
2391 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for " 2499 "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
2392 "preference %s for %d seconds\n", 2500 "preference %s for %d seconds\n",
2393 GNUNET_i2s(peer), 2501 GNUNET_i2s (peer),
2394 "UNKNOWN", 2502 "UNKNOWN",
2395 GNUNET_ATS_print_preference_type(kind), 2503 GNUNET_ATS_print_preference_type (kind),
2396 scope.rel_value_us / 1000000); 2504 scope.rel_value_us / 1000000);
2397} 2505}
2398 2506
2399 2507
@@ -2403,12 +2511,12 @@ GAS_ril_address_preference_feedback(void *solver,
2403 * @param solver the solver 2511 * @param solver the solver
2404 */ 2512 */
2405static void 2513static void
2406GAS_ril_bulk_start(void *solver) 2514GAS_ril_bulk_start (void *solver)
2407{ 2515{
2408 struct GAS_RIL_Handle *s = solver; 2516 struct GAS_RIL_Handle *s = solver;
2409 2517
2410 LOG(GNUNET_ERROR_TYPE_DEBUG, 2518 LOG (GNUNET_ERROR_TYPE_DEBUG,
2411 "API_bulk_start() lock: %d\n", s->bulk_lock + 1); 2519 "API_bulk_start() lock: %d\n", s->bulk_lock + 1);
2412 2520
2413 s->bulk_lock++; 2521 s->bulk_lock++;
2414} 2522}
@@ -2420,26 +2528,26 @@ GAS_ril_bulk_start(void *solver)
2420 * @param solver the solver handle 2528 * @param solver the solver handle
2421 */ 2529 */
2422static void 2530static void
2423GAS_ril_bulk_stop(void *solver) 2531GAS_ril_bulk_stop (void *solver)
2424{ 2532{
2425 struct GAS_RIL_Handle *s = solver; 2533 struct GAS_RIL_Handle *s = solver;
2426 2534
2427 LOG(GNUNET_ERROR_TYPE_DEBUG, 2535 LOG (GNUNET_ERROR_TYPE_DEBUG,
2428 "API_bulk_stop() lock: %d\n", 2536 "API_bulk_stop() lock: %d\n",
2429 s->bulk_lock - 1); 2537 s->bulk_lock - 1);
2430 2538
2431 if (s->bulk_lock < 1) 2539 if (s->bulk_lock < 1)
2432 { 2540 {
2433 GNUNET_break(0); 2541 GNUNET_break (0);
2434 return; 2542 return;
2435 } 2543 }
2436 s->bulk_lock--; 2544 s->bulk_lock--;
2437 2545
2438 if (0 < s->bulk_changes) 2546 if (0 < s->bulk_changes)
2439 { 2547 {
2440 ril_step(solver); 2548 ril_step (solver);
2441 s->bulk_changes = 0; 2549 s->bulk_changes = 0;
2442 } 2550 }
2443} 2551}
2444 2552
2445 2553
@@ -2454,38 +2562,40 @@ GAS_ril_bulk_stop(void *solver)
2454 * @param peer the identity of the peer 2562 * @param peer the identity of the peer
2455 */ 2563 */
2456static void 2564static void
2457GAS_ril_get_preferred_address(void *solver, 2565GAS_ril_get_preferred_address (void *solver,
2458 const struct GNUNET_PeerIdentity *peer) 2566 const struct GNUNET_PeerIdentity *peer)
2459{ 2567{
2460 struct GAS_RIL_Handle *s = solver; 2568 struct GAS_RIL_Handle *s = solver;
2461 struct RIL_Peer_Agent *agent; 2569 struct RIL_Peer_Agent *agent;
2462 2570
2463 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n"); 2571 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
2464 2572
2465 agent = ril_get_agent(s, peer, GNUNET_YES); 2573 agent = ril_get_agent (s, peer, GNUNET_YES);
2466 2574
2467 agent->is_active = GNUNET_YES; 2575 agent->is_active = GNUNET_YES;
2468 envi_set_active_suggestion(solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES); 2576 envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
2577 agent->bw_out, GNUNET_YES);
2469 2578
2470 ril_try_unblock_agent(solver, agent, GNUNET_YES); 2579 ril_try_unblock_agent (solver, agent, GNUNET_YES);
2471 2580
2472 if (agent->address_inuse) 2581 if (agent->address_inuse)
2473 { 2582 {
2474 LOG(GNUNET_ERROR_TYPE_DEBUG, 2583 LOG (GNUNET_ERROR_TYPE_DEBUG,
2475 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n", 2584 "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
2476 GNUNET_i2s(peer), agent->address_inuse->plugin, agent->address_inuse->addr); 2585 GNUNET_i2s (peer), agent->address_inuse->plugin,
2477 } 2586 agent->address_inuse->addr);
2587 }
2478 else 2588 else
2479 { 2589 {
2480 LOG(GNUNET_ERROR_TYPE_DEBUG, 2590 LOG (GNUNET_ERROR_TYPE_DEBUG,
2481 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n", 2591 "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
2482 GNUNET_i2s(peer)); 2592 GNUNET_i2s (peer));
2483 s->parameters.temperature = s->parameters.temperature_init; 2593 s->parameters.temperature = s->parameters.temperature_init;
2484 s->parameters.epsilon = s->parameters.epsilon_init; 2594 s->parameters.epsilon = s->parameters.epsilon_init;
2485 } 2595 }
2486 if (NULL != agent->address_inuse) 2596 if (NULL != agent->address_inuse)
2487 s->env->bandwidth_changed_cb(s->env->cls, 2597 s->env->bandwidth_changed_cb (s->env->cls,
2488 agent->address_inuse); 2598 agent->address_inuse);
2489} 2599}
2490 2600
2491 2601
@@ -2499,41 +2609,42 @@ GAS_ril_get_preferred_address(void *solver,
2499 * @param peer the peer 2609 * @param peer the peer
2500 */ 2610 */
2501static void 2611static void
2502GAS_ril_stop_get_preferred_address(void *solver, 2612GAS_ril_stop_get_preferred_address (void *solver,
2503 const struct GNUNET_PeerIdentity *peer) 2613 const struct GNUNET_PeerIdentity *peer)
2504{ 2614{
2505 struct GAS_RIL_Handle *s = solver; 2615 struct GAS_RIL_Handle *s = solver;
2506 struct RIL_Peer_Agent *agent; 2616 struct RIL_Peer_Agent *agent;
2507 2617
2508 LOG(GNUNET_ERROR_TYPE_DEBUG, 2618 LOG (GNUNET_ERROR_TYPE_DEBUG,
2509 "API_stop_get_preferred_address()"); 2619 "API_stop_get_preferred_address()");
2510 2620
2511 agent = ril_get_agent(s, peer, GNUNET_NO); 2621 agent = ril_get_agent (s, peer, GNUNET_NO);
2512 2622
2513 if (NULL == agent) 2623 if (NULL == agent)
2514 { 2624 {
2515 GNUNET_break(0); 2625 GNUNET_break (0);
2516 return; 2626 return;
2517 } 2627 }
2518 if (GNUNET_NO == agent->is_active) 2628 if (GNUNET_NO == agent->is_active)
2519 { 2629 {
2520 GNUNET_break(0); 2630 GNUNET_break (0);
2521 return; 2631 return;
2522 } 2632 }
2523 2633
2524 s->parameters.temperature = s->parameters.temperature_init; 2634 s->parameters.temperature = s->parameters.temperature_init;
2525 s->parameters.epsilon = s->parameters.epsilon_init; 2635 s->parameters.epsilon = s->parameters.epsilon_init;
2526 2636
2527 agent->is_active = GNUNET_NO; 2637 agent->is_active = GNUNET_NO;
2528 2638
2529 envi_set_active_suggestion(s, agent, agent->address_inuse, agent->bw_in, agent->bw_out, 2639 envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in,
2530 GNUNET_YES); 2640 agent->bw_out,
2641 GNUNET_YES);
2531 2642
2532 ril_step(s); 2643 ril_step (s);
2533 2644
2534 LOG(GNUNET_ERROR_TYPE_DEBUG, 2645 LOG (GNUNET_ERROR_TYPE_DEBUG,
2535 "API_stop_get_preferred_address() Paused agent for peer '%s'\n", 2646 "API_stop_get_preferred_address() Paused agent for peer '%s'\n",
2536 GNUNET_i2s(peer)); 2647 GNUNET_i2s (peer));
2537} 2648}
2538 2649
2539 2650
@@ -2543,233 +2654,258 @@ GAS_ril_stop_get_preferred_address(void *solver,
2543 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment' 2654 * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
2544 */ 2655 */
2545void * 2656void *
2546libgnunet_plugin_ats_ril_init(void *cls) 2657libgnunet_plugin_ats_ril_init (void *cls)
2547{ 2658{
2548 static struct GNUNET_ATS_SolverFunctions sf; 2659 static struct GNUNET_ATS_SolverFunctions sf;
2549 struct GNUNET_ATS_PluginEnvironment *env = cls; 2660 struct GNUNET_ATS_PluginEnvironment *env = cls;
2550 struct GAS_RIL_Handle *solver = GNUNET_new(struct GAS_RIL_Handle); 2661 struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
2551 struct RIL_Scope * cur; 2662 struct RIL_Scope *cur;
2552 int c; 2663 int c;
2553 char *string; 2664 char *string;
2554 float f_tmp; 2665 float f_tmp;
2555 2666
2556 LOG(GNUNET_ERROR_TYPE_DEBUG, 2667 LOG (GNUNET_ERROR_TYPE_DEBUG,
2557 "API_init() Initializing RIL solver\n"); 2668 "API_init() Initializing RIL solver\n");
2558 2669
2559 GNUNET_assert(NULL != env); 2670 GNUNET_assert (NULL != env);
2560 GNUNET_assert(NULL != env->cfg); 2671 GNUNET_assert (NULL != env->cfg);
2561 GNUNET_assert(NULL != env->stats); 2672 GNUNET_assert (NULL != env->stats);
2562 GNUNET_assert(NULL != env->bandwidth_changed_cb); 2673 GNUNET_assert (NULL != env->bandwidth_changed_cb);
2563 GNUNET_assert(NULL != env->get_preferences); 2674 GNUNET_assert (NULL != env->get_preferences);
2564 2675
2565 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor)) 2676 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats",
2566 { 2677 "RIL_RBF_DIVISOR",
2567 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR; 2678 &solver->parameters.
2568 } 2679 rbf_divisor))
2680 {
2681 solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
2682 }
2569 2683
2570 if (GNUNET_OK 2684 if (GNUNET_OK
2571 != GNUNET_CONFIGURATION_get_value_time(env->cfg, "ats", "RIL_STEP_TIME_MIN", 2685 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats",
2572 &solver->parameters.step_time_min)) 2686 "RIL_STEP_TIME_MIN",
2573 { 2687 &solver->parameters.step_time_min))
2574 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN; 2688 {
2575 } 2689 solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
2690 }
2576 2691
2577 if (GNUNET_OK 2692 if (GNUNET_OK
2578 != GNUNET_CONFIGURATION_get_value_time(env->cfg, "ats", "RIL_STEP_TIME_MAX", 2693 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats",
2579 &solver->parameters.step_time_max)) 2694 "RIL_STEP_TIME_MAX",
2580 { 2695 &solver->parameters.step_time_max))
2581 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX; 2696 {
2582 } 2697 solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
2698 }
2583 2699
2584 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(env->cfg, "ats", "RIL_ALGORITHM", &string)) 2700 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats",
2701 "RIL_ALGORITHM",
2702 &string))
2703 {
2704 GNUNET_STRINGS_utf8_toupper (string, string);
2705 if (0 == strcmp (string, "SARSA"))
2585 { 2706 {
2586 GNUNET_STRINGS_utf8_toupper(string, string); 2707 solver->parameters.algorithm = RIL_ALGO_SARSA;
2587 if (0 == strcmp(string, "SARSA"))
2588 {
2589 solver->parameters.algorithm = RIL_ALGO_SARSA;
2590 }
2591 if (0 == strcmp(string, "Q-LEARNING"))
2592 {
2593 solver->parameters.algorithm = RIL_ALGO_Q;
2594 }
2595
2596 GNUNET_free(string);
2597 } 2708 }
2598 else 2709 if (0 == strcmp (string, "Q-LEARNING"))
2599 { 2710 {
2600 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM; 2711 solver->parameters.algorithm = RIL_ALGO_Q;
2601 } 2712 }
2602 2713
2603 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(env->cfg, "ats", "RIL_SELECT", &string)) 2714 GNUNET_free (string);
2604 { 2715 }
2605 solver->parameters.select = !strcmp(string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2606 GNUNET_free(string);
2607 }
2608 else 2716 else
2609 { 2717 {
2610 solver->parameters.select = RIL_DEFAULT_SELECT; 2718 solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
2611 } 2719 }
2720
2721 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats",
2722 "RIL_SELECT",
2723 &string))
2724 {
2725 solver->parameters.select = ! strcmp (string, "EGREEDY") ?
2726 RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
2727 GNUNET_free (string);
2728 }
2729 else
2730 {
2731 solver->parameters.select = RIL_DEFAULT_SELECT;
2732 }
2612 2733
2613 2734
2614 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA; 2735 solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
2615 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats", 2736 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2616 "RIL_DISCOUNT_BETA", &f_tmp)) 2737 "RIL_DISCOUNT_BETA",
2738 &f_tmp))
2739 {
2740 if (f_tmp < 0.0)
2617 { 2741 {
2618 if (f_tmp < 0.0) 2742 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Invalid %s configuration %f \n"),
2619 { 2743 "RIL_DISCOUNT_BETA", f_tmp);
2620 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"), 2744 }
2621 "RIL_DISCOUNT_BETA", f_tmp); 2745 else
2622 } 2746 {
2623 else 2747 solver->parameters.beta = f_tmp;
2624 { 2748 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2625 solver->parameters.beta = f_tmp; 2749 "RIL_DISCOUNT_BETA", f_tmp);
2626 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2627 "RIL_DISCOUNT_BETA", f_tmp);
2628 }
2629 } 2750 }
2751 }
2630 2752
2631 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA; 2753 solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
2632 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats", 2754 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2633 "RIL_DISCOUNT_GAMMA", &f_tmp)) 2755 "RIL_DISCOUNT_GAMMA",
2756 &f_tmp))
2757 {
2758 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2634 { 2759 {
2635 if ((f_tmp < 0.0) || (f_tmp > 1.0)) 2760 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Invalid %s configuration %f \n"),
2636 { 2761 "RIL_DISCOUNT_GAMMA", f_tmp);
2637 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2638 "RIL_DISCOUNT_GAMMA", f_tmp);
2639 }
2640 else
2641 {
2642 solver->parameters.gamma = f_tmp;
2643 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2644 "RIL_DISCOUNT_GAMMA", f_tmp);
2645 }
2646 } 2762 }
2647 2763 else
2648 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2649 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2650 "RIL_GRADIENT_STEP_SIZE", &f_tmp))
2651 { 2764 {
2652 if ((f_tmp < 0.0) || (f_tmp > 1.0)) 2765 solver->parameters.gamma = f_tmp;
2653 { 2766 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2654 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"), 2767 "RIL_DISCOUNT_GAMMA", f_tmp);
2655 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2656 }
2657 else
2658 {
2659 solver->parameters.alpha = f_tmp;
2660 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2661 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2662 }
2663 } 2768 }
2769 }
2664 2770
2665 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY; 2771 solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
2666 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats", 2772 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2667 "RIL_TRACE_DECAY", &f_tmp)) 2773 "RIL_GRADIENT_STEP_SIZE",
2774 &f_tmp))
2775 {
2776 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2668 { 2777 {
2669 if ((f_tmp < 0.0) || (f_tmp > 1.0)) 2778 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Invalid %s configuration %f \n"),
2670 { 2779 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2671 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2672 "RIL_TRACE_DECAY", f_tmp);
2673 }
2674 else
2675 {
2676 solver->parameters.lambda = f_tmp;
2677 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2678 "RIL_TRACE_DECAY", f_tmp);
2679 }
2680 } 2780 }
2681 2781 else
2682 solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
2683 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2684 "RIL_EXPLORE_RATIO", &f_tmp))
2685 { 2782 {
2686 if ((f_tmp < 0.0) || (f_tmp > 1.0)) 2783 solver->parameters.alpha = f_tmp;
2687 { 2784 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2688 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"), 2785 "RIL_GRADIENT_STEP_SIZE", f_tmp);
2689 "RIL_EXPLORE_RATIO", f_tmp);
2690 }
2691 else
2692 {
2693 solver->parameters.epsilon_init = f_tmp;
2694 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2695 "RIL_EXPLORE_RATIO", f_tmp);
2696 }
2697 } 2786 }
2787 }
2698 2788
2699 solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY; 2789 solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
2700 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats", 2790 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2701 "RIL_EXPLORE_DECAY", &f_tmp)) 2791 "RIL_TRACE_DECAY",
2792 &f_tmp))
2793 {
2794 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2702 { 2795 {
2703 if ((f_tmp < 0.0) || (f_tmp > 1.0)) 2796 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Invalid %s configuration %f \n"),
2704 { 2797 "RIL_TRACE_DECAY", f_tmp);
2705 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2706 "RIL_EXPLORE_DECAY", f_tmp);
2707 }
2708 else
2709 {
2710 solver->parameters.epsilon_decay = f_tmp;
2711 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2712 "RIL_EXPLORE_DECAY", f_tmp);
2713 }
2714 } 2798 }
2715 2799 else
2716 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2717 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats",
2718 "RIL_TEMPERATURE", &f_tmp))
2719 { 2800 {
2720 if (f_tmp <= 0.0) 2801 solver->parameters.lambda = f_tmp;
2721 { 2802 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2722 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"), 2803 "RIL_TRACE_DECAY", f_tmp);
2723 "RIL_TEMPERATURE", f_tmp);
2724 }
2725 else
2726 {
2727 solver->parameters.temperature_init = f_tmp;
2728 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2729 "RIL_TEMPERATURE", f_tmp);
2730 }
2731 } 2804 }
2805 }
2732 2806
2733 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY; 2807 solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
2734 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float(env->cfg, "ats", 2808 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2735 "RIL_TEMPERATURE_DECAY", &f_tmp)) 2809 "RIL_EXPLORE_RATIO",
2810 &f_tmp))
2811 {
2812 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2736 { 2813 {
2737 if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1) 2814 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Invalid %s configuration %f \n"),
2738 { 2815 "RIL_EXPLORE_RATIO", f_tmp);
2739 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2740 "RIL_TEMPERATURE_DECAY", f_tmp);
2741 }
2742 else
2743 {
2744 solver->parameters.temperature_decay = f_tmp;
2745 LOG(GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2746 "RIL_TEMPERATURE_DECAY", f_tmp);
2747 }
2748 } 2816 }
2749 2817 else
2750 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
2751 { 2818 {
2752 solver->simulate = GNUNET_NO; 2819 solver->parameters.epsilon_init = f_tmp;
2820 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2821 "RIL_EXPLORE_RATIO", f_tmp);
2753 } 2822 }
2823 }
2754 2824
2755 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES")) 2825 solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
2826 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2827 "RIL_EXPLORE_DECAY",
2828 &f_tmp))
2829 {
2830 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2756 { 2831 {
2757 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE; 2832 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Invalid %s configuration %f \n"),
2833 "RIL_EXPLORE_DECAY", f_tmp);
2758 } 2834 }
2759 else 2835 else
2760 { 2836 {
2761 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE; 2837 solver->parameters.epsilon_decay = f_tmp;
2838 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2839 "RIL_EXPLORE_DECAY", f_tmp);
2762 } 2840 }
2841 }
2763 2842
2764 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string)) 2843 solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
2844 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2845 "RIL_TEMPERATURE",
2846 &f_tmp))
2847 {
2848 if (f_tmp <= 0.0)
2765 { 2849 {
2766 solver->parameters.social_welfare = !strcmp(string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN; 2850 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Invalid %s configuration %f \n"),
2767 GNUNET_free(string); 2851 "RIL_TEMPERATURE", f_tmp);
2768 } 2852 }
2769 else 2853 else
2770 { 2854 {
2771 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE; 2855 solver->parameters.temperature_init = f_tmp;
2856 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2857 "RIL_TEMPERATURE", f_tmp);
2772 } 2858 }
2859 }
2860
2861 solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
2862 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2863 "RIL_TEMPERATURE_DECAY",
2864 &f_tmp))
2865 {
2866 if ((f_tmp <= 0.0) ||(solver->parameters.temperature_decay > 1) )
2867 {
2868 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Invalid %s configuration %f \n"),
2869 "RIL_TEMPERATURE_DECAY", f_tmp);
2870 }
2871 else
2872 {
2873 solver->parameters.temperature_decay = f_tmp;
2874 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2875 "RIL_TEMPERATURE_DECAY", f_tmp);
2876 }
2877 }
2878
2879 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats",
2880 "RIL_SIMULATE",
2881 &solver->simulate))
2882 {
2883 solver->simulate = GNUNET_NO;
2884 }
2885
2886 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "ats",
2887 "RIL_REPLACE_TRACES"))
2888 {
2889 solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
2890 }
2891 else
2892 {
2893 solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
2894 }
2895
2896 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats",
2897 "RIL_SOCIAL_WELFARE",
2898 &string))
2899 {
2900 solver->parameters.social_welfare = ! strcmp (string, "NASH") ?
2901 RIL_WELFARE_NASH :
2902 RIL_WELFARE_EGALITARIAN;
2903 GNUNET_free (string);
2904 }
2905 else
2906 {
2907 solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
2908 }
2773 2909
2774 solver->env = env; 2910 solver->env = env;
2775 sf.cls = solver; 2911 sf.cls = solver;
@@ -2784,35 +2920,38 @@ libgnunet_plugin_ats_ril_init(void *cls)
2784 sf.s_bulk_stop = &GAS_ril_bulk_stop; 2920 sf.s_bulk_stop = &GAS_ril_bulk_stop;
2785 2921
2786 solver->networks_count = env->network_count; 2922 solver->networks_count = env->network_count;
2787 solver->network_entries = GNUNET_malloc(env->network_count * sizeof(struct RIL_Scope)); 2923 solver->network_entries = GNUNET_malloc (env->network_count * sizeof(struct
2924 RIL_Scope));
2788 solver->step_count = 0; 2925 solver->step_count = 0;
2789 solver->done = GNUNET_NO; 2926 solver->done = GNUNET_NO;
2790 2927
2791 for (c = 0; c < env->network_count; c++) 2928 for (c = 0; c < env->network_count; c++)
2792 { 2929 {
2793 cur = &solver->network_entries[c]; 2930 cur = &solver->network_entries[c];
2794 cur->type = c; 2931 cur->type = c;
2795 cur->bw_in_available = env->in_quota[c]; 2932 cur->bw_in_available = env->in_quota[c];
2796 cur->bw_out_available = env->out_quota[c]; 2933 cur->bw_out_available = env->out_quota[c];
2797 LOG(GNUNET_ERROR_TYPE_DEBUG, 2934 LOG (GNUNET_ERROR_TYPE_DEBUG,
2798 "init() Quotas for %s network: IN %llu - OUT %llu\n", 2935 "init() Quotas for %s network: IN %llu - OUT %llu\n",
2799 GNUNET_NT_to_string(cur->type), 2936 GNUNET_NT_to_string (cur->type),
2800 cur->bw_in_available / 1024, 2937 cur->bw_in_available / 1024,
2801 cur->bw_out_available / 1024); 2938 cur->bw_out_available / 1024);
2802 } 2939 }
2803 2940
2804 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n"); 2941 LOG (GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
2805 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n", 2942 LOG (GNUNET_ERROR_TYPE_DEBUG,
2806 solver->parameters.algorithm ? "Q" : "SARSA", 2943 "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
2807 solver->parameters.alpha, 2944 solver->parameters.algorithm ? "Q" : "SARSA",
2808 solver->parameters.beta, 2945 solver->parameters.alpha,
2809 solver->parameters.lambda); 2946 solver->parameters.beta,
2810 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n", 2947 solver->parameters.lambda);
2811 solver->parameters.epsilon, 2948 LOG (GNUNET_ERROR_TYPE_DEBUG,
2812 solver->parameters.temperature, 2949 "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
2813 solver->parameters.select ? "EGREEDY" : "SOFTMAX"); 2950 solver->parameters.epsilon,
2814 LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n", 2951 solver->parameters.temperature,
2815 solver->parameters.rbf_divisor); 2952 solver->parameters.select ? "EGREEDY" : "SOFTMAX");
2953 LOG (GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
2954 solver->parameters.rbf_divisor);
2816 2955
2817 return &sf; 2956 return &sf;
2818} 2957}
@@ -2824,32 +2963,32 @@ libgnunet_plugin_ats_ril_init(void *cls)
2824 * @param cls the solver handle 2963 * @param cls the solver handle
2825 */ 2964 */
2826void * 2965void *
2827libgnunet_plugin_ats_ril_done(void *cls) 2966libgnunet_plugin_ats_ril_done (void *cls)
2828{ 2967{
2829 struct GNUNET_ATS_SolverFunctions *sf = cls; 2968 struct GNUNET_ATS_SolverFunctions *sf = cls;
2830 struct GAS_RIL_Handle *s = sf->cls; 2969 struct GAS_RIL_Handle *s = sf->cls;
2831 struct RIL_Peer_Agent *cur_agent; 2970 struct RIL_Peer_Agent *cur_agent;
2832 struct RIL_Peer_Agent *next_agent; 2971 struct RIL_Peer_Agent *next_agent;
2833 2972
2834 LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n"); 2973 LOG (GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
2835 2974
2836 s->done = GNUNET_YES; 2975 s->done = GNUNET_YES;
2837 2976
2838 cur_agent = s->agents_head; 2977 cur_agent = s->agents_head;
2839 while (NULL != cur_agent) 2978 while (NULL != cur_agent)
2840 { 2979 {
2841 next_agent = cur_agent->next; 2980 next_agent = cur_agent->next;
2842 GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent); 2981 GNUNET_CONTAINER_DLL_remove (s->agents_head, s->agents_tail, cur_agent);
2843 agent_die(s, cur_agent); 2982 agent_die (s, cur_agent);
2844 cur_agent = next_agent; 2983 cur_agent = next_agent;
2845 } 2984 }
2846 2985
2847 if (NULL != s->step_next_task_id) 2986 if (NULL != s->step_next_task_id)
2848 { 2987 {
2849 GNUNET_SCHEDULER_cancel(s->step_next_task_id); 2988 GNUNET_SCHEDULER_cancel (s->step_next_task_id);
2850 } 2989 }
2851 GNUNET_free(s->network_entries); 2990 GNUNET_free (s->network_entries);
2852 GNUNET_free(s); 2991 GNUNET_free (s);
2853 2992
2854 return NULL; 2993 return NULL;
2855} 2994}