Here I want to avoid an obstacle(shown in blue) by moving at right angles to it and then going on the same trajectory as initially,the turtle can approach from any direction to the obstacle,In my application,these objects will be moving robots(turtles).
The problem I am facing is setting the heading independent of the present heading(lets say theta),i.e whether it lies in first,second or other quadrants.
Avoid blue obstacle
I think this is one of the ways,but there may be many more,need suggestions
ifelse min-one-of other turtles in-radius 2 [distance myself]!= nobody
[face patch-right-and-ahead 90 1 fd 2 repeat 2 [face patch-left-and-ahead 90 1 fd 3] face patch-right-and-ahead 90 1 fd 3]
[pd fd 1]]
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 two netlogo files with this identical code:
to setup
ca
crt 10
[setxy random-xcor random-ycor]
reset-ticks
end
to go
ask turtles [
ifelse any? turtles-on patch-ahead 1
[rt random 40 lt random 40]
[fd 1]]
tick
end
In this file works: test 1,
but not on this one: test 2. It states
TURTLES-ON expected input to be an agent or agentset but got NOBODY
instead.
Why does this happen?
The error code is part of a larger model that states the same error, how can I fix it?
This issue is due to the difference in world-wrapping in your two files- in Test 1 you have world wrapping on:
Whereas you have it turned off in Test 2:
This means any turtle that reaches the edge of the world is querying a patch that does not exist- a patch outside of the world (nobody). You can either turn world wrapping on, or address the movement model by checking that movement is possible either with something like can-move? or by coding it manually. For example, you could change your go in test 2 to
to go
ask turtles [
ifelse patch-ahead 1 = nobody or any? turtles-on patch-ahead 1
[rt random 40 lt random 40]
[fd 1]
]
tick
end
Note that order matters in this case- you have to check for nobody before checking any? turtles-on patch-ahead 1
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
I have created a new patch type which allows turtles to turn randomly down various paths while one a junction patch ( with pcolor 6 ). How should I modify this code so that I do not get the error "Towards expected input to be an agent but got nobody instead." The code is as follows :
if pcolor = 6 [ set heading towards one-of neighbors in-cone 1 180]
Any help would be much appreciated.
To specifically answer your question, you need to check if there are any patches that fulfill your criteria. To do that, you can do
ask a-turtle [if any? neighbors in-cone 1 180 [face one-of neighbors in-cone 1 180]]
Doing it this way, you will create the same turtle set twice (when you check if there are any, and before you face one of them), so a more optimized way of doing this is:
ask a-turtle [
let eligible-neighbors neighbors in-cone 1 180
if any? eligible-neighbors [face one-of eligible-neighbors]
]
That said, I think Alan is right that you are getting this error because you have wrapping off and your turtles are either in a corner or facing a wall. If this is the case, you need to figure out what to do in that case. If you just want them to turn around and keep going, you could use ifelse like this:
ask a-turtle [
let eligible-neighbors neighbors in-cone 1 180
ifelse any? eligible-neighbors
[face one-of eligible-neighbors] ;; face a neighboring patch if there are any
[rt 180] ;; else, turn around 180 degrees
]
try first making a temporary variable with 'let', then setting the heading towards that.
e.g.
let FaceHere one-of neighbors in-cone 1 180
face FaceHere
haven't tried that - just an idea.
by the way, you can replace
set heading towards
with
face
Im making a race model. the function is that the turtles move across the grid horizontally starting at xcor -13 to xcor 13 at speeds that
constantly vary and when a turtle reaches xcor = 13, all of the other turtles (besides the one that crossed first) die
to Race
wait .3
fd random 5
if xcor = 13 ( this is where i want to tell all other turtles to die )
end
how do i ask all other turtles to die?
THe first answer doesnt help me, someone else please respond
You can do that by asking the winner to ask other turtles [die]
to setup
clear-all
reset-ticks
;resize-world min-pxcor max-pxcor min-pycor max-pycor
resize-world -15 20 0 3
set-patch-size 15
;set-patch-size size
create-turtles 10
[setxy -13 1 set heading 90 set shape "car" wait 0.3]
ask patch -13 2 [Set plabel "Start" set pcolor 110] ; just for visualization
ask patch 13 2 [Set plabel "END" set pcolor 110]
end
to go
ifelse count turtles > 1
[
ask turtles
[Race]
]
[stop]
tick
end
to Race
fd random 5
if xcor >= 13 [ set size 2 ask other turtles [die] ]
end
This is a sample screenshot
I really low examples, so there is another way to improve visualization of the race by having multiple lines of cars:
resize-world -15 20 0 5
set-patch-size 15
create-turtles 20
[set xcor -13 set ycor one-of [0 1 2 3 4 ] set heading 90 set shape "car" ]
ask patch -13 5 [Set plabel "Start" set pcolor 110]
ask patch 13 5 [Set plabel "END" set pcolor 110]
What you are trying to do doesn't make much sense. When posting, please make sure that the question provides a better context for answering you question.
My interpretation of your question is that the cars don't matter at all. All you want to figure out is that when one turtle crosses the finish line, you kill all the other turtles.
Doing so I would probably give each turtle a property of a name or label. Store them all in an array.
Then if the turtle crosses the finish line, remove all the turtles from the array except for
if turtle.name == turtle[i].name.
Hope that helps. Please be a little more clear next time.
You need a way to identify the living turtle and kill the other turtles. To do this, you could either write a ton of if statements but it would look horrible.
if(larry.coordinates == 13){
kill(tom);
kill(harry);
}
Your best bet would be to read how to create an array. Store it in an array. Trust me, arrays are pretty simple.