NetLogo从补丁变量中查询乌龟变量

时间:2014-02-14 10:09:57

标签: netlogo

我正在尝试选择status乌龟的owner小于myself的邻居补丁。如果存在此类修补程序,则myself将其区域扩展到这些修补程序并成为owner。但是我要求NetLogo识别status邻近补丁的owner。我可以选择owner的邻居,然后打印这些所有者的status,但就是这样。任何帮助都会非常感激。代码如下。

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

1 个答案:

答案 0 :(得分:2)

在这段代码中:

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

这为您提供了一个 OF预期输入为turtle agentset或turtle,但却得到了NOBODY 错误。原因是您首先检查是否有所有者的邻居,但您正在检查所有邻居的status(通过neighbors with)。

所以也许你可以将owner != nobody添加到with条款中?

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

但是你得到一个补丁无法在没有指定哪个乌龟的情况下访问乌龟变量。那是因为with会让你在提问者层次结构中更深一层,所以{{1}现在指的是补丁,而不是像你希望的那样指动物。您可以使用临时变量(例如,myself过程顶部的let current-animal self)来解决此类问题。在您的情况下,我会直接将状态存储在变量中。稍微重组后,您当前的expand块可能会变为:

ask territory

请注意,我已经摆脱了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]) ] 检查。您并不真正需要它们:如果没有适合您条件的补丁,您只需将空补丁集添加到现有补丁集中,这无关紧要。