Netlogo, Moving Turtle along links - netlogo

I am doing model where I want my agents to move along links between nodes. The idea is that all turtles move a constant distance after each tick. (Note I dont want them jump from one node to the another, also lets assume that its not possible to make the nodes at a constant distances, because I am trying to implement it on a gis model where of course the vertices are not at constant distances)
For this model there are two type of agents butterflies and collectors. Each of the agents placed on nodes which are linked to each other. The collectors find the closest butterfly to them using in-radius and then finds the path using nw: turtles-on-weight-path function. So for example lets suppose collector-02 has located butterfly-19. The path is [node-2 node-5 node-1 node-9]. I want the collector to travel to these nodes to get to the butterfly but instead of jumping to each node. He has to travel a distance of 0.01 netlogo units on the link from node-2 to node-5 and eventually node-9 where the butterfly is located.
The function follow-path is incomplete and it where i getting into trouble.
ANy ideas how to solve this problem. Thanks
extensions [nw]
globals [current-loc-global butterfly-location link-distance]
breed [nodes node]
breed [collectors collector]
breed [butterflys butterfly]
links-own [dist]
nodes-own [consist-butterfly linked-to]
collectors-own [ distance-from-location current-loc new-location targeted-
butterfly initial-node next-node current-node previous-node
node-butterfly-at commute-path route-length next-pos]
butterflys-own [ initial-node-collectors targeted? node-butterfly-on]
to setup
clear-all
;creating nodes
create-nodes 60[
set color blue
set shape "circle"
set consist-butterfly nobody
]
ask nodes [create-link-with one-of other nodes]
repeat 500[layout]
ask nodes [setxy 0.95 * xcor * 0.95 ycor]
create-butterflys number-of-butterflys[
set node-butterfly-on one-of nodes
set shape "butterfly"
move-to node-butterfly-on
set initial-node-collectors current-loc-global
set targeted? false
]
ask nodes [
set consist-butterfly min-one-of butterflys in-radius 0.0001 [distance
myself]
]
create-collectors number-of-collectors[
set current-loc one-of nodes with [consist-butterfly = nobody]
set color red
set targeted-butterfly nobody
move-to current-loc
]
ask collectors[
set current-loc-global current-loc
]
ask links [
let end-point-1 end1
let end-point-2 end2
set link-distance 0
ask end1[set linked-to end-point-2
set link-distance distance end-point-2
]
set dist link-distance
ask end2[
set linked-to end-point-1
]
]
reset-ticks
end
to layout
layout-spring nodes links 0.5 1 1
end
to go
find-agents
find-path
follow-path
end
to find-agents
ask collectors [
if targeted-butterfly = nobody[set targeted-butterfly min-one-of
butterflys in-radius 10 with [not targeted?] [distance myself]]
if targeted-butterfly != nobody [ask targeted-butterfly [set targeted?
true]]
]
ask collectors with [targeted-butterfly != nobody][
ask targeted-butterfly [set butterfly-location node-butterfly-on]
set node-butterfly-at butterfly-location
]
end
to find-path
ask collectors with [targeted-butterfly != nobody ][
set initial-node min-one-of nodes in-radius 0.0001 [distance myself]
let end-location node-butterfly-at
let path []
ask initial-node [set path nw:turtles-on-weighted-path-to end-location
dist]
set commute-path path
set route-length length commute-path
set current-node initial-node
set previous-node current-node
set next-node current-node
]
end
to follow-path
ask collectors with [targeted-butterfly != nobody][
set distance-from-location distance current-node
ifelse 0.09 < distance-from-location
[jump 0.09]
[move-to current-node
set current-node next-node
face next-node]
]
end

Managed to sort it out. Need to change the follow-path function as follows:
to follow-path
ask collectors with [targeted-butterfly != nobody][
set distance-from-location distance current-node
set next-pos ((position(current-node) commute-path) + 1)
if next-pos < route-length [set next-node item(next-pos) commute-path]
ifelse 0.09 < distance-from-location
[jump 0.09]
[move-to current-node
set current-node next-node
face next-node]
]
end

Related

How to spawn turtles a certain amount of patches away from each other

