Skip to content

Commit 1648fe0

Browse files
committed
examples/boid: Update viz
Update the visualisation with sliders sliders for coloured boids.
1 parent 6a60350 commit 1648fe0

File tree

1 file changed

+39
-8
lines changed
  • examples/basic/boid_flockers

1 file changed

+39
-8
lines changed
Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
11
from boid_flockers.model import BoidFlockers
22
from mesa.visualization import SolaraViz, make_space_matplotlib
3-
3+
from mesa.visualization import Slider
44

55
def boid_draw(agent):
6-
return {"color": "tab:red"}
6+
if not agent.neighbors: # Only for the first Frame
7+
neighbors = len(agent.model.space.get_neighbors(agent.pos, agent.vision, False))
8+
else:
9+
neighbors = len(agent.neighbors)
710

11+
if neighbors <= 1:
12+
return {"color": "red", "size": 20}
13+
elif neighbors >= 2:
14+
return {"color": "green", "size": 20}
815

916
model_params = {
10-
"population": 100,
17+
"population": Slider(
18+
label="Number of boids",
19+
value=100,
20+
min=10,
21+
max=200,
22+
step=10,
23+
),
1124
"width": 100,
1225
"height": 100,
13-
"speed": 5,
14-
"vision": 10,
15-
"separation": 2,
26+
"speed": Slider(
27+
label="Speed of Boids",
28+
value=5,
29+
min=1,
30+
max=20,
31+
step=1,
32+
),
33+
"vision": Slider(
34+
label="Vision of Bird (radius)",
35+
value=10,
36+
min=1,
37+
max=50,
38+
step=1,
39+
),
40+
"separation": Slider(
41+
label="Minimum Separation",
42+
value=2,
43+
min=1,
44+
max=20,
45+
step=1,
46+
),
1647
}
1748

18-
model = BoidFlockers(100, 100, 100, 5, 10, 2)
49+
model = BoidFlockers()
1950

2051
page = SolaraViz(
2152
model,
2253
[make_space_matplotlib(agent_portrayal=boid_draw)],
2354
model_params=model_params,
24-
name="BoidFlockers",
55+
name="Boid Flocking Model",
2556
)
2657
page # noqa

0 commit comments

Comments
 (0)