may*_*cca 8 patch report netlogo
我有一个问题需要理解to-report
和report
在NetLogo中的作用,即使它看起来非常有用,我也无法找到用"人类风格"语言编写的帮助.
在NetLogo dictionnary http://ccl.northwestern.edu/netlogo/docs/dictionary.html#report中 我可以找到以下定义to-report
:
to-report procedure-name
to-report procedure-name [input1 ...]
Used to begin a reporter procedure.
The body of the procedure should use report to report a value for the procedure. See report.
Run Code Online (Sandbox Code Playgroud)
并为report
:
report value
Immediately exits from the current to-report procedure and reports value as the result of that procedure. report and to-report are always used in conjunction with each other. See to-report for a discussion of how to use them.
Run Code Online (Sandbox Code Playgroud)
所以,似乎to-report
并report
计算一些价值并报告它.
因此,当我尝试添加
to-report average [a b c]
report (a + b + c) / 2
end
Run Code Online (Sandbox Code Playgroud)
到我的代码,然后average
在我的代码pe中的某处使用变量:
to go
...
print average
tick
end
Run Code Online (Sandbox Code Playgroud)
我有一个错误:AVERAGE expected 3 inputs
.当我尝试创建我的变量[abc]时,globals [a b c]
我遇到了错误There is already a global variable called A
.如果我[a b c]
在to-report
程序中定义我的变量:
to-report average [a b c]
set a 1
set b 2
set c 3
report (a + b + c) / 2
end
Run Code Online (Sandbox Code Playgroud)
我的错误又来了AVERAGE expected 3 inputs
.
那么,我怎样才能简单地测试报告程序的用处呢?在我的代码中将它正确放置在哪里,看看它到底在做什么?来自Urban Suite - 经济差异(http://ccl.northwestern.edu/netlogo/models/UrbanSuite-EconomicDisparity)我看到to-report用于计算与每个补丁相关的值:
to-report patch-utility-for-poor
report ( ( 1 / (sddist / 100 + 0.1) ) ^ ( 1 - poor-price-priority ) ) * ( ( 1 / price ) ^ ( 1 + poor-price-priority ) )
end
Run Code Online (Sandbox Code Playgroud)
但是这个报告的值并没有直接定义为补丁变量,这增加了我的困惑......
谢谢 !
函数可以接受一些输入(通常是一个或多个变量或值)并返回一些输出(通常是单个值)。您可以在函数标头中使用 to-report 指定函数返回一个值,并且 report 返回实际值。
您的错误是由于您从未将参数传递给平均函数
to go
...
print average
tick
end
Run Code Online (Sandbox Code Playgroud)
应该
to go
...
print average 5 2 3 ;;a = 5, b = 2, c =3
tick
end
Run Code Online (Sandbox Code Playgroud)
在平均函数中,不应重新分配 a、b 和 c 的值。
每当您想从函数返回结果时,都应该使用报告。