我想知道是否有任何网络徽标专家可以帮助我完成以下工作:假设我的补丁有两个属性(比如attr1和attr2 ),我需要计算这两个属性的函数,就像下面这个愚蠢的例子
patches-own [ attr1 attr2]
to setup
clear-all
ask patches [set attr1 random 10]
ask patches [set attr2 random 20]
let mean-1 mean [attr1] of patches
let mean-2 mean [attr2] of patches
show mean-1
show mean-2
end我想编写一个过程来计算传递给它的任何属性的平均值(比如说)。类似于:
ask patches [set attr1 random 10]
ask patches [set attr2 random 20]
compute-mean attr1
compute-mean attr2
end
to compute-mean #attr
let mean-1 mean [#attr] of patches
show mean-1
end有人能帮忙吗?先谢谢你
发布于 2022-09-12 09:22:43
compute-mean [attr1] of patches要容易得多,不必费心地提取过程中的值。它还使您的过程更加灵活,允许您将其用于修补程序子集。如果您希望一个过程是一个计算过程,最好使用to-report将其写成一个报告过程。这样,您也可以使用它在代码的其他部分中生成的输出,而不仅仅是显示它。
to go-1
show compute-mean-1 [attr1] of patches
show compute-mean-1 [attr2] of patches
end
to-report compute-mean-1 [#attr-list]
let mean-1 mean #attr-list
report mean-1
end现在,如果您真的只想传递您的过程--属性的名称,而不是值的列表--则需要将属性的名称作为字符串传递给它,然后使用run-result将该字符串("attr1")解释为一个记者(attr1)。
to go-2
show compute-mean-2 "attr1"
show compute-mean-2 "attr2"
end
to-report compute-mean-2 [#attr-name]
let mean-2 mean [run-result #attr-name] of patches
report mean-2
endhttps://stackoverflow.com/questions/73648022
复制相似问题