Skip to content

Commit d0b4dc5

Browse files
committed
fix logic issues in predprey example
Ensure prey (1) have children, which (2) aren’t predators
1 parent 1134124 commit d0b4dc5

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

episodes/files/pred-prey/predprey.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ def avoid_predators(self, predator_list):
5151

5252
# Add a steering factor away from each predator. Strength increases with closeness.
5353
for predator in predator_list:
54-
# Fetch location of predator
55-
predator_x = self.x;
56-
predator_y = self.y;
57-
58-
# Check if the two predators are within interaction radius
54+
# Check if predator and prey are within interaction radius
5955
dx = self.x - predator.x
6056
dy = self.y - predator.y
6157
distance = math.sqrt(dx * dx + dy * dy)
@@ -159,6 +155,7 @@ def reproduce(self):
159155
child.vx = np.random.uniform() * 2 - 1
160156
child.vy = np.random.uniform() * 2 - 1
161157
child.life = self.life
158+
return child
162159

163160

164161
class Predator:
@@ -304,7 +301,7 @@ def eaten(self, prey_list):
304301

305302
class Model:
306303

307-
def __init__(self, steps = 250):
304+
def __init__(self, steps = 50):
308305
self.steps = steps
309306
self.num_prey = 200
310307
self.num_predators = 50
@@ -373,7 +370,7 @@ def _step(self):
373370
c = p.reproduce()
374371
if c:
375372
children.append(c)
376-
self.predators.extend(children)
373+
self.prey.extend(children)
377374
children = []
378375
for p in self.predators:
379376
c = p.reproduce()

0 commit comments

Comments
 (0)