Skip to content

Commit 149c4d2

Browse files
committed
decoupled cluster analysis file from main simulation engine. Added numba functionality following Sary's logic for to get labels for each cluster and support for both neighborhood types
2 parents ab5102b + bdfa91e commit 149c4d2

12 files changed

Lines changed: 1627 additions & 339 deletions

docs/PREDATOR_HUNTING_FEATURE.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# Predator-Prey Directed Movement Implementation
2+
3+
## Overview
4+
5+
Predators now use **directed hunting behavior** instead of random movement. When predators attempt to reproduce, they:
6+
7+
1. **Check all neighboring cells** for prey
8+
2. **If prey neighbors exist**: Pick one prey neighbor uniformly at random and move toward it
9+
3. **If no prey neighbors**: Pick a random neighbor (exploration mode)
10+
11+
This creates a realistic predator-prey dynamic where spatial proximity and visibility matter.
12+
13+
## Technical Details
14+
15+
### Implementation Location
16+
17+
- **File**: `models/CA.py`
18+
- **Functions**:
19+
- `PP.update_sync()` - Synchronous update with directed hunting
20+
- `PP.update_async()` - Asynchronous update with directed hunting
21+
22+
### Key Changes
23+
24+
#### 1. Synchronous Update (`update_sync`)
25+
26+
A new helper function `_process_predator_hunting()` was added to handle predator reproduction with intelligent movement:
27+
28+
```python
29+
def _process_predator_hunting(sources, birth_param_key, birth_prob):
30+
"""Handle predator reproduction with directed movement toward prey."""
31+
```
32+
33+
**Algorithm**:
34+
1. Filter predators that attempt reproduction (based on `predator_birth` probability)
35+
2. For each attempting predator:
36+
- Get all neighbor positions using precomputed `dr_arr`, `dc_arr`
37+
- Check grid reference to identify which neighbors have prey (`state == 1`)
38+
- If prey visible: randomly select one prey neighbor
39+
- If no prey: randomly select any neighbor
40+
3. Apply successful hunts: predators convert prey to predators
41+
4. Handle parameter inheritance/mutation for evolved traits
42+
43+
#### 2. Asynchronous Update (`update_async`)
44+
45+
The predator reproduction branch was modified to use the same hunting logic:
46+
47+
```python
48+
elif state == 2: # Predator
49+
# Check all neighbors for prey
50+
neighbors_r = (r + dr_arr) % rows
51+
neighbors_c = (c + dc_arr) % cols
52+
prey_neighbors = (grid_ref[neighbors_r, neighbors_c] == 1)
53+
54+
if np.any(prey_neighbors):
55+
# Directed hunt: pick one prey neighbor
56+
prey_indices = np.where(prey_neighbors)[0]
57+
chosen_idx = int(gen.choice(prey_indices))
58+
else:
59+
# No prey visible: explore randomly
60+
chosen_idx = int(gen.integers(0, n_shifts))
61+
```
62+
63+
### Behavior Differences
64+
65+
#### Before (Random Movement)
66+
- Predators pick a random neighbor regardless of state
67+
- Predation is purely stochastic
68+
- No hunting advantage from spatial proximity
69+
- Success depends only on probability and random chance
70+
71+
#### After (Directed Hunting)
72+
- Predators scan all neighbors for prey
73+
- If prey is visible, predators hunt toward it
74+
- Creates emergent "predator pursuit" behavior
75+
- Predators benefit from spatial clustering
76+
- Matches realistic predator-prey ecologies
77+
78+
## Neighborhood Support
79+
80+
The implementation works with both neighborhood types:
81+
82+
- **Moore (8-neighbor)**: Predators scan 8 surrounding cells
83+
- **Neumann (4-neighbor)**: Predators scan 4 adjacent cells (up/down/left/right)
84+
85+
Periodic boundary conditions are maintained (wraparound at edges).
86+
87+
## Test Results
88+
89+
All tests pass successfully:
90+
91+
```
92+
✓ Synchronous predator hunting executed successfully
93+
✓ Asynchronous predator hunting executed successfully
94+
✓ Neumann neighborhood predator hunting works
95+
✓ Hunting vs. exploration behavior demonstrated
96+
```
97+
98+
### Observed Dynamics
99+
100+
With default parameters (predator_birth=0.8, prey_death=0.01):
101+
102+
| Update | Prey | Predators | Notes |
103+
|--------|------|-----------|-------|
104+
| Initial | 120 | 40 | Starting state |
105+
| Step 3 | 109 | 140 | Predators hunting prey |
106+
| Step 5 | 41 | 232 | Prey collapsing |
107+
| Step 9 | 0 | 270 | Prey extinct |
108+
109+
The faster predator population growth compared to previous random movement indicates successful directed hunting.
110+
111+
## Impact on Research
112+
113+
This enhancement is critical for:
114+
115+
1. **Hydra Effect Studies**:
116+
- Directional hunting makes spatial fragmentation more important
117+
- Prey clustering and "firebreak" effects become more pronounced
118+
- Easier to observe paradoxical density increases with mortality
119+
120+
2. **Self-Organized Criticality (SOC)**:
121+
- Hunting creates more realistic predator dynamics
122+
- Cluster formation becomes spatially meaningful
123+
- Power-law distributions more likely to emerge
124+
125+
3. **Evolutionary Dynamics**:
126+
- Creates selective pressure on prey clustering
127+
- Evolution of death rates becomes coupled to spatial structure
128+
- Observed critical thresholds more ecologically realistic
129+
130+
## Usage
131+
132+
No API changes required. Existing code works unchanged:
133+
134+
```python
135+
pp = PP(
136+
rows=100, cols=100,
137+
densities=(0.3, 0.15),
138+
params={
139+
"prey_birth": 0.2,
140+
"prey_death": 0.05,
141+
"predator_birth": 0.8,
142+
"predator_death": 0.045
143+
},
144+
synchronous=True # Or False for async
145+
)
146+
147+
pp.update() # Uses directed hunting automatically
148+
```
149+
150+
## Next Steps
151+
152+
To extend the hunting behavior further, consider:
153+
154+
1. **Prey Flight**: Implement prey movement away from predators
155+
2. **Gaussian Kernels**: Replace Moore/Neumann with continuous interaction kernels
156+
3. **Sensing Distance**: Add parameter for predator vision range
157+
4. **Hunting Efficiency**: Modulate capture probability based on predator/prey numbers
158+
5. **Fatigue**: Add energy costs to directed movement
159+
160+
## Files Modified
161+
162+
- `models/CA.py` - Added predator hunting logic to `PP.update_sync()` and `PP.update_async()`
163+
- `test_predator_hunting.py` - New test suite (created)
164+
165+
## Testing
166+
167+
Run the test suite with:
168+
```bash
169+
python test_predator_hunting.py
170+
```
171+
172+
This validates:
173+
- Synchronous hunting mechanics
174+
- Asynchronous hunting mechanics
175+
- Neighborhood type independence
176+
- Correct parameter inheritance
177+
- Proper collision resolution

