0

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

ELC
  • 41
  • 5

1 Answers1

2

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.

JenB
  • 17,620
  • 2
  • 17
  • 45
  • Thanks, I think this should work. But Netlogo gives me an 'expected command' error while highlighting num-nodes and I can't get it to work. Seems like I can't use the `min` procedure in this context? Edit: I think I got it to work by using `hatch min list expansion-rate (num-nodes - count nodes)` – ELC Jun 15 '17 at 12:26
  • yes, sometimes NetLogo gets confused about ordering. You shouldn't need the `list` since it's exactly two, but you may need extra brackets so it's `hatch min (expansion-rate (num-nodes - count nodes))`.If that doesn't work, try `hatch min (list expansion-rate (num-nodes - count nodes))`. Note the positioning of the primitive `list` here, inside the brackets. – JenB Jun 15 '17 at 22:42