Reputation: 789
In my model the behaviour of turtles is defined by a combination of different procedures, depending of setup parameters. Let's say I have a code:
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
(Actually, I have now 3 such parameters each with 2 or 3 possible values, but I have to add more as I develop my model) This works, but is is very clumsy. Both parameters are constant, set up at the beginning of the simulation, and the situation when each turtle at each time-step have to ask about the value of these parameters is very ineffective. Is it possible to ask it only once at the beginning of the simulation? Something like:
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
Upvotes: 2
Views: 141
Reputation: 14972
Sure, you can do this! This is what tasks are for!
Here is a variant of your code using 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
The task
primitive stores a command block in a variable and you can use run
to execute that code later.
And if you already have a procedure defined, you don't need to provide a code block: you can pass the procedure name directly to task
. I used blocks like [ show "1A" ]
in my example, but in your case, you can probably do something like:
if (parameter1 = "A") [ set behaviour1 task behaviour-1A ]
Upvotes: 3