我正在尝试在Netlogo上模拟机器人割草机(like this one)。 当电池电量不足时,我希望它找到回家的路。
然而,当我收到错误“DISTANCE expect”时,我找不到可行的解决方案 输入是一个代理人,但却得到了NOBODY。“每一次。
我刚开始使用Netlogo学习,如果有人帮我找到解决方案,我会非常高兴。
谢谢!
breed [cars car]
cars-own [target]
breed [houses house]
to setup
clear-all
setup-patches
setup-cars
setup-house
reset-ticks
end
to setup-patches
ask patches [set pcolor green] ;;Setup grass patches
ask patches with [ pycor >= -16 and pycor >= 16]
[ set pcolor red ] ;; setup a red frame stopping the lawn mower
ask patches with [ pycor <= -16 and pycor <= 16]
[ set pcolor red ]
ask patches with [ pxcor >= -16 and pxcor >= 16]
[ set pcolor red ]
ask patches with [ pxcor <= -16 and pxcor <= 16]
[ set pcolor red ]
end
to setup-cars
create-cars 1 [
setxy 8 8
set target one-of houses
]
end
to setup-house
set-default-shape houses "house"
ask patch 7 8 [sprout-houses 1]
end
to place-walls ;; to choose obstacles with mouse clicks
if mouse-down? [
ask patch mouse-xcor mouse-ycor [ set pcolor red ]
display
]
end
to go
move-cars
cut-grass
check-death ;; Vérify % battery.
tick
end
to move-cars
ask cars
[
ifelse [pcolor] of patch-ahead 1 = red
[ lt random-float 360 ] ;; cant go on red as it is a wall
[ fd 1 ] ;; otherwise go
set energy energy - 1
]
tick
end
to cut-grass
ask cars [
if pcolor = green [
set pcolor gray
]
]
end
to check-death ;; check battery level
ask cars [
ifelse energy >= 150
[set label "energy ok"]
[if distance target = 0
[ set target one-of houses
face target ]
;; move towards target. once the distance is less than 1,
;; use move-to to land exactly on the target.
ifelse distance target < 1
[ move-to target ]
[ fd 1 ]
]
]
end
答案 0 :(得分:1)
看起来这个问题是由于您setup-cars
之前setup-houses
- 因此新house
没有car
设置为其目标。您可以更改设置调用的顺序,也可以将if distance target = 0
更改为if target = nobody
,或者您可以执行以下操作,当能量下降时,乌龟将选择最近的房屋作为其目标低于0:
to check-death
ask cars [
ifelse energy >= 150
[ set label "Energy ok" ]
[ set target min-one-of houses [distance myself]
face target
ifelse distance target < 1
[ move-to target ]
[ fd 1 ]
]
]
end
作为旁注,如果您计划扩展模型以包含更多割草机,您可能希望将energy
变为乌龟变量。如果您打算让世界更大,您可能还需要稍微更改框架设置以动态缩放 - 例如:
to setup-patches
ask patches [set pcolor green] ;;Setup grass patches
ask patches with [
pxcor = max-pxcor or
pxcor = min-pxcor or
pycor = max-pycor or
pycor = min-pycor ] [
set pcolor red
]
end