NetLogo:在设置中选择一个程序?

标签 netlogo procedures

在我的模型中,海龟的行为是由不同过程的组合定义的,具体取决于设置参数。假设我有一个代码:

to go
  ask turtles [
    if (parameter1 = "A") [behaviour-1A]
    if (parameter1 = "B") [behaviour-1B]

    if (parameter2 = "a") [behaviour-2a]
    if (parameter2 = "b") [behaviour-2b]

    ...
  ]
end

(实际上,我现在有 3 个这样的参数,每个参数都有 2 或 3 个可能的值,但在开发模型时我必须添加更多)这可行,但非常笨拙。这两个参数都是恒定的,在模拟开始时设置,当每只海龟在每个时间步都必须询问这些参数的值时,这种情况是非常无效的。是否可以在模拟开始时只询问一次?像这样的东西:

if (parameter1 = "A") [set behaviour1 behaviour-1A]
if (parameter1 = "B") [set behaviour1 behaviour-1B]

if (parameter2 = "a") [set behaviour2 behaviour-2a]
if (parameter2 = "b") [set behaviour2 behaviour-2b]

...

to go
  ask turtles [
    behaviour1
    behaviour2
  ]
end

最佳答案

当然,你可以做到!这就是tasks是为了!

这是使用任务的代码的变体:

globals [
  behaviour1
  behaviour2
]

to setup
  crt 2
  if (parameter1 = "A") [ set behaviour1 task [ show "1A" ]]
  if (parameter1 = "B") [ set behaviour1 task [ show "1B" ]]  
  if (parameter2 = "a") [ set behaviour2 task [ show "2a" ]]
  if (parameter2 = "b") [ set behaviour2 task [ show "2b" ]]
end

to go
  ask turtles [
    run behaviour1
    run behaviour2
  ]
end

task原语将命令 block 存储在变量中,您可以使用 run稍后执行该代码。

如果您已经定义了一个过程,则不需要提供代码块:您可以将过程名称直接传递给 task 。我在示例中使用了诸如 [ show "1A"] 之类的 block ,但在您的情况下,您可能可以执行以下操作:

if (parameter1 = "A") [ set behaviour1 task behaviour-1A ]

关于NetLogo:在设置中选择一个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25197812/

相关文章:

python - 为什么这段 Python 代码可以正常工作?请解释

mysql - 为什么我的 SQL 过程返回整个表?

netlogo - 如何 'ask'列表中的所有海龟

NetLogo rnd :weighted-n-of by turtle variable

memory - NetLogo:最后两个价格变动的全局变量的值可以存储在列表中并在过程中调用吗?

MySQL日期计算和选择大于55

sql - 具有像 where 0=0 这样的条件的确切含义是什么?

php - 从 PHP 创建存储过程

java - 如何使 NetLogo 5.x 中的扩展返回值 "nobody"?

netlogo - 如何改变相邻补丁的pcolor?