Skip to content

Commit 0c6e8b6

Browse files
RobadobJostMigenda
andauthored
Add steps argument to Predator Prey exercise code (#84)
* Add steps argument to Predator Prey exercise code Argument is mandatory, and requires a positive integer, previously defaulted to 250 Closes #83 * Update episodes/profiling-lines.md Co-authored-by: Jost Migenda <[email protected]> * Update episodes/files/pred-prey/predprey.py Co-authored-by: Jost Migenda <[email protected]> * Jost review suggestions --------- Co-authored-by: Jost Migenda <[email protected]>
1 parent 4591178 commit 0c6e8b6

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

episodes/files/pred-prey/predprey.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import math
23
import numpy as np
34
import matplotlib.pyplot as plt
@@ -415,8 +416,14 @@ def run(self, random_seed=12):
415416
# plot graph of results
416417
self._plot()
417418

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

419-
420-
421-
model = Model()
428+
model = Model(steps=steps)
422429
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +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.
467-
468-
```python
469-
# line ~420
470-
model = Model(50) # 50 steps (originally defaulted to 250)
471-
```
466+
Since this will take much longer to run due to `line_profiler`, you may wish to profile fewer `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.
472468

473469
Alternatively, you can kill the profiling process (e.g. `ctrl + c`) after a minute and the currently collected partial profiling information will be output.
474470

0 commit comments

Comments
 (0)