使用距离变量 - 机器人割草机仿真项目
Using the distance variable - Robotic lawn mower simulation project
我正在尝试在 Netlogo 上模拟机器人割草机 (like this one)。
我希望它能在电量不足时找到回家的路给自己充电。
但是我找不到有效的解决方案,因为我收到错误“DISTANCE expected
输入成为代理人却得到了 NOBODY。”每次。
我刚开始学习 Netlogo,如果有人能帮助我找到解决方案,我会很高兴。
Interface
谢谢!
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
看起来问题是由于您 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
我正在尝试在 Netlogo 上模拟机器人割草机 (like this one)。 我希望它能在电量不足时找到回家的路给自己充电。
但是我找不到有效的解决方案,因为我收到错误“DISTANCE expected 输入成为代理人却得到了 NOBODY。”每次。
我刚开始学习 Netlogo,如果有人能帮助我找到解决方案,我会很高兴。
Interface
谢谢!
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
看起来问题是由于您 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