制作一只乌龟然后去特殊活动 - 机器人割草机模拟项目

时间:2017-10-30 11:32:22

标签: netlogo

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

现在已经完成了(感谢Luke!)但我无法想象如何让割草机在到达房屋时停下来(它会不停地移动2个补丁)。因此,我的能量滑块无限低于零。 我首先考虑添加一个事件“充电”并将其放在中的“check death”之后,使用 if 实例进行。但是:

  • 我怎么说如果汽车进入特定补丁:停止并获得能量?我应该为房子使用特殊的补丁颜色,然后编码如果汽车进入蓝色补丁.....
  • 可以在结束时添加充值事件元素可能会产生冲突,因为只有在充电不足时再充电?将它置于检查死亡是一个解决方案吗?

然后我希望它在即时充电后重新开始工作。

enter image description here

有什么想法吗?

以下是代码:

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

1 个答案:

答案 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-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

我建议您更改energycar-energy这样的海龟变量,以便海龟值与滑块无关。这意味着您可以拥有多个割草机,一旦充电,他们可以将能量水平重置为滑块设置!你只需要包含一行

set car-energy energy

setup-cars中(然后进行修改,以便您的汽车程序中的energy代替car-energy)。