NetLogo Tutorial Command Center
From Backspaces Wiki
| NetLogo Tutorial |
In this section, we're going to explore NetLogo from the bottom up .. by using its Command Center. The Command Center is useful for building very simple models step by step, and for debugging larger models. Try the Command Center section of Tutorial 2 as an example of tinkering with an existing model.
I've already used this without mentioning it: the boids in the previous flocking model were a bit small, so I made them bigger by clicking on setup, which initialized the model, then I used this to set the boid size to be 3 patches big.
Contents |
Command Center Basics
Below is a default File > New NetLogo model after four commands have been executed from the Command Center. It has annotations for various actions we will need to perform:
- Making Selections: All the UI components, and in this case the View, can be selected by clicking on the white background, then dragging into the UI component. It can then be moved via dragging, and resized by using the black control points. Deselect by clicking in the white background. (Note: you can also select by right-clicking in the View to pop-up a menu with a select item.)
- Location: The Command Center defaults to being at the bottom of the NetLogo window. Change the size by clicking on the small Resize dimple. You can flip the Command Center to be at the right side of the NetLogo window by clicking on the diagonal double arrow icon on the top of the Command Center, next to the Clear button..
- Commands: Individual commands are typed into the one-line typing area at the bottom of the NetLogo window. These commands have a context as shown by the menu to the left of the typing area.
- Observer: The commands are executed in the global context.
- Turtles: These commands are executed for each turtle.
- Patches: These commands are executed for each patch.
- Links: These commands are executed for each link.
Tabbing will cycle through these modes without requiring the menu. We'll use each of these contexts in the following set of example commands.
- History: You can view earlier commands by either the History menu at the down arrow at the right edge of the typing area, or by using the up/down keys on your keyboard. Use the Return key to re-execute the command.
- The Clear button will clear the command history, leaving you with a blank command history area.
Getting Started
Restart NetLogo. You will have a default initial window. We're going to change this to make it easier to use with the Command Center:
- Select the View by click & drag. Then move the View to the far left of the NetLogo window. Deselect by clicking outside the view.
- Click the diagonal double arrow icon on the right of the Command Center's heading. This will re-layout the window to have the Command Center on the right edge of the window, and the same height as the window.
- Finally, resize the window and the Command Center to look like this:
First Four Commands
We'll next repeat the four commands used to create the view in the Command Center Basics section above. Note that in the commands below, the text ending with "> " is not part of the command .. it only indicates the command mode (context) being used.
- observer> create-turtles 100
- Creates 100 "turtles" .. the mobile NetLogo agents.
- Make sure you are in observer mode. You'll generally get a polite error if NetLogo can figure our you are in the wrong mode.
- Note that the new turtles are all on top of each other in a bunch in the center! The default is to place new turtles at patch 0,0.
- There is a short form for "create-turtles": crt. Thus the above could be replaced with crt 100. Several common NetLogo commands have acronyms.
- patches> set pcolor black + random 3
- Set the patch colors to be a random dark gray color. Note that this executes for each patch, thus all are colored.
- Make sure you change mode to patches, either by using the Tab key or the menu.
- Note the text colors NetLogo uses as you type. These are hints that you are using valid NetLogo syntax.
- This subtle shading of the patches is useful for knowing where you are in the patch space.
- Colors in NetLogo are numbers. Black = 0. So black + random 3 is one of 0, 1, 2 .. black or dark grays.
- turtles> forward 12
- Again, change mode. Forward has an acronym: fd .. so you could use fd 12.
- Now the turtles untangle themselves into a circle of radius 12.
- Note the default shape is boid-like so that you can tell the heading.
- The default heading is randomly chosen. Ditto for the colors.
- turtles> set shape "circle"
- We're changing the turtle shape to be circles so they look good in a network.
You should now have something like this. If not, you can re-initialize by entering the clear-all (acronym: ca) command (observer mode), and re-execute the four commands.
Making a Network
Starting from the end of the last section, we'll use 5 more commands to create a network.
- turtles> create-link-with one-of other turtles
- This creates links amongst the turtles.
- Note weird links going off the edge of the "world"
- Fix this by clicking on the Settings... button, uncheck both World wraps Horizongally/Vertically, click OK. See Views Document
- links> set thickness .2
- Note the .2 means .2 of the patch size. .. and again, make sure you're in links mode.
- turtles> forward 100
- The turtles now are all crowded on the edge of the View due to our change in the wrapping Setting above.
- observer> layout-circle turtles max-pxcor - 2
- Use layout to return to a circular network.
- This circle is more uniform than earlier due to the layout algorithm.
- observer> repeat 100 [layout-spring turtles links 1 1 1]
- The layout was quite "stiff" and rather fast. So we'll revert to the circle than change the spring constant to .1 (1/10th its initial value).
- (But first, we'll need to revert to the earlier circular layout, see next command.)
- We'll also us the speed slider on the top of the View to be slower:
- observer> layout-circle turtles max-pxcor - 2 (use up/down keys or History menu to repeat circular layout)
- observer> repeat 100 [layout-spring turtles links .1 .1 .1]
- Repeat several times (up arrow key) to see how the animation proceeds
Adding UI Components
We'll add a few simple UI components: A slider for the spring layout parameters, and a histogram of the number of links the turtles have. See the Working with Interface Elements section of the NetLogo documentation.
First, lets make some space for the UI components:
- Make the NetLogo window wider by grabbing the lower right resize corner and dragging to the right.
- Shrink the View by selecting it and using the lower right black selection handle to change the View size.
First we'll add a slider. Click on the drop-down menu just above the View. It lets you select UI items. Select the Slider.
This will put you into a "mode" where a slider will be created where you click next in the white background area. Note the cursor changes to a plus shape. Click just to the right of the View .. don't worry about getting it wrong, you can select, move, resize the UI item just like you did with the View.
Fill in the 5 items circled in red above:
- Global Variable: s
- Minimum: 0
- Increment: .01
- Maximum: 1
- Value: 0.1
This results in a global variable we'll use for the spring layout. It's name is s (spring), it has a range from 0 to 1, incrementing by .01. And its initial value is 0.1.
Lets try it:
- observer> repeat 100 [layout-spring turtles links s s s]
- Repeat it a few times.
- Use the History (up arrow key or menu) to go back to the circular layout and run the repeat command, with different values of of the slider.
Next, we'll add a small plot. Go to the UI component menu just as you did with the slider, but this time choose a Plot. Place it below the slider. Make two changes:
- Name: Histogram
- Mode: Change to Bar via drop-down menu.
Now lets use the plot for creating a histogram of links:
- observer> histogram [count link-neighbors] of turtles
- Note there is no bar at "0". Why?
- Can you spot the "1"s and "2"s? How would you describe them?
- Ditto for the "3"s.
Procedures
Now that we've experimented with individual commands, we'll place them into procedures, which can also be called from the Command Center.
We'll add two procedures that you'll find in most models:
- setup: clears old state and initializes a new run of the model.
- go: evolves the model by repeatedly running a set of commands.
The procedures will look like this:
to setup ca crt 100 [set shape "circle" fd 12] ask patches [set pcolor black + random 3] ask turtles [create-link-with one-of other turtles] ask links [set thickness .2] histogram [count link-neighbors] of turtles end
to go layout-spring turtles links s s s end
Notice they are the same commands as we ran in the Command Center with one difference. Rather than switching modes for the commands, we use the "ask" command, which passes a set of commands to patches, turtles and links above.
Enter the two procedures in the Procedures pane:
- Click on the Procedures tab at the top of the NetLogo window.
- Enter the above -- you can just cut and paste from the wiki if you'd like to save time.
- Save the file (File>Save, or Cmd/Ctl S)
Now return to the Interface pane (clicking on the Interface tab at the top of the NetLogo window. Enter these (much simpler!) commands into the typing area of the Command Center:
- observer> setup
- This calls the setup procedure you created in the Procedures pane. Repeat it a few times & note the different histograms you get.
- observer> go
- This similarly calls the go command once. Not much happens so try:
- observer> repeat 100 [go]
Completing the Model
Lets finally add two buttons for these procedures. Just as you did for the Slider and Plot, go to the UI menu and select Button. Place it just below the Histogram, and enter "setup" in the Commands text area. Click OK, then make a second button adjacent to the setup button. Enter "go" for the Commands, and in addition, click the "forever" checkbox.
Click setup, then go. Note the go button stays black, showing it is active. This is because of the forever checkbox: it means to run the button's commands until the button is clicked again to stop. Experiment with the setup, go, and s slider. You can move the slider while the model is running to see the impact of the layout parameters.
Save your model. It now should look something like this:
So you now have a your first model. Because the contents of the Command Center are not saved, we'll keep the individual commands we used for history's sake, and to show you the last NetLogo pane: Information.
- Select all the text in the Command Center via Edit>Select All (or Cmd/Ctl A, or just click and drag).
- Use Edit>Copy (or Cmd/Ctl C) for later Pasting.
- Click on the Information Tab.
- Click on the Edit icon on the top left. This will turn the formated text into editable plane text.
- Paste the commands you Copied into the How It Works section.
- Delete from there to the end of the text.
- Put something sensible into the What Is It? section.
- Click Edit again to stop editing. It will look like the image below, left:
We'll clean up by putting the Command Center back on the bottom of the NetLogo window (use the double diagonal arrow icon) and modify the layout. Our completed model is above, right.
Notes
- Even though I used some html links to the ccl.northwestern.edu site above, you also have your own set of documentation downloaded with your version of NetLogo. Just go to the Help menu.
- The model is available as CmdCenter.nlogo in the NetLogoTut download folder.



