Courses‎ > ‎Introduction to Computer Science 1‎ > ‎Konstantinovich‎ > ‎ML1 9/2012‎ > ‎labs‎ > ‎

Lab 08 - Cars and Pedestrians + Solutions

posted Dec 5, 2012, 6:26 AM by Samuel Konstantinovich   [ updated Dec 21, 2012, 5:26 AM ]
You will want to use new concepts: (I would not give you these hints on a test)

Concept 1: 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 'with'

ask turtles-here with [ boolean ] [commands]

ask turtles-here with [color = yellow] [commands]

OPTIONAL: ask other turtles-here [set size 3] ;this is how you exclude yourself from the turtles-here command. the other command removes yourself from the agentset, and it is optional for now. 

Concept 2: Random % chance

To make a boolean true 37% of the time you would say:   random 100 < 37

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 3:
The N from the last concept could be a variable OR REPORTER FUNCTION like this:

if random 100 < REPORTER [commands]

Concept 4:  Variables (from before)
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

Now the lab:  (This is where a test question would start)
1. Start with your basic cars and people from the weekend.

to setup
  ca
  crt 5 
  [
    set shape "car" 
    set color red 
    set xcor (- abs random-xcor)
    set heading random 4 * 90
  ]
  crt 10
  [
    set shape "person"
    set color blue
    set xcor abs random-xcor
  ]
  ask turtles 
  [
    set ycor random-ycor
    set size 2
  ]
end

to go
  ask turtles with [shape = "person"] [peopleMotion]
  ask turtles with [shape = "car"] [carMotion]
  wait .06
end

to carMotion
  fd 1
end


to peopleMotion  
  fd .3
  rt random 10
  lt random 10
end


2. Now make the cars paint the gray roads like yesterday.
to carMotion
  set pcolor gray
  fd 1
end

3. Turtles should have a variable deathTimer.

turtles-own[deathTimer]


4. There should be global variables accidentDeath and naturalDeath. These will act as counters for the number of deaths that happen. We will use them in monitors.

globals [accidentDeaths naturalDeaths]


Additional Car Behavior:
5. When a car is on the same patch as a person (concept 1), it makes the person die right away. This will add 1 to the accidentDeath count. (concept 4) 

to carMotion
  set pcolor gray
  fd 1
  ;kill pedestrians
  ask turtles-here with [shape = "person"] 
  [  set accidentDeaths accidentDeaths + 1 
     die
  ] 
end


New Reporter function:

6. deathRate is a reporter function that reports 10 when there are more than 150 people, or it reports 6 otherwise. 
to-report deathRate
  ifelse count turtles with [shape = "person"] > 150
  [report 10]
  [report 6]
end


New Person Behavior:
7-DeathTimer logic:
 People should initialize their deathTimer to -1 when they are created.

Inside of the CRT 10 in your setup: set deathTimer -1
You don't have to do this in hatch because they copy the -1 from the parent

 People with a deathTimer that is > 0 reduce their timer by 1 and reduce their size by .1  every time they move. 
 People with a deathTimer that is = 0 should die. This increases naturalDeath by 1. 
 People have a 'deatRate' % chance (either 10 or 6) of starting to die. When they start to  die, they turn red, and set their deathTimer to 20.  (concept 3, use the deathrate function as your percent)


to peopleMotion
  ;death logic
  if deathTimer = 0 
  [ set naturalDeaths naturalDeaths + 1
    die
  ] 
  if deathTimer > 0 [set deathTimer deathTimer - 1 set size size - .1]
  if color = blue and random 100 < deathRate [set color red set deathTimer 20]

  fd .3
  rt random 10
  lt random 10 
end




8-Hatch Behavior:
 Blue people have an 8% chance of hatching a child. (dying people cannot hatch) (concept 2)

After your death logic above insert this: 

  ;birth logic
  if color = blue and random 100 < 8 [hatch 1 [rt 90]]
  

New Widgets:
9. Make a monitor for the number of naturalDeaths, and one for the number of accidentDeaths. Just put the name of the variable into the monitor, there is nothing fancy here.
10. Make a plot for the number of people. Count the number of people (not all turtles) on the plot. 
You need to use the following commands for a plot:
inside of setup after you clear-all use : reset-ticks
inside the go at the end use: tick


Final result:

globals [accidentDeaths naturalDeaths]
turtles-own[deathTimer]

to setup
  ca
  crt 5 
  [
    set shape "car" 
    set color red 
    set xcor (- abs random-xcor)
    set heading random 4 * 90
  ]
  crt 10
  [
    set shape "person"
    set color blue
    set xcor abs random-xcor
    set deathTimer -1
  ]
  ask turtles 
  [
    set ycor random-ycor
    set size 2
  ]
end

to go
  ask turtles with [shape = "person"] [peopleMotion]
  ask turtles with [shape = "car"] [carMotion]
  wait .06
end

to carMotion
  set pcolor gray
  fd 1
  ;kill pedestrians
  ask turtles-here with [shape = "person"] 
  [  set accidentDeaths accidentDeaths + 1 
     die
  ] 
end


to-report deathRate
  ifelse count turtles with [shape = "person"] > 150
  [report 10]
  [report 6]
end

to peopleMotion
  ;death logic
  if deathTimer = 0 
  [ set naturalDeaths naturalDeaths + 1
    die
  ] 
  if deathTimer > 0 [set deathTimer deathTimer - 1 set size size - .1]
  if color = blue and random 100 < deathRate [set color red set deathTimer 20]

  ;birth logic
  if color = blue and random 100 < 8 [hatch 1 [rt 90]]
  
  fd .3
  rt random 10
  lt random 10
end




Comments