Skip to content

Commit 46003d2

Browse files
authored
Update homework submission text (#18)
1 parent 8f50b93 commit 46003d2

File tree

3 files changed

+189
-11
lines changed

3 files changed

+189
-11
lines changed

docs/src/lecture_02/hw.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ include(joinpath(projdir,"src","lecture_02","Lab02Ecosystem.jl"))
1111

1212
## How to submit?
1313

14-
Put all your code (including your or the provided solution of lab 2) in a
15-
script named `hw.jl` alongside with the `Project.toml` and `Manifest.toml` of
16-
the environment. Please only include packages in the environment that are
17-
necessary for the homework. Create a `.zip` archive of the three files and
18-
send it to the lab instructor, who has assigned the task, via email (contact
19-
emails are located on the [homepage](@ref emails) of the course).
14+
Put all your code (including your or the provided solution of lab 2)
15+
in a script named `hw.jl`. Zip only this file (not its parent folder) and
16+
upload it to BRUTE. Your file can contain one dependency `using StatsBase`,
17+
but no other packages are can to be used. For example, having a `using Plots`
18+
in your code will cause the automatic evaluation to fail.
19+
2020

2121

2222
## Counting Agents
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
using StatsBase
2+
3+
abstract type Species end
4+
abstract type Agent{S<:Species} end
5+
6+
abstract type PlantSpecies <: Species end
7+
abstract type Grass <: PlantSpecies end
8+
9+
abstract type AnimalSpecies <: Species end
10+
abstract type Sheep <: AnimalSpecies end
11+
abstract type Wolf <: AnimalSpecies end
12+
13+
abstract type Sex end
14+
abstract type Male <: Sex end
15+
abstract type Female <: Sex end
16+
17+
id(a::Agent) = a.id
18+
19+
20+
########## World Definition ##################################################
21+
22+
mutable struct World{A<:Agent}
23+
agents::Dict{Int,A}
24+
max_id::Int
25+
end
26+
27+
function World(agents::Vector{<:Agent})
28+
ids = id.(agents)
29+
length(unique(ids)) == length(agents) || error("Not all agents have unique IDs!")
30+
World(Dict(id(a)=>a for a in agents), maximum(ids))
31+
end
32+
33+
function world_step!(world::World)
34+
for id in deepcopy(keys(world.agents))
35+
!haskey(world.agents,id) && continue
36+
a = world.agents[id]
37+
agent_step!(a,world)
38+
end
39+
end
40+
41+
function Base.show(io::IO, w::World)
42+
println(io, typeof(w))
43+
for (_,a) in w.agents
44+
println(io," $a")
45+
end
46+
end
47+
48+
49+
########## Plant Definition ##################################################
50+
51+
mutable struct Plant{P<:PlantSpecies} <: Agent{P}
52+
id::Int
53+
size::Int
54+
max_size::Int
55+
end
56+
57+
Base.size(a::Plant) = a.size
58+
max_size(a::Plant) = a.max_size
59+
grow!(a::Plant) = a.size += 1
60+
61+
# constructor for all Plant{<:PlantSpecies} callable as PlantSpecies(...)
62+
(A::Type{<:PlantSpecies})(id, s, m) = Plant{A}(id,s,m)
63+
(A::Type{<:PlantSpecies})(id, m) = (A::Type{<:PlantSpecies})(id,rand(1:m),m)
64+
65+
function agent_step!(a::Plant, w::World)
66+
if size(a) != max_size(a)
67+
grow!(a)
68+
end
69+
end
70+
71+
function Base.show(io::IO, p::Plant{P}) where P
72+
x = size(p)/max_size(p) * 100
73+
print(io,"$P #$(id(p)) $(round(Int,x))% grown")
74+
end
75+
76+
Base.show(io::IO, ::Type{Grass}) = print(io,"🌿")
77+
78+
79+
########## Animal Definition #################################################
80+
81+
mutable struct Animal{A<:AnimalSpecies,S<:Sex} <: Agent{A}
82+
id::Int
83+
energy::Float64
84+
Δenergy::Float64
85+
reprprob::Float64
86+
foodprob::Float64
87+
end
88+
89+
energy(a::Animal) = a.energy
90+
Δenergy(a::Animal) = a.Δenergy
91+
reprprob(a::Animal) = a.reprprob
92+
foodprob(a::Animal) = a.foodprob
93+
energy!(a::Animal, e) = a.energy = e
94+
incr_energy!(a::Animal, Δe) = energy!(a, energy(a)+Δe)
95+
96+
function (A::Type{<:AnimalSpecies})(id::Int, E, ΔE, pr, pf, S=rand(Bool) ? Female : Male)
97+
Animal{A,S}(id,E,ΔE,pr,pf)
98+
end
99+
100+
function agent_step!(a::Animal, w::World)
101+
incr_energy!(a,-1)
102+
if rand() <= foodprob(a)
103+
dinner = find_food(a,w)
104+
eat!(a, dinner, w)
105+
end
106+
if energy(a) <= 0
107+
kill_agent!(a,w)
108+
return
109+
end
110+
if rand() <= reprprob(a)
111+
reproduce!(a,w)
112+
end
113+
return a
114+
end
115+
116+
function find_rand(f, w::World)
117+
xs = filter(f, w.agents |> values |> collect)
118+
isempty(xs) ? nothing : sample(xs)
119+
end
120+
121+
find_food(a::Animal, w::World) = find_rand(x->eats(a,x),w)
122+
123+
eats(::Animal{Sheep},p::Plant{Grass}) = size(p)>0
124+
eats(::Animal{Wolf},::Animal{Sheep}) = true
125+
eats(::Agent,::Agent) = false
126+
127+
function eat!(a::Animal{Wolf}, b::Animal{Sheep}, w::World)
128+
incr_energy!(a, energy(b)*Δenergy(a))
129+
kill_agent!(b,w)
130+
end
131+
function eat!(a::Animal{Sheep}, b::Plant{Grass}, w::World)
132+
incr_energy!(a, size(b)*Δenergy(a))
133+
b.size = 0
134+
end
135+
eat!(::Animal,::Nothing,::World) = nothing
136+
137+
function reproduce!(a::A, w::World) where A<:Animal
138+
b = find_mate(a,w)
139+
if !isnothing(b)
140+
energy!(a, energy(a)/2)
141+
a_vals = [getproperty(a,n) for n in fieldnames(A) if n!=:id]
142+
new_id = w.max_id + 1
143+
= A(new_id, a_vals...)
144+
w.agents[id(â)] =
145+
w.max_id = new_id
146+
end
147+
end
148+
149+
find_mate(a::Animal, w::World) = find_rand(x->mates(a,x),w)
150+
151+
function mates(a,b)
152+
error("""You have to specify the mating behaviour of your agents by overloading `mates` e.g. like this:
153+
154+
mates(a::Animal{S,Female}, b::Animal{S,Male}) where S<:Species = true
155+
mates(a::Animal{S,Male}, b::Animal{S,Female}) where S<:Species = true
156+
mates(a::Agent, b::Agent) = false
157+
""")
158+
end
159+
160+
kill_agent!(a::Animal, w::World) = delete!(w.agents, id(a))
161+
162+
Base.show(io::IO, ::Type{Sheep}) = print(io,"🐑")
163+
Base.show(io::IO, ::Type{Wolf}) = print(io,"🐺")
164+
Base.show(io::IO, ::Type{Male}) = print(io,"")
165+
Base.show(io::IO, ::Type{Female}) = print(io,"")
166+
function Base.show(io::IO, a::Animal{A,S}) where {A,S}
167+
e = energy(a)
168+
d = Δenergy(a)
169+
pr = reprprob(a)
170+
pf = foodprob(a)
171+
print(io,"$A$S #$(id(a)) E=$e ΔE=$d pr=$pr pf=$pf")
172+
end

docs/src/lecture_03/hw.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ using Scientific_Programming_in_Julia
77

88
In this homework we will add another species of plant to our simulation
99
(*poisoned mushrooms*) and practice the use of closures with callback
10-
functions.
11-
The solution of lab 3 can be found on Github for the
12-
[`World`](https://github.com/JuliaTeachingCTU/EcosystemCore.jl/blob/main/src/world.jl),
13-
[`Plant`s](https://github.com/JuliaTeachingCTU/EcosystemCore.jl/blob/main/src/plant.jl), and
14-
[`Animal`s](https://github.com/JuliaTeachingCTU/EcosystemCore.jl/blob/main/src/animal.jl).
10+
functions. The solution of lab 3 can be found
11+
[here](https://github.com/JuliaTeachingCTU/Scientific-Programming-in-Julia/blob/master/docs/src/lecture_03/Lab03Ecosystem.jl). You can use this file and add the code that you write
12+
for the homework to it.
13+
14+
## How to submit?
15+
16+
Put all your code (including your or the provided solution of lab 2)
17+
in a script named `hw.jl`. Zip only this file (not its parent folder) and
18+
upload it to BRUTE. Your file can contain one dependency `using StatsBase`,
19+
but no other packages are can to be used. For example, having a `using Plots`
20+
in your code will cause the automatic evaluation to fail.
1521

1622

1723
## Poisoned Mushrooms

0 commit comments

Comments
 (0)