计算代理与代理集共享的常用功能的数量

时间:2014-05-13 13:31:54

标签: netlogo

我想报告一个共同特征数量的计数(例如[1 8 4]是三个特征)代理(target_turtle)与代理集(neighbor_turtle)中的每个代理共享。有什么建议吗?

例如:如果代理具有[18 7]特征并且代理集中的代理具有特征[1 7 8],则它们共享一个共同特征,即1。功能8和7不包括在内,因为功能的顺序是相关的。

我得到的当前错误是:FOREACH的所有列表参数必须是相同的长度。

干杯, 马歇尔

;; 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

1 个答案:

答案 0 :(得分:0)

您的代码似乎已经几乎正确,但ask neighbor_turtle部分并非必要;您已经使用of来切换观点。

您收到的错误消息似乎表明您需要以某种方式处理乌龟的功能列表长度不相同的情况。

我假设您只想忽略两个列表中较长的任何尾随项目。这是代码:

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

请注意,NetLogo中的惯用项是将变量like-this命名为like_this