最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

netlogo - How to count the number of different variables for an agent attribute? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited 2 days ago desertnaut 60.5k32 gold badges155 silver badges181 bronze badges asked Mar 31 at 15:18 Elliot Howard-SpinkElliot Howard-Spink 236 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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!

发布评论

评论列表(0)

  1. 暂无评论