Netlogo:如何获得链接的权重

时间:2018-05-17 07:11:10

标签: netlogo

我得到了一组连接相互连接的海龟。我想检索两个节点之间链接的权重,我尝试过搜索但找不到任何关于如何操作的信息。我没有使用nw因为我不想要最短的路径。有任何想法吗?这是我的代码的一部分:

to calculate-oldpath
let oldList [ 25 0 1 2 3 4 9 8 7 6 5 10 11 12 13 14 19 18 17 16 15 20 21 22 23 24]
let weighted-dist 0
( foreach ( but-last oldList ) ( but-first oldList ) [
[ a b ] ->
ask turtle a [
  let node-link link-with turtle b 
  ;Then retrieve weight link to do adding
]
] )
print weighted-dist
end

enter image description here

S是我的起点(列表中的25个),E是结束(列表中的24个)我想计算这个“橙色路径”的重量

2 个答案:

答案 0 :(得分:2)

Jen关于如何获得链接权重的答案是正确的,但我建议另一种计算这些权重之和的方法:使用sum原语!

这需要将envelopeDefinition变为foreach,但除此之外,它非常简单:

map

另一个小评论:使用let weighted-dist sum (map [ [a b] -> [ [ weight ] of link-with turtle b ] of turtle a ] (but-last oldList) (but-first oldList)) 数字列表可能不是解决问题的最佳方法,但我不太了解您的问题,建议替代方案......

答案 1 :(得分:1)

假设您调用了重量weight(在您的links-own声明中,您还没有显示),那么这样的事情应该有效:

to calculate-oldpath
  let oldList [ 25 0 1 2 3 4 9 8 7 6 5 10 11 12 13 14 19 18 17 16 15 20 21 22 23 24]
  let weighted-dist 0
  ( foreach ( but-last oldList ) ( but-first oldList ) [
      [ a b ] ->
      ask turtle a [
        let node-link link-with turtle b 
        set weighted-dist weighted-dist + [weight] of node-link
      ]
  ] )
  print weighted-dist
end

获取链接的属性值与获取乌龟或补丁的属性值完全相同,您使用of