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

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!

Related

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.

NetLogo: Having a turtle remember its starting location

I want to have my turtles move back and forth between a central area and their starting location. I have set the central area (patch 0 0, and its neighbouring patches). I have set these turtles to begin from random locations on setup.
Now I need them to move to the central area and be able to remember and return to their respective starting positions. Here is my attempt, but one that is not working.
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
create-turtles 10
[ set shape "car"
set size 1
set color white
setxy random-xcor random-ycor
if (patches != patches with [pcolor = green])
[ set start-position (random-xcor random-ycor)] ;; line with error
]
to go
ask turtles
[ set heading target-patch move-to target-patch
set hold-time hold-time + 5
]
ask turtles
[ if hold-time >= 10
[ set heading start-position move-to start-position]
]
end
There are several problems with your code. I strongly suggest that you code in smaller pieces. That is, add some code and make sure it works before writing the next piece. Making sure it works is not just making it through without error messages, it needs to do what you expect it to do.
On your specific question. The line if (patches != patches with [pcolor = green]) is causing an error. First, patches is the set of all patches, not just a particular patch. So you are (sort of) asking whether the set of all patches is not equal to the set of patches that are green. Is that really what you intended? If so, it is easier to simply ask whether there is any patch that is not green:
if any? patches with [pcolor != green]
or to check whether they are all green and continue if not:
if not all? patches [pcolor = green]
However, since you are asking about moving back and forth to and from the central green patches, I think you really want to have the turtle check whether the patch they happen to be located on is green. This code looks at the patch where the turtle is located (patch-here) and checks whether the color (pcolor) is green:
if [pcolor] of patch-here = green [ ]
However, one of the tricks of NetLogo is that turtles can access the variables of the patch they are on directly. Note that a patch cannot access a turtle's variables because there may be multiple turtles on the patch so the patch doesn't know which turtle you want. But a turtle can only ever be on one patch at once. So you could write:
if pcolor = green [ ]
You also need to rethink this code:
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
This suggests to me that you have misunderstood something very fundamental to NetLogo programming. You need to think from the perspective of an individual agent. Looking at this code, you first do ask turtles, so that is going to run through all the turtles in random order. Let's call them A, then B, then C and so on.
What is each turtle going to do? Everything in the [ ]. So, A sets the value of the global variable named "target-patch" to patch 0 0. Then A asks that patch to turn green, have the 8 surrounding patches to turn green, and to set the variable "hold-time" to the value 5.
So far, so good. But then turtle B does exactly the same thing - it assigns "target-patch", turns it and its neighbors green etc. Then turtle C. If you have 100 turtles, this block of code will run 100 times and do exactly the same thing each time.

getting a particular colored turtle to do something on a particular colored patch in Netlogo

I am putting together a Netlogo model where students and criminals are interacting on a college campus. If criminals are close to students, a chase ensues so that when a criminal is distance < 1 the student dies. In addition to dying when criminal are to close I also want students(leader) to die during the chase when they are on a green patch. Green patches in this sense equate 'safe' areas. I have procedures set up for criminals chasing(follower) and students running away(leader), but I am at a complete block with getting the students being chased to die when coming across a green patch. One thought I had was making students being chased blue with the follower procedure:
ask students[
set no-threat criminals in-radius 25
ifelse any? no-threat[set color blue][set color black]
]
Then having blue students die when they encounter a green patch. I think I am missing something obvious to get this to work. Any nudge in the right direction would be much appreciated!
FYI, I am also relatively new to asking questions here so all apologize for any forum faux pas
here is the meat of the program I have been working on for reference
'''
to move-to-student
ask criminals[
set candidate students in-radius 25
if any? candidate[
set leader min-one-of candidate [distance myself]
if leader != nobody [ set color red]
;set follower myself
face leader
fd .3
if [pcolor] of patch-ahead 5 = 2.2 [
set heading heading - 100]
]
]
end
to run-from-criminal
ask students[
;while[distance follower > 1][
set threat criminals in-radius 25
if any? threat[
set follower min-one-of threat [distance myself]
if distance follower < 1 [ set number-dead number-dead + 1 die]; set candidate leader set threat follower ] ;students 'die' if criminal gets to close
if threat != nobody [set color blue]
face follower
rt 180 lt random 20 rt random 20 fd .2
if [pcolor] of patch-ahead 5 = 2.2 [
set heading heading - 100]
]]
end
to safe-die
ask students[
if follower = blue and if [pcolor] of patch-here = green [die]
]
end
to revert-leader
ask students[
set no-threat criminals in-radius 25
ifelse any? no-threat[set color blue][set color black]
]
end
to create-new-students
ask students[
if count students < student-count [hatch 1 setxy random-xcor random-ycor]
]
end
I figured it out. Maybe writing it down helped.
Looks like my solution was a combination of a nested if statement (something I've tried and failed at previously) and assigning the agents I want to die to an agent-set. There might be a better way to get this done but I wanted to share the solution I got...
ask students[
if any? no-threat[if [pcolor] of patch-here = green [die]
]

How could I set turtles heading to the destination by go along the road

Recently I have discovered the code that helps the turtles to walk along the road that I have assigned the color which is not black.
let target one-of patches in-cone 1.5 180 with [pcolor != black]
if target != nobody
[
face target
move-to target
]
But my problem is that
When the turtles meet the dead end, it stops right there and not moving anymore.
My turtles already have its own destination to go to (This model is another one). If I use the code that I have mentioned before, maybe it doesn't work. So how could I fix this?
Here is some code that I wrote (Another model).
ask turtles
[
set wlocation one-of patches with [pcolor = pink]
move-to wlocation
set myhome wlocation
]
After this, I will let turtles wander around in the model. Then in some specific time, turtles will go back to its home by the following code. When the turtles reach its home, it will stay still.
ifelse distance myhome > 2
[face myhome fd random 3]
[wander]
to wander
ifelse random 2 = 0 [rt random 30] [lt random 30]
end
Does anyone have any idea or suggestion?

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

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