Hannah
Hannah

Reputation: 33

NW extension - multiple graphs with nw:generate?

I want to build several random subgraphs in one world. As, in general, the number of subgraphs is not fixed, I don't want to use different breeds for each subgraph. So far I use a pretty rude approach (see below) with "handmade" random networks, assingning a "my-group" variable to the turtles. I am new to the NW extension and I wonder if it would be possible to create several subgraphs using the nw:generate-random module without using breeds for each subgraph. Do you have any suggestions? Thanks, Hannah

to create-subgroups
  crt network_size [ setxy random-xcor random-ycor ]
  let group-n 0
  repeat network_size / group_size [ ;; group_size is always a factor of network_size
    ask turtles with [who >= (group-n * group_size) and who < ((group-n + 1) * group_size)] [
      set my-group group-n           ;; get group number
      repeat (random 4) + 1 [        ;; create links
        let target one-of other turtles with [who >= (group-n * group_size) and who < ((group-n + 1) * group_size)]
        if target != nobody [
          create-group-link-with target ] ] ]
    set group-n group-n + 1 ]
end

Upvotes: 3

Views: 153

Answers (1)

Bryan Head
Bryan Head

Reputation: 12580

Good question! This should be a nicer way of generating the same graphs that you're currently generating:

to create-subgroups
  repeat network_size / group_size [
    let group (turtle-set)
    crt group_size [ set group (turtle-set group self) ]
    ask group [
      create-group-links-with n-of (1 + random 4) other group
    ]
  ]
end

This assumes that group_size is greater than 4 (otherwise there may not be enough turtles in the group to link to).

The idea here is that rather than keeping track of who's in the group with who numbers, you can can use a turtle-set. Besides being cleaner, it allows you to store the groups in a list for later reference. So if you have a global groups that you've set to a list, you can add the groups to it like so:

to create-subgroups
  repeat network_size / group_size [
    let group (turtle-set)
    crt group_size [ set group (turtle-set group self) ]
    ask group [
      create-group-links-with n-of (1 + random 4) other group
    ]
    set groups (lput group groups)
  ]
end

Update:

You can use a similar technique with nw:generate-random. nw:generate-random generates an subgraph, it won't take over your whole network. The turtles and links will be whatever breed you specify, but they will only connect to other turtles in breeds being currently created by nw:generate-random. Thus, this should work:

to create-subgroups
  repeat network_size / group_size [
    nw:generate-random turtles links group_size connection-probability
  ]
end

where connection-probability is the probability that any two turtles in the newly created subgraph will be connected. You can still snag references to the newly created turtles to store in a list in a similar way as above:

to create-subgroups
  let group (turtle-set)
  repeat network_size / group_size [
    nw:generate-random turtles links group_size connection-probability [
      set group (turtle-set group self)
    ]
  ]
  set groups (lput group groups)
end

This way, you've got turtle-sets for each of the components, the number of components is completely variable and changeable in a behavior space run, and the components are completely disconnected from each other.

Upvotes: 3

Related Questions