优化 Netlogo 代码 - 每个 运行 太多滴答?

Optimizing Netlogo Code - too much ticks each run?

我想优化我的 netlogo 代码,并通过使用探查器扩展来追踪瓶颈来实现这一点。此外,我 运行 在无头模式下设置我的模型,我给它 2 GB 的 RAM (-Xmx2048m),并且有 2601 个补丁和大约 100-1500 个代理。我使用 time extension 作为 netlogo。

现在,我有四个一般性问题。

1) 在模型(.nlogo)或 BehaviorSearch(我使用 .xml 无头模式下的文件),还是没有任何区别?

模型内:

if count turtles = 0 [ stop ]
if time:is-equal dt (time:create "1722-04-05") [ stop ] ;using the time extension

行为空间内:

<exitCondition>count turtles = 0</exitCondition>
<exitCondition>time:is-equal dt (time:create "1722-04-05")</exitCondition>

2) 我想我可以通过优化代码来优化我的模型,使其快 运行 秒(参见我在 6 之后拍摄的屏幕截图, 5 小时的模拟 运行ning):

但是,我越来越怀疑我在模型中缩放每个刻度以表示一天的方式(这实际上是一个关键因素)。我越来越怀疑,因为我的模型中可能的最大时间跨度是 472 年。这将使模型的完整 运行 产生 172280 个刻度!我不知道在 Netlogo 中每个 运行 有很多滴答声是否会成为一个巨大的瓶颈,考虑到我的性能(见上面的分析器输出)。

3) 添加 gpu extension into the model to gain more speed or is it not so easy to implement that? I ask because I have not found any documentation about that. This possibility(?) seems to be largely ignored, see this thread.

是否合理

4) 在 RAM 中 运行 netlogo-headless 有意义吗?技术上是否可行?您可能已经看到这些问题,我对这些技术细节并不十分确定,但对可能性很感兴趣。

编辑: 在下方,您可以根据要求找到 orientation 的代码片段:

to orientation
   ifelse (energy < 4) [ ;if hungry
     let nearest-resource min-one-of (patches with [pcolor = green] in-radius 3 ) [ distance myself ] ;check distance between you and nearest resource (3 fields 360 degrees view)
     if is-patch? nearest-resource [ ;if green patch exist at all
     move-to nearest-resource ]
    ]
   [ rt random-float 30 - random-float 30 ] ;otherwise just walk randomly around
end

这部分代码是最近在 Whosebug 上讨论的主题

感谢您发布代码!幸运的是,它可以被优化! patches with [pcolor = green] 将遍历每个补丁,检查哪些是绿色的。 ... in-radius 3 将遍历所有绿色的,检查哪些足够接近。但是,如果您先执行 patches in-radius 3,NetLogo 可以准确计算出该半径内有哪些补丁,而无需检查所有补丁。然后,检查颜色的补丁也少得多。通过交换这些顺序,我得到了将近 10 倍的加速:

to orientation
   ifelse (energy < 4) [ ;if hungry
     let nearest-resource min-one-of (patches in-radius 3 with [pcolor = green] ) [ distance myself ] ;check distance between you and nearest resource (3 fields 360 degrees view)
     if is-patch? nearest-resource [ ;if green patch exist at all
     move-to nearest-resource ]
    ]
   [ rt random-float 30 - random-float 30 ] ;otherwise just walk randomly around
end

查询补丁时必须小心,因为通常有很多补丁。在其他检查之前进行 in-radius 检查会有很大帮助,因为 NetLogo 可以优化 patches in-radius.

问题的快速解答:

  1. 应该没什么区别,但我喜欢把我的放在 BehaviorSpace 中。
  2. 单是更多的蜱虫并不一定是坏事。例如,参见伊辛。
  3. 还没有用过,但如果它有效,我会感到非常惊讶。最后一次提交是在 4 年多以前,也就是许多 NetLogo 版本之前。那就是说,如果你让它工作,我很想知道!
  4. 不太确定你的意思,但 NetLogo 完全运行 RAM。也就是说,如果 NetLogo 和其他程序 运行 同时使用的内存多于您的 RAM,则 OS 将 offload memory onto the hard drive,这在某些情况下会导致严重的速度下降。这不太可能,但对于超大模型(您的模型不是)绝对是个问题。您的计算机有多少内存?

最后,确保让 BehaviorSpace 在您机器上的每个核心使用一个线程。这是默认设置,因此除非您设置线程数,否则它应该会发生。可以肯定的是,运行 NetLogo时每个核心应该接近100%。您的 OSes activity 监视器应该能够为您提供此信息。如果这没有发生(可能是 NetLogo 错误地检测了核心数),您可以尝试显式设置 --threads 选项。