@@ -236,8 +236,10 @@ input types and is our first practical example of [*multiple dispatch*](https://
236236```
237237Implement a function ` eat!(::Sheep, ::Grass, ::World) ` which increases the sheep's
238238energy by $\Delta E$ multiplied by the size of the grass.
239+
239240After the sheep's energy is updated the grass is eaten and its size counter has
240241to be set to zero.
242+
241243Note that you do not yet need the world in this function. It is needed later
242244for the case of wolves eating sheep.
243245``` @raw html
@@ -248,9 +250,8 @@ for the case of wolves eating sheep.
248250``` @example non_parametric_agents
249251function eat!(a::Sheep, b::Grass, w::World)
250252 incr_energy!(a, size(b)*Δenergy(a))
251- kill_agent!(b,w)
253+ b.size = 0
252254end
253- kill_agent!(a::Plant, w::World) = a.size = 0
254255nothing # hide
255256```
256257``` @raw html
@@ -281,9 +282,12 @@ eat!(grass,sheep,world);
281282<div class="admonition-body">
282283```
283284Next, implement a ` Wolf ` with the same properties as the sheep ($E$, $\Delta
284- E$, $p_r$, and $p_f$) as well as the correspoding ` eat! ` method which increases
285+ E$, $p_r$, and $p_f$) as well as its ` eat! ` method. The ` eat! ` method for wolves increases
285286the wolf's energy by ` energy(sheep)*Δenergy(wolf) ` and kills the sheep (i.e.
286287removes the sheep from the world).
288+ Both ` eat! ` and ` agent_step! ` need to be able to remove agents from the world
289+ so it makes sense to create another function ` kill_agent!(::Animal,::World) ` .
290+ Please implement it as well to make your ` agent_step! ` work.
287291
288292Hint: You can use ` delete! ` to remove agents from the dictionary in your world.
289293
@@ -343,7 +347,7 @@ function find_food(a::Animal, w::World)
343347 isempty(as) ? nothing : sample(as)
344348end
345349
346- eats(::Sheep,::Grass) = true
350+ eats(::Sheep,g ::Grass) = size(g) > 0
347351eats(::Wolf,::Sheep) = true
348352eats(::Agent,::Agent) = false
349353```
@@ -362,7 +366,8 @@ random `Grass` from all available `Grass` agents.
362366```
363367
364368Implement the method ` find_food(::Sheep, ::World) ` which first returns either a
365- ` Grass ` (sampled randomly from all ` Grass ` es) or returns ` nothing ` .
369+ ` Grass ` (sampled randomly from all ` Grass ` es with a size larger than zero) or
370+ returns ` nothing ` .
366371
3673721 . Hint: For the functional programming way of coding this can use ` filter ` and
368373 ` isa ` to filter for a certain type and ` StatsBase.sample ` to choose a random
@@ -382,7 +387,7 @@ using StatsBase # needed for `sample`
382387# you can install it by typing `]add StatsBase` in the REPL
383388
384389function find_food (a:: Sheep , w:: World )
385- as = filter (x-> isa (x,Grass), w. agents |> values |> collect)
390+ as = filter (x-> isa (x,Grass) && size (x) > 0 , w. agents |> values |> collect)
386391 isempty (as) ? nothing : sample (as)
387392end
388393```
@@ -460,7 +465,7 @@ function find_food(a::Animal, w::World)
460465 isempty (as) ? nothing : sample (as)
461466end
462467
463- eats (:: Sheep ,:: Grass ) = true
468+ eats (:: Sheep ,g :: Grass ) = size (g) > 0
464469eats (:: Wolf ,:: Sheep ) = true
465470eats (:: Agent ,:: Agent ) = false
466471```
0 commit comments