-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoves.py
More file actions
282 lines (152 loc) · 9.48 KB
/
moves.py
File metadata and controls
282 lines (152 loc) · 9.48 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
## MOVEMENTS
import copy
from collections import namedtuple
from util import *
from conditions import *
from swarm_types import *
def rotate(A: namedtuple) -> namedtuple:
new_A = A._replace(width = A.height, height = A.width, last_move = 'rotate')
return new_A
def action_correction(A: namedtuple, layout_zone : namedtuple) -> namedtuple:
_, extend, _ = calculate_protrusion(layout_zone, A)
new_A = A._replace(xmin = A.xmin + extend[0], ymin = A.ymin + extend[1])
return new_A
# SWARM Movements
def reenter(A : namedtuple, layout_zone : namedtuple) -> list: # Only activated and working in case of a totally lost participant
participant_left_of_layout_zone = (A.xmin < layout_zone.xmin)
participant_right_of_layout_zone = (A.xmin + A.width >= layout_zone.xmin + layout_zone.width) # To detect if an participant is at the north eastern corner of the layout zone
participant_above_layout_zone = (A.ymin >= layout_zone.ymin + layout_zone.height)
participant_below_layout_zone = (A.ymin < layout_zone.ymin)
x_min_new = A.xmin
y_min_new = A.ymin
x_min_new = layout_zone.xmin if participant_left_of_layout_zone else x_min_new
x_min_new = (layout_zone.xmin + layout_zone.width - A.width) if participant_right_of_layout_zone else x_min_new
y_min_new = layout_zone.ymin if participant_below_layout_zone else y_min_new
y_min_new = (layout_zone.ymin + layout_zone.height - A.height) if participant_above_layout_zone else y_min_new
new_A = A._replace(xmin = x_min_new, ymin = y_min_new, last_move = 'reenter')
return [new_A]
def evade(A : namedtuple, layout_zone : namedtuple, layout_zone_edge : str, align_position : str) -> list:
if layout_zone_edge == 'north':
if align_position == 'left':
x_min_new = layout_zone.xmin
y_min_new = (layout_zone.ymin + layout_zone.height - A.height)
elif align_position == 'center':
x_min_new = int(layout_zone.xmin + 0.5 * layout_zone.width - 0.5 * A.width)
y_min_new = (layout_zone.ymin + layout_zone.height - A.height)
else: #right
x_min_new = layout_zone.xmin + layout_zone.width - A.width
y_min_new = (layout_zone.ymin + layout_zone.height - A.height)
elif layout_zone_edge == 'east': # Rotate layout zone clockwise virtually for edge orientation
if align_position == 'left':
x_min_new = layout_zone.xmin + layout_zone.width - A.width
y_min_new = (layout_zone.ymin + layout_zone.height - A.height)
elif align_position == 'center':
x_min_new = layout_zone.xmin + layout_zone.width - A.width
y_min_new = int((layout_zone.ymin + 0.5 * layout_zone.height - 0.5 * A.height))
else:
x_min_new = layout_zone.xmin + layout_zone.width - A.width
y_min_new = layout_zone.ymin
elif layout_zone_edge == 'south':
if align_position == 'left':
x_min_new = layout_zone.xmin
y_min_new = layout_zone.ymin
elif align_position == 'center':
x_min_new = int(layout_zone.xmin + 0.5 * layout_zone.width - 0.5 * A.width)
y_min_new = layout_zone.ymin
else:
x_min_new = layout_zone.xmin + layout_zone.width - A.width
y_min_new = layout_zone.ymin
elif layout_zone_edge == 'west': # Rotate layout zone counter-clockwise virtually for edge naming orientation
if align_position == 'left':
x_min_new = layout_zone.xmin
y_min_new = (layout_zone.ymin + layout_zone.height - A.height)
elif align_position == 'center':
x_min_new = layout_zone.xmin
y_min_new = int((layout_zone.ymin + 0.5 * layout_zone.height - 0.5 * A.height))
else:
x_min_new = layout_zone.xmin
y_min_new = layout_zone.ymin
else:
print('No correct edge given!')
new_A = A._replace(xmin = x_min_new, ymin = y_min_new, last_move = 'evade')
return [new_A]
def center(A: namedtuple) -> list:
x_min_new = int(A.freespace.xmin + 0.5 * A.freespace.width - 0.5 * A.width)
y_min_new = int(A.freespace.ymin + 0.5 * A.freespace.height - 0.5 * A.height)
new_A = A._replace(xmin = x_min_new, ymin = y_min_new, last_move = 'center')
return [new_A]
def linger(A: namedtuple) -> list:
new_A = A._replace(last_move = 'linger')
return [new_A]
def budge(A: namedtuple, direction : str) -> list:
secondary_free_space = getattr(A, direction)
x_min_new = int(secondary_free_space.xmin + 0.5 * secondary_free_space.width - 0.5 * A.width)
y_min_new = int(secondary_free_space.ymin + 0.5 * secondary_free_space.height - 0.5 * A.height)
new_A = A._replace(xmin = x_min_new, ymin = y_min_new, last_move = 'budge')
return [new_A]
def swap(A: namedtuple, B: namedtuple) -> list:
new_A = A._replace(xmin = B.xmin, ymin = B.ymin, last_move = 'swap')
new_B = B._replace(xmin = A.xmin, ymin = A.ymin, last_move = 'swap')
return [new_A, new_B]
def pair(A : namedtuple, B : namedtuple, direction : str, layout_zone : namedtuple) -> list:
if direction == 'horizontal-push-right' :
x_min_new_A = int(B.xmin - 0.5 * A.width)
x_min_new_B = int(B.xmin + 0.5 * A.width)
y_min_new_A = B.ymin
y_min_new_B = B.ymin
elif direction == 'horizontal-push-left' :
x_min_new_A = int(B.xmin + B.width - 0.5 * A.width)
x_min_new_B = int(B.xmin - 0.5 * A.width)
y_min_new_A = B.ymin
y_min_new_B = B.ymin
elif direction == 'vertical-push-up' :
x_min_new_A = B.xmin
x_min_new_B = B.xmin
y_min_new_A = int(B.ymin - 0.5 * A.height)
y_min_new_B = int(B.ymin + 0.5 * A.height)
elif direction == 'vertical-push-down':
x_min_new_A = B.xmin
x_min_new_B = B.xmin
y_min_new_A = int(B.ymin + B.height - 0.5 * A.height)
y_min_new_B = int(B.ymin - 0.5 * A.height)
new_A = A._replace(xmin = x_min_new_A, ymin = y_min_new_A, last_move = 'pair with ' + B.idx)
new_B = B._replace(xmin = x_min_new_B, ymin = y_min_new_B, last_move = 'pair with ' + A.idx)
# Action correction to not push other participants out of the safe zone
new_B_corrected = action_correction(new_B, layout_zone)
return [new_A, new_B_corrected]
def hustle(A : namedtuple, layout_zone : namedtuple, participants : namedtuple) -> list:
moved_participants = []
overlapping_participants = [p for p in participants if p.idx in A.overlap_with_idx]
for B in overlapping_participants:
overlap, locations = calculate_overlap(A, B)
if locations[1]: # B fully encloses A -> A can not hustle but must flee // Added to prevent stuck state in case of full overlap
if B.ymin > 0.5 * (layout_zone.ymin + layout_zone.height): # Enclosure in the upper half of the layout zone -> A flees south
xmin_new_A = int(B.xmin + 0.5 * B.width - 0.5 * A.width)
ymin_new_A = int(B.ymin - 0.5 * A.height)
else: # A flees north
xmin_new_A = int(B.xmin + 0.5 * B.width - 0.5 * A.width)
ymin_new_A = int(B.ymin + B.height - 0.5 * A.height)
new_A = A._replace(xmin = xmin_new_A, ymin = ymin_new_A, last_move = 'flee')
break
else:
if overlap.width <= overlap.height:
delta_x = overlap.width if B.xmin > A.xmin else overlap.width * -1
delta_y = 0
else:
delta_x = 0
delta_y = overlap.height if B.ymin >= A.ymin else overlap.height * -1
x_min_new_B = B.xmin + delta_x
y_min_new_B = B.ymin + delta_y
new_B = B._replace(xmin = x_min_new_B, ymin = y_min_new_B, last_move = ' got hustled by ' + A.idx)
# Action correction to not push other participants out of the safe zone
new_B_corrected = action_correction(new_B, layout_zone)
moved_participants = moved_participants + [new_B_corrected]
new_A = A._replace(last_move = 'hustle')
return [new_A] + moved_participants
def yielt(A : namedtuple) -> list: # Intentional typo in "yield" to avoid keyword
x_center_yield_poly = A.yield_polygon.xmin + 0.5 * A.yield_polygon.width
y_center_yield_poly = A.yield_polygon.ymin + 0.5 * A.yield_polygon.height
x_min_new_A = x_center_yield_poly - 0.5 * A.width
y_min_new_A = y_center_yield_poly - 0.5 * A.height
new_A = A._replace(xmin = x_min_new_A, ymin = y_min_new_A, last_move = 'yield')
return [new_A]