⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 epiet.nlogo.txt

📁 netlogo模拟疾病传播,设定相应参数
💻 TXT
📖 第 1 页 / 共 3 页
字号:
breed [ bus ]
breed [ persons ]


Globals [
  time
  minutes      ;; minutes is the models smallest steps. They represent 10 minutes each
  hours
  days 
  weekend
  home-patches  ;; agentset with residence-patches
  work-patches  ;; agentset with work place patches
  home-busstop 
  work-busstop
  sum-1/n
  busdistance  ;;the distance from turtle's home to the busstop + from the far busstop to the workplace + 1/5 of the distance driven by bus.
  Rtime
  R_time-1
  init_count_turtles
  a      ;; Function coefficents for the sprouting of agents by cells
  b
  incubation_variance
  run-number  ;; controls unique filenames for behavior space
  result       ;; variable used to stop the connectedness behavior-space experiment
  stopper       ;; variable used to stop the connectedness behavior-space experiment
  dead        ;;counter for dead turtles
  ]

patches-own [
  residence    ;;defines work patches
  employees    ;;used for controlling how many turtles each work-patch should hold
  ]
  
turtles-own [
  my-home   ;;holds the id of the patch where the turtle lives
  at-home
  my-work    ;;holds the id of the patch where the turtle works
  at-work
  job-leave-time  ;;the time when the turtle is off (set as the time of arrival at work + 480)
  employed
  busrider
  riding
  heading-to-bus
  journey-to-work
  heading-north             ;; controls the bus direction
  depart                    ;; controls the bus departure time
  ;;------------------------------------------------------------
  infected                  ;;here begins the disease-specific variables
  time-of-infection
  latent
  incubating
  infectious
  disease
  immune
  asymptomatic              ;;if turtle is infected but does not fall ill. Asymptotic turtles have only half the infectiousness of symptotic turtles
  R
  ]
  
;;----------------------------------------------------------------------------------------------

to setup
  clear-global-variables                      ;; all variables but the filename-identifier variable are cleared
  define_function_parameters_for_sprouting
  define-cases
  define-homepatches
  set init_count_turtles count turtles
  define-workpatches
  distribute-workers
  define-busstops 
  create-bus
  define-busriders
  set-immunity-and-get-infected
  ;;set incubation_variance (user-input "incubation_variance")
 
end

to make-movie

  ;; prompt user for movie location
  user-message "First, save your new movie file (choose a name ending with .mov)"
  let path user-new-file
  if not is-string? path [ stop ]  ;; stop if user canceled

  ;; run the model
  setup
  movie-start path
  movie-grab-view
  while [ count persons with [infected = 1] > 0 ]
    [ go
      movie-grab-view ]

  ;; export the movie
  movie-close
  user-message (word "Exported movie to " path)
end

