How to move a turtle to the nearest colored patch which isn't occupied?

53 Views Asked by At

I am trying to build a drone model in which the drones that have a battery level and moves to different white patches that acts a coverage points and moves to the center/house for recharging. Currently the drones move to a random patch and stays there until they need to recharge, however the drones are stacking upon each other and I want them to move to a white patch that doesn't already have another drone on it.

I tried with the following check in my go procedure.

set target one-of patches with [pcolor = white and not any? people-here] face target

But the drones are still stacking upon each and I am not sure what to do even after looking at similar posts.

This is my complete code:

breed [people person]
people-own [target energy]

breed [houses house]


to setup
  clear-all
  set-default-shape houses "house"
  setup-patch
  ;; Create a house in the middle
  create-houses 1 [
    setxy 0 0
    set size 2
  ]

  create-people number-of-people [
    setxy random-xcor random-ycor
    set target one-of houses
    set energy 100
    face target
    set color blue
  ]
  reset-ticks
end

to setup-patch
  let white-patch-count 0

  ask patches [
    set pcolor green
    if random-float 1 < 0.2 and white-patch-count < number-of-patches [
      if not any? other patches in-radius radius-size with [pcolor = white] [
        set pcolor white
        set white-patch-count white-patch-count + 1
      ]
    ]
  ]
end

to go
  ask people [
    ;; Decrease energy level for each tick
    set energy energy - 1

    if energy <= 0 [
      die
    ]

    ;; If at target, choose a new random target
    if distance target = 0 [
      ifelse pcolor = white [
        ifelse energy > energy-level [
          stay-at-patch
        ] [
          set target one-of houses
          face target
        ]
      ] [
        
        set target one-of patches with [pcolor = white and not any? people-here]
        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
      if any? houses-here [set energy 100]
    ] [
      fd 1
    ]
  ]
  tick
end

to stay-at-patch
  ;; Implement the behavior for staying at the current patch
  ;; Keep staying until the energy level is back at 30
  if energy = 30 [
    set target one-of houses
    face target
  ]
end

Please ignore that turtles/agents are called people. I will rename it once I get it to work.

#UPDATE

I managed to make some changes that allows drones to move to other patches however it seems like that it also is forcing existing drones to relocate and doesn't always makes the drones stay in place if the patch is free:

The updated code:

ifelse distance target < 1 [
      ifelse any? other people-here and not any? houses-here[
        set target one-of patches with [pcolor = white and not any? people-here]
        face target]
      [
      move-to target
      if any? houses-here[ set energy 100]
    ]
    ] [
      fd 1
    ]
1

There are 1 best solutions below

3
Pol On

I've been trying to solve the problem using your code but I couldn't achieve that. However, I'm quite sure the problem is the way you are defining target.

Your code needs some kind of restriction which avoids two or more drones from being in the same target patch.

Therefore, I think about 2 alternatives:

  1. Make sure your drones are updating the target all the time, so they will choose another one if the patch they had chosen has just been reached by another drone.

  2. When defining the target, ensure a drone can't choose a patch which has been already set as target by another drone.

Hope this can help you!