我正在从一个楼层对疏散行为进行建模,我编写了代码,但是Netlogo并没有出现任何错误,但是我不知道为什么我的模型不起作用。
to go
ask turtles with [pcolor = yellow] [walk]
tick
end
to walk
;room1
ask turtles with [pxcor < 71 and pycor < 79 and pxcor > 0 and pycor > 50]
[ face one-of patches with [pxcor <= 58.57 and pxcor >= 49.42 and pycor = 50]]
;room2
ask turtles with [pxcor < 112 and pycor < 79 and pxcor > 71 and pycor > 50]
[ face one-of patches with [pxcor <= 108.57 and pxcor >= 99.42 and pycor = 50]]
;room3
ask turtles with [pxcor < 82 and pycor < 36 and pxcor > 0 and pycor > 0]
[face one-of patches with [pxcor <= 79.575 and pxcor >= 70.425 and pycor = 36]]
;room4
ask turtles with [pxcor < 132 and pycor < 36 and pxcor > 82 and pycor > 0]
[face one-of patches with [pxcor <= 93.575 and pxcor >= 84.425 and pycor = 36]]
fd 0.5
;corridor
ask turtles with [pxcor < 132 and pycor < 50 and pxcor > 0 and pycor > 36]
[face one-of patches with [pxcor <= 132 and pxcor >= 112 and pycor = 50]]
;exit
ask turtles with [pxcor < 132 and pycor < 79 and pxcor >= 112 and pycor >= 50]
[face one-of patches with [pxcor <= 129.57 and pxcor >= 120.42 and pycor = 79]]
fd 1
end
答案 0 :(得分:1)
您的问题不是特别清楚,因为“不起作用”不能提供有关该问题的足够信息。为了获得更有用的答案,您应该描述您期望的行为和实际获得的行为。话虽如此,但是您的代码中存在一个明显的问题,这可能正是您要问的。
在go语句中,您有turtles
调用walk过程。在步行过程中,face
命令位于ask turtles
括号内,而fd
命令位于括号之内。我假设这是有意的,并且您要尝试做的是让四个if
语句使turtle
朝向您想要的方向,然后fd
将其移动。
但是,在步行过程中,您要重复ask turtles
。您可能只想改变一只乌龟(被要求行走的一只乌龟)的方向。
to go
ask turtles with [pcolor = yellow] [walk]
tick
end
to walk
;room1
if pxcor < 71 and pycor < 79 and pxcor > 0 and pycor > 50
[ face one-of patches with [pxcor <= 58.57 and pxcor >= 49.42 and pycor = 50]
]
;room2
if pxcor < 112 and pycor < 79 and pxcor > 71 and pycor > 50
[ face one-of patches with [pxcor <= 108.57 and pxcor >= 99.42 and pycor = 50]
]
;room3
if pxcor < 82 and pycor < 36 and pxcor > 0 and pycor > 0
[ face one-of patches with [pxcor <= 79.575 and pxcor >= 70.425 and pycor = 36]
]
;room4
if pxcor < 132 and pycor < 36 and pxcor > 82 and pycor > 0
[ face one-of patches with [pxcor <= 93.575 and pxcor >= 84.425 and pycor = 36]
]
fd 0.5
;corridor
if pxcor < 132 and pycor < 50 and pxcor > 0 and pycor > 36
[ face one-of patches with [pxcor <= 132 and pxcor >= 112 and pycor = 50]
]
;exit
if pxcor < 132 and pycor < 79 and pxcor >= 112 and pycor >= 50
[face one-of patches with [pxcor <= 129.57 and pxcor >= 120.42 and pycor = 79]
]
fd 1
end
此代码中还有其他一些问题。首先是您似乎混用了xcor
和pxcor
(对于y坐标也是如此)。 xcor
是乌龟坐标,pxcor
是面块坐标。乌龟确实有权访问补丁的变量,包括坐标,因此代码不会产生错误。但是我对诸如face one-of patches with [pxcor <= 108.57 and pxcor >= 99.42 and pycor = 50]
之类的声明感到担忧。这等效于face one-of patches with [pxcor <= 108 and pxcor >= 100 and pycor = 50]
,因为pxcor
和pycor
是整数。您在其中使用小数的事实使我认为您对坐标感到困惑。
第二个是所有被要求行走的海龟都将向前移动1.5,但我认为您希望某些移动0.5,其他一些移动1。一旦海龟进入行走程序,{{1} }语句(或您的版本中的if
)仅更改标题。 with
语句适用于所有海龟。
第三,我很想使用fd
来简化此代码的可读性。像这样:
patch-sets
请注意,这与您的不一样,因为我不知道哪些补丁是黄色的。您必须进行修改才能解决此问题。
我在过程中移动to go
ask turtles with [pcolor = yellow] [walk]
tick
end
to walk
let room1 patches with [pxcor < 71 and pycor < 79 and pxcor > 0 and pycor > 50]
let room2 patches with [pxcor < 112 and pycor < 79 and pxcor > 71 and pycor > 50]
let room3 patches with [pxcor < 82 and pycor < 36 and pxcor > 0 and pycor > 0]
let room4 patches with [pxcor < 132 and pycor < 36 and pxcor > 82 and pycor > 0]
let corridor patches with [pxcor < 132 and pycor < 50 and pxcor > 0 and pycor > 36]
let exitpoint patches with [pxcor < 132 and pycor < 79 and pxcor >= 112 and pycor >= 50]
ask turtles-on room1
[ face one-of patches with [pxcor <= 58 and pxcor >= 50 and pycor = 50
fd 0.5
]
ask turtles-on room2
[ face one-of patches with [pxcor <= 108 and pxcor >= 100 and pycor = 50
fd 0.5
]
ask turtles-on room3
[ face one-of patches with [pxcor <= 79 and pxcor >= 71 and pycor = 36
fd 0.5
]
ask turtles-on room4
[ face one-of patches with [pxcor <= 93 and pxcor >= 85 and pycor = 36
fd 0.5
]
ask turtles on corridor
[ face one-of patches with [pxcor <= 132 and pxcor >= 112 and pycor = 50]
fd 1
]
ask turtles-on exitpoint
[ face one-of patches with [pxcor <= 129 and pxcor >= 121 and pycor = 79]
fd 1
]
end
的原因是补丁集仅创建了一次。如果您打算在其他地方使用这些房间等,那么将它们作为全局变量并将结构放入设置中是值得的。