-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
251 lines (207 loc) · 9.1 KB
/
main.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
from time import time
from base import *
class RWP1Car(SynCar):
def set_target(self):
if self.target_idx == len(self.targets):
px, py = self.get_prev_target()
# if the source has reached the last target,
# append the previous target so that it won't generate a new one
if self.index == 0:
self.targets.append((px, py))
return
tx = self.rand.uniform(0, X_MAX)
ty = self.rand.uniform(0, Y_MAX)
while tx == px and ty == py:
tx = self.rand.uniform(0, X_MAX)
ty = self.rand.uniform(0, Y_MAX)
self.targets.append((tx, ty))
class RWP2Car(SynCar):
def set_target(self):
if self.target_idx == len(self.targets):
px, py = self.get_prev_target()
# if the source has reached the last target,
# append the previous target so that it won't generate a new one
if self.index == 0:
self.targets.append((px, py))
return
cx, cy = self.get_pos()
x_max = cx + 0.5 * X_MAX
x_min = cx - 0.5 * X_MAX
y_max = cy + 0.5 * Y_MAX
y_min = cy - 0.5 * Y_MAX
tx = self.rand.uniform(x_min, x_max)
ty = self.rand.uniform(y_min, y_max)
while tx == px and ty == py:
tx = self.rand.uniform(x_min, x_max)
ty = self.rand.uniform(y_min, y_max)
self.targets.append((tx, ty))
class RDCar(SynCar):
def set_target(self):
if self.target_idx == len(self.targets):
px, py = self.get_prev_target()
# if the source has reached the last target,
# append the previous target so that it won't generate a new one
if self.index == 0:
self.targets.append((px, py))
return
max_target = 2 * X_MAX + 2 * Y_MAX
while True:
raw_target = self.rand.uniform(0, max_target)
if 0 <= raw_target < X_MAX:
target = (raw_target, 0)
elif X_MAX <= raw_target < X_MAX + Y_MAX:
raw_target -= X_MAX
target = (X_MAX, raw_target)
elif X_MAX + Y_MAX <= raw_target < 2 * X_MAX + Y_MAX:
raw_target -= (X_MAX + Y_MAX)
target = ((X_MAX - raw_target), Y_MAX)
else:
raw_target -= (2 * X_MAX + Y_MAX)
target = (0, (Y_MAX - raw_target))
if px == 0 and target[0] == 0:
continue
if px == X_MAX and target[0] == X_MAX:
continue
if py == 0 and target[1] == 0:
continue
if py == Y_MAX and target[1] == Y_MAX:
continue
break
self.targets.append(target)
class MG1Car(SynMGCar):
def __init__(self, index, seed, source_pos, targets=None):
super().__init__(index, seed, source_pos, targets, 1)
def set_target(self):
if self.target_idx == len(self.targets):
px, py = self.get_prev_target()
# if the source has reached the last target,
# append the previous target so that it won't generate a new one
if self.index == 0:
self.targets.append((px, py))
return
cx, cy = self.get_pos()
dirs = [(cx - 1, cy), (cx + 1, cy), (cx, cy - 1), (cx, cy + 1)]
if cx == 0:
dirs.remove((cx - 1, cy))
elif cx == X_MAX:
dirs.remove((cx + 1, cy))
if cy == 0:
dirs.remove((cx, cy - 1))
elif cy == Y_MAX:
dirs.remove((cx, cy + 1))
assert len(dirs) in [2, 3, 4]
if len(dirs) == 2:
weights = [0.5 for _ in range(2)]
elif len(dirs) == 3:
weights = [1 / 3 for _ in range(3)]
else:
weights = [0.25 for _ in range(4)]
if len(self.courses) == 1:
dir = self.rand.choices(dirs, weights)
assert len(dir) == 1
dir = dir[0]
else:
last_x, last_y = self.courses[-2]
assert (last_x, last_y) in dirs
last_idx = dirs.index((last_x, last_y))
if (last_x, last_y) == (cx - 1, cy):
opposite = (cx + 1, cy)
elif (last_x, last_y) == (cx + 1, cy):
opposite = (cx - 1, cy)
elif (last_x, last_y) == (cx, cy - 1):
opposite = (cx, cy + 1)
else:
opposite = (cx, cy - 1)
plus_idx = dirs.index(opposite) if opposite in dirs else -1
plus_weight, weights[last_idx] = weights[last_idx], 0
if plus_idx != -1:
weights[plus_idx] += plus_weight
else:
for i, wei in enumerate(weights):
if wei != 0:
weights[i] = wei + plus_weight * (1 / (len(weights) - 1))
dir = self.rand.choices(dirs, weights)
assert len(dir) == 1
dir = dir[0]
self.targets.append(dir)
class MG2Car(SynMGCar):
def __init__(self, index, seed, source_pos, targets=None):
super().__init__(index, seed, source_pos, targets, 2)
def set_target(self):
if self.target_idx == len(self.targets):
px, py = self.get_prev_target()
# if the source has reached the last target,
# append the previous target so that it won't generate a new one
if self.index == 0:
self.targets.append((px, py))
return
cx, cy = self.get_pos()
dirs = [(cx - 1, cy), (cx + 1, cy), (cx, cy - 1), (cx, cy + 1)]
dirs = [(x % X_MAX, y % Y_MAX) for (x, y) in dirs]
weights = [0.25 for _ in range(4)]
if len(self.courses) == 1:
dir = self.rand.choices(dirs, weights)
assert len(dir) == 1
dir = dir[0]
else:
last_x, last_y = self.courses[-2]
last_x, last_y = last_x % X_MAX, last_y % Y_MAX
assert (last_x, last_y) in dirs
last_idx = dirs.index((last_x, last_y))
if (last_x, last_y) == ((cx - 1) % X_MAX, cy % Y_MAX):
opposite = (cx + 1, cy)
elif (last_x, last_y) == ((cx + 1) % X_MAX, cy % Y_MAX):
opposite = (cx - 1, cy)
elif (last_x, last_y) == (cx % X_MAX, (cy - 1) % Y_MAX):
opposite = (cx, cy + 1)
else:
opposite = (cx, cy - 1)
opposite = opposite[0] % X_MAX, opposite[1] % Y_MAX
# in torus map, the opposite is always an option
plus_idx = dirs.index(opposite)
plus_weight, weights[last_idx] = weights[last_idx], 0
weights[plus_idx] += plus_weight
dir = self.rand.choices(dirs, weights)
assert len(dir) == 1
dir = dir[0]
# dir = round(dir[0], 0), round(dir[1], 0)
self.targets.append(dir)
class RWP1Simulation(SynSimulation):
def __init__(self, seed, source_pos, source_source):
super().__init__()
self.cars.append(RWP1Car(0, seed, source_pos, source_source))
self.cars.extend([RWP1Car(i, seed, source_pos) for i in range(1, NUM_OF_CARS)])
class RWP2Simulation(TorSynSimulation):
def __init__(self, seed, source_pos, source_source):
super().__init__()
self.cars.append(RWP2Car(0, seed, source_pos, source_source))
self.cars.extend([RWP2Car(i, seed, source_pos) for i in range(1, NUM_OF_CARS)])
class RDSimulation(SynSimulation):
def __init__(self, seed, source_pos, source_source):
super().__init__()
self.cars.append(RDCar(0, seed, source_pos, source_source))
self.cars.extend([RDCar(i, seed, source_pos) for i in range(1, NUM_OF_CARS)])
class MG1Simulation(SynSimulation):
def __init__(self, seed, source_pos, source_source):
super().__init__()
self.cars.append(MG1Car(0, seed, source_pos, source_source))
self.cars.extend([MG1Car(i, seed, source_pos) for i in range(1, NUM_OF_CARS)])
class MG2Simulation(TorSynSimulation):
def __init__(self, seed, source_pos, source_source):
super().__init__()
self.cars.append(MG2Car(0, seed, source_pos, source_source))
self.cars.extend([MG2Car(i, seed, source_pos) for i in range(1, NUM_OF_CARS)])
if __name__ == '__main__':
pass
typ = "RWP2"
RAND_SEED = "%.30f" % time()
# 1584493223.638249874114990234375000000000]
print(RAND_SEED)
SOURCE_POS = (0, 0)
sim = RWP2Simulation(RAND_SEED, SOURCE_POS, RWP2_zigzag_23())
sim.simulate()
print(sim.cars[0].courses)
for i in range(200):
gui = GUISnapshot2(sim, i)
gui.draw()
gui.save(f"./pngs/{i}")