-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathresidential_algorithm_reference.algo
More file actions
242 lines (209 loc) · 10.8 KB
/
residential_algorithm_reference.algo
File metadata and controls
242 lines (209 loc) · 10.8 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
SET elevatorID TO 1
SET floorRequestButtonID TO 1
SET callButtonID TO 1
DEFINE Column USING _id AND _status AND _amountOfFloors AND _amountOfElevators
SET THIS ID TO _id
SET THIS status TO _status
SET THIS elevatorList TO EMPTY ARRAY
SET THIS callButtonList TO EMPTY ARRAY
CALL THIS createElevators USING _amountOfFloors AND _amountOfElevators
CALL THIS createCallButtons USING _amountOfFloors
'//---------------------------------Methods--------------------------------------------//'
SEQUENCE createCallButtons USING _amountOfFloors
SET buttonFloor TO 1
FOR _amountOfFloors
IF buttonFloor < _amountOfFloors THEN '//If it's not the last floor
SET callButton TO NEW CallButton WITH callButtonID AND OFF AND buttonFloor AND Up '//id, status, floor, direction
ADD callButton TO THIS callButtonsList
INCREMENT callButtonID
ENDIF
IF buttonFloor > 1 THEN '//If it's not the first floor
SET callButton TO NEW CallButton WITH callButtonID AND OFF AND buttonFloor AND Down '//id, status, floor, direction
ADD callButton TO THIS callButtonsList
INCREMENT callButtonID
ENDIF
INCREMENT buttonFloor
END FOR
ENDSEQUENCE
SEQUENCE createElevators USING _amountOfFloors AND _amountOfElevators
FOR _amountOfElevators
SET elevator TO NEW Elevator WITH elevatorID AND idle AND _amountOfFloors AND 1 '//id, status, amountOfFloors, currentFloor
ADD elevator TO THIS elevatorsList
INCREMENT elevatorID
ENDFOR
ENDSEQUENCE
'//Simulate when a user press a button outside the elevator
SEQUENCE requestElevator USING floor AND direction RETURNING elevator
SET elevator TO CALL THIS findElevator WITH floor AND direction RETURNING bestElevator
ADD floor TO elevator requestList
CALL elevator move
CALL elevator operateDoors
RETURN elevator
ENDSEQUENCE
'//We use a score system depending on the current elevators state. Since the bestScore and the referenceGap are
'//higher values than what could be possibly calculated, the first elevator will always become the default bestElevator,
'//before being compared with to other elevators. If two elevators get the same score, the nearest one is prioritized.
SEQUENCE findElevator USING requestedFloor AND requestedDirection RETURNING bestElevator
INIT bestElevator
SET bestScore TO 5
SET referenceGap TO 10000000
INIT bestElevatorInformations
FOR EACH elevator IN THIS elevatorsList
'//The elevator is at my floor and going in the direction I want
IF requestedFloor EQUALS elevator currentFloor AND elevator status EQUALS stopped AND requestedDirection EQUALS elevator direction THEN
SET bestElevatorInformations TO CALL THIS checkIfElevatorIsBetter WITH 1 AND elevator AND bestScore AND referenceGap AND bestElevator AND requestedFloor RETURNING bestElevatorInformations
'//The elevator is lower than me, is coming up and I want to go up
ELSE IF requestedFloor IS GREATER THAN elevator currentFloor AND elevator direction EQUALS up AND requestedDirection EQUALS elevator direction THEN
SET bestElevatorInformations TO CALL THIS checkIfElevatorIsBetter WITH 2 AND elevator AND bestScore AND referenceGap AND bestElevator AND requestedFloor RETURNING bestElevatorInformations
'//The elevator is higher than me, is coming down and I want to go down
ELSE IF requestedFloor IS LESS THAN elevator currentFloor AND elevator direction EQUALS down AND requestedDirection EQUALS elevator direction THEN
SET bestElevatorInformations TO CALL THIS checkIfElevatorIsBetter WITH 2 AND elevator AND bestScore AND referenceGap AND bestElevator AND requestedFloor RETURNING bestElevatorInformations
'//The elevator is idle
ELSE IF elevator status EQUALS idle THEN
SET bestElevatorInformations TO CALL THIS checkIfElevatorIsBetter WITH 3 AND elevator AND bestScore AND referenceGap AND bestElevator AND requestedFloor RETURNING bestElevatorInformations
'//The elevator is not available, but still could take the call if nothing better is found
ELSE
SET bestElevatorInformations TO CALL THIS checkIfElevatorIsBetter WITH 4 AND elevator AND bestScore AND referenceGap AND bestElevator AND requestedFloor RETURNING bestElevatorInformations
ENDIF
SET bestElevator TO bestElevatorInformations bestElevator
SET bestScore TO bestElevatorInformations bestScore
SET referenceGap TO bestElevatorInformations referenceGap
ENDFOR
RETURN bestElevator
ENDSEQUENCE
SEQUENCE checkIfElevatorIsBetter USING scoreToCheck AND newElevator AND bestScore AND referenceGap AND bestElevator AND floor RETURNING bestElevatorInformations
IF scoreToCheck IS LESS THAN bestScore THEN
SET bestScore TO scoreToCheck
SET bestElevator TO newElevator
SET referenceGap TO ABSOLUTE VALUE OF newElevator currentFloor - floor
ELSE IF bestScore EQUALS scoreToCheck
SET gap TO ABSOLUTE VALUE OF newElevator currentFloor - floor
IF referenceGap IS GREATER THAN gap THEN
SET bestElevator TO newElevator
SET referenceGap TO gap
ENDIF
ENDIF
RETURN bestElevator AND bestScore AND referenceGap AS bestElevatorInformations
ENDSEQUENCE
ENDDEFINE '//Column
DEFINE Elevator USING _id AND _status AND _amountOfFloors AND _currentFloor
SET THIS ID TO _id
SET THIS status TO _status
SET THIS currentFloor TO _currentFloor
SET THIS direction TO null
SET THIS door TO NEW Door WITH _id AND closed
SET THIS floorRequestsButtonsList TO EMPTY ARRAY
SET THIS floorRequestList TO EMPTY ARRAY
CALL THIS createFloorRequestButtons WITH _amountOfFloors
SEQUENCE createFloorRequestButtons USING _amountOfFloors
SET buttonFloor TO 1
FOR _amountOfFloors
SET floorRequestButton TO NEW FloorRequestButton WITH floorRequestButtonID AND OFF AND buttonFloor '//id, status, floor
ADD floorRequestButton TO THIS floorButtonsList
INCREMENT buttonFloor
INCREMENT floorRequestButtonID
ENDFOR
ENDSEQUENCE
'//Simulate when a user press a button inside the elevator
SEQUENCE requestFloor USING floor
ADD floor TO THIS requestList
CALL THIS move
CALL THIS operateDoors
ENDSEQUENCE
SEQUENCE move
WHILE THIS requestList IS NOT empty
SET destination TO first element of THIS requestList
SET THIS status TO moving
IF THIS currentFloor IS LESS THAN destination THEN
SET THIS direction TO up
CALL THIS sortFloorList
WHILE THIS currentFloor IS LESS THAN destination
INCREMENT THIS currentFloor
SET THIS screenDisplay TO THIS currentFloor
ENDWHILE
ELSE IF THIS currentFloor IS GREATER THAN destination THEN
SET THIS direction TO down
CALL THIS sortFloorList
WHILE THIS currentFloor IS GREATER THAN destination
DECREMENT THIS currentFloor
SET THIS screenDisplay TO THIS currentFloor
ENDWHILE
ENDIF
SET THIS status TO stopped
REMOVE first element OF THIS requestList
ENDWHILE
SET THIS status TO idle
ENDSEQUENCE
SEQUENCE sortFloorList
IF THIS direction IS up
SORT THIS requestList ASCENDING
ELSE
SORT THIS requestList DESCENDING
ENDIF
ENDSEQUENCE
SEQUENCE operateDoors
SET THIS door status TO opened
WAIT 5 seconds
IF THIS IS NOT overweight
SET THIS door status TO closing
IF no obstruction
SET THIS door status TO closed
ELSE
CALL THIS operateDoors
ELSE
WHILE THIS IS overweight
Activate overweight alarm
ENDWHILE
CALL THIS operateDoors
ENDIF
ENDSEQUENCE
ENDDEFINE '//Elevator
DEFINE CallButton USING _id, _status, _floor, _direction
SET THIS ID TO _id
SET THIS status TO _status
SET THIS floor TO _floor
SET THIS direction TO _direction
ENDDEFINE
DEFINE FloorRequestButton USING _id, _status, _floor
SET THIS ID TO _id
SET THIS status TO _status
SET THIS floor TO _floor
ENDDEFINE
DEFINE Door USING _id, _status
SET THIS ID TO _id
SET THIS status TO _status
ENDDEFINE
'==================================Scenario 1=================================================
SET column TO NEW Column WITH 1 AND online AND 10 AND 2 '//id, status, amountOfFloors, amountOfElevators
SET first elevator floor OF column elevatorsList TO 2
SET second elevator floor OF column elevatorsList TO 6
SET elevator TO CALL column requestElevator WITH 3 AND Up RETURNING elevator
CALL elevator requestFloor WITH 7
'==================================End Scenario 1=============================================
'==================================Scenario 2=================================================
SET column TO NEW Column WITH 1 AND online AND 10 AND 2 '//id, status, amountOfFloors, amountOfElevators
SET first elevator floor OF column elevatorsList TO 10
SET second elevator floor OF column elevatorsList TO 3
'//Part 1
SET elevator TO CALL column requestElevator WITH 1 AND Up RETURNING elevator
CALL elevator requestFloor WITH 6
'//Part 2
SET elevator TO CALL column requestElevator WITH 3 AND Up RETURNING elevator
CALL elevator requestFloor WITH 5
'//Part 3
SET elevator TO CALL column requestElevator WITH 9 AND Down RETURNING elevator
CALL elevator requestFloor WITH 2
'==================================End Scenario 2=============================================
'==================================Scenario 3=================================================
SET column TO NEW Column WITH 1 AND online AND 10 AND 2 '//id, status, amountOfFloors, amountOfElevators
SET first elevator floor OF column elevatorsList TO 10
SET second elevator floor OF column elevatorsList TO 3
SET second elevator status OF column elevatorsList TO moving
ADD 6 TO second elevator floorRequestList OF column elevatorsList
'//Part 1
SET elevator TO CALL column requestElevator WITH 3 AND Down RETURNING elevator
CALL elevator requestFloor WITH 2
'//Part 2
SET elevator TO CALL column requestElevator WITH 10 AND Down RETURNING elevator
CALL elevator requestFloor WITH 3
'==================================End Scenario 3=============================================