netlogoagentagent-based-modeling

How to count the number of different variables for an agent attribute?


I have a set of agents called 'trees', which have an attribute 'species' that is a string, e.g. 'species1', 'species2', 'species3' etc.

When the trees have fruits, they are blue. When they have no fruits, they are red.

I want to be able to get the number of different species that have trees with fruits at any different step of my model. I can use the following observer command to get an output that describes the species of each tree with fruits:

ask trees with [color != red] [show species]

However this will return something like

(tree 1)  'species 1'
(tree 18) 'species 3'
(tree 24) 'species 1'
(tree 30) 'species 5'

How can I turn this into something which can count the number of different species that have trees which have fruit? In the above example, it would return 3 (as trees of 'species 1', 'species 3', and 'species 5' still have fruit on them).

The ideal output would therefore be

3

I'd like just a number as this will feed into an equation to make a variable that effects they next timestep of the simulation.


Solution

  • That's an interesting question...

    One possibility:

    a) Create a list with the species of all the trees: let species-list [species] of trees

    b) Remove the duplicate values on that list: set species-list remove-duplicates species-list

    c) Count how many (unique) species are now on the list: let num-species length species-list

    Give it a try!