Skip to content

Commit 92674ad

Browse files
authored
Lab 02 update (#9)
1 parent c8655d6 commit 92674ad

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/Lab02Ecosystem.jl renamed to docs/src/lecture_02/Lab02Ecosystem.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ food_prob(a::Animal) = a.food_prob
9696
energy!(a::Animal, e) = a.energy = e
9797
incr_energy!(a::Animal, Δe) = energy!(a, energy(a)+Δe)
9898

99+
function Base.show(io::IO, g::Grass)
100+
x = size(g)/max_size(g) * 100
101+
print(io,"🌿 #$(id(g)) $(round(Int,x))% grown")
102+
end
103+
function Base.show(io::IO, w::Wolf)
104+
e = energy(w)
105+
d = Δenergy(w)
106+
pr = reprprob(w)
107+
pf = foodprob(w)
108+
print(io,"🐺 #$(id(w)) E=$e ΔE=$d pr=$pr pf=$pf")
109+
end
110+
function Base.show(io::IO, s::Sheep)
111+
e = energy(s)
112+
d = Δenergy(s)
113+
pr = reprprob(s)
114+
pf = foodprob(s)
115+
print(io,"🐑 #$(id(s)) E=$e ΔE=$d pr=$pr pf=$pf")
116+
end
117+
118+
99119
function eat!(a::Sheep, b::Grass, w::World)
100120
incr_energy!(a, size(b)*Δenergy(a))
101121
kill_agent!(b,w)

docs/src/lecture_02/hw.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
In this lab you will continue working on your agent simulation. If you did not
44
manage to finish the homework, do not worry, you can use [this
5-
script](https://github.com/JuliaTeachingCTU/Scientific-Programming-in-Julia/blob/master/src/Lab02Ecosystem.jl)
5+
script](https://github.com/JuliaTeachingCTU/Scientific-Programming-in-Julia/blob/master/docs/src/lecture_02/Lab02Ecosystem.jl)
66
which contains all the functionality we developed in the lab.
77
```@setup hw02
88
projdir = dirname(Base.active_project())
9-
include(joinpath(projdir,"..","src","Lab02Ecosystem.jl"))
9+
include(joinpath(projdir,"src","lecture_02","Lab02Ecosystem.jl"))
1010
```
1111

1212
## How to submit?

docs/src/lecture_02/lab.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ In this lab we will first define what our agent simulation does on a high level.
1515
Then you will write the core methods for finding food (`find_food`),
1616
to specify what an animal eats (`eat!`), and how it reproduces (`reproduce!`).
1717

18+
### High level-description
1819
In an agent simulation we assume that we have a bunch of agents (in our case
1920
grass, 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
2122
performs some of the actions that it can take. For example, a *grass* agent will grow
2223
a little at every step. A *sheep* agent will try to find some grass and
2324
reproduce.
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`
2634
acts as the root of our tree. All animals and plants will be subtypes of `Agent`.
2735
There 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
154162
The constructor for grass with random growth countdown:
155163
```@example non_parametric_agents
156164
Grass(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
157171
nothing # hide
158172
```
159173
Creation of a world with a few grass agents:
@@ -196,6 +210,15 @@ foodprob(a::Animal) = a.foodprob
196210
# set field values
197211
energy!(a::Animal, e) = a.energy = e
198212
incr_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
199222
nothing # hide
200223
```
201224

@@ -285,6 +308,15 @@ function eat!(wolf::Wolf, sheep::Sheep, w::World)
285308
end
286309
287310
kill_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
288320
nothing # hide
289321
```
290322
```@raw html

0 commit comments

Comments
 (0)