Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Persefone.jl
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Persefone
Persefone.jl
Commits
7c8b7287
Commit
7c8b7287
authored
Feb 3, 2023
by
xo30xoqa
Browse files
Options
Downloads
Patches
Plain Diff
Added @shuffle! macro
parent
ce8d15a3
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/Persephone.jl
+1
-0
1 addition, 0 deletions
src/Persephone.jl
src/core/input.jl
+26
-4
26 additions, 4 deletions
src/core/input.jl
src/nature/nature.jl
+0
-11
0 additions, 11 deletions
src/nature/nature.jl
src/nature/populations.jl
+5
-5
5 additions, 5 deletions
src/nature/populations.jl
with
32 additions
and
20 deletions
src/Persephone.jl
+
1
−
0
View file @
7c8b7287
...
@@ -60,6 +60,7 @@ export
...
@@ -60,6 +60,7 @@ export
@distancetoedge
,
@distancetoedge
,
@countanimals
,
@countanimals
,
@rand
,
@rand
,
@shuffle
!
,
#functions
#functions
simulate
,
simulate
,
simulate!
,
simulate!
,
...
...
This diff is collapsed.
Click to expand it.
src/core/input.jl
+
26
−
4
View file @
7c8b7287
...
@@ -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()
...
...
This diff is collapsed.
Click to expand it.
src/nature/nature.jl
+
0
−
11
View file @
7c8b7287
...
@@ -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
This diff is collapsed.
Click to expand it.
src/nature/populations.jl
+
5
−
5
View file @
7c8b7287
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment