'How to give property to agents Netlogo

I´m setting up some turtles randomly on the the map, then they have to change the color of the patches to show that they cultivated however some times they overlap and change the color of other turtles. To solve this i asked not any? other turtles in-radius 6 to force them to be apart, but this is not elegant nor efficient. What would be a better way to give each turtle their own patch to make a more populated world.

to setup 
  ca
  resize-world 0 100 0 100
create-turtles 50

  [ set size 1
    set color 135
    setxy random-xcor random-ycor
    move-to one-of patches with [not any? other turtles in-radius 6]
    ask patches in-radius (2 + random 2) [set pcolor 35] 
  ]
end

to go 
  
ask turtles[ ask patches in-radius 4  with [pcolor = 35]; falta representar las cosechas 4-2-3-3
    [ set pcolor 42]]
  
end

The go is using the fact that there are no turtles in a radius of 6, but if i wanted to get them closer or change the color on different ticks this would be bad.

Thanks. If more is need please let me know.



Solution 1:[1]

You can give the turtles a new turtles-variable that you can assign a patch-set to. This is done by using turtles-own at the start of your program. The turtle then remembers which patches were assigned to this variable and can access them again at a later time. This way, when it comes time to change patch colors, they only change the colors of patches within their original radius and not of the patches in the extended radius.

turtles-own [territory]

to setup 
  ca
  resize-world 0 100 0 100
create-turtles 50

  [ set size 1
    set color 135
    setxy random-xcor random-ycor
    set territory patches in-radius (2 + random 2)
    ask territory [set pcolor 35] 
  ]
  
  
  
end

to go 
  
  ask turtles [ ask territory ; falta representar las cosechas 4-2-3-3
    [ set pcolor 42]]
  
end

As an alternative solution, here it is not the turtles that remember which patch they own, but the patches that remember which turtle owns them. In the case I use here, patches can only be owned by a single turtle. Here, I give each patch a patch-variable using patches-own. I then let the turtles tell the patches within their radius to choose that turtle as their owner ask patches ... [set owner myself]. Myself is a reporter that refers not to the patch carrying out the command, but to the turtle that asked the patch to carry out the command.

patches-own [owner]

to setup 
  ca
  resize-world 0 100 0 100
  
  create-turtles 50 [
    set size 1
    set color 135
    setxy random-xcor random-ycor
    ask patches in-radius (2 + random 2) [
      set owner myself
      set pcolor 35
    ]
  ]

end

to go 
  
  ask turtles [ ask patches in-radius 4 with [owner = myself] ; falta representar las cosechas 4-2-3-3
    [ set pcolor 42]
  ]
  
end

If multiple turtles try to be the owner of the same patch, the last one becomes the owner. This is because each turtle completely overwrites the previous owner value.

If you still want an option for multiple owners, you can work with a turtle-set instead. This requires a few different primitives. In setup, you have to define owners as an empty turtle-set in order for the rest to work, using set owners (turtle-set). To then assign a turtle to owners, you use set owners (turtle-set owners myself). This changes the owners turtle-set to add the turtle calling the patch to the set. Finally, you can no longer ask patches to with [owner = myself] to change color since owners is now a turtle-set, not a turtle. Instead, you use the member? primitive that looks if a turtle is part of a turtle-set.

patches-own [owners]

to setup 
  ca
  resize-world 0 100 0 100
  ask patches [set owners (turtle-set)]
  
  create-turtles 50 [
    set size 1
    set color 135
    setxy random-xcor random-ycor
    ask patches in-radius (2 + random 2) [
      set owners (turtle-set owners myself)
      set pcolor 35
    ]
  ]

end

to go 
  
  ask turtles [ 
    ask patches in-radius 4 with [member? myself owners] ; falta representar las cosechas 4-2-3-3
    [ set pcolor 42]
  ]
  
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1