Skip to content

Commit ed85bf2

Browse files
committed
Added partially synchronous update to numba_optimized
1 parent d5df509 commit ed85bf2

2 files changed

Lines changed: 67 additions & 110 deletions

File tree

models/numba_optimized.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,65 @@ def set_numba_seed(seed: int) -> None:
5656
# ============================================================================
5757
# PREDATOR-PREY KERNELS
5858
# ============================================================================
59+
@njit(cache=True)
60+
def _pp_async_kernel_fast(
61+
grid: np.ndarray,
62+
prey_death_arr: np.ndarray,
63+
p_birth_val: float,
64+
p_death_val: float,
65+
pred_birth_val: float,
66+
pred_death_val: float,
67+
dr_arr: np.ndarray,
68+
dc_arr: np.ndarray,
69+
evolve_sd: float,
70+
evolve_min: float,
71+
evolve_max: float,
72+
evolution_stopped: bool,
73+
occupied_buffer: np.ndarray,
74+
) -> np.ndarray:
75+
"""Partially synchronous predator-prey update kernel."""
76+
rows, cols = grid.shape
77+
n_shifts = len(dr_arr)
78+
grid_copy = grid.copy()
79+
prey_death_arr_copy = prey_death_arr.copy()
80+
81+
prey_death = np.random.random(size=grid.shape)
82+
grid_copy[(grid == 1) & (prey_death < prey_death_arr)] = 0
83+
prey_death_arr_copy[(grid == 1) & (prey_death < prey_death_arr)] = np.nan
84+
85+
pred_death = np.random.random(size=grid.shape)
86+
grid_copy[(grid == 2) & (pred_death < pred_death_val)] = 0
87+
88+
count = np.count_nonzero(grid)
89+
indices = np.random.permutation(count)
90+
rs = indices // cols
91+
cs = indices % cols
92+
93+
nb = np.random.randint(0, n_shifts, size=count)
94+
nrs = (rs + dr_arr[nb]) % rows
95+
ncs = (cs + dc_arr[nb]) % cols
96+
97+
for r, c, nr, nc in zip(rs, cs, nrs, ncs):
98+
state = grid[r, c]
99+
nstate = grid[nr, nc]
100+
101+
if state == 1 and nstate == 0 and np.random.random() < p_birth_val:
102+
grid_copy[nr, nc] = 1
103+
parent_val = prey_death_arr[r, c]
104+
if not evolution_stopped:
105+
child_val = parent_val + np.random.normal(0, evolve_sd)
106+
prey_death_arr_copy[nr, nc] = np.clip(child_val, evolve_min, evolve_max)
107+
else:
108+
prey_death_arr_copy[nr, nc] = parent_val
109+
110+
elif state == 2 and nstate == 1 and np.random.random() < pred_birth_val:
111+
grid_copy[nr, nc] = 2
112+
prey_death_arr_copy[nr, nc] = np.nan
113+
114+
grid = grid_copy
115+
prey_death_arr = prey_death_arr_copy
116+
117+
return grid
59118

60119
@njit(cache=True)
61120
def _pp_async_kernel_random(

notebooks/phase1.ipynb

Lines changed: 8 additions & 110 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)