2014-12-03 Notes+Lab

posted Dec 2, 2014, 6:37 PM by Samuel Konstantinovich   [ updated Dec 2, 2014, 6:43 PM ]
Complete this lab at home if you are not done in class.

DISCLAIMER: Notes in this lab are critical. I am giving a comprehensive set of text that MUST BE READ CAREFULLY. You are responsible for all new commands on this page. You are responsible to test out all the things that are discussed. If you don't understand ANY component of this page, discuss on piazza right away (in class too!). This will allow both classes to collaborate on the topics. If there are no questions on piazza that means you are 100% confident with ALL of the material on this page. 

You watched a video about Agentsets.  An agentset is a group of agents (turtles, patches, or links).
Strictly speaking an agentset is a subset of one type of agent (it can be empty, or have all of the agents.)

You have already seen some agent sets:
 turtles is the agent set of all turtles
 patches is the agent set of all patches.

Agent Set Commands:

Basic Commands:
ask AGENTSET [ commandBlock ]
e.g.
ask turtles [ wiggle ]
ask turtles [ stamp die ]

count AGENTSET
e.g.
display count turtles
ask turtles [ set size count turtles ]


These are commands that give you an agent set of turtles, they are used with other commands like ask or count:
turtles  is the agent set of all turtles. 
turtles-here   is the agent set of all turtles on the same patch as the agent using the command (including itself)

Filtering an agent set:  These commands specify which agents in an agent set you are talking about:

one-of AGENTSET      ;  gives you a randomly chosen agent from the agentset 
e.g.
ask one-of turtles [die]   ; one-of turtles   is an agentset with exactly one turtle

AGENTSET with [ boolean reporter ]  ; gets the agents from the agentset, that match the boolean.
e.g.
ask turtles with [color = red] [die]   ; turtles with [color = red]    is an agentset with some turtles... but it could be empty!
ask turtles with [ xcor > 0] [set color red]

Test if there is at least one agent:
any?  AGENTSET     ;this reports true when there is at least 1 agent in the set.
e.g.
if any? turtles with [color = red] 
[ ask turtles with [color = red]  [die]

Advanced filters (we will discuss later dont worry for now)
max-one-of 
min-one-of 
in-cone 
in-radius


LAB! Create:
SECTION I (Most of you should be able to do this part)
1. An observer context function setup that:
1a. clears the world
1b. makes 5 turtles  that are red and "car" shaped 
1c. each cars should face any of these four directions randomly: 0 90 180 or 270; 
1d. make 10 blue "person" shaped turtles. 
1e. place the cars on the left half of the screen at random, and the people on the right side of the screen at random. 

2. A turtle context functions carMotion : a turtle that runs this command moves forward 0.1 

3. A turtle context function peopleMotion : a turtle that runs this command wiggles randomly going forward .01 and then turning at most 10 degrees in either direction.

4. An observer context function go.
4a. Make each turtle run either carMotion or peopleMotion depending on their shape. Use the with command, you don't need to use if/else here. (Ask turtles that match your criteria to perform the action)
4b. There should be a brief wait at the end of the go to make the program run smoothly at a reasonable speed. 

5. Buttons for setup/go. Make go forever. 

SECTION II(Use the notes below, this is new stuff!)
Now improve the model:  
1. Make cars that touch pedestrians kill the pedestrians.
2. Make the cars have a 2% chance of reversing their direction 180 degrees. 
3. Try to give each car a different speed using a randomly set turtle variable speed, rather than a slider.

Concept 1: What can you do when a turtle can detect other turtles on the same patch?!?  You can make cars hit pedestrians... you can make doctors heal injured pedestrians and many other things!!!

Talking to the turtles that are on the same patch as you. 

turtles-here is the agent set of all turtles located on the same patch as the turtle that uses it. This can be used in turtle context, and it DOES INCLUDE YOURSELF unless you specify otherwise:     
ask turtles-here [ commands ]  
ask turtles-here [set size 3]      ;everyone including myself 

OR you can reduce the agentset more using the with command

ask turtles-here with [ boolean ] [commands]
ask turtles-here with [color = yellow] [set size 3]


Also the other command removes yourself from the agentset:
ask other turtles-here [set size 3]     ;this excludes the turtle performing the ask from the turtles-here command. 


Concept 2:  Not as obvious as I had originally expected, you can make something happen randomly, by giving it a Random % chance to do something. 

To make a boolean true 57% of the time you would say:   random 100 < 57
To make a boolean true 33% of the time you would say:   random 100 < 33       OR:    random 3 = 0      (if you wanted 33.3333%)

You can use this in an if statement like this:
if random 100 < 50 
[fd 2]                            ;50% of the time move forward 2

or like this:
if random 100 < N 
[commands]
;N % of the time, do the commands

Concept 2.5:  
The N from the last concept could be a variable OR REPORTER FUNCTION like this:

if random 100 < sqrt my-accuracy        ;sqrt reports the value of the square root of the my-accuracy variable.
[ do stuff in here ]   

Concept 3:  You can give turtles more properties, like an individual speed, or individual health, or individual scores. 

Variables (from the video)
Turtle variables are local to the turtle. Look up the turtles-own command.
Global variables are shared by everyone. Look up the globals command.

You can give a variable a specific value with set like:
set size 6
set color red

OR increase/decrease like this:
set heading heading + 1
set size size - .5

Comments