问题描述
我正在尝试在Netlogo上模拟机器人割草机.我的第一个目标是当电池电量不足时,它找到回家充电的方式.
I am trying to simulate a robotic lawn mower on Netlogo. My first goal was it finds its way home to recharge itself when the battery is low.
现在已经完成了(感谢卢克!),但是我不知道如何使割草机到达房屋时停下来(它会不断移动2个斑块).因此,我的能量滑块会无限地降到零以下.我首先想到的是添加一个事件"recharge",并将其放置在 if 实例的"去"中的检查死亡"之后.但是:
This is now done (thanks Luke !) but I can't figure how to make the lawnmower stop when it reaches the house (it keeps moving 2 patches around). Therefore my energy slider goes infinitely below zero.I first thought of adding an event "recharge" and placing it after "check death" in to go with a if instance. But :
- 我怎么说如果汽车行驶在特定的地方:停车并获取能量?我是否应该为房屋使用特殊的色块颜色,然后在汽车上使用蓝色色块时代码 ..... 吗?
- 是否可以在 go 元素的末尾添加充电事件,可能会产生冲突,因为仅在电池电量不足时才可以充电?将其置于死亡检查中将是一个解决方案吗?
- How do I say if car goes on specific patch : stop and get energy ? Should I use a special patch color for the house and then code if car goes on blue patch..... ?
- Could adding the recharge event at the end of the to go elements would possibly create a conflict because recharge only goes when on low battery ? Would putting it in check death be a solution ?
然后,我希望它在即时充电后恢复工作.
Then I would like it to go back to work after instant recharge.
有什么想法吗?
这是代码:
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
推荐答案
您可以将逻辑标志(例如charging?
)与计数器(例如charge-time
)结合使用.尝试像这样修改您的cars-own
定义:
You can use a logical flag (eg charging?
) in conjunction with a counter (eg charge-time
) to do this. Try modifying your cars-own
definitions like this:
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
中都打了勾):
Then you can have the mower do different things based on whether charging?
is true or false. Try modifying your move-cars
and check-death
to accommodate these changes (side note- you've got a tick in both go
and 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
这样的乌龟变量,以便乌龟值独立于滑块.这意味着您可能拥有多台割草机,一旦充电,它们就可以将其能量级别重置为滑块设置!您只需添加
I suggest you change energy
a turtle variable like car-energy
so that the turtles values are independent of your slider. That would mean you could have multiple mowers and they could reset their energy level to the slider setting once they are charged! You would just include a line like
set car-energy energy
(然后进行修改,以使汽车程序中的energy
改为car-energy
).
in your setup-cars
(and then modify so that energy
in your car procedures is instead car-energy
).
这篇关于停下乌龟,然后再进行特殊活动-机器人割草机模拟项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!