NetLogo - turtles to imprint 'who' on patches and only move-to patches which match their 'who'

Viewed 274

I'm working on a land-use model featuring a forested World where turtles (smallholders and companies) have the ability to convert forest into crop-land. I would like to introduce a feature that turtles 'own' the patches they convert and are able to revisit them later to get these patches certified. The main issue is that when turtles move-to crop-land patches to get them certified, they do not only move to those they 'own' but also jump across the world to other turtles' crop-land patches and certify those. I've tried a few different workarounds, but I seem to run into the same two issues eventually:

#1 - error: can't use who in a patch context

I wanted to use the 'who' variable to mark crop-land patches as belonging to the turtle that converted the patch, e.g., turtle 0 goes to the forest, converts it to crop-land and that patch of cropland should be 'owned' by turtle 0, i.e., the patches owned-by variable should be equivalent to the turtle's 'who'. The issue here is that 'who' is a turtles-own variable. So, when I use it in a patch-context it produces an error. For example, ask smallholders [move-to one-of patches with [[owner = who]] --> error.

#2 - can't manage to set a global variable = 'who'

Two, I tried to work around this by using a proxy variable: a globals-variable called 'owner-ID'. I would use set owner-ID who to imprint the turtles individual number to the owner-ID. This seems to work to some extent, namely that the patches' 'owner' variable corresponds to the turtle that converted the patch. It also works when counting how many patches of certified and conventional crop-land turtles own (see set-land-ownership command below). However, when the smallholders-certify-crop-land commands are triggered, turtles don't stick to the patches they own, but 'jump' across the world. When prompting turtles through the command-center ask turtles [print owner-ID] they all return the same owner-ID value. I feel there might be a mistake in my move-to command-line but I just can't find it.

Summary & Question I want crop-land patches to be 'owned by' the turtles that converted them, and want turtles to move only to the patches they 'own' when certifying crop-land patches, not to patches they don't own. I guess my questions revolve around whether it's possible to somehow use the 'who' variable in a patch-context. And, if not, what a good workaround for the problem could look like.

Relevant code is below (I hope)!

globals [owner-ID]
turtles-own [conventional-land-ownership certified-land-ownership]
patches-own [owned-by owner certified?]

to setup [ 
  ask patches [
    set pcolor green     ;; green = forest
    set certified? "no"
    set owner "nobody"
    ]
]

to go
  ask turtles [set-land-ownership]
  ask smallholders [check-smallholder-status]
  tick
end

to set-land-ownership
  ask smallholders [
    set owner-ID who
    set conventional-land-ownership count patches with [owner = owner-ID and certified? = "no"]
    set certified-land-ownership count patches with [owner = owner-ID and certified? = "yes"]
  ]
end

to check-smallholder-status
  if wealth >= 0 and (conventional-land-ownership + certified-land-ownership) < SH_max-land-ownership [
    smallholders-choose-activity
    ]

  if wealth < 0 [
      set color red
      set shape "cow skull"
  ]

  if (conventional-land-ownership + certified-land-ownership) >= SH_max-land-ownership [
      set color orange + 2
  ]
end

;; smallholders-choose-activities is a reporter-based command where turtles choose the most economical option available. One of the outcomes is: smallholders-certify-crop-land 



to smallholders-certify-crop-land
  let available-patch max-one-of patches with [owner = owner-ID and certified? = "no"] [count neighbors with [certified? = "yes"]]

  ifelse not any? turtles-on available-patch [
    move-to available-patch
    ]
  []

  set wealth wealth - smallholder-certification-cost
  set pcolor brown + 1
  set certified? "yes"
end


1 Answers

Your first approach is definitely the way to go and could be fixed with one small adjustment.

ask smallholders [move-to one-of patches with [owner = who]]

should be

ask smallholders [move-to one-of patches with [owner = [who] of myself]]

Within the block after with, variables are in the context of patches, but myself refers to the agent that asked the patches to check their owner, in this case, each smallholder. The global variable owner-ID is then unnecessary. If you carry this through the rest of the code, your second problem may solve itself.

BUT, in general it is best not to use who numbers at all, but rather refer to the agent directly. (You have actually taken that approach implicitly when you initialize owner to nobody, which is "no agent".) I don't see where you ask a patch to set its owner, but if a smallholder is on a patch, the smallholder would

ask patch-here [set owner myself]

and the line above would now read

ask smallholders [move-to one-of patches with [owner = myself]]

The NetLogo gurus suggest that we use who numbers only when there is no other approach.

Related