countdown replaced by growth to count up and fix grass colors

event winter added
events improved
main
Daniel Philipp 2024-06-23 02:16:55 +02:00
parent 5b6891bdd8
commit c4a202d68b
2 changed files with 67 additions and 30 deletions

View File

@ -107,6 +107,7 @@ function eat!(a::Animal, model)
end end
if "Grass" a.def.food && model.fully_grown[a.pos...] if "Grass" a.def.food && model.fully_grown[a.pos...]
model.fully_grown[a.pos...] = false model.fully_grown[a.pos...] = false
model.growth[a.pos...] = 0
a.energy += model.Δenergy_grass a.energy += model.Δenergy_grass
end end
return return
@ -194,7 +195,7 @@ function initialize_model(;
model_properties = Dict( model_properties = Dict(
:events => events, :events => events,
:fully_grown => falses(dims), :fully_grown => falses(dims),
:countdown => zeros(Int, dims), :growth => zeros(Int, dims),
:regrowth_time => regrowth_time, :regrowth_time => regrowth_time,
:Δenergy_grass => Δenergy_grass, :Δenergy_grass => Δenergy_grass,
) )
@ -212,8 +213,8 @@ function initialize_model(;
## Add grass with random initial growth ## Add grass with random initial growth
for p in positions(model) for p in positions(model)
fully_grown = rand(abmrng(model), Bool) fully_grown = rand(abmrng(model), Bool)
countdown = fully_grown ? regrowth_time : rand(abmrng(model), 1:regrowth_time) - 1 growth = fully_grown ? regrowth_time : rand(abmrng(model), 1:regrowth_time) - 1
model.countdown[p...] = countdown model.growth[p...] = growth
model.fully_grown[p...] = fully_grown model.fully_grown[p...] = fully_grown
end end
return model return model
@ -245,7 +246,7 @@ function animal_step!(a::Animal, model)
end end
function model_step!(model) function model_step!(model)
event_handler!(model) handle_event!(model)
grass_step!(model) grass_step!(model)
end end
@ -264,25 +265,26 @@ function grass_step!(model)
end end
@inbounds for p in positions(model) # we don't have to enable bound checking @inbounds for p in positions(model) # we don't have to enable bound checking
if !(model.fully_grown[p...]) if !(model.fully_grown[p...])
if model.countdown[p...] 0 if model.growth[p...] model.regrowth_time#≤ 0
model.fully_grown[p...] = true model.fully_grown[p...] = true
model.countdown[p...] = model.regrowth_time #model.growth[p...] = model.regrowth_time
else else
model.countdown[p...] -= 1 model.growth[p...] += 1
end end
end end
end end
end end
# Check current step and start event at step t function handle_event!(model)
function event_handler!(model)
ids = collect(allids(model)) ids = collect(allids(model))
for event in model.events for event in model.events
if event.timer == event.t_start # start event if event.timer == event.t_start # start event
if event.name == "Drought" if event.name == "Drought"
model.regrowth_time = event.value #model.regrowth_time = event.value
for id in ids
abmproperties(model)[Symbol(model[id].def.type*"_"*"perception")] += 1 predators = filter(id -> !("Grass" model[id].def.food), ids)
for id in predators
abmproperties(model)[Symbol(model[id].def.type*"_"*"perception")] = 2
end end
elseif event.name == "Flood" elseif event.name == "Flood"
@ -291,6 +293,7 @@ function event_handler!(model)
abmproperties(model)[Symbol(model[id].def.type*"_"*"Δenergy")] -= 1 abmproperties(model)[Symbol(model[id].def.type*"_"*"Δenergy")] -= 1
end end
elseif event.name == "PreyReproduceSeasonal" elseif event.name == "PreyReproduceSeasonal"
prey = filter(id -> "Grass" model[id].def.food, ids) prey = filter(id -> "Grass" model[id].def.food, ids)
for id in prey for id in prey
@ -306,11 +309,37 @@ function event_handler!(model)
end end
end end
if (event.timer event.t_start) && (event.timer < event.t_end)
if event.name == "Drought"
for p in positions(model)
dry_out_chance = 0.4 * (model.growth[p...] / model.regrowth_time)
if model.fully_grown[p...] && (dry_out_chance rand(abmrng(model)))
#model.growth[p...] = 0
model.growth[p...] = rand(abmrng(model), 1:model.regrowth_time) - 1
model.fully_grown[p...] = false
end
end
elseif event.name == "Winter"
block_field_every = 2
i = 1
for p in positions(model)
if i % block_field_every == 0
model.growth[p...] = rand(abmrng(model), 1:(model.regrowth_time / 2))
model.fully_grown[p...] = false
end
i += 1
end
end
end
if event.timer == event.t_end # end event if event.timer == event.t_end # end event
if event.name == "Drought" if event.name == "Drought"
model.regrowth_time = event.pre_value #model.regrowth_time = event.pre_value
for id in ids predators = filter(id -> !("Grass" model[id].def.food), ids)
abmproperties(model)[Symbol(model[id].def.type*"_"*"perception")] -= 1 for id in predators
abmproperties(model)[Symbol(model[id].def.type*"_"*"perception")] = 1
end end
elseif event.name == "Flood" elseif event.name == "Flood"
@ -334,9 +363,9 @@ function event_handler!(model)
end end
end end
if event.timer == event.t_cycle # reset cycle if event.timer == event.t_cycle # reset timer
event.timer = 1 event.timer = 0
else else # continue timer
event.timer += 1 event.timer += 1
end end
end end

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 8,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -21,7 +21,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 81,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -42,7 +42,7 @@
"acolor(a) = a.def.color\n", "acolor(a) = a.def.color\n",
"\n", "\n",
"# and instruct [`abmplot`](@ref) how to plot grass as a heatmap:\n", "# and instruct [`abmplot`](@ref) how to plot grass as a heatmap:\n",
"grasscolor(model) = model.countdown ./ model.regrowth_time\n", "grasscolor(model) = model.growth ./ model.regrowth_time\n",
"# and finally define a colormap for the grass:\n", "# and finally define a colormap for the grass:\n",
"heatkwargs = (colormap = [:brown, :green], colorrange = (0, 1))\n", "heatkwargs = (colormap = [:brown, :green], colorrange = (0, 1))\n",
"\n", "\n",
@ -60,7 +60,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 105,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -68,11 +68,13 @@
"#Pkg.status([\"Agents\",\"GLMakie\"]; mode = Pkg.Types.PKGMODE_MANIFEST, io=stdout)\n", "#Pkg.status([\"Agents\",\"GLMakie\"]; mode = Pkg.Types.PKGMODE_MANIFEST, io=stdout)\n",
"using GLMakie\n", "using GLMakie\n",
"GLMakie.activate!()\n", "GLMakie.activate!()\n",
"events = RecurringEvent[]\n", "events = RecurringEvent[\n",
"#push!(events, RecurringEvent(\"Drought\", 80, 30, 30, 50, 120, 1))\n", "#RecurringEvent(\"Drought\", 30, 30, 30, 60, 120, 0)\n",
"#push!(events, RecurringEvent(\"Flood\", 50, 30, 70, 80, 120, 1))\n", "#RecurringEvent(\"Flood\", 50, 30, 70, 80, 120, 0)\n",
"#push!(events, RecurringEvent(\"PreyReproduceSeasonal\", 0.5, 0.1, 1, 4, 12, 1))\n", "RecurringEvent(\"Winter\", 0, 0, 18, 24, 24, 0)\n",
"#push!(events, RecurringEvent(\"PredatorReproduceSeasonal\", 0.1, 0.07, 4, 6, 12, 1))\n", "RecurringEvent(\"PreyReproduceSeasonal\", 0.5, 0.05, 1, 6, 24, 0)\n",
"#RecurringEvent(\"PredatorReproduceSeasonal\", 0.1, 0.07, 4, 6, 12, 0)\n",
"]\n",
"animal_defs = [\n", "animal_defs = [\n",
"AnimalDefinition(30,'●',RGBAf(1.0, 1.0, 1.0, 0.8),20, 0.3, 20, 3, \"Sheep\", [\"Wolf\",\"Bear\"], [\"Grass\"])\n", "AnimalDefinition(30,'●',RGBAf(1.0, 1.0, 1.0, 0.8),20, 0.3, 20, 3, \"Sheep\", [\"Wolf\",\"Bear\"], [\"Grass\"])\n",
"AnimalDefinition(3,'▲',RGBAf(0.2, 0.2, 0.3, 0.8),20, 0.07, 20, 1, \"Wolf\", [], [\"Sheep\"])\n", "AnimalDefinition(3,'▲',RGBAf(0.2, 0.2, 0.3, 0.8),20, 0.07, 20, 1, \"Wolf\", [], [\"Sheep\"])\n",
@ -110,17 +112,23 @@
" alabels = [\"Sheep\", \"Wolf\", \"Eaten\", \"Starved\"],\n", " alabels = [\"Sheep\", \"Wolf\", \"Eaten\", \"Starved\"],\n",
" mdata, mlabels = [\"Grass\"]\n", " mdata, mlabels = [\"Grass\"]\n",
")\n", ")\n",
"#, step! = (model) -> begin event_handler!(model, \"Dürre\") model.wolf_reproduce = 0.1 Agents.step!() end\n",
"#fig, ax, abmobs = abmplot(model; add_controls=true, plotkwargs...)\n", "#fig, ax, abmobs = abmplot(model; add_controls=true, plotkwargs...)\n",
"\n", "\n",
"fig\n", "fig\n",
"#run!(model, 2)" "#run!(model, 2)"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Julia 1.10.3", "display_name": "Julia 1.10.2",
"language": "julia", "language": "julia",
"name": "julia-1.10" "name": "julia-1.10"
}, },
@ -128,7 +136,7 @@
"file_extension": ".jl", "file_extension": ".jl",
"mimetype": "application/julia", "mimetype": "application/julia",
"name": "julia", "name": "julia",
"version": "1.10.3" "version": "1.10.2"
} }
}, },
"nbformat": 4, "nbformat": 4,