-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPeerTrustSimulation_v3_1_collusive.nlogo
1999 lines (1708 loc) · 48.1 KB
/
PeerTrustSimulation_v3_1_collusive.nlogo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;direct link which stored the transaction history between these two nodes
;;directed-link-breed [connection_history_links connection_history_link]
;;connection_history_links-own [n_transactions list_satisfaction list_credibility]
globals [time total_global_transactions successful_transactions number_of_nodes number_of_malicous_nodes number_of_good_nodes collusive_transaction current_ticks]
;;peer
turtles-own [service total_transactions my_trust_value malicious list_time_of_feedback_given list_time_of_transaction list_satisfaction list_credibility list_transaction_context_factor_value list_other_peer_id]
;;standard setup procedure
to setup
clear-all
set-default-shape turtles "circle"
;;create peers
create-turtles number_peers
ask turtles
[
set color green
set size 1.2
;;set service
set service random no_services_available
;;initialise the initial trust values
set my_trust_value global_initial_trust_value
;;set the label for the nodes
set label precision my_trust_value 2
;;set malicious status
set malicious false
]
;;set malicious peers according to percentage
let n_malicious round ((number_peers * malicious_peers) / 100)
let malicious_peers_set n-of n_malicious turtles
ask malicious_peers_set
[
set color red
set malicious true
]
set successful_transactions 0
set total_global_transactions 0
set number_of_nodes number_peers
;;calculate number of malicous nodes in the network
set number_of_malicous_nodes n_malicious
;;calculate number of good nodes
set number_of_good_nodes number_of_nodes - number_of_malicous_nodes
;set default collusive transaction behaviour
set collusive_transaction false
reset-ticks
layout
end
;;this method is execute on each clock tick
to go
if not any? turtles [stop]
set current_ticks ticks
;;testing section - to remove
; if total_global_transactions = 2000
; [
; let peer1 one-of turtles
; let peer2 one-of other turtles
; ;;let x similarity peer1 peer2 recent_time_window
; let y calculate-general-peer-trust-PSM-credibility peer1 recent_time_window
; show y
; stop
; ]
collaborate
clear-output
if total_global_transactions > 0
[
let percent_successful precision ((successful_transactions / total_global_transactions) * 100) 2
output-print word (word successful_transactions " (") (word percent_successful "%)")
]
layout
;;remove a random peer
remove-random-peer
;;add a random peer
add-random-peer
;;resize-nodes
tick
end
;;this method adds a new peer to the network
to add
crt 1
[
set size 1.2
;;set service
set service random no_services_available
;;initialise the initial trust values
set my_trust_value new_initial_trust
;;set the label for the nodes
set label precision my_trust_value 2
;;set malicious status
set malicious new_malicious
ifelse malicious
[
set color red
set number_of_malicous_nodes number_of_malicous_nodes + 1
]
[
set color green
set number_of_good_nodes number_of_good_nodes + 1
]
]
set number_of_nodes number_of_nodes + 1
end
;;this method removes a random peer from the network
to remove-random-peer
if random 100 <= rate_of_peer_removal AND rate_of_peer_removal > 0
[
ask one-of turtles
[
ask my-links
[
die
]
show word "turtle removed " who
ifelse malicious
[
set number_of_malicous_nodes number_of_malicous_nodes - 1
]
[
set number_of_good_nodes number_of_good_nodes - 1
]
die
]
set number_of_nodes number_of_nodes - 1
]
end
;;this method adds a random peer from the network
to add-random-peer
if random 100 <= rate_of_random_peer_adding AND rate_of_random_peer_adding > 0
[
crt 1
[
set size 1.2
;;set service
set service random no_services_available
;;initialise the initial trust values
set my_trust_value random-float 1
;;set the label for the nodes
set label precision my_trust_value 2
ifelse random 100 <= 60
[
;;set malicious status
set malicious false
]
[
;;set malicious status
set malicious true
]
ifelse malicious
[
set color red
set number_of_malicous_nodes number_of_malicous_nodes + 1
]
[
set color green
set number_of_good_nodes number_of_good_nodes + 1
]
show word "turtle added " who
]
set number_of_nodes number_of_nodes + 1
]
end
;;performs a single collaboration between a random peer and a peer that performs the desired service and is considered trustworthy
to collaborate
;;determine if collusive groups should form among malicious nodes
ifelse (collusive_malicious_peers AND total_global_transactions > 0 AND number_of_malicous_nodes > 1 AND (random 101 < collusive_transaction_frequency))
[
let malicious_group get-malicious-collusive-group collusive_group_size
set collusive_transaction true
;;perform collusive group transactions
ask malicious_group
[
;;evaluate current links
evaluate-current-connections self
let peer2 one-of other malicious_group
perform-transaction self peer2
;;set my_trust_value calculate-general-peer-trust self
set my_trust_value calculate-adaptive-trust self
set label precision my_trust_value 2
]
]
[
;;behave normally
set collusive_transaction false
let peer1 one-of turtles
;;evaluate current links
evaluate-current-connections peer1
;;create a link to another turtle
let random_service random no_services_available
;;list of other peers who render this service
let potential_partners_list find-potential_peers-to-connect-with random_service
;;check if there are items in the list
let peer2 0
ifelse length potential_partners_list < 1
[
stop ;;exit the procedure
]
[
;;select a random peer
let list_counter 0
;;set peer2 item random (length potential_partners_list) potential_partners_list
set peer2 item 0 potential_partners_list ;;set the chosen peer to the most trusted peer
while [([who] of peer2 = [who] of peer1) AND list_counter < length potential_partners_list]
[
set peer2 item list_counter potential_partners_list
set list_counter list_counter + 1
]
;;final check
if [who] of peer1 = [who] of peer2
[
stop ;;exit the procedure
]
]
;;perform the transaction between two peers
perform-transaction peer1 peer2
]
end
;;this method performs the transaction part of a collaboration
to perform-transaction [peer1 peer2]
;;get the id of origional node
let peer1_id [who] of peer1
;;get the id of other node
let peer2_id [who] of peer2
;;get the exact time of the transaction
let time_of_transaction current_ticks
;;get the context factor of the transaction
let transaction_context_factor get-transaction-context-factor
;;determing if any of the peers will act maliciously during this transaction
let peer1_act_maliciously true
let peer2_act_maliciously true
;;determine if this is a collusive transaction - if yes, skip this step
if not collusive_transaction
[
;;check peer1
ifelse [malicious] of peer1 ;;this peer is malicious
[
if random 101 <= malicious_transactions
[
set peer1_act_maliciously true;;act maliciously
]
]
[
set peer1_act_maliciously false
]
;;check peer2
ifelse [malicious] of peer2 ;;this peer is malicious
[
if random 101 <= malicious_transactions
[
set peer2_act_maliciously true ;;act maliciously
]
]
[
set peer2_act_maliciously false
]
]
;;perform actions via peer1
ask turtle peer1_id
[
;;update origional peer (peer1) feedback history based on the feedback from peer2
if (total_transactions = 0)
[
initialize-turtle-lists self
]
if (peer-provides-feedback peer2 OR collusive_transaction)
[
;;set feedback
set list_time_of_transaction lput time_of_transaction list_time_of_transaction
set list_satisfaction lput get-satisfaction self peer2 peer1_act_maliciously peer2_act_maliciously list_satisfaction
set list_credibility lput get-credibility peer2 list_credibility
set list_other_peer_id lput peer2_id list_other_peer_id
set list_transaction_context_factor_value lput transaction_context_factor list_transaction_context_factor_value
;;set the time of the feedback given during this transaction
set list_time_of_feedback_given lput time_of_transaction list_time_of_feedback_given
]
set total_transactions total_transactions + 1
;;set the other peers feedback from peer1
ask turtle peer2_id
[
if (total_transactions = 0)
[
initialize-turtle-lists self
]
if (peer-provides-feedback peer1 OR collusive_transaction)
[
;;set feedback
set list_time_of_transaction lput time_of_transaction list_time_of_transaction
set list_satisfaction lput get-satisfaction self peer1 peer2_act_maliciously peer1_act_maliciously list_satisfaction
set list_credibility lput get-credibility peer1 list_credibility
set list_other_peer_id lput peer1_id list_other_peer_id
set list_transaction_context_factor_value lput transaction_context_factor list_transaction_context_factor_value
;;set the time of the feedback given during this transaction
set list_time_of_feedback_given lput time_of_transaction list_time_of_feedback_given
]
set total_transactions total_transactions + 1
]
;;create a link for this collaboration if it doesn't exist
if not link-neighbor? peer2
[
create-link-with peer2
]
]
;;determine if the transaction was successful
if not peer1_act_maliciously AND not peer2_act_maliciously
[
set successful_transactions successful_transactions + 1
]
;;update the global number of transactions
set total_global_transactions total_global_transactions + 1
end
;;this method initialises the lists for turtles
to initialize-turtle-lists [peer]
;;initialise feedback lists
set list_satisfaction []
set list_credibility []
set list_other_peer_id []
set list_time_of_transaction []
set list_transaction_context_factor_value []
set list_time_of_feedback_given []
end
;;finding peers who offer the desired service
;;returns a list of the top [no_peers_to_return] peers
to-report find-potential_peers-to-connect-with [required-service]
let potential_peers other turtles with [service = required-service]
;;determine the trust for each peer in the list
ask potential_peers
[
;;set my_trust_value calculate-general-peer-trust self
set my_trust_value calculate-adaptive-trust self
set label precision my_trust_value 2
]
let sorted_list 0
;;return number of peers
ifelse count potential_peers >= no_peers_to_return ;;list is small than number that needs to be returned
[
;;sort the list and return only the first [no_peers_to_return]
set sorted_list sublist ( sort-on [( - my_trust_value)] potential_peers ) 0 no_peers_to_return
]
[
;;sort the entire list
set sorted_list sort-on [(- my_trust_value)] potential_peers
]
report sorted_list
end
;;returns the feedback for a collaboration
;;peer2 will give feedback for peer1's actions
to-report get-binary-satisfaction [peer1 peer2 peer1_acts_malicious peer2_acts_malicious]
;;calculate performance
let performance_quality 0
;;peer1 performs the service
ifelse peer1_acts_malicious
[
set performance_quality 0
]
[
;;performance of a good node
;;ifelse random 100 < 80 ;;20% chance of maybe performing badly
;;[
set performance_quality 1
;;]
;;[
;; set performance_quality 0
;;]
]
;;calculate satisfaction
let satisfaction 0
;;peer2 provides feedback on the service
ifelse peer2_acts_malicious
[
;;dont collaborate
ifelse performance_quality = 0
[
set satisfaction 1
]
[
set satisfaction 0
]
]
[
;;dont act maliciously
set satisfaction performance_quality
]
report satisfaction
end
;;returns the feedback for a collaboration
;;peer2 will give feedback for peer1's actions
to-report get-satisfaction [peer1 peer2 peer1_acts_malicious peer2_acts_malicious]
;;check if this is a collusive transaction
if collusive_transaction
[
;;return a good rating
report 0.5 + random-float 0.5
]
;;not a collusive transaction - therefore behave normally
;;calculate performance
let performance_quality 0
;;peer1 performs the service
ifelse peer1_acts_malicious
[
set performance_quality random 4 ;;between 0 and 3
]
[
;;performance of a good node
ifelse random 100 < 80 ;;20% chance of maybe performing badly
[
set performance_quality 5 + random 6 ;;between 5 and 10
]
[
set performance_quality 3 + random 8 ;;between 3 and 10
]
]
;;calculate satisfaction
let satisfaction 0.0
;;peer2 provides feedback on the service
ifelse peer2_acts_malicious
[
;;dont collaborate
ifelse performance_quality <= 5
[
set satisfaction (performance_quality - (random (performance_quality + 1))) / 10 ;;satisfaction is anywhere between 0 and the performance quality
if performance_quality < 0
[
set performance_quality 0
]
]
[
;;performance is greater than 5 so only rate between 0 and 5
set satisfaction (random 6) / 10
]
]
[
;;collaborate - same as good node
ifelse random 2 = 1 AND performance_quality < 10 ;;add or subtract
[
set satisfaction (performance_quality + random 2) / 10 ;;adds 0 or 1 to the performance_quality
]
[
set satisfaction (performance_quality - random 2) / 10 ;;subtracts 0 or 1 from the performance_quality
]
]
report satisfaction
end
;;this method returns the transaction context factor for a particular transaction
to-report get-transaction-context-factor
ifelse transaction_context_factor?
[
report random-float 1.0
]
[
report 1
]
end
;;determines the credibility of a peer
;;returns a normalised credibility value
to-report get-credibility [peer]
report random-float 1.0
end
;;evaluates all the connections of a peer
;;the link is removed if the nodes trust value is below the threshold
to evaluate-current-connections [peer]
ask peer
[
ask my-links
[
if [my_trust_value] of other-end < trust_threshold
[
die
]
]
]
end
;;this method determines if a peer should provide feedback or not
;;returns a boolean with a yes or no
to-report peer-provides-feedback [peer]
let result true
ifelse (community_context_factor?)
[
ask peer
[
ifelse malicious AND random (100) < 40
[
set result false
]
[
set result true
]
]
]
[
set result true
]
report result
end
;;this method forms collusive groups among a number of malicious peers and gives good ratings
to-report get-malicious-collusive-group [group_size_percentage]
let group_size round ((group_size_percentage / 100 ) * number_of_malicous_nodes)
let group_list n-of group_size turtles with [ malicious = true ]
report group_list
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; GENERAL PEER TRUST CALCULATION (BASIC) - UPDATE IF USING THIS ;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;this method calculates the trust of a peer according to the peerTrust algorithm using the PeerTrust TVM metric
;;this method assumes that the transaction history for this peer is stored at this peer
;;the out-links store all the transaction information as given to a particular node
to-report calculate-general-peer-trust [peer]
let total_trust 0.0
ask peer
[
let counter 0
ifelse total_transactions > 0
[
foreach list_satisfaction ;;all transactions with this neighbour
[
let temp_credibility (item counter list_credibility)
let temp_transaction_context_factor (item counter list_transaction_context_factor_value)
let transaction_total precision (? * temp_credibility * temp_transaction_context_factor) 3
;;add to total sum for trust
set total_trust (total_trust + transaction_total)
set counter counter + 1
]
]
[
set total_trust my_trust_value
]
]
;;normalise the trust value
;;set total_trust total_trust
show total_trust
report total_trust
end
;;this is a utility function to square a number x
to-report square [x]
report x * x
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; CREDIBILITY PEER TRUST CALCULATION (BASIC) ;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;this method calculates the trust for a peer, taking into account the adaptive trust calculation
to-report calculate-adaptive-trust [peer]
let recent_trust 0
let adaptive_trust 0
ifelse psm_credibility;; AND [total_transactions] of peer > 100
[
set recent_trust calculate-general-peer-trust-PSM-credibility peer recent_time_window
set adaptive_trust calculate-general-peer-trust-PSM-credibility peer adaptive_time_window
]
[
set recent_trust calculate-general-peer-trust-TVM-credibility peer recent_time_window
set adaptive_trust calculate-general-peer-trust-TVM-credibility peer adaptive_time_window
]
ifelse (recent_trust - adaptive_trust) > time_threshold
[
report adaptive_trust
]
[
report recent_trust
]
end
;this method calculates the community context factor within a particular time windows
to-report calculate-community-context-factor [peer time_ticks]
let number_times_given_feedback 0
let transactions_in_time_window 0
ask peer
[
if total_transactions > 0
[
;get number of transactions where feedback is provided in the given time window
foreach list_time_of_feedback_given
[
if (time_ticks > current_ticks OR ? >= (current_ticks - time_ticks))
[
set number_times_given_feedback number_times_given_feedback + 1
]
]
;get total number of transactions in a provide time window
foreach list_time_of_transaction
[
if (time_ticks > current_ticks OR ? >= (current_ticks - time_ticks))
[
set transactions_in_time_window transactions_in_time_window + 1
]
]
]
]
report number_times_given_feedback / transactions_in_time_window
end
;;this method calculates the total credibility (trust values) for all a peers transactions
;;this method takes into account the time of the transactions - using global variables recent_time_window and adaptive_time_window passed as parameters
to-report calculate-total-trust-values-for-TVM-credibility [peer time_ticks]
let total_trust 0.0
ask peer
[
if total_transactions > 0
[
let counter 0 ;;to get the time of the transaction
foreach list_other_peer_id
[
;;get the time of the transaction
let transaction_time item counter list_time_of_transaction
if (time_ticks > current_ticks OR transaction_time >= (current_ticks - time_ticks)) AND not (turtle ? = NOBODY)
[
ask turtle ?
[
set total_trust total_trust + my_trust_value
]
]
set counter counter + 1
]
]
]
report total_trust
end
;;this method calculates the trust of a peer according to the peerTrust algorithm
;;this method assumes that the transaction history for this peer is stored at this peer
;;the out-links store all the transaction information as given to a particular node
;;this method takes into account the time of the transactions - using global variables recent_time_window and adaptive_time_window passed as parameters
to-report calculate-general-peer-trust-TVM-credibility [peer time_ticks]
let total_trust 0.0
ask peer
[
let counter 0
ifelse total_transactions > 0
[
;;get the sum of the trust value of other peers for all transactions in a given time window. Used to normalise credibility
let sum_other_peers_trust_values calculate-total-trust-values-for-TVM-credibility self time_ticks
ifelse not (sum_other_peers_trust_values = 0)
[
;;get the community context factor
let temp_community_context_factor 1
if community_context_factor?
[
set temp_community_context_factor calculate-community-context-factor self time_ticks
]
foreach list_satisfaction ;;all transactions with this neighbour
[
;;check if this transaction is in the given time window
let transaction_time item counter list_time_of_transaction
if time_ticks > current_ticks OR transaction_time >= (current_ticks - time_ticks) ;;only consider this transaction if it is in the time interval
[
let temp_id (item counter list_other_peer_id)
if not (turtle temp_id = NOBODY) ;;this turtle still exists
[
let temp_credibility 0.0
ask turtle temp_id
[
set temp_credibility my_trust_value
]
;;get transaction context factor
let temp_transaction_context_factor (item counter list_transaction_context_factor_value)
let transaction_total precision (alpha *(? * (temp_credibility / sum_other_peers_trust_values) * temp_transaction_context_factor)) 3
;;add to total sum for trust
set total_trust (total_trust + transaction_total)
]
]
set counter counter + 1
]
set total_trust total_trust + (beta * temp_community_context_factor)
]
[ ;;trust not calculated because sum of other peers is zero
set total_trust my_trust_value
]
]
[ ;;no transactions so trust is not calculated
set total_trust my_trust_value
]
]
;;remove negatives
if total_trust < 0 [set total_trust 0]
report total_trust
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; CREDIBILITY PEER TRUST CALCULATION (Similarity) ;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;this method gets the ids of the common peers the two peers (paramaters have interacted with) during a specified time window
to-report get-common-transaction-peers [peer1 peer2 time_ticks]
let counter 0
let list_peer1_transaction_peer_ids []
let list_peer1_transaction_satisfaction []
let list_peer2_transaction_peer_ids []
let list_common_transaction_peer_ids []
;;get ids of other peers transacted with during this time interval
ask peer1
[
if length list_time_of_transaction > 0
[
foreach list_time_of_transaction
[
if (time_ticks > current_ticks OR ? >= (current_ticks - time_ticks))
[
set list_peer1_transaction_peer_ids lput (item counter list_other_peer_id) list_peer1_transaction_peer_ids
]
set counter counter + 1
]
]
]
;remove duplicates
set list_peer1_transaction_peer_ids remove-duplicates list_peer1_transaction_peer_ids
;;show (word "peer1 transaction times" [list_time_of_transaction] of peer1)
;;show (word "peer1 all transactions " [list_other_peer_id] of peer1)
;;show (word "peer1 " list_peer1_transaction_peer_ids)
;;get ids of other peers transacted with during this time interval
set counter 0
ask peer2
[
if length list_time_of_transaction > 0
[
foreach list_time_of_transaction
[
if (time_ticks > current_ticks OR ? >= (current_ticks - time_ticks))
[
set list_peer2_transaction_peer_ids lput (item counter list_other_peer_id) list_peer2_transaction_peer_ids
]
set counter counter + 1
]
]
]
;;remove duplicates
set list_peer2_transaction_peer_ids remove-duplicates list_peer2_transaction_peer_ids
;;show (word "peer2 transaction times" [list_time_of_transaction] of peer2)
;;show (word "peer2 all transactions " [list_other_peer_id] of peer2)
;;show (word "peer2 " list_peer2_transaction_peer_ids)
if (length list_peer1_transaction_peer_ids > 0 AND length list_peer2_transaction_peer_ids > 0)
[
;;determine common peers
foreach list_peer1_transaction_peer_ids
[
let temp_id ?
foreach list_peer2_transaction_peer_ids
[
if temp_id = ?
[
set list_common_transaction_peer_ids lput ? list_common_transaction_peer_ids
]
]
]
;;remove duplicates
;;set list_common_transaction_peer_ids remove-duplicates list_common_transaction_peer_ids
]
;; show (word "common " list_common_transaction_peer_ids)
report list_common_transaction_peer_ids
end
;;this method determines the similarity between two peers
;;similarity is determed by the feedback ratings provided to peers that have interacted with both peers
to-report similarity [peer1 peer2 time_ticks]
let common_list get-common-transaction-peers peer1 peer2 time_ticks
let default_similarity 0.5
let no_feedback_provided false
;;check if the common list is empty
if length common_list = 0
[
report default_similarity
]
;;variables
let list_peer1_satisfaction_ratings []
let list_peer2_satisfaction_ratings []
;;get the ids of the two peers
let peer1_id [who] of peer1
let peer2_id [who] of peer2
;;show (word "peer1 id " peer1_id)
;;show (word "peer2 id " peer2_id)
let numerator_total 0
let denominator 0
foreach common_list
[
if not (turtle ? = nobody)
[
ask turtle ?
[
let counter 0
let peer1_total_transactions 0
let peer1_satisfaction_total 0
let peer2_total_transactions 0
let peer2_satisfaction_total 0
;;show (word "other node transaction times" list_time_of_transaction)
;;to remove
let list_valid_transactions []
foreach list_time_of_transaction ;;check if transaction is in the time window
[
if (time_ticks > current_ticks OR ? >= (current_ticks - time_ticks))
[
let other_peer_id item counter list_other_peer_id
;;to remove
set list_valid_transactions lput other_peer_id list_valid_transactions
;;show (word "other_id " other_peer_id)
if (other_peer_id = peer1_id)
[
set peer1_total_transactions peer1_total_transactions + 1
set peer1_satisfaction_total peer1_satisfaction_total + item counter list_satisfaction
;;set list_peer1_satisfaction_ratings lput item counter list_satisfaction list_peer1_satisfaction_ratings ;;save satisfaction rating
]
if (other_peer_id = peer2_id)
[
set peer2_total_transactions peer2_total_transactions + 1
set peer2_satisfaction_total peer2_satisfaction_total + item counter list_satisfaction
;;set list_peer2_satisfaction_ratings lput item counter list_satisfaction list_peer2_satisfaction_ratings ;;save satisfaction rating
]
]
set counter counter + 1
]
;;check if this peer provided any feedback
ifelse not(peer1_total_transactions = 0 OR peer2_total_transactions = 0)
[
;;calculate difference for the transactions with this peer
let square_of_difference square ((peer1_satisfaction_total / peer1_total_transactions) - (peer2_satisfaction_total / peer2_total_transactions))
set numerator_total numerator_total + square_of_difference
;;set denominator denominator + abs (peer1_satisfaction_total + peer2_satisfaction_total)
]
[
set no_feedback_provided true
]
]
]
]
;;show (word "numerator " numerator_total)
;;show (word "denominator " denominator)
set denominator length common_list
;;check if any feedback is provided (numerator_total of 0 and denominator of 1 and it failed a provide feedback check = no feedback)
if (numerator_total = 0 AND denominator = 1 AND no_feedback_provided)
[
report default_similarity
]
let calculated_similarity (1 - sqrt (numerator_total / denominator))
;;show (word "similarity " calculated_similarity)
report calculated_similarity
end
;;this method calculates the total credibility (personalized similarity) for all a peers transactions
;;this method takes into account the time of the transactions - using global variables recent_time_window and adaptive_time_window passed as parameters
to-report calculate-total-trust-values-for-PSM-credibility [peer time_ticks]
let total_similarity 0.0