Netlogo How to make turtles avoid certain patch - netlogo

I am using netlogo to simulate the migration of some fish, if pcolor==98, then they can swim in any direction, but they can't move to patches with pcolor==0, how do I set it?
My code is below.
if pcolor = 98 [
rt random 360
fd 1
set heading 0
]
Thanks in advance!

Related

Netlogo 'towards' behaviour

For a pursuit-evasion assignment I need to use the NetLogo command 'towards', but it doesn't seem to work, or I don't understand it.
What I think it should do: give the angle between the heading of the turtle and the line connecting the turtle with the target.
Here's the code for a simple model I made just to show the problem.
to set-up
clear-all
create-turtles 2
ask turtle 0 [
set xcor 0
set ycor 0
set shape "circle"
]
ask turtle 1 [
set xcor min-pxcor
set ycor max-pycor
set heading 135
]
end
to go
ask turtle 0 [ fd 0.1 ]
ask turtle 1 [ show towards turtle 0 ]
end
And here's a video of the behaviour. https://youtu.be/MUBiAypppc4 (I couldn't find a way to remove the audio without just replacing it using YouTube's current editing system, I'm sorry; you'll have to mute the audio yourself)
Examples of expected behaviour:
from 0:14 to 0:19, I would expect the number to gradually decrease, not increase
at about 0:38, I would expect the number to be 0, not somewhere around 300
between 0:38 and 0:42, I would expect the number to decrease or increase consistently, without those two sudden jumps
Is there a problem somewhere, or does 'towards' mean something different than I thought?
So turtle 0 is moving and turtle 1 is reporting the direction to turtle 0. I think towards is working fine but you have forgotten about the world settings. For example, in the 14-19s part, the shortest path from 0 to 1 is down and left (about 220 heading), but that shortest path is with the world wrapped. Your turtles can move off one side and come in on the other (as you can see turtle 1 doing).
NetLogo measures distances and directions taking into account the wrapping configuration. It knows that the shortest path to get from turtle 0 to turtle 1 goes off the side and comes in the other, and reports the direction that the turtle would have to move to follow that path.
Create a link and you can see this. Revised code:
to set-up
clear-all
create-turtles 2
ask turtle 0 [
set xcor 0
set ycor 0
set shape "circle"
]
ask turtle 1 [
set xcor min-pxcor
set ycor max-pycor
set heading 135
create-link-with turtle 0
]
end
to go
ask turtle 0 [ fd 0.1 ]
ask turtle 1 [ show towards turtle 0 ]
end

Bounce routine with random walk in Netlogo 3D

I'm having trouble coding a bounce routine with a random walk in Netlogo. I want to have agents do a random walk in a unidirectional current (the code for this bit is not correct, but is a functional placeholder). I also need to prevent agents from entering a seabed feature, delineated by an RGB pcolor. Here's my effort:
to move-resources
ask resources [
right random 45
left random 45
tilt-up random 45
tilt-down random 45
fd 1
;;; simulated current in one direction:
set heading 90
set pitch 0
set roll 0
fd 1
while [ any? patches in-radius 1 with [ pcolor = [218 160 62]] ] [
let nearest-patch min-one-of (patches with [pcolor = [218 160 62]])[distance
myself] ;;; find the closest sandy patch
face nearest-patch ;; face that patch
set heading heading - 180 ;; face away from that patch
fd 1 ;; move away from that patch
]
]
end
Apologies for missing a trivial mistake, but my problem was simply the use of:
set heading heading - 180
When in 3D I should've used (for a horizontally-oriented seabed):
set pitch pitch - 180

Netlogo Reporter Not Reporting

I've made a animal behavior model involving "turtles" and "roads" and I want the model to report back to me when the turtle "crosses" a road. All I want is that it tells me when the turtle moves from a patch that is the grey color to the red color. I've included the code asking it to report this and the program has no issue with the code. To give me a visual representation of what I want it to report, I put a monitor on interface. But it always gives me a "0" for road crossings, even as I can see that my turtle has crossed roads. I would count it by hand, but it's impossible to tell for certain how many road crossings there are and this is for scientific publication. My code is as follows...
turtles-own [
road-crossings
]
to setup
clear-all
;; create turtles on random patches.
ask patch 6 -15 [
sprout 1 [
set color one-of [green]
set size 1
set road-crossings 0
]
]
ask turtles [
if [pcolor] of patch-here = 14.9 [
set road-crossings road-crossings + 1
]
]
reset-ticks
end
to go
ask turtles [
repeat 100 [
repeat 39 [
pen-down
rt random-float 360
lt random-float 360
fd random-float 1.375
]
setxy 6 -15
]
]
tick
end
Any help is appreciated! Thank you!
There are several potential problems with this that I can see.
First, road-crossings is a turtle variable, which is the correct thing to do if you want each turtle to remember how many times it crosses a road. If so, however, the monitor must report sum [road-crossings] of turtles to get the road crossings of all turtles.
Second, which I think is actually your problem: you have the turtle checking whether it crosses the road in the setup procedure rather than the go procedure. The setup procedure is only run at the beginning.
Third, you don't actually have any roads in your example code, but I suspect that's just a failure to create a proper example. I assume that there are patches with pcolor of 14.9 in your real code. If not, though, that would also cause your error. You can make sure by going into the command center and asking count patches with [pcolor = 14.9]

How to add paths showing turtle movement in Netlogo

I'm trying to make a model that will tick a stipulated number of times and will move a maximum distance each time. I've gotten that done, but I can't figure out how to make the model show where the turtle has been using paths connecting the different locations the turtle went to.
I don't even really know if this is possible...? I've googled, searched stack overflow and looked through the Netlogo dictionary and looked through the somewhat helpful textbook I have on hand and can't find anything. I'm sure it's simple, but I just have no idea how to go about it. My current code:
to setup
clear-all
;; create turtles on random patches.
ask patch 118 28 [
sprout 1 [
set color one-of [green]
set size 10
]
]
reset-ticks
end
to go
ask turtles [
rt random-float 360
lt random-float 360
fd random-float 15
]
if ticks = 28 [return]
tick
end
to return
ask turtles [
setxy 118 28
]
end

Move turtles around a patch

I am trying to move turtle around patch 0 0 starting from random position in world. But circle keeps on growing. What am I doing wrong here?.
Code:
to setup
clear-all
create-turtles 5
ask turtles [
setxy random-xcor random-ycor
]
ask patch 0 0 [ set pcolor green ]
reset-ticks
end
to go
move-turtles
tick
end
to move-turtles
ask turtles
[
face patch 0 0
right 90
fd 0.01
set pen-size 3
pen-down
]
end
Secondly I want a turtle to move around any patch I define when it reaches with in a certain range
Your approach is to take a small step along a tangent to the circle you want, but this takes you a little bit outside the circle. You do this repeatedly, so it accumulates over time.
For a better way, see the Turtles Circling example in the NetLogo Models Library.