【发布时间】:2020-06-19 17:38:21
【问题描述】:
我正在 netlogo 中研究群聚行为,为了跟踪各种群,我使用了一个隐藏的“flock-holder”海龟,如果创建了新群或现有群用完,我可以孵化或让它死亡的成员。但是,我遇到了一个问题,有时当我尝试与群中的某些数据交互时,正如通过群中的单个成员所引用的那样,我收到一条消息说“那已经死了”,导致代码失败。
根据我对“die”命令的理解,如果任何种类的海龟死亡,它应该从引用它的任何代理集或变量中删除自己,因此这种错误应该不是问题?我该如何解决或至少调试这个奇怪的问题?
我的羊群评估函数的代码存在以下问题:
to evaluate-flock
if is-in-flock = True ; checking to see if a flock has died out
[
if get-flock-size flock-reference < 2 ; is the turtle the only one in the flock?
[
if verbose = True [ print "Flock has dwindled to nothing" ]
ask flock-reference ; has no more members, so is removed.
[
ask flock-members
[
set flock-reference nobody ; clear any remaining flock members of association with this flock
]
die
]
set is-in-flock False ; no longer in a flock
]
]
ifelse is-in-flock = True ; is turtle in a flock?
[
if verbose = True [ type "This turtle is in flock " print [ who ] of [ flock-reference ] of self ]
if any? other preys in-radius vision with [ is-in-flock = True ] with [ flock-reference != [ flock-reference ] of myself ]; check for nearby turtles that are in different flocks
[
if verbose = True [ print "There are other nearby flocks" ]
let current-school-size ( get-flock-size [ flock-reference ] of self )
if verbose = True [ type "I am part of a school of " print current-school-size ]
let temp-list turtle-set other preys in-radius vision with [ is-in-flock = True ] with [ flock-reference != [ flock-reference ] of myself ] with [ ( get-flock-size flock-reference ) > current-school-size ] with [ subtract-headings ( average-schoolmate-heading [ flock-members ] of flock-reference ) heading < 60]; are any nearby turtles in different, larger flocks that I am alligned with? if so, add them to a list
if count temp-list > 0 ; does the list have any members?
[
if verbose = True [ print "Found a bigger flock" ]
ask flock-reference
[
remove-from-flock myself ; remove myself from my old flock
]
set flock-reference [ flock-reference ] of ( max-one-of temp-list [ get-flock-size flock-reference ] ); join the biggest flock on this list
set is-in-flock True ; sets it to true in case it wasn't for some reason.
]
]
]
[
if verbose = True [ type "Turtle " type [ who ] of self print " is not in a flock" ]
ifelse any? other preys in-radius vision with [ is-in-flock = True ] ; are there any pre-existing flocks the turtle can join?
[
if verbose = True [ print "There are nearby flocks" ]
let potential-flock turtle-set other preys in-radius vision with [ is-in-flock = True ] ; grab any nearby turtles that are already in a flock
***set potential-flock potential-flock with [ subtract-headings ( average-schoolmate-heading ( [ flock-members ] of flock-reference ) ) heading < 60]; remove any that are not aligned with this turtle***
if count potential-flock > 0
[
if verbose = True [ print "There are nearby flocks that I am aligned with" ]
set flock-reference [ flock-reference ] of ( max-one-of potential-flock [ get-flock-size flock-reference ] ); join the biggest flock on this list
set is-in-flock True ; turtle is now in a flock
]
]
[ ; if there are no pre-existing flocks, turtle starts its own
let potential-flock turtle-set other preys in-radius vision with [ is-in-flock = False ] ; Grab any nearby turtles not already in a flock
set potential-flock potential-flock with [ subtract-headings ( average-schoolmate-heading potential-flock ) heading < 60]; remove any that that are not aligned with this turtle
if count potential-flock > 0
[
if visualize-flock-creation = True
[
set color green
ask potential-flock [ set color green ]
wait 0.25
set color blue
ask potential-flock [ set color blue ]
]
if verbose = True [ type "Number of nearby potential flockmates " print count potential-flock ]
hatch-flock-holders 1 ; create a new flock-holder
[
set size 0
set color black ; sets the new flock's placeholder color to the background
set flock-members potential-flock ; adds the list of members to the new flock
ask flock-members
[
set flock-reference myself ; asks the new flock members to add the new flock as their flock-reference
set is-in-flock True ; all these turtles are now in a flock
]
]
]
]
]
end
以上代码可能不清楚的变量名称参考: 群参考: - 每个群聚“猎物”乌龟持有的变量,它只指向隐藏的“flock-holder”乌龟。 flock-members: - 附着在隐藏的“flock-holder”海龟上的“猎物”海龟的代理集。
如果对这里发生的事情有任何困惑,或者有什么我可以澄清的,请告诉我。谢谢!
【问题讨论】:
-
您能告诉我们错误发生在代码的哪一行吗?我怀疑你明确指的是猎物 2。
-
我已经更新了我原来的帖子,用星号包围了这条线。
-
突出显示“减去标题(average-schoolmate-heading([flock-members] of flock-reference))标题”中的“of”
标签: netlogo