【发布时间】:2020-09-11 04:52:06
【问题描述】:
我正在构建一个包含社交传播组件的 Netlogo 模型,并且在使用 Table 扩展时遇到了一个令人惊讶的性能问题。
对于某些上下文:在模型中,每个代理都有一个态度表,其中包含一个字符串键(例如“Environment”)和一个从 -1 到 1 的浮点值。每个滴答声,代理都会根据他们的态度更新他们的态度联系人和每个联系人的权重。为了制作原型,我为每个联系人使用了简单的更新规则:
a(t) = a(t-1) + w * [b(t-1) - a(t-1)]
其中a是代理的态度,b是接触的态度,w是权重。每个代理有 5-10 个联系人。
我通过两种方式实现了这一点。第一个使用表现不错的链接。但是,当我扩大规模时,它的内存占用似乎变得相当大。由于网络不是动态的,我正在尝试将其实现为一个表,其中键是联系人海龟 ID,值是权重(严格来说,我创建了一个名为“联系人”的海龟 ID 列表和一个相应权重表以避免重新创建每次迭代时的列表)。对于许多代理来说,这似乎使用少得多的内存,但出乎意料地慢了一个数量级。
这是基于网络的更新的 sn-p:
ask my-in-links [
let neighborAttitude table:get [attitudes] of other-end "Environment"
let myAttitude table:get [attitudes] of myself "Environment"
let influence weight * (neighborAttitude - myAttitude)
set myAttitude myAttitude + influence
table:put [attitudes] of myself "Environment" myAttitude
]
还有基于表格的版本:
foreach (contacts) [ i ->
let myAttitude table:get attitudes "Environment"
let neighborAttitude table:get [attitudes] of homeowner i "Environment"
let w table:get contactWeights i
let influence w * (neighborAttitude - myAttitude)
set myAttitude myAttitude + influence
table:put attitudes "Environment" myAttitude
]
从一些测试看来,获得邻居态度会导致大幅放缓。如果我注释掉那行,从代理的态度表中获取和放置似乎与基于网络的块一样快。所以它似乎正在寻找另一只乌龟(“房主”)。
在 NetLogo 中通过 id 查找海龟/代理是否存在本质上非常缓慢的问题?这似乎应该基本上是免费的,但我不知道引擎盖下的数据结构是什么。
【问题讨论】:
标签: performance netlogo