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

问题描述:

我想模拟Netlogo上的机器人割草机(like this one)。 我希望它找到回家的路,当电池电量不足时自行充电。使用距离变量 - 机器人割草机模拟项目

但是我找不到工作解决方案,因为我收到错误“DISTANCE expected input is an agent,but got NOBODY instead。”每次。

我刚刚开始学习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 

貌似这个问题是由于事实,你setup-carssetup-houses之前 - 也因此没有housecar设定为目标。您可以更改设置调用的顺序,也可以更改if distance target = 0if 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 
+0

非常感谢您的帮助! 我刚刚发现,现在回家时没有考虑到红色的“墙壁”了。任何想法来解决这个问题?我想这是因为车削代码不是在“检查死亡”,而是没有找到解决方案的线索。 另外我现在正试图让机器人在到达家时停下来并重新充电,然后重新开始工作。你有想法吗? –

+1

是的,你说得对,它并没有检查代码修复功能,使它在墙壁上导航实际上非常复杂,需要进行某种寻路。最好为您的两次后续活动发布新问题! –

+0

再次感谢! 好主意,我只是读了一些关于寻路的信息,看起来确实很复杂。 –