2014-02-14 50 views
2

我试图选择owner龟的status小于myself的邻居补丁。如果存在这种补丁,则myself将其领土扩展到这些补丁并成为owner。但我无法要求NetLogo确定相邻修补程序ownerstatus。我可以用owner选择邻居,然后打印这些所有者的status,但就是这样。任何帮助将非常感激。代码如下。NetLogo从补丁变量查询龟变量

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 

回答

2

在这个代码块:

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

这给你一个预期的输入是一个龟agentset或龟,但得到任何人,而不是错误。原因是您首先要检查是否与业主有邻居关系,但是您正在检查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在提问者层次结构中会带你更深一层,所以myself现在指的是补丁,而不是像你可能希望的那样引用动物。您可以通过使用临时变量(例如expand过程顶部的let current-animal self)来解决这类问题。在你的情况下,我会直接将状态存储在一个变量中。略微调整,当前的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]) 
] 

请注意,我摆脱了if any? neighbors with [...]检查。您并不需要它们:如果没有适合您条件的修补程序,则只需将空补丁集添加到现有修补程序集中,这没有任何意义。

+0

谢谢Nicolas!我一直陷在这太久。现在我明白“我自己”是指错误的水平。 – user2359494