创建具有参数节点数和平均度数的网络
Create networks with parameter number of nodes and mean degree
我想创建一个具有 N
个节点和 <k>
作为平均度数的随机网络(和无标度网络)。我该怎么做?
NetLogo 的 NW 扩展的 nw: generate-random
(和 nw:generate-preferential-attachment
)方法似乎不允许处理节点的平均度数。
我错了?提示?
谢谢
的确,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
.
因此,在生成随机网络的情况下,我们尝试添加一个link,检查我们是否有足够的,如果没有,我们继续前进直到我们完成。在这里使用 while
而不是 repeat
的原因是外观的主体实际上可能不会创建 link (如果乌龟试图 link 与乌龟它已经 link编辑)。以这种方式编写是为了防止度数分布出现偏差:乌龟拥有的 link 越多,获得新墨水的可能性就越小。
在优先依附的情况下,我们一次添加一个节点,然后向该节点添加links,直到我们的平均度正确为止。这比总是让乌龟以 avg-degree / 2
links 进来更好,因为它在奇数度数下表现得更好。
我想创建一个具有 N
个节点和 <k>
作为平均度数的随机网络(和无标度网络)。我该怎么做?
NetLogo 的 NW 扩展的 nw: generate-random
(和 nw:generate-preferential-attachment
)方法似乎不允许处理节点的平均度数。
我错了?提示? 谢谢
的确,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
.
因此,在生成随机网络的情况下,我们尝试添加一个link,检查我们是否有足够的,如果没有,我们继续前进直到我们完成。在这里使用 while
而不是 repeat
的原因是外观的主体实际上可能不会创建 link (如果乌龟试图 link 与乌龟它已经 link编辑)。以这种方式编写是为了防止度数分布出现偏差:乌龟拥有的 link 越多,获得新墨水的可能性就越小。
在优先依附的情况下,我们一次添加一个节点,然后向该节点添加links,直到我们的平均度正确为止。这比总是让乌龟以 avg-degree / 2
links 进来更好,因为它在奇数度数下表现得更好。