使用距离变量-机器人割草机模拟项目

问题描述 投票:0回答:2

我正在尝试在 Netlogo 上模拟机器人割草机(就像这个)。 我希望它能在电池电量低时找到回家的路并自行充电。

但是我找不到有效的解决方案,因为我收到错误“预期距离 输入成为代理人,但每次都没有得到“NOBODY”。

我刚刚开始使用 Netlogo 学习,如果有人能帮助我找到解决方案,我会非常高兴。

Interface

谢谢!

breed [cars car]
cars-own [target]

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 [ pycor >= -16  and pycor >= 16] 
  [ set pcolor red ] ;; setup a red frame stopping the lawn mower
    ask patches with [ pycor <= -16  and pycor <= 16]
  [ set pcolor red ]
    ask patches with [ pxcor >= -16  and pxcor >= 16]
  [ set pcolor red ]
    ask patches with [ pxcor <= -16  and pxcor <= 16]
  [ set pcolor red ]
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 ;; to choose obstacles with mouse clicks
  if mouse-down? [
    ask patch mouse-xcor mouse-ycor [ set pcolor red ]
    display
  ]
end


to go
  move-cars
  cut-grass
  check-death ;; Vérify % battery.
  tick
end

to move-cars
  ask cars
  [
    ifelse [pcolor] of patch-ahead 1 = red
      [ lt random-float 360 ]   ;; cant go on red as it is a wall
      [ fd 1 ]                  ;; otherwise go
    set energy energy - 1
]
  tick
end

to cut-grass
  ask cars [
    if pcolor = green [
      set pcolor gray
    ]
  ]
end

to check-death ;; check battery level
  ask cars [
    ifelse energy >= 150
    [set label "energy ok"]
    [if distance target = 0
      [ set target one-of houses
        face target ]
    ;; move towards target.  once the distance is less than 1,
    ;; use move-to to land exactly on the target.
    ifelse distance target < 1
      [ move-to target ]
      [ fd 1 ]
   ]
  ]
end
netlogo
2个回答
1
投票

看起来问题是由于您

setup-cars
在您
setup-houses
之前 - 因此没有
house
可以将新的
car
设置为目标。您可以更改设置调用的顺序,或者可以将
if distance target = 0
更改为
if target = nobody
,或者您可以执行类似以下操作,其中当能量低于 0 时,海龟将选择最近的房屋作为其目标:

to check-death
  ask cars [
    ifelse energy >= 150 
    [ set label "Energy ok" ]
    [ set target min-one-of houses [distance myself]
      face target 
      ifelse distance target < 1
      [ move-to target ]
      [ fd 1 ]
    ]
  ]
end

顺便说一句,如果您计划扩展模型以包含更多割草机,您可能需要将

energy
设置为海龟变量。如果您打算让世界变得更大,您可能还需要稍微更改框架设置以动态缩放 - 例如:

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 
  ]
end

-1
投票

您似乎遇到了与 NetLogo 代码中使用 DISTANCE 函数相关的错误。当 DISTANCE 函数与不存在或尚未正确定义的代理一起使用时,通常会出现错误消息“DISTANCE 预期输入是代理,但得到的是 NOBODY”。

为了帮助您排除故障并找到解决方案,让我们分解错误消息并探讨其发生的可能原因:

Agent Existence: Ensure that the agent you are referencing when using the DISTANCE function exists in your NetLogo model. If you're trying to calculate the distance between your robotic lawn mower and its home base, make sure that the home base agent is correctly defined in your model.

Agent Identification: Verify that the agent being referenced by the DISTANCE function is properly identified and accessible within your code. If you're using variables or identifiers to represent agents, double-check that they are correctly assigned and referenced.

Agent Attributes: Confirm that the agent being used with the DISTANCE function has the necessary attributes or properties required for distance calculation. For instance, if you're attempting to calculate the distance between two agents, ensure that both agents have defined coordinates or positions.

Initialization: Check if all relevant agents and variables are properly initialized before attempting to use them with the DISTANCE function. Initialization ensures that agents have valid values and attributes at the time of calculation.

Error Handling: Implement error handling mechanisms in your code to handle cases where agents or attributes may be missing or undefined. This can help prevent runtime errors and provide informative messages to assist with debugging.

以下是如何实现机器人割草机模拟解决方案的基本示例:

网络标志

breed [割草机 割草机] 割草机自有 [电池级]

设置 全部清除 创建割草机 1 [ setxy 随机-xcor 随机-ycor ;将割草机放置在随机位置 设置电池电量 100 ;将电池电量初始化为满(可以根据需要调整) ] 重置刻度 结束

移动割草机 询问割草机[ ;检查电池电量 如果电池电量 <= 20 [ let home min-one-of patches [distance myself] ; Find the nearest home base (adjust as needed) face home ; Turn towards the home base move-to home ; Move towards the home base set battery-level 100 ; Recharge the battery (you can adjust as needed) ] ; Implement other movement logic for regular mowing behavior here ] end

在此示例中,移动割草机程序包括检查机器人割草机电池电量的逻辑。如果电池电量低于某个阈值(例如 20%),割草机将导航至最近的基地(由补丁表示)进行充电。可以根据您的具体要求和模型结构进行调整以自定义行为。

通过遵循这些步骤并考虑所提供的建议,您应该能够解决错误并开发一个可行的解决方案,用于在 NetLogo 中模拟“机器人割草机”。如果您还有任何疑问或遇到困难,请随时寻求额外帮助!

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.