SLuke
SLuke

Reputation: 115

Order of processing in netlogo

I am not sure how netlogo processes commands. Consider the following scenario. There are 100 turtles. The "go" procedure calls the following other procedures: A, B, and C. Procedure A tells turtles to do some things, Procedure B tells turtles to do some things, and Procedure C tells turtles to do some things.

Understanding X: It is my understanding that all turtles would complete the commands in A, then all turtles would complete the commands in B, then all turtles would complete the commands in C.

Now, inside procedure A there are commands A1, A2, and A3.

Understanding Y: It was my understanding that, inside procedure A, one turtle would do command A1, then A2, then A3, then a second turtle would do command A1, A2, and A3, and so on.

Is my understanding Y correct? Thanks for any insights you can provide.

Upvotes: 2

Views: 213

Answers (1)

JenB
JenB

Reputation: 17678

It depends. The basic rules is that the user presses the go button (or types 'go' in the command center) and that tells NetLogo to run the go procedure. NetLogo runs that procedure from top to bottom. But how you include the called procedures changes the order.

Consider version 1. The first line ask turtles says to choose a random turtle, make it do everything in the code block (delimited by the [ ] symbols) and then choose the next random turtle, make it do everything and then the next turtle until all the turtles are done. In this case, both proc-A and proc-B are within the same code block, so the turtle would do both before NetLogo switches to the next turtle.

to go
  ask turtles
  [ proc-A
    proc-B
  ]
end

to proc-A
  forward 1
  set heading heading + random 10
end

to proc-B
  forward 3
  set heading heading - random 20
end

How about version 2? The first line ask turtles [proc-A] says to choose a random turtle, make it do everything in the code block (delimited by the [ ] symbols) which in this case is only proc-A. And then choose the next random turtle, make it do everything and then the next turtle until all the turtles are done. Only after ALL the turtles are done with the ask turtles will the code move on to the next line. In this case therefore, all turtles do proc-A and then all turtles do proc-B.

to go
  ask turtles [ proc-A ]
  ask turtles [ proc-B ]
end

to proc-A
  forward 1
  set heading heading + random 10
end

to proc-B
  forward 3
  set heading heading - random 20
end

You could also do something like version 3. The first line says to run through proc-A, which gets every turtle to do something. Then, at the end of proc-A, control returns to the go procedure and moves to the next line, which is to run the procedure proc-B. This would achieve the same outcome as version 2.

to go
  proc-A
  proc-B
end

to proc-A
  ask turtles
  [ forward 1
    set heading heading + random 10
  ]
end

to proc-B
  ask turtles
  [ forward 3
    set heading heading - random 20
  ]
end

When I am teaching NetLogo, I encourage my students to construct the code along the lines of version 3. This is because the ask is within the same procedure as the actions they are being asked to do. This has several advantages. (1) Much easier to read because you don't have to try and read across procedures that could be a long way apart. (2) Avoids one of the most common beginner issues of nested ask - with an ask turtles in the go procedure and then ask turtles again as the first line of the called procedure. This is actually such a common bug that the NetLogo developers have made it impossible to have nested asks.

However, if you truly want a turtle to do more than one thing before the next turtle starts, then you have two options. You either code as in version 1. Or you put both things that you want the turtle to do in the same procedure.

Upvotes: 3

Related Questions