NetLogo Basics

171 views
Skip to first unread message

Alan Isaac

unread,
May 13, 2014, 10:14:25 AM5/13/14
to au-ec...@googlegroups.com
Use this thread to discuss and ask questions about the basic features of the NetLogo language.

Today, use this thread to discuss the Simple Computations exercise.

Alan Isaac

Kevin Carrig

unread,
May 13, 2014, 10:19:45 PM5/13/14
to au-ec...@googlegroups.com
Netlogo's Command Center shares some functionality with traditional interpretative languages, but also departs in its processes for variable assignment. For example, Python places an emphasis on proper indentation- for loops and other operations require the user to follow indentation guidelines in order for operations to be properly completed. However, white space is less of a concern for simple computations in Python. The operations below achieves the same result regardless of white space in Python:
  1.   print 2+2
  2.   print 2  +  2
  3.   print 2 +         2
  A similar implementation in Netlogo produces different results and demonstrates the importance of syntax in the Command Center. In the case above (addition of 2 and 2),      Netlogo requires users to equally space each integer and the operator in order to print the desired result. 

The distinction between "let" and "set" in the Command Line is also unique. The "let" command creates a new local variable with a value assignment. Instances in which this value must be changed in subsequent lines must use the "set command". This is illustrated in the implementation of the repeat command here: let x 0 repeat 50 [set x (x + 1)] show x . In this case, we use "let" as a temporary variable assignment of x as 0. We then specify the number of iterations to initiate and the function to iterate over the square brackets. Effectively, this command repeatedly sets the value of x as one integer larger than its input. The analogous Python implementation is provided below. In both forms, our expected result is 50
 
 #analogous to let x 0
  x=0 
# analogous to repeat 50
  for i in range(0,50,1): 
       #analogous to set x(x+1)
       x= i+1 
       #analogous to show x
       print x 




 
 

Alan Isaac

unread,
May 14, 2014, 10:16:01 PM5/14/14
to au-ec...@googlegroups.com
Hi Kevin,

Mutliple white space characters is the same as a single white space character in NetLogo, even more so than in Python (because an end-of-line is just white space for NetLogo). The big difference here is that operator characters can be used in names in NetLogo, so white space is required to use them as operators.

You understand the difference between let and set.  But I will just point out that in your Python  the x=0 plays a role more like globals [x].

Cheers,
Alan Isaac

Kevin Carrig

unread,
May 14, 2014, 10:36:29 PM5/14/14
to au-ec...@googlegroups.com

Thank you for the feedback- it seems as if every language has its peculiarities. I was alluding primarily to the operation 2+2 (no spacing between integers and operators). While "print 2+2" returns 4 in Python, the same expression in NetLogo returns "illegal number format". This makes much more sense now given that operator characters can be used in names in NetLogo.

Kevin  

Adin Dobkin

unread,
May 15, 2014, 1:10:28 PM5/15/14
to au-ec...@googlegroups.com
I too am familiar with Python and a couple other programming languages.  NetLogo seems to depart from them (as is to be expected).

My question revolves around the use of order in the code section.  The tutorial seemed to mention this in the very briefest sense, but is there any specific order that the code must go in?  I couldn't seem to find a specific case when building the turtle model, but it would seem as though it must be relevant at some point...

Alan Isaac

unread,
May 15, 2014, 3:36:37 PM5/15/14
to au-ec...@googlegroups.com
Code Tab

The Code tab is implicitly divided into two sections: the declarations section and the procedures sections.  Declarations (such as globals or turtles-own) come first, then all your procedure definitions.

I hope this answers Adin's question about "order".  If not, please ask again.

Alan Isaac


Kevin Carrig

unread,
May 15, 2014, 11:58:38 PM5/15/14
to au-ec...@googlegroups.com
Talking to Patches

If you do the random color experiment a second time, will the outcome be identical? Why or why not? How could you use the random-seed command to ensure that you get the same outcome each time?

The random seed command is useful in simulations as a pseudorandom process.In the simulation space, this is useful to reproduce the same sequence of "randomness" for a set series of iterations. By producing identical sequences of random numbers, we can ensure that our results are reproducible and generalizable to the general population. The random seed function deterministically produces a sequence that exhibits statistical randomness but is not truly random. By using this functionality, we can ensure we get the same outcome each time because we know that the same seed will always generate the same sequence of random numbers from then on.    





Alan Isaac

unread,
May 16, 2014, 8:53:01 AM5/16/14
to au-ec...@googlegroups.com
random-seed

Yes, that is the role of random-seed.  On a computer, our "random" numbers are usually generated by a deterministic algorithm.  For this reason, these numbers are more accurately called "pseudo random".  Not so long ago, tables of "random" numbers were published as books.  To ensure repeatability of an experiment, you would record a page number, column number, and row number, and then start reading the "random" numbers sequentially from that point.  Setting the seed in your program is like picking the page, column, and row in the old approach.

Natalie Chambers

unread,
May 16, 2014, 1:29:06 PM5/16/14
to au-ec...@googlegroups.com
I have a questions regarding the Netlogo language. In the Traffic Model we were instructed and shown how to adjust the colors of the turtles and patches but it was then noted and realized that if you press set up it would adjust the model back to the original setup. If we wanted to permanently adjust the model  (if it was our own lets say) would we need to go to the third panel (code) and alter the code? 

Alan Isaac

