netlogo - 使用reduce 乘以NetLogo 中的列表

标签 netlogo reduce

我正在尝试调试我的 NetLogo 代码, 我想根据邻居的概率创建一个列表,然后使用“reduce”将列表中的所有这些值相乘。据我所知,这是有效的,除非它在运行时给出如下运行时错误。

我也尝试使用“让 P 减少 [?1 * ?2] 概率列表”,但它给出了相同的错误。

注意 Prob_water_water_writing、Prob_solute_solute_writing 被定义为“全局变量”,然后在设置中将它们分配给特定品种。

to-report all-probabilities  
    let prob-list (list ([Prob_water_water_breaking ] of turtles-on neighbors4) 
      ([Prob_solute_solute_breaking ] of turtles-on neighbors4)
     let P reduce  *  prob-list ;;this is the line that's causing the error
     report P
  end

这是运行时错误:

* expected input to be a number but got the list [0.3 0.3 0.3] instead.
error while solute 2 running *
  called by procedure ALL-PROBABILITIES
  called by procedure GO
  called by Button 'go'

任何帮助将不胜感激,非常感谢!

最佳答案

啊!我喜欢涉及 reduce 的问题。但就你而言,问题出在这一行:

let prob-list (list ([Prob_water_water_breaking ] of turtles-on neighbors4) 
      ([Prob_solute_solute_breaking ] of turtles-on neighbors4)

您(和 reduce)期望得到一个数字列表,但 prob-list 实际上是一个列表列表:每次调用 of生成一个列表,每个列表都会成为您使用 list 创建的列表中的一个项目。 .

由于您想将列表连接在一起,因此可以使用 sentence而不是列表:

let prob-list (sentence ([Prob_water_water_breaking ] of turtles-on neighbors4) 
      ([Prob_solute_solute_breaking ] of turtles-on neighbors4))

避免该问题的另一种(可能更好)方法是在 of 内执行第一次乘法:

let prob-list [ Prob_water_water_breaking * Prob_solute_solute_breaking ]
  of turtles-on neighbors4

关于netlogo - 使用reduce 乘以NetLogo 中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27052995/

相关文章:

netlogo - 在 NetLogo 中使用向量

Netlogo - 在特定事件后停止模拟

Netlogo 重置勾选错误消息

arrays - 在 Swift 4 中使用 reduce 时出现 "contextual closure type expects 2 arguments"错误

java - MapReduce canopy 聚类中心

算法最小化播放列表而不改变播放

netlogo - 如何与合作者共享我的 netlogo 模型而不透露或发送实际代码?

version-control - NetLogo 模型的版本控制

对象上的 Javascript reduce()

javascript - 使用reduce函数汇总/统计数组