Skip to content
Snippets Groups Projects
Commit b614eee8 authored by xo30xoqa's avatar xo30xoqa
Browse files

Added animal species traits

parent d8c33a04
No related branches found
No related tags found
No related merge requests found
...@@ -35,9 +35,10 @@ end ...@@ -35,9 +35,10 @@ end
Execute one update of the model. Execute one update of the model.
""" """
function stepsimulation!(model::AgentBasedModel) function stepsimulation!(model::AgentBasedModel)
@info "Simulating day $(model.date+Day(1))." @info "Simulating day $(model.date)."
for a in Schedulers.ByType((Farmer,Animal,CropPlot), true)(model) for a in Schedulers.ByType((Farmer,Animal,CropPlot), true)(model)
stepagent!(getindex(model, a), model) #The animal may have been killed, so we need a try/catch
try stepagent!(getindex(model, a), model) catch keyerror end
end end
savepopulationdata(model) savepopulationdata(model)
model.date += Day(1) model.date += Day(1)
......
...@@ -16,6 +16,7 @@ struct Species ...@@ -16,6 +16,7 @@ struct Species
name::String name::String
initpop!::Function # takes one argument: model initpop!::Function # takes one argument: model
update!::Function # takes two arguments: animal, model update!::Function # takes two arguments: animal, model
traits::Dict{String,Any} # a collection of species-specific traits
end end
""" """
...@@ -55,6 +56,27 @@ let specieslist = Dict{String, Species}() ...@@ -55,6 +56,27 @@ let specieslist = Dict{String, Species}()
end end
end end
"""
newspecies(properties...)
A utility function to create and register a new species.
All function arguments are passed on to Species().
"""
function newspecies(properties...)
registerspecies(Species(properties...))
end
"""
trait(animal, trait)
A utility function to return a trait value for this animal's species.
Returns nothing if the trait is not defined.
"""
function trait(animal::Animal, trait::String)
!(trait in keys(animal.species.traits)) && (return nothing)
return animal.species.traits[trait]
end
""" """
stepagent!(animal, model) stepagent!(animal, model)
......
...@@ -13,11 +13,11 @@ Initialise a population of Wolpertingers in random locations around the landscap ...@@ -13,11 +13,11 @@ Initialise a population of Wolpertingers in random locations around the landscap
function initwolpertinger!(model::AgentBasedModel) function initwolpertinger!(model::AgentBasedModel)
species = getspecies("Wolpertinger") species = getspecies("Wolpertinger")
x, y = size(model.landscape) x, y = size(model.landscape)
popsize = Int(round((x*y)/10000)) popsize = Int(round((x*y)*species.traits["popdensity"]))
for i in 1:popsize for i in 1:popsize
add_agent!(Animal, model, species, hermaphrodite, 0, 100) add_agent!(Animal, model, species, hermaphrodite, 0, species.traits["birthenergy"])
end end
@debug "Hid $(popsize) Wolpertingers for gullible tourists to find." @debug "Hid $(popsize) wolpertingers for gullible tourists to find."
end end
""" """
...@@ -29,13 +29,22 @@ and occasionally reproduce by spontaneous parthogenesis... ...@@ -29,13 +29,22 @@ and occasionally reproduce by spontaneous parthogenesis...
function updatewolpertinger!(w::Animal, model::AgentBasedModel) function updatewolpertinger!(w::Animal, model::AgentBasedModel)
# walk in a random direction # walk in a random direction
direction = Tuple(rand([-1,1], 2)) direction = Tuple(rand([-1,1], 2))
walk!(w, direction, model; ifempty=false) speed = rand([1, trait(w, "maxspeed")])
w.energy -= rand([1, 10])*5 for i in 1:speed
walk!(w, direction, model; ifempty=false)
end
w.energy -= speed
# reproduce every once in a blue moon # reproduce every once in a blue moon
if rand() < 0.01 if rand() < 0.05
@debug "Wolpertinger $(w.id) has reproduced." @debug "Wolpertinger $(w.id) has reproduced."
add_agent!(w.pos, Animal, model, getspecies("Wolpertinger"), hermaphrodite, 0, 100) add_agent!(w.pos, Animal, model, getspecies("Wolpertinger"),
hermaphrodite, 0, trait(w, "birthenergy"))
end end
end end
registerspecies(Species("Wolpertinger", initwolpertinger!, updatewolpertinger!)) newspecies("Wolpertinger",
initwolpertinger!,
updatewolpertinger!,
Dict("popdensity"=>1/10000,
"birthenergy"=>400,
"maxspeed"=>5))
...@@ -9,19 +9,19 @@ ...@@ -9,19 +9,19 @@
[core] [core]
configfile = "src/parameters.toml" # location of the configuration file configfile = "src/parameters.toml" # location of the configuration file
landcovermap = "data/landcover_jena.tif" # location of the landcover map landcovermap = "data/landcover_jena.tif" # location of the landcover map
farmfieldsmap = "data/fields_jena.tif" # location of the field geometry map TODO not the real file yet farmfieldsmap = "data/fields_jena.tif" # location of the field geometry map
outdir = "results" # location and name of the output folder outdir = "results" # location and name of the output folder
loglevel = "debug" # verbosity level: "debug", "info", "quiet" loglevel = "debug" # verbosity level: "debug", "info", "quiet"
seed = 0 # seed value for the RNG (0 -> random value) seed = 0 # seed value for the RNG (0 -> random value)
# dates to start and end the simulation # dates to start and end the simulation
startdate = 2020-01-01 startdate = 2020-01-01
enddate = 2020-01-05 enddate = 2020-01-31
[farm] [farm]
[nature] [nature]
targetspecies = ["Wolpertinger"] # list of target species to simulate targetspecies = ["Wolpertinger", "Wyvern"] # list of target species to simulate
[crop] [crop]
cropmodel = "linear" # crop growth model to use, "linear" or "aquacrop" (not yet implemented) cropmodel = "linear" # crop growth model to use, "linear" or "aquacrop" (not yet implemented)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment