让乌龟停下来然后在特殊事件后继续前进 - 机器人割草机模拟项目

Making a turtle stop and then go after a special event - Robotic lawn mower simulation project

我正在尝试在 Netlogo 上模拟机器人割草机。我的第一个目标是它在电量不足时找到回家的路给自己充电。

这已经完成了(感谢 Luke!)但是我不知道如何让割草机在到达房子时停下来(它一直在移动 2 个补丁)。因此,我的能量滑块无限低于零。 我首先想到添加一个事件 "recharge" 并将其放在 中的 "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

您可以结合使用逻辑标志(例如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-carscheck-death 以适应这些更改(旁注 - 您在 gomove-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)。