Make turtles move to another node depending on their current node - netlogo

I have a network of nodes connected by links. Agents move randomly around this network, i.e they have an equal probability of going backwards and forwards. Here is my code to move the turtles to a random nearby node:
to start-movement
let nearest-node min-one-of nodes [distance myself]
set wlocation nearest-node
move-to wlocation
end
to move
ask walkers
[
set prevlocation wlocation
if any? [link-neighbors] of wlocation [
let new-location one-of [link-neighbors] of wlocation
move-to new-location
set wlocation new-location
]
]
end
When a turtle moves to a node, I would like to check where they came from, then with some probability move them to another node that is not the previous one. For example they move from node 1 to node 2. Node 3 is to the right, node 4 straight and node 5 to the right. I would like something like 'if turtle is on node 2, move to node 3 with p=x, node 4 with p=y, or node 5 with p=z.
Could I do this by accessing the who variable of wlocation and prevlocation?

I think you want something like this. It's unclear how you want to choose the probability values for selecting the next location, so I have done something that favours heading 'up'. To make it clear that nodes and walkers are different types of turtles, I have added explicit breed statements that I assume you have in your code. This is not tested or syntax checked.
The trick is to work out the possible next places to go BEFORE changing the value of prevlocation to the current location. Then you can exclude it from the agentset of potential next places.
breed [nodes node]
breed [walkers walker]
walkers-own [prevlocation wlocation]
to start-movement ; walker procedure
set wlocation min-one-of nodes [distance myself]
move-to wlocation
end
to move
ask walkers
[ let targets ([link-neighbors] of wlocation) with [self != [prevlocation] of myself]
set prevlocation wlocation
if any? targets
[ set wlocation ifelse-value random-float 1 < 0.3
[ max-one-of targets [y-cor] ]
[ one-of targets ]
move-to wlocation
]
]
end

Related

How to Check if the Two Turtles are on the Same Y Coordinate in NetLogo

I am working on a NetLogo project and I want my pupil turtles to move straight until they come to be on the same Y coordinate as their relevant link neighbour and from there move towards them (the link neighbour).
Note that each pupil has only one link neighbour.
This is the code that I have come up with,
to go
ask pupils [
let target one-of link-neighbors
ifelse [ycor] of myself != [ycor] of target
[
set heading 0
fd 1
]
[
face target
fd 1
]
]
tick
end
This does not work, the turtles keep moving straight. Can someone please help. I just want the turtles to get to their link neighbours, but there are walls that they must avoid.
Your issue is that ycor is a decimal value. So, For example turtle 1 may be on 3.2 and turtle 2 may be on 3.3.
Instead, I think you want to use turtles-here.
to go
ask pupils [
let target one-of link-neighbors
ifelse member? target turtles-here
[set heading 0]
[face target]
fd 1
]
tick
end
On a side note, how many link-neighbors does each target have? My concern is that let target one-of link-neighbors will reset the target each tick.

How could I set turtles heading to the destination by go along the road

Recently I have discovered the code that helps the turtles to walk along the road that I have assigned the color which is not black.
let target one-of patches in-cone 1.5 180 with [pcolor != black]
if target != nobody
[
face target
move-to target
]
But my problem is that
When the turtles meet the dead end, it stops right there and not moving anymore.
My turtles already have its own destination to go to (This model is another one). If I use the code that I have mentioned before, maybe it doesn't work. So how could I fix this?
Here is some code that I wrote (Another model).
ask turtles
[
set wlocation one-of patches with [pcolor = pink]
move-to wlocation
set myhome wlocation
]
After this, I will let turtles wander around in the model. Then in some specific time, turtles will go back to its home by the following code. When the turtles reach its home, it will stay still.
ifelse distance myhome > 2
[face myhome fd random 3]
[wander]
to wander
ifelse random 2 = 0 [rt random 30] [lt random 30]
end
Does anyone have any idea or suggestion?

Move agents in nodes

