1

Background

I used this answer to select a turtle based on a probability distribution determined by the turtle's popularity or fitness.

Issue

I am trying to pass a parameter that determines which of the turtle's property the probability distribution is determined by.

Question

How can I perform an equivalent of a "python unpacking" of a parameter in netlogo?

Sample code

turtles-own
[
  fitness
  strength
  degree     ;;Node's Connectness
  popularity
  wealth
]

to-report pick-turtle-biased-by-property [turtle-list property-to-unpack]
  let prob-list []
  let turtle-list []

  ask turtles [ 
       set prob-list lput [[property-to-unpack] of self ] prob-list
       set turtle-list lput self turtle-list
  ]

  report first rnd:weighted-one-of-list (map list turtle_list prob-list) last
end
Community
  • 1
  • 1
Gabriel Fair
  • 4,081
  • 5
  • 33
  • 54

1 Answers1

3

The key to what you're trying to do is to use "anonymous reporters" for passing the "property to unpack". See the Anonymous procedures section of the programming guide.

Here is a full example:

extensions [ rnd ]

turtles-own [
  strength
  wealth
]

to setup
  clear-all
  create-turtles 10 [
    set strength random 100
    set wealth random 100
  ]
end

to go
  print pick-turtle-biased-by-property [ -> strength ]
  print pick-turtle-biased-by-property [ -> wealth ]
end

to-report pick-turtle-biased-by-property [ property-to-unpack ]
  let pairs [ (list self runresult property-to-unpack) ] of turtles  
  report first rnd:weighted-one-of-list pairs last
end
Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37