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

老子叫甜甜 提交于 2019-12-25 16:09:19

问题


I'm trying to create a 'Hierarchical' or 'Tree' network structure with a parameter expansion rate. At first, one node is placed at the top, and each node in the network is connected to a number of nodes below him equal to expansion rate. Currently my code looks like this:

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

The network currently has the correct structure, but the number of nodes in the network exceed the number of nodes selected by the num-nodesslider. This is because there is first checked if count nodes < num-nodes after which the last hatch is performed. However, what I want is that this last hatch of nodes is performed up until num-nodes is reached and then stops. Thus, while each level of the hierarchy before the last one contains a number of nodes equal to a power of expansion rate, the last level may have fewer than this if the population does not divide properly.

How can I achieve this?

I need the turtle-owned branch variable because I later want to rewire the nodes in certain branches with a fixed probability. Will probably post a question about that later ;)


回答1:


Replace your hatch expansion-rate with hatch min expansion-rate (num-nodes - count nodes) so it creates the minimum of two numbers - the expansion rate and the total you still need.



来源:https://stackoverflow.com/questions/44501067/netlogo-creating-hierarchical-tree-network-with-correct-number-of-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!