Repository navigation
Expand file tree
/
Copy pathVirtualBug.py
More file actions
194 lines (169 loc) · 5.64 KB
/
Copy pathVirtualBug.py
File metadata and controls
194 lines (169 loc) · 5.64 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
import turtle
import numpy as np
from matplotlib import pyplot as plt
class VirtualBug:
def __init__(self, noise_level=.5):
"""
A class to model
:param noise_level:
"""
self.noise_level = noise_level
self.min_x = 0
self.max_x = 1000
self.min_y = 0
self.max_y = 500
self.x = np.random.randint(self.min_x, self.max_x)
self.y = np.random.randint(self.min_y, self.max_y)
self.heading = np.random.random() * 2 * np.pi
self.velocity = np.random.random() * 10
self.last_moved = 0 # distance moved last
def move(self):
"""
advance the bug 1 move, randomly perturb velocity based on noise level prior to doing so.
:return: Nothing
"""
heading_adjust = (np.random.random() - .5) * self.noise_level
self.heading += heading_adjust
old_x, old_y = self.x, self.y
self.x += self.velocity * np.cos(self.heading)
self.y += self.velocity * np.sin(self.heading)
if self.x > self.max_x:
self.x = 2 * self.max_x - self.x
self.heading = np.pi - self.heading
if self.x < self.min_x:
self.x = abs(self.x)
self.heading = np.pi - self.heading
if self.y > self.max_y:
self.y = 2 * self.max_y - self.y
self.heading = 2*np.pi - self.heading
if self.y < self.min_y:
self.y = abs(self.y)
self.heading = 2*np.pi - self.heading
self.last_moved = np.sqrt((old_x - self.x)**2 + (old_y - self.y)**2)
def move_trial(moves):
positions = np.zeros((moves, 3))
bug = VirtualBug()
for i in range(moves):
positions[i, 0], positions[i, 1] = bug.x, bug.y
positions[i, 2] = i
bug.move()
return positions
def static_plot(moves):
fig, ax = plt.subplots()
ax.scatter(moves[:, 0], moves[:, 1], c=moves[:, 2], alpha=0.5)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
ax.plot(moves[0, 0], moves[0, 1], 'r*')
fig.tight_layout()
plt.show()
def dynamic_tracking(bug, moves):
canvas = turtle.Screen()
pad = 100
canvas.setup(bug.max_x + pad, bug.max_y + pad)
# coordinate system needs to be transformed by half of the bounds of the box
# draw box
t = turtle.Turtle()
t.penup()
t.speed("fastest")
t.setx(-bug.max_x*1./2)
t.sety(-bug.max_y*1./2)
t.pendown()
t.forward(bug.max_x)
t.left(90)
t.forward(bug.max_y)
t.left(90)
t.forward(bug.max_x)
t.left(90)
t.forward(bug.max_y)
t.penup()
t.speed("fast")
t.color("red", "red")
# simulate moves
t.setheading(bug.heading*360./(2*np.pi))
t.setx(bug.x - bug.max_x * 1. / 2)
t.sety(bug.y - bug.max_y * 1. / 2)
t.pendown()
for i in range(moves):
bug.move()
t.setheading(bug.heading * 360. / (2 * np.pi))
t.forward(bug.last_moved)
canvas.exitonclick()
class BugZone:
def __init__(self, nBugs, visualize=False, *args):
"""
:param nBugs:
:param xshape:
:param yshape:
:param visualize:
:param args:
"""
self.nBugs = nBugs
self.visualize = visualize
self.canvas = None
self.bugs = []
for i in range(self.nBugs):
self.bugs.append(VirtualBug())
#self.bugs[-1].max_x = self.xshape
#self.bugs[-1].max_y = self.yshape
self.xshape = self.bugs[-1].max_x
self.yshape = self.bugs[-1].max_y
if self.visualize:
self.pad = 100
self.turtles = []
self.setup_canvas()
turtle.tracer(0, 0)
for b in self.bugs:
self.turtles.append(turtle.Turtle())
#self.turtles[-1].tracer(False)
self.turtles[-1].penup()
self.turtles[-1].setheading(b.heading * 360. / (2 * np.pi))
self.turtles[-1].setx(b.x - b.max_x * 1. / 2)
self.turtles[-1].sety(b.y - b.max_y * 1. / 2)
r, g, b = np.random.randint(1, 255), np.random.randint(1, 255), np.random.randint(1, 255)
self.canvas.colormode(255)
self.turtles[-1].color(r, g, b)
self.turtles[-1].pendown()
turtle.update()
def setup_canvas(self):
self.canvas = turtle.Screen()
self.canvas.setup(self.xshape + self.pad, self.yshape + self.pad)
# coordinate system needs to be transformed by half of the bounds of the box
# draw box
t = turtle.Turtle()
t.hideturtle()
t.penup()
t.speed("fastest")
t.setx(-self.xshape * 1. / 2)
t.sety(-self.yshape * 1. / 2)
t.pendown()
t.forward(self.xshape)
t.left(90)
t.forward(self.yshape)
t.left(90)
t.forward(self.xshape)
t.left(90)
t.forward(self.yshape)
t.penup()
def simulate_movement(self, nmoves):
for i in range(nmoves):
#self.canvas.ontimer(self.move1())
self.move1()
turtle.update()
self.canvas.exitonclick()
def move1(self):
for k in range(len(self.bugs)):
self.bugs[k].move()
self.turtles[k].setheading((self.bugs[k].heading * 360. / (2 * np.pi)) % 360)
self.turtles[k].forward(self.bugs[k].last_moved)
x, y = self.bugs[k].x, self.bugs[k].y
tx, ty = self.turtles[k].xcor(), self.turtles[k].ycor()
if __name__ == "__main__":
'''
pos = move_trial(10000)
static_plot(pos)
b = VirtualBug()
dynamic_tracking(b, 10000)
'''
b = BugZone(10, True)
b.simulate_movement(5000)