首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Netlogo:以补丁属性作为输入变量的过程

Netlogo:以补丁属性作为输入变量的过程
EN

Stack Overflow用户
提问于 2022-09-08 11:02:40
回答 1查看 37关注 0票数 1

我想知道是否有任何网络徽标专家可以帮助我完成以下工作:假设我的补丁有两个属性(比如attr1和attr2 ),我需要计算这两个属性的函数,就像下面这个愚蠢的例子

代码语言:javascript
复制
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

我想编写一个过程来计算传递给它的任何属性的平均值(比如说)。类似于:

代码语言:javascript
复制
  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

有人能帮忙吗?先谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-12 09:22:43

compute-mean [attr1] of patches要容易得多,不必费心地提取过程中的值。它还使您的过程更加灵活,允许您将其用于修补程序子集。如果您希望一个过程是一个计算过程,最好使用to-report将其写成一个报告过程。这样,您也可以使用它在代码的其他部分中生成的输出,而不仅仅是显示它。

代码语言:javascript
复制
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)。

代码语言:javascript
复制
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
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73648022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档