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

Added @shuffle! macro

parent ce8d15a3
No related branches found
No related tags found
No related merge requests found
...@@ -60,6 +60,7 @@ export ...@@ -60,6 +60,7 @@ export
@distancetoedge, @distancetoedge,
@countanimals, @countanimals,
@rand, @rand,
@shuffle!,
#functions #functions
simulate, simulate,
simulate!, simulate!,
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
## (https://github.com/CCTB-Ecomods/gemm/blob/master/src/input.jl) ## (https://github.com/CCTB-Ecomods/gemm/blob/master/src/input.jl)
""" """
The file that stores all default parameters. The file that stores all default parameters: `src/parameters.toml`
""" """
const PARAMFILE = joinpath(pkgdir(Persephone), "src/parameters.toml") const PARAMFILE = joinpath(pkgdir(Persephone), "src/parameters.toml")
## (DO NOT CHANGE THIS VALUE! Instead, specify simulation-specific configuration files ## (DO NOT CHANGE THIS VALUE! Instead, specify simulation-specific configuration files
...@@ -19,11 +19,11 @@ const PARAMFILE = joinpath(pkgdir(Persephone), "src/parameters.toml") ...@@ -19,11 +19,11 @@ const PARAMFILE = joinpath(pkgdir(Persephone), "src/parameters.toml")
Return a configuration parameter from the global settings. Return a configuration parameter from the global settings.
The argument should be in the form `<domain>.<parameter>`, The argument should be in the form `<domain>.<parameter>`,
for example `@param(core.outdir)`. Possible values for for example `@param(core.outdir)`. Possible values for
<domain> are `core`, `nature`, `farm`, or `crop`. For a full `<domain>` are `core`, `nature`, `farm`, or `crop`. For a full
list of parameters, see `src/parameters.toml`. list of parameters, see `src/parameters.toml`.
Note that this macro only works in a context where the `model` Note: this macro only works in a context where the `model`
object is available. object is available!
""" """
macro param(domainparam) macro param(domainparam)
domain = String(domainparam.args[1]) domain = String(domainparam.args[1])
...@@ -112,6 +112,28 @@ function flattenTOML(tomldict) ...@@ -112,6 +112,28 @@ function flattenTOML(tomldict)
flatdict flatdict
end end
"""
@rand(args...)
Return a random number or element from the sample, using the model RNG.
This is a utility wrapper that can only be used a context where the
`model` object is available.
"""
macro rand(args...)
:($(esc(:rand))($(esc(:model)).rng, $(map(esc, args)...)))
end
"""
@shuffle!(collection)
Shuffle the given collection in place, using the model RNG.
This is a utility wrapper that can only be used a context where the
`model` object is available.
"""
macro shuffle!(collection)
:($(esc(:shuffle!))($(esc(:model)).rng, $(esc(collection))))
end
""" """
parsecommandline() parsecommandline()
......
...@@ -375,17 +375,6 @@ macro countanimals(args...) ...@@ -375,17 +375,6 @@ macro countanimals(args...)
:(countanimals($(esc(:pos)), $(esc(:model)); $(map(esc, args)...))) :(countanimals($(esc(:pos)), $(esc(:model)); $(map(esc, args)...)))
end end
"""
@rand(args...)
Return a random number or element from the sample, using the model RNG.
This is a utility wrapper that can only be used nested within `@phase` or `@habitat`
(or in other contexts where the `model` object is available).
"""
macro rand(args...)
:($(esc(:rand))($(esc(:model)).rng, $(map(esc, args)...)))
end
##TODO @chance macro: @chance(0.5) => rand(model.rng) < 0.5 ##TODO @chance macro: @chance(0.5) => rand(model.rng) < 0.5
##TODO add movement macros ##TODO add movement macros
...@@ -40,15 +40,15 @@ function initpopulation(habitatdescriptor::Function; phase::Union{String,Nothing ...@@ -40,15 +40,15 @@ function initpopulation(habitatdescriptor::Function; phase::Union{String,Nothing
(!isnothing(phase)) && (species["phase"] = phase) (!isnothing(phase)) && (species["phase"] = phase)
width, height = size(model.landscape) width, height = size(model.landscape)
while n == 0 || n < popsize while n == 0 || n < popsize
for x in shuffle!(model.rng, Vector(1:width)) for x in @shuffle!(Vector(1:width))
for y in shuffle!(model.rng, Vector(1:height)) for y in @shuffle!(Vector(1:height))
if habitatdescriptor((x,y), model) if habitatdescriptor((x,y), model)
if pairs if pairs
add_agent!((x,y), Animal, model, deepcopy(species), female, 0) add_agent!((x,y), Animal, model, deepcopy(species), female, 0)
add_agent!((x,y), Animal, model, deepcopy(species), male, 0) add_agent!((x,y), Animal, model, deepcopy(species), male, 0)
n += 2 n += 2
else else
sex = asexual ? hermaphrodite : rand(model.rng, [male, female]) sex = asexual ? hermaphrodite : @rand([male, female])
add_agent!((x,y), Animal, model, deepcopy(species), sex, 0) add_agent!((x,y), Animal, model, deepcopy(species), sex, 0)
n += 1 n += 1
end end
...@@ -88,7 +88,7 @@ Produce one or more offspring for the given animal at its current location. ...@@ -88,7 +88,7 @@ Produce one or more offspring for the given animal at its current location.
""" """
function reproduce!(animal::Animal, model::AgentBasedModel, n::Int64=1) function reproduce!(animal::Animal, model::AgentBasedModel, n::Int64=1)
for i in 1:n for i in 1:n
sex = (animal.sex == hermaphrodite) ? hermaphrodite : rand(model.rng, [male, female]) sex = (animal.sex == hermaphrodite) ? hermaphrodite : @rand([male, female])
# We need to generate a fresh species dict here # We need to generate a fresh species dict here
species = @eval $(Symbol(animal.traits["name"]))($model) species = @eval $(Symbol(animal.traits["name"]))($model)
add_agent!(animal.pos, Animal, model, species, sex, 0) add_agent!(animal.pos, Animal, model, species, sex, 0)
...@@ -103,7 +103,7 @@ Kill this animal, optionally with a given percentage probability. ...@@ -103,7 +103,7 @@ Kill this animal, optionally with a given percentage probability.
Returns true if the animal dies, false if not. Returns true if the animal dies, false if not.
""" """
function kill!(animal::Animal, model::AgentBasedModel, probability::Float64=1.0, cause::String="") function kill!(animal::Animal, model::AgentBasedModel, probability::Float64=1.0, cause::String="")
if rand(model.rng) < probability if @rand() < probability
postfix = isempty(cause) ? "." : " from $cause." postfix = isempty(cause) ? "." : " from $cause."
@debug "$(animalid(animal)) has died$(postfix)" @debug "$(animalid(animal)) has died$(postfix)"
kill_agent!(animal, model) kill_agent!(animal, model)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment