-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsimple_crowd_sim.py
More file actions
151 lines (124 loc) · 4.78 KB
/
Copy pathsimple_crowd_sim.py
File metadata and controls
151 lines (124 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
78
79
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
151
import mesa
import numpy as np
import random
import math
import matplotlib.pyplot as plt
class SimpleAgent(mesa.Agent):
"""A simple agent in the crowd simulation."""
def __init__(self, unique_id, model, pos, panicked=False):
# In Mesa 3.0, model is the first parameter
super().__init__(model)
# Manually assign the unique_id since we're not using Mesa's auto-generation
self.unique_id = unique_id
self.pos = pos
self.panicked = panicked
self.velocity = np.array([random.uniform(-1,1), random.uniform(-1,1)])
self.speed = 1.0
def step(self):
# Simple movement
if self.panicked:
self.speed = 2.0
else:
self.speed = 1.0
# Add some randomness to direction
angle = random.uniform(-0.5, 0.5)
rotation = np.array([
[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]
])
self.velocity = rotation @ self.velocity
# Normalize and apply speed
velocity_norm = np.linalg.norm(self.velocity)
if velocity_norm > 0:
self.velocity = self.velocity / velocity_norm * self.speed
# Update position
new_x = self.pos[0] + self.velocity[0]
new_y = self.pos[1] + self.velocity[1]
# Bounce off walls
if new_x < 0 or new_x > self.model.width:
self.velocity[0] *= -1
new_x = np.clip(new_x, 0, self.model.width)
if new_y < 0 or new_y > self.model.height:
self.velocity[1] *= -1
new_y = np.clip(new_y, 0, self.model.height)
self.pos = (new_x, new_y)
# Panic spreads to nearby agents
if self.panicked:
for agent in self.model.schedule.agents:
if agent != self and not agent.panicked:
distance = np.sqrt((self.pos[0] - agent.pos[0])**2 +
(self.pos[1] - agent.pos[1])**2)
if distance < 5.0: # Panic radius
if random.random() < 0.2: # 20% chance to spread
agent.panicked = True
class SimpleCrowdModel(mesa.Model):
"""A simple model for crowd simulation."""
def __init__(self, width=100, height=100, num_agents=50):
super().__init__()
self.width = width
self.height = height
self.num_agents = num_agents
# Create scheduler
self.schedule = mesa.time.RandomActivation(self)
# Create space
self.space = mesa.space.ContinuousSpace(width, height, True)
# Create agents
for i in range(self.num_agents):
x = random.random() * self.width
y = random.random() * self.height
agent = SimpleAgent(i, self, (x, y))
self.schedule.add(agent)
# Setup data collector
self.datacollector = mesa.datacollection.DataCollector(
model_reporters={"Panicked": lambda m: sum(1 for a in m.schedule.agents if a.panicked)}
)
def step(self):
self.datacollector.collect(self)
self.schedule.step()
def inject_panic(self, x=None, y=None, radius=10):
"""Inject panic at a specific location"""
if x is None:
x = self.width / 2
if y is None:
y = self.height / 2
# Panic agents near the location
for agent in self.schedule.agents:
distance = np.sqrt((agent.pos[0] - x)**2 + (agent.pos[1] - y)**2)
if distance < radius:
agent.panicked = True
# Run the model
def run_simple_model():
print("Creating model...")
model = SimpleCrowdModel(width=100, height=100, num_agents=50)
print("Running steps...")
# Run for 50 steps without panic
for i in range(50):
if i % 10 == 0:
print(f"Step {i}")
model.step()
# Inject panic
print("Injecting panic...")
model.inject_panic()
# Run for 50 more steps with panic
for i in range(50, 100):
if i % 10 == 0:
print(f"Step {i}")
model.step()
# Plot results
print("Generating plot...")
results = model.datacollector.get_model_vars_dataframe()
plt.figure(figsize=(10, 6))
plt.plot(results["Panicked"])
plt.title("Number of Panicked Agents")
plt.xlabel("Step")
plt.ylabel("Panicked Agents")
plt.savefig("simple_panic.png")
plt.close()
print("Done! Results saved to simple_panic.png")
if __name__ == "__main__":
try:
run_simple_model()
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()