首页 > 解决方案 > 在 netlogo 中,乌龟走到墙而不是门

问题描述

我编写了一个将海龟疏散到唯一出口的代码。但正如我注意到的那样,海龟分为几组。有海龟向出口门走去,其他的则走到最近的墙边。我想创建一个模拟,所有的海龟都会去出口。这是我的代码。有人可以建议吗?

globals [
  ;wall
  door
  goal-x goal-y
  n
]

patches-own [ path? ]

turtles-own [
  direction
  fast?
  other-nearby
]

to setup
  clear-all
  setup-patches
  set-default-shape turtles "person"
  ask n-of number-room1 patches with [pxcor < 47 and pxcor > 0 and pycor < 45 and pycor > 0 and pcolor = black]
  [
    sprout 1 [
      facexy 41 45
      set direction 1
      set color yellow
    ]
  ]
  reset-ticks
end

to setup-patches
  draw-wall
  draw-exit
  
  ask patch 21 41 [
    set plabel-color white
    set plabel "Room1"
  ]
end

to draw-wall
  ask patches with [pxcor <= 38 and pxcor >= 0 and pycor = 45]
    [set pcolor blue]
  ask patches with [pxcor <= 47 and pxcor >= 44 and pycor = 45]
    [set pcolor blue]
  ask patches with [pxcor <= 47 and pxcor >= 0 and pycor = 0]
    [set pcolor blue]
  ask patches with [pxcor = 0 and pycor <= 45 and pycor >= 0]
    [set pcolor blue]
  ask patches with [pxcor = 47 and pycor <= 45 and pycor >= 0]
    [set pcolor blue]
end

to draw-exit
  ask patches with [pxcor <= 43 and pxcor >= 39 and pycor = 45]
    [set pcolor green]
end

to go
  if ticks >= 100 [stop]
  ask turtles [walk]
  tick
end

to walk
  let room1 patches with [pxcor < 47 and pxcor > 0 and pycor < 45 and pycor > 0]
  ;let exitpoint patches with [pxcor < 46 and pxcor > 33 and pycor < 45 and pycor > 32]
  
  ask turtles-on room1
  [face one-of patches with [pxcor <= 43 and pxcor >= 39 and pycor = 47]
    fd 0.01
  ]
  ;ask turtles-on exitpoint
  ;[face one-of patches with [pxcor <= 42 and pxcor >= 38 and pycor = 47]
  ;  fd 0.01
  ;]
end

标签: netlogo

解决方案


您可以尝试以下 2 个步骤来使其工作:

  1. 检查你的模拟设置(除了右上角的模拟速度滑块),确保取消选中 world wrap Horizo​​ntal 和 world wrap vertical。如果仍然勾选,则表示海龟可以越过你的垂直/水平边界,出现在另一边。这就是为什么一些海龟正在向墙壁移动的原因,他们假设他们可以通过边界并到达出口,而由于您的代码限制,他们会被卡在那里。

  2. 确定设置没问题后,如果你还有一些卡在靠近出口的墙上,你可以尝试用下面的代码替换你的to walk代码:

    to walk
       set heading towards one-of patches with [pcolor = green]
       fd 1
    end
    

希望这将帮助您解决问题


推荐阅读