-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel1.py
94 lines (77 loc) · 2.44 KB
/
model1.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
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
class Car:
def __init__(self, v_max, reaction_time, acceleration, position) -> None:
self.v_max : int = v_max
self.r : int = reaction_time
self.a : int = acceleration
self.pos : int = position
self.v : int = v_max
self.pre_car : Car = None
def __repr__(self) -> str:
return "Car(v=%i, r=%i, a=%i, pos=%i, v_max=%i)" % (self.v, self.r, self.a, self.pos, self.v_max)
class MemCell:
def __init__(self, v, pos) -> None:
self.v = v
self.pos = pos
def __repr__(self) -> str:
return "MemCell(v=%i, pos=%i)" % (self.v, self.pos)
N = 50
V_MAX = 400
D_TOT = 31415
REACTION_TIME = 1
ACCELERATION = 30
dt = 1
def setup_cars():
cars = [Car(V_MAX, REACTION_TIME, ACCELERATION, int(i/N*D_TOT)) for i in range(N)]
memory = []
for i in range(REACTION_TIME):
time_instance = []
for car in cars:
time_instance.append(MemCell(car.v, car.pos))
memory.append(time_instance)
cars[0].v = 0
cars[1].v = 0
cars[2].v = 0
return cars, memory
def move(car, pre_speed, dt):
if car.v > pre_speed:
car.v -= car.a * dt
elif car.v >= car.v_max:
car.v = car.v_max
else:
car.v += car.a * dt
car.pos = (car.pos + car.v * dt) % D_TOT
def update_mem(mem, speeds):
mem.pop()
mem.insert(0, speeds)
return mem
def update(cars, memory):
new_mem = []
for i, car in enumerate(cars):
new_mem.append(MemCell(car.v, car.pos))
move(car, memory[car.r - 1][(i + 1) % N].v, dt)
memory = update_mem(memory, new_mem)
def draw(cars, memory):
fig = plt.figure()
axis = plt.axes(xlim=(-1.1, 1.1), ylim=(-1.1, 1.1))
axis.set_aspect('equal')
line, = axis.plot([], [], "rs")
def init():
thetas = [2*np.pi * car.pos/D_TOT for car in cars]
line.set_data(np.cos(thetas), np.sin(thetas))
circle_road = plt.Circle((0, 0), 1 , fill = False)
axis.add_artist(circle_road)
return line,
def animate(frame):
update(cars, memory)
thetas = [2*np.pi * memcell.pos/D_TOT for memcell in memory[0]]
line.set_data(np.cos(thetas), np.sin(thetas))
return line,
anim = FuncAnimation(fig, animate, init_func=init, interval=40, blit=True)
plt.show()
def main():
cars, memory = setup_cars()
draw(cars, memory)
main()