Skip to content

Commit aa1a026

Browse files
committed
Add steps argument to Predator Prey exercise code
Argument is mandatory, and requires a positive integer, previously defaulted to 250 Closes #83
1 parent 4591178 commit aa1a026

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

episodes/files/pred-prey/predprey.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,14 @@ def run(self, random_seed=12):
415415
# plot graph of results
416416
self._plot()
417417

418+
# Argument parsing
419+
if len(sys.argv) != 2:
420+
print("Script expects 1 positive integer argument (number of steps), %u found."%(len(sys.argv) - 1))
421+
sys.exit()
422+
steps = int(sys.argv[1])
423+
if steps < 1:
424+
print("Script expects 1 positive integer argument (number of steps), %s converts < 1."%(sys.argv[1]))
425+
sys.exit()
418426

419-
420-
421-
model = Model()
427+
model = Model(steps=steps)
422428
model.run()

episodes/profiling-functions.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,11 @@ Download and profile <a href="files/pred-prey/predprey.py" download>the Python p
437437
>
438438
> The three agents; predators, prey and grass exist in a two dimensional grid. Predators eat prey, prey eat grass. The size of each population changes over time. Depending on the parameters of the model, the populations may oscillate, grow or collapse due to the availability of their food source.
439439
440-
The program can be executed via `python predprey.py`.
440+
The program can be executed via `python predprey.py <steps>`.
441+
The value of `steps` for a full run is 250, however a full run may not be necessary to find the bottlenecks.
441442

442-
It takes no arguments, but contains various environment properties which can be modified to change the model's behaviour.
443443
When the model finishes it outputs a graph of the three populations `predprey_out.png`.
444444

445-
446445
:::::::::::::::::::::::: solution
447446

448447
It should be clear from the profile that the method `Grass::eaten()` (from `predprey.py:278`) occupies the majority of the runtime.

episodes/profiling-lines.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,10 @@ from line_profiler import profile
461461
def eaten(self, prey_list):
462462
```
463463

464-
`line_profiler` can then be executed via `python -m kernprof -lvr predprey.py`.
464+
`line_profiler` can then be executed via `python -m kernprof -lvr predprey.py <steps>`.
465465

466-
This will take much longer to run due to `line_profiler`, you may wish to reduce the number of steps. In this instance it may change the profiling output slightly, as the number of `Prey` and their member variables evaluated by this method both change as the model progresses, but the overall pattern is likely to remain similar.
466+
This will take much longer to run due to `line_profiler`, you may wish profile less steps than you did in the function-level profiling exercise (250 was suggested for a full run).
467+
In this instance it may change the profiling output slightly, as the number of `Prey` and their member variables evaluated by this method both change as the model progresses, but the overall pattern is likely to remain similar.
467468

468469
```python
469470
# line ~420

0 commit comments

Comments
 (0)