unread,
May 16, 2014, 2:47:43 PM5/16/14
to au-ec...@googlegroups.com
Traffic Model

Yes, the right way to make permanent changes to a model is usually in the Code tab.

That statement is complicated a bit by the fact that you can make some permanent changes in the Interface tab.  E.g., you can change the world size, add global variables via sliders, and so forth.

The required readings will include some best practices for setting up your models.

Matthew Reardon

unread,
May 17, 2014, 7:59:13 PM5/17/14
to au-ec...@googlegroups.com

During the talk-to examples, I found myself asking what the difference is between using the ask command with the observer and changing the command center to the appropriate agent.  For example, observer -> ask turtles [ set color red]   vs  turtles -> set color red.

Using the word ask as part of a command gives the impression of a request instead of an order.  Are there commands that cannot be executed from the observer? (Could there be a "no" response when the observer "asks" something?)  

I assume there has to be, otherwise Netlogo would just utilize the "ask" command from the observer instead of providing the option to tab between observer, turtles, patches and links.

Alan Isaac

unread,
May 17, 2014, 11:32:31 PM5/17/14
to au-ec...@googlegroups.com
Context

In response to Matthew's query, there are different ways of estabslishing a turtle "context" (as NetLogo puts it).  One way is to choose the turtles context at the Command Center.  This is just a convenience feature for when one is working in the Command Center.  Another way to to ask turtles.  This is a standard way to establish a turtle context when working in the Code tab.

Despite the apparent politeness of ask, you are really telling the observer to pass a command to each turtle.

You can find a good discussion here:
http://ccl.northwestern.edu/netlogo/5.0/docs/programming.html#ask

Alan Isaac

Adaora Isichei

unread,
May 29, 2014, 2:23:18 PM5/29/14
to au-ec...@googlegroups.com
I am not a 100percent sure what the random-float command means. Does it mean generate a random outcome with a specified probability?

On Tuesday, May 13, 2014 10:14:25 AM UTC-4, Alan Isaac wrote:

Alan Isaac

unread,
May 29, 2014, 7:18:47 PM5/29/14
to au-ec...@googlegroups.com
random-float

Whenver you have doubts about the meaning of a command, turn to the NetLogo Dictionary.  Does the Dictionary documentation answer your question?

As Railsback and Grimm explain, whenever your are programming, you should have the NetLogo Dictionary open and at hand.  If you read the documentation and still do not understand, try a few examples, and then tell us what seems inadequate about the documentation.

In this case, the contrast is between random, which always returns an integer, and random-float, which can also return numbers in between the integers (i.e., floating point numbers).  So, for example, random-float 1 reports a number between 0 and 1 (i.e., in the [0,1) interval).

Keep asking questions!

Adaora Isichei

unread,
May 30, 2014, 11:21:24 AM5/30/14
to au-ec...@googlegroups.com
Thanks Professor.

I have another question in regards to the empty list command. I know how to run the command and I did look at the netlogo dictionary ( which explained this command as "Reports true if the given list or string is empty, false otherwise") I am still a like confused. Do you mind providing an example? We used this in the coin-flip exercise.  

Kevin Carrig

unread,
May 30, 2014, 4:36:58 PM5/30/14
to au-ec...@googlegroups.com
What programming practices do R&G recommend in chapter 3? What justifications do they provide for these recommendations?

Railsback and Grimm highlight some programming practices in Chapter 3 that are particularly useful in ABM. They discuss a simple implementation of the Butterfly model to advocate starting with a simple model by ignoring most components and processes that are expected to be incorporated later. This practice is most akin to the KISS method highlighted earlier in the course. In the Butterfly model, three components were intentionally absent of the initial program to provide a simple base for analysis: 
  1. Observations and additional output that quantify the width of the corridor used by a large number of butterflies, 
  2. A more sophisticated model landscape to not restrict our analysis to an artificial set of patches, 
  3. Additional analysis of the model (i.e. to see how the parameter q affects butterfly movement and the appearance of virtual corridors.
A second suggestion offered in the chapter is to develop a program in a hierarchical way- starting your initial model with a skeleton of structures, testing each iteration for syntax and composition errors, and then adding new methods to the general framework. I found this suggestion to have some weight in the discussion of the Butterfly model in Chapter 3. The initial ODD of the model outlined in the information tab was a surprisingly helpful reference tool when working through the program. 

Railsback and Grimm also discuss the importance of starting with a simplified artificial environment and progressively moving to the realistic model outlined in your initial ODD. The simplification, coupled with well-formatted and commented code, is suggested to better identify error in both programming and logic. 


Alan Isaac

unread,
May 30, 2014, 7:06:03 PM5/30/14
to au-ec...@googlegroups.com
Yes, this is absolutely critical!
Alan Isaac

Alan Isaac

unread,
Jun 5, 2014, 3:39:45 PM6/5/14
to au-ec...@googlegroups.com
empty?

I seem to have overlooked this query.  An empty list is []; it contains no items.  An empty string is ""; it contains no characters.  So if you

print empty? []
print empty? ""
print empty? [0 1]
print empty? "01"


NetLogo will print true twice and then false twice.

Students used to other languages may be surprised by one thing here: the question mark in empty? is just part of the command name, like any other character.  In fact, it is a convention to make the last character of a reporter the question mark if the value returned is Boolean (i.e., always true or false).

Reply all
Reply to author
Forward
0 new messages