teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

追記2を追加

2019/12/10 12:53

投稿

shmpwk
shmpwk

スコア13

title CHANGED
File without changes
body CHANGED
@@ -46,7 +46,7 @@
46
46
 
47
47
  どこを直せば良いか検討がつきません。ご教授お願いします。
48
48
 
49
- [追記]
49
+ [追記1]
50
50
  コードを一部抜粋したものを載せました。汚いコードですみません。
51
51
  実行すると、上記と同じエラーが出ます。
52
52
 
@@ -131,4 +131,7 @@
131
131
  for boid in flock:
132
132
  boid.apply_behaviour(flock)
133
133
  run()
134
- ```
134
+ ```
135
+
136
+ [追記2]
137
+ そもそもVectorというオブジェクトを作らずにnumpy?など既存のものを使えば避けられる問題なのでしょうか。

1

全体のコードの追加

2019/12/10 12:53

投稿

shmpwk
shmpwk

スコア13

title CHANGED
File without changes
body CHANGED
@@ -44,4 +44,91 @@
44
44
  すると、kernel startingのまま止まってしまいました。(Jupyter labでコードを書いています)
45
45
  jupyterではなく、.pyファイルに書いてterminalで実行すると、セグメンテーションフォルトになります。
46
46
 
47
- どこを直せば良いか検討がつきません。ご教授お願いします。
47
+ どこを直せば良いか検討がつきません。ご教授お願いします。
48
+
49
+ [追記]
50
+ コードを一部抜粋したものを載せました。汚いコードですみません。
51
+ 実行すると、上記と同じエラーが出ます。
52
+
53
+ ```python
54
+ import sys
55
+ sys.setrecursionlimit(10000)
56
+
57
+
58
+ class Vector:
59
+ def __init__(self, x, y):
60
+ self.x = x
61
+ self.y = y
62
+ def __add__(self, v):
63
+ self.x = self.x + v.x
64
+ self.y = self.y + v.y
65
+ return self
66
+ def __sub__(self, v):
67
+ self.x = self.x - v.x
68
+ self.y = self.y - v.y
69
+ return self
70
+ def __mul__(self, v):
71
+ self.x = self.x * v.x
72
+ self.y = self.y * v.y
73
+ return self
74
+ def sqrt(self):
75
+ self.x = math.sqrt(self.x)
76
+ self.y = math.sqrt(self.y)
77
+ return self
78
+ def __lt__(self, other):
79
+ return any((
80
+ self == other,
81
+ self < other,
82
+ ))
83
+ class Boid():
84
+
85
+ def __init__(self, x, y, width, height):
86
+ self.position = Vector(x, y)
87
+ vec = (np.random.rand(2) - 0.5)*10
88
+ self.velocity = Vector(*vec)
89
+
90
+ vec = (np.random.rand(2) - 0.5)/2
91
+ self.acceleration = Vector(*vec)
92
+ self.max_force = 0.3
93
+ self.max_speed = 5
94
+ self.perception = 100
95
+
96
+ def apply_behaviour(self, boids):
97
+ alignment = self.align(boids)
98
+ cohesion = self.cohesion(boids)
99
+ separation = self.separation(boids)
100
+
101
+ self.acceleration += alignment
102
+ self.acceleration += cohesion
103
+ self.acceleration += separation
104
+ self.width = width
105
+ self.height = height
106
+
107
+ def align(self, boids):
108
+ steering = Vector(*np.zeros(2))
109
+ total = 0
110
+ avg_vec = Vector(*np.zeros(2))
111
+ for boid in boids:
112
+ if np.linalg.norm(boid.position - self.position) < self.perception:
113
+ avg_vec += boid.velocity
114
+ total += 1
115
+ if total > 0:
116
+ avg_vec /= total
117
+ avg_vec = Vector(*avg_vec)
118
+ avg_vec = (avg_vec /np.linalg.norm(avg_vec)) * self.max_speed
119
+ steering = avg_vec - self.velocity
120
+
121
+ return steering
122
+
123
+
124
+ width = 1000
125
+ height = 1000
126
+
127
+ flock = [Boid(*np.random.rand(2)*1000, width, height) for _ in range(50)]
128
+ def run():
129
+ global flock
130
+
131
+ for boid in flock:
132
+ boid.apply_behaviour(flock)
133
+ run()
134
+ ```