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

Wrote the `initpopulation()` function

Still needs to be tested.
parent 1647b8d1
No related branches found
No related tags found
No related merge requests found
......@@ -42,7 +42,7 @@ function setupdatadir()
simulationlogger = TeeLogger(ConsoleLogger(logfile, loglevel),
ConsoleLogger(stdout, loglevel))
global_logger(simulationlogger)
@info "Setting up output directory $(param("core.outdir"))"
@debug "Setting up output directory $(param("core.outdir"))"
# Export a copy of the current parameter settings to the output folder.
# This can be used to replicate this exact run in future, and also
# records the current time and git commit.
......
......@@ -10,7 +10,8 @@ Initialise the model: read in parameters, create the output data directory,
and instantiate the AgentBasedModel object.
"""
function initialise(config::String=PARAMFILE)
#XXX add a seed parameter?
@info "Simulation run started at $(Dates.now())."
#TODO add a seed parameter - requires mutable parameters
# do some housekeeping
initsettings(config)
Random.seed!(param("core.seed"))
......@@ -29,7 +30,6 @@ function initialise(config::String=PARAMFILE)
initfarms!(model)
initfields!(model)
initnature!(model)
@info "Simulation initialised at $(Dates.now())."
model
end
......@@ -52,11 +52,11 @@ end
"""
finalise(model)
Wrap up the simulation. Output all remaining data and exit.
Wrap up the simulation. Currently doesn't do anything except print some information.
"""
function finalise(model::AgentBasedModel)
@info "Simulated $(model.date-param("core.startdate"))."
@info "Simulation completed at $(Dates.now()),\nwrote output to $(param("core.outdir"))."
@info "Simulated $(model.date-param("core.startdate")) days."
@info "Simulation run completed at $(Dates.now()),\nwrote output to $(param("core.outdir"))."
#XXX is there anything to do here?
#genocide!(model)
end
......
......@@ -59,7 +59,7 @@ function initfields!(model::AgentBasedModel)
end
end
end
@debug "Initialised $n farm plots."
@info "Initialised $n farm plots."
end
#XXX only needed during development, can be deleted again?
......
......@@ -304,12 +304,11 @@ macro distancetoedge()
end
"""
@countanimals(speciesname)
@countanimals(speciesname, radius=0)
Count the number of animals of the given species in this location.
This is a utility wrapper that can only be used nested within `@phase` or `@habitat`.
"""
macro countanimals(speciesname)
#XXX this also counts the enquiring agent
:(countanimals($(esc(:pos)), $(esc(:model)), $speciesname))
macro countanimals(speciesname, radius=0)
:(countanimals($(esc(:pos)), $(esc(:model)), $speciesname, $radius))
end
......@@ -4,6 +4,63 @@
### reproduction, and mortality.
###
"""
initpopulation(habitatdescriptor; popsize=-1, pairs=false, asexual=false)
Creates a function that initialises individuals at random locations across the landscape.
This can be used to create the `initialise!` variable in a species definition block.
- `habitatdescriptor` is a function that determines whether a given location is suitable
or not (create this using `@habitat`).
- `popsize` determines the number of individuals that will be created. If this is zero or
negative, one individual will be created in every suitable location in the landscape.
If `popsize` is greater than the number of suitable locations, multiple individuals
will be created in one place. (Maximum population density can be set in the habitat
descriptor using the `@countanimals` macro.)
- If `pairs` is true, a male and a female individual will be created in each selected
location, otherwise, only one individual will be created at a time.
- If `asexual` is true, all created individuals are assigned the sex `hermaphrodite`,
otherwise, they are randomly assigned male of female. (If `pairs` is true, `asexual`
is ignored.)
"""
function initpopulation(habitatdescriptor::Function;
popsize::Int64=-1, pairs::Bool=false, asexual=false)
function(species::Dict{String,Any}, model::AgentBasedModel)
n = 0
lastn = 0
specname = species["name"]
width, height = size(model.landscape)
while n == 0 || n < popsize
for x in shuffle!(Vector(1:width))
for y in shuffle!(Vector(1:height))
if habitatdescriptor((x,y), model)
if pairs
add_agent!(Animal, (x,y), model, species, female, 0)
add_agent!(Animal, (x,y), model, species, male, 0)
n += 2
else
sex = asexual ? hermaphrodite : rand([male, female])
add_agent!(Animal, (x,y), model, species, sex, 0)
n += 1
end
end
(n >= popsize) && break
end
(n >= popsize) && break
end
if lastn == n # prevent an infinite loop - we don't have a Cray...
@warn "There are not enough suitable locations for $(specname) in the landscape."
break
end
lastn = n
end
@info "Initialised $(n) $(specname)s."
end
end
"""
initrandompopulation(popsize, asexual=true)
......@@ -12,7 +69,7 @@ A simplified version of `initpopulation()`. Creates a function that initialises
than 1, it is interpreted as a population density (i.e. 1 animal per `popsize` pixels).
"""
function initrandompopulation(popsize::Union{Int64,Float64}, asexual::Bool=true)
initfunc = function(species::Dict{String,Any}, model::AgentBasedModel)
function(species::Dict{String,Any}, model::AgentBasedModel)
if popsize < 1.0
x, y = size(model.landscape)
popsize = Int(round(x*y*popsize))
......@@ -21,12 +78,10 @@ function initrandompopulation(popsize::Union{Int64,Float64}, asexual::Bool=true)
sex = asexual ? hermaphrodite : rand([male, female])
add_agent!(Animal, model, species, sex, 0)
end
@debug "Initialised $(popsize) $(species["name"])s."
@info "Initialised $(popsize) $(species["name"])s."
end
return initfunc
end
#TODO initpopulation with habitat descriptor
#XXX initpopulation with dispersal from an original source?
#XXX initpopulation based on known occurences in real-life?
......
......@@ -35,7 +35,7 @@ At the moment, this implementation is still in development.
@respond harvest @kill(@trait(eggharvestmortality), "harvest")
if animal.age == 14
if @trait(age) == 14
@trait(phase) = "nestling"
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment