|
| 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 |
0 commit comments