![]() |
|||
Home Download Help Forum Resources Extensions FAQ NetLogo Publications Contact Us Donate Models: Library Community Modeling Commons Beginners Interactive NetLogo Dictionary (BIND) NetLogo Dictionary User Manuals: Web Printable Chinese Czech Farsi / Persian Japanese Spanish
![]() |
NetLogo Models Library: |
![]() |
If you download the NetLogo application, this model is included. You can also Try running it in NetLogo Web |
This model is from Chapter Five of the book "Introduction to Agent-Based Modeling: Modeling Natural, Social and Engineered Complex Systems with NetLogo", by Uri Wilensky & William Rand.
This model is in the IABM Textbook folder of the NetLogo Models Library. The model, as well as any updates to the model, can also be found on the textbook website: http://www.intro-to-abm.com/.
The code for this model differs from the code in the textbook. The code here is the most up to date version. The code for the agentset efficiency model in the book (pages 219-222) should be replaced with the code in this model.
This model addresses a couple of concerns that can arise when using agentsets, in particular when filtering them using the "with
" primitive. The first of these concerns has to do with efficiency: it is best to avoid building the same agentset multiple times. The second concern has to do with the timing of side effects: rebuilding an agentset using the same condition can lead to different results if the state of the agents has changed in the meanwhile.
SETUP creates a world where roughly half the patches are red and half the patches are green.
GO-1 sets the labels of red patches to a small random number (0-4) and the labels of green patches to a larger random number (5-9). It only sets the label of each patch if there are at least 5 patches of the other color.
GO-2 is a more efficient implementation of GO-1, as it only computes the red-patches
and green-patches
agentsets once instead of recomputing the green patches again for each red patch and recomputing the red patches again for each green patch.
GO-3 is written as if the intention is to swap green patches with red patches. However, because of a bug in the code, it first changes all the red patches to green patches, and then doesn't change any patches to red. GO-3 has unexpected behavior and is given here as an example of the potential pitfalls of changing agentsets on the fly.
GO-4 is similar to GO-3, but it computes the red-patches
and green-patches
agentsets before changing them, which avoids the problem we had with GO-3.
Press SETUP to create the world of red and green patches. Then press GO-1 to see what effect it has. Now try GO-2. Is the result any different from GO-1? You can try both of them a few times to see if you notice anything.
After that press GO-3 to see what effect it has on the colors of the patches. Is that what you expected? Do you have any idea what the cause of this behavior is? Now try GO-4. Does it give the expected result? Do you understand why?
While playing around with the model, you should have seen that GO-3 gives very different results than GO-4: instead of swapping red and green patches like we intended, it turns all the patches green! Why is that?
This has to do with the timing of "side effects". In GO-3, we start by using the following code to ask all the red patches to turn green if there are at least 5 green patches:
ask patches with [ pcolor = red ] [
if count patches with [ pcolor = green ] > 5 [
set pcolor green
]
]
Then, we ask all the green patches to turn red if there are at least 5 red patches:
ask patches with [ pcolor = green ] [
if count patches with [ pcolor = red ] > 5 [
set pcolor red
]
]
But since we previously turned all red patches green, "count patches with [ pcolor = red ]
" is now 0, so they all stay green!
What is it about GO-4 that prevents this from happening? It is the fact that we constructed our agentsets (red-patches
and green-patches
) before performing any changes on our patches. The red-patches
agentset is insulated from the side effects of turning red patches to green, so when we run this code:
ask green-patches [
if count red-patches > 5 [
set pcolor red
]
]
...the original set of green patches turn red as expected.
Try running GO-1 and GO-2 a number of times. Which is faster? Sometimes, it can be hard to tell when you're just playing with a model "by hand". This why we wrote a reporter procedure called test-1-2
. There is no button for this procedure on the interface, but you can invoke it from the command center. If you try it, you will see the patch labels flickering in the view for a while, and then, printed in the command center, the time it took to run GO-1 and GO-2 a hundred times each. Although GO-2 is somewhat faster than GO-1, the difference isn't extreme. Why is that?
As is common with a lot of NetLogo models, the bulk of the processing time is taken up by view updates: in this case, displaying all the patch labels on the screen. If you really want to measure the speed difference between two procedures, it is a good idea to turn off view updates by unchecking the corresponding checkbox in the NetLogo toolbar.
Try it. Which procedure is faster now?
Another thing that you can try is to increase the size of the world. Is the difference between GO-1 and GO-2 more noticeable when there are more patches?
We have used the reset-timer
and timer
primitives to write a test function that compares the running times of G0-1 and GO-2, but NetLogo also has a built in profiler
extension that serves the same purpose and gives you a lot more information about the performance of your model.
You can read the documentation for the profiler
extension online at http://ccl.northwestern.edu/netlogo/docs/profiler.html.
Try to replace the test-1-2
procedure with one that is adapted from the example in the documentation of the profiler
extension. Can you replicate the results that you had with test-1-2
?
This model is part of the textbook, “Introduction to Agent-Based Modeling: Modeling Natural, Social and Engineered Complex Systems with NetLogo.”
If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.
For the model itself:
Please cite the NetLogo software as:
Please cite the textbook as:
Copyright 2008 Uri Wilensky.
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.
(back to the NetLogo Models Library)