我在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上的第一个帖子/问题!我希望我的问题(和我的提问方式)不是很糟糕。
发布于 2013-04-14 00:02:57
这是我能想到的最简单的工作版本:
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除了我完全重写了你的设置过程之外,它与你自己的版本没有太大的不同。我认为你的主要问题是在转向之前后退:因为你在下一个go周期开始时再次face dest,你的rt基本上是无用的。
https://stackoverflow.com/questions/15987352
复制相似问题