@@ -15,14 +15,22 @@ In this lab we will first define what our agent simulation does on a high level.
1515Then you will write the core methods for finding food (` find_food ` ),
1616to specify what an animal eats (` eat! ` ), and how it reproduces (` reproduce! ` ).
1717
18+ ### High level-description
1819In an agent simulation we assume that we have a bunch of agents (in our case
1920grass, sheep, and wolves) that act in some environment (we will call it a
2021* world* ). At every iteration of the simulation each agent will perform a * step* in which it
2122performs some of the actions that it can take. For example, a * grass* agent will grow
2223a little at every step. A * sheep* agent will try to find some grass and
2324reproduce.
2425
25- To get started we need a type hierarchy. The first abstract type ` Agent ` that
26+ In short:
27+ * Wolves, sheep, and grass exist in a one dimensional world ` Dict(1=>🐺, 2=>🐑, 3=>🌿, 4=>🌿, ...) `
28+ and are identified by a unique ID
29+ * Each agent can perform certain actions (eating, growing, reproducing, dying...)
30+ * In one iteration of the simulation each agent performs its actions
31+
32+ ### Code skeleton
33+ To get started we need a type hierarchy. The first abstract type ` Agent `
2634acts as the root of our tree. All animals and plants will be subtypes of ` Agent ` .
2735There are different kinds of animals and plants so it makes sense to create an
2836` Animal ` type which will be the supertype of all animals. The same is
@@ -154,6 +162,12 @@ nothing # hide
154162The constructor for grass with random growth countdown:
155163``` @example non_parametric_agents
156164Grass(id,m) = Grass(id, rand(1:m), m)
165+
166+ # optional: overload show function for Grass
167+ function Base.show(io::IO, g::Grass)
168+ x = size(g)/max_size(g) * 100
169+ print(io,"🌿 #$(id(g)) $(round(Int,x))% grown")
170+ end
157171nothing # hide
158172```
159173Creation of a world with a few grass agents:
@@ -196,6 +210,15 @@ foodprob(a::Animal) = a.foodprob
196210# set field values
197211energy!(a::Animal, e) = a.energy = e
198212incr_energy!(a::Animal, Δe) = energy!(a, energy(a)+Δe)
213+
214+ # optional: overload the show method for Sheep
215+ function Base.show(io::IO, s::Sheep)
216+ e = energy(s)
217+ d = Δenergy(s)
218+ pr = reprprob(s)
219+ pf = foodprob(s)
220+ print(io,"🐑 #$(id(s)) E=$e ΔE=$d pr=$pr pf=$pf")
221+ end
199222nothing # hide
200223```
201224
@@ -285,6 +308,15 @@ function eat!(wolf::Wolf, sheep::Sheep, w::World)
285308end
286309
287310kill_agent!(a::Animal, w::World) = delete!(w.agents, id(a))
311+
312+ # optional: overload the show method for Wolf
313+ function Base.show(io::IO, w::Wolf)
314+ e = energy(w)
315+ d = Δenergy(w)
316+ pr = reprprob(w)
317+ pf = foodprob(w)
318+ print(io,"🐺 #$(id(w)) E=$e ΔE=$d pr=$pr pf=$pf")
319+ end
288320nothing # hide
289321```
290322``` @raw html
0 commit comments