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
+112-1Lines changed: 112 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -157,4 +157,115 @@ class CA:
157
157
Removed a few unnecessary lines.
158
158
159
159
## Add update logic
160
-
Now the basic predator-prey update rules need to be defined. This is done
160
+
Now the basic predator-prey update rules need to be defined. This is done by creating a predator-prey class inheriting the CA class and overriding the update function.
161
+
162
+
### prompt
163
+
Create a predator-prey class named PP which inherits the CA class. Create default values for the following parameters: prey_death, predator_death, reproduction, consumption. Make sure not to override the user-set values for these parameters. Also raise an error if there are any other user-defined parameters and check whether the set parameters are valid (between 0 and 1).
164
+
165
+
Next, override the update function with the following functionality. First, create a copy of the current grid to be used for all the rules. Then, use the inherited count_neighbors function. For each empty cell with at least one neighboring prey, populate it with a prey (state 1) with a random chance. Use generator.random() < 1 - (1 - birth)**prey_neighbors to simulate the possibility for each neighboring prey to reproduce into the cell. For each prey cell in the copied grid with at least one neighboring predator, replace it with a predator with a random chance. use the same logic for this. Afterwards, handle the prey and predator deaths stochastically based on the relevant parameter. Use the copied grid for this to ensure newly occupied cells dont instantly become empty.
166
+
167
+
### output
168
+
class PP(CA):
169
+
"""Predator-Prey cellular automaton.
170
+
171
+
States: 0 = empty, 1 = prey, 2 = predator
172
+
173
+
Expected params keys (all values in [0,1]):
174
+
- "prey_death": probability a prey dies each step
175
+
- "predator_death": probability a predator dies each step
176
+
- "reproduction": per-neighbor prey reproduction probability
177
+
- "consumption": per-neighbor predation probability
178
+
179
+
Defaults are provided for any missing keys and user-provided values
180
+
are preserved. Any unknown keys in `params` will raise a ValueError.
0 commit comments