Question

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:

http://imgur.com/nQzhA7g,R5ZYJrp#0

(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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top