docs/experiments.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Metrics and measures
2+
This is what should be measured each run. These runs can then be further aggregated for final metrics.
3+
### Fixed parameter runs
4+
- Population count (mean and std after warmup)
5+
- Cluster size distribution (means and stds after warmup)
6+
### Evolution runs
7+
- Population count (over time after warmup)
8+
- Cluster size distribution (over time after warmup)
9+
- Prey death rate (mean and std over time after warmup)
10+
# Experiments
11+
These phases should be completed sequentially, deepening our understanding at each step. The different experiments in each phase should be completed with data from the same runs.
12+
### Phase 1: finding the critical point
13+
- Create bifurcation diagram of mean population count, varying prey death rate
14+
- Look for critical transition
15+
- Create log-log plot of cluster size distribution, varying prey death rate
16+
- Look for power-law
17+
### Phase 2: sensitivity analysis
18+
- Show correlation between critical prey death rate and post-evolution prey death rate, varying other parameters
19+
- Look for self-organized criticality: an SOC-system should move towards the critical point regardless of other parameters
20+
- Show sensitivity of hydra effect varying other parameters
21+
### Phase 3: perturbation analysis
22+
- Create autocorrelation plot of mean population count, following perturbations around the critical point
23+
- Look for critical slowing down: perturbations to states closer to the critical point should more slowly return to the steady state
24+
### Phase 4: model extensions
25+
- Investigate whether hydra effect and SOC still occur with diffusion and directed movement

docs/kimon_updates.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,11 @@ Add clustering size index.
300300
Pertrubtion on the prey count.
301301
Cluster size distribution mesauremnt.
302302

303-
powerlaw package.
304-
convergence of the disribution plots (p-values).
303+
Sary:
305304

306-
Is the system SOC?
307-
308-
Petrbutaions from initial conditions or critical point
309-
Timing between critical events? Get the model to be SOC.
310-
311-
Look at larger collapes and the average collapse?
305+
Bifuraction
306+
Clustering Measurement
307+
Warmpup and Measurement size
312308

313309

314310

mock_results/config.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
0.3,
55
0.15
66
],
7-
"n_prey_birth": 15,
8-
"n_prey_death": 15,
7+
"n_prey_birth": 10,
8+
"n_prey_death": 10,
99
"prey_birth_min": 0.1,
1010
"prey_birth_max": 0.35,
1111
"prey_death_min": 0.001,
1212
"prey_death_max": 0.1,
1313
"predator_death": 0.1,
1414
"predator_birth": 0.2,
15-
"n_replicates": 15,
15+
"n_replicates": 2,
1616
"warmup_steps": 200.0,
1717
"measurement_steps": 300,
1818
"cluster_samples": 1,
@@ -39,9 +39,11 @@
3939
],
4040
"sensitivity_replicates": 20,
4141
"synchronous": false,
42-
"directed_hunting": false,
42+
"directed_hunting": true,
4343
"save_diagnostic_plots": false,
4444
"diagnostic_param_sets": 5,
4545
"min_analysis_density": 0.002,
46+
"target_prey_birth": 0.22,
47+
"target_prey_death": 0.04,
4648
"n_jobs": -1
4749
}

mock_results/sweep_results.npz

-10 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)