Courses/CS 461/Winter 2006/Bill Kunyiha/Homework4

From CSWiki

Jump to: navigation, search

For this homework, I modified the Virus model from. This model simulates the transmission and perpetuation of a virus in a human population. The model is initialized with 150 people, of which 10 are infected. People move randomly about the screen in one of three states: healthy but susceptible to infection (green), sick and infectious (red), and healthy and immune (gray). People may die of infection or old age. When the population dips below the environment's "carrying capacity" (700 ) healthy people may reproduce healthy and susceptible offspring. What I added to the model is the ability for people to get healed after taking drugs. This was not available in the original model. You are able to set the percentage of people who recover after taking prescription medicine and this has an advance infect on the whole population. At a 30 percentage cure rate the virus eventually is eliminated. This is healed mostly by infected people dying and other getting immunized against the virus.


Here the Code

turtles-own
 [ sick?        ;; if true, the turtle is infectious
   immune?      ;; if true, the turtle can't be infected
   sick-count   ;; how long the turtle has been infectious
   age ]        ;; how many weeks old the turtle is
globals
 [ weeks                ;; each tick is called a week
   %infected            ;; what % of the population is infectious
   %immune              ;; what % of the population is immune
   lifespan             ;; the average lifespan of a turtle
   average-offspring    ;; the average number of offspring a turtle could have
   carrying-capacity    ;; the number of turtles that can be on the screen at one time
   %healed              ;; What % of the population has been healed
   healed-count]        ;; This will help count the percentage of people healed
;; The setup is divided into three subroutines
to setup
 ca
 set weeks 0
 setup-constants
 setup-turtles
 update-plot
 update-global-variables
  set healed-count 0
end
;; We create a variable number of turtles of which 10 are infectious,
;; and distribute them randomly
to setup-turtles
 set-default-shape turtles "person"
 cct people
   [ setxy random-float screen-size-x
           random-float screen-size-y
     set age random lifespan
     set sick-count 0
     set immune? false
     set size 1.5  ;; easier to see
     ifelse (who < 10)
       [ get-sick ]
       [ get-healthy ] ]
end
to get-sick ;; turtle procedure
 set sick? true
 set immune? false
 set color red
end
to get-healthy ;; turtle procedure
 set sick? false
 set immune? false
 set sick-count 0
 set color green
end
to become-immune ;; turtle procedure
 set sick? false
 set sick-count 0
 set immune? true
 set color gray
end
to setup-constants
 set lifespan 100
 set carrying-capacity 750
 set average-offspring 4
end
to go
 set weeks (weeks + 1)
 
 get-older
 move
 infect
 healed
 recover
 reproduce
 update-global-variables
 update-plot
end
to healed
   set healed-count (healed-count + 1)
   if (healed-count) = 10 [set healed-count 0]
   ask turtles with [sick?]
   [ if (healed-count) < (1)  
       [ ifelse ((random-float 100) < chance-recover)        ;; either recover or die
            [ become-immune ]
            [ get-sick ] ] ]
 end
to update-global-variables
 if count turtles > 0
 [
   set %infected (count turtles with [sick?]) / (count turtles) * 100
   set %immune (count turtles with [immune?]) / (count turtles) * 100
  
 ]
end
;;Turtle counting variables are advanced.
to get-older
 ask turtles
   [ set age (age + 1)
     if sick?
       [ set sick-count (sick-count + 1) ]
     ;; Turtles die of old age once their age equals the
     ;; lifespan (set at 1500 in this model).
     if age > lifespan
       [ die ] ]
end
;;Turtles move about at random.
to move
 ask turtles
 [ rt random-float 100 - random-float 100
   fd 1 ]
end
;; If a turtle is sick, it infects other turtles on the same patch.
;; Immune turtles don't get sick.
to infect
 ask turtles with [sick?]
   [ ask other-turtles-here with [ not immune? ]
       [ if (random-float 100) < infectiousness
           [ get-sick ] ] ]
end
;; Once the turtle has been sick long enough, it
;; either recovers (and becomes immune) or it dies.
to recover
  ask turtles with [sick?]
    [ if (random sick-count) > (lifespan * (duration / 100))  ;; If the turtle has survived past the virus' duration, then
        [ ifelse ((random-float 100) < chance-recover)        ;; either recover or die
            [ become-immune ]
            [ die ] ] ]
end
;; If there are less turtles than the carrying-capacity
;;  then turtles can reproduce.
;; The probability of reproduction depends on average number
;;  of offspring per life.  In this model it is 4 per life (e.g.
;;  4 per 100 weeks.  The chance, therefore, for a turtle to
;;  reproduce at any given turn is 0.04 (if the population
;;  is below carrying-capacity).
to reproduce
 ask turtles with [not sick?]
   [ if (count turtles) < carrying-capacity
        and (random lifespan) < average-offspring
      [ hatch 1
          [ set age 1
            lt 45 fd 1
            get-healthy ] ] ]
end
to update-plot
 set-current-plot "Populations"
 set-current-plot-pen "sick"
 plot count turtles with [sick?]
 set-current-plot-pen "immune"
 plot count turtles with [immune?]
 set-current-plot-pen "healthy"
 plot count turtles with [not sick? and not immune?]
 set-current-plot-pen "total"
 plot count turtles
end
Personal tools