-
Notifications
You must be signed in to change notification settings - Fork 0
/
Services-Clusters-13.nlogo
1557 lines (1334 loc) · 32.3 KB
/
Services-Clusters-13.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
breed [ services service ]
breed [ needs need ]
breed [ clusters cluster]
;breed [ links link ]
;my-FCs: Vector which contains a number (0-1) that depicts an service's performance with a specific feature.
; *FCs are enumerated.
;W*: list of values from other services
;S*: list of coalitions from other services
;joined: the coalition the service is associated with
services-own [ my-FCs W S joined my-price]
;requirements: Vector of requirements for a need that identifies the need of each feature.
; *Size determined by num-FCs and enumerated in the same manner as my-FCs.
;coalition-in-use: As coalitions are associated with a need, they are added to this list for record-keeping
needs-own [ requirements coalition-in-use ]
clusters-own [id beta c-FCs LServices LClusters LBundles]
;L: global list of coalitions
globals [ L num-clusters RedPos BluePos GreenPos YellowPos OrangePos bg-color fg-color]
patches-own [ mem ]
to setup
;; (for this model to work with NetLogo's new plotting features,
;; __clear-all-and-reset-ticks should be replaced with clear-all at
;; the beginning of your setup procedure and reset-ticks at the end
;; of the procedure.)
__clear-all-and-reset-ticks
set RedPos -20
set BluePos -15
set GreenPos 5
set YellowPos -5
set OrangePos 15
set L []
set bg-color white ; <-------- SET BACKGROUND COLOR --------
set fg-color bg-color - 4 ; <-------- SET FOREGROUND COLOR --------
ask patches [ set pcolor bg-color ]
; Generate Clusters
set num-clusters (2 ^ num-FCs) - 1
create-clusters num-clusters
setup-clusters
create-services num-services
create-needs num-needs
setup-services
setup-needs
end
to setup-services
output-show "my-FCs:"
ask services
[
;set my-FCs build-list-randomly num-FCs 1
;set my-FCs build-list-randomly num-FCs 2
set my-FCs build-list-service num-FCs
set W []
set S []
set label who
set joined []
set shape "box"
set size 1
set color red
;Carlos:
set my-price random 500
set label-color black
;setxy -25 random-ycor
set xcor -25
set ycor (30 / num-services) * ceiling((who - num-clusters) / 2) * (-1)^((who - num-clusters))
create_my_links self my-FCs
output-show (list my-FCs "Price:" my-price)
;Add himself to L
set L lput (list who) L
]
end
to setup-needs
output-show "required FCs:"
ask needs [
set coalition-in-use []
set label coalition-in-use
;set requirements build-list-randomly num-FCs 2
set requirements build-list-service num-FCs
set shape "person"
set size 3
set color blue ;(color - 20) ;make it darker for readability
;set xy 25 random-ycor
set xcor 25
;let YPos (30 / num-needs) * ceiling((who - num-needs) / 2) * (-1)^(who)
let YPos1 (30 / num-needs)
let YPos2 ceiling(((who - (num-services + num-clusters)) - num-needs) / 2)
let Ypos3 (-1)^((who - (num-services + num-clusters)))
let Ypos (YPos1 * YPos2 * YPos3)
set ycor YPos
set label-color black
]
; We assume that there is always a need requiering all the FCs
let ThisNeed random num-needs
set ThisNeed ThisNeed + (num-clusters + num-services)
show " ALL-FEATURE NEED"
show ThisNeed
ask need ThisNeed
[
set requirements build-list-need num-FCs
show requirements
]
ask needs
[
set label requirements
output-show requirements
]
end
to setup-clusters
;output-show "clusters:"
ask clusters [
set LServices []
set LClusters []
set LBundles []
set c-FCs []
set id who + 1;- (num-needs + num-services) + 1
set label id
set hidden? true
set shape "circle"
set size 1
;setxy RedPos random-ycor
set xcor RedPos
set ycor (30 / num-clusters) * ceiling(who / 2) * (-1)^(who)
;output-show id
set label-color black
]
end
to setup-L
; Compute L
; Expand All the Clusters
ask clusters
[
let MyBundles LBundles
foreach MyBundles
[
let MyB ?
set L lput MyB L
]
]
set L remove-duplicates L
end
to-report add-to-list [ i lst ]
report lput i lst
end
to-report build-list-randomly [ sz limit ]
;lst: The list returned.
let lst 0
set lst []
ifelse (limit <= 1) [ repeat sz [ set lst lput (((random (limit * 100 - 1)) + 1) / 100) lst ] ] ;need to normalize if limit is less than 1
[ repeat sz [ set lst lput (random limit) lst ] ]
report lst
end
to-report build-list-need [ sz ]
;lst: The list returned.
let lst 0
set lst []
repeat sz [ set lst lput (1) lst ]
report lst
end
to-report build-list-service [ sz ]
;lst: The list returned.
let lst 0
let NumOnes 0
let NumZeros 0
set lst []
repeat sz
[
let MyFC random 2
ifelse(MyFC = 1)
[
set NumOnes (NumOnes + 1)
]
[
set NumZeros (NumZeros + 1)
]
if (NumOnes = sz)
[
set MyFC 0
]
if(NumZeros = sz)
[
set MyFC 1
]
set lst lput (MyFC) lst
]
report lst
end
to create_my_links [YO these-FCs]
let current_FCs 0 ;[my-FCs] of (service ?)
let sum_value 0
let index 0
ask YO
[
let ThisID who
set current_FCs these-FCs
set sum_value 0
set index 0
show current_FCs
repeat num-FCs
[
if( (item index current_FCs) > 0)
[ set sum_value (sum_value + (2 ^ index))]
set index ( index + 1 )
]
show sum_value
if (sum_value > 0)
[
create-link-to cluster (sum_value - 1)
[
set color red
]
ask cluster (sum_value - 1)
[
;let MyC count my-in-links
set hidden? false
set color red; random 255
set c-FCs current_FCs
set label c-FCs
face YO
set LServices lput ThisID LServices
set LServices remove-duplicates LServices
]
]
]
end
to combine-lower-clusters
; Code here
show " combine-lower-clusters "
show L
let Objective num-clusters
;show Objective
; Combine Lower Clusters
let MySTOP ceiling(Objective / 2)
;show MySTOP
let P1 0
let Current P1
let Previous 0
let HMany MySTOP - P1
;show P1
; New stuff to stop exploration
let Count1 0
let MySTOPCL 2
repeat HMany - 1
[
;show L
show " Exploring "
show P1
if (Current != P1)
[
set Previous P1 - 1 ;Current
set Current P1
;show Previous
let i 0
let CI Previous + 1
repeat CI
[
show i
if(0 = (overlapped i P1))
[
let CiP1 (i + P1) + 1
let LinkC random 255
ask cluster i
[
create-link-to cluster P1
[
set color LinkC
;set width 3
]
create-link-from cluster P1
[
set color LinkC
;set width 3
]
create-link-to cluster CiP1
[
set color LinkC
]
set hidden? false
;set color blue
set size 2
;setxy BluePos random-ycor
set xcor BluePos
;set ycor (30 / num-clusters) * ceiling(who / 2) * (-1)^(who)
face cluster P1
]
ask cluster P1
[
create-link-to cluster CiP1
[
set color LinkC
]
set hidden? false
;set color blue
set size 2
;setxy BluePos random-ycor
set xcor BluePos
;set ycor (30 / num-clusters) * ceiling(who / 2) * (-1)^(who)
face cluster i
]
ask cluster CiP1
[
;create-link-with cluster CiP1
set hidden? false
set color yellow
set size 2
;setxy YellowPos random-ycor
set xcor YellowPos
;set ycor (30 / num-clusters) * ceiling(who / 2) * (-1)^(who)
let MyC []
set MyC lput i MyC
set MyC lput P1 MyC
set LClusters lput MyC LClusters
set LClusters remove-duplicates LClusters
set c-FCs combine-FCs i P1
set label c-FCs
]
; Counting a new cluster
set Count1 (Count1 + 1)
]; END - if(0 = (overlapped i P1))
;show i
set i (i + 1)
]; END - repeat CI
]
set P1 (P1 + 1)
; Analyse whether we reached MySTOP
if (Count1 = MySTOPCL)
[
show " MySTOPCL reached "
show Count1
stop
]
]
end
to compute-coalitions
show L
let Objective num-clusters
let MySTOP ceiling(Objective / 2)
;show MySTOP
let P1 0
let Current P1
let Previous 0
let HMany MySTOP - P1
;
; Generate Solution Clusters
;
show "Combining Upper and Lower"
let MySTOPCL 1
let Count2 0
let MyPU (Objective - 1)
while [MyPU >= MyStop]
[
show MyPU
let A[]
let B[]
let ThisUP nobody
ask cluster MyPU
[
set ThisUP self
set A c-FCs
ifelse (empty? A)
[
set A []
; error
]
[
let PL 0
let ThisLO nobody
let HManyPL MyStop
repeat HManyPL
[
show PL
ask cluster PL
[
set B c-FCs
set ThisLO self
ifelse (empty? B)
[
set B []
; cannot be combined
]
[
if(0 = (overlapped MyPU PL))
[
; Lower
create-link-to cluster MyPU
[
;set color yellow
]
create-link-from cluster MyPU
[
;set color yellow
]
;set color green
set xcor GreenPos
ask cluster MyPU
[
;set color green
set size 2
set xcor GreenPos
]
; Combining upper with lower in MyR
let MyR MyPU + PL
set MyR (MyR + 1)
ask cluster MyR
[
set color orange
set size 2
set hidden? false
;setxy OrangePos random-ycor
set xcor OrangePos
create-link-from ThisLO
[
;set color yellow
]
create-link-from ThisUP
[
;set color yellow
]
let MyC []
set MyC lput PL MyC
set MyC lput MyPU MyC
set LClusters lput MyC LClusters
set LClusters remove-duplicates LClusters
set c-FCs combine-FCs PL MyPU
set label c-FCs
]
set Count2 (Count2 + 1)
]; END - if(0 = (overlapped MyPU PL))
]
]
set PL (PL + 1)
]
]
]
set MyPU (MyPU - 1)
; Have we reached MySTOPCL
if(Count2 = MySTOPCL)
[
show " MySTOPCL 2 has been reached - stopping exploration "
show Count2
stop
]
]; END - while [MyPU >= MyStop]
setup-L
end
to-report overlapped [Cluster-i Cluster-P1]
let A []
let B []
let MyRes 1
ask cluster (Cluster-i)
[
show c-FCs
set A c-FCs
]
ask cluster (Cluster-P1)
[
show c-FCs
set B c-FCs
]
ifelse( (empty? A) or (empty? B) )
[
set MyRes 1
]
[
let OP (map [?1 + ?2 = 2] A B)
show OP
ifelse (member? true OP)
[
set MyRes 1
]
[
set MyRes 0
]
]
report MyRes
end
to-report combine-FCs [Cluster-i Cluster-P1]
let MyC (Cluster-i + Cluster-P1) + 1
let A []
let B []
let CF []
let MyRes 1
let S1 []
let S2 []
let B1 []
let B2 []
ask cluster (Cluster-i)
[
show c-FCs
set A c-FCs
set S1 LServices
set B1 LBundles
]
ask cluster (Cluster-P1)
[
show c-FCs
set B c-FCs
set S2 LServices
set B2 LBundles
]
; Combining services with services
let MyBundle []
foreach S1
[
let MyS1 ?
foreach S2
[
set MyBundle []
let MyS2 ?
set MyBundle lput MyS1 MyBundle
set MyBundle lput MyS2 MyBundle
set MyBundle sort MyBundle
;set MyBundles lput MyBundle MyBundles
ask cluster MyC
[
set LBundles lput MyBundle LBundles
]
]
]
; Combining S1 with B2
foreach S1
[
let MyS1 ?
foreach B2
[
;set MyBundle []
let MyB2 ?
set MyB2 lput MyS1 MyB2
set MyB2 sort MyB2
ask cluster MyC
[
set LBundles lput MyB2 LBundles
]
]
]
; Combining S2 with B1
foreach S2
[
let MyS2 ?
foreach B1
[
;set MyBundle []
let MyB1 ?
set MyB1 lput MyS2 MyB1
set MyB1 sort MyB1
ask cluster MyC
[
set LBundles lput MyB1 LBundles
]
]
]
; Combining B1 with B2
foreach B1
[
let MyB1 ?
foreach B2
[
let MyB2 ?
; add element by element the services in MyB1
foreach MyB1
[
let MySubB1 ?
set MyB2 lput MySubB1 MyB2
]
set MyB2 sort MyB2
ask cluster MyC
[
set LBundles lput MyB2 LBundles
]
]
]
set LBundles remove-duplicates LBundles
set CF (map [?1 + ?2] A B)
show CF
report CF
end
;
;
; FIND ANSWER TO OUR NEED
;
to find-co
;foreach ([who] of needs) [ ask services [ find-coalition who ? ] ]
ask needs
[
set coalition-in-use []
let ThisNeed self
let SolBundles []
let N_FCs requirements
;ask cluster ((2 ^ num-FCs) - 2)
;[
; set SolBundles lbundles
;]
ask clusters
[
let ThisCluster who
let C_FCs c-FCs
if(N_FCs = C_FCs)
[
ifelse(empty? lbundles)
[
ifelse(empty? lservices)
[
]
[
;set SolBundles lservices
; Create a list for each service
foreach lservices
[
let ThisSer ?
let NewL []
set NewL lput ThisSer NewL
set SolBundles lput NewL SolBundles
]
create-link-to ThisNeed
[
set color Blue
]
face ThisNeed
set Size 3
forward 5
]
]
[
set SolBundles lbundles
create-link-to ThisNeed
[
set color Blue
]
face ThisNeed
set size 3
forward 5
]
]
]
set coalition-in-use SolBundles
ifelse(empty? coalition-in-use)
[
set color red
]
[
;output-show coalition-in-use
display-coalitions who
]
]
end
to display-coalitions [ThisN]
output-show "Best bundles (based on price):"
ask need ThisN
[
;output-show (list my-FCs "Price:" my-price)
let Coalitions-Prices []
let SumPrices 0
let ThisC-Price []
let TempL1 []
let TempL2 []
foreach coalition-in-use
[
set ThisC-Price []
set SumPrices 0
let ServicesinC ?
foreach ServicesinC
[
let ThisService ?
ask service ThisService
[
set SumPrices (my-price + SumPrices)
]
]
set ThisC-Price lput SumPrices ThisC-Price
set ThisC-Price lput ServicesinC ThisC-Price
set TempL1 []
set TempL2 []
;output-show "ThisC-Price : "
;output-show ThisC-Price
ifelse (empty? Coalitions-Prices)
[
set TempL1 lput ThisC-Price TempL1
;output-show "First TempL1 : "
;output-show TempL1
]
[
; insert ThisC-Price in the right position
; it can be inserted at the beginning, somewhere in the middle or at the end.
let Ban 0
foreach Coalitions-Prices
[
let ThisCoalition ?
let ThisValue item 0 ThisCoalition
;output-show "Comparing : "
;output-show SumPrices
;output-show ThisValue
; Inserting the new coalition at the beginning
ifelse( (SumPrices < ThisValue) and (Ban = 0) )
[
set TempL1 lput ThisC-Price TempL1
set TempL2 lput ThisCoalition TempL2
set Ban 1
]
[
ifelse(Ban = 1) ; ThisC-Price has been inserted already
[
set TempL2 lput ThisCoalition TempL2
]
[
set TempL1 lput ThisCoalition TempL1
]
]
;
]
; Inserting ThisC-Price at the end
if (Ban = 0)
[
set TempL2 lput ThisC-Price TempL2
]
]
;output-show (list ServicesinC "Price:" SumPrices)
;output-show "TempL1 : "
;output-show TempL1
;output-show "TempL2 : "
;output-show TempL2
ifelse(empty? TempL2)
[]
[
foreach TempL2
[
let ThisL2 ?
set TempL1 lput ThisL2 TempL1
]
]
set Coalitions-Prices TempL1
;output-show Coalitions-Prices
]
foreach Coalitions-Prices
[
let MyCoalition ?
let MyPriceC item 0 MyCoalition
let MyServices item 1 MyCoalition
output-show (list MyServices "Price:" MyPriceC)
]
]
end
;find-coalition algorithm.
to find-coalition [ service-i need_i ]
;L_i: list of coalitions containing service-i
;Max_i: [S_i* w_i*] retrieved after evaluating all coalitions in L_i
;w_max: the maximum value in W*
;S_max: the coalition corrisponding to w_max (W*)
let L_i 0
let Max_i 0
let w_max 0
let S_max 0
set L_i coalitions service-i
; Remove coalitions that do not match needs
set L_i coalitions-matching L_i service-i need_i
show L_i
;output-show (list " L_i : " L_i)
ask need need_i
[
foreach L_i
[
let ThisC ?
;set color random 255
set coalition-in-use lput ThisC coalition-in-use
;set label coalition-in-use
]
set coalition-in-use remove-duplicates coalition-in-use
]
end
;Fetches the coalitions that contain member i
to-report coalitions [ i ]
;lst: the list returned
let lst 0
set lst []
foreach L [ if( member? i ? ) [ set lst lput ? lst ] ]
report lst
end
;Keep coalitions matching the j need
to-report coalitions-matching [ MyCoalitions i j]
;lst: the list returned
let lst 0
show " Filtering Service"
show i
show MyCoalitions
set lst []
;foreach L [ if( member? i ? ) [ set lst lput ? lst ] ]
foreach MyCoalitions
[
let ThisCoalition ?
let CombinedFCs []
show ThisCoalition
repeat num-FCs
[
set CombinedFCs lput 0 CombinedFCs
]
;show " Elements "
foreach ThisCoalition
[
let ThisService ?
;let ThisFCs []
;show ThisService
ask service ThisService
[
set CombinedFCs (map [?1 + ?2] CombinedFCs my-FCs)
]
]
let ThisNeed []
ask need j
[
set ThisNeed requirements
]
let Res (map [?1 = ?2] CombinedFCs ThisNeed)
show " Results "
show Res
ifelse (member? false Res)
[]
[
show " Valid coalition "
set lst lput ThisCoalition lst
]
]
report lst
end
;Fetches the coalition with the highest value in a given set
;c_lst: list of possible coalitions
;need_i: current need
to-report max-coalition [ c_lst need_i ]
;maximum-value: v(S*)
;maximum-col: S*
;sum_vector: temporary space to hold the sum of my-FCs for each service in the coalition
;index: for moving through the elements in the my-FCs vector
let maximum-value 0
let maximum-col 0
let sum_vector 0
let sum_prices 0
let current_skills 0
let current_price 0
let index 0
set maximum-value -1000 ;preset low
foreach c_lst [ ;iterate through each coalition
set sum_vector []
set sum_prices 0
repeat num-FCs [ set sum_vector lput 0 sum_vector ] ;reset summation vector
;fetch the my-FCs vector from each service
foreach ? [
set index 0
set current_skills [my-FCs] of (service ?)
set current_price [my-price] of (service ?)
show "---Current Price"
show current_price
;sum up prices
set sum_prices (sum_prices + current_price)
repeat num-FCs [ ;sum up the values in the my-FCs vectors for each service in the current coalition
set sum_vector replace-item index sum_vector precision ( (item index sum_vector) + (item index current_skills) ) 2
set index ( index + 1 ) ] ]
set index 0
show "-------SumPrices:"
show sum_prices