如何按反向度数中心性排序前 10 名的海龟?
How to get the top 10 turtles sorted by reverse in-degree centrality?
我想获得度中心性排名前十的海龟列表。我已经尝试过,但没有得到所需的结果。
在下面的代码中,我将中心性存储在列表中,然后对其进行反向排序。但是,它仅存储中心性。我想要根据中心性对海龟进行排序。我也尝试过将海龟保存在列表中并使用排序依据但出现错误。
我也曾尝试让代理使用具有最大度中心性的海龟,但是当多个节点具有相同的中心性时就会出现问题。我想以高效的方式做到这一点。
globals [indeg]
turtles-own [centrality]
to setup
ca
crt 160
ask turtles [
set indeg []
fd random 15
]
ask turtles with [color = red] [create-links-to other turtles with [color = blue]]
ask turtles with [color = green] [create-links-from other turtles with [color = yellow]]
inf
end
to inf
ask turtles [
set centrality count my-in-links
set indeg lput centrality indeg
]
set indeg sort(indeg)
print "indeg"
print reverse(indeg)
print max(indeg)
end
以下是获取该信息的三种不同方式,性能和结果可能略有不同:
to setup
clear-all
create-turtles 160 [ forward random 15 ]
ask turtles with [color = red] [create-links-to other turtles with [color = blue]]
ask turtles with [color = green] [create-links-from other turtles with [color = yellow]]
let top-10-a reverse sort-on [ count my-in-links ] max-n-of 10 turtles [ count my-in-links ]
show-results top-10-a "Top ten turtles using max-n-of:"
let sorted-turtles reverse sort-on [ count my-in-links ] turtles
let top-10-b sublist sorted-turtles 0 9
show-results top-10-b "Top ten turtles from sorted list:"
let top-10-c filter [ t ->
[ count my-in-links ] of t >= [ count my-in-links ] of item 9 sorted-turtles
] sorted-turtles
show-results top-10-c "Turtles with top ten centrality:"
end
to show-results [ turtle-list title ]
print title
foreach turtle-list [ t -> ask t [ show count my-in-links ] ]
end
第一个(方法 "a")也是最明显的是使用 NetLogo 的 max-n-of
原语。该原语返回一个代理集(不是列表),所以如果你想要一个代理集,那就是要走的路。
你的问题似乎表明你最终想要一个按中心性递减排序的海龟列表,所以你必须在 max-n-of
的结果上使用 reverse sort-on [ count my-in-links ]
,这就是我正在做的以上。
另一种方法(方法 "b")是根据所有海龟的中心性对它们进行排序,将结果列表存储在 sorted-turtles
变量中,然后取其中的前 10 个。该方法更直观一些,但可能比 max-n-of
方法慢一些,因为它必须对整个列表进行排序。然而,根据您拥有的海龟数量,差异可以忽略不计。
前两种方法的共同点是随机断开连接。这意味着如果你有,比方说,三只乌龟的中心地位值得在你的前十名中排在第十位,你只会得到其中的一只。 (考虑到您在问题示例中构建网络的方式,这很可能发生。)如果您希望前十名在平等的情况下可能包括超过 10 只海龟,则需要使用方法 "c".
最后一种方法对整体进行排序,查看该列表中第十只海龟的中心性,并过滤列表以仅保留中心性大于或等于该海龟的海龟。
我想获得度中心性排名前十的海龟列表。我已经尝试过,但没有得到所需的结果。 在下面的代码中,我将中心性存储在列表中,然后对其进行反向排序。但是,它仅存储中心性。我想要根据中心性对海龟进行排序。我也尝试过将海龟保存在列表中并使用排序依据但出现错误。 我也曾尝试让代理使用具有最大度中心性的海龟,但是当多个节点具有相同的中心性时就会出现问题。我想以高效的方式做到这一点。
globals [indeg]
turtles-own [centrality]
to setup
ca
crt 160
ask turtles [
set indeg []
fd random 15
]
ask turtles with [color = red] [create-links-to other turtles with [color = blue]]
ask turtles with [color = green] [create-links-from other turtles with [color = yellow]]
inf
end
to inf
ask turtles [
set centrality count my-in-links
set indeg lput centrality indeg
]
set indeg sort(indeg)
print "indeg"
print reverse(indeg)
print max(indeg)
end
以下是获取该信息的三种不同方式,性能和结果可能略有不同:
to setup
clear-all
create-turtles 160 [ forward random 15 ]
ask turtles with [color = red] [create-links-to other turtles with [color = blue]]
ask turtles with [color = green] [create-links-from other turtles with [color = yellow]]
let top-10-a reverse sort-on [ count my-in-links ] max-n-of 10 turtles [ count my-in-links ]
show-results top-10-a "Top ten turtles using max-n-of:"
let sorted-turtles reverse sort-on [ count my-in-links ] turtles
let top-10-b sublist sorted-turtles 0 9
show-results top-10-b "Top ten turtles from sorted list:"
let top-10-c filter [ t ->
[ count my-in-links ] of t >= [ count my-in-links ] of item 9 sorted-turtles
] sorted-turtles
show-results top-10-c "Turtles with top ten centrality:"
end
to show-results [ turtle-list title ]
print title
foreach turtle-list [ t -> ask t [ show count my-in-links ] ]
end
第一个(方法 "a")也是最明显的是使用 NetLogo 的 max-n-of
原语。该原语返回一个代理集(不是列表),所以如果你想要一个代理集,那就是要走的路。
你的问题似乎表明你最终想要一个按中心性递减排序的海龟列表,所以你必须在 max-n-of
的结果上使用 reverse sort-on [ count my-in-links ]
,这就是我正在做的以上。
另一种方法(方法 "b")是根据所有海龟的中心性对它们进行排序,将结果列表存储在 sorted-turtles
变量中,然后取其中的前 10 个。该方法更直观一些,但可能比 max-n-of
方法慢一些,因为它必须对整个列表进行排序。然而,根据您拥有的海龟数量,差异可以忽略不计。
前两种方法的共同点是随机断开连接。这意味着如果你有,比方说,三只乌龟的中心地位值得在你的前十名中排在第十位,你只会得到其中的一只。 (考虑到您在问题示例中构建网络的方式,这很可能发生。)如果您希望前十名在平等的情况下可能包括超过 10 只海龟,则需要使用方法 "c".
最后一种方法对整体进行排序,查看该列表中第十只海龟的中心性,并过滤列表以仅保留中心性大于或等于该海龟的海龟。