Java StarLogo
2.0
english
`turtle`
turtles-own [age infecttime fertrate kids gender [male female] deathrate health [healthy latent sick recovered] lastx lasty lasth]

to go 
  infect 
  recover 
  check-health 
  setage age + time-step 
  if (health = sick) or (health = latent)
    [setinfecttime infecttime + 1]
  hatch-babies 
  die?
end

to infect
  rt random 60
  lt random 60
  fd 1
  if health = healthy    
    [if  (((health-of one-of-turtles-here) = sick) or    ;if turtle you meet is sick or latent  
       ((health-of one-of-turtles-here) = latent))   
      and ((random 100) < infect-rate)                   ;then with some chance
     [sethealth latent setinfecttime 0]]                 ;become latent and note time of infection
end
   
to recover
  if health = sick   
    [if (random (100 / time-step)) < recover-rate          ;recover with some chance
      [sethealth recovered]]
end

to check-health   
  if health = healthy [setc green]   
  if health = latent [ifelse infecttime > latent-time                         
                      [sethealth sick]                          
                      [setc yellow]]   
  if health = sick [setc red]   
  if health = recovered [setc blue]
end

to pop-dist                   ;draw the population distribution histogram  
  setlastx xcor setlasty ycor  setlasth heading    
  sety (-1 * screen-half-height)  
  ifelse age = 0       
    [setx (-1 * screen-half-width)]      
    [setx (-1 * screen-half-width) + ( age / 2)]  
  seth 0  
  repeat screen-height [if count-turtles-here > 1 [fd 1]]
end

to go-back       ;go back to where you were before drawing histogram  
  pu  
  setx lastx  
  sety lasty  
  seth lasth
end

;--------------------Population

to die?             ;deathrate based on age  
  if health = sick 
    [if (infecttime) > (sick-time + latent-time)  
      [setdead dead + 1 die]]  
  if age <= 20 [setdeathrate 0] 
  if age > 20 and age <= 50 [setdeathrate 10]  
  if age > 50 and age <= 60 [setdeathrate 20]  
  if age > 60 and age <= 70 [setdeathrate 40]  
  if age > 70 and age <= 80 [setdeathrate 80]  
  if age > 80 and age <= 90 [setdeathrate 120]  
  if age > 90 and age <= 100 [setdeathrate 240]  
  if age > 100 [setdead dead + 1 die]  
  if (random 1000 / time-step) < deathrate 
    [setdead dead + 1 die]
end

to hatch-babies          ;fertrate based on age and size of total population
  if age > 13 and age <= 19 [setfertrate 30]
  if age > 19 and age <= 24 [setfertrate 120]
  if age > 24 and age <= 29 [setfertrate 200]
  if age > 29 and age <= 34 [setfertrate 180]
  if age > 34 and age <= 39 [setfertrate 56]
  if age > 39 and age <= 44 [setfertrate 12]
  if age > 44 and age <= 49 [setfertrate 1]
  if age > 49 or age <= 13 [setfertrate 0]
  if pop > (1.1 * number) [setfertrate .8 * fertrate]
  if pop > (1.2 * number) [setfertrate .6 * fertrate] 
  if pop > (1.3 * number) [setfertrate .4 * fertrate] 
  if pop > (1.5 * number) [setfertrate .2 * fertrate] 
  if pop < (.9 * number) [setfertrate 1.2 * fertrate] 
  if pop < (.8 * number) [setfertrate 1.4 * fertrate]
  if pop < (.7 * number) [setfertrate 1.6 * fertrate] 
  if pop < (.5 * number) [setfertrate 1.8 * fertrate] 
  if (random (1000 / time-step)) < fertrate and gender = female and kids < 4    
    [setkids kids + 1    
     hatch [setage 0          
            setinfecttime 0          
            setfertrate 0          
            setkids 0          
            ifelse (random 2) = 0            
              [setgender male]            
              [setgender female]         
            setdeathrate 0          
            setxy random screen-width random screen-height          
            sethealth healthy    ;all babies healthy, regardless of parent health         
            ]]
end


`observer`
globals [dead time-step pop] 

to startup
 plotid 3
end

