Skip to content

Commit 45717f1

Browse files
committed
Fixed directed update kernel
Now chooses a random valid neighbor for reproduction instead of a random neighbor (without checking for validity) like in the undirected kernel
1 parent 617d6aa commit 45717f1

1 file changed

Lines changed: 23 additions & 42 deletions

File tree

models/numba_optimized.py

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def _pp_async_kernel_directed(
207207
evolution_stopped: bool,
208208
occupied_buffer: np.ndarray,
209209
) -> np.ndarray:
210-
"""Async predator-prey update kernel with directed hunting."""
210+
"""Async predator-prey update kernel with directed reproduction."""
211211
rows, cols = grid.shape
212212
n_shifts = len(dr_arr)
213213

@@ -229,19 +229,22 @@ def _pp_async_kernel_directed(
229229
c = occupied_buffer[i, 1]
230230

231231
state = grid[r, c]
232-
if state == 0:
233-
continue
234232

235233
if state == 1: # PREY
236-
nbi = np.random.randint(0, n_shifts)
237-
nr = (r + dr_arr[nbi]) % rows
238-
nc = (c + dc_arr[nbi]) % cols
239-
240234
if np.random.random() < prey_death_arr[r, c]:
241235
grid[r, c] = 0
242236
prey_death_arr[r, c] = np.nan
243-
elif grid[nr, nc] == 0:
244-
if np.random.random() < p_birth_val:
237+
elif np.random.random() < p_birth_val:
238+
valid = []
239+
for k in range(n_shifts):
240+
nr = (r + dr_arr[k]) % rows
241+
nc = (c + dc_arr[k]) % cols
242+
if grid[nr, nc] == 0:
243+
valid.append(k)
244+
if len(valid) > 0:
245+
choice = np.random.choice(valid)
246+
nr = (r + dr_arr[choice]) % rows
247+
nc = (c + dc_arr[choice]) % cols
245248
grid[nr, nc] = 1
246249
parent_val = prey_death_arr[r, c]
247250
if not evolution_stopped:
@@ -254,44 +257,22 @@ def _pp_async_kernel_directed(
254257
else:
255258
prey_death_arr[nr, nc] = parent_val
256259

257-
elif state == 2: # PREDATOR - directed hunting
260+
elif state == 2: # PREDATOR
258261
if np.random.random() < pred_death_val:
259262
grid[r, c] = 0
260-
continue
261-
262-
prey_count = 0
263-
for k in range(n_shifts):
264-
check_r = (r + dr_arr[k]) % rows
265-
check_c = (c + dc_arr[k]) % cols
266-
if grid[check_r, check_c] == 1:
267-
prey_count += 1
268-
269-
if prey_count > 0:
270-
target_idx = np.random.randint(0, prey_count)
271-
found = 0
272-
nr, nc = 0, 0
263+
elif np.random.random() < pred_birth_val:
264+
valid = []
273265
for k in range(n_shifts):
274-
check_r = (r + dr_arr[k]) % rows
275-
check_c = (c + dc_arr[k]) % cols
276-
if grid[check_r, check_c] == 1:
277-
if found == target_idx:
278-
nr = check_r
279-
nc = check_c
280-
break
281-
found += 1
282-
283-
if np.random.random() < pred_birth_val:
266+
nr = (r + dr_arr[k]) % rows
267+
nc = (c + dc_arr[k]) % cols
268+
if grid[nr, nc] == 1:
269+
valid.append(k)
270+
if len(valid) > 0:
271+
choice = np.random.choice(valid)
272+
nr = (r + dr_arr[choice]) % rows
273+
nc = (c + dc_arr[choice]) % cols
284274
grid[nr, nc] = 2
285275
prey_death_arr[nr, nc] = np.nan
286-
else:
287-
nbi = np.random.randint(0, n_shifts)
288-
nr = (r + dr_arr[nbi]) % rows
289-
nc = (c + dc_arr[nbi]) % cols
290-
291-
if grid[nr, nc] == 1:
292-
if np.random.random() < pred_birth_val:
293-
grid[nr, nc] = 2
294-
prey_death_arr[nr, nc] = np.nan
295276

296277
return grid
297278

0 commit comments

Comments
 (0)