'Resize grid in the world based on number of turtles (input) and link specific breeds together

I would need to visualize the below turtles (from three different breeds) in a squared grid where links are only between turtles from the same breed, except for breeds types2 and types3, that can be also linked to each other. So what I would like to have is a 2D grid where the number of turtles per each type is

40% of type1
40% of type2
20% of type3

(in total 100 turtles).

  set-default-shape types1 "circle"
  set-default-shape types2 "circle"
  set-default-shape types3 "triangle"

  ask n-of 100 patches [ sprout-types1 1 ]
  ask n-of (100 * 0.4) types1 [set breed types2]
  ask n-of (100 * 0.2) types1 [set breed types3]

the values are ok but the turtles are 'free' in the world, not displayed on a grid. How can I display them into a grid and link them based on the above conditions?

This answer Different types of turtles in a lattice grid has provided some help on this, but the resize of the grid and the number of turtles are not the expected ones.



Solution 1:[1]

You should resize the world before creating agents. That is, from the perspective of the code's workflow: if you want to have n agents, then n is first and foremost the number of patches. Then, once this is the case, all patches will sprout.

You need the resize-world command.

You mentioned that you want to have 100 turtles, that is 100 patches, that is a 10x10 world.

This means that you could do:

to setup
  clear-all
  
  resize-world 0 9 0 9   ; This creates a 10x10 world.
  set-patch-size 30
  
  ask patches [
    sprout 1 [
      set shape "circle"
      set size 0.5
    ]
  ]
end

The code above works as long as you are happy for your world to be the size of exactly 100 patches, given that the size = 100 is hard-coded.

You might want to think about some way to accomodate a change in the number of agents. For example, the approach below works as long as the number of agents is the perfect square of an integer:

globals [
  n-agents
]

to setup
  clear-all
  set n-agents 100

  let side-length n-agents ^ (1 / 2) - 1
  resize-world 0 side-length 0 side-length
  set-patch-size 30
  
  ask patches [
    sprout 1 [
      set shape "circle"
      set size 0.5
    ]
  ]
end

After all the point is that the shape of the world in NetLogo can only be a square or a rectangle; i.e. you cannot have a NetLogo world that is made of a prime number of patches (only exception being a world whose world-height and/or world-width equal 1).

So, in order to have your code be the most accomodating to changes in n, you could come up with more elaborated steps that resize the world based on n so that it gives you n patches even when n is not a perfect square; but for example, unless you are happy to have a monodimensional world, you can never have 53 patches. However, since you are talking of grids, I think this shouldn't be a problem for you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1