我正在尝试在Netlogo上模拟一台机器人割草机。我的第一个目标是,当电池电量不足时,它找到回家的路。
现在已经完成了(感谢Luke!)但我无法想象如何让割草机在到达房屋时停下来(它会不停地移动2个补丁)。因此,我的能量滑块无限低于零。 我首先考虑添加一个事件“充电”并将其放在中的“check death”之后,使用 if 实例进行。但是:
然后我希望它在即时充电后重新开始工作。
有什么想法吗?
以下是代码:
breed [cars car]
cars-own [target]
breed [houses house]
to setup
clear-all
setup-patches
setup-cars ;;represent lawn mower
setup-house
reset-ticks
end
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 ;; Setup the borders of the garden
]
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
if mouse-down? [
ask patch mouse-xcor mouse-ycor [ set pcolor red ]
display
]
end
to go
move-cars
cut-grass
check-death ;; Check % battery.
tick
end
to move-cars
ask cars
[
ifelse [pcolor] of patch-ahead 1 = red
[ lt random-float 360 ] ;; see red patch ahead turn left.
[ fd 1 ] ;; otherwise it is ok to go.
set energy energy - 1
]
tick
end
to cut-grass
ask cars [
if pcolor = green [
set pcolor gray
]
]
end
to check-death ;; when low energy lawn mower will go back to house
ask cars [
ifelse energy >= 150
[ set label "Energy ok" ]
[ set label "Low Energy, returning to base"
set target min-one-of houses [distance myself]
face target
ifelse distance target < 1
[ move-to target ]
[ fd 1 ]
]
]
end
答案 0 :(得分:1)
您可以将逻辑标志(例如charging?
)与计数器(例如charge-time
)结合使用来执行此操作。尝试修改这样的cars-own
定义:
cars-own [target charging? charge-time]
和setup-cars
是这样的:
to setup-cars
create-cars 1 [
setxy 8 8
set target one-of houses
set charging? false
]
end
然后你可以让割草机根据charging?
是真还是假来做不同的事情。尝试修改move-cars
和check-death
以适应这些变化(旁注 - 您在go
和move-cars
都勾选了一个勾号:
to move-cars
ask cars [
ifelse charging? [
set charge-time charge-time + 1
if charge-time > 14 [
set energy 200
set charging? false
set charge-time 0
]
] [
ifelse [pcolor] of patch-ahead 1 = red
[ lt random-float 360 ] ;; see red patch ahead turn left.
[ fd 1 ] ;; otherwise it is ok to go.
set energy energy - 1
]
]
end
to check-death ;; when low energy lawn mower will go back to house
ask cars [
ifelse energy >= 150
[ set label "Energy ok" ]
[ set label "Low Energy, returning to base"
set target min-one-of houses [distance myself]
face target
ifelse distance target < 1
[ move-to target
set charging? true
]
[ fd 1 ]
]
]
end
我建议您更改energy
像car-energy
这样的海龟变量,以便海龟值与滑块无关。这意味着您可以拥有多个割草机,一旦充电,他们可以将能量水平重置为滑块设置!你只需要包含一行
set car-energy energy
在setup-cars
中(然后进行修改,以便您的汽车程序中的energy
代替car-energy
)。