Josh Finkill
Josh Finkill

Reputation: 27

How do you spawn multiple turtles in multiple locations on NetLogo?

I am creating a simulation which has multiple bee hives around the area, these are represented by brown patches. I would like multiple turtles to spawn on all of hives upon setup, however when using my code it can only spawn turtles in one hive. I have been trying it by using preset coordinates.

    to setup-bees

  create-turtles number-of-bees
  ask turtles [ setxy 0 0 ]
  ask turtles [ set pcolor brown ]
  ask turtles [ set size 1.5 ]
  ask turtles [ set color red]
  ask turtles [
  set xy_list (list)
  ]

    create-turtles number-of-bees
  ask turtles [ setxy -80 0 ]
  ask turtles [ set pcolor brown ]
  ask turtles [ set size 1.5 ]
  ask turtles [ set color red]
  ask turtles [
  set xy_list (list)
  ]

      create-turtles number-of-bees
  ask turtles [ setxy 80 0 ]
  ask turtles [ set pcolor brown ]
  ask turtles [ set size 1.5 ]
  ask turtles [ set color red]
  ask turtles [
  set xy_list (list)
  ]

end

This is all I have and only spawns the turtles on the last location. Any help would be great, thanks.

Upvotes: 1

Views: 556

Answers (1)

Charles
Charles

Reputation: 4168

The problem is that each of the ask turtles applies to all of the turtles that have been created up to that point. So, what is really happening, is that the first batch of turtles originally created and placed at 0,0, are then asked to move to -80 0 along with the second batch of turtles, and then asked to move (along with the second batch of turtles) to 80 0 with the third batch created. What you want is to have the setxy and the other asks apply to only the batch being created. That would look like

turtles-own [xy_list]
globals [number-of-bees]
to setup-bees
  clear-all
  set number-of-bees 10

  create-turtles number-of-bees [
    setxy 0 0 
    set pcolor brown 
    set size 1.5 
    set color red
    set xy_list (list)
  ]

  create-turtles number-of-bees [
    setxy -80 0
    set pcolor brown 
    set size 1.5 
    set color red
    set xy_list (list)
  ]

  create-turtles number-of-bees [
    setxy 80 0
    set pcolor brown 
    set size 1.5 
    set color red
    set xy_list (list)
  ]
end

The commands in the brackets after each create-turtles apply only to the turtles being created. Thus you could have bees in different hives being different colors or sizes.

A question and an observation. I assume that the size of your world accommodates x coordinates of +-80? And set xy_list (list) can simply be set xy_list [].

By the way, another approach would be to have the hive sprout the bees. E.g.,

ask patch 0 0 [
  set pcolor brown
  sprout number-of-bees 
    [
    set size 1.5 
    set color red
    set xy_list []
    ]
]

Upvotes: 2

Related Questions