You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: prompts.md
+113Lines changed: 113 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -412,4 +412,117 @@ Implement the synchronous updating for the PP class as a vectorized (optimized)
412
412
self.grid[prey_death_mask] = 0
413
413
self.grid[pred_death_mask] = 0
414
414
415
+
### changes
416
+
Swapped the order of birth and death checks. This results in the same behavior, but removes the need to check whether the post-birth grid matches the reference grid.
417
+
418
+
## Add basic visualisations
419
+
Basic tests pass and the grid updates seem plausible, but to see it in better detail we need to add visualisation. A first step in this is an imshow plot which updates every N iterations.
420
+
421
+
### prompt
422
+
Add optional grid visualisation support to the CA class. Use matplotlib.pyplot's interactive mode to dynamically update the the plot every N intervals. Do this by adding a function visualize which takes the interval, the figsize, the pause, and a colormap. this function should turn on the visualisation, creating the necessary variables. Ensure that there is reasonable default behavior for all arguments, including the colormap. Make sure the plot gets updated each interval iterations inside of the run function, showing the current iteration. if this would bloat the run function, add a helper function to update the plot.
423
+
424
+
Next, update the PP class to change the default to a black empty cells, with green prey and red predators. Again, ensure that this can be overridden by the user when calling visualize, just like the other arguments.
425
+
426
+
For both classes, ensure that the matplotlib library is not needed to run the simulation without visualisation. Also, make the necessary overrides (for the colormap default for instance) as short as possible. I do not want classes inheriting CA to have to override the entire visualize function just to change the default color map. However, if visualization is off, there is no need to store any of the variables in the CA / PP class.
427
+
428
+
### output (also added the defaults)
429
+
def run(self, steps: int) -> None:
430
+
"""Run the CA for a number of steps.
431
+
432
+
Args:
433
+
- steps (int): number of iterations to run (must be non-negative).
434
+
435
+
Returns: None
436
+
"""
437
+
assert isinstance(steps, int) and steps >= 0, "steps must be a non-negative integer"
438
+
for i in range(steps):
439
+
self.update()
440
+
# Update visualization if enabled every `interval` iterations
441
+
if getattr(self, "_viz_on", False):
442
+
# iteration number is 1-based for display
443
+
try:
444
+
self._viz_update(i + 1)
445
+
except Exception:
446
+
# Don't let visualization errors stop the simulation
447
+
pass
448
+
449
+
def visualize(
450
+
self,
451
+
interval: int = 1,
452
+
figsize: Tuple[float, float] = (5, 5),
453
+
pause: float = 0.001,
454
+
cmap=None,
455
+
) -> None:
456
+
"""Enable interactive visualization of the grid.
457
+
458
+
Args:
459
+
- interval: update plot every `interval` iterations (>=1)
460
+
- figsize: figure size passed to matplotlib
461
+
- pause: seconds to pause after draw (controls responsiveness)
462
+
- cmap: colormap spec (string, sequence of colors, or matplotlib Colormap).
463
+
464
+
This function imports matplotlib lazily so simulations without
465
+
visualization do not require matplotlib to be installed.
466
+
"""
467
+
if not isinstance(interval, int) or interval < 1:
468
+
raise ValueError("interval must be a positive integer")
0 commit comments