to setup
  plotid 3
  clearplot
  setplot-title ""
  ca
  setdead 0
  settime-step 0.1
  crt number
  ask-turtles [setinfecttime 0            
               setfertrate 0            
               setage (random 85)            
               ifelse (random 2) = 0               
                 [setgender male]               
                 [setgender female]            
               if (age > 15) and (random 3) = 0 and (gender = female)                 
                 [setkids (random 4)]           
               setdeathrate 0              
               setxy random screen-width random screen-height            
               ifelse who < infected                
                 [sethealth sick setinfecttime random (sick-time + latent-time)]             
                 [sethealth healthy]           
               check-health]
  setup-graph
end

to setup-graph
  pp1 ppreset setppc green     ;healthy
  pp2 ppreset setppc red       ;sick
  pp3 ppreset setppc blue      ;recovered
  pp4 ppreset setppc yellow    ;latent
  setplot-yrange 0 100 
  setplot-xrange 0 50
  setplot-title "Population vs. Time"
end

to graph
  pp1 ppd plot healthy-%
  pp2 ppd plot sick-%
  pp3 ppd plot recovered-%
  pp4 ppd plot latent-%
end

to start
  startgobutton
  startgraphbutton
end

to sick-%
  output ((count-turtles-with [health = sick]) * 100) / count-turtles
end

to recovered-%
  output ((count-turtles-with [health = recovered]) * 100) / count-turtles
end

to latent-%
  output ((count-turtles-with [health = latent]) * 100) / count-turtles
end

to healthy-%
  output ((count-turtles-with [health = healthy]) * 100) / count-turtles
end

to deaths
  output dead
end

to population
  setpop count-turtles
  output pop
end

to stopit
  stopgobutton
  stopgraphbutton
end


`information`
For use with the Adventures in Modeling book,  by Vanessa Colella, Eric Klopfer, and Mitchel Resnick published by Teachers College Press, 2001.  

For more information, see  
<http://education.mit.edu/starlogo/adventures>  

Challenge 8: Disease  
In this sample project, a disease afflicts the turtle population.  Though most turtles start out healthy, many eventually succumb to  the disease. When a turtle is infected, by sharing a patch with an  infected turtle, it becomes a latent carrier of the disease. Soon  it becomes sick and displays symptoms of the disease. Eventually,  it recovers or dies. Recovered turtles are no longer susceptible  to the disease.

Turtles contract the disease from other infected turtles with the  probability specified by the "infect-rate" slider and recover with  the probability specified by the "recover-rate" slider. Turtles remain latent for "latent-time" and sick for "sick-time".

You can specify the initial conditions of the simulation with the "number" and "infected" sliders.   

Press "stop-it" and use the "pop-dist" button to create a  distribution of turtles by age, with the youngest turtles on the left. The height of each line shows how many turtles fall into a particular age bracket. Open a few Turtle Monitors to see if all of the turtles are in the correct position. You can restart the simulation by pressing "start" or by first asking the turtles to "go-back" to their previous positions and then pressing "start".


`interface`
SLCanvas
top-left 23 266

SLSlider
top-left 48 158
width-height 85 25
name "slider4"
variable "infected"
min-value 0
max-value 300
current-value 62
slider-number 4
show-name? false

SLSlider
top-left 151 140
width-height 99 25
name "slider5"
variable "sick-time"
min-value 0
max-value 10
current-value 7
slider-number 5
show-name? false

SLSlider
top-left 114 140
width-height 99 25
name "slider2"
variable "latent-time"
min-value 0
max-value 10
current-value 3
slider-number 1
show-name? false

SLSlider
top-left 220 140
width-height 99 25
name "slider3"
variable "recover-rate"
min-value 0
max-value 100
current-value 5
slider-number 3
show-name? false

SLSlider
top-left 185 140
width-height 99 25
name "slider2"
variable "infect-rate"
min-value 0
max-value 100
current-value 80
slider-number 2
show-name? false

SLSlider
top-left 15 158
width-height 85 25
name "slider1"
variable "number"
min-value 0
max-value 500
current-value 500
slider-number 0
show-name? false

SLButton
turtle-or-observer? turtle
top-left 89 16
width-height 40 30
name "gobutton"
line-to-run "go"
forever? true
button-number 2
show-name? false
whichip 20

SLButton
turtle-or-observer? observer
top-left 89 71
width-height 55 30
name "graphbutton"
line-to-run "graph"
forever? true
button-number 3
show-name? false
whichip 21

