How should i modify this code to allow me to let turtles turn randomly down paths at a junction? - netlogo

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

Related

Obstacle avoidance (in cone)

I am trying to create a command so the turtles would avoid the green patches in my map.
to avoid-obstacle
let front-patches patches in-cone 5 60
if pcolor of one-of front-patches = green [set heading heading - 45]
end
unfortunately gives me
of expected this input to be a reporter block, but got a number or list instead
any idea how to fix it?
The error message is saying that the command of wants a reporter block, which is a code block (in square brackets) that returns something. In this case it wants to get the value of the variable pcolor from the specified turtle. Try this (note the brackets around pcolor):
to avoid-obstacle
let front-patches patches in-cone 5 60
if [pcolor] of one-of front-patches = green [set heading heading - 45]
end
By the way, it's not going to do exactly what you have described. This code will randomly select one of the front-patches (that's what one-of does) and check if it's green. From your description, I think you want to change direction if any of them are green. That would look like:
to avoid-obstacle
let front-patches patches in-cone 5 60
if any? front-patches with [pcolor = green] [set heading heading - 45]
end

How to Check if the Two Turtles are on the Same Y Coordinate in NetLogo

I am working on a NetLogo project and I want my pupil turtles to move straight until they come to be on the same Y coordinate as their relevant link neighbour and from there move towards them (the link neighbour).
Note that each pupil has only one link neighbour.
This is the code that I have come up with,
to go
ask pupils [
let target one-of link-neighbors
ifelse [ycor] of myself != [ycor] of target
[
set heading 0
fd 1
]
[
face target
fd 1
]
]
tick
end
This does not work, the turtles keep moving straight. Can someone please help. I just want the turtles to get to their link neighbours, but there are walls that they must avoid.
Your issue is that ycor is a decimal value. So, For example turtle 1 may be on 3.2 and turtle 2 may be on 3.3.
Instead, I think you want to use turtles-here.
to go
ask pupils [
let target one-of link-neighbors
ifelse member? target turtles-here
[set heading 0]
[face target]
fd 1
]
tick
end
On a side note, how many link-neighbors does each target have? My concern is that let target one-of link-neighbors will reset the target each tick.

Tell agent to not cross a road in Netlogo model

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.

Why do i get this runtime error when i instruct a turtles to set its heading towards a patch with a certain pcolor?

I want to be make turtles set their heading towards one of the patches that have pcolor = grey in a cone of 1 270 when they are on a patch with pcolor = 6. My code is as follows:
ask turtles[if [pcolor] of patch-here = 6 [ set heading towards one-of neighbors4 in-cone 1 270 with [pcolor] = grey
However I get the error:
TOWARDS expected input to be an agent but got NOBODY instead.
What should be done to the code to prevent this error? Any help would be great.
If you're getting nobody, that means that there isn't always a patch meeting the conditions you have set up.
You need to decide what you want to do in that case. Suppose you decide the turtle should do nothing. Then the new code is:
ask turtles [
if pcolor = 6 [
let target one-of ...
if is-patch? target [
face target
]
]
]
Note that of patch-here is always redundant. A turtle always has direct access to the patch variables of the patch it is standing on.
Note also the use of face, which is more concise than set heading towards.

How to ensure turtles follow heading to leader's heading and avoid the obstacles at the same time

I'm trying to set turtles heading equal to leader's heading and at the same all turtles including leaders must avoid obstacle( avoid_obstacles function). My problem is, when i add the set heading code
in flock function:
[set heading [heading] of one-of nearby-leaders ]
it cause my avoid-obstacle code to break. if i comment out this code, avoid obstacle work fine. below is my complete code.
to go
set time-to-evacuate time-to-evacuate + 1
ask turtles [check fd 0.1]
ask turtles [avoid_obstacles fd 0.1]
ask turtles with [pcolor = red][die] ;;turtles exit thru red door will die
if all? turtles [ pcolor = red ] ;; stop simulation
[ stop ]
tick
end
to check
if not leader?
[let beings-seen patches in-cone vision vision-angle with [pcolor = red]
ifelse any? beings-seen
[let target one-of beings-seen face target fd 1 ]
[flock]]
if leader?
[let beings-seen patches in-cone leader-vision leader-vision-angle with [pcolor = red]
ifelse any? beings-seen
[let target one-of beings-seen face target fd 1 ]
[flock]]
end
to flock
let nearby-leaders turtles with [leader? ]
if any? nearby-leaders in-radius vision
[ set heading [heading] of one-of nearby-leaders ]
end
to avoid_obstacles ;; all obstacles set as green patches
let i 1
while [[pcolor] of patch-ahead i != green and i <= vision]
[set i (i + 1) ]
if ([pcolor] of patch-ahead i = green)
[
ifelse [pcolor] of patch-at-heading-and-distance (heading - 20) i + 1 = green
[
ifelse [pcolor] of patch-at-heading-and-distance (heading + 20) i + 1 = green
[
ifelse random 1 = 0
[ rt 30 ]
[ lt 30 ]
]
[ rt 60 ]
]
[lt 60]
]
end
can somebody point what wrong with my code
First of all, consider abandoning the concept of having "leaders" as such. In crowd escape panic situations, they don't stop and hold elections. LOL. I don't know what your model is trying to model, so I have no suggestions here. My model "
Anyway, what your "followers" need to do is integrate to desires, the desire to go the same way as the leader, and the desire to avoid obstacles.
For the first, the follower simply needs a place to remember the "desired" direction. So, rather than set heading directly, you store the heading of the leader./ Then you perhaps use that heading to influence the movement around obstacles.j
For the second, well, obstacle avoidance is a complex discussion on its own. Whatever you have put together, you need to modify it so that it takes the "desired" heading into account, while still effectively avoiding obstacles. This is can be very difficult to do simply.
My model "homing particles 2009" uses one method. This model is designed to explore a particular homing/avoidance behavior. When a particle can't move in the desired direction, it is allowed to move in a limited number of other directions, instead.
Here is the link: http://www.turtlezero.com/models/view.php?model=homing-particles_2009
Unless you have added http://www.turtlezero.com to your list of allowed sites in your Configure Java console (not recommended, but you can trust me, right?), you will not be able to run my models in the browser. I haven't checked what that model needs to run in NetLogo 5.
The ultimate solution to that problem may be to use a path-finding algorithm, like a* (a-star).
In that case you provide your follower with a preferred destination (and that can be vaguely defined as "a point somewhere in 'that' direction"), and the route-planner algorithm plots a route. You can make the route-planner unaware of obstacles until they are "visible" or whatever you define (for example, in a press of bodies, a follower might not be aware of an obstacle until stumbled upon).
Hope this helps!