Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,157 +1,209 @@
|
|
| 1 |
-
#
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
-
import plotly.
|
| 5 |
-
from dataclasses import dataclass
|
| 6 |
import time
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
from my_mouse_tracker import my_mouse_tracker
|
| 10 |
-
|
| 11 |
-
@dataclass
|
| 12 |
class Boid:
|
| 13 |
-
position:
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
total = 0
|
| 33 |
for other in boids:
|
| 34 |
-
distance = np.linalg.norm(other.position -
|
| 35 |
-
if other !=
|
| 36 |
-
|
| 37 |
-
cohesion += other.position
|
| 38 |
-
separation += (boid.position - other.position) / distance
|
| 39 |
total += 1
|
| 40 |
if total > 0:
|
| 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 |
height=600,
|
| 78 |
-
|
| 79 |
-
shapes=[],
|
| 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 |
-
fig = plot_boids(st.session_state.boids, width, height, mouse_pos)
|
| 151 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 152 |
-
|
| 153 |
-
# 自動リロードの代替
|
| 154 |
-
# st.experimental_rerun() を削除し、Streamlitの再実行機能に任せる
|
| 155 |
-
|
| 156 |
-
if __name__ == "__main__":
|
| 157 |
-
main()
|
|
|
|
| 1 |
+
# boids_streamlit_advanced.py
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
import numpy as np
|
| 5 |
+
import plotly.graph_objects as go
|
|
|
|
| 6 |
import time
|
| 7 |
|
| 8 |
+
# Boidクラスの定義
|
|
|
|
|
|
|
|
|
|
| 9 |
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.acceleration = np.zeros(2, dtype='float64')
|
| 14 |
|
| 15 |
+
def update(self, boids, width, height, params):
|
| 16 |
+
self.flock(boids, params)
|
| 17 |
+
self.velocity += self.acceleration
|
| 18 |
+
speed = np.linalg.norm(self.velocity)
|
| 19 |
+
if speed > params['max_speed']:
|
| 20 |
+
self.velocity = (self.velocity / speed) * params['max_speed']
|
| 21 |
+
self.position += self.velocity
|
| 22 |
+
self.acceleration = np.zeros(2, dtype='float64')
|
| 23 |
|
| 24 |
+
# 画面端でのラッピング
|
| 25 |
+
self.position[0] = self.position[0] % width
|
| 26 |
+
self.position[1] = self.position[1] % height
|
| 27 |
+
|
| 28 |
+
def flock(self, boids, params):
|
| 29 |
+
alignment = self.align(boids, params)
|
| 30 |
+
cohesion = self.cohere(boids, params)
|
| 31 |
+
separation = self.separate(boids, params)
|
| 32 |
+
|
| 33 |
+
self.acceleration += alignment * params['align_weight']
|
| 34 |
+
self.acceleration += cohesion * params['cohesion_weight']
|
| 35 |
+
self.acceleration += separation * params['separation_weight']
|
| 36 |
+
|
| 37 |
+
def align(self, boids, params):
|
| 38 |
+
steering = np.zeros(2, dtype='float64')
|
| 39 |
total = 0
|
| 40 |
for other in boids:
|
| 41 |
+
distance = np.linalg.norm(other.position - self.position)
|
| 42 |
+
if other != self and distance < params['neighbor_radius']:
|
| 43 |
+
steering += other.velocity
|
|
|
|
|
|
|
| 44 |
total += 1
|
| 45 |
if total > 0:
|
| 46 |
+
steering /= total
|
| 47 |
+
steering = self.set_magnitude(steering, params['max_speed'])
|
| 48 |
+
steering -= self.velocity
|
| 49 |
+
steering = self.limit(steering, params['max_force'])
|
| 50 |
+
return steering
|
| 51 |
+
|
| 52 |
+
def cohere(self, boids, params):
|
| 53 |
+
steering = np.zeros(2, dtype='float64')
|
| 54 |
+
total = 0
|
| 55 |
+
for other in boids:
|
| 56 |
+
distance = np.linalg.norm(other.position - self.position)
|
| 57 |
+
if other != self and distance < params['neighbor_radius']:
|
| 58 |
+
steering += other.position
|
| 59 |
+
total += 1
|
| 60 |
+
if total > 0:
|
| 61 |
+
steering /= total
|
| 62 |
+
steering -= self.position
|
| 63 |
+
steering = self.set_magnitude(steering, params['max_speed'])
|
| 64 |
+
steering -= self.velocity
|
| 65 |
+
steering = self.limit(steering, params['max_force'])
|
| 66 |
+
return steering
|
| 67 |
+
|
| 68 |
+
def separate(self, boids, params):
|
| 69 |
+
steering = np.zeros(2, dtype='float64')
|
| 70 |
+
total = 0
|
| 71 |
+
for other in boids:
|
| 72 |
+
distance = np.linalg.norm(other.position - self.position)
|
| 73 |
+
if other != self and distance < params['desired_separation']:
|
| 74 |
+
diff = self.position - other.position
|
| 75 |
+
if distance > 0:
|
| 76 |
+
diff /= distance
|
| 77 |
+
steering += diff
|
| 78 |
+
total += 1
|
| 79 |
+
if total > 0:
|
| 80 |
+
steering /= total
|
| 81 |
+
steering = self.set_magnitude(steering, params['max_speed'])
|
| 82 |
+
steering -= self.velocity
|
| 83 |
+
steering = self.limit(steering, params['max_force'])
|
| 84 |
+
return steering
|
| 85 |
+
|
| 86 |
+
def set_magnitude(self, vector, magnitude):
|
| 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': 50,
|
| 101 |
+
'max_speed': 4.0,
|
| 102 |
+
'max_force': 0.1,
|
| 103 |
+
'neighbor_radius': 50.0,
|
| 104 |
+
'desired_separation': 25.0,
|
| 105 |
+
'align_weight': 1.0,
|
| 106 |
+
'cohesion_weight': 1.0,
|
| 107 |
+
'separation_weight': 1.5
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
# Streamlitのサイドバーでパラメータを調整
|
| 111 |
+
st.sidebar.title("Boids Simulation Parameters")
|
| 112 |
+
params['num_boids'] = st.sidebar.slider("Number of Boids", 10, 200, 50)
|
| 113 |
+
params['max_speed'] = st.sidebar.slider("Max Speed", 1.0, 10.0, 4.0)
|
| 114 |
+
params['max_force'] = st.sidebar.slider("Max Force", 0.01, 1.0, 0.1)
|
| 115 |
+
params['neighbor_radius'] = st.sidebar.slider("Neighbor Radius", 10.0, 100.0, 50.0)
|
| 116 |
+
params['desired_separation'] = st.sidebar.slider("Desired Separation", 5.0, 50.0, 25.0)
|
| 117 |
+
params['align_weight'] = st.sidebar.slider("Alignment Weight", 0.0, 5.0, 1.0)
|
| 118 |
+
params['cohesion_weight'] = st.sidebar.slider("Cohesion Weight", 0.0, 5.0, 1.0)
|
| 119 |
+
params['separation_weight'] = st.sidebar.slider("Separation Weight", 0.0, 5.0, 1.5)
|
| 120 |
+
|
| 121 |
+
# シミュレーション画面サイズ
|
| 122 |
+
width, height = 800, 600
|
| 123 |
+
|
| 124 |
+
# Boidsの初期化
|
| 125 |
+
boids = []
|
| 126 |
+
for _ in range(params['num_boids']):
|
| 127 |
+
position = [np.random.uniform(0, width), np.random.uniform(0, height)]
|
| 128 |
+
angle = np.random.uniform(0, 2 * np.pi)
|
| 129 |
+
velocity = [np.cos(angle), np.sin(angle)]
|
| 130 |
+
boids.append(Boid(position, velocity))
|
| 131 |
+
|
| 132 |
+
# Plotlyのグラフ設定
|
| 133 |
+
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=800,
|
| 138 |
height=600,
|
| 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=10, color='blue')
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
fig.add_trace(scatter)
|
| 151 |
+
|
| 152 |
+
# Streamlitのメインループ
|
| 153 |
+
st.title("Boids Simulation with Streamlit and Plotly")
|
| 154 |
+
|
| 155 |
+
animation_placeholder = st.empty()
|
| 156 |
+
|
| 157 |
+
# アニメーションの実行
|
| 158 |
+
frame_rate = st.sidebar.slider("Frame Rate (FPS)", 1, 60, 20)
|
| 159 |
+
sleep_time = 1.0 / frame_rate
|
| 160 |
+
|
| 161 |
+
# リセットボタンの追加
|
| 162 |
+
if st.sidebar.button("Reset Simulation"):
|
| 163 |
+
boids = []
|
| 164 |
+
for _ in range(params['num_boids']):
|
| 165 |
+
position = [np.random.uniform(0, width), np.random.uniform(0, height)]
|
| 166 |
+
angle = np.random.uniform(0, 2 * np.pi)
|
| 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 |
+
# 各Boidの向きを計算
|
| 177 |
+
angles = [np.arctan2(boid.velocity[1], boid.velocity[0]) for boid in boids]
|
| 178 |
+
|
| 179 |
+
# 矢印用の線データを作成
|
| 180 |
+
arrow_x = []
|
| 181 |
+
arrow_y = []
|
| 182 |
+
arrow_u = []
|
| 183 |
+
arrow_v = []
|
| 184 |
+
for boid, angle in zip(boids, angles):
|
| 185 |
+
arrow_x.append(boid.position[0])
|
| 186 |
+
arrow_y.append(boid.position[1])
|
| 187 |
+
arrow_u.append(np.cos(angle) * 10) # 矢印の長さ
|
| 188 |
+
arrow_v.append(np.sin(angle) * 10)
|
| 189 |
+
|
| 190 |
+
# グラフの更新
|
| 191 |
+
fig.data[0].x = [boid.position[0] for boid in boids]
|
| 192 |
+
fig.data[0].y = [boid.position[1] for boid in boids]
|
| 193 |
+
|
| 194 |
+
# 矢印の追加
|
| 195 |
+
fig.data = fig.data[:1] # 既存のデータをクリア
|
| 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)
|
| 207 |
+
|
| 208 |
+
# フレームレート調整
|
| 209 |
+
time.sleep(sleep_time)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|