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

Bug fixes in the skylark model

parent b3df632b
No related branches found
No related tags found
No related merge requests found
...@@ -76,14 +76,18 @@ end ...@@ -76,14 +76,18 @@ end
""" """
occupy!(animal, model, position) occupy!(animal, model, position)
Add the given location to the animal's territory. Add the given location to the animal's territory. Returns `true` if successful
(i.e. if the location was not already occupied by a conspecific), `false` if not.
""" """
function occupy!(animal::Animal, model::SimulationModel, position::Tuple{Int64,Int64}) function occupy!(animal::Animal, model::SimulationModel, position::Tuple{Int64,Int64})
if isoccupied(model, speciesof(animal), position) #XXX should this be an error? if isoccupied(model, speciesof(animal), position) #XXX should this be an error?
@warn "Position $position is already occupied by a $(speciesof(animal))." animal @warn "Position $position is already occupied by a $(speciesof(animal))." animal
end return false
else
push!(animal.territory, position) push!(animal.territory, position)
push!(model.landscape[position...].territories, animal.id) push!(model.landscape[position...].territories, animalid(animal))
return true
end
end end
""" """
......
...@@ -146,7 +146,7 @@ Test whether this location is part of the territory of an animal of the given sp ...@@ -146,7 +146,7 @@ Test whether this location is part of the territory of an animal of the given sp
""" """
function isoccupied(model::SimulationModel, species::String, position::Tuple{Int64,Int64}) function isoccupied(model::SimulationModel, species::String, position::Tuple{Int64,Int64})
for id in model.landscape[position...].territories for id in model.landscape[position...].territories
(speciesof(model.animals[id]) == species) && return true occursin(species, id) && return true
end end
return false return false
end end
......
...@@ -77,6 +77,7 @@ ...@@ -77,6 +77,7 @@
mate::Int64 = -1 # the agent ID of the mate (-1 if none) mate::Int64 = -1 # the agent ID of the mate (-1 if none)
nest::Tuple = () # coordinates of current nest nest::Tuple = () # coordinates of current nest
clutch::Int64 = 0 # number and life stage of offspring in current clutch clutch::Int64 = 0 # number and life stage of offspring in current clutch
#following::Int64 = -1 # ID of the individual being followed in the non-breeding phase
end end
...@@ -140,8 +141,7 @@ adjusting it to new conditions when and as necessary. ...@@ -140,8 +141,7 @@ adjusting it to new conditions when and as necessary.
@move(@rand(self.territory)) @move(@rand(self.territory))
if model.date > self.nestingend if model.date > self.nestingend
# once the breeding season is over and all the young have left the nest, stop breeding # once the breeding season is over and all the young have left the nest, stop breeding
#FIXME under which conditions would @animal(self.mate) == nothing? if self.mate == -1 || isnothing(@animal(self.mate)) || @animal(self.mate).clutch == 0
if self.mate == -1 || !isnothing(@animal(self.mate)) || @animal(self.mate).clutch == 0
@setphase(nonbreeding) @setphase(nonbreeding)
end end
end end
...@@ -162,8 +162,8 @@ Females returning from migration move around to look for a suitable partner with ...@@ -162,8 +162,8 @@ Females returning from migration move around to look for a suitable partner with
@setphase(nesting) @setphase(nesting)
return return
else else
self.mate = -1
@animal(self.mate).mate = -1 @animal(self.mate).mate = -1
self.mate = -1
end end
else else
self.mate = -1 self.mate = -1
...@@ -279,7 +279,7 @@ If it is, return the list of coordinates that make up the new territory, else re ...@@ -279,7 +279,7 @@ If it is, return the list of coordinates that make up the new territory, else re
function findterritory(skylark::Skylark, model::SimulationModel) function findterritory(skylark::Skylark, model::SimulationModel)
effectivesize::Area = 0 # the usable size of the territory, weighted by habitat quality effectivesize::Area = 0 # the usable size of the territory, weighted by habitat quality
territory::Vector{Tuple{Int64,Int64}} = [] territory::Vector{Tuple{Int64,Int64}} = []
msize = size(model.landscape) width, height = size(model.landscape)
radius = 0 radius = 0
constrained = false constrained = false
# Inspect the landscape in concentric circles around the individual until enough pixels have # Inspect the landscape in concentric circles around the individual until enough pixels have
...@@ -304,7 +304,7 @@ function findterritory(skylark::Skylark, model::SimulationModel) ...@@ -304,7 +304,7 @@ function findterritory(skylark::Skylark, model::SimulationModel)
end end
#FIXME some duplicates remain? #FIXME some duplicates remain?
for c in coords # ...then inspect them for c in coords # ...then inspect them
(c[1] <= 0 || c[2] <= 0 || c[1] > msize[1] || c[2] > msize[2]) && continue (c[1] <= 0 || c[2] <= 0 || c[1] > width || c[2] > height) && continue
(isoccupied(model, "Skylark", c)) && continue (isoccupied(model, "Skylark", c)) && continue
push!(territory, c) push!(territory, c)
quality = foragequality(skylark, model, c) quality = foragequality(skylark, model, c)
...@@ -347,10 +347,9 @@ function allowsnesting(skylark::Skylark, model::SimulationModel, pos::Tuple{Int6 ...@@ -347,10 +347,9 @@ function allowsnesting(skylark::Skylark, model::SimulationModel, pos::Tuple{Int6
(@landcover() == grass || (@landcover() == grass ||
(@landcover() == agriculture && (@landcover() == agriculture &&
(skylark.nestingheight[1] <= @cropheight() <= skylark.nestingheight[2]) && (skylark.nestingheight[1] <= @cropheight() <= skylark.nestingheight[2]) &&
(skylark.nestingcover[1] <= @cropcover() <= skylark.nestingcover[2]))) #&& (skylark.nestingcover[1] <= @cropcover() <= skylark.nestingcover[2])) &&
#FIXME if we add the distance requirement, females don't find a nesting spot? (@distanceto(forest) < skylark.mindistancetoedge) &&
#(@distanceto(forest) < skylark.mindistancetoedge) && (@distanceto(builtup) < skylark.mindistancetoedge))
#(@distanceto(builtup) < skylark.mindistancetoedge)
end end
""" """
......
...@@ -22,7 +22,7 @@ mutable struct Pixel ...@@ -22,7 +22,7 @@ mutable struct Pixel
fieldid::Union{Missing,Int64} # ID of the farmplot (if any) at this position fieldid::Union{Missing,Int64} # ID of the farmplot (if any) at this position
events::Vector{Management} # management events that have been applied to this pixel events::Vector{Management} # management events that have been applied to this pixel
animals::Vector{Int64} # IDs of animals currently at this position animals::Vector{Int64} # IDs of animals currently at this position
territories::Vector{Int64} # IDs of animals that claim this pixel as part of their territory territories::Vector{String} # IDs of animals that claim this pixel as part of their territory
end end
Pixel(landcover::LandCover, fieldid::Union{Missing,Int64}) = Pixel(landcover::LandCover, fieldid::Union{Missing,Int64}) =
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment