ELC
ELC

Reputation: 41

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

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 ;)

Upvotes: 0

Views: 308

Answers (1)

JenB
JenB

Reputation: 17678

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.

Upvotes: 2

Related Questions