Netlogo:创建具有正确节点数的分层(树)网络

Netlogo: Creating Hierarchical (tree) network with correct number of nodes

我正在尝试使用参数 expansion rate 创建 'Hierarchical' 或 'Tree' 网络结构。首先,将一个节点放在最上面,网络中的每个节点都连接到他下面的节点数等于expansion rate。目前我的代码如下所示:

to wire-tree
  clear-all
  ask patches [ set pcolor white ]
  create-nodes 1 [         ; create root node of tree
    set shape "circle"
    set color red
    set branch 0
    expand-network
    rewire-branches
  ]

  radial-layout


  reset-ticks
end

to expand-network

  if expansion-rate = 0 [ stop ]
  while [count nodes < num-nodes] [
     ask nodes with-max [branch] [
      hatch expansion-rate [
      create-edge-with myself
      set branch branch + 1
      ]
    ]
  ]

end

网络当前具有正确的结构,但网络中的节点数超过了 num-nodes 滑块选择的节点数。这是因为首先检查 if count nodes < num-nodes,然后执行最后一个孵化。但是,我想要的是执行最后一个节点孵化直到达到 num-nodes 然后停止。因此,虽然最后一层之前的层次结构的每一层包含的节点数等于 expansion rate 的幂,但如果种群未正确划分,最后一层的节点数可能少于此数。

我怎样才能做到这一点?

我需要 turtle-owned branch 变量,因为我稍后想以固定概率重新连接某些分支中的节点。稍后可能 post 会对此提出问题 ;)

将您的 hatch expansion-rate 替换为 hatch min expansion-rate (num-nodes - count nodes),这样它会创建两个数字中的最小值 - 扩展率和您仍然需要的总数。