如何避免在两个节点之间两次添加 link

How to avoid adding a link between two nodes twice

我做了以下 netlogo 程序,它创建了一个具有给定数量的节点和节点之间的无向链接的网络:

to setup
  ;; Here I created the nodes and sort them around a circle
  clear-all
  create-turtles number-of-nodes 
  layout-circle turtles max-pxcor

  ;; Here I add the links
  let i 0
  while [i < number-of-links] [
    ;; I randomly extract two nodes and link them
    let node1 random number-of-nodes
    let node2 random number-of-nodes
    if node1 != node2 [
      ask turtle node1 [ create-link-with turtle node2 ]
    ] 
    set i i + 1
  ]

  reset-ticks
end

问题是这也可能会在已经连接的节点之间添加链接。有没有办法知道两个节点是否连接,如果它们已经连接,我可以随机提取另一个数字?

link-neighbor? 会告诉你的。这是一个海龟记者,它需要一个参数;你想知道它是否连接到的乌龟。所以:

ask n-of number-of-links turtles [create-link-with one-of other turtles with [not link-neighbor? myself]]

会成功的。请记住,这会给你的网络一个特定的结构(我认为是随机网络?)但我不是这方面的专家。

或者,

repeat number-of-links [ ask one-of turtles [create-link-with one-of other turtles with [not link-neighbor? myself] ] 会给你另一个结构。

两者的区别在于,前者是每只海龟都会创建一个link给另一只海龟。在后者中,同一只海龟可能会被随机选择多次以与另一只海龟创建 link。