Search code examples
netlogo

Using the distance variable - Robotic lawn mower simulation project


I am trying to simulate a robotic lawn mower (like this one) on Netlogo. I would like it finds its way home to recharge itself when the battery is low.

However I cannot find a working solution as I got the error "DISTANCE expected input to be an agent but got NOBODY instead." every time.

I just started studying with Netlogo and would be very happy if someone give me some help finding a solution.

Interface

Thanks !

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

Solution

  • Looks like the issue is due to the fact that you setup-cars before you setup-houses- there is therefore no house for the new car to set as its target. You can change the order of the setup calls, or you can change if distance target = 0 to if target = nobody, or you can do something like the following, where the turtle will just choose the nearest house as its target when energy drops below 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
    

    As a side note, if you plan on expanding the model to include more mowers you may want to make energy a turtle variable. If you plan on making the world bigger, you may also want to change your frame setup slightly to dynamically scale- something like:

    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