to go
  if count persons with [infected = 1] = 0 [stop show "im done!"]   ;;;while [count persons with [infected = 1] > 0] [
    let-time-pass
    get-bus-going
    ask persons with [infectious = 1] [
      if trace? [show "Time is " + time + ": " + "I am " + who + " and I have infected " + R]      ;; writes how many turtles the infected turtle has infected in total
      ifelse at-work = 1 and job-leave-time > time [
        ;;if time mod 10 = 0 [
          infect
          ;;]
        ]
          [
          infect
        ]
      ]
      ask persons with [infected = 1]
        [check-status]
        
  ifelse trace-route? 
    [ask persons with [infected = 1] [pd]]
    [ask persons                     [pu]]
  
  go-home-if-sick
  go-to-work
  go-home
  if time mod 1441 = 0 [
    update-plot  
    ]
end

to clear-global-variables
  clear-turtles
  clear-patches
  clear-all-plots
  clear-drawing
  set time 0
  set minutes 0
  set hours 0
  set days 0
  set weekend 0
  set home-patches 0
  set work-patches 0
  set home-busstop 0
  set work-busstop 0
  ;;set sum-1/n 0
  set busdistance 0
  set Rtime 0
  set R_time-1 0
  set init_count_turtles 0
  set a 0
  set b 0
  set incubation_variance 0
  set stopper 0
end


to infect
  ifelse asymptomatic = 1 [        ;;;note: convert the following to a repeat loop going from 1 to infectious_period with infectiousness for each day stored in a list
    if int ((time - (time-of-infection + (incubation * 1440))) / 1440) = 0 [     ;;infectiousness the first day of infectious
      ask persons in-radius radius with [immune = 0 and infected = 0] [ ;; in-radius radius 
        if (0.0001637 * 0.5) > random-float 1 [  ;;if assymptomatic, rate is halfed
          get-infected 
          ask myself [set R R + 1]
          ;;show time + ": " + myself + " just infected me!"
          if trace? [show "Time is " + time + ": " + "I am " + who + " infected by " + myself]
        ]
      ]
    ]
 
  if int ((time - (time-of-infection + (incubation * 1440))) / 1440) = 1 [     ;;infectiousness the second day of infectious
    ask persons in-radius radius with [immune = 0 and infected = 0] [ ;; in-radius radius 
      if (0.0000356 * 0.5) > random-float 1 [  ;;if assymptomatic, rate is halfed
        get-infected 
        ask myself [set R R + 1]
        ;;show time + ": " + myself + " just infected me!"
        if trace? [show "Time is " + time + ": " + "I am " + who + " infected by " + myself]
       ]
      ]
    ]
    
   if int ((time - (time-of-infection + (incubation * 1440))) / 1440) = 2 [     ;;infectiousness the third day of infectious
    ask persons in-radius radius with [immune = 0 and infected = 0] [ ;; in-radius radius 
      if (0.000014 * 0.5) > random-float 1 [  ;;if assymptomatic, rate is halfed
        get-infected 
        ask myself [set R R + 1]
        ;;show time + ": " + myself + " just infected me!"
        if trace? [show "Time is " + time + ": " + "I am " + who + " infected by " + myself]
       ]
      ]
    ]  
       
    if int ((time - (time-of-infection + (incubation * 1440))) / 1440) >= 3 [     ;;infectiousness the fourth day of infectious
    ask persons in-radius radius with [immune = 0 and infected = 0] [ ;; in-radius radius 
      if (0.0000001 * 0.5) > random-float 1 [  ;;if assymptomatic, rate is halfed
        get-infected 
        ask myself [set R R + 1]
        ;;show time + ": " + myself + " just infected me!"
        if trace? [show "Time is " + time + ": " + "I am " + who + " infected by " + myself]
       ]
      ]
    ]      
      
    ]  ;;end of first part of ifelse
    
    [  ;;;if NOT asymptomatic--------------------------------
if int ((time - (time-of-infection + (incubation * 1440))) / 1440) = 0 [     ;;infectiousness the first day of infectious
      ask persons in-radius radius with [immune = 0 and infected = 0] [ ;; in-radius radius 
        if 0.0001637 > random-float 1 [ 
          get-infected 
          ask myself [set R R + 1]
          ;;show time + ": " + myself + " just infected me!"
          if trace? [show "Time is " + time + ": " + "I am " + who + " infected by " + myself]
        ]
      ]
    ]
 
  if int ((time - (time-of-infection + (incubation * 1440))) / 1440) = 1 [     ;;infectiousness the second day of infectious
    ask persons in-radius radius with [immune = 0 and infected = 0] [ ;; in-radius radius 
      if 0.0000356 > random-float 1 [ 
        get-infected 
        ask myself [set R R + 1]
        ;;show time + ": " + myself + " just infected me!"
        if trace? [show "Time is " + time + ": " + "I am " + who + " infected by " + myself]
       ]
      ]
    ]
    
   if int ((time - (time-of-infection + (incubation * 1440))) / 1440) = 2 [     ;;infectiousness the third day of infectious
    ask persons in-radius radius with [immune = 0 and infected = 0] [ ;; in-radius radius 
      if 0.000014 > random-float 1 [
        get-infected 
        ask myself [set R R + 1]
        ;;show time + ": " + myself + " just infected me!"
        if trace? [show "Time is " + time + ": " + "I am " + who + " infected by " + myself]
       ]
      ]
    ]  
       
    if int ((time - (time-of-infection + (incubation * 1440))) / 1440) >= 3 [     ;;infectiousness the fourth day of infectious
    ask persons in-radius radius with [immune = 0 and infected = 0] [ ;; in-radius radius 
      if 0.0000001 > random-float 1 [
        get-infected 
        ask myself [set R R + 1]
        ;;show time + ": " + myself + " just infected me!"
        if trace? [show "Time is " + time + ": " + "I am " + who + " infected by " + myself]
       ]
      ]
    ]      
 ]
end 

to set-immunity-and-get-infected
  ask persons [
    if random 100 < immunity [set immune 1]
    ]
  ask n-of Init-seed persons with [immune != 1] [get-infected] ;;seeds the disease
end


to get-infected
    set infected 1
    if random 100 > 67 [set asymptomatic 1]
    ;;show "im infected but I dont know"
    set time-of-infection time
    set latent 1
    set incubating 1
    set color red
end


to check-status
 if latent = 1 and (time-of-infection + (latency * 1440)) = time              ;; transit from latent to infectious
   [set latent 0
   set infectious 1
   set color yellow 
  ;;show "im infectious at time since infection: " + (time - time-of-infection) / 1440
   ]

 if incubating = 1 and (time-of-infection + (incubation  * 1440))  = time
   [set incubating 0
    if asymptomatic = 0 [
      set disease 1
      set color red
      ;;show "im so sick!"  + (time - time-of-infection) / 1440
      ;;set label "Im sick!"
      ]
   ]
   
if infectious = 1 and (time-of-infection + ((latency + infectious_period) * 1440)) = time
  [set infectious 0
   set color red
  ;;show "im not infectious" + (time - (time-of-infection + ((latency * 1440)))) / 1440
  ]
 

 
 if disease = 1 and (time-of-infection + ((incubation + clinical_disease_period) * 1440)) = time [
   ifelse random 100 >= case-fatality [
   set disease 0
   set color green
   set immune 1
   set infected 0
   set infectious 0
   set label ""
   ;;show "i survive " +  (time - (time-of-infection + (incubation * 1440)))/ 1440 + "disease is " + disease
   ]
  [;;show "i die"
  die
  set dead dead + 1]
  ]
  
  
 if asymptomatic = 1 and (time-of-infection + ((incubation + clinical_disease_period) * 1440)) = time [
   set disease 0
   set color green
   set immune 1
   set infected 0
   set infectious 0
   set label ""
   ;;show "i survive"
   ]
 
  
  
  ifelse show-immune-as-blue? 
   [ask turtles with [immune = 1]
      [set color blue]
    ]
    [ask turtles with [immune = 1]
      [set color green]
    ]
end

 
to update-plot
  set-current-plot "Main Plot"
  set-current-plot-pen "ill"
  plot count turtles with [disease = 1 and (time-of-infection + (incubation * 1440)) > (time - 1440)]
  set-current-plot-pen "immune"
  plot (count turtles with [immune = 1] / init_count_turtles) * 100
  set-current-plot-pen "dead"
  plot (init_count_turtles - (count turtles - 1)) / init_count_turtles * 100
  
  ifelse any? persons with [R > 0] [
    set Rtime mean values-from persons with [R > 0] [R]]
    [set Rtime 0]
    set-current-plot "Rt Plot"
    plot Rtime
    ask persons with [R > 0] [set R 0]
    
end
 

;-----------------------------------------------------------------------------------

  


to create-bus
create-custom-bus 1 [
    setxy (pxcor-of home-busstop) (pycor-of home-busstop)
    set shape "van top"
    set size 10
    set busdistance (distance-nowrap work-busstop / 1.5)
    set heading-north  1
    set depart (hours * 60) + minutes + 5
    ]
end




to get-bus-going
 ask bus [
   if hours = 0 and minutes = 0 [set depart 5]
   
   if distance work-busstop < 3 and heading-north = 1 [          ;; stop bus if close to work-busstop
     setxy pxcor-of work-busstop pycor-of work-busstop
     set heading-north 0
     set depart (hours * 60) + minutes + 5
   ]
      
   if heading-north = 1 and (hours * 60) + minutes >= depart [  ;; let bus drive to work-busstop if it has been waiting 5 minutes at home-busstop
     face-nowrap work-busstop
     fd 5
     ]
   
   if distance home-busstop < 3 and heading-north = 0 [        ;; stop bus if close to home-busstop
     setxy pxcor-of home-busstop pycor-of home-busstop
     set heading-north 1
     set depart (hours * 60) + minutes + 5
   ]
   
   if heading-north = 0 and (hours * 60) + minutes >= depart [  ;; let bus drive to home-busstop if it has been waiting 5 minutes at work-busstop
     face-nowrap home-busstop
     fd 5
   ]
 ]
end


to define-homepatches
let counter 1
repeat 8 [
    ask n-of ((a * exp (b * counter)) * (population / 1.807918318)) patches with [residence != 1]
      [                                                                       
      set pcolor red
      set residence 1
      sprout-persons counter [
        set size 3
        set color green
        set my-home myself                                                                   ;;and instructs the turtles where they live
        set at-home 1
        set at-work 0
     ]
   ]
   set counter counter + 1
]
set home-patches patches with [residence = 1]
end

to define-workpatches
set work-patches n-of (init_count_turtles * 0.046) patches with [ residence != 1 and pxcor > 0 and pycor > 0]       ;; Define work-patches as an agentset in the upper right corner of the canvas where workplaces is the number of patches to be included (as specified on the UI slider)
ask work-patches [set employees 0]
while [any? work-patches with [employees = 0]] [
  let counter counter + 1
  ask one-of work-patches with [employees = 0] [
    set pcolor green
    set employees (1 / counter)                                                                          ;;sets the rank-size distribution of how many employees each work-patch shall have
    ]
  ]
end

to distribute-workers
set sum-1/n sum values-from work-patches [employees]
  ask work-patches [
    set employees int (count persons * (employees / sum-1/n))                                            ;;ascribes the turtles to the workpatches according to the rank-size
    without-interruption [
      ask n-of employees persons with [my-work = 0 and employed = 0] [
        set my-work myself
        set employed 1
        ]
      ]
    ]
  show count persons with [my-work = 0] + " unemployed"
end




to define-busstops
set home-busstop patch-at (mean values-from home-patches with [pycor < 0] [pxcor]) (mean values-from home-patches with [pycor < 0] [pycor])
  ask home-busstop [set pcolor white]
set work-busstop patch-at (mean values-from work-patches [pxcor]) (mean values-from work-patches [pycor])
  ask work-busstop [set pcolor white]
end

to define-busriders
  ask persons with [employed = 1] [
    setxy (pxcor-of work-busstop) (pycor-of work-busstop)
    set journey-to-work distance-nowrap my-work
    setxy (pxcor-of my-home) (pycor-of my-home)
    set journey-to-work journey-to-work + (distance-nowrap home-busstop) + busdistance
    if distance-nowrap my-work > journey-to-work
      [set busrider 1
       set heading-to-bus 1
       ;;set color white
      ]
    ]
end


 ;; ------------------------MOBILITY PROCEDURES

to ride-bus-to-work
  ifelse any? bus in-radius 6 [
    setxy xcor-of one-of bus ycor-of one-of bus
    ]
    [
     ifelse residence-of patch-ahead 1 = 1
      [rt random 90]
      [fd 1
       face-nowrap home-busstop
      ]
    ]
  if patch-here = work-busstop
    [set heading-to-bus 0
    ;;show heading-to-bus
    ;;show "im getting of at work"
    ]
end

to ride-bus-home
  ifelse any? bus in-radius 6 and heading-to-bus = 1  [
    setxy xcor-of one-of bus ycor-of one-of bus
    ]
    [
     if heading-to-bus = 1 [
       ifelse residence-of patch-ahead 1 = 1 
         [rt 90]
         [fd 1
          face-nowrap work-busstop
         ]
       ]
     ]
  if patch-here = home-busstop

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -