-
Notifications
You must be signed in to change notification settings - Fork 0
/
definitions.py
597 lines (537 loc) · 23.3 KB
/
definitions.py
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
from dataclasses import dataclass, astuple
from queue import PriorityQueue
from parameters import *
from random import getrandbits, shuffle, gauss, randint
from itertools import chain
import pickle
@dataclass
class Point:
x: int
y: int
def __str__(self):
return f"(x={self.x}, y={self.y})"
def __add__(self, other):
if isinstance(other, Point):
return Point(self.x+other.x, self.y+other.y)
elif isinstance(other, tuple):
return Point(self.x+other[0], self.y+other[1])
else:
return NotImplemented
@dataclass
class Agent:
target: Point
coords: Point
group: str
state: str = 'go'
def __str__(self):
return f"(t={str(self.target):>13}, c={str(self.coords):>13}, g={self.group}, s={self.state})"
# this may very well break execution_queue, so disabled for now
# ensures random ordering in the execution_queue for simultaneous agents.
def __lt__(self, other):
# or maybe there should be (self is not other)?
return (self != other) and bool(getrandbits(1))
def move(self, board, dx, dy):
"""
Moves agent if possible. Returns duration in ticks
"""
x, y = astuple(self.coords)
if board[x+dx][y+dy] is None:
board[x+dx][y+dy] = self
board[x][y] = None
self.coords += dx, dy
else:
raise Exception(
f"{self}, tried to move by [{dx},{dy}] to the occupied location")
def act(self, board, execution_queue, time, no_shuffles=False, no_stowing=False, mu=10):
"""
Agent modifies its state based on the board state and returns the duration
of the current action. Return value None means end of the execution.
"""
x, y = astuple(self.coords)
def go():
if y != aisle_y:
raise Exception(f"Agent {self} not in aisle in the 'go' state")
if no_shuffles:
self.state = 'ns_go'
if x < self.target.x-1:
if (board[x+1][y] is None) and no_shuffle_conflict(board, x+1, y):
self.move(board, 1, 0)
return walk_tick_cnt
else:
return skip_tick_cnt
elif x == self.target.x-1:
self.state = 'my_turn'
return skip_tick_cnt
else:
raise Exception(
f"Agent {self} went past its target in the 'go' state")
def ns_go():
if y != aisle_y:
raise Exception(
f"Agent {self} not in aisle in the 'ns_go' state")
if x < self.target.x:
if board[x+1][y] is None:
self.move(board, 1, 0)
return walk_tick_cnt
else:
return skip_tick_cnt
elif x == self.target.x:
self.state = 'ns_luggage'
return skip_tick_cnt
else:
raise Exception(
f"Agent {self} went past its target in the 'ns_go' state")
def my_turn():
if x != self.target.x-1:
raise Exception(
f"Agent {self} not just before its row in the 'my_turn' state")
if y != aisle_y:
raise Exception(
f"Agent {self} not in aisle in 'my_turn' state")
if (board[x+1][y] is None) and no_shuffle_need(board, self.target.x, self.target.y) and no_shuffle_other(board, self.target.x, self.target.y):
self.move(board, 1, 0)
self.state = 'luggage'
return walk_tick_cnt
else:
return skip_tick_cnt
# if (board[x+1][y] is None) and no_shuffle_conflict(board, x+1, y):
# to_shuffle = get_shuffle_need(board, self.target.x, self.target.y)
# if len(to_shuffle) == 0:
# self.move(board, 1, 0)
# self.state = 'luggage'
# return walk_tick_cnt
# elif len(to_shuffle) == 1:
# if to_shuffle == (None,):
# self.move(board, 1, 0)
# self.state = 'luggage'
# return walk_tick_cnt
# else:
# (j,) = to_shuffle
# if j.state == 'waiting':
# j.state = 'shuffle_1b'
# execution_queue.put(time+skip_tick_cnt, j)
# return skip_tick_cnt
# elif len(to_shuffle) == 2:
# if to_shuffle == (None, None):
# self.move(board, 1, 0)
# self.state = 'luggage'
# return walk_tick_cnt
# else:
# (k,l) = to_shuffle
# if k is None:
# if k.state == 'waiting':
# k.state = 'shuffle_1b'
# execution_queue.put(time+skip_tick_cnt, k)
# return skip_tick_cnt
# elif l is None:
# if l.state == 'waiting':
# l.state = 'shuffle_1a'
# execution_queue.put(time+skip_tick_cnt, l)
# return skip_tick_cnt
# else:
# if k.state == 'waiting' and l.state == 'waiting':
# k.state = 'shuffle_2c'
# execution_queue.put(time+skip_tick_cnt, k)
# l.state = 'shuffle_2a'
# execution_queue.put(time+skip_tick_cnt, l)
# return skip_tick_cnt
# return skip_tick_cnt
def luggage():
if x != self.target.x:
raise Exception(
f"Agent {self} not in its row in the 'luggage' state")
if y != aisle_y:
raise Exception(
f"Agent {self} not in aisle in 'luggage' state")
self.state = 'entering'
return 0 if no_stowing else abs(int(gauss(mu, sigma)))
def ns_luggage():
if x != self.target.x:
raise Exception(
f"Agent {self} not in its row in the 'ns_luggage' state")
if y != aisle_y:
raise Exception(
f"Agent {self} not in aisle in 'ns_luggage' state")
self.state = 'ns_entering'
return 0 if no_stowing else abs(int(gauss(mu, sigma)))
def entering():
if x != self.target.x:
raise Exception(
f"Agent {self} not in its row in the 'entering' state")
if y != self.target.y:
dy = (self.target.y - y)
dy = dy//abs(dy)
if board[x][y+dy] is None:
self.move(board, 0, dy)
return walk_tick_cnt
else:
return skip_tick_cnt
else:
self.state = 'is_that_all'
return skip_tick_cnt
def ns_entering():
if x != self.target.x:
raise Exception(
f"Agent {self} not in its row in the 'ns_entering' state")
if y != self.target.y:
dy = (self.target.y - y)
self.move(board, 0, dy)
return walk_tick_cnt*abs(dy)
else:
self.state = 'ns_done'
return None
def is_that_all():
if x != self.target.x:
raise Exception(
f"Agent {self} not in its row in the 'waiting' state")
if y != self.target.y:
raise Exception(
f"Agent {self} not in its seat in 'waiting' state")
ys = range(*((seats_left, y, -1) if y >
0 else (-seats_right, y, 1)))
if all(board[x][_y] is not None for _y in ys):
self.state = 'done'
return None
else:
self.state = 'waiting'
return skip_tick_cnt
def waiting():
if x != self.target.x:
raise Exception(
f"Agent {self} not in its row in the 'waiting' state")
if y != self.target.y:
raise Exception(
f"Agent {self} not in its seat in 'waiting' state")
if board[x-1][0] is not None and board[x-1][0].target.x == x:
ty = board[x-1][0].target.y
ys = range(0, ty, ty//abs(ty))
if y in ys:
# self.state='shuffleXD'
if y in {-1, 1}:
if board[x][2*y//abs(y)] is None:
self.state = 'shuffle_1b'
return skip_tick_cnt
elif board[x][2*y//abs(y)].state == 'waiting' or board[x][2*y//abs(y)].state.startswith('shuffle'):
self.state = 'shuffle_2c'
return skip_tick_cnt
elif y in {-2, 2}:
if board[x][0] is not None and board[x][0].target.y in ys:
return skip_tick_cnt
else:
if board[x][y//abs(y)] is None:
self.state = 'shuffle_1a'
return skip_tick_cnt
else:
self.state = 'shuffle_2a'
return skip_tick_cnt
return skip_tick_cnt
def shuffle():
if self.state.endswith('a'):
dy = -y//abs(y)
if board[x][y+dy] is None:
self.move(board, 0, dy)
self.state = self.state[:-1]+'b'
return walk_tick_cnt
else:
return skip_tick_cnt
elif self.state.endswith('b'):
if y != 0:
dy = -y//abs(y)
if board[x][y+dy] is None and (self.state.endswith('2b') or no_shuffle_conflict(board, x, 0)):
self.move(board, 0, dy)
return walk_tick_cnt
else:
return skip_tick_cnt
elif x == self.target.x:
if self.state.endswith('1b'):
if board[x+1][y] is None:
self.move(board, 1, 0)
return walk_tick_cnt
else:
return skip_tick_cnt
else: # .endswith('2b')
if board[x+1][y] is None:
self.move(board, 1, 0)
return walk_tick_cnt
else:
return skip_tick_cnt
else: # now shuffle ends
if (board[x-2][0] is not None and
# in same row
board[x-2][0].target.x == self.target.x and
# on same side
board[x-2][0].target.y * self.target.y > 0 and
# it's The One, who elese could it be
not self.state.endswith('2b') and
not (abs(board[x-2][0].target.y) > abs(self.target.y))): # either way will be first
return skip_tick_cnt
else:
if board[x-1][0] is None:
self.move(board, -1, 0)
self.state = 'entering'
return walk_tick_cnt
else:
return skip_tick_cnt
elif self.state.endswith('c'):
if y != 0:
dy = -y//abs(y)
if board[x][y+dy] is None and no_shuffle_conflict(board, x, 0) and board[x][2*y].state.startswith('shuffle'):
self.move(board, 0, dy)
return walk_tick_cnt
else:
return skip_tick_cnt
elif y == 0 and x == self.target.x and no_shuffle_conflict(board, x+1, 0):
if board[x+1][0] is None:
self.move(board, 1, 0)
return walk_tick_cnt
else:
return skip_tick_cnt
elif x == self.target.x+1:
if (board[x-2][0] is not None and
# in same row
board[x-2][0].target.x == self.target.x and
board[x-2][0].target.y * self.target.y > 0): # on same side
if board[x+1][0] is None:
self.move(board, 1, 0)
return walk_tick_cnt
else:
return skip_tick_cnt
else:
if board[x-1][0] is None:
self.move(board, -1, 0)
self.state = 'entering'
return walk_tick_cnt
else:
return skip_tick_cnt
else:
if (board[x-3][0] is not None and
# in same row
board[x-3][0].target.x == self.target.x and
board[x-3][0].target.y * self.target.y > 0): # on same side
return skip_tick_cnt
else: # idziemy w lewo
if board[x-1][0] is None:
self.move(board, -1, 0)
return walk_tick_cnt
else:
return skip_tick_cnt
else:
raise Exception(f"Agent {self} in invalid 'shuffle' state")
return skip_tick_cnt
if self.state == 'go':
return go()
if self.state == 'ns_go':
return ns_go()
elif self.state == 'my_turn':
return my_turn()
elif self.state == 'luggage':
return luggage()
elif self.state == 'ns_luggage':
return ns_luggage()
elif self.state == 'entering':
return entering()
elif self.state == 'ns_entering':
return ns_entering()
elif self.state == 'is_that_all':
return is_that_all()
elif self.state == 'waiting':
return waiting()
elif self.state.startswith('shuffle'):
return shuffle()
elif self.state == 'done':
raise Exception(
"Agent {self} in state 'done' appeared in the 'execution_queue'")
elif self.state == 'ns_done':
raise Exception(
"Agent {self} in state 'ns_done' appeared in the 'execution_queue'")
else:
return None
def no_shuffle_conflict(board, x, y):
return all(
board[x+dx][0] is None
or
board[x+dx][0].coords.x <= board[x+dx][0].target.x
for dx
in range(1, 1+max_shuffle)
)
def no_shuffle_other(board, tx, ty):
# no shuffle for any other agent here
return all(
board[tx+dx][0] is None
or
board[tx+dx][0].target.x != tx # not in shuffle here
or
board[tx+dx][0].target.y * ty > 0 # target on the same side
and
True
# idę window & shuffle_2 = moje
# idę window & shuffle_1 & idzie middle = moje
# idę window & shuffle_1 & idzie ailse = mogę iść, bo jeśli nie moje to poczekam
# idę middle = jak się wepchnę to będzie spoko
for dx
in range(1, 1+max_shuffle)
)
def no_shuffle_need(board, x, y):
ys = range(0, y, 1 if y > 0 else -1)
return all(
board[x][_y] is None or board[x][_y].target.y not in ys
for _y
in ys
)
def get_shuffle_need(board, x, y):
ys = range(0, y, 1 if y > 0 else -1)
if all(
board[x][_y] is None or board[x][_y].target.y not in ys
for _y
in ys
):
return (None, None)
else:
return tuple(
board[x][_y]
for _y
in ys
)
def pickle_as(name, data):
with open(f"./pickle_dumps/{name}.pkl", 'wb') as f:
pickle.dump(data, f)
def initialize(board, execution_queue: PriorityQueue, boarding_method):
"""
Fills board and execution_queue with freshly generated Agents.
Sets up their target, position, group and puts them in random
order in the execution_queue as well as in the plane queue.
"""
if False:
pass
elif boarding_method == 'random_order':
time_agents = [(0, Agent(Point(tx, ty*(2*s-1)), Point(0, 0), None)) for tx in range(plane_length)
for ty in range(1, 4) for s in range(2)]
shuffle(time_agents)
for i, (time, agent) in enumerate(time_agents):
agent.coords.x = i-total_agents-1
elif boarding_method == 'back_to_front':
time_agents = [(0, Agent(Point(tx, ty*(2*s-1)), Point(tx*6+s*3+(ty-1)-1-total_agents, 0), None)) for tx in range(plane_length)
for ty in range(1, 4) for s in range(2)]
elif boarding_method == 'front_to_back':
time_agents = [(0, Agent(Point(tx, (4-ty)*(2*s-1)), Point(-tx*6-s*3-(ty-1)-1, 0), None)) for tx in range(plane_length)
for ty in range(1, 4) for s in range(2)]
elif boarding_method == 'back_to_front_four':
time_agentss = [[(0, Agent(Point(plane_length//4*(3-group) + tx, ty*(2*s-1)), Point(0, 0), group)) for tx in range(plane_length//4)
for ty in range(1, 4) for s in range(2)] for group in range(4)]
for time_agents in time_agentss:
shuffle(time_agents)
for group, time_agents in enumerate(time_agentss):
for i, (time, agent) in enumerate(time_agents):
# 0,1,.. -> -2,-3,..
agent.coords.x = -(total_agents//4*group+i+2)
time_agents = chain(*time_agentss)
elif boarding_method == 'front_to_back_four':
time_agentss = [[(0, Agent(Point(plane_length//4*group + tx, ty*(2*s-1)), Point(0, 0), group)) for tx in range(plane_length//4)
for ty in range(1, 4) for s in range(2)] for group in range(4)]
for time_agents in time_agentss:
shuffle(time_agents)
for group, time_agents in enumerate(time_agentss):
for i, (time, agent) in enumerate(time_agents):
# 0,1,.. -> -2,-3,..
agent.coords.x = -(total_agents//4*group+i+2)
time_agents = chain(*time_agentss)
elif boarding_method == 'window_middle_aisle':
time_agentss = [[(0, Agent(Point(tx, side*column), Point(0, 0), column))
for side in {-1, 1} for tx in range(0, plane_length)] for column in [3, 2, 1]]
for time_agents in time_agentss:
shuffle(time_agents)
for group, time_agents in enumerate(time_agentss):
for i, (time, agent) in enumerate(time_agents):
# 0,1,.. -> -2,-3,..
agent.coords.x = -(total_agents//3*group+i+2)
time_agents = chain(*time_agentss)
elif boarding_method == 'aisle_middle_window':
time_agentss = [[(0, Agent(Point(tx, side*column), Point(0, 0), column))
for side in {-1, 1} for tx in range(0, plane_length)] for column in [1, 2, 3]]
for time_agents in time_agentss:
shuffle(time_agents)
for group, time_agents in enumerate(time_agentss):
for i, (time, agent) in enumerate(time_agents):
# 0,1,.. -> -2,-3,..
agent.coords.x = -(total_agents//3*group+i+2)
time_agents = chain(*time_agentss)
elif boarding_method == 'steffen_perfect':
time_agents = [(0, Agent(Point((plane_length-2-tx)+shift, column*side), Point(0, 0), None)) for column in {3, 2, 1} for shift in {
1, 0} for side in {-1, 1} for tx in reversed(range(0, plane_length, 2))]
for i, (time, agent) in enumerate(time_agents):
agent.coords.x = i-total_agents-1
elif boarding_method == 'steffen_modified':
time_agents = [(0, Agent(Point((plane_length-2-tx)+shift, column*side), Point(0, 0), None)) for shift in {
1, 0} for side in {-1, 1} for tx in reversed(range(0, plane_length, 2)) for column in {3, 2, 1}]
for i, (time, agent) in enumerate(time_agents):
agent.coords.x = i-total_agents-1
elif boarding_method == '_test_0wr':
target = Point(0, -seats_right)
coords = Point(-1, aisle_y)
x, y = astuple(coords)
agent = Agent(target, coords, 0)
board[x][y] = agent
execution_queue.put((0, agent))
return [(0, agent)]
elif boarding_method == '_test_2r_shuffle':
t1 = Point(0, -seats_right+0)
t2 = Point(0, -seats_right+1)
c1 = Point(-2+0, aisle_y)
c2 = Point(-2+1, aisle_y)
agents = []
for target, coords in [(t1, c1), (t2, c2)]:
x, y = astuple(coords)
agent = Agent(target, coords, 0)
board[x][y] = agent
execution_queue.put((0, agent))
agents.append((0, agent))
return agents
elif boarding_method == '_test_2s_shuffle':
t1 = Point(0, -seats_right+0)
t2 = Point(0, -seats_right+1)
c1 = Point(-2+1, aisle_y)
c2 = Point(-2+0, aisle_y)
agents = []
for target, coords in [(t1, c1), (t2, c2)]:
x, y = astuple(coords)
agent = Agent(target, coords, 0)
board[x][y] = agent
execution_queue.put((0, agent))
agents.append((0, agent))
return agent
elif boarding_method == '_test_3r_shuffle':
t1 = Point(0, -seats_right+0)
t2 = Point(0, -seats_right+1)
t3 = Point(0, -seats_right+2)
c1 = Point(-3+0, aisle_y)
c2 = Point(-3+1, aisle_y)
c3 = Point(-3+2, aisle_y)
agents = []
for target, coords in [(t1, c1), (t2, c2), (t3, c3)]:
x, y = astuple(coords)
agent = Agent(target, coords, 0)
board[x][y] = agent
execution_queue.put((0, agent))
agents.append((0, agent))
return agents
elif boarding_method == '_test_3s_shuffle':
t1 = Point(0, -seats_right+0)
t2 = Point(0, -seats_right+1)
t3 = Point(0, -seats_right+2)
c1 = Point(-3+2, aisle_y)
c2 = Point(-3+1, aisle_y)
c3 = Point(-3+0, aisle_y)
agents = []
for target, coords in [(t1, c1), (t2, c2), (t3, c3)]:
x, y = astuple(coords)
agent = Agent(target, coords, 0)
board[x][y] = agent
execution_queue.put((0, agent))
agents.append((0, agent))
return agents
else:
raise Exception(f"'{boarding_method}' boarding method not implemented")
for time, agent in time_agents:
board[agent.coords.x][agent.coords.y] = agent
execution_queue.put((time, agent))
return [(time, agent) for time, agent in time_agents]