質問編集履歴

2

追記2を追加

2019/12/10 12:53

投稿

shmpwk
shmpwk

スコア13

test CHANGED
File without changes
test CHANGED
@@ -94,7 +94,7 @@
94
94
 
95
95
 
96
96
 
97
- [追記]
97
+ [追記1]
98
98
 
99
99
  コードを一部抜粋したものを載せました。汚いコードですみません。
100
100
 
@@ -265,3 +265,9 @@
265
265
  run()
266
266
 
267
267
  ```
268
+
269
+
270
+
271
+ [追記2]
272
+
273
+ そもそもVectorというオブジェクトを作らずにnumpy?など既存のものを使えば避けられる問題なのでしょうか。

1

全体のコードの追加

2019/12/10 12:53

投稿

shmpwk
shmpwk

スコア13

test CHANGED
File without changes
test CHANGED
@@ -91,3 +91,177 @@
91
91
 
92
92
 
93
93
  どこを直せば良いか検討がつきません。ご教授お願いします。
94
+
95
+
96
+
97
+ [追記]
98
+
99
+ コードを一部抜粋したものを載せました。汚いコードですみません。
100
+
101
+ 実行すると、上記と同じエラーが出ます。
102
+
103
+
104
+
105
+ ```python
106
+
107
+ import sys
108
+
109
+ sys.setrecursionlimit(10000)
110
+
111
+
112
+
113
+
114
+
115
+ class Vector:
116
+
117
+ def __init__(self, x, y):
118
+
119
+ self.x = x
120
+
121
+ self.y = y
122
+
123
+ def __add__(self, v):
124
+
125
+ self.x = self.x + v.x
126
+
127
+ self.y = self.y + v.y
128
+
129
+ return self
130
+
131
+ def __sub__(self, v):
132
+
133
+ self.x = self.x - v.x
134
+
135
+ self.y = self.y - v.y
136
+
137
+ return self
138
+
139
+ def __mul__(self, v):
140
+
141
+ self.x = self.x * v.x
142
+
143
+ self.y = self.y * v.y
144
+
145
+ return self
146
+
147
+ def sqrt(self):
148
+
149
+ self.x = math.sqrt(self.x)
150
+
151
+ self.y = math.sqrt(self.y)
152
+
153
+ return self
154
+
155
+ def __lt__(self, other):
156
+
157
+ return any((
158
+
159
+ self == other,
160
+
161
+ self < other,
162
+
163
+ ))
164
+
165
+ class Boid():
166
+
167
+
168
+
169
+ def __init__(self, x, y, width, height):
170
+
171
+ self.position = Vector(x, y)
172
+
173
+ vec = (np.random.rand(2) - 0.5)*10
174
+
175
+ self.velocity = Vector(*vec)
176
+
177
+
178
+
179
+ vec = (np.random.rand(2) - 0.5)/2
180
+
181
+ self.acceleration = Vector(*vec)
182
+
183
+ self.max_force = 0.3
184
+
185
+ self.max_speed = 5
186
+
187
+ self.perception = 100
188
+
189
+
190
+
191
+ def apply_behaviour(self, boids):
192
+
193
+ alignment = self.align(boids)
194
+
195
+ cohesion = self.cohesion(boids)
196
+
197
+ separation = self.separation(boids)
198
+
199
+
200
+
201
+ self.acceleration += alignment
202
+
203
+ self.acceleration += cohesion
204
+
205
+ self.acceleration += separation
206
+
207
+ self.width = width
208
+
209
+ self.height = height
210
+
211
+
212
+
213
+ def align(self, boids):
214
+
215
+ steering = Vector(*np.zeros(2))
216
+
217
+ total = 0
218
+
219
+ avg_vec = Vector(*np.zeros(2))
220
+
221
+ for boid in boids:
222
+
223
+ if np.linalg.norm(boid.position - self.position) < self.perception:
224
+
225
+ avg_vec += boid.velocity
226
+
227
+ total += 1
228
+
229
+ if total > 0:
230
+
231
+ avg_vec /= total
232
+
233
+ avg_vec = Vector(*avg_vec)
234
+
235
+ avg_vec = (avg_vec /np.linalg.norm(avg_vec)) * self.max_speed
236
+
237
+ steering = avg_vec - self.velocity
238
+
239
+
240
+
241
+ return steering
242
+
243
+
244
+
245
+
246
+
247
+ width = 1000
248
+
249
+ height = 1000
250
+
251
+
252
+
253
+ flock = [Boid(*np.random.rand(2)*1000, width, height) for _ in range(50)]
254
+
255
+ def run():
256
+
257
+ global flock
258
+
259
+
260
+
261
+ for boid in flock:
262
+
263
+ boid.apply_behaviour(flock)
264
+
265
+ run()
266
+
267
+ ```