NetLogo:包含相似元素的对列表
NetLogo: pair lists containing similar elements
我想知道是否有人可以帮助解决这个我有点迷茫的问题。我的海龟具有三维布尔列表 [a b c]
,其中 a, b, c in {0, 1}
。我希望每只乌龟都创建一个 link 与另一个在列表的所有相同位置上具有 1
的乌龟。因此,乌龟应该确定它在其列表中的哪个位置具有 1
并寻找在每个相同位置具有 1
的另一只乌龟。在原始乌龟有 0
的地方,第二个可以有 1
或 0
.
即:
乌龟 1 [0 1 0]
乌龟 2 [1 1 1]
乌龟 3 [1 0 1]
海龟 4 [0 1 1]
Turtle 1 应该用 Turtle 2 或 Turtle 4 创建 links(因为两者在 item 1
上都有 1
,第二个位置)但是 没有 与海龟 3 因为它在那个位置有一个 0
。海龟 4 应该只用海龟 2 创建一个 link(1
在第二和第三个位置),海龟 3 也应该(1
在第一和第三个位置),而海龟 2 应该无法创建 links(所有三个位置都没有 1
的海龟)。
我有的是
let candidate one-of turtles with [list1 = [list1] of myself]
create-link-with candidate
这当然行不通,因为乌龟会寻找另一个具有完全相同列表(包括零)的乌龟,而不是仅 1
具有相同位置的乌龟。我知道这应该与 foreach
、map
、reduce
和 filter
有关,但我不太理解语法正确...
祝大家年末快乐
我相信更擅长列表的人将能够使用 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 过程只是用来调用该过程并检查您的测试数据。代码是一个完整的模型。
这是一个递归的解决方案:
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
我认为最后一个版本可能是最清晰的,但这是个人喜好问题。
我想知道是否有人可以帮助解决这个我有点迷茫的问题。我的海龟具有三维布尔列表 [a b c]
,其中 a, b, c in {0, 1}
。我希望每只乌龟都创建一个 link 与另一个在列表的所有相同位置上具有 1
的乌龟。因此,乌龟应该确定它在其列表中的哪个位置具有 1
并寻找在每个相同位置具有 1
的另一只乌龟。在原始乌龟有 0
的地方,第二个可以有 1
或 0
.
即:
乌龟 1 [0 1 0]
乌龟 2 [1 1 1]
乌龟 3 [1 0 1]
海龟 4 [0 1 1]
Turtle 1 应该用 Turtle 2 或 Turtle 4 创建 links(因为两者在 item 1
上都有 1
,第二个位置)但是 没有 与海龟 3 因为它在那个位置有一个 0
。海龟 4 应该只用海龟 2 创建一个 link(1
在第二和第三个位置),海龟 3 也应该(1
在第一和第三个位置),而海龟 2 应该无法创建 links(所有三个位置都没有 1
的海龟)。
我有的是
let candidate one-of turtles with [list1 = [list1] of myself]
create-link-with candidate
这当然行不通,因为乌龟会寻找另一个具有完全相同列表(包括零)的乌龟,而不是仅 1
具有相同位置的乌龟。我知道这应该与 foreach
、map
、reduce
和 filter
有关,但我不太理解语法正确...
祝大家年末快乐
我相信更擅长列表的人将能够使用 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 过程只是用来调用该过程并检查您的测试数据。代码是一个完整的模型。
这是一个递归的解决方案:
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
我认为最后一个版本可能是最清晰的,但这是个人喜好问题。