'How does asking an procedure to run in ask patches produce a different output than running the patches as ask patches?
I was working on a NetLogo project for school, when I ran into this issue with my NetLogo code. When I first made the code, I put the procedure death-check in the ask patches command with this being my program:
patches-own [num-live]
to setup ;;this sets up all of the code
clear-all
ask patches [
if random 100 < percent-alive [
cell-birth
]
]
reset-ticks
end
to go
ask patches [
set num-live count neighbors with [
pcolor = yellow
]
death-check
]
tick
end
to cell-birth
set pcolor yellow
end
to cell-death
set pcolor black
end
to death-check
if num-live < 2 or num-live > 3 [
cell-death
]
if num-live = 3 [
cell-birth
]
end
Now if I put the ask patches inside the procedure, it comes out with a different output. Here is what I changed inside the code:
to go
ask patches [
set num-live count neighbors with [
pcolor = yellow
]
]
death-check
tick
end
to death-check
ask patches [
if num-live < 2 or num-live > 3 [
cell-death
]
if num-live = 3 [
cell-birth
]
]
end
What I was wondering was why did this small change make such a big difference for my program?
Solution 1:[1]
The difference here is that in one instance, you are calling ask twice, whereas in the other you are only calling ask once. Consider two examples, which I'm calling go-1-ask and go-2-ask:
go-1-ask:
to go-1-ask
ask patches [
set num-live count neighbors with [
pcolor = yellow
]
if num-live < 2 or num-live > 3 [
cell-death
]
if num-live = 3 [
cell-birth
]
]
tick
end
go-2-ask:
to go-2-ask
ask patches [
set num-live count neighbors with [
pcolor = yellow
]
]
ask patches [
if num-live < 2 or num-live > 3 [
cell-death
]
if num-live = 3 [
cell-birth
]
]
tick
end
All I've done relative to your code is nest the code from each different death-check examples within the go procedure.
In go-1-ask, you are asking all patches, in a random order, to set num-live then to evaluate it. So, patch 43 will run the code, then maybe patch 71, and so on. In contrast, in go-2-ask, you are first asking all patches to assess their num-live. Only then, once all patches have performed that first check, are you performing the death-check. In short- the order of operations is different between the two methods, which accounts for the difference in behaviour.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Luke C |
