Search code examples
netlogo

Different types of turtles in a lattice grid


In Netlogo there are some default models, including the lattice grid. I am wondering how to add three types/breeds (40%, 40%, 20%) of turtles connected to each others only at the beginning, then, when tick > 0, some new links can be created between turtles of different type.

In the models library in Netlogo, I found the code for a regular lattice:

to setup-square
  clear-all
  ask patches [ sprout 1 ]
  ask turtles [ create-links-with turtles-on neighbors4 ]
  reset-ticks
end

Instead of turtles, I should consider the three types (types1, types2, types3) in order to have a lattice with:

  • 40% of turtles as breed type1 (red);
  • 40% of turtles as breed type2 (blue);
  • 20% of turtles as breed type3 (white).

Initially, all the turtles above are connected to other turtles of the same type. Links between turtles may change through time, so links between turtles of different breeds can exist when tick>0.

I would do as follows (instead of ask turtles [...]):

ask types1 [ create-links-with types1-on neighbors4 ]
ask types2 [ create-links-with types2-on neighbors4 ]
...

but the problem is that I have no control on the proportion of turtles for each type. Also, a condition on the breed of the neighbors of a turtle should be considered as well. How can I set the proportion of these types and make connections only between turtles on the same type, then mix links?


Solution

  • There are several ways to do this, some of them more exact than others. It is especially important to realise that you can change a turtles breed after it has been created, simply by asking it to set breed <breed>.

    In the first example, I give all turtles type1 as they are created, and then change a predefined number of them to another type. Keep in mind how you get that number as rounding errors can have an effect here (see further down for the counts I had). It is important that you give them all a breed at first, as this version will not work with ask n-of <percentage> turtles [set breed <breed>]. That is because the agentset turtles will always contain all turtles, even those who you have given a different breed.

    breed [types1 type1]
    breed [types2 type2]
    breed [types3 type3]
    
    to setup-shape
      
      set-default-shape types1 "default"
      set-default-shape types2 "x"
      set-default-shape types3 "square"
      
    end
    
    to setup-1
      
      ca
      setup-shape
      ask patches [sprout-types1 1 ]
      let total count turtles
      ask n-of (total * 0.4) types1 [set breed types2]
      ask n-of (total * 0.2) types1 [set breed types3]
      
    end
    

    A second version that works with a more sochastic approach is to let each newly created turtle choose random number, and give it a breed depending on the number. Keep in mind that the proportions will vary more, as they are determined by chance rather than exact numbers.

    to setup-2
      
      ca
      setup-shape
      ask patches [ sprout 1 [
        let number random-float 1.0
        show number
        (ifelse number < 0.4 [set breed types1]
          number < 0.8 [set breed types2]
          [set breed types3])
        ]
      ]
    
      
    end
    

    The counts I had of the different versions for each type. Notice that in version 1, type1 and type2 have a different count because I let all turtles start with the breed types1.

    to count-types
      
      ;standard world of 33x33
      show count types1 ; version 1: 437 , version 2: 454
      show count types2 ; version 1: 435 , version 2: 432
      show count types3 ; version 1: 217 , version 2: 203
      
    end