Netlogo上的寻路 - 机器人割草机模拟项目

时间:2017-11-12 00:26:18

标签: netlogo path-finding

我正在尝试在Netlogo上模拟一台机器人割草机。我的目标是:

  • 在绿色斑块(草)上随机切割它们(绿色变成灰色)。
  • 当电池电量不足时,它找到回家的路。
  • 它不会消失被红色斑块缩小的限定周边(花园),并且用户点击鼠标左右会有障碍物。

我现在有两个第一个目标的工作代码(感谢Luke!)。

然而,在进入to check death步骤之后,让割草机尊重我不会出现红色斑块的情况变得越来越复杂。 事实上,它似乎没有考虑我在to move-cars步骤中的先前订单。

有些人告诉我,如果我想让它围绕墙壁导航,它实际上非常复杂,需要某种寻路。

通过我的研究,我找到了一些关于所谓的A *的文档。 我很好地理解了算法的概念,但它在Netlogo上的翻译对我来说非常困难。到目前为止,我没有课程的做法非常基础,基于现有的代码并遵循我的建议。但是从现在开始,为我的代码调整A *会让我失去理智。我希望通过了解如何在我的代码中实现路径查找来最终理解Netlogo的复杂性。 我知道有些人不想详细解释它是如何运作的。我只希望找到一个有耐心的人向我解释一下A *如何与一步一步的工作。

非常感谢

这是我的代码:

breed [cars car]
cars-own [target charging? charge-time car-energy]

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 [
    pxcor = max-pxcor or
    pxcor = min-pxcor or
    pycor = max-pycor or
    pycor = min-pycor ] [
    set pcolor red ;; Setup garden perimeter
  ]
end

to setup-cars

  set-default-shape cars "car"
create-cars 1 [
    setxy 8 8 ;; Place the lawn mower near the house
    set target one-of houses
    set charging? false
    set car-energy energy
  ]
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 ;; Permits the user to place red patches as new obstacles in garden.
  ]
end


to go
  move-cars
  cut-grass
  check-death ;; Verify % battery.
  tick
end

to move-cars
  ask cars [
    ifelse charging? [
      set charge-time charge-time + 1 ;; Counts the time spent to charge
      if charge-time > 15 [ ;; Defined charge time here
        set car-energy 1000
        set charging? false
        set charge-time 0
      ]
    ] [
      ifelse [pcolor] of patch-ahead 1 = red
      [ lt random-float 360 ]   ;; See a red patch ahead : turn left randomly
      [ fd 1 ]                  ;; Otherwise, its safe to go foward.
      set car-energy car-energy - 1
    ]
  ]
end

to cut-grass
  ask cars [
    if pcolor = green [ ;; cut grass (green patches go grey)
      set pcolor gray
    ]
  ]
end

to check-death ;; checking battery level
  ask cars [
    ifelse car-energy >= 300
    [ 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
        set label "Charging"
      ]
      [ fd 1 ]
    ]
  ]
end

enter image description here

0 个答案:

没有答案