-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework2.xml
More file actions
969 lines (867 loc) · 32.2 KB
/
homework2.xml
File metadata and controls
969 lines (867 loc) · 32.2 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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE nta PUBLIC '-//Uppaal Team//DTD Flat System 1.1//EN' 'http://www.it.uu.se/research/group/darts/uppaal/flat-1_2.dtd'>
<nta>
<declaration>// Grid Dimension Declaration
const int N = 8;
const int M = 11;
// Global clock useful during debugging
clock g;
// Max task
const int MAX_TASK = 12;
/************************************/
/* Custom types declaration */
/************************************/
typedef struct {
int[-1, N] row;
int[-1, M] col;
} position;
typedef struct {
position pos;
bool taken;
} pod;
/*************************************/
/* Functions declaration */
/*************************************/
position createPosition(int row, int col) {
position pos;
pos.row = row;
pos.col = col;
return pos;
}
// Used to model a normal distribution
const double PI = 3.14159265358979323846;
double stdNormal() { return sqrt(-2*ln(1-random(1)))*cos(2*PI*random(1)); }
double Normal(double mean, double stdDev) { return (mean + stdDev * stdNormal()); }
// Add two positions
position addPos(position a, position b){ return createPosition(a.row + b.row, a.col + b.col); }
// Return true if 2 positions are equal
bool isEqual(position a, position b){ return a.row==b.row && a.col==b.col; }
/*************************************/
/* Constants declaration */
/*************************************/
// Number of Pods
const int NUM_OF_PODS = 24;
// Entry and delivery points
const position entryPoint = createPosition(0, 5);
const position deliveryPoint = createPosition(7, 5);
const position nullPosition = createPosition(-1, -1);
// Number of robots
const int NUM_OF_ROBOTS = 3;
/********************************/
/* "Grid" variables */
/********************************/
// Position of all the robots
position robotPositions[NUM_OF_ROBOTS] = {nullPosition, nullPosition, nullPosition};
// Initialization of Pods and their positions
pod pods[NUM_OF_PODS] = {
{{2, 0}, false}, {{2, 2}, false}, {{2, 4}, false}, {{2, 6}, false}, {{2, 8}, false}, {{2, 10}, false},
{{3, 0}, false}, {{3, 2}, false}, {{3, 4}, false}, {{3, 6}, false}, {{3, 8}, false}, {{3, 10}, false},
{{4, 0}, false}, {{4, 2}, false}, {{4, 4}, false}, {{4, 6}, false}, {{4, 8}, false}, {{4, 10}, false},
{{5, 0}, false}, {{5, 2}, false}, {{5, 4}, false}, {{5, 6}, false}, {{5, 8}, false}, {{5, 10}, false}
};
/********************************************/
/* Shared variables declaration */
/********************************************/
// set by taskManager, read by the robots when the broadcast channel is activate
// every robot will read to his position and if the position is different from {-1,-1}
// the robot will be able to move to that pod
position robotTask[NUM_OF_ROBOTS] = { nullPosition, nullPosition, nullPosition };
// used by TaskManager to know which robot is claiming the task
int synchronizationRequest;
// used by robot and semaphore to know if they can move or not
bool myMove[NUM_OF_ROBOTS] = {false, false, false};
// used by robot and semaphore to synch on which robot is asking to move
int[0, NUM_OF_ROBOTS - 1] nextRobotId;
/*************************************/
/* Channels declaration */
/*************************************/
// used by semaphore and robot to synch for the move
broadcast chan tryingToMove, move;
// used by human and robot to synch on item exchange
broadcast chan takeItem, go;
// used by task manager and robot to synch for the tasks release
broadcast chan claim, releaseTask;</declaration>
<template>
<name>Semaphore</name>
<declaration>// Robot Id that is trying to move
int[-1, NUM_OF_ROBOTS - 1] nextId;
</declaration>
<location id="id0" x="34" y="229">
<name x="-85" y="221">WaitingForMove</name>
</location>
<location id="id1" x="263" y="229">
<name x="288" y="220">SomeoneTryingToMove</name>
<committed/>
</location>
<init ref="id0"/>
<transition>
<source ref="id1"/>
<target ref="id0"/>
<label kind="synchronisation" x="127" y="119">move!</label>
<label kind="assignment" x="119" y="136">nextId = -1</label>
<nail x="144" y="161"/>
</transition>
<transition>
<source ref="id0"/>
<target ref="id1"/>
<label kind="synchronisation" x="102" y="306">tryingToMove?</label>
<label kind="assignment" x="85" y="323">nextId = nextRobotId,
myMove[nextId] = true</label>
<nail x="144" y="297"/>
</transition>
</template>
<template>
<name>Human</name>
<parameter>double mean, double stdDev</parameter>
<declaration>clock t;
int delay;
void computeDelay() { delay = abs(fint(Normal(mean, stdDev))); }</declaration>
<location id="id2" x="-229" y="-85">
<name x="-272" y="-102">Idle</name>
</location>
<location id="id3" x="-42" y="-85">
<name x="-52" y="-119">TakingItem</name>
<label kind="invariant" x="-52" y="-68">t <= delay</label>
</location>
<init ref="id2"/>
<transition>
<source ref="id2"/>
<target ref="id3"/>
<label kind="synchronisation" x="-178" y="-204">takeItem?</label>
<label kind="assignment" x="-178" y="-187">delay = 0, t = 0,
computeDelay()</label>
<nail x="-136" y="-144"/>
</transition>
<transition>
<source ref="id3"/>
<target ref="id2"/>
<label kind="guard" x="-161" y="-34">t >= delay</label>
<label kind="synchronisation" x="-152" y="-17">go!</label>
<nail x="-136" y="-34"/>
</transition>
</template>
<template>
<name>Robot</name>
<parameter>int id, int K, int lambda</parameter>
<declaration>// General robot clock
clock t;
// Record total time spent on completing tasks
int totalTime = 0;
// Completed number of task (needed for 2nd property)
int numTasks = 0;
// Completed number of task (needed for 3rd property)
int countPod = 1;
// Clock used to monitor time spent on completing tasks
clock p;
// Notable positions that the robot must remember
position currentPosition = nullPosition;
position currentPodPosition = nullPosition;
position nextPosition = nullPosition;
// True if the pathfinder cannot find a move that is acceptable by the Semaphore
bool noPossibleMoves;
// What is the robot goal in this phase
int[0, 2] target;
// Possible target states
const int TO_DELIVERY = 0; // If the robot has taken the pod and has to carry it to the Human
const int TO_POD = 1; // If a task was given to the robot, and it has to go to pick up the pod
const int BACK_TO_POD = 2; // If the task was fullfilled but the pod has to return to its initial position
// Next goal position for the robot
position goal;
// This function actually performs the move actions by changing robotPositions
void updateSharedPosAfterMove() {
// Update global position
robotPositions[id] = nextPosition;
// Update private position
currentPosition = nextPosition;
}
// True if the robot has to move to another cell to reach its target
bool isNormalMove() {
return
!(target == TO_DELIVERY && currentPosition == deliveryPoint) &&
!(target == TO_POD && currentPosition == currentPodPosition) &&
!(target == BACK_TO_POD && currentPosition == currentPodPosition);
}
// Sets the pod as NOT TAKEN, called when the pod is returned back to its initial position
void releasePod() {
int i;
for(i = 0; i < NUM_OF_PODS; i++) {
if(pods[i].pos.row == currentPodPosition.row && pods[i].pos.col == currentPodPosition.col) {
pods[i].taken = false;
currentPodPosition = nullPosition;
return;
}
}
}
// Called when the robot is picking up a pod
void takePod() {
int i;
for(i = 0; i < NUM_OF_PODS; i++) {
if(pods[i].pos.row == currentPodPosition.row && pods[i].pos.col == currentPodPosition.col) {
pods[i].taken = true;
return;
}
}
}
// If robot isn't already on grid, add it
void updatePosIfFirstTime(){
if(isEqual(currentPosition, nullPosition)){
currentPosition = entryPoint;
robotPositions[id] = currentPosition;
}
}
// Used when robot change target to update it's variables
void updateTarget(int[0,2] update){
// Simply update the tager variable
target = update;
// Depending on target, update other wariables
if(update == TO_DELIVERY){
goal = deliveryPoint;
} else if(update == TO_POD){
currentPodPosition = robotTask[id];
goal = currentPodPosition;
} else if(update == BACK_TO_POD){
goal = currentPodPosition;
}
}
/********************************************/
/* Path Finding Support Functions */
/********************************************/
// Pathfinding helper variables
const position UP = createPosition(-1, 0);
const position RIGHT = createPosition(0, 1);
const position DOWN = createPosition(1, 0);
const position LEFT = createPosition(0, -1);
const position NULL = nullPosition;
bool isEqual(position a, position b){ return a.row==b.row && a.col==b.col; }
// Reset to NULL position all path positions
//void resetPath(){ int i; for(i=0; i<N*M; i++){ path[i] = nullPosition; } }
// Get the next direction clockwise
position getNextDir(position d){
if(isEqual(d, UP)) { return RIGHT; }
else if(isEqual(d, RIGHT)) { return DOWN; }
else if(isEqual(d, DOWN)) { return LEFT; }
else { return UP; }
}
// Return true if a position is a "possible" position
bool isFeasible(position p, bool reroute){
int i;
position close, dir = UP;
// Only if on the grid, check other things
if(p.row>=0 && p.row<N && p.col>=0 && p.col<M){
// If robot is moving a pod, check if pos has a pod
if(target==BACK_TO_POD || target==TO_DELIVERY){
// Check every pod is p is at the same position
for(i=0; i<NUM_OF_PODS; i++){
// Check all pods if we are referring to same position
if(isEqual(p, pods[i].pos)) {
// If the pod is still in its original position, then it isn't a feasible position
if(!pods[i].taken){ return false; }
}
}
}
// If this is after a cannotMove, perform additional checks
if(reroute){
// Position is already excluded nextPos --> return false
if(p.row==nextPosition.row && p.col==nextPosition.col) { return false; }
// If p == one of 4 pos near currentPos and it contains a robot, then it isn't feasible
for(i=0; i<4; i++){
// Generate position close to the current one
close = addPos(currentPosition, dir);
// If p is equal to this new position
if(p.row==close.row && p.col==close.col){
for(i=0; i<NUM_OF_ROBOTS; i++){ if(isEqual(robotPositions[i], p)) { return false; } }
}
dir = getNextDir(dir);
}
}
// If none of the "bad" things happened, then it's a feasible position :)
return true;
}
// If it isn't on the grid, obviously it's unfeasible
return false;
}
// Romve the last position in the queue
position removeLastFromQueue(position& queue[N*M], int& len){
// Obtain correct pos
position pos = queue[len-1];
// Move up one position every other
queue[len-1] = nullPosition;
// Update actual array length
len = len - 1;
return pos;
}
// Add position to the queue
void addToQueue(position pos, position& queue[N*M], int& len){
// Do something only if enough space
if(len < N*M){
// Insert new pos
queue[len] = pos;
// Increase length array
len = len + 1;
}
}
// To eliminate very stupid paths, we decide the first direction to check in a greedy way
void greedyBestDir(position p, position& posArray[4]){
// Vector representing distance on x axis
int x = goal.col - p.col;
// Vector representing distance on y axis
int y = p.row - goal.row;
// If abs(x) >= abs(y) then the first direction to check should be RIGHT
if(abs(x) >= abs(y)){
if (x>=0) {
// X is the biggest and >=0, so first is RIGHT
posArray[0] = RIGHT;
// If y is >=0, we are in the first quadrant
if(y>=0){ posArray[1] = UP; posArray[2] = DOWN; posArray[3] = LEFT; }
// If y is <=0, we are in the second quadrant
else { posArray[1] = DOWN; posArray[2] = UP; posArray[3] = LEFT; }
} else {
// X is the biggest and <=0, so first is LEFT
posArray[0] = LEFT;
// If y is >=0, we are in the fourth quadrant
if(y>=0){ posArray[1] = UP; posArray[2] = DOWN; posArray[3] = RIGHT; }
// If y is <=0, we are in the third quadrant
else { posArray[1] = DOWN; posArray[2] = UP; posArray[3] = RIGHT; }
}
} else {
if (y>=0) {
// Y is the biggest and >=0, so first is UP
posArray[0] = UP;
// If x is >=0, we are in the first quadrant
if(x>=0){ posArray[1] = RIGHT; posArray[2] = LEFT; posArray[3] = DOWN; }
// If x is <=0, we are in the fourth quadrant
else { posArray[1] = LEFT; posArray[2] = RIGHT; posArray[3] = DOWN; }
} else {
// Y is the biggest and <=0, so first is DOWN
posArray[0] = DOWN;
// If x is >=0, we are in the second quadrant
if(x>=0){ posArray[1] = RIGHT; posArray[2] = LEFT; posArray[3] = UP; }
// If x is <=0, we are in the third quadrant
else { posArray[1] = LEFT; posArray[2] = RIGHT; posArray[3] = UP; }
}
}
}
/************************************************/
/* Path finding Algorithm (Backtracking) */
/************************************************/
bool findPath(position& nextMove[1], bool reroute){
// Queue needed to seach the entire matrix
position queue[N*M];
// Current queue length
int len = 0;
// Matrix representing if a cell is already visited
bool visited[N][M];
// Control variables for while loop
bool searching = true;
bool found = false;
bool added = false;
// Miscellaneous
int i, j;
position dirArray[4], next, curPos;
// Initialize visited matrix
for(i=0; i<N; i++){ for(j=0; j<M; j++){ visited[i][j] = false; } }
// Given that it was outside of the grid, set the robot on entryPoint
if(currentPosition == nullPosition) { curPos = entryPoint; }
// Else initialize curPos normally
else { curPos = currentPosition; }
// Add first position that in the end will be removed
addToQueue(curPos, queue, len);
while(searching && not found){
if(len > 0){
curPos = queue[len-1];
// Goal found, ending everything :)
if(isEqual(curPos, goal)){ found = true; }
else {
// Set to visited the cell
visited[curPos.row][curPos.col] = true;
// Let's set the sequence of best directions
greedyBestDir(curPos, dirArray);
// Check each direction
for(i=0; i<4; i++){
// Obtain the resulting position after "moving" in the specified direction
next = addPos(curPos, dirArray[i]);
// Only is feasible and not already visited, add the position to the queue
if(isFeasible(next, reroute)){
if(not visited[next.row][next.col]){
addToQueue(next, queue, len);
added = true;
i=4; // Needed to end the loop
}
}
}
// If no position was added, then the current position is not part of the solution, so it's removed from queue
if(not added){ removeLastFromQueue(queue, len); }
// Variable reset to be ready for next cycle
added = false;
}
} else { searching = false; }
}
if(found){
// Copy next move
nextMove[0] = queue[1];
return true;
} else { return false; }
}
position getNextMove(bool reroute){
int i;
position nextMove[1], dir = UP, close;
bool existPath;
// Initialize nextMove
nextMove[0] = nullPosition;
// Can a path be established?
existPath = findPath(nextMove, reroute);
// If a path exists, then return the next move
if(existPath) { return nextMove[0]; }
else {
// If any direction near the currePosition is free, just move in that direction.
// This is done to eliminate possible deadlocks (random movement if no path may
// free some space needed by another robot)
for(i=0; i<4; i++){
// Generate a close position
close = addPos(currentPosition, dir);
// If it feasible (= no robot && no pod if the robot is moving one) then move in that direction!
if(isFeasible(close, true)){ return close; }
// No luck for now, change direction
dir = getNextDir(dir);
}
// No near position was feasible, so you're BLOCKED!!11!1!!!1!!
noPossibleMoves = true;
// Return a fictious position
return nullPosition;
}
}</declaration>
<location id="id4" x="289" y="459">
<name x="255" y="485">DecideAction</name>
<committed/>
</location>
<location id="id5" x="892" y="-68">
<name x="909" y="-85">DecideMove</name>
<committed/>
</location>
<location id="id6" x="297" y="-68">
<name x="246" y="-127">WaitingToMove</name>
<label kind="invariant" x="272" y="-110">t <= K</label>
</location>
<location id="id7" x="-42" y="170">
<name x="-169" y="161">WaitingHuman</name>
</location>
<location id="id8" x="-425" y="8">
<name x="-476" y="25">ClaimingTask</name>
</location>
<location id="id9" x="-671" y="8">
<name x="-714" y="-8">idle</name>
<label kind="exponentialrate" x="-739" y="8">lambda</label>
</location>
<location id="id10" x="-340" y="-68">
<name x="-416" y="-110">TaskChecking</name>
<committed/>
</location>
<location id="id11" x="544" y="-68">
<name x="493" y="-102">WaitingGreenLight</name>
</location>
<location id="id12" x="-85" y="-68">
<committed/>
</location>
<init ref="id9"/>
<transition>
<source ref="id5"/>
<target ref="id11"/>
<label kind="guard" x="680" y="25">!myMove[id]</label>
<label kind="synchronisation" x="680" y="42">tryingToMove!</label>
<label kind="assignment" x="680" y="59">nextRobotId = id</label>
<nail x="722" y="17"/>
</transition>
<transition>
<source ref="id5"/>
<target ref="id6"/>
<label kind="guard" x="901" y="-187">noPossibleMoves &&
myMove[id]</label>
<label kind="assignment" x="901" y="-153">noPossibleMoves = false,
t = 0, myMove[id] = false</label>
<nail x="892" y="-212"/>
<nail x="484" y="-212"/>
</transition>
<transition>
<source ref="id12"/>
<target ref="id6"/>
<label kind="guard" x="17" y="-161">currentPosition != goal</label>
<label kind="assignment" x="76" y="-144">t = 0, p = 0</label>
<nail x="93" y="-127"/>
</transition>
<transition>
<source ref="id4"/>
<target ref="id6"/>
<label kind="guard" x="306" y="195">isNormalMove()</label>
<label kind="assignment" x="306" y="212">t = 0</label>
</transition>
<transition>
<source ref="id12"/>
<target ref="id6"/>
<label kind="guard" x="-17" y="-59">currentPosition == goal</label>
<label kind="assignment" x="-17" y="-42">takePod(),
updateTarget(TO_DELIVERY),
t = 0, p = 0</label>
</transition>
<transition>
<source ref="id6"/>
<target ref="id11"/>
<label kind="guard" x="408" y="-127">t >= K</label>
<label kind="synchronisation" x="382" y="-110">tryingToMove!</label>
<label kind="assignment" x="374" y="-93">nextRobotId = id</label>
</transition>
<transition>
<source ref="id5"/>
<target ref="id4"/>
<label kind="guard" x="909" y="306">!noPossibleMoves &&
myMove[id]</label>
<label kind="assignment" x="909" y="246">noPossibleMoves = false,
updateSharedPosAfterMove(),
myMove[id] = false</label>
<nail x="892" y="459"/>
</transition>
<transition>
<source ref="id10"/>
<target ref="id9"/>
<label kind="guard" x="-663" y="-93">robotTask[id] == nullPosition</label>
<nail x="-671" y="-68"/>
</transition>
<transition>
<source ref="id4"/>
<target ref="id6"/>
<label kind="guard" x="510" y="170">target == TO_POD &&
currentPosition == currentPodPosition</label>
<label kind="assignment" x="510" y="204">takePod(),
updateTarget(TO_DELIVERY),
t=0</label>
<nail x="493" y="399"/>
<nail x="493" y="85"/>
</transition>
<transition>
<source ref="id4"/>
<target ref="id9"/>
<label kind="guard" x="-535" y="467">target == BACK_TO_POD &&
currentPosition == currentPodPosition</label>
<label kind="assignment" x="-535" y="501">releasePod(), robotTask[id] = nullPosition,
countPod++, totalTime += fint(p), numTasks++</label>
<nail x="-671" y="450"/>
</transition>
<transition>
<source ref="id10"/>
<target ref="id12"/>
<label kind="guard" x="-297" y="-127">robotTask[id] != nullPosition</label>
<label kind="assignment" x="-297" y="-110">updateTarget(TO_POD),
updatePosIfFirstTime()</label>
</transition>
<transition>
<source ref="id11"/>
<target ref="id5"/>
<label kind="synchronisation" x="696" y="-119">move?</label>
<label kind="assignment" x="620" y="-102">nextPosition = getNextMove(true)</label>
</transition>
<transition>
<source ref="id4"/>
<target ref="id7"/>
<label kind="guard" x="-34" y="221">currentPosition == deliveryPoint &&
target == TO_DELIVERY</label>
<label kind="synchronisation" x="-34" y="255">takeItem!</label>
<nail x="-42" y="356"/>
</transition>
<transition>
<source ref="id7"/>
<target ref="id6"/>
<label kind="synchronisation" x="195" y="93">go?</label>
<label kind="assignment" x="-8" y="127">updateTarget(BACK_TO_POD),
t = 0</label>
<nail x="212" y="170"/>
</transition>
<transition>
<source ref="id8"/>
<target ref="id10"/>
<label kind="synchronisation" x="-331" y="-8">releaseTask?</label>
<nail x="-340" y="8"/>
</transition>
<transition>
<source ref="id9"/>
<target ref="id8"/>
<label kind="synchronisation" x="-578" y="-34">claim!</label>
<label kind="assignment" x="-646" y="-17">synchronizationRequest = id</label>
</transition>
</template>
<template>
<name>TaskManager</name>
<parameter>double mean, double stdDev</parameter>
<declaration>clock t;
int delay;
// Sample delay from normal distribution
void computeDelay() { delay = fint(Normal(mean, stdDev)); }
// This is a queue which works with a FIFO policy, where MAX_T is the maximum number of tasks settable
position tasks[MAX_TASK];
// To keep track of the actual number of task present in the tasks array
int[0, MAX_TASK] actualLength;
// Here there is the id of the robot which has claimed a task
int[-1,NUM_OF_ROBOTS] actualRobotId;
// Used to signal if the max capacity of the array is reached
bool exploded;
// To initialize every local variable
void settingUp() {
int i;
for (i=0; i<MAX_TASK; i++) { tasks[i] = nullPosition; }
actualLength = 0;
actualRobotId = -1;
exploded = false;
}
// This verify if the pod in input is present in the queue checking all the tasks array (if it is the case)
bool podIsInTasks(pod myPod) {
int i;
for (i = 0; i < actualLength; i++) {
if (isEqual(myPod.pos, tasks[i])) { return true; }
}
return false;
}
// Check if the pod is already assigned to a robot
bool podIsAssigned(pod myPod) {
int i;
for (i = 0; i < NUM_OF_ROBOTS; i++) {
if (isEqual(myPod.pos, robotTask[i])) { return true; }
}
return false;
}
// Find another "random" number adding 1 to the previous random number, keeping attention to the NUM_OF_PODS present in the layout
int anotherTmp(int tmp) {
int myTmp = tmp;
if (tmp == NUM_OF_PODS - 1)
myTmp = 0;
else
myTmp += 1;
return myTmp;
}
// Returning the position of the selected pod from the global pods array
// If it is not already in the tasks array and if it is not taken (a robot has taken it)
// Else continuing searching until finding a pod to add as a task
position selectAPod(int tmp) {
int myTmp = tmp, i;
position newPos = {-1, -1};
// Loop until you've checked all pods
for(i=0; i<NUM_OF_PODS; i++) {
// If the pod isn't already in the task queue
if (!podIsInTasks(pods[myTmp])) {
// If the pod isn't already assigned to any robot
if (!podIsAssigned(pods[myTmp])) {
newPos.row = pods[myTmp].pos.row;
newPos.col = pods[myTmp].pos.col;
i = NUM_OF_PODS; // to end the loop
}
}
// Check another pod
myTmp = anotherTmp(myTmp);
}
return newPos;
}
// Get the number of pods already assigned to a robot
int getNumOfAssignedPods() {
int num = 0, i;
for (i = 0; i < NUM_OF_ROBOTS; i++) {
if (!isEqual(robotTask[i], nullPosition)) { num++; }
}
return num;
}
// Selecting a position which corresponding to a free pod using the random value in input
// Adding it as task in the tasks array, updating the true length of it
void addTask(int randomValue) {
// Every pods is tasked
if ((actualLength + getNumOfAssignedPods()) >= NUM_OF_PODS) {
exploded = true; // tried to add, but no pods are available, so I lost the task
return;
}
// The array has reached the max length possible?
if (actualLength < MAX_TASK) {
position taskPos = selectAPod(randomValue);
tasks[actualLength] = taskPos;
actualLength += 1;
} else { exploded = true; }
}
//-----------------------------------------------------------------------------------------------
// Extracting a position of a pod with a FIFO policy
position extractFIFO() {
position tmp = tasks[0];
int i = 1;
while (i < actualLength) {
tasks[i-1] = tasks[i];
i++;
}
tasks[i-1] = nullPosition;
actualLength--;
return tmp;
}
// Extract a task from the queue and insert it in the robotTask array in the position represented by actualRobotId
void takeTask() {
// If there is no task, TM will not set a position for the robot
if (actualLength != 0) {
position ext;
ext = extractFIFO();
robotTask[actualRobotId] = ext;
}
actualRobotId = -1;
}</declaration>
<location id="id13" x="-229" y="-119">
<name x="-238" y="-102">Idle</name>
<label kind="invariant" x="-255" y="-85">t <= delay</label>
</location>
<location id="id14" x="-544" y="-119">
<name x="-595" y="-162">ReleasingTask</name>
<committed/>
</location>
<location id="id15" x="-229" y="-272">
<name x="-255" y="-314">SettingUp</name>
<committed/>
</location>
<init ref="id15"/>
<transition>
<source ref="id15"/>
<target ref="id13"/>
<label kind="assignment" x="-221" y="-255">t = 0,
settingUp(),
computeDelay()</label>
</transition>
<transition>
<source ref="id14"/>
<target ref="id13"/>
<label kind="synchronisation" x="-425" y="-221">releaseTask!</label>
<label kind="assignment" x="-425" y="-204">takeTask()</label>
<nail x="-391" y="-178"/>
</transition>
<transition>
<source ref="id13"/>
<target ref="id14"/>
<label kind="synchronisation" x="-408" y="-51">claim?</label>
<label kind="assignment" x="-527" y="-34">actualRobotId = synchronizationRequest</label>
<nail x="-391" y="-59"/>
</transition>
<transition>
<source ref="id13"/>
<target ref="id13"/>
<label kind="select" x="-34" y="-161">podIndex: int[0,NUM_OF_PODS-1]</label>
<label kind="guard" x="-34" y="-144">t >= delay</label>
<label kind="assignment" x="-34" y="-127">addTask(podIndex), delay = 0,
computeDelay(), t = 0</label>
<nail x="-51" y="-187"/>
<nail x="-51" y="-59"/>
</transition>
</template>
<system>// Configuration LOW
h_LOW = Human(13.0, 1.5);
r0_LOW = Robot(0, 2, 2);
r1_LOW = Robot(1, 2, 2);
r2_LOW = Robot(2, 2, 2);
taskManager_LOW = TaskManager(38.0, 2.5);
// Configuration HIGH
h_HIGH = Human(20.0, 1.5);
r0_HIGH = Robot(0, 4, 2);
r1_HIGH = Robot(1, 4, 2);
r2_HIGH = Robot(2, 4, 2);
taskManager_HIGH = TaskManager(35.0, 2.5);
// The semaphore is equal in both systems
semaphore = Semaphore();
/**************************** ATTENTION! ****************************/
// To run one system or the other, please decomment the desired system and comment the undesired one
// After this, symply run all the properties in the verifier
// Low probability of losing a task
system r0_LOW, r1_LOW, r2_LOW, taskManager_LOW, h_LOW, semaphore;
// High probability of losing a task
//system r0_HIGH, r1_HIGH, r2_HIGH, taskManager_HIGH, h_HIGH, semaphore;
</system>
<queries>
<query>
<formula>**** Before trying to check anything, please take a look at the System Declarations and set the epsilon = 0.025 ****</formula>
<comment></comment>
</query>
<query>
<formula></formula>
<comment></comment>
</query>
<query>
<formula>******** This is for configuration LOW ********</formula>
<comment></comment>
</query>
<query>
<formula>Mandatory property</formula>
<comment></comment>
</query>
<query>
<formula>Pr[<=10000](<>taskManager_LOW.exploded == true)</formula>
<comment></comment>
</query>
<query>
<formula></formula>
<comment></comment>
</query>
<query>
<formula>2nd property</formula>
<comment></comment>
</query>
<query>
<formula>Pr[<=10000](<>(abs(r0_LOW.numTasks - r1_LOW.numTasks) > 23 or abs(r0_LOW.numTasks - r2_LOW.numTasks) > 23 or abs(r1_LOW.numTasks - r2_LOW.numTasks) > 23))</formula>
<comment></comment>
</query>
<query>
<formula></formula>
<comment></comment>
</query>
<query>
<formula>3rd property</formula>
<comment></comment>
</query>
<query>
<formula>Pr[<=10000](<>( 45 < (r0_LOW.totalTime/r0_LOW.countPod) and 45 < (r1_LOW.totalTime/r1_LOW.countPod) and 45 < (r2_LOW.totalTime/r2_LOW.countPod) ) )</formula>
<comment></comment>
</query>
<query>
<formula>Pr[<=10000]([]((r0_LOW.totalTime/r0_LOW.countPod) < 70 and (r1_LOW.totalTime/r1_LOW.countPod) < 70 and (r2_LOW.totalTime/r2_LOW.countPod) < 70 ) )</formula>
<comment></comment>
</query>
<query>
<formula></formula>
<comment></comment>
</query>
<query>
<formula>******** This is for configuration HIGH ********</formula>
<comment></comment>
</query>
<query>
<formula>Mandatory property</formula>
<comment></comment>
</query>
<query>
<formula>Pr[<=10000](<>taskManager_HIGH.exploded == true)</formula>
<comment></comment>
</query>
<query>
<formula></formula>
<comment></comment>
</query>
<query>
<formula>2nd property</formula>
<comment></comment>
</query>
<query>
<formula>Pr[<=10000](<>(abs(r0_HIGH.numTasks - r1_HIGH.numTasks) > 15 or abs(r0_HIGH.numTasks - r2_HIGH.numTasks) > 15 or abs(r1_HIGH.numTasks - r2_HIGH.numTasks) > 15))</formula>
<comment></comment>
</query>
<query>
<formula></formula>
<comment></comment>
</query>
<query>
<formula>3rd property</formula>
<comment></comment>
</query>
<query>
<formula>Pr[<=10000]([]((r0_HIGH.totalTime/r0_HIGH.countPod) < 145 and (r1_HIGH.totalTime/r1_HIGH.countPod) < 145 and (r2_HIGH.totalTime/r2_HIGH.countPod) < 145 ) )</formula>
<comment></comment>
</query>
<query>
<formula>Pr[<=10000](<>( 90 < (r0_HIGH.totalTime/r0_HIGH.countPod) and 90 < (r1_HIGH.totalTime/r1_HIGH.countPod) and 90 < (r2_HIGH.totalTime/r2_HIGH.countPod) ) )</formula>
<comment></comment>
</query>
</queries>
</nta>