代码之家  ›  专栏  ›  技术社区  ›  Gannicus

Netlogo,创建避障算法

  •  4
  • Gannicus  · 技术社区  · 11 年前

    我正在NetLogo中模拟行人的运动,并且在从头开始创建避障算法时遇到了困难。网上有算法,但它们不适合移动障碍物(其他行人)。此外,我的经纪人正在从他们的产卵点(A点)转移到他们的目标(B点)。

    这是我的NetLogo算法:

    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
    

    此模拟的模拟环境是一个交叉点:

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

    (对不起,我需要10名代表才能发布图片:()

    图1显示了设置后的环境状态。图2显示了代理移动到他们的目标(目标!=他们的产卵点)后会发生什么。面对不同方向的特工显示了那些穿过中心杂乱特工的特工,现在正朝着目标前进的特工。然而,由于我的算法,中心的代理被困在那里。当有更多数量的代理时,模拟就更有问题了,这意味着它们只会在环境的中心杂乱无章,在移动时会断断续续。

    我的算法基于 http://files.bookboon.com/ai/Vision-Cone-Example-2.html 。原谅我的算法,我一周前开始在NetLogo中编程,直到现在我仍然没有在其中编程的正确心态。我相信有更好的方法来实现我心中的想法,但遗憾的是,我在尝试许多我脑海中的实现时感到沮丧(但从未接近真实的东西)。

    附言:这是我在StackOverflow的第一篇帖子/问题!我希望我的问题(以及我的提问方式)不会太糟糕。

    1 回复  |  直到 11 年前
        1
  •  1
  •   Nicolas Payette    11 年前

    以下是我能想到的最简单的工作版本:

    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
    

    除了我完全重写了您的设置过程之外,它与您自己的版本没有太大区别。我认为你的主要问题是在转弯前倒车:因为你 face dest 在下一个开始时再次 go 循环,你的 rt 基本上没用。

    推荐文章