How to have a turtle choose max-one-of from nearby turtles?

Viewed 12

The situation is very simple: there are some turtles, producers, and others, choosers. Producers produce something with a certain objective-quality, choosers choose on the basis on perceived quality.

Choosers should be able to choose only from producers in Moore Neighbour, and can choose only one producer. Once chosen, they create a local for the chosen producer.

Here is what I have to begin with:

ask turtles with [group = "choosers"] [
    let friends turtles-on neighbors
    let x friends with-max [perceived-quality]
    ask x [set chosen? (true) ]
  ]

Of course, with this code turtles choose all those with max perceived-quality (>1).

If I use max-one-of, there is an error, because not all the turtles have “friends”.

How can I do it?

1 Answers

max-one-of indeed always expects there to be at least 1 option to choose from. An easy way to work around it is to first check if there is an option to choose from before trying to choose one.

ask turtles with [any? turtles-on neighbors] [
    ask max-one-of turtles-on neighbors perceived-quality [...]
]
Related