我想创建一个随机网络(和一个无标度的网络),以N节点和<k>作为平均度。我怎么能这么做?
nw: generate-random (和nw:generate-preferential-attachment)方法的NW扩展NetLogo似乎不允许处理节点的平均程度。
我错了?小费?谢谢。
发布于 2017-11-10 16:26:07
确实,nw:generate-random和nw:generate-preferential-attachment都不允许您指定精确的平均度。然而,在nw:generate-random的情况下,平均程度将近似于connection-probability * num-nodes。例如:
observer> repeat 10 [ ca nw:generate-random turtles links 1000 0.1 print 2 * count links / count turtles ]
99.902
100.358
100.522
99.674
100.338
100.272
99.772
100.24
100.24
100.412尽管如此,如果您确实希望指定精确的平均程度,则可以使用以下方法:
to generate-random [ num-nodes avg-degree ]
crt num-nodes
while [ 2 * count links < avg-degree * count turtles ] [
ask one-of turtles [
create-link-with one-of other turtles
]
]
end请注意,该代码有意不执行类似create-link-with one-of other turtles with [ not link-neighbor? myself ]的操作,因为这样做最终会产生更多的海龟,其程度比它应该的要大(也就是说,平均程度是正确的,但程度分布将是倾斜的)。
优先依恋则要复杂一些。我们必须为即将到来的海龟种下足够的种子,因为它们有足够的海龟可以附着:
to generate-preferential-attachment [ num-nodes avg-degree ]
crt avg-degree + 1 [
create-links-with other turtles
]
repeat (num-nodes - (avg-degree + 1)) [
crt 1 [
while [ 2 * count links < avg-degree * count turtles ] [
create-link-with one-of other [ both-ends ] of one-of links
]
]
]
end此代码使用与模型库中的优先附件模型相同的优先附加机制。在这种模式下:
;; This code is the heart of the "preferential attachment" mechanism, and acts like
;; a lottery where each node gets a ticket for every connection it already has.
;; While the basic idea is the same as in the Lottery Example (in the Code Examples
;; section of the Models Library), things are made simpler here by the fact that we
;; can just use the links as if they were the "tickets": we first pick a random link,
;; and than we pick one of the two ends of that link.在大多数情况下,我只是在我的模型中使用NW程序生成,但当我真的需要控制精确的平均程度时,我使用上述变量。再一次,它们比你想象的要复杂一些,可以防止学位分布中的偏见悄悄地出现。
这两种程序都假定没有预先存在的海龟。如果你的模型不是这样的话,让我知道,我会修改。否则会不必要地使代码复杂化(因为您必须跟踪您创建的海龟)。
编辑是对评论中的问题的回答:
while [ 2 * count links < avg-degree * count turtles ] [ ... ]将导致...一遍又一遍地运行,直到平均度等于avg-degree为止。回想一下,平均学位等于2 * count links / count turtles。
因此,在产生随机网络的情况下,我们尝试添加一个链接,检查我们是否有足够的资源,如果没有,我们一直往前走。之所以在这里使用while而不是repeat,是因为外观的主体可能实际上不会创建链接(如果海龟试图与已经链接的海龟链接)。它是这样写的,以防止学位分布上的偏差:海龟有越多的链接,它就越不可能获得新的墨水。
在优先依附的情况下,我们每次添加一个节点,然后添加到该节点的链接,直到我们的平均程度是正确的。这比总是让海龟带avg-degree / 2链接进来更好,因为它在奇怪的程度上表现得更好。
https://stackoverflow.com/questions/47217886
复制相似问题