-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
439 lines (347 loc) · 13.7 KB
/
analysis.py
File metadata and controls
439 lines (347 loc) · 13.7 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
"""
Parameter sweep and analysis for predator-prey dynamics.
Runs experiments varying predator speed and avoidance strength,
collects metrics, and generates heatmap visualizations (Tier 3).
Usage (from backend directory):
python analysis.py
"""
import numpy as np
import matplotlib.pyplot as plt
from dataclasses import dataclass
from typing import List
import time
from boids.flock import SimulationParams
from boids.flock_optimized import FlockOptimized
from boids.metrics import run_simulation_with_metrics, RunMetrics
@dataclass
class ExperimentConfig:
"""Configuration for a parameter sweep experiment."""
# Parameter 1: Predator speed
predator_speeds: List[float]
# Parameter 2: Avoidance strength
avoidance_strengths: List[float]
# Experiment settings
num_boids: int = 50
num_frames: int = 500
num_repetitions: int = 5
# Base simulation parameters (others kept at defaults)
base_params: SimulationParams = None
def __post_init__(self):
if self.base_params is None:
self.base_params = SimulationParams()
@dataclass
class ExperimentResults:
"""Results from a parameter sweep experiment."""
# Parameter values (axes)
predator_speeds: np.ndarray
avoidance_strengths: np.ndarray
# Metric grids (shape: len(speeds) x len(strengths))
mean_avg_distance: np.ndarray # Mean of avg distance across repetitions
std_avg_distance: np.ndarray # Std of avg distance across repetitions
mean_min_distance: np.ndarray # Mean of min distance across repetitions
std_min_distance: np.ndarray
mean_cohesion: np.ndarray # Mean cohesion across repetitions
std_cohesion: np.ndarray
# Metadata
num_repetitions: int = 0
num_frames: int = 0
total_runs: int = 0
elapsed_time: float = 0.0
def run_single_experiment(
predator_speed: float,
avoidance_strength: float,
config: ExperimentConfig,
seed: int
) -> RunMetrics:
"""
Run a single simulation with given parameters.
Args:
predator_speed: Predator speed parameter
avoidance_strength: Predator avoidance strength parameter
config: Experiment configuration
seed: Random seed for reproducibility
Returns:
RunMetrics from the simulation
"""
np.random.seed(seed)
# Create params with modified predator settings
params = SimulationParams(
width=config.base_params.width,
height=config.base_params.height,
visual_range=config.base_params.visual_range,
protected_range=config.base_params.protected_range,
max_speed=config.base_params.max_speed,
min_speed=config.base_params.min_speed,
cohesion_factor=config.base_params.cohesion_factor,
alignment_factor=config.base_params.alignment_factor,
separation_strength=config.base_params.separation_strength,
margin=config.base_params.margin,
turn_factor=config.base_params.turn_factor,
# Varied parameters:
predator_speed=predator_speed,
predator_avoidance_strength=avoidance_strength,
predator_detection_range=config.base_params.predator_detection_range,
predator_hunting_strength=config.base_params.predator_hunting_strength
)
# Create flock with predator
flock = FlockOptimized(
num_boids=config.num_boids,
params=params,
enable_predator=True
)
# Run simulation and collect metrics
return run_simulation_with_metrics(flock, num_frames=config.num_frames)
def run_parameter_sweep(config: ExperimentConfig, verbose: bool = True) -> ExperimentResults:
"""
Run full parameter sweep experiment.
Args:
config: Experiment configuration
verbose: If True, print progress
Returns:
ExperimentResults with all metrics
"""
start_time = time.time()
n_speeds = len(config.predator_speeds)
n_strengths = len(config.avoidance_strengths)
n_reps = config.num_repetitions
total_runs = n_speeds * n_strengths * n_reps
if verbose:
print(f"Running parameter sweep: {n_speeds} speeds × {n_strengths} strengths × {n_reps} reps = {total_runs} runs")
# Storage for all repetitions
all_avg_dist = np.zeros((n_speeds, n_strengths, n_reps))
all_min_dist = np.zeros((n_speeds, n_strengths, n_reps))
all_cohesion = np.zeros((n_speeds, n_strengths, n_reps))
run_count = 0
for i, speed in enumerate(config.predator_speeds):
for j, strength in enumerate(config.avoidance_strengths):
for rep in range(n_reps):
seed = 1000 * i + 100 * j + rep # Unique seed per run
metrics = run_single_experiment(speed, strength, config, seed)
all_avg_dist[i, j, rep] = metrics.mean_avg_distance
all_min_dist[i, j, rep] = metrics.mean_min_distance
all_cohesion[i, j, rep] = metrics.mean_cohesion
run_count += 1
if verbose and run_count % 10 == 0:
elapsed = time.time() - start_time
remaining = elapsed / run_count * (total_runs - run_count)
print(f" Progress: {run_count}/{total_runs} ({100*run_count/total_runs:.0f}%) "
f"- ETA: {remaining:.0f}s")
elapsed_time = time.time() - start_time
if verbose:
print(f"Completed {total_runs} runs in {elapsed_time:.1f}s")
# Compute mean and std across repetitions
results = ExperimentResults(
predator_speeds=np.array(config.predator_speeds),
avoidance_strengths=np.array(config.avoidance_strengths),
mean_avg_distance=np.mean(all_avg_dist, axis=2),
std_avg_distance=np.std(all_avg_dist, axis=2),
mean_min_distance=np.mean(all_min_dist, axis=2),
std_min_distance=np.std(all_min_dist, axis=2),
mean_cohesion=np.mean(all_cohesion, axis=2),
std_cohesion=np.std(all_cohesion, axis=2),
num_repetitions=n_reps,
num_frames=config.num_frames,
total_runs=total_runs,
elapsed_time=elapsed_time
)
return results
def create_heatmap(
results: ExperimentResults,
metric: str = 'avg_distance',
title: str = None,
filename: str = None,
show: bool = True
) -> plt.Figure:
"""
Create heatmap visualization of experiment results.
Args:
results: ExperimentResults from parameter sweep
metric: Which metric to plot ('avg_distance', 'min_distance', 'cohesion')
title: Plot title (auto-generated if None)
filename: If provided, save figure to this path
show: If True, display the figure
Returns:
matplotlib Figure object
"""
# Select metric data
if metric == 'avg_distance':
data = results.mean_avg_distance
std_data = results.std_avg_distance
default_title = 'Average Distance to Predator'
cmap = 'viridis' # Higher is better (escape)
label = 'Avg Distance (px)'
elif metric == 'min_distance':
data = results.mean_min_distance
std_data = results.std_min_distance
default_title = 'Minimum Distance to Predator'
cmap = 'viridis' # Higher is better (survival)
label = 'Min Distance (px)'
elif metric == 'cohesion':
data = results.mean_cohesion
std_data = results.std_cohesion
default_title = 'Flock Cohesion (Dispersion)'
cmap = 'viridis_r' # Lower is better (tighter flock)
label = 'Position Std Dev (px)'
else:
raise ValueError(f"Unknown metric: {metric}")
title = title or default_title
# Create figure
fig, ax = plt.subplots(figsize=(10, 8))
# Create heatmap
im = ax.imshow(
data.T, # Transpose so x=speed, y=strength
aspect='auto',
origin='lower',
cmap=cmap,
extent=[
results.predator_speeds[0],
results.predator_speeds[-1],
results.avoidance_strengths[0],
results.avoidance_strengths[-1]
]
)
# Add colorbar
cbar = plt.colorbar(im, ax=ax)
cbar.set_label(label, fontsize=12)
# Add contour lines
X, Y = np.meshgrid(results.predator_speeds, results.avoidance_strengths)
contours = ax.contour(X, Y, data.T, colors='white', alpha=0.5, linewidths=0.5)
ax.clabel(contours, inline=True, fontsize=8, fmt='%.0f')
# Labels and title
ax.set_xlabel('Predator Speed', fontsize=12)
ax.set_ylabel('Avoidance Strength', fontsize=12)
ax.set_title(f'{title}\n(mean over {results.num_repetitions} runs, {results.num_frames} frames each)',
fontsize=14)
# Add grid
ax.set_xticks(results.predator_speeds)
ax.set_yticks(results.avoidance_strengths)
plt.tight_layout()
if filename:
plt.savefig(filename, dpi=150, bbox_inches='tight')
print(f"Saved: {filename}")
if show:
plt.show()
return fig
def create_combined_figure(
results: ExperimentResults,
filename: str = None,
show: bool = True
) -> plt.Figure:
"""
Create combined figure with all three metrics.
Args:
results: ExperimentResults from parameter sweep
filename: If provided, save figure to this path
show: If True, display the figure
Returns:
matplotlib Figure object
"""
fig, axes = plt.subplots(1, 3, figsize=(16, 5))
metrics = [
('avg_distance', 'Mean Avg Distance to Predator', 'viridis', 'Avg Distance (px)'),
('min_distance', 'Mean Min Distance to Predator', 'viridis', 'Min Distance (px)'),
('cohesion', 'Flock Dispersion', 'viridis_r', 'Position Std Dev (px)')
]
data_sources = [
results.mean_avg_distance,
results.mean_min_distance,
results.mean_cohesion
]
for ax, (metric, title, cmap, label), data in zip(axes, metrics, data_sources):
im = ax.imshow(
data.T,
aspect='auto',
origin='lower',
cmap=cmap,
extent=[
results.predator_speeds[0],
results.predator_speeds[-1],
results.avoidance_strengths[0],
results.avoidance_strengths[-1]
]
)
cbar = plt.colorbar(im, ax=ax)
cbar.set_label(label, fontsize=10)
# Contour lines
X, Y = np.meshgrid(results.predator_speeds, results.avoidance_strengths)
contours = ax.contour(X, Y, data.T, colors='white', alpha=0.5, linewidths=0.5)
ax.clabel(contours, inline=True, fontsize=7, fmt='%.0f')
ax.set_xlabel('Predator Speed', fontsize=10)
ax.set_ylabel('Avoidance Strength', fontsize=10)
ax.set_title(title, fontsize=11)
ax.set_xticks(results.predator_speeds)
ax.set_yticks(results.avoidance_strengths)
fig.suptitle(
f'Predator-Prey Dynamics: Parameter Sweep Results\n'
f'({results.num_repetitions} repetitions × {results.num_frames} frames, {results.total_runs} total runs)',
fontsize=13
)
plt.tight_layout()
if filename:
plt.savefig(filename, dpi=150, bbox_inches='tight')
print(f"Saved: {filename}")
if show:
plt.show()
return fig
def print_results_table(results: ExperimentResults) -> None:
"""Print results as formatted table."""
print("\n" + "="*70)
print("PARAMETER SWEEP RESULTS")
print("="*70)
print(f"\nExperiment: {results.total_runs} runs in {results.elapsed_time:.1f}s")
print(f"Settings: {results.num_repetitions} reps × {results.num_frames} frames")
print("\n--- Mean Average Distance to Predator ---")
print(" ", end="")
for s in results.avoidance_strengths:
print(f" str={s:.1f}", end="")
print()
for i, speed in enumerate(results.predator_speeds):
print(f"speed={speed:.1f} ", end="")
for j in range(len(results.avoidance_strengths)):
val = results.mean_avg_distance[i, j]
print(f" {val:6.1f}", end="")
print()
print("\n--- Mean Minimum Distance to Predator ---")
print(" ", end="")
for s in results.avoidance_strengths:
print(f" str={s:.1f}", end="")
print()
for i, speed in enumerate(results.predator_speeds):
print(f"speed={speed:.1f} ", end="")
for j in range(len(results.avoidance_strengths)):
val = results.mean_min_distance[i, j]
print(f" {val:6.1f}", end="")
print()
print("\n" + "="*70)
def run_default_experiment(
num_reps: int = 5,
num_frames: int = 500,
verbose: bool = True
) -> ExperimentResults:
"""
Run the default parameter sweep experiment.
Args:
num_reps: Number of repetitions per parameter combination
num_frames: Frames per simulation run
verbose: Print progress
Returns:
ExperimentResults
"""
config = ExperimentConfig(
predator_speeds=[1.5, 2.0, 2.5, 3.0, 3.5],
avoidance_strengths=[0.1, 0.3, 0.5, 0.7, 0.9],
num_boids=50,
num_frames=num_frames,
num_repetitions=num_reps
)
return run_parameter_sweep(config, verbose=verbose)
if __name__ == "__main__":
print("Running Tier 3 parameter sweep experiment...")
print("This may take a few minutes.\n")
# Run experiment
results = run_default_experiment(num_reps=5, num_frames=500)
# Print table
print_results_table(results)
# Generate figures
create_combined_figure(results, filename='parameter_sweep_results.png', show=False)
print("\nExperiment complete!")