-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFM_HWdeterministic.xml
More file actions
1233 lines (1082 loc) · 40.5 KB
/
Copy pathFM_HWdeterministic.xml
File metadata and controls
1233 lines (1082 loc) · 40.5 KB
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE nta PUBLIC '-//Uppaal Team//DTD Flat System 1.6//EN' 'http://www.it.uu.se/research/group/darts/uppaal/flat-1_6.dtd'>
<nta>
<declaration>// Place global declarations here.
// Broadcast Channels
broadcast chan Initialized;
clock global;
// Content type
const int U = 4; //Drone's next move is UP
const int D = 1; //Drone's next move is DOWN
const int L = 2; //Drone's next move is LEFT
const int R = 3; //Drone's next move is RIGHT
const int CIVILIAN = 1;
const int FIRE = 2;
const int EXIT = 3;
const int FIRST_RESPONDER = 4;
const int NEEDY_CIV = 5;
const int RESCUING = 6;
const int STEP_TIME = 1;
const int DEFAULT = -1;
const int OUT_ROW = -1;
// Parameters
const int GRID_WIDTH = 14;
const int GRID_HEIGHT = 14;
const int FIRE_NUM = 23;
const int EXIT_NUM = 4;
const int CIVILIAN_NUMBER = 8;
const int FIRST_RESPONDER_NUMBER = 3;
const int DRONE_NUM = 4;
const int PATH_LEN = 32;
const int FR_PATH_LEN = 36;
const int Tfrs[FIRST_RESPONDER_NUMBER] = {1,1,1};
const int Tzr[CIVILIAN_NUMBER] = {3,2,2,4,2,3,3,2};
const int Tv[CIVILIAN_NUMBER] = {4,2,5,3,2,6,3,1};
const int Nv[DRONE_NUM] = {1,1,1,1};
// Structs
typedef struct {
int[-1,GRID_HEIGHT-1] row;
int[-1,GRID_WIDTH-1] col;
} position_t;
typedef struct {
int[1,4] d; //direction
position_t p; //position
} direction_t;
typedef int[1, 4] initial_direction_t;
const position_t fires_positions[FIRE_NUM] = { {6,5}, {6,6}, {7,6}, {7,7}, {8,7}, {8,8}, {9,8}, {6,7}, {7,8}, {5,7}, {5,8}, {4,9}, {5,9}, {8,9}, {5,5}, {5,4}, {5,6}, {4,6}, {3,6}, {7,5}, {8,5}, {8,6}, {9,6} };
const position_t exits_positions[EXIT_NUM] = { {7,0}, {7,13}, {0,7}, {13,7} };
position_t first_responders_positions[FIRST_RESPONDER_NUMBER] = { {8,11}, {9,3}, {2,8} };
position_t civilians_positions[CIVILIAN_NUMBER] = { {5,10}, {4,4}, {9,5}, {1,1}, {13,13}, {10,2}, {1,10}, {12,10} };
const direction_t drones_path[PATH_LEN] = {{D,{8,3}}, {D,{9,3}}, {R,{10,3}}, {R,{10,4}}, {U,{10,5}}, {U,{9,5}}, {L,{8,5}}, {L,{8,4}}, {D,{8,8}}, {D,{9,8}}, {R,{10,8}}, {R,{10,9}}, {U,{10,10}}, {U,{9,10}}, {L,{8,10}}, {L,{8,9}}, {D,{3,8}}, {D,{4,8}}, {R,{5,8}}, {R,{5,9}}, {U,{5,10}}, {U,{4,10}}, {L,{3,10}}, {L,{3,9}}, {D,{3,3}}, {D,{4,3}}, {R,{5,3}}, {R,{5,4}}, {U,{5,5}}, {U,{4,5}}, {L,{3,5}}, {L,{3,4}}};
position_t drones_positions[DRONE_NUM] = {{8,3}, {8,8}, {3,10}, {3,3}};
//const direction_t first_responders_path[FR_PATH_LEN] = {{D,{2,5}}, {D,{3,5}}, {D,{4,5}}, {D,{5,5}}, {D,{6,5}}, {L,{7,5}}, {U,{7,4}}, {U,{6,4}}, {U,{5,4}}, {U,{4,4}}, {U,{3,4}}, {R,{2,4}}};
const direction_t first_responders_path[FR_PATH_LEN] = {{D,{4,2}}, {D,{5,2}}, {D,{6,2}}, {R,{7,2}}, {D,{7,3}}, {D,{8,3}}, {D,{9,3}}, {D,{10,3}}, {R,{11,3}}, {R,{11,4}}, {R,{11,5}}, {R,{11,6}}, {R,{11,7}}, {R,{11,8}}, {R,{11,9}}, {U,{11,10}}, {R,{10,10}}, {U,{10,11}}, {U,{9,11}}, {U,{8,11}}, {U,{7,11}}, {U,{6,11}}, {U,{5,11}}, {U,{4,11}}, {U,{3,11}}, {L,{2,11}}, {L,{2,10}}, {L,{2,9}}, {L,{2,8}}, {L,{2,7}}, {L,{2,6}}, {L,{2,5}}, {L,{2,4}}, {L,{2,3}}, {D,{2,2}}, {D,{3,2}}};
// Id types
typedef int[0, CIVILIAN_NUMBER-1] civilian_t;
typedef int[0, FIRST_RESPONDER_NUMBER -1] firstResponder_t;
typedef int[0, DRONE_NUM - 1] drone_t;
// Special Channels
broadcast chan Civilian_Saved[CIVILIAN_NUMBER];
broadcast chan drone_signal;
broadcast chan civilian_signal;
// Outcomes of the simulation
int[0,CIVILIAN_NUMBER] casualties = 0; //Numbcer of dead civilians.
int[0,CIVILIAN_NUMBER] rescued_civilians = 0;
// Data structures---------------------------------------
int[0,6] grid[GRID_HEIGHT][GRID_WIDTH];
meta int path[GRID_HEIGHT][GRID_WIDTH];
int fr_path[GRID_HEIGHT][GRID_WIDTH];
int [-1,CIVILIAN_NUMBER-1] go_save_zr[CIVILIAN_NUMBER]; //in each position we have the id of the civilian to save.
int[-1,CIVILIAN_NUMBER-1] go_save_fr[FIRST_RESPONDER_NUMBER];
int[-1,FIRST_RESPONDER_NUMBER-1] go_call[CIVILIAN_NUMBER]; //Similarly to others above, it is indexed by civilian to "tell" each civilian which fr to summon. Used alongside the omonimous channel.
// int summoner[FIRST_RESPONDER_NUMBER] = {DEFAULT}; //Meant for FRs to know the civilian who summoned them
// Methods
void setFire(){ //Place fire
int k=0;
for(k=0; k<FIRE_NUM;k++){
grid[fires_positions[k].row][fires_positions[k].col] = FIRE;
}
}
void setExits(){ //Place exits
int k=0;
for(k=0; k<EXIT_NUM;k++){
grid[exits_positions[k].row][exits_positions[k].col] = EXIT;
}
}
void setDronesPath(){ //design drones' path(s)
int k=0;
for(k=0; k<PATH_LEN;k++){
path[drones_path[k].p.row][drones_path[k].p.col] = drones_path[k].d;
}
}
void setFirstRespondersPath(){
int i=0;
for(i=0;i<FR_PATH_LEN;i++){
fr_path[first_responders_path[i].p.row][first_responders_path[i].p.col] = first_responders_path[i].d;
}
}
int manhattan_distance(position_t a, position_t b) {
return abs(a.row - b.row)+abs(a.col - b.col);
}
int getFRPath(position_t pos){
// Since matrix is initilized to 0 by default, will return 0 is fr not on path
return fr_path[pos.row][pos.col];
}
int getPath(position_t pos){
return path[pos.row][pos.col];
}
</declaration>
<template>
<name>Civilian</name>
<parameter>const civilian_t id</parameter>
<declaration>clock x;
// const int local_Tzr = Tzr[id];
// const int local_Tv = Tv[id];
// int local_Tfr = 0;
int wait;
meta bool allowed[5];
bool isNextToFire(){
position_t pos = civilians_positions[id];
int start_row, start_col;
int end_row, end_col;
int i=0;
int j=0;
start_row = pos.row > 0 ? pos.row-1 : pos.row;
end_row = pos.row < GRID_HEIGHT-1 ? pos.row+1 : pos.row;
start_col = pos.col > 0 ? pos.col-1 : pos.col;
end_col = pos.col < GRID_WIDTH-1 ? pos.col+1 : pos.col;
for(i=start_row;i<=end_row; i++){
for(j=start_col;j<=end_col;j++){
/*if( !(i == pos.row && j == pos.col) && grid[i][j]==EXIT ){
return false;
}*/
if( !(i == pos.row && j == pos.col) && grid[i][j]==FIRE ){
return true;
}
}
}
return false;
}
bool isNextToExit(){
position_t pos = civilians_positions[id];
int start_row, start_col;
int end_row, end_col;
int i=0;
int j=0;
start_row = pos.row > 0 ? pos.row-1 : pos.row;
end_row = pos.row < GRID_HEIGHT-1 ? pos.row+1 : pos.row;
start_col = pos.col > 0 ? pos.col-1 : pos.col;
end_col = pos.col < GRID_WIDTH-1 ? pos.col+1 : pos.col;
for(i=start_row;i<=end_row; i++){
for(j=start_col;j<=end_col;j++){
if( !(i == pos.row && j == pos.col) && grid[i][j]==EXIT ){
return true;
}
}
}
return false;
}
bool isCloserToExit(int new){
position_t old_pos = civilians_positions[id];
position_t pos = civilians_positions[id];
position_t closest_exit;
int i;
closest_exit = exits_positions[0];
for(i = 1; i< EXIT_NUM;i++){
if(manhattan_distance(pos,exits_positions[i]) < manhattan_distance(pos,closest_exit)){
closest_exit = exits_positions[i];
}
}
if(new == U){
pos.row--;
}else if (new == D){
pos.row++;
}else if (new == R){
pos.col++;
}else if (new == L){
pos.col--;
}
return (manhattan_distance(old_pos,closest_exit)>manhattan_distance(pos,closest_exit));
}
bool CanGoRight(){
position_t pos = civilians_positions[id];
return ((pos.col < GRID_WIDTH-1) && grid[pos.row][pos.col + 1]==0 && isCloserToExit(R));
}
bool CanGoLeft(){
position_t pos = civilians_positions[id];
return ((pos.col > 0) && grid[pos.row][pos.col - 1]==0 && isCloserToExit(L));
}
bool CanGoUp(){
position_t pos = civilians_positions[id];
return ((pos.row > 0) && grid[pos.row - 1][pos.col]==0 && isCloserToExit(U));
}
bool CanGoDown(){
position_t pos = civilians_positions[id];
return ((pos.row < GRID_HEIGHT-1) && grid[pos.row + 1][pos.col]==0 && isCloserToExit(D));
}
bool CantMove(){
return (!(CanGoRight() || CanGoLeft() || CanGoUp() || CanGoDown()));
}
void become_needy(){
position_t pos = civilians_positions[id];
grid[pos.row][pos.col] = NEEDY_CIV;
}
void Move_Right(){
position_t pos = civilians_positions[id];
grid[pos.row][pos.col] = 0;
pos.col++;
grid[pos.row][pos.col] = CIVILIAN;
civilians_positions[id].col++;
}
void Move_Left(){
position_t pos = civilians_positions[id];
grid[pos.row][pos.col] = 0;
pos.col--;
grid[pos.row][pos.col] = CIVILIAN;
civilians_positions[id].col--;
}
void Move_Up(){
position_t pos = civilians_positions[id];
grid[pos.row][pos.col] = 0;
pos.row--;
grid[pos.row][pos.col] = CIVILIAN;
civilians_positions[id].row--;
}
void Move_Down(){
position_t pos = civilians_positions[id];
grid[pos.row][pos.col] = 0;
pos.row++;
grid[pos.row][pos.col] = CIVILIAN;
civilians_positions[id].row++;
}
bool if_receiver() {
return (go_save_zr[id] != DEFAULT || go_call[id] != DEFAULT);
}
bool if_zr() {
return (go_save_zr[id] != DEFAULT);
}
bool if_fr() {
return (go_call[id] != DEFAULT);
}
//wait time is computed using the data stored in the pos array. Don't modify those before.
int waitTime() {
int fr_id;
int needy_id;
int civ_2_fr;
int fr_2_needy;
int civ_2_needy;
int rescue_time;
//wait the time necessary for the civilian to reach the FR and the FR to go to the Needy and the rescue time of the FR
if(if_fr()) {
fr_id = go_call[id];
needy_id = go_save_fr[fr_id];
civ_2_fr = manhattan_distance(civilians_positions[id], first_responders_positions[fr_id]);
fr_2_needy = manhattan_distance(first_responders_positions[fr_id], civilians_positions[needy_id]);
rescue_time = Tfrs[fr_id];
return civ_2_fr + fr_2_needy + rescue_time;
}
//wait the time necessary for the zr to reach the needy civilian and rescue him
else {
needy_id = go_save_zr[id];
civ_2_needy = manhattan_distance(civilians_positions[id], civilians_positions[needy_id]);
rescue_time = Tzr[id];
return civ_2_needy + rescue_time;
}
}
void remove_from_pos_array() {
civilians_positions[id].row = OUT_ROW;
}
void remove_from_grid() {
if(grid[civilians_positions[id].row][civilians_positions[id].col] == FIRST_RESPONDER){
grid[civilians_positions[id].row][civilians_positions[id].col] = FIRST_RESPONDER; // FR has teleported
}else{
grid[civilians_positions[id].row][civilians_positions[id].col] = 0; // died, leave grid, FR won't teleport here
}
//grid[civilians_positions[id].row][civilians_positions[id].col] = 0;
}
void clean_variables() {
//forse non serve
// Can safely erase both. At each time one of the two will be redundant based on which edge this method was called on, but no problem.
go_save_zr[id] = DEFAULT;
go_call[id] = DEFAULT;
wait=0;
}
</declaration>
<location id="id0" x="-1198" y="-1003">
</location>
<location id="id1" x="-1198" y="-578">
<name x="-1223" y="-613">Active</name>
<label kind="invariant" x="-1164" y="-613">x <= STEP_TIME</label>
</location>
<location id="id2" x="-2057" y="-535">
<name x="-2083" y="-518">Exited</name>
</location>
<location id="id3" x="-1589" y="-544">
<name x="-1640" y="-578">Needy</name>
<label kind="invariant" x="-1640" y="-527">x <= Tv[id]</label>
</location>
<location id="id4" x="-1428" y="-348">
<label kind="invariant" x="-1462" y="-391">x <= wait</label>
</location>
<location id="id5" x="-1198" y="-807">
<committed/>
</location>
<location id="id6" x="-1258" y="-289">
<committed/>
</location>
<init ref="id0"/>
<transition id="id7">
<source ref="id4"/>
<target ref="id2"/>
<label kind="guard" x="-1955" y="-476">(!if_fr() && civilians_positions[go_save_zr[id]].row==OUT_ROW)</label>
<label kind="assignment" x="-1836" y="-450">remove_from_pos_array(), clean_variables(),
rescued_civilians++</label>
<nail x="-1428" y="-459"/>
<nail x="-2023" y="-459"/>
</transition>
<transition id="id8">
<source ref="id6"/>
<target ref="id4"/>
<label kind="guard" x="-1385" y="-374">!if_fr()</label>
<nail x="-1258" y="-348"/>
</transition>
<transition id="id9">
<source ref="id6"/>
<target ref="id4"/>
<label kind="guard" x="-1360" y="-314">if_fr()</label>
<label kind="synchronisation" x="-1402" y="-280">civilian_signal!</label>
<nail x="-1377" y="-289"/>
</transition>
<transition id="id10">
<source ref="id1"/>
<target ref="id6"/>
<label kind="guard" x="-1198" y="-399">if_receiver()</label>
<label kind="synchronisation" x="-1190" y="-374">drone_signal?</label>
<label kind="assignment" x="-1207" y="-348">x=0, remove_from_grid(),
wait=waitTime()</label>
<nail x="-1198" y="-289"/>
</transition>
<transition id="id11">
<source ref="id1"/>
<target ref="id5"/>
<label kind="guard" x="-722" y="-467">CantMove() && x >= STEP_TIME</label>
<nail x="-1198" y="-450"/>
<nail x="-476" y="-450"/>
<nail x="-476" y="-807"/>
</transition>
<transition id="id12">
<source ref="id0"/>
<target ref="id5"/>
<label kind="synchronisation" x="-1190" y="-960">Initialized?</label>
<nail x="-1198" y="-892"/>
</transition>
<transition id="id13">
<source ref="id5"/>
<target ref="id1"/>
<label kind="guard" x="-1190" y="-747">!isNextToFire() && !isNextToExit()</label>
<label kind="assignment" x="-1190" y="-688">x = 0</label>
</transition>
<transition id="id14">
<source ref="id4"/>
<target ref="id2"/>
<label kind="guard" x="-1980" y="-255">x >= wait && if_fr()</label>
<label kind="assignment" x="-2099" y="-229">remove_from_pos_array(), clean_variables(), rescued_civilians++</label>
<nail x="-1428" y="-238"/>
<nail x="-2082" y="-238"/>
<nail x="-2082" y="-493"/>
</transition>
<transition id="id15">
<source ref="id4"/>
<target ref="id2"/>
<label kind="guard" x="-2023" y="-365">x >= wait && !if_fr()</label>
<label kind="synchronisation" x="-2023" y="-382">Civilian_Saved[go_save_zr[id]]!</label>
<label kind="assignment" x="-2023" y="-348">remove_from_pos_array(), clean_variables(),
rescued_civilians++</label>
<nail x="-2040" y="-348"/>
<nail x="-2040" y="-450"/>
</transition>
<transition id="id16">
<source ref="id3"/>
<target ref="id2"/>
<label kind="synchronisation" x="-1895" y="-705">Civilian_Saved[id]?</label>
<label kind="assignment" x="-1946" y="-671">remove_from_grid(), remove_from_pos_array(),
rescued_civilians++</label>
<nail x="-1589" y="-680"/>
<nail x="-1946" y="-680"/>
</transition>
<transition id="id17">
<source ref="id1"/>
<target ref="id5"/>
<label kind="guard" x="-1156" y="-501">CanGoUp() && x >= STEP_TIME</label>
<label kind="assignment" x="-892" y="-501">Move_Up()</label>
<nail x="-1181" y="-484"/>
<nail x="-535" y="-484"/>
<nail x="-535" y="-807"/>
</transition>
<transition id="id18">
<source ref="id1"/>
<target ref="id5"/>
<label kind="guard" x="-1164" y="-526">CanGoDown() && x >= STEP_TIME</label>
<label kind="assignment" x="-901" y="-526">Move_Down()</label>
<nail x="-1164" y="-509"/>
<nail x="-637" y="-509"/>
<nail x="-637" y="-807"/>
</transition>
<transition id="id19">
<source ref="id1"/>
<target ref="id5"/>
<label kind="guard" x="-918" y="-560">CanGoLeft() && x >= STEP_TIME</label>
<label kind="assignment" x="-1011" y="-560">Move_Left()</label>
<nail x="-1079" y="-535"/>
<nail x="-654" y="-535"/>
<nail x="-654" y="-807"/>
<nail x="-1122" y="-807"/>
</transition>
<transition id="id20">
<source ref="id1"/>
<target ref="id5"/>
<label kind="guard" x="-1096" y="-824">CanGoRight() && x >= STEP_TIME</label>
<label kind="assignment" x="-1096" y="-841">Move_Right()</label>
<nail x="-773" y="-569"/>
<nail x="-773" y="-807"/>
</transition>
<transition id="id21">
<source ref="id3"/>
<target ref="id2"/>
<label kind="guard" x="-1904" y="-569">x >= Tv[id]</label>
<label kind="assignment" x="-1963" y="-544">remove_from_grid(), remove_from_pos_array(),
casualties++</label>
<nail x="-1861" y="-544"/>
</transition>
<transition id="id22">
<source ref="id5"/>
<target ref="id3"/>
<label kind="guard" x="-1538" y="-612">isNextToFire() && !isNextToExit()</label>
<label kind="assignment" x="-1539" y="-586">become_needy(),
x = 0</label>
<nail x="-1258" y="-756"/>
<nail x="-1258" y="-586"/>
<nail x="-1539" y="-586"/>
</transition>
<transition id="id23">
<source ref="id5"/>
<target ref="id2"/>
<label kind="guard" x="-1581" y="-833">isNextToExit()</label>
<label kind="assignment" x="-1802" y="-799">remove_from_grid(), remove_from_pos_array(), rescued_civilians++</label>
<nail x="-1997" y="-807"/>
</transition>
</template>
<template>
<name>Drone</name>
<parameter>const drone_t id</parameter>
<declaration>clock x;
int needy_id;
//what if in the area of sight of the drone there are multiple civilians?
void moveDrone(){ //Moves the drone according to the path configured in the global declarations
int next;
next = getPath(drones_positions[id]);
if(next == 4){
drones_positions[id].row--;
}
else if(next == 1){
drones_positions[id].row++;
}
else if(next == 2){
drones_positions[id].col--;
}
else if(next == 3){
drones_positions[id].col++;
}
return;
}
bool if_pair() {
int start_row, start_col;
int end_row, end_col;
bool needyInRange = false;
bool civilianInRange = false;
int i = 0;
int j = 0;
int k = 0;
int local_Nv = Nv[id];
position_t position;
//get the current drone position
position = drones_positions[id];
//define visibility area of the drone
start_row = position.row > local_Nv ? position.row-local_Nv : 0;
end_row = position.row < GRID_HEIGHT-local_Nv ? position.row+local_Nv : GRID_HEIGHT-1;
start_col = position.col > local_Nv ? position.col-local_Nv : 0;
end_col = position.col < GRID_WIDTH-local_Nv ? position.col+local_Nv : GRID_WIDTH-1;
//scan visibility area to find a pair composed by a civilian and a needy
for(i=start_row;i<=end_row && !(needyInRange && civilianInRange); i++){
for(j=start_col;j<=end_col && !(needyInRange && civilianInRange);j++){
if(grid[i][j]==NEEDY_CIV){
needyInRange = true;
}
if(grid[i][j]==CIVILIAN) {
civilianInRange = true;
}
}
}
return (needyInRange && civilianInRange);
}
int find_civ_id(int row, int col) {
int k;
for(k = 0; k < CIVILIAN_NUMBER; k++){
if(civilians_positions[k].row == row && civilians_positions[k].col == col && go_call[k]==-1 && go_save_zr[k]==-1){
return k;
}
}
return -1;
}
void assign_task_and_remove_involved() {
position_t needy_pos = {-1,-1};
position_t civ_pos = {-1,-1};
int start_row, start_col;
int end_row, end_col;
bool needyInRange = false;
bool civilianInRange = false;
bool found = false;
int i = 0;
int j = 0;
int k = 0;
int local_Nv = Nv[id];
int fr_id;
int civ_id;
// int needy_id;
position_t cur_fr_pos = {-1,-1};
position_t best_fr_pos = {-1,-1};
int cur_manhattan;
int best_manhattan;
position_t drone_pos = {-1,-1};
drone_pos = drones_positions[id];
//define visibility area of the drone
start_row = drone_pos.row > local_Nv ? drone_pos.row-local_Nv : 0;
end_row = drone_pos.row < GRID_HEIGHT-local_Nv ? drone_pos.row+local_Nv : GRID_HEIGHT-1;
start_col = drone_pos.col > local_Nv ? drone_pos.col-local_Nv : 0;
end_col = drone_pos.col < GRID_WIDTH-local_Nv ? drone_pos.col+local_Nv : GRID_WIDTH-1;
//scan visibility area to find the pair composed by a civilian and a needy (there will always been one since the method is called
//after we verified that there is one)
for(i=start_row;i<=end_row && !(needyInRange && civilianInRange); i++){
for(j=start_col;j<=end_col && !(needyInRange && civilianInRange);j++){
if(grid[i][j]==NEEDY_CIV){
needy_pos.row = i;
needy_pos.col = j;
needyInRange = true;
}
if(grid[i][j]==CIVILIAN) {
civ_pos.row = i;
civ_pos.col = j;
civilianInRange = true;
}
}
}
//find the id of the selected civilian and needy
civ_id = find_civ_id(civ_pos.row, civ_pos.col);
needy_id = find_civ_id(needy_pos.row, needy_pos.col);
//look for FRs in the visibility area and select the one with the min manhattan distance
fr_id = 0;
for(k=0; k<FIRST_RESPONDER_NUMBER; k++){
cur_fr_pos = first_responders_positions[k];
best_fr_pos = first_responders_positions[fr_id];
if(cur_fr_pos.row >= start_row && cur_fr_pos.row <= end_row && cur_fr_pos.col >= start_col && cur_fr_pos.col <= end_col && grid[first_responders_positions[k].row][first_responders_positions[k].col] == FIRST_RESPONDER){
found = true;
cur_manhattan = manhattan_distance(civ_pos, cur_fr_pos) + manhattan_distance(cur_fr_pos, needy_pos);
best_manhattan = manhattan_distance(civ_pos, best_fr_pos) + manhattan_distance(best_fr_pos, needy_pos);
fr_id = cur_manhattan < best_manhattan ? k : fr_id;
}
}
//if the FR has been found, so instruct the civilian to call the FR
if(found) {
go_save_fr[fr_id] = needy_id;
go_call[civ_id] = fr_id;
// summoner[fr_id] = civ_id;
grid[first_responders_positions[fr_id].row][first_responders_positions[fr_id].col] = RESCUING;
grid[civilians_positions[civ_id].row][civilians_positions[civ_id].col] = RESCUING;
grid[civilians_positions[needy_id].row][civilians_positions[needy_id].col] = RESCUING;
}
//the FR has not been found so instruct hte civilian to become a zero responder
else {
go_save_zr[civ_id] = needy_id;
grid[civilians_positions[civ_id].row][civilians_positions[civ_id].col] = RESCUING;
grid[civilians_positions[needy_id].row][civilians_positions[needy_id].col] = RESCUING;
}
//in any case set the needy civilian and the civilian outside of the map
}
</declaration>
<location id="id24" x="153" y="-433">
</location>
<location id="id25" x="-408" y="-187">
<committed/>
</location>
<location id="id26" x="-42" y="-433">
<label kind="invariant" x="-127" y="-459">x<=STEP_TIME</label>
</location>
<location id="id27" x="-212" y="-314">
<committed/>
</location>
<init ref="id24"/>
<transition id="id28">
<source ref="id27"/>
<target ref="id26"/>
<label kind="synchronisation" x="-170" y="-340">drone_signal!</label>
</transition>
<transition id="id29">
<source ref="id24"/>
<target ref="id26"/>
<label kind="synchronisation" x="17" y="-459">Initialized?</label>
<label kind="assignment" x="-24" y="-433">x=0</label>
</transition>
<transition id="id30">
<source ref="id25"/>
<target ref="id26"/>
<label kind="guard" x="-221" y="-187">!if_pair()</label>
<label kind="assignment" x="-204" y="-170">x=0</label>
<nail x="-42" y="-187"/>
</transition>
<transition id="id31">
<source ref="id25"/>
<target ref="id27"/>
<label kind="guard" x="-331" y="-280">if_pair()</label>
<label kind="assignment" x="-391" y="-255">assign_task_and_remove_involved(), x=0</label>
</transition>
<transition id="id32">
<source ref="id26"/>
<target ref="id25"/>
<label kind="guard" x="-348" y="-467">x >= STEP_TIME</label>
<label kind="assignment" x="-340" y="-450">moveDrone()</label>
<nail x="-408" y="-433"/>
</transition>
</template>
<template>
<name>FirstResponder</name>
<parameter>const firstResponder_t id</parameter>
<declaration>int[0,CIVILIAN_NUMBER-1] needy_id;
bool isNextToNeedy = false;
int wait;
clock x;
// position_t closest_fire;
//int distance = 0;
bool if_receiver() {
// return (summoner[id] != DEFAULT);
return go_save_fr[id] != DEFAULT;
}
//Check FR's own position and set booleans according to the surroundings. Sets the needy civilian to RESCUING state.
void checkPosition(){
int start_row, start_col;
int end_row, end_col;
bool break = false;
int i = 0;
int j = 0;
int k = 0;
position_t position = {-1,-1};
position = first_responders_positions[id];
grid[position.row][position.col]=FIRST_RESPONDER;
start_row = position.row > 0 ? position.row-1 : position.row;
end_row = position.row < GRID_HEIGHT-1 ? position.row+1 : position.row;
start_col = position.col > 0 ? position.col-1 : position.col;
end_col = position.col < GRID_WIDTH-1 ? position.col+1 : position.col;
for(i=start_row;i<=end_row && !break; i++){
for(j=start_col;j<=end_col && !break;j++){
if( !(i == position.row && j == position.col) ){
if(grid[i][j]==NEEDY_CIV){
isNextToNeedy = true;
grid[i][j]=RESCUING;
for(k = 0; k < CIVILIAN_NUMBER; k++){
if(civilians_positions[k].row == i && civilians_positions[k].col == j && civilians_positions[k].row !=-1){
needy_id = k;
}
}
break=true;
}
}
}
}
//CanGoR = (position.col < GRID_WIDTH-1) && !isNextToNeedy && grid[position.row][position.col + 1]==0;
//CanGoL = (position.col > 0) && !isNextToNeedy && grid[position.row][position.col - 1]==0;
//CanGoUp = (position.row > 0) && !isNextToNeedy && grid[position.row - 1][position.col]==0;
//CanGoD = (position.row < GRID_HEIGHT-1) && !isNextToNeedy && grid[position.row + 1][position.col]==0;
}
bool CanGoRight(){
position_t position = {-1,-1};
position = first_responders_positions[id];
return ((position.col < GRID_WIDTH-1) && grid[position.row][position.col + 1]==0);
}
bool CanGoLeft(){
position_t position = {-1,-1};
position = first_responders_positions[id];
return ((position.col > 0) && grid[position.row][position.col - 1]==0);
}
bool CanGoUp(){
position_t position = {-1,-1};
position = first_responders_positions[id];
return ((position.row > 0) && grid[position.row - 1][position.col]==0);
}
bool CanGoDown(){
position_t position = {-1,-1};
position = first_responders_positions[id];
return ((position.row < GRID_HEIGHT-1) && grid[position.row + 1][position.col]==0);
}
bool CantMove(){
return (!(CanGoRight() || CanGoLeft() || CanGoUp() || CanGoDown()));
}
void Move_Right(){
position_t position = {-1,-1};
position = first_responders_positions[id];
grid[position.row][position.col] = 0;
position.col++;
grid[position.row][position.col] = FIRST_RESPONDER;
first_responders_positions[id].col++;
checkPosition();
}
void Move_Left(){
position_t position = {-1,-1};
position = first_responders_positions[id];
grid[position.row][position.col] = 0;
position.col--;
grid[position.row][position.col] = FIRST_RESPONDER;
first_responders_positions[id].col--;
checkPosition();
}
void Move_Up(){
position_t position = {-1,-1};
position = first_responders_positions[id];
grid[position.row][position.col] = 0;
position.row--;
grid[position.row][position.col] = FIRST_RESPONDER;
first_responders_positions[id].row--;
checkPosition();
}
void Move_Down(){
position_t position = {-1,-1};
position = first_responders_positions[id];
grid[position.row][position.col] = 0;
position.row++;
grid[position.row][position.col] = FIRST_RESPONDER;
first_responders_positions[id].row++;
checkPosition();
}
//wait time is computed using the data stored in the pos array. Don't modify those before.
int wait_time() {
int civ_id;
// int needy_id;
int civ_2_fr;
int fr_2_needy;
int civ_2_needy;
int rescue_time;
int i=0;
bool stay = true;
//wait the time necessary for the fr to rescue the needy
if(isNextToNeedy) {
rescue_time = Tfrs[id];
return rescue_time;
}
//wait the time necessary for the the fr to be reached by the FR and the FR to go to the Needy and the rescue time of the FR
else {
needy_id = go_save_fr[id];
// civ_id = summoner[id];
for(i=0; i < CIVILIAN_NUMBER && stay; i++){
if(go_call[i] == id){
civ_id = i;
stay = false;
}
}
civ_2_fr = manhattan_distance(civilians_positions[civ_id], first_responders_positions[id]);
fr_2_needy = manhattan_distance(first_responders_positions[id], civilians_positions[needy_id]);
rescue_time = Tfrs[id];
return civ_2_fr + fr_2_needy + rescue_time;
}
}
void update_pos(){
position_t new_pos;
if(civilians_positions[needy_id].row != OUT_ROW){
// Civilian has been saved
grid[first_responders_positions[id].row][first_responders_positions[id].col] = 0; //Remove 'RESCUING'
new_pos = civilians_positions[needy_id];
grid[new_pos.row][new_pos.col] = FIRST_RESPONDER; // Update grid accordingly
// Update FR's position to that of the rescued civilian
/*first_responders_positions[id].row = civilians_positions[needy_id].row;
first_responders_positions[id].col = civilians_positions[needy_id].col;*/
first_responders_positions[id] = new_pos;
}else{
// Civlians died before being saved
grid[first_responders_positions[id].row][first_responders_positions[id].col] = FIRST_RESPONDER; //Revert from 'RESCUING'
}
checkPosition();
}
void moveTowards(position_t wrt) {
position_t pos = first_responders_positions[id];
if (pos.row < wrt.row && CanGoDown()) {
Move_Down();
} else if (pos.row > wrt.row && CanGoUp()) {
Move_Up();
}
else if (pos.col < wrt.col && CanGoRight()) {
Move_Right();
} else if (pos.col > wrt.col && CanGoLeft()) {
Move_Left();
}
}
/*void moveAway(position_t wrt) {
position_t pos = first_responders_positions[id];
if (pos.row > wrt.row && CanGoDown()) {
Move_Down();
} else if (pos.row < wrt.row && CanGoUp()) {
Move_Up();
}
else if (pos.col > wrt.col && CanGoRight()) {
Move_Right();
} else if (pos.col < wrt.col && CanGoLeft()) {
Move_Left();
}
}*/
bool fr_on_path(){
position_t pos = first_responders_positions[id];
int i;
if(pos.row < 0 || pos.row > GRID_HEIGHT-1 || pos.col < 0 || pos.col > GRID_WIDTH-1){return false;}
i = getFRPath(pos);
return i != 0;
}
void moveTowardsPath(){
position_t pos = first_responders_positions[id];
int i;
int min_md = GRID_HEIGHT + GRID_WIDTH; // don't have max()...
int local_md = min_md;
position_t closest;
// Gotta find the closest point of the path...
for(i=0;i<FR_PATH_LEN;i++){
local_md = manhattan_distance(pos, first_responders_path[i].p);
//min_md = local_md < min_md ? local_md : min_md;
if(local_md < min_md){
min_md = local_md;
closest = first_responders_path[i].p;
}
}
// found our "entry point" to the path: now move
moveTowards(closest);
}
void moveAwayFromPath(){ //called when on path but not possible to follow it
position_t pos = first_responders_positions[id];
int d = getFRPath(pos);
if(d == D && CanGoLeft()){ //fire on our right
Move_Left();
}else if(d == R && CanGoDown()){ // fire above
Move_Down();
}else if(d == U && CanGoUp()){
Move_Up();
}else if(d == L && CanGoLeft()){
Move_Left();
}else{ // Can neither move along path nor away from fire: stay here and wait for someone else to move
//checkPosition();
return;
}
}
// movement policy: reach the nearest fire and then circle around it standing always far enough (2 manhattan distance)
void move() {
int i = 0;
int d;
position_t pos = first_responders_positions[id];
position_t cur_fire = {-1,-1};
//int distance = 0;
//check if he can move
if (CantMove()) {checkPosition(); return;}
//find closest fire
/*for (i = 0; i < FIRE_NUM; i ++) {
cur_fire = fires_positions[i];
closest_fire = manhattan_distance(pos, closest_fire) > manhattan_distance(pos, cur_fire)? cur_fire : closest_fire;
}