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.
Related
I have a model where humans and a door are created. Humans face the door and run to it and exit. The problem is that some humans stop for some reason. Even if only one human is used, it some times reaches the door, and some times it doesn't. What do I have to do so humans always reach the door? This is the model, and this is the code:
globals [ID-door]
breed [door doors]
breed [human humans]
to setup
clear-all
set-default-shape door "star"
crt number [
setxy random-xcor random-ycor
set color cyan
set breed human]
new-door
reset-ticks
end
to new-door
ask one-of patches [sprout-door 1]
ask door [
set color yellow
set size 2
set ID-door who]
end
to go
if count human = 0 [stop]
ask human [
move-human
check-door]
tick
end
to move-human
face doors ID-door
ifelse any? human-on patch-ahead 1
[rt random 40 lt random 40]
[fd 1]
end
to check-door
if any? door-on patch-here [die]
end
Your problem is patch-ahead 1. This looks a distance of 1 in whatever direction the turtle is facing. Imagine the turtle is at the top left corner and looking toward the bottom right corner. The distance to the corner is >1 and the turtle is triggering the 'stay here' check and will be stuck until it is sufficiently turned around so that there is a different patch in front of it.
So you need to get the turtle to exclude itself from the check, which is a job for other. Change ifelse any? human-on patch-ahead 1 to ifelse any? other human-on patch-ahead 1.
I changed the move-human procedure to the following, and now the model works:
to move-human
ifelse patch-ahead 1 = nobody or any? other humans-on patch-ahead 1
[rt random 40 lt random 40]
[fd 1
face door ID-door]
end
As stated by JenB, patch-ahead 1 was the problem, so:
a) Because the world has horizontal and vertical limits (it´s not a wrapped space) the line patch-ahead 1 = nobody checks the absence of patches when such limits are reached.
b) The line other humans-on patch-ahead 1 excludes the current turtle as it may be counted because of the possibility that the distance of 1 may still be inside the current patch, like this image:
I have built the road shapefile which already intersected with patches. I want the turtle to move on the road that I had assigned.
to go
ask turtles
[
step
avoid-wall
]
tick
end
to-report coinflip?
report random 2 = 0
end
to avoid-wall
if [pcolor] of patch-ahead 3 = black [set heading heading - 180]
end
to step
ifelse coinflip? [rt random 60] [lt random 60]
fd random 3
end
Actually, I intend to let the turtle move out of the assigned patches. But I still want them to come back in the next tick or just the few tick later. Some turtles come back and some do not (according to my code). How should I fix the code?
I have tried to use this code in Turtles moving in a pattern (Netlogo) but it does not work (I think that it does not work because my patches are a narrow road, not an area.)
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
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]
I want to limit number of turtles per patch. I thought if I restrict movement of turtles as per the (1) and (2) conditions it will limit number of turtles per patch but whatever code I tried for this till now did not worked.
Let's suppose there are five turtles on patch Y and five is the limit.
1) to ask turtles standing at front on patch X (refer figure) to stop moving till there are five turtles on patch Y (refer figure).
2) to ask turtles standing at front on patch Y to move forward to patch z (refer figure) if patch z has less than five(5) turtles on it else stop.
At last I am using following simple code
let turtles-ahead other turtles in-cone speed 90
let turtle-ahead min-one-of turtles-ahead [distance myself]
ifelse turtle-ahead != nobody
[
set speed [speed] of turtle-ahead
slow-down
]
[speed-up]
This code simply ask turtles to move one-behind-another pattern or queue but it does not help me to limit number of turtles per patch whatever limit may be 4,5,6,7, 8... I have sprouted turtles within "go" procedures (1 turtle per patch, as per my need). The turtles are sprouted on a defined set of patches not in the whole world. So slowly number of turtles starts increasing and move around the world and after certain amount of ticks they are ask to exit out of the defined area and they die. Now at times it shows 10,11,.... 37 or above turtles on certain patches and this I want to stop actually.
I have checked one-turtles-per-patch, other code examples and many other helps from internet but no results.
For any other idea or help I would be obliged. Please help me.
I think you want to have turtles assess the count of turtles-here of the patch to which they are trying to move. Consider this simple example:
to setup
ca
ask n-of 15 patches with [ pycor = 0 ] [
sprout 3 [
set heading 90
]
]
reset-ticks
end
to go
ask turtles [
if ( count [turtles-here] of patch-ahead 1 ) < 5 and xcor < 16 [
fd 1
]
]
print [count turtles-here] of patches with [ any? turtles-here ]
tick
end
On each tick, the turtles with an xcor of less than 16 (just to set a stop for this example) all check the patch-ahead 1 for the count of turtles on that patch. If the count is less than 5, the turtle moves to that patch. Otherwise, the turtle does nothing.