From d0b4dc590a57bf9f9bc9129e8bb051d158aea327 Mon Sep 17 00:00:00 2001 From: Jost Migenda Date: Mon, 3 Nov 2025 07:44:24 +0000 Subject: [PATCH] =?UTF-8?q?fix=20logic=20issues=20in=20predprey=20example?= =?UTF-8?q?=20Ensure=20prey=20(1)=20have=20children,=20which=20(2)=20aren?= =?UTF-8?q?=E2=80=99t=20predators?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- episodes/files/pred-prey/predprey.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/episodes/files/pred-prey/predprey.py b/episodes/files/pred-prey/predprey.py index bf927b0a..9ad0cfb1 100644 --- a/episodes/files/pred-prey/predprey.py +++ b/episodes/files/pred-prey/predprey.py @@ -51,11 +51,7 @@ def avoid_predators(self, predator_list): # Add a steering factor away from each predator. Strength increases with closeness. for predator in predator_list: - # Fetch location of predator - predator_x = self.x; - predator_y = self.y; - - # Check if the two predators are within interaction radius + # Check if predator and prey are within interaction radius dx = self.x - predator.x dy = self.y - predator.y distance = math.sqrt(dx * dx + dy * dy) @@ -159,6 +155,7 @@ def reproduce(self): child.vx = np.random.uniform() * 2 - 1 child.vy = np.random.uniform() * 2 - 1 child.life = self.life + return child class Predator: @@ -304,7 +301,7 @@ def eaten(self, prey_list): class Model: - def __init__(self, steps = 250): + def __init__(self, steps = 50): self.steps = steps self.num_prey = 200 self.num_predators = 50 @@ -373,7 +370,7 @@ def _step(self): c = p.reproduce() if c: children.append(c) - self.predators.extend(children) + self.prey.extend(children) children = [] for p in self.predators: c = p.reproduce()