Reputation: 15
I'm trying to work out how to implement agent navigation. I have set up my model to sprout nodes on every "floor" patch in my evacuation model. i have also linked the turtles to respective nodes. What i'm trying to do is have evacuees plot a path towards the exit and execute evacuation simultaneously. The png image i have used is a basic floorplan with a white floor, and black walls/obstacles, i figured by sprouting nodes on the white floor the agents will automatically navigate around said obstacles. I understand this is possible using nw extension however i have failed in incorporating it into my model. I have looked at the link walking turtle example and still can't figure this out. Has anyone got any advice as to how this can be done based on the code below? or any advice on how to incorporate path-finding in general? Any help would be much appreciated.
breed [nodes node]
breed [cells cell]
breed [evacuees evacuee]
evacuees-own
[ target
direction
]
to setup
clear-all
set-default-shape turtles "person";
import-pcolors "floorplan.png"
ask n-of evacnum patches with [pcolor = white]
[
sprout-evacuees 1 [
set size 7
set color green
;set speed 1 + random-float 1.5
ifelse random 2 = 0
[ set direction 1
]
[ set direction -1
]
]
]
ask patches with [ pcolor = white ] [
sprout-nodes 1 [
set size 0.5
set shape "circle"
set color grey
]
]
ask nodes [
create-links-with turtles-on neighbors4 [
set color grey
]
]
ask patch 146 199
[
sprout-cells 1 [
set size 1.5
set shape "box"
set color yellow
]
]
ask evacuees
[ set target one-of min-n-of 5 nodes [distance myself]
face target
]
reset-ticks
end
to go
move
tick
end
to move
ask evacuees
[
walk
forward 0.25
if distance target < 0.2
[ set target one-of min-n-of 5 nodes [distance myself]
]
face target
]
end
to walk
if not wall? (90 * direction) and wall? (135 * direction) [ rt 90 *
direction ]
while [wall? 0] [ lt 90 * direction ]
end
to-report wall? [angle]
report black = [pcolor] of patch-right-and-ahead angle 1
end
Upvotes: 0
Views: 202
Reputation: 10291
This answer is a simplified version of my answer here, so for more detail or a more flexible implementation maybe have a look at that. This simple approach gets the evacuees to check which of the nodes on neighboring patches are closest to the exit-node
, then they move to that node. With a simple setup based on your code above (but without the floorplan, I don't see that posted):
extensions [ nw ]
breed [nodes node]
breed [cells cell]
breed [evacuees evacuee]
globals [ exit-node ]
to setup
clear-all
set-default-shape turtles "person";
ask patches [ set pcolor white ]
ask patches with [ pxcor mod 5 = 0 ] [
set pcolor black
]
ask patches with [ pycor mod 10 = 0 ] [
set pcolor white
]
ask n-of 5 patches with [pcolor = white] [
sprout-evacuees 1 [
set size 2
set color green
]
]
ask patches with [ pcolor = white ] [
sprout-nodes 1 [
set size 0.5
set shape "circle"
set color grey
]
]
ask nodes [
create-links-with nodes-on neighbors4 [
set color grey
]
]
ask patch 16 0 [
sprout-cells 1 [
set size 1.5
set shape "box"
set color yellow
]
]
ask one-of cells [
set exit-node one-of nodes-here
]
reset-ticks
end
Now, some simple movement code:
to go
ask evacuees [
let target min-one-of nodes-on neighbors4 [
length nw:turtles-on-path-to exit-node
]
move-to target
if any? cells-here [
show "I HAVE EVACUATED!"
die
]
]
tick
end
To get behavior kind of like:
Edit:
Change globals to globals [ exits ]
and replace this:
ask patch 16 0 [
sprout-cells 1 [
set size 1.5
set shape "box"
set color yellow
]
]
ask one-of cells [
set exit-node one-of nodes-here
]
With this:
set exits nodes-on ( patch-set patch 16 0 patch -16 0 )
ask exits [
hatch-cells 1 [
set size 1.5
set shape "box"
set color yellow
]
]
And you should be able to use the modified go
below to have evacuees seek the nearest door:
to go
ask evacuees [
let my-node one-of nodes-on patch-here
let my-exit min-one-of exits [
length nw:turtles-on-path-to my-node
]
let target min-one-of nodes-on neighbors4 [
length nw:turtles-on-path-to my-exit
]
move-to target
if any? cells-here [
show "I HAVE EVACUATED!"
die
]
]
tick
end
Upvotes: 1