Search code examples
netlogo

How to create multiple turtles in certain patches of the world quickly using NetLogo 6.2?


I'm trying to solve a problem of speed in creating turtles in the NetLogo world.

I have a model that has a world with a size of 600X600. I, too, have 31 turtle profiles (each turtle can only born in a specific habitatcover type or habitatcover set (see ValidHabs variable in the code). In the code, too, there are 2 variables that are metabolism with 2 levels (list1 in the code) and reproduction with 2 levels (list2 in the code). Also, I would like to have at least 200 turtles born in the world or more in this world.

The problem is that it is taking a long time for turtles to be born in the world. Have I already created a switch called Display? to speed up the creation of the turtles. I also set it to faster speed and it still takes a long time to create the turtles.

Does anyone know how I can speed up the creation of turtles based on my code?

I don't know what else to do to speed up the code... I appreciate any kind of help :)

Thanks in advance

The code below:

globals [ ValidHabs  ValidHabsItem HotPatchSet CurrentHotPatchSet ]

patches-own [ habitatcover ]

turtles-own [ turtle-profiles-habitat metabolism reproduction all-code code-metabolism code-reproduction ]

to setup
  clear-all
  random-seed 1
  ;; ValidHabs are the habitat profiles of the turtles are all combinations of 5 types of habitatcover without repetition
  set ValidHabs [[1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [1 3 4] [1 3 5] [1 4 5] [2 3 4] [2 3 5] [2 4 5] [3 4 5] [1 2 3 4] [1 2 3 5] [1 2 4 5] [1 3 4 5] [2 3 4 5] [1 2 3 4 5]]
  ifelse ( Display? = true ) [ display ] [ no-display ] ;; display is a switch in interface
  resize-world 599 * 0 ( 599 * 1 )  ( 599 * -1 ) 599 * 0 ;; the world is 600X600 in size
  setup-world
  setup-patches
  reset-ticks
  print ( word "That setup took " timer " seconds" )
end

to setup-world
  let pcolors []
  set pcolors [ 25 65 23 53 105 10 ]
  ask patches [
    set pcolor item (random 5) pcolors
  ]

  ask patches [
    if pcolor = 25 [ set habitatcover 1 ]
    if pcolor = 65 [ set habitatcover 2 ]
    if pcolor = 23 [ set habitatcover 3 ]
    if pcolor = 53 [ set habitatcover 4 ]
    if pcolor = 105 [ set habitatcover 5 ]
    if pcolor = 10 [ set habitatcover 6 ]
  ]
end

to setup-patches
  set-patch-size 0.6 ;; view patch size
  set HotPatchSet patches with  [ ( habitatcover != 6 ) ]
  let list1 ( list 2 4 )
  let list2 ( list 5 10 )
  let n0 count turtles
  set CurrentHotPatchSet HotPatchSet with [ habitatcover = one-of item ValidHabsItem ValidHabs ]
  while [ n0 < ( length list1 ) * ( length list2 ) * 200 ] ;; I want 200 ou more turtles in the word. Here I put 200
  [
    (
      foreach list1
      [
        this-metabolism ->
        foreach list2
        [
          this-reproduction ->
          let c count CurrentHotPatchSet
          if c = 0 [ stop ]
          ask one-of CurrentHotPatchSet
          [
            sprout 1
            [
              set turtle-profiles-habitat item ValidHabsItem ValidHabs
              set metabolism this-metabolism
              set reproduction this-reproduction
              setup-turtles who
            ]
            set CurrentHotPatchSet CurrentHotPatchSet with [ not any? turtles in-radius 2 ]
          ]
        ]
      ]
    )
    set n0 count turtles
  ]
end

to setup-turtles [ whichTurtle? ]
  set color black
  ask turtle who [
    (
      ifelse
      metabolism = 2 [set code-metabolism "M1"]
      metabolism = 4 [set code-metabolism "M2"]
    )
    (
      ifelse
      reproduction = 5 [set code-reproduction "R1"]
      reproduction = 10 [set code-reproduction "R2"]
    )
    set all-code ( word code-metabolism code-reproduction )
  ]
end

Solution

  • I'm afraid I did not look for the particular problems in your code, but there is general advice on speeding up models here: http://jasss.soc.surrey.ac.uk/20/1/3.html with updates here: http://www.railsback-grimm-abm-book.com/jasss-models/

    I'm pretty sure that some of the advice applies to your code.