netlogo

ask turtles - ordering of agentset


I would like to apply some process stochastically, but the order matters, or rather it should be done at random, how can I select a set of the same turtles to "treat" but to do it such that each tick, the order is random?

ask turtles [
  ;; apply a process to turtles, but not all turtles will get something as the pool of energy may have run out by the time they are selected.
]

Solution

  • Elements of agentsets (such as turtles) are always returned in a random order. Each time you use ask with an agentset, it will ask them in a new, random order. From the docs on agentsets:

    An agentset is not in any particular order. In fact, it’s always in a random order. And every time you use it, the agentset is in a different random order. This helps you keep your model from treating any particular turtles, patches or links differently from any others (unless you want them to be). Since the order is random every time, no one agent always gets to go first.

    And here is a quick example to demonstrate. If you run it in the Command Center you'll see the who numbers will be different each time you ask them to be shown.

    to test
      clear-all
      create-turtles 10
      show "Asking once..."
      ask turtles [ show who ]
      show "Asking a second time..."
      ask turtles [ show who ]
    end
    

    And here is an example showing an energy pool that will be randomly used until it is gone. Note the turtles that will get to use it are whichever happen to come first out of the agentset for ask:

    to test-pool
      clear-all
      let energy-pool 1000
      create-turtles 100 
      ask turtles [
        ; can only act if some energy is left...
        ifelse energy-pool > 0 [
          let energy-use (min (list random 100 energy-pool))
          set energy-pool (energy-pool - energy-use)
          show (word "I used up " energy-use " energy points! " who "  Only " energy-pool " points left")
          
        ] [
          show (word "No energy left for me! " who)
        ]
      ]
    end