2017-10-17 HW

posted Oct 17, 2017, 6:20 AM by Konstantinovich Samuel   [ updated Oct 17, 2017, 7:38 AM ]
Homework:
Watch the netlogo Tutorial Videos 5,6,7,9.
Note: Think about the usefulness of random vs random-xcor

Procedures - context is important.
Do Now:
Determine the context of each procedure, or write invalid if there is a problem.
to A[ n ]
  crt n
  fd 3
end

to B [ n ]
   fd n
   rt 2 * n
   bk n
end

to C [ n ]
   ca
   crt n
end


Goal: Ask and you shall receive.


Netlogo has 2 primary procedures: setup and go

setup - configures the world so that the model can be run. 
go - causes the model to advance by one unit of time, this is usually run repeatedly to create the illusion of motion.

We almost always want observer context setup and go procedures.

Since setup configures turtles, we need a way to run turtle commands from an observer context. Lucky for us, there are several ways to do this.

We can easily switch context when we use commands that create turtles:

crt 5 [ TURTLE_COMMANDS_HERE ]

cro 10 [ TURTLE_COMMANDS_HERE ]

The newly created turtles will run those turtle commands.

When this code is run, you get 5 red turtles and 10 blue ones!

to setup
 ca
 crt 5 [ 
   ;only these 5 turtles are affected
   set size 3 
   set color red]
 crt 10 [ 
   ;only these 10 turtles are affected
   set size 2
   set color blue]
end

This is how our setup procedures will look most times, a clear all, followed by creating and configuring a bunch of turtles and patches. 

The go command is also best left as an observer procedure. If that is a case, we need a way for go to talk to the turtles too!

We can switch context with the ask command. This requires us to be more careful!
to go
  ask turtles[  
    fd 1
  ]
end

Observers can ask anyone to do things!

Agents (turtles and patches) can also ask anyone to do things but we must be careful.

Assume you created 100 turtles.
What would happen when you type go in the observer command center?
What would happens when you type go in the turtles command center?

syntax error - you didn't write the commands properly e.g. misspelled, or wrong arguments. This produces an error when you check the code. 
e.g. 
abs "fish"  ;doesn't work on string
1+1 ;no spaces
togo[n]  ;togo isn't a command

logical errors - do not crash, they run but produce unexpected results. 

This is a RUNTIME error: (it is valid syntax but some value was not allowed when it runs the code)

to setup
  ca
  crt 10
  ask turtles[
    foo 
    ;each turtle runs foo
  ]
end

to foo
  ;turtles cannot "ask turtles" for a good reason!
  ask turtles[ 
     ;do stuff
  ]
end

Classwork: 

Make setup and go procedures. Test them with the command center. 
Setup.
-Leave the NetLogo world as default. (doesn't require you to do anything, it just means you shouldn't change it)
-Create 100 red turtles
-Set their positions randomly around the TOP HALF of the world.
-Create 100 blue turtles
-Set their position randomly around the BOTTOM HALF of the world. 
-All turtles have their pens down.

Go.
-Each turtle moves forward 0.1


Make two buttons: setup and go.
Make go an observer forever button.
Make go an observer button.
ONLY call your setup and go functions inside the setup/go buttons, do not copy/write code here.

Now Test your setup/go buttons. 




Experiment with the following commands:
stamp
max-pxcor
min-pxcor
Comments