I am trying to move my agents from one vertex of my road network to another one which the following code. However, I got an error, saying that MOVE-TO expected input to be an agent but got NOBODY instead.
If new location is already defined which this code as part of the agentset of slocation, where is the problem?
to go
ask citizens [start-movement]
move
end
to start-movement
let nearest-node min-one-of nodes [distance myself]
set slocation nearest-node
move-to slocation
end
to move
ask citizens
[let new-location one-of [link-neighbors] of slocation
move-to new-location
set slocation new-location]
end
Here is a complete minimum working example copying your code and adding the setup and breed information required to make it run.
breed [ citizens citizen ]
citizens-own [slocation]
breed [ nodes node ]
to setup
clear-all
create-nodes 20 [ set color blue setxy random-xcor random-ycor ]
ask nodes [ create-links-with n-of 2 other nodes ]
create-citizens 1 [ set size 3 set color red ]
end
to go
ask citizens [start-movement]
move
end
to start-movement
let nearest-node min-one-of nodes [distance myself]
set slocation nearest-node
move-to slocation
end
to move
ask citizens
[ let new-location one-of [link-neighbors] of slocation
move-to new-location
set slocation new-location
]
end
This works fine. As I suggested in my comment, the most likely problem is that one of your citizens happened to start at a node without any link-neighbors. The way to check this is show count nodes with [not any? link-neighbors]. The error is saying that it couldn't find any agents in the set of link-neighbors.
If the nodes are only there to mark roads, then simply delete any that aren't marking roads. If there are other nodes as well, then you need to restrict your citizens to nodes that are road waypoints.

random walk in netlogo- stoping condition issue

I have created a small network consist of nodes which are connected through links.
Some of the nodes are sources and some of the targets. I try to implement Random Walk algorithm.
I place walkers on source nodes, and walkers are moving randomly through the network. Now, I want to check walker reached to targets nodes, if all target nodes are visited by walker then ask walker to stop or die.
I'm new to NetLogo and don't know how to implement this logic.
Any help or guide will be appreciated.
One way to do this is to have your nodes know if they are a target, and if they have been visited. That way, if a turtle visits a node, that node can be marked as having been visited. Then, you can have a stop procedure at the end of your go procedure that checks if any nodes are still present that are a target but have not been visited. I have made slight modifications to the Link-Walking Turtles Example to show one way that you could do this- almost all the code below is directly pulled from that model.
breed [nodes node]
breed [walkers walker]
walkers-own [location]
nodes-own [ target? visited? ]
to setup
clear-all
set-default-shape nodes "circle"
create-nodes 30 [
set color blue
set target? false
set visited? false
]
ask nodes [ create-link-with one-of other nodes ]
repeat 500 [ layout ]
ask nodes [
setxy 0.95 * xcor 0.95 * ycor
]
ask n-of 5 nodes [
set target? true
set color white
]
create-walkers 1 [
set color red
set location one-of nodes
move-to location
]
reset-ticks
end
to layout
layout-spring nodes links 0.5 2 1
end
to go
ask links [ set thickness 0 ]
ask walkers [
let new-location one-of [link-neighbors] of location
move-to new-location
set location new-location
;; This gets turtles to ask their current location
;; to set visited and target to true.
ask location [
set visited? true
if target? = true [
set color red
]
]
]
;; Check for target nodes that have NOT been visited.
;; If there aren't any, stop the model.
if not any? nodes with [ target? = true and visited? = false ] [
print ("All target nodes have been visited.")
stop
]
tick
end

NetLogo continue search under specific criteria

I'm simulating female animals dispersing from their mother's territory to search for their own territory. Essentially they need to find areas that are unoccupied by other female territories. Patches have a variable owner-fem that identifies which female it belongs to. Ideally, I'd like to have females:
move to a patch,
search within some radius around that patch for any other territory, and if there is another female's territory within that radius to
move to another patch to start the search process again. Below is what I have so far but I don't think I'm using the in-radius correctly.
I'm not sure what the best way is to tell the female to continue searching until the condition is met. Any help would be much appreciated.
to female-disperse
move-to one-of patches with [owner-fem = nobody]
if [owner-fem] of patches in-radius 3 != nobody
[
move-to one-of patches with [owner-fem = nobody]
]
end
If you want to it in "one shot", you could have them move directly to a suitable patch:
to female-disperse
move-to one-of patches with [
not any? patches in-radius 3 with [owner-fem != nobody]
]
end
Note that patches in-radius includes the patch that the turtle is on so there is no need for a separate move-to one-of patches with [owner-fem = nobody].
I don't know what your model requires, but if I were you, I might try to have them disperse a little more gradually. Here is another version that you could call from your go procedure (or any other procedure that runs "forever"):
to female-disperse
ask females with [owner-fem != self ] [
move-to one-of neighbors ; or however you want them to move
if not any? patches in-radius 3 with [owner-fem != nobody] [
set owner-fem self
]
]
end
In this version, all females that are not on a patch where they are the owner move to one of the neighboring patches. They then check if that new patch is suitable. If it is, they become the owner of it. If it is not, they just stop there for now: they will continue searching at the next iteration of go. You don't have to do it exactly this way; it could just be something loosely along those lines.