如何在netlogo中列出累积金额

时间:2015-11-06 15:54:36

标签: simulation netlogo agent-based-modeling

如何列出其他列表的累计总和?

我试过这样的方式:

;;all temperatrue-values around the turtle saved in list 
set temperature_values (list [(output-heat + 1)^ Freedom] of neighbors) 


;;build cumulative value of temperatures and put each value in list 
let tempsum 0 
set tempsum_list [] 
foreach temperature_values 
[set tempsum (tempsum + ? ) 
set tempsum_list fput tempsum tempsum_list 

] 

但它不起作用。有谁能解决这个问题?它说“+除了输入,而是获取列表”。

4 个答案:

答案 0 :(得分:3)

累积金额的代码有效(除了我认为您需要lput而不是fput。您可以看到它:

to test
  let ll [1 2 3 4] 
  let tempsum 0 
  let tempsum_list [] 
  foreach ll 
  [ set tempsum (tempsum + ? ) 
    set tempsum_list lput tempsum tempsum_list 
  ]
  print tempsum_list
end

错误是否突出显示第set temperature_values (list [(output-heat + 1)^ Freedom] of neighbors)行?尝试在之间放置一个空格)和^。 NetLogo对数学运算符周围的空间很挑剔。

答案 1 :(得分:1)

正如Jen建议的那样,您可以使用foreach。另一个不错的方法是减少:

to-report partial-sums [#lst]
  set #lst (fput [0] #lst)  ;;prepare for reduce
  report butfirst reduce [lput (?2 + last ?1) ?1] #lst
end

答案 2 :(得分:0)

这就像艾伦的解决方案,只是抽象了一点。 (也许太过分,取决于你的口味!我也喜欢JenB的解决方案。)

让我们首先定义像reduce之类的东西,但这样可以保留所有中间结果:

to-report scan [fn xs]
  report reduce [lput (runresult fn ?2 last ?1) ?1]
                (fput (list first xs) butfirst xs)
end

现在我们可以用它来计算部分和:

observer> show scan task + [1 2 3 4 5]
observer: [1 3 6 10 15]

但我们也可以自由交换其他操作:

observer> show scan task * [1 2 3 4 5]
observer: [1 2 6 24 120]

答案 3 :(得分:0)

与Alan的解决方案类似(只是对最近版本的NetLogo的更新,用于替换?with - >用于匿名程序。)

to-report partial-sums [lst]
report butfirst reduce [[result-so-far next-item] -> lput (next-item + last 
result-so-far) result-so-far] fput [0] lst
end