Move agents in nodes - netlogo

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.

Related

Make turtles move to another node depending on their current node

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

How can I move a turtle as close as possible to a certain patch?

I have a single blue patch and would like to move a turtle to the closest, empty patch to it. The only way I can think of doing this is using in-radius in a loop, increasing the radius size by one each time, but is there a better way?
globals [bluey]
to setup
ca
ask one-of patches [set pcolor blue set bluey self]
ask n-of 250 patches [sprout 1]
end
to-report nearest-empty [#patch]
report min-one-of
[other (patches with [not any? turtles-here])] of #patch
[distance #patch]
end
to test
setup
;the following fails if all patches occupied
;(can add a test for nobody)
ask nearest-empty bluey [set pcolor red]
end

Turtle Behavior after asking for Patch Evaluation

I'm creating a fish-tank style simulation in NetLogo. There are "prey", "predators", and "hidingspots".
The idea is that when a predator appears on the map, the prey will individually run the "hide" behavior and head to the nearest "hidingspot" - provided there are no predators between it and the "hidingspot".
to move-turtles
ask prey [
if (any? predators)
[
hide
stop
]
The relevant code to run the hide command.
to hide
face min-one-of hidingspot [distance myself]
set d distance min-one-of hidingspot [distance myself]
ask patches in-cone d 80
[ set pcolor yellow
if (any? predators-here)
[ ask prey
[ forward 1
set color red
output-print "DANGER"]]]
forward 1
end
The problem is that I do not know how to use the if statement in the "ask patches" properly. And therefore when one prey spots a threat all prey are running the else part of the statement rather than evaluating it individually.
How would I fix this?
Any help is appreciated.
You need to separate what you are asking the prey to do from what you are asking the patches to do. As King-Ink said, you are asking the patches to ask all the prey to do things.
The easiest way is to create a patchset for the 'danger' patches and then check if there's a predator on those patches. To do this, you want something like the following (note that this is a complete model, so you can copy this whole code into a new model and run it).
A couple of other things in your code that I cleaned up. I used let for the local variable d so that it doesn't have to appear in your globals. I asked for min-one-of only once and reused because otherwise a different hidingspot could be selected each time (if multiple at same distance). While this would not have caused an error this time (because the second selection is just to find the distance which is, by definition, the same), it is good practice.
breed [prey a-prey]
breed [predators predator]
breed [hidingspots hidingspot]
to setup
clear-all
create-predators 1 [setxy random-xcor random-ycor set color red]
create-prey 5 [setxy random-xcor random-ycor set color brown]
create-hidingspots 20
[ setxy random-xcor random-ycor
hide-turtle
ask patch-here [set pcolor green]
]
reset-ticks
end
to go
ifelse any? predators
[ ask prey [hide] ]
[ ask prey [swim] ]
end
to hide ; turtle procedure
let target min-one-of hidingspots [distance self]
let path patches in-cone distance target 80
ask path [ set pcolor yellow ]
if any? predators-on path
[ set color red
output-print "DANGER"
face target
]
forward 1
end
to swim
end
you are asking each prey to ask all prey to hide. If you drop the ask prey from the command all prey are running it should work fine and be a bit faster
to hide
face min-one-of hidingspot [distance myself]
set d distance min-one-of hidingspot [distance myself]
ask patches in-cone d 80
[set pcolor yellow]
if (any? predators-here)
[
forward 1
set color red
output-print "DANGER"
]
end

How to set follower follow any leader nearest to them in NetLogo

I really need some advice on this, I try to create few leaders and some amount of followers, however it seems that followers do not follow any leader, how do I make a follower follow the nearest leader to them.thanks
turtles-own
[
is-leader?
follower
]
to setup
clear-all
reset-ticks
ask n-of 50 patches
[sprout 1
[set color blue
set follower self]]
choose-leaders
end
to choose-leaders
ask max-n-of 5 turtles [count turtles in-radius 3]
[
set is-leader? true
set color white
]
end
to go
ask turtles [follow_leader]
tick
end
to follow_leader
ask follower
if any? is-leader? in-radius 30
[ set heading (towards min-one-of is-leader? [distance self]) fd 1]
end
It's a bit hard to make sense of what you are trying to do with the follower turtle variable. With the code you posted, all turtles have themselves as follower, which I am almost certain is not right. In other words, currently the variable doesn't do anything, and you can delete it unless you want to do something else with it.
Regardless, the problem with your code is in your follow_leader procedure. This will work - I added comments so you can see what
to follow_leader
ask follower
;; since all turtles are asked to run this procedure, and all turtles have themselves as
;;follower, this asks turtles to ask themselves to do something.
if any? is-leader? in-radius 30
;; this is incorrect syntax.
[ set heading (towards min-one-of is-leader? [distance self]) fd 1]
;; 'self' refers to the turtle itself, so this will always return 0, and turtles will face themselves
end
Given these errors, this is probably what you want:
to go
ask turtles with [not leader?] [follow_leader];; we only want to ask turtles that are not leaders
tick
end
to follow-leader ;; changed just for NetLogo convention
let nearby-leaders turtles with [leader? and distance myself < 30] ;; find nearby leaders
if any? nearby-leaders [ ;; to avoid 'nobody'-error, check if there are any first
face min-one-of nearby-leaders [distance myself] ;; then face the one closest to myself
fd 1
]
end
Make sure to initialize the leader? boolean in all your turtles. Add set leader? false in the command block that you send to turtles that you sprout in your to setup procedure.

asking red turtles to avoid other turtles and move to one of its neighbor which is empty and has highest conc

I'm new to Netlogo. Here I'm trying ask red turtles to move towards the hight conc. patches. yellow turtles do not move. I did that! but I also want to ask the red turtles to avoid the patches which have yellow or red turtles on them and move to the neighbor of high conc.. In my code I asked them to stop once they become next to an occupied patch just because I couldn't do it. I also want to avoid getting 2 turtles on the same patch at any time. Any one could help me with that please?
patches-own [conc]
to set-up
clear-all
ask patch random-pxcor random-pycor [
set conc 200
set pcolor scale-color red conc 0 1]
crt 5 [setxy random-xcor random-ycor set shape "circle" set color red]
crt 20 [setxy random-xcor random-ycor set shape "circle" set color yellow]
reset-ticks
end
to go
diffuse conc 0.1
ask patches [set pcolor scale-color red conc 0 1]
ask turtles with [color = red]
[ifelse not any? turtles-on neighbors
[if [conc] of max-one-of neighbors [conc] > conc [
face max-one-of neighbors4 [conc]
move-to max-one-of neighbors4 [conc]]]
[stop]
]
tick
end
I think your code would read a little nicer if you used let to avoid repetition, like this:
let target max-one-of neighbors [conc]
if [conc] of target > conc [
face target
move-to target
]
For some different possible approaches to enforcing a "one turtle per patch" rule, see the One Turtle Per Patch Example model, in the Code Examples section of NetLogo's Models Library.
I assume that ifelse not any? turtles-on neighbors is your attempt to make turtles avoid occupied patches. But as you've written it, it has a stronger effect than that — it makes it so that any turtle with an adjacent occupied patch won't move at all.
I think you may have meant something more like:
ask turtles with [color = red] [
let targets neighbors with [not any? turtles-here]
let target max-one-of targets [conc]
if target != nobody and [conc] of target > conc [
face target
move-to target
]
]
Hope this helps.