Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
#
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
import numpy as np
|
|
@@ -10,7 +10,7 @@ class Boid:
|
|
| 10 |
def __init__(self, position, velocity):
|
| 11 |
self.position = np.array(position, dtype='float64')
|
| 12 |
self.velocity = np.array(velocity, dtype='float64')
|
| 13 |
-
self.
|
| 14 |
|
| 15 |
def update(self, boids, width, height, params):
|
| 16 |
self.flock(boids, params)
|
|
@@ -25,98 +25,97 @@ class Boid:
|
|
| 25 |
self.position[0] = self.position[0] % width
|
| 26 |
self.position[1] = self.position[1] % height
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def flock(self, boids, params):
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
def align(self, boids, params):
|
| 38 |
-
steering = np.zeros(2, dtype='float64')
|
| 39 |
-
total = 0
|
| 40 |
for other in boids:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
for other in boids:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
def separate(self, boids, params):
|
| 69 |
-
steering = np.zeros(2, dtype='float64')
|
| 70 |
-
total = 0
|
| 71 |
for other in boids:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
norm = np.linalg.norm(vector)
|
| 88 |
-
if norm == 0:
|
| 89 |
-
return vector
|
| 90 |
-
return (vector / norm) * magnitude
|
| 91 |
-
|
| 92 |
-
def limit(self, vector, max_value):
|
| 93 |
-
norm = np.linalg.norm(vector)
|
| 94 |
-
if norm > max_value:
|
| 95 |
-
return (vector / norm) * max_value
|
| 96 |
-
return vector
|
| 97 |
|
| 98 |
# シミュレーションパラメータ
|
| 99 |
params = {
|
| 100 |
-
'num_boids':
|
| 101 |
-
'
|
| 102 |
-
'
|
| 103 |
-
'
|
| 104 |
-
'
|
| 105 |
-
'
|
| 106 |
-
'
|
| 107 |
-
'
|
|
|
|
| 108 |
}
|
| 109 |
|
| 110 |
# Streamlitのサイドバーでパラメータを調整
|
| 111 |
st.sidebar.title("Boids Simulation Parameters")
|
| 112 |
-
params['num_boids'] = st.sidebar.slider("Number of Boids", 10,
|
| 113 |
-
params['
|
| 114 |
-
params['
|
| 115 |
-
params['
|
| 116 |
-
params['
|
| 117 |
-
params['
|
| 118 |
-
params['
|
| 119 |
-
params['
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# シミュレーション画面サイズ
|
| 122 |
width, height = 800, 600
|
|
@@ -134,28 +133,41 @@ fig = go.Figure(
|
|
| 134 |
layout=go.Layout(
|
| 135 |
xaxis=dict(range=[0, width], autorange=False, showgrid=False, zeroline=False),
|
| 136 |
yaxis=dict(range=[0, height], autorange=False, showgrid=False, zeroline=False),
|
| 137 |
-
width=
|
| 138 |
-
height=
|
| 139 |
margin=dict(l=0, r=0, t=0, b=0)
|
| 140 |
)
|
| 141 |
)
|
| 142 |
|
|
|
|
| 143 |
scatter = go.Scatter(
|
| 144 |
x=[boid.position[0] for boid in boids],
|
| 145 |
y=[boid.position[1] for boid in boids],
|
| 146 |
mode='markers',
|
| 147 |
-
marker=dict(size=
|
| 148 |
)
|
| 149 |
|
| 150 |
fig.add_trace(scatter)
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
|
|
|
| 155 |
animation_placeholder = st.empty()
|
| 156 |
|
| 157 |
# アニメーションの実行
|
| 158 |
-
frame_rate =
|
| 159 |
sleep_time = 1.0 / frame_rate
|
| 160 |
|
| 161 |
# リセットボタンの追加
|
|
@@ -167,40 +179,31 @@ if st.sidebar.button("Reset Simulation"):
|
|
| 167 |
velocity = [np.cos(angle), np.sin(angle)]
|
| 168 |
boids.append(Boid(position, velocity))
|
| 169 |
|
| 170 |
-
# アニメーションの実行
|
| 171 |
while True:
|
| 172 |
# Boidsの更新
|
| 173 |
for boid in boids:
|
| 174 |
boid.update(boids, width, height, params)
|
| 175 |
|
| 176 |
-
#
|
| 177 |
-
|
|
|
|
| 178 |
|
| 179 |
-
#
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
arrow_v.append(np.sin(angle) * 10)
|
| 189 |
|
| 190 |
# グラフの更新
|
| 191 |
-
fig.data[0].x =
|
| 192 |
-
fig.data[0].y =
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
fig.add_trace(go.Scatter(
|
| 197 |
-
x=[x for x, u in zip(arrow_x, arrow_u)],
|
| 198 |
-
y=[y for y, v in zip(arrow_y, arrow_v)],
|
| 199 |
-
mode='markers+lines',
|
| 200 |
-
marker=dict(size=5, color='red'),
|
| 201 |
-
line=dict(width=1, color='red'),
|
| 202 |
-
showlegend=False
|
| 203 |
-
))
|
| 204 |
|
| 205 |
# アニメーション表示
|
| 206 |
animation_placeholder.plotly_chart(fig, use_container_width=True)
|
|
|
|
| 1 |
+
# boids_streamlit.py
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
import numpy as np
|
|
|
|
| 10 |
def __init__(self, position, velocity):
|
| 11 |
self.position = np.array(position, dtype='float64')
|
| 12 |
self.velocity = np.array(velocity, dtype='float64')
|
| 13 |
+
self.history = []
|
| 14 |
|
| 15 |
def update(self, boids, width, height, params):
|
| 16 |
self.flock(boids, params)
|
|
|
|
| 25 |
self.position[0] = self.position[0] % width
|
| 26 |
self.position[1] = self.position[1] % height
|
| 27 |
|
| 28 |
+
# 履歴に現在の位置を追加
|
| 29 |
+
if params['draw_trail']:
|
| 30 |
+
self.history.append(self.position.copy())
|
| 31 |
+
if len(self.history) > params['max_history']:
|
| 32 |
+
self.history.pop(0)
|
| 33 |
+
|
| 34 |
def flock(self, boids, params):
|
| 35 |
+
self.acceleration = np.zeros(2, dtype='float64')
|
| 36 |
+
self.fly_towards_center(boids, params)
|
| 37 |
+
self.avoid_others(boids, params)
|
| 38 |
+
self.match_velocity(boids, params)
|
| 39 |
+
self.limit_speed(params)
|
| 40 |
|
| 41 |
+
def fly_towards_center(self, boids, params):
|
| 42 |
+
centering_factor = params['centering_factor']
|
| 43 |
+
center_x = 0.0
|
| 44 |
+
center_y = 0.0
|
| 45 |
+
num_neighbors = 0
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
for other in boids:
|
| 48 |
+
if other is not self and self.distance(other) < params['visual_range']:
|
| 49 |
+
center_x += other.position[0]
|
| 50 |
+
center_y += other.position[1]
|
| 51 |
+
num_neighbors += 1
|
| 52 |
+
|
| 53 |
+
if num_neighbors > 0:
|
| 54 |
+
center_x /= num_neighbors
|
| 55 |
+
center_y /= num_neighbors
|
| 56 |
+
direction = np.array([center_x, center_y]) - self.position
|
| 57 |
+
self.acceleration += centering_factor * direction
|
| 58 |
+
|
| 59 |
+
def avoid_others(self, boids, params):
|
| 60 |
+
min_distance = params['min_distance']
|
| 61 |
+
avoid_factor = params['avoid_factor']
|
| 62 |
+
move = np.zeros(2, dtype='float64')
|
| 63 |
+
|
| 64 |
for other in boids:
|
| 65 |
+
if other is not self and self.distance(other) < min_distance:
|
| 66 |
+
move += self.position - other.position
|
| 67 |
+
|
| 68 |
+
if np.linalg.norm(move) > 0:
|
| 69 |
+
move = move / np.linalg.norm(move) * avoid_factor
|
| 70 |
+
self.acceleration += move
|
| 71 |
+
|
| 72 |
+
def match_velocity(self, boids, params):
|
| 73 |
+
matching_factor = params['matching_factor']
|
| 74 |
+
avg_velocity = np.zeros(2, dtype='float64')
|
| 75 |
+
num_neighbors = 0
|
| 76 |
+
|
|
|
|
|
|
|
|
|
|
| 77 |
for other in boids:
|
| 78 |
+
if other is not self and self.distance(other) < params['visual_range']:
|
| 79 |
+
avg_velocity += other.velocity
|
| 80 |
+
num_neighbors += 1
|
| 81 |
+
|
| 82 |
+
if num_neighbors > 0:
|
| 83 |
+
avg_velocity /= num_neighbors
|
| 84 |
+
self.acceleration += matching_factor * (avg_velocity - self.velocity)
|
| 85 |
+
|
| 86 |
+
def limit_speed(self, params):
|
| 87 |
+
speed = np.linalg.norm(self.velocity)
|
| 88 |
+
if speed > params['max_speed']:
|
| 89 |
+
self.velocity = (self.velocity / speed) * params['max_speed']
|
| 90 |
+
|
| 91 |
+
def distance(self, other):
|
| 92 |
+
return np.linalg.norm(self.position - other.position)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# シミュレーションパラメータ
|
| 95 |
params = {
|
| 96 |
+
'num_boids': 100,
|
| 97 |
+
'visual_range': 75.0,
|
| 98 |
+
'min_distance': 20.0,
|
| 99 |
+
'centering_factor': 0.005,
|
| 100 |
+
'avoid_factor': 0.05,
|
| 101 |
+
'matching_factor': 0.05,
|
| 102 |
+
'max_speed': 15.0,
|
| 103 |
+
'draw_trail': False,
|
| 104 |
+
'max_history': 50
|
| 105 |
}
|
| 106 |
|
| 107 |
# Streamlitのサイドバーでパラメータを調整
|
| 108 |
st.sidebar.title("Boids Simulation Parameters")
|
| 109 |
+
params['num_boids'] = st.sidebar.slider("Number of Boids", 10, 300, 100)
|
| 110 |
+
params['visual_range'] = st.sidebar.slider("Visual Range", 10.0, 200.0, 75.0)
|
| 111 |
+
params['min_distance'] = st.sidebar.slider("Minimum Separation Distance", 5.0, 100.0, 20.0)
|
| 112 |
+
params['centering_factor'] = st.sidebar.slider("Centering Factor", 0.001, 0.02, 0.005)
|
| 113 |
+
params['avoid_factor'] = st.sidebar.slider("Avoidance Factor", 0.01, 0.1, 0.05)
|
| 114 |
+
params['matching_factor'] = st.sidebar.slider("Matching Factor", 0.01, 0.1, 0.05)
|
| 115 |
+
params['max_speed'] = st.sidebar.slider("Maximum Speed", 5.0, 30.0, 15.0)
|
| 116 |
+
params['draw_trail'] = st.sidebar.checkbox("Draw Trails")
|
| 117 |
+
if params['draw_trail']:
|
| 118 |
+
params['max_history'] = st.sidebar.slider("Trail Length", 10, 100, 50)
|
| 119 |
|
| 120 |
# シミュレーション画面サイズ
|
| 121 |
width, height = 800, 600
|
|
|
|
| 133 |
layout=go.Layout(
|
| 134 |
xaxis=dict(range=[0, width], autorange=False, showgrid=False, zeroline=False),
|
| 135 |
yaxis=dict(range=[0, height], autorange=False, showgrid=False, zeroline=False),
|
| 136 |
+
width=width,
|
| 137 |
+
height=height,
|
| 138 |
margin=dict(l=0, r=0, t=0, b=0)
|
| 139 |
)
|
| 140 |
)
|
| 141 |
|
| 142 |
+
# Boidsの初期位置をプロット
|
| 143 |
scatter = go.Scatter(
|
| 144 |
x=[boid.position[0] for boid in boids],
|
| 145 |
y=[boid.position[1] for boid in boids],
|
| 146 |
mode='markers',
|
| 147 |
+
marker=dict(size=8, color='blue')
|
| 148 |
)
|
| 149 |
|
| 150 |
fig.add_trace(scatter)
|
| 151 |
|
| 152 |
+
# トレイル用のトレース
|
| 153 |
+
if params['draw_trail']:
|
| 154 |
+
trail_scatter = go.Scatter(
|
| 155 |
+
x=[],
|
| 156 |
+
y=[],
|
| 157 |
+
mode='lines',
|
| 158 |
+
line=dict(color='rgba(0,0,255,0.2)', width=1),
|
| 159 |
+
showlegend=False
|
| 160 |
+
)
|
| 161 |
+
fig.add_trace(trail_scatter)
|
| 162 |
+
|
| 163 |
+
# Streamlitのタイトル
|
| 164 |
+
st.title("Boids Simulation")
|
| 165 |
|
| 166 |
+
# アニメーションの表示領域
|
| 167 |
animation_placeholder = st.empty()
|
| 168 |
|
| 169 |
# アニメーションの実行
|
| 170 |
+
frame_rate = 30 # フレームレート (FPS)
|
| 171 |
sleep_time = 1.0 / frame_rate
|
| 172 |
|
| 173 |
# リセットボタンの追加
|
|
|
|
| 179 |
velocity = [np.cos(angle), np.sin(angle)]
|
| 180 |
boids.append(Boid(position, velocity))
|
| 181 |
|
|
|
|
| 182 |
while True:
|
| 183 |
# Boidsの更新
|
| 184 |
for boid in boids:
|
| 185 |
boid.update(boids, width, height, params)
|
| 186 |
|
| 187 |
+
# Boidsの位置データを更新
|
| 188 |
+
scatter.x = [boid.position[0] for boid in boids]
|
| 189 |
+
scatter.y = [boid.position[1] for boid in boids]
|
| 190 |
|
| 191 |
+
# トレイルの更新
|
| 192 |
+
if params['draw_trail']:
|
| 193 |
+
trail_x = []
|
| 194 |
+
trail_y = []
|
| 195 |
+
for boid in boids:
|
| 196 |
+
trail_x.extend([pos[0] for pos in boid.history])
|
| 197 |
+
trail_y.extend([pos[1] for pos in boid.history])
|
| 198 |
+
trail_scatter.x = trail_x
|
| 199 |
+
trail_scatter.y = trail_y
|
|
|
|
| 200 |
|
| 201 |
# グラフの更新
|
| 202 |
+
fig.data[0].x = scatter.x
|
| 203 |
+
fig.data[0].y = scatter.y
|
| 204 |
+
if params['draw_trail']:
|
| 205 |
+
fig.data[1].x = trail_scatter.x
|
| 206 |
+
fig.data[1].y = trail_scatter.y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
# アニメーション表示
|
| 209 |
animation_placeholder.plotly_chart(fig, use_container_width=True)
|