Tutorial #3: Procedures walk-thru

In this post I describe following the Tutorial 3 form the NetLogo User Manual.

it all starts here !

Having followed the previous 2 tutorials, which walk thru existing code to gain familiarity with the NetLogo User Interface (UI) this tutorial starts with a completely blank interface. The tutorial takes us thru adding various buttons, sliders and graphs with the accompanying code, as well as the turtle behaviour.

start here.. setup

Adding elements to the UI is really simple – the ‘Add’ menu option has various components that can be added tothe UI, in this case the ‘setup’ button. The UI helpfully tells us that this button wont work/do anything by being in red, this is due to the absence of code to support the action.

By selecting code, I can easily assign actions to a button with the ‘to … end’ method used within Netlogo

to setup
  clear-all
  create-turtles 100 [ setxy random-xcor random-ycor ]
  reset-ticks
end


This iterates for all the controls that will be added.

The iteratve loop for the sheep program is the ‘go’ button code.

to go
  if ticks >= 500 [ stop ]
  move-turtles
  eat-grass
  reproduce
  check-death
  regrow-grass
  tick
end

In this way our world takes shape with the sheep expending energy to move and reproduce, and eventually dieing when energy is expended. The sheep interact with the ‘world’ by the way of the netlogo ’tiles’ when they random move and encounter a green tile, energy increases. Grass only regrows after a number of turns, so the balance between sheep, movement, birth, deaths and grass growing is exhibited.

to eat-grass
  ask turtles [
    if pcolor = green [
      set pcolor black
      set energy (energy + energy-from-grass)
    ]
    ifelse show-energy?
    [ set label energy ]
    [ set label "" ]
  ]
end

The tutorial takes you from blank canvas to sheep environment quickly

Conclusion

I found the three tutorials all very well written and easy to follow, I was quickly able to grasp how to add buttons, counters, toggles and sliders, and the association between these and the code.

It is still early in the development practices, but I think a section on comments and why ‘globals’ are declared at the start of the program would of been useful. I think too many devleopers/programming overlook the importance of commenting code.

Leave a Reply

Your email address will not be published. Required fields are marked *