user1993416
user1993416

Reputation: 768

Ask the id of another agent when are together and check it is not repeated

Walkers (dynamic) moves between nodes connecting links. Some nodes have events (static) agents. I would like to save the id's of those events in a walker-own list (eve-ids). Now I am doing:

move-to n ; n is a next node
if any? events-here [ 
   let event-x events-here
   set eve-ids lput ([who] of event-x) eve-ids 
]

This lines write the event breed id into eve-ids, but I would like to know that is not a repeated event already found by the walker (i.e. only write event id's the first time they are found).

Thank you very much for your help

Upvotes: 0

Views: 100

Answers (1)

Charles
Charles

Reputation: 4168

I can think of two ways to approach this. Both recognize that events-here produces an agentset, so [who] of event-x yields a list of event agents. Other things being equal, therefore, eve-ids will be a list of lists of agent who numbers.

One approach is to make the list of lists into a simple list and then use remove-duplicates to weed out any duplicate events. In NetLogo v.5.3, the first step could be done using reduce and sentence as follows;

set eve-ids lput event-x eve-ids   ;add the event-x list to eve-ids
set eve-ids reduce [sentence ?1 ?2] eve-ids  ;collapses eve-ids to a simple list
set eve-ids remove-duplicates eve-ids   ;insures no duplicate events

Of course if this is done each time new events are found, then eve-ids will be collapsed to a simple list each time, which then suggests a different approach using foreach (although the same might be done with map). Here we just append the events in event-x not already found in eve-ids.

foreach event-x [
  if not member? ? eve-ids [set eve-ids lput ? eve-ids]
]

Now I am going to channel the NetLogo gurus and suggest that unless you really need them, you should not be using who numbers at all, but rather agentsets for your events. This also makes life simpler as an agentset can not contain duplicates. So, if you do something like:

set eve-ids no-turtles  ;start with an empty set of events.

in initiating your walker agents.

Then you code could be:

move-to n ; n is a next node
if any? events-here [ 
   let event-x events-here
   set eve-ids (turtle-set eve-ids event-x)
]

and you will have a growing set of event agents with no duplicates.

Hope this helps - and if you are using NetLogo v6.0, the reduce and foreach coding will be a bit different as then will use anonymous reporters.

Charles

Upvotes: 1

Related Questions