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

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

Related

How to move to a patch that is at least x distance from a turtle

I'd like to ask a turtle to move to any patch that is at least a distance of x away from another turtle. So in a mix of NetLogo and English it would be:
move-to one-of patches with [distance to nearest turtle > 4]
How can this be done please?
You find the nearest turtle using the min-one-of + [distance myself] reporter. You also need to make sure that you only look at other turtles since a turtle will always be the turtle that is closest to itself.
The code can be broken up like this:
let nearest-turtle min-one-of other turtles [distance myself]
move-to one-of patches with [distance nearest-turtle > 4]
For (arguably) better readability.
edit: thanks Nicolas for the correction. You are totally right.
Using all of the above, here is a solution:
to go
ask patches [set nearest-turtle min-one-of turtles [distance myself]
set distance-turtle distance nearest-turtle
]
crt 1 [
set color blue
move-to one-of patches with [distance-turtle > 4]]
end

NetLogo: Moving turtles towards patch color

Is it possible to have a turtle move towards a patch with a certain color?
i.e. have a turtle move towards red patch from blue patch?
sure enough
ask the turtles in question to
face one-of patches with [pcolor = red]
fd 1
although you should do some exception handling because if there is not a patch of that color you will get a
"FACE expected input to be an agent but got NOBODY instead. error
while turtle 0 running FACE called by Agent Monitor"
error
I do it something like this
let targ one-of patches with[pcolor = red]
if targ != nobody [set heading towards targ fd 1]
I hope that works for what you want.
Alternately as I was reminded in the comments.
If any? Patches with[ pcolor = red]
[set heading towards one-of patches with[ pcolor = red ] fd 1]
But as Seth says in the comments that computes the red patches twice which is costly.

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

NetLogo: Apply set [variable] from sub-procedure to global process?

I want to simulate reproduction process of turtles in time when single patch can by used only once. If the patch is red and ticks mod 50 = 0 then turtles-here (on this patch) hatch new 10 turtles. Every patch can be used only once during the whole simulation run.
Please, how can I include this condition into my code? I tried simple to change patch color to green with hope that the next hatching process will run only with red ones. However next time step NetLogo doesn't keep this patch green but changes it back to red. Thus my reproduction run from the same patch.
Any suggestions will be highly appreciated
The part of my code:
to go
if ticks mod 50 = 0 [ask patches with [pcolor= red] [reproduce] ]
end
to reproduce
ask one-of turtles-here
[hatch 10 ;
die]
; set pcolor green - change infestlev from 2 to 5 only for specific tick, not for the rest of the simulation
end
The code you have should work fine. In your description, you state that the colour turns back to red - so that's why this code isn't working, somewhere else you have a colouring procedure. Alternatively, if you don't want to rely on colour (or if you want colours to mean something else), then you can add a variable to each patch to keep track of whether it has already reproduced.
patches-own [reproduced?]
to setup
...
ask patches [set reproduced? FALSE]
...
end
to go
if ticks mod 50 = 0 [ask patches with [not reproduced?] [reproduce] ]
end
to reproduce
ask one-of turtles-here
[ hatch 10
die ]
set reproduced? TRUE
end
Just as a general comment, it is a little odd to ask the patch to reproduce when what you are really trying to do is have the turtle on a patch reproduce. Logically you are saying that once one turtle on a patch has reproduced, then no other turtle on that patch can ever reproduce. If the reproduction is truly governed by the patch, it would be more usual to use sprout instead of hatch. That gets you code that looks like this:
to reproduce
sprout 10 [ any commands you want the new turtles to do ]
set reproduced? TRUE
end
My final working code with steps (available here: http://ulozto.cz/xRqtDDfV/timing-of-turtle-sprout-nlogo):
setup turtles
if turtle touch red patch, turn this patch blue
at the same time - tick 10 -> sprout from every blue patch 10 nwe turtles
every patch can be used only once during simulation run (turn red, assured by reproduced? variable)
enter code here
patches-own [reproduced?]
to setup
clear-all
setup-turtles
setup-patches
change-color
reset-ticks
end
to setup-patches
ask patches [set reproduced? FALSE]
ask patches [set pcolor green]
ask n-of 80 patches [set pcolor red] ; identify turles which could be a source for new turtles
end
to setup-turtles
crt 1
ask turtles [set color yellow]
end
to go
if ticks mod 10 = 0 [
ask patches with [(pcolor = blue) and not (reproduced?)]
[reproduce] ; set reproduction to every 10 ticks for all blue patches
]
move-turtles
change-color
tick
end
to move-turtles
ask turtles [fd 1]
end
to change-color ; if turtle touch red patch, red turns blue
ask turtles [if pcolor = red
[set pcolor blue]
]
end
to reproduce ; sprout 10 new turtles from blue patches at defined time step (multiply of 10 ticks)
sprout 10
set reproduced? TRUE
end

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.