user2359494
user2359494

Reputation: 733

NetLogo querying turtle variables from patch variables

I'm trying to select neighbor patches where the status of the owner turtle is less than myself. If such patches exist then myself extends its territory to those patches and becomes the owner. But I'm having trouble asking NetLogo to identify the status of the owner of neighboring patches. I can select neighbors with an owner and then print the status of those owners but that is it. Any help would be immensely appreciated. Code is below.

breed [animals animal]

animals-own [ orig territory food status]  
patches-own [ owner hsi]   

to setup 
  clear-all
  ask patches 
  [ 
    set owner nobody 
    set hsi random 5 
    set pcolor scale-color (black) hsi 1 4
  ] 
  let $colors [red pink yellow blue orange brown gray violet sky lime] 
  ask n-of 10 patches 
  [ 
    sprout-animals 1
    [ 
      set orig patch-here  
      set territory patch-set orig 
      set status random 4
      set color item who $colors  
      set pcolor color 
      set owner self
      pen-down
    ] 
  ] 
  reset-ticks 
end 

to go 
  if all? animals [food >= 150] [ stop ] 
  if ticks = 50 [ stop ]
  ask animals [ expand ]
  tick 
end 

to expand  
  let neighborline no-patches
  let pot-comp no-patches
  if food < 150
  [ 
     ask territory
     [
       if any? neighbors with [owner = nobody] 
       [
         set neighborline (patch-set neighborline neighbors with [owner = nobody]) ;this works fine.
       ]
       if any? neighbors with [owner != nobody]
       [
         set pot-comp (patch-set pot-comp neighbors with [[status] of owner < [status] of myself]) ; this does not work. What am I doing wrong? 
       ]
     ]
       let target max-n-of 3 neighborline [hsi]
       set territory (patch-set territory target) ; grow territory to 3 patches with no owners and highest HSI 
       ask territory 
       [ 
         set owner myself
         set pcolor [color] of myself
       ]
     set food sum [hsi] of territory
  ]
end

Upvotes: 2

Views: 1929

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

In this block of code:

if any? neighbors with [owner != nobody]
[
  set pot-comp (patch-set pot-comp neighbors with
    [[status] of owner < [status] of myself]) 
]

This gives you an OF expected input to be a turtle agentset or turtle but got NOBODY instead error. The reason is that you are first checking if there are neighbors with an owner, but you are then checking for the status of all neighbors (via neighbors with).

So maybe you could just add the owner != nobody to your with clause?

set pot-comp (patch-set pot-comp neighbors with
  [owner != nobody and [status] of owner < [status] of myself])

But then you get A patch can't access a turtle variable without specifying which turtle. That's because with takes you one level deeper in the asker hierarchy, so myself now refers to the patch instead of referring to the animal like you probably hoped it would. You can always get around that kind of issues by using temporary variables (e.g., let current-animal self at the top of your expand procedure). In your case, I would directly store the status in a variable. Slightly restructured, your current ask territory block could become:

let my-status status ; store this while `self` is the animal
ask territory
[
  set neighborline (patch-set neighborline neighbors with [owner = nobody])
  set pot-comp (patch-set pot-comp neighbors with
    [owner != nobody and [status] of owner < my-status])
]

Notice that I got rid of the if any? neighbors with [...] checks. You don't really need them: if there are no patches that fit your conditions, you will just be adding empty patch sets to your existing patch sets, which is of no consequence.

Upvotes: 4

Related Questions