Question

I would like to report a count of the number of common features (e.g. [1 8 4] is three features) an agent (target_turtle) shares with each agent in an agentset (neighbor_turtle). Any suggestions please?

For example: If the agent has the features [1 8 7] and an agent from the agent set has the features [1 7 8], they share one common feature i.e. 1 . The features 8 and 7 are not included as the order of the features is relevant.

The current error I get is: All the list arguments to FOREACH must be the same length.

Cheers, Marshall

;; reporting overlap between two agents

to-report overlap_between [target_turtle neighbor_turtle]
  let suma 0
  ask neighbor_turtle 
  [
  (foreach [feature] of target_turtle [Feature] of neighbor_turtle
    [ if ?1 = ?2 [ set suma suma + 1]  ]
    )
  report suma
  ]
end
Was it helpful?

Solution

Your code seems almost correct already, though the ask neighbor_turtle part isn't necessary; you're already using of to switch perspectives.

The error message you're getting seems to indicate that you need to somehow handle the case where the turtle's feature lists aren't the same length.

I'll assume you just want to ignore any trailing items in the longer of the two lists. Here's code that does that:

to-report overlap-between [target-turtle neighbor-turtle]
  let features1 [feature] of target-turtle
  let features2 [feature] of neighbor-turtle
  ifelse length features1 > length features2
    [ set features1 sublist features1 0 length features2 ]
    [ if length features2 > length features1
      [ set features2 sublist features2 0 length features1 ] ]
  report sum (map [ifelse-value (?1 = ?2) [1] [0]]
                features1 features2)
end

Note that it's idiomatic in NetLogo to name variables like-this not like_this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top