Performance Netlogo:优化补丁内的海龟邻居计算

Performance Netlogo:优化补丁内的海龟邻居计算,performance,optimization,netlogo,Performance,Optimization,Netlogo,我需要计算受某些属性约束的所有海龟在同一补丁中的最近邻海龟。我写的代码花费了很多时间,我想知道是否有其他方法可以优化它。我在turtle中定义了一个属性邻居来存储最近的邻居 to set_neighbor ask patches [ ask turtles-here with [ life_stage = "STAGE1" ] [ set neighbor min-one-of other turtles-here with [ life_stage != "STAG

我需要计算受某些属性约束的所有海龟在同一补丁中的最近邻海龟。我写的代码花费了很多时间,我想知道是否有其他方法可以优化它。我在turtle中定义了一个属性邻居来存储最近的邻居

to set_neighbor  
  ask patches [
    ask turtles-here with [ life_stage = "STAGE1" ] [
      set neighbor min-one-of other turtles-here with [ life_stage != "STAGE2" ] [ (distance myself) ]
    ]
  ]  
end
我试图通过做以下更改来优化代码,但最终花了更多的时间。我假设如果我将查询结果存储在一个变量中,并在以后多次使用该变量,那么它将比多次执行查询本身更快

to set_neighbor  
  ask patches [
    let turt_temp turtles-here with [ life_stage != "STAGE2" ]
    ask turtles-here with [ life_stage = "STAGE1" ] [
      set neighbor min-one-of other turt_temp [ (distance myself) ]
    ]
  ]  
end

我真的很感激你能给我一些建议。提前谢谢。

为什么补丁会问海龟呢?这应该做完全相同的事情,但速度要快一点,特别是如果在
阶段1有很多补丁没有海龟的话:

to set_neighbor  
  ask turtles with [ life_stage = "STAGE1" ] [
    set neighbor min-one-of other turtles-here with [ life_stage != "STAGE2" ] [ (distance myself) ]
  ]
end
我不确定为什么您的第二个实现速度较慢。如果你在
STAGE1
(特别是如果有很多其他海龟在上面的话)有很多补丁,而没有海龟的话,我可以看到速度会变慢

另一个选择(我不知道哪一个更快)是限制询问的数量,只在同一个补丁上同时有stage1和not(stage2)海龟的地方进行。我还将
与min
一起使用,而不是
min-one/of
,但我也不知道哪个更快

to set_neighbor
  ask patches with [ any? turtles with [ life_stage = "STAGE1" ] and any? turtles with [ life_stage != "STAGE2" ] ]
  [ ask turtles-here with [ life_stage = "STAGE1" ]
    [ set neighbor other turtles-here with [ life_stage != "STAGE2" ] with-min [ distance myself ]
    ]
  ]
end

实际模型是否需要
other
。在本例中,只询问stage1海龟,只从stage2海龟中选择。检查
stage2
是一个不等式,而不是一个等式,因此它包括
stage1
turtles。因此,
其他
是必要的。不过,考虑一下也不错,因为删除不必要的
其他
会加快速度!很好,我读得不够仔细。最好去给我的建议修好答案。我尝试了您的建议,但这也比原始代码花费了更多的时间。询问补丁背后的想法是以某种方式将海龟缓存在这里,并一次计算一个补丁中海龟的所有邻居。有趣。你介意在你的问题中包括一张你模型的典型状态的图片吗?这里的性能在很大程度上取决于代理的分布方式。