嵌套 Foreach 列表 NetLogo
Nested Foreach List NetLogo
嗨,
在 Netlogo v.6 中,我正在尝试计算对等方合作行为的时间加权度量。关键是,我不知道如何在 NetLogo 中做一个嵌套的 foreach。
我目前的做法:
- 设置列表:reputation_peer
- 设置一个列表:reputation_peer_list,包含变量 1(行为)和变量 2(时间刻度)
- 每次遇到,将reputation_list添加到reputation_peer,使reputation_peer成为列表的列表
- 计算加权信誉:
- 取reputation_peer中每个列表的第2项,这是那次遭遇的时间标记
- 并将其除以当前时间刻度。
- 这给出了一个分数:encounter/total 时间滴答
- 设置列表reputation_weighted
- 为了衡量声誉,将时间分数乘以 reputation_peer 的第一项,即行为变量
- 然后,我想通过从 reputation_weighted 中获取所有值并将它们相加来了解 reputation_current。
我正在弄乱 foreach 函数,但我似乎无法弄明白。
这种方法将如何在 Netlogo 中编码?
我现在的代码是(设置reputation_peer自己测试):
to calculate_reputation
set reputation_peer [[8 4][9 2][10 3][11 2][14 1]]
if ticks > 0 [
foreach reputation_peer [x -> set reputation_peer_list list (item 0 x) ((item 1 x) / ticks )
set reputation_peer_list_2 lput reputation_peer_list reputation_peer_list_2]
foreach reputation_peer_list_2 [x -> set reputation_peer_list_list (list (item 0 x * item 1 x))]
foreach reputation_peer_list_list [x -> set reputation_peer_current reputation_peer_current + x]
]
end
我真的不知道我做的是否正确,但主要是,这段代码看起来非常笨重,所有 list_list 事情都在进行。我猜它可能会简单得多。
如果你们有一些提示,将极大地帮助我。
我不能 100% 确定您对最终输出的期望,但也许这就是您所需要的?
to calc-rep-2
set reputation_peer [[8 4][9 2][10 3][11 2][14 1]]
let weighted_rep_list []
if ticks > 0 [
foreach reputation_peer [ x ->
; Pull out the values from reputation_peer for ease of use
let encounter_behavior item 0 x
let encounter_time item 1 x
; Calculate the time fraction for the current item
let time_fraction encounter_time / ticks
; Calculate the weighted reputations
let weighted_rep encounter_behavior * time_fraction
; Add the weighted rep to the list of weighted reps
set weighted_rep_list lput weighted_rep weighted_rep_list
]
; Now, weighted_rep_list is a list of weighted reputations
print weighted_rep_list
; Get the sum of the list
print sum weighted_rep_list
]
tick
end
嗨,
在 Netlogo v.6 中,我正在尝试计算对等方合作行为的时间加权度量。关键是,我不知道如何在 NetLogo 中做一个嵌套的 foreach。
我目前的做法:
- 设置列表:reputation_peer
- 设置一个列表:reputation_peer_list,包含变量 1(行为)和变量 2(时间刻度)
- 每次遇到,将reputation_list添加到reputation_peer,使reputation_peer成为列表的列表
- 计算加权信誉:
- 取reputation_peer中每个列表的第2项,这是那次遭遇的时间标记
- 并将其除以当前时间刻度。
- 这给出了一个分数:encounter/total 时间滴答
- 设置列表reputation_weighted
- 为了衡量声誉,将时间分数乘以 reputation_peer 的第一项,即行为变量
- 然后,我想通过从 reputation_weighted 中获取所有值并将它们相加来了解 reputation_current。
我正在弄乱 foreach 函数,但我似乎无法弄明白。 这种方法将如何在 Netlogo 中编码?
我现在的代码是(设置reputation_peer自己测试):
to calculate_reputation
set reputation_peer [[8 4][9 2][10 3][11 2][14 1]]
if ticks > 0 [
foreach reputation_peer [x -> set reputation_peer_list list (item 0 x) ((item 1 x) / ticks )
set reputation_peer_list_2 lput reputation_peer_list reputation_peer_list_2]
foreach reputation_peer_list_2 [x -> set reputation_peer_list_list (list (item 0 x * item 1 x))]
foreach reputation_peer_list_list [x -> set reputation_peer_current reputation_peer_current + x]
]
end
我真的不知道我做的是否正确,但主要是,这段代码看起来非常笨重,所有 list_list 事情都在进行。我猜它可能会简单得多。
如果你们有一些提示,将极大地帮助我。
我不能 100% 确定您对最终输出的期望,但也许这就是您所需要的?
to calc-rep-2
set reputation_peer [[8 4][9 2][10 3][11 2][14 1]]
let weighted_rep_list []
if ticks > 0 [
foreach reputation_peer [ x ->
; Pull out the values from reputation_peer for ease of use
let encounter_behavior item 0 x
let encounter_time item 1 x
; Calculate the time fraction for the current item
let time_fraction encounter_time / ticks
; Calculate the weighted reputations
let weighted_rep encounter_behavior * time_fraction
; Add the weighted rep to the list of weighted reps
set weighted_rep_list lput weighted_rep weighted_rep_list
]
; Now, weighted_rep_list is a list of weighted reputations
print weighted_rep_list
; Get the sum of the list
print sum weighted_rep_list
]
tick
end