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