我正在为在特定路径上行走的人建立模型(道路补丁,颜色=白色)。代理商的移动仅限于白色补丁,当遇到黑色补丁时,它们将旋转180度。
我发现的问题是当我尝试设置代理的标题时。我希望标题走到最近的红色补丁(目的地),但是当我使用时:
代理人不会离开他们的补丁。事实证明,上面代码生成的“标题”的值为-360到360,其中包含小数。我建立的步行程序只允许以90(-360,-180 ... 90,180,270,360)的倍数进行标题。这是我的步行程序的完整代码:使用[pcolor = red] [距离自己]<
将标题设置为min-one-patches
globals [ flagl flagr ]
ask agents [
;reset flag
set flagr 0
set flagl 0
; check and note if there is a path on the left or the right
ask patch-left-and-ahead 90 1 [if (pcolor = white ) [set flagl 1]]
ask patch-right-and-ahead 90 1 [if (pcolor = white ) [set flagr 1]
]
;in T-junction, decide to turn left or right (random)
if((flagl = 1) and (flagr = 1))
[
ifelse random 100 > 50
[set heading heading - 90]
[set heading heading + 90]
]
;if it's only applicable to turn right, then turn right
if((flagl = 0) and (flagr = 1)) [set heading heading + 90]
;if it's only applicable to turn left, then turn right
if((flagl = 1) and (flagr = 0)) [set heading heading - 90]
;return if there's no mor path
if [pcolor] of patch-at-heading-and-distance heading 1 = black [rt 180]
;agent movement
**;face min-one-of patches with [pcolor = red ] [ distance myself ]**
fd 1
;stopping procedure
if [pcolor] of patch-here = red [fd 0]
]]
我试图将#1生成的标题四舍五入:
let direction heading towards min-one-of patches with [pcolor = red ] [ distance myself ]
if direction != mod 90
set direction 90 * round(arah/ 90)
但由于“let”命令无法接受这种输入,因此它会发出错误提示。
这是我构建的地图的屏幕截图: Map
非常感谢任何有助于解决此问题的帮助。谢谢!
答案 0 :(得分:2)
这可能会让你开始 - 只需要很少的检查就可以了,但你可能想检查异常。
cardinal-4
报告器接受任何输入并检查模数是否等于0.如果不是,它将检查模数是否小于/等于45-如果是,则从方向中减去该值(dir
)。如果modulo大于45,则从方向中减去模数并加上90.
check
过程只会打印列出的值的报告输出。
to setup
ca
reset-ticks
end
to-report cardinal-4 [ heading-input ]
let dir heading-input
let remain dir mod 90
if remain != 0 [
ifelse remain <= 45 [
set dir dir - remain
]
[
set dir dir - remain + 90
]
]
report dir
end
修改:改进了check
程序:
to check
let testVals [ -361 -359 -271 -269 -181 -179 -91 -89 -1 0 1 89 91 179 181 269 271 359 360 361 ]
print map [ i -> ( word i " becomes " cardinal-4 i "\n" ) ] testVals
end