I am trying to spawn turtles 5 patches away from each other but I'm not sure how, right now they all spawn on green patches (I don't want them to spawn on brown ones) and I'm not sure how exactly you control the distance between the spawning of turtles, thanks.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches [
set pcolor green
]
ask n-of 100 patches [
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown][sprout-humans 1 [set size 5
set color blue
set shape "person"]]
ask n-of 5 patches with [pcolor != brown][sprout-zombies 1 [set size 4
set color red
set shape "person"]]
end
Have you read this question: NetLogo Create turtle at regular distance from each other?
Anyway, I thought that showing you some working functions would be helpful, here I made two alternatives, sprout-distanced1 and sprout-distanced2, you can test them both by alternating which line is commented; I also added a slider called Min-Distance to control the turtles spacing.
sprout-distanced1 uses the keyword carefully with is basically a try-else block, it's there in case that the turtle doesn't find a patch distanced enough to move to, in which case rather than sending a warning the turtle will stay where it is and print its distance to the closest turtle.
sprout-distanced2 uses a while loop, in case that the turtle doesn't find a place to move to that is at least Min-Distance from another turtle it will reduce the minimum radius by a small amount until it can distance itself from other turtles, if it had to move to a patch where it is less than Min-Distance away from other turtles it will log the distance at the Command Center.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask n-of 100 patches
[
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown]
[
sprout-humans 1
[
set size 5
set color blue
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
ask n-of 5 patches with [pcolor != brown]
[
sprout-zombies 1
[
set size 4
set color red
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
end
to sprout-distanced1
carefully
[
; try to move at least Min-Distance away from other turtles
move-to one-of patches with [not any? other turtles in-radius Min-Distance]
]
[
; if can't move Min-Distance away from other turtles
; stay put and log the min distance to other turtle, just for reference
show distance min-one-of other turtles [distance myself]
setxy random-xcor random-ycor
sprout-distanced1
]
end
to sprout-distanced2
let min-dist Min-Distance
let moved? FALSE
while [not moved? and min-dist > 0]
[
; can distance it self somewhere?
ifelse any? patches with [not any? other turtles in-radius min-dist]
[
; if yes, go there
move-to one-of patches with [not any? other turtles in-radius min-dist]
set moved? TRUE
; if had to reduce the distancing radious log it
if moved? and min-dist < Min-Distance
[
show distance min-one-of other turtles [distance myself]
]
]
[
; no where to go, reduce the distancing radious
set min-dist min-dist - 0.1
]
]
end
Choose whichever suits better your model.

Shortest path between nodes in netlogo using nw extension

I want to find the shortest path between two nodes - slocation and new-location - in a network of links
I have several slocation and new-location. One slocation for each citizen with some age, sex, and other characteristics, and one new-location for each node close to a work building.
I could identify these nodes with colors - see image.
However, I could not identify the shortest path between each slocation (for each citizen) and newlocation.
I got the following error: BUT-FIRST expected input to be a string or list but got the TRUE/FALSE false instead.
when I run these lines of code:
ask slocation
[set path but-first nw:turtles-on-weighted-path-to new-location weight]
Is this because I have several slocation and new-location?
should I introduce and foreach variable to calculate the shortest path for each citizen? I tried already only with one citizen and still this part of the code is not capable to find the path
I think the error is that the nodes slocation and new-location are not connected with a single link. The citizens have to select the shortest path created by links connecting these nodes to reach the new-location. Someone knows how to do this?
Here, it is my other part of the code:
breed [citizens citizen]
breed [nodes node]
links-own [weight]
to go
set-timekeeper
ask citizens
[find-day-activities]
end
to set-timekeeper
tick
if (counter = 2) [set timekeeper 2] ;From 7:00 AM to 9:00 AM
end
to find-day-activities
if (timekeeper = 2) [Do_7AM_9AM]
end
to Do_7AM_9AM
if (sex = 0 and age = 1 and employment = 0 and household-size = 0)
[shortest-distance]
end
to shortest-distance
let slocation min-one-of nodes [distance myself]
ask slocation [set color yellow]
let new-location one-of nodes with-min [distance one-of workbuildings]
ask new-location [set color blue]
ask links [set weight link-length]
let path nobody
ask slocation
[set path but-first nw:turtles-on-weighted-path-to new-location weight]
ask turtle-set path [ set color yellow ]
end
If there is any question about how I create the road network from a shapefile, this is the code:
to make-road-network
clear-links
let first-node nobody
let previous-node nobody
foreach gis:feature-list-of roads [ polyline ->
foreach gis:vertex-lists-of polyline [ segment ->
foreach segment [ coordinate ->
let location gis:location-of coordinate
if not empty? location [
create-nodes 1 [
set color green
set size 1
set xcor item 0 location
set ycor item 1 location
if first-node = nobody [
set first-node self
]
if previous-node != nobody [
create-link-with previous-node
]
set previous-node self
]
]
]
set previous-node nobody
]
]
ask links [set color red
set thickness 1]
end

How to ask turtles to place in a desired area, on Netlogo?

I want to create turtles, which they place in a desired area with random coordinate:
they should place in the white area and in middle of it in a line. in other words, in the top regtangle, their xcor should be random and their ycor is 10. in the right regtangle, their ycor should be random and their xcor is 10 and so on.
When you create turtles, you can give them instructions such as their location. For example:
create-turtles 1 [ set ycor 10 ]
Alternatively, you can sprout the turtles from the relevant patches and their location will already be set. For example:
ask n-of 5 patches with [pcolor > 1] [ sprout 1 ]
to place-on-color [#color]
let _patches (patches with [pcolor = #color])
ask turtles [
move-to one-of (_patches with [not any? turtles-here])
]
end
Add error checking if you may have too many turtles. (Or remove the unique occupancy constraint if you don't want it.)

How to avoid turtles revisiting the patch on which they were last time?

Turtles stay on patches for 60 ticks, and then move to another target patch. How to avoid turtles revisiting the patch on which they were last time? Thanks
Hi Seth and Frank,
Thank you very much for your reply. I am sorry I did not describe the questions in detail.
Turtles will not visit the patch that they were on the last tick, and will move to another nearest patch instead in next tick. The following codes mean they find the nearest patch, and move on to it.
What I would want to do is the turtle will find the nearest patch again in the next tick. They will move to other alternative that is nearest to themselves, if the nearest patch is still the same one that they were on the last tick. Thanks
let closest-leaf min-one-of (patches in-radius 1 with [pcolor = lime]) [distance myself]
face closest-leaf
fd distance closest-leaf
A good way is to have a turtles-own variable of patches visited that can be maintained (remember to initialize it to the empty list when you create the turtle).
turtles-own [ patches-visited ]
to setup
...
ask turtles [ set patches-visited [] ]
...
end
to move
let potential-targets filter [ not member? ? patches-visited ] target-patches
let target-patch one-of potential-targets
if target-patch != NOBODY [
set patches-visited fput target-patch patches-visited
; move to target patch
]
end
Add a get-target reporter.
to-report get-target ;;turtle proc
let %close patches in-radius 1 with [pcolor = lime and self != [patch-here] of myself]
ifelse (any? %close) [
report min-one-of %close [distance myself]
] [
report nobody
]
end
Now you can easily change your mind about how to pick a target. You can then move to the target (if one exists).
to move ;; turtle proc
let %target get-target
ifelse (%target = nobody) [
;handle this case
] [
face %target move-to %target
]
end

On netlogo, what command do I use to make a turtle stop if the patch it wants to move to is a certain color

I'm making a maze on netlogo and I want to do it so that once it tries to walk into the violet lines, it'll stay on its own patch instead of moving forward. What command would that be? I tried bk 1 to reverse the fd 1 but it doesn't work all the time
You can undo your step like this:
ask turtles [
fd 1
if pcolor = violet [fd -1]
]
Or you can check ahead of time as Marzy answered. Basically it's the difference of asking for forgiveness vs permission :-)
I hope this example answer your questions:
turtles-own [target]
to setup
clear-all
reset-ticks
ask n-of 100 patches [
set pcolor red
]
create-turtles 1
[ move-to one-of patches with [pcolor != red]
set heading 90
set target one-of patches with [pcolor != red]
ask target
[
set pcolor green
]
]
end
to go
ask turtles
[ifelse pcolor != green
[
ifelse [pcolor] of patch-ahead 1 != red
[
Your-Move-Function
]
[
Your-Bounce-Function
]
leave-a-trail
]
[stop
print ticks
]
]
tick
end
to Your-Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Your-Bounce-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != red and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
to leave-a-trail
ask patch-here [set plabel ticks]
end
This is how it works:
Random patches are colored Red to show walls or obstacles, one turtle is created in a random location with a random target which is colored green:
I have used a variable to store all available patches which turtle can step on , but since I have considered a target for the turtle, turtle chooses the one patch which is closest to the target, and since I have noticed in some cases it might go in circle I have asked the turtle to leave tick number which is its move number as a plabel, you can use a variable for that for specifying if that path was already selected or not.