juliana
juliana

Reputation: 101

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

Upvotes: 0

Views: 404

Answers (1)

TurtleZero
TurtleZero

Reputation: 1064

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!

Upvotes: 1

Related Questions