SLButton
turtle-or-observer? observer
top-left 129 34
width-height 72 30
name "button8"
line-to-run "view-plot"
forever? false
button-number 9
show-name? false
whichip 22

SLMonitor
top-left 167 34
width-height 72 36
name "population"
list-to-run "population"
digits 0
delay 0.5
monitor-number 2
show-name? true
whichip 28
onewhichip 29

SLMonitor
top-left 209 35
width-height 73 36
name "deaths"
list-to-run "deaths"
digits 0
delay 0.5
monitor-number 4
show-name? true
whichip 30
onewhichip 31

SLMonitor
top-left 257 9
width-height 66 36
name "latent-%"
list-to-run "latent-%"
digits 1
delay 0.5
monitor-number 6
show-name? true
whichip 32
onewhichip 33

SLMonitor
top-left 257 92
width-height 66 36
name "sick-%"
list-to-run "sick-%"
digits 1
delay 0.5
monitor-number 3
show-name? true
whichip 34
onewhichip 35

SLMonitor
top-left 258 175
width-height 66 36
name "immune-%"
list-to-run "recovered-%"
digits 1
delay 0.5
monitor-number 7
show-name? false
whichip 36
onewhichip 37

SLButton
turtle-or-observer? turtle
top-left 305 42
width-height 68 31
name "button5"
line-to-run "pop-dist"
forever? false
button-number 6
show-name? false
whichip 23

SLButton
turtle-or-observer? turtle
top-left 305 136
width-height 58 31
name "button6"
line-to-run "go-back"
forever? false
button-number 7
show-name? false
whichip 24

SLLineWidget
top-left 340 60
width-height 116 68
id 3
order 3
type 1
title "Population vs. Time"
delay-string "1.0"
xlabel ""
ylabel ""
grid false
connected true
autoscaletrue
location 0 0
wizard-on false

SLButton
turtle-or-observer? observer
top-left 10 49
width-height 55 30
name "button4"
line-to-run "setup"
forever? false
button-number 5
show-name? false
whichip 27

SLButton
turtle-or-observer? observer
top-left 47 6
width-height 67 30
name "button3"
line-to-run "start"
forever? false
button-number 4
show-name? false
whichip 25

SLButton
turtle-or-observer? observer
top-left 47 82
width-height 67 30
name "button7"
line-to-run "stopit"
forever? false
button-number 8
show-name? false
whichip 26


`turtle shapes`
[encoded data, 5330 characters]
qqqqqqqaoR8DO5wb3dkOFQAAFQ7Z

`template`
null

`settings`
patch-size 8
num-shapes 256
screen-half-width 22
screen-half-height 22
interface-window-xcor 314
interface-window-ycor 83
interface-window-size 659 411
output-window-xcor 0
output-window-ycor 0
output-window-width 300
output-window-height 219
info-window-xcor 236
info-window-ycor 241
info-window-width 500
info-window-height 419
control-center-xcor 0
control-center-ycor 85
control-center-width 285
control-center-height 415
turtle-command-center-height 150
observer-command-center-height 150

`string table`
H4sIAAAAAAAAAGNgYGBiYGAQDsgvKM1JLMnMz1MoK9ZTCMnMTQUA/IQ98BsAAAAA
AAAb

`symbol table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`double table`
H4sIAAAAAAAAADWUO0sDQRSFt1CwEURERER8S/Cd1fh2Jol5bR7uuptCa03lj/Bn
GLW2srOyslaijZWdIAQEC0URRQvdvd9Mczjn3plzbnYylmUd6eShFS51WQ/XsboG
7+1wzasH+KslfY/gU1S3dXDe+F832j8QrEQbTvTKRVhO6OyO8Oqk1Ltkv07SF7yI
vkH/Jvp4LVz7urUuvMg5s/Ql4ZtgCgzIUYJv49uBr0fdI3eqFOKd7qFewD8PKvwy
8ArnzqNvwR3jxzw5+qtj4Rx7uo3zt/EfZb6AfFPwBc5dA9dBl7wJ40vuIj7Dsl81
I6xpBz0LevjM4NPbEK7JbZGvHz2Pvoqfz1yD7E81xH+Rejf7fHMfmjJ3p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