4

I am simulating pedestrian motion in NetLogo, and am having trouble creating an obstacle avoidance algorithm from scratch. There are algorithms online but they are not suited for moving obstacles (other pedestrians). In addition, my agents are moving from their spawnpoint (point A) to their goal (point B).

This is my NetLogo algorithm:

globals [ wall walkway center dest ]
turtles-own [ gender goal velocity spawnpoint mid turn ]

to setup
  clear-all
  ask patches[
set wall patches with [
  (pxcor > 3 and pycor > 3) or 
  (pxcor < -3 and pycor > 3) or
  (pxcor < -3 and pycor < -3) or
  (pxcor > 3 and pycor < -3)
  ]
set walkway patches with [
  (pxcor > -4 and pxcor < 4) or
  (pycor > -4 and pycor < 4) 
]

set center patch 0 0
  ]

  ask patches [
set pcolor black
  ]

  ask walkway [
set pcolor 9
  ]

  crt population [
set velocity 0.1
set mid 0

set gender random 2
if gender = 0 [set color red]
if gender = 1 [set color blue]

set spawnpoint random 4
if spawnpoint = 0 [ move-to one-of walkway with [not any? turtles-here and (pxcor < -11)]]
if spawnpoint = 1 [ move-to one-of walkway with [not any? turtles-here and (pycor > 11)]]
if spawnpoint = 2 [ move-to one-of walkway with [not any? turtles-here and (pxcor > 11)]]
if spawnpoint = 3 [ move-to one-of walkway with [not any? turtles-here and (pycor < -11)]]

set goal random 4
while [ goal = spawnpoint ] [ set goal random 4 ]
if spawnpoint != 0 and goal = 0 [set goal patch -16 0]
if spawnpoint != 1 and goal = 1 [set goal patch 0 16]
if spawnpoint != 2 and goal = 2 [set goal patch 16 0]
if spawnpoint != 3 and goal = 3 [set goal patch 0 -16]
  ]

  reset-ticks

end
to decelerate
  ifelse velocity > 0.01
  [ set velocity velocity - 0.01 ]
  [ rt 5 ]
end

to accelerate
   if velocity < 0.1
   [ set velocity velocity + 0.01 ]
end

to go 

  ask turtles [
   ifelse patch-here != goal[
     set turn random 2
     if distance center < 3 [ set mid 1]
     if mid = 0 [ set dest center ]
     if mid = 1 [ set dest goal ]
     face dest
     ifelse any? other turtles-on patches in-cone 1.5 60
       [ if any? other turtles-on patches in-cone 1.5 60
         [ bk velocity
           rt 90 ]  ]
       [ accelerate
         face dest
         fd velocity ]
  ]
  [ die ]

  ]
end

The simulated environment of this simulation is an intersection:

https://i.stack.imgur.com/Jp7cO.png

(sorry, I need 10 rep to post images :( )

Image 1 shows the state of the environment after setup. Image 2 shows what happens after the agents move to their goal (goal != their spawnpoint). The agents facing the different directions shows the agents which made its way past the clutter of agents in the center and are now on the way to their goal. The agents in the center, however, are stuck there because of my algorithm. The simulation is more problematic when there are more number of agents, which means they will just clutter in the center of the environment and just stutter when moving.

I based my algorithm on http://files.bookboon.com/ai/Vision-Cone-Example-2.html . Forgive my algorithm, I started programming in NetLogo a week ago and until now I still don't have the proper mindset in programming in it. I'm sure there's a better way to implement what I have in mind, but alas I am frustrated upon trying many implementations that came to my mind (but never got close to the real thing).

P.S: This is my first post/question in StackOverflow! I hope my question (and my way of asking) isn't bad.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Gannicus
  • 530
  • 1
  • 7
  • 26

1 Answers1

1

Here is the simplest working version I could come up with:

turtles-own [ goal dest velocity ]

to setup
  clear-all
  let walkway-color white - 1
  ask patches [
    set pcolor ifelse-value (abs pxcor < 4 or abs pycor < 4) [ walkway-color ] [ black ]
  ]
  let goals (patch-set patch -16 0 patch 0 16 patch 16 0 patch 0 -16)
  ask n-of population patches with [ pcolor = walkway-color and distance patch 0 0 > 10 ] [
    sprout 1 [
      set velocity 0.1
      set color one-of [ red blue ] ; representing gender
      set dest patch 0 0 ; first head towards center
      set goal one-of goals with [ distance myself > 10 ]
    ]
  ]
  reset-ticks  
end

to go 
  ask turtles [
    if patch-here = goal [ die ] ; the rest will not execute
    if dest = patch 0 0 and distance patch 0 0 < 3 [ set dest goal ]
    face dest
    if-else any? other turtles in-cone 1.5 60
      [ rt 5
        bk velocity ]
      [ fd velocity ]
  ]
  tick
end

Aside from the fact that I completely rewrote your setup procedure, it's not that different from your own version. I think your main problem was backing before turning: since you face dest again at the beginning of the next go cycle, your rt was basically useless.

Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37
  • Thank you for your taking the time to write an algorithm for my problem, but I failed to mention that the black colored patches are walls, so the only area the agents can travel on are the white patches. The simulation that I am working on is pedestrian movement on an infrastructure with an intersection (and the possible addition of a roundabout to improve pedestrian motion in such situation). P.S: I really appreciate you re-writing the code I have posted, I am learning a lot from your revisions of my code. – Gannicus Apr 13 '13 at 16:34
  • I figured that wall avoidance was something you would need to add at some point, but I wanted to give you a base upon which to build. Hint: `any? patches with [ pcolor = black ] in-cone 1.5 60` will tell you if there is a wall in the cone of vision. Always turning right may not be sophisticated enough for that, however, so you probably need to be smarter about the direction. The [`towards`](http://ccl.northwestern.edu/netlogo/docs/dictionary.html#towards) primitive should help you achieve that. Let us know what you come up with... – Nicolas Payette Apr 13 '13 at 16:48
  • Thank you! I'll try building up on this. I still can't get over the fact that a NetLogo developer answered my question :) I really appreciate your help. I will update this post with the algorithm that I'll come up with. – Gannicus Apr 13 '13 at 17:01
  • I posted an algorithm that I came up with, but is my idea of implementing obstacle avoidance acceptable? – Gannicus Apr 14 '13 at 14:30