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.)
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'm trying to add a condition that doesn't allow an agent to cross over a road. The road patches are set to color red. I can't seem to figure out how to get this condition to work. I ultimately want the agent to turn around if the road is in the patch ahead. Here is my net logo code so far.
to go
ask turtles [
move
]
tick
if ticks >= 60 [stop]
end
to move
ifelse random-float 1 < q
[
ifelse random-float 1 < w
[let target-patch max-one-of neighbors [veg-suitability]
face target-patch]
[let target-patch max-one-of neighbors [pelev]
face target-patch]
]
[
ifelse [pcolor] of patch-ahead 1 = red
[lt random-float 180]
move-to one-of neighbors
ldd-normal
]
end
to ldd-normal
let ldd-distance (ldd-scale)
fd ldd-distance
end
The logic of your move procedure is a bit confused I think. First you have a random chance to either move to a patch with a higher value of a variable of interest (with the uphill primitive) or, if the random draw fails, it moves to a random neighbour. If you don't want it to move onto a red patch then you need to test if the patch that is chosen is red, but you just move it without checking.
After you have moved the turtle, you then check the colour of patch-ahead. Your problem here is that patch-ahead depends on the direction the turtle is facing, which has nothing to do with the direction it has already been moving. You either make it turn (though it may not turn enough) OR move forward. So it never actually moves away.
I can't give you an actual answer because I don't know what your logic is supposed to be. But you could look at structures like:
move-to one-of neighbors with [pcolor != red]
Or, if there are enough red patches that it is possible that there aren't any non-red neighbours (which would cause an error if you tried to move to one), you could use:
let okay-patches neighbors with [pcolor != red]
if any? okay-patches [move-to one-of okay-patches]
Another option is that you only meant to face rather than move to the patch in the first sections of code, then test whether it is facing a red patch and turn around if it is.
I've created two breeds of turtles in my simulation: one is a regular turtle and the other is a halo that is intended to overlap each turtle. Whenever a turtle is hatched (either created as part of the setup procedure or created with netlogo's hatch function), a halo is also hatched and linked by calling a separate make-halo function.
create turtles turtle-initial-number
;;(all the turtle genes are set here)
if halos-enabled [make-halo]
to make-halo
hatch-halos 1
[ set size sight-radius * 2 + 1
set shape "square"
set color lput 64 extract-rgb color
__set-line-thickness 0.5
create-link-from myself
[ tie
hide-link ] ]
end
Due to some interactions I've implemented, sometimes the turtles and the halos become detached from one another, so I'd like to add a step at the end of each tick where all halos snap back to their turtles where they belong. Is there a way to move the halo or set its coordinates to the turtle at the other end of the link?
The other option is to solve whatever is happening when the disconnect occurs. I have another breed of turtle (people) who can "push" others with this push-away function below. Turtles (and their halos) occupying the 9 squares in front of the person are pushed forward along the same direction at the person is facing. When they are pushed, for some reason the turtle isn't at the centre of the halo anymore.
to push-away
ask people [
let push-dir heading
ask patch-ahead 2
[ask turtles-here
[set heading push-dir
fd 2]
ask neighbors
[ask turtles-here
[set heading push-dir
fd 2]
]
]
]
end
In theory, the tie should link the movements. But to snap the halo to its turtle, you can ask the halo to move-to the turtle. The only trick will be identifying the correct turtle and you haven't shown enough of your code for me to sort out the identification for you.
I suggest you actually add a variable to the halos that records their turtle rather than using a link. If the link has no other purpose, there is no need to create all those extra model entities. You would use it like this:
halos-own [my-owner]
to make-halo
hatch-halos 1
[ set size sight-radius * 2 + 1
set shape "square"
set color lput 64 extract-rgb color
__set-line-thickness 0.5
set my-owner myself ; this is the new line
]
end
to push-away
<all the code you have already>
ask halos
[ move-to my-owner
]
end
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.
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.