Reputation:
I would like to know how to find the 'source' of an item in a list. The actions that I need to consider for this task are the following:
1) an item is added in a list created by a turtle;
2) as each turtle has its own list with items created by different turtles, I would like to set a counter that says how often this turtle has picked turtle A's item.
APPROACH & CODE:
This piece of code adds an item (local variable) called 'this_item' in the selected turtle's list:
ask one-of turtles [
set archive fput this_item archive
]
and this other code adds the same item to the neighbours' lists:
ask in-link-neighbors [
set archive fput this_item archive
]
I would set set a local variable, e.g. picked, as the first item from the list.
let picked first archive
To find the source of the item, I thought to use who
. However, who
is used for the turtle who adds the item to its own list, after extracted it.
print (word "Source: " who " has this list " list " after added item " picked)
If I consider a variable source
defined as the source of the item by using myself when a turtle creates a new item, this reports me only the breed of the source (student), but not the corresponding turtle of the source (e.g. student 2).
This makes impossible to count how many times one source's item has been selected.
QUESTION:
How can I count the number of time an item by the same turtle was selected?
Thanks in advance for your help and suggestions.
Upvotes: 3
Views: 350
Reputation: 4168
The easiest way I can think of to do this is to have each this-item
be a two-element list, item 0 being the "item" itself and item 1 being the source turtle. this-item
would then be created by a turtle with
set this-item list x self
(Note that "item" itself is a reserved keyword, so my "item" is x.) Any turtle that puts this-item
in its list would then know that item 0 this-item
was the thing itself and that item 1 this-item
was the turtle from which it came. To count the number of entries in a list, say archive
, owned by a given turtle, say turtle 1
, from a given turtle, say turtle 3
, you could use
ask turtle 1 [show length filter [a -> item 1 a = turtle 3] archive]
If you need the number of times turtle 3
has made entries in all turtles' archives, you can do it in steps. First create a list of all the archive lists.
let all-archives [archives] of turtles
Then use map to go through that list to make a list of the number of entries in each archive and sum that list.
show sum map [t -> length filter [a -> item 1 a = turtle 3] t ] all-archives
(I can't test this right now so check the syntax.) Of course, turtles
could be a subset of all turtles if you like.
Upvotes: 2