【问题标题】:NetLogo: pair lists containing similar elementsNetLogo:包含相似元素的配对列表
【发布时间】:2019-12-27 15:37:42
【问题描述】:

我想知道是否有人可以帮助我解决这个我有点迷茫的问题。我有带有三维布尔列表[a b c] 其中a, b, c in {0, 1} 的海龟。我希望每只海龟都与另一个在列表的所有相同位置上具有1 的海龟创建链接。因此,乌龟应该确定它在其列表中的哪个位置有1,并寻找另一个在每个相同位置都有1 的乌龟。原来的海龟有0,第二个可以有10

即:

乌龟 1 [0 1 0]

乌龟2[1 1 1]

乌龟 3 [1 0 1]

乌龟 4 [0 1 1]

Turtle 1 应该与 Turtle 2 或 Turtle 4 创建链接(因为两者都有 1item 1,第二个位置)但 与 Turtle 3 因为它有一个 0in那个位置。 Turtle 4 应该只创建与 Turtle 2 的链接(1 在第二和第三位置),Turtle 3 也应该创建链接(1 在第一和第三位置),并且 Turtle 2 应该无法创建链接(没有海龟1 在所有三个位置)。

我拥有的是

let candidate one-of turtles with [list1 = [list1] of myself]
create-link-with candidate

这当然行不通,因为海龟会寻找另一个具有完全相同列表(包括零)的列表,而不是仅针对1 具有相同位置的列表。我知道这应该与foreachmapreducefilter有关,但我不能完全正确地理解语法......

祝大家年末快乐

【问题讨论】:

  • 嗨@JenB 感谢您的评论。我想我理解你的意思,但如果我从 Turtle 4 中减去 Turtle 1,那么我将在 abs 中得到 [0 0 -1][0 0 1]。那么问题是我无法区分第一个零和第二个零,而我需要的是乌龟能够分辨出它们有第二个共同元素......除非我没有正确理解。 Turtle 3 与 Turtle 2 的链接可以工作,因为它会给出[0 -1 0],但我认为没有一种简单的方法可以查看何时发生 0,因为两者都有 1 或 0...
  • 再次感谢@JenB。我已经重写了我的问题,希望现在更清楚......我也会尝试两步的想法:)
  • 不要尝试这两个步骤 - 以前不清楚所有 1 必须匹配。我已经稍微编辑了这个问题,但这两个步骤与该要求无关。

标签: netlogo agent-based-modeling


【解决方案1】:

我相信更擅长列表的人将能够使用reduce 或其他聪明的工具来做到这一点。然而,由于position 只给出了第一个位置,我看不到任何矢量化的方式来做到这一点。所以我改用foreach 进行迭代。

to testme
  let list1 [0 1 0]
  let list2 [1 1 1]
  let list3 [1 0 1]
  let list4 [0 1 1]
  type "check 1, 2: " print match-ones list1 list2
  type "check 1, 3: " print match-ones list1 list3
  type "check 1, 4: " print match-ones list1 list4
  type "check 2, 1: " print match-ones list2 list1
  type "check 2, 3: " print match-ones list2 list3
  type "check 2, 4: " print match-ones list2 list4
  type "check 3, 1: " print match-ones list3 list1
  type "check 3, 2: " print match-ones list3 list2
  type "check 3, 4: " print match-ones list3 list4
  type "check 4, 1: " print match-ones list4 list1
  type "check 4, 2: " print match-ones list4 list2
  type "check 4, 3: " print match-ones list4 list3
end

to-report match-ones [#source #target]
  foreach range length #source
  [ x -> if item x #source = 1 and item x #target != 1
    [ report false
    ]
  ]
  report true
end

报告程序采用第一个列表并简单地检查每个项目。如果它是 1 并且另一个列表没有 1,那么程序会报告 false(并且在不测试其他列表的情况下结束)。如果这从未发生过,程序会报告true

testme 程序只是用来调用该程序并检查您的测试数据。代码是一个完整的模型。

【讨论】:

  • 是的,这与我所追求的完全一样!坦克很多,如果我挣扎,我会回来,但我认为我应该能够适应我的问题。 2020 年过得愉快!
  • 查看我对几个替代版本的回答
【解决方案2】:

这是一个递归解决方案:

to-report match-ones [source target]
  if empty? source [ report true ]
  if first source = 1 and first target != 1 [ report false]
  report match-ones butfirst source butfirst target
end

还有一个使用foreach而不使用索引的版本:

to-report match-ones [source target]
  (foreach source target [[?s ?t] ->
    if ?s = 1 and ?t != 1 [ report false ]
  ])
  report true
end

我认为最后一个版本可能是最清晰的,但这是个人喜好问题。

【讨论】:

  • 非常感谢赛斯,我离开了我的电脑,但这确实是解决问题的一种明确方法(在纸上,第二个确实对我来说看起来更清晰)。我会尽快尝试。 2020 年最美好的祝愿
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 1970-01-01
  • 2014-05-14
相关资源
最近更新 更多