Hi,
I am trying to create a model where the agents should set up territory on patches they have occupied and avoid patches that have been occupied by other agents.
To do this I decided to assign each patch the “who” of the agent that occupied it and then have the agents check if the patch has not been occupied previously. Assigning patches to agents seem to work, but recognising occupied patches does not.
When I ask the agents to print if the patch they are facing is safe or not, they often report it being usafe even if it has not previously been occupied or is within their own territory. When I run the code with only one agent I do not get false reports of patches being unsafe. I think the issue might be in the " [ifelse ([territory] of patch-ahead 1 = [who] of myself) or ([territory] of patch-ahead 1 = nobody)" statement but I cannot see what it could be. I would greatly apprechiate any feedback on what the issue might be!
There is also an issue that agents sometimes enter patches not within their own territory. I have setup the code so that agents should not move when the patch-ahead is “unsafe”, only turn, but they still enter them. Does it have anything to do with the angle the agent is facing?
Code:
to setup
ca ;clear environment
setup-habitat
setup-mammal
reset-ticks
end ;end of setup procedure
;
;
;
;PATCH SETUP
to setup-habitat
ask patches [
set pcolor black
set territory nobody ;all patches start as unclaimed territory
]
end
;
;
; AGENT SETUP
to setup-mammal
create-mammals n.mammals
[ set size 1
setxy random-pxcor random-pycor
set label who ;track individuals
]
ask mammals [
mark-territory
]
end ;of mammal setup
;
;
; GO PROCEDURES
to go
ask mammals [
set at-edge false ;reset edge checker
check-territory
move ;forage or travel
]
tick
if ticks >= 100 [stop]
end
to check-territory
ask mammals [
ifelse patch-ahead 1 = nobody ;check if at edge of world
[
show(“at edge - turn”) ;defensive code
set at-edge true ;set patch ahead to unsafe, for using in move procedure
]
[ifelse ([territory] of patch-ahead 1 = [who] of myself) or ([territory] of patch-ahead 1 = nobody) ;check if patch is part of own territory or not part of a territory yet
[
set patch-safe true ;patch is safe if this is true
show (“patch safe”) ;defensive code
]
[set patch-safe false ;patch is unsafe if this is false
show (“too close to other territory - patch unsafe”) ;defensive code
]
]
]
end
to move
pd
ask mammals [
ifelse at-edge = true
[right 90 fd 0 ;turn away from edge
]
[ifelse patch-safe = true ;check if patch ahead is safe to move to
[
fd 1 ;move to patch
mark-territory ;mark patch as territory if unclaimed
right random-normal 0 120 ;change turning angle
]
[right 90 fd 0] ;if patch is unsafe turn and check if new patch is safe
]
]
end
to mark-territory
ask mammals [
if [territory] of patch-here = nobody [ ;check if patch has not been claimed
ask patch-here [set territory [who] of myself] ;set territory to own ID
]
]
end