SEIS Disease Model Help in NetLogo - infected individuals do not become susceptible again? - netlogo

I'm developing a simple NetLogo disease model that has 4 compartments:
S - susceptible individuals
E - Exposed individuals
I - Infected individuals
S - Recovered individuals that become susceptible again (i.e. there is no immunity).
My simulation starts off with 1 individual who is initially infected with the rest being susceptible.
This is the code I have so far:
turtles-own [
disease?
latent?
susceptible?
latent-period-time
infectious-period-time
]
to setup
clear-all
create-turtles num-agents [ setxy random-xcor random-ycor
set shape "wolf"
set size 2
become-susceptible
]
ask n-of infected-agents turtles [become-infected]
reset-ticks
end
to go
move
spread
tick
end
to move
ask turtles [
right random 50
left random 50
fd 1 ]
end
to spread
ask turtles [
ifelse disease? [] [
if any? other turtles-here with [ disease? ]
[ become-latent
set latent-period-time 0 ]
]]
ask turtles [
if latent-period-time = latent-period ;latent-period is a slider variable set to 30
[
become-infected
set infectious-period-time 0]
]
ask turtles [
if infectious-period-time = infectious-period ;infectious-period is a slider variable set to 100
[
become-susceptible]
]
ask turtles [
if latent?
[ set latent-period-time latent-period-time + 1 ]
if disease?
[set infectious-period-time infectious-period-time + 1] ]
end
to become-susceptible
set disease? false
set latent? false
set susceptible? true
set color orange
end
to become-latent
set latent? true
set disease? false
set susceptible? false
set color gray
end
to become-infected
set latent? false
set disease? true
set susceptible? false
set color blue
end
For some reason only the initial infected individual seems to go back to the susceptible pool, while any other newly infected individuals do not go back to the susceptible pool. The initially infected individual is also unable to get infected again after going back to the susceptible pool even though it encounters infected individuals.
I'm not sure how to fix this problem.
Thanks!

Your problem is that you never reset the value of latent-period-time and infectious-period-time back to 0. There are two ways to fix this:
Put the set to 0 into the same code that changes all the state flags and colours
Scrap the tracking and incrementing entirely and use a variable that records when the turtle gets to the state - assume it's called 'state-start-time' then you would simply have set state-start-time ticks and then do a subtraction for your test of duration.

Related

To do a netlogo model with several patch layers (think floors of a building) do I need to go to Netlogo 3D?

I have a netlogo application in mind which involves multiple non interacting layers. Think floors of a building. Would I need to go to netlogo 3D or is there a suggested way to handle in regular netlogo?
This was an interesting enough question that I decided to make a simple sample.
The method I use is to have a global variable to track the number-of-floors we'll have in our building, as well as the active-floor that we are currently working with. Then all our agents, walls and workers, have a floor-num that tracks which they are on. We have a set-active-floor procedure that handles switching our currently active floor that we want to see and work with, making the patches a certain color (active-color) if they have a wall agent present and swapping which workers are hidden?. In this way, most of the magic happens in the setup-floor and set-active-floor procedures, and the real work of our model in go can be pretty typical NetLogo code asking the active-workers to do whatever we want.
While the model is running you can call set-active-floor n to any value 0 to 4 to change the current workers and walls. I also included a show-all procedure that'll un-hide all the walls and workers and let you see where they are at; you'll need to run that with the model stopped.
globals [colors active-color number-of-floors active-floor]
turtles-own [floor-num]
breed [walls wall]
breed [workers worker]
to setup
clear-all
set colors (list red blue green orange violet)
set number-of-floors 5
foreach (range 0 number-of-floors) setup-floor
reset-ticks
set-active-floor 0
end
to setup-floor [num]
set active-floor num
set active-color (item num colors)
ask patches [ set pcolor black ]
; make some random walls
create-walls (count patches * 0.2) [
set floor-num num
set hidden? true
set size 0.5
setxy random-pxcor random-pycor
set pcolor active-color
]
; only one wall per patch
ask patches [
let patch-walls floor-walls-here
let wall-count count patch-walls
if (wall-count > 1) [
ask n-of (wall-count - 1) patch-walls [ die ]
]
]
; make some workes
create-workers 10 [
set floor-num num
set hidden? true
set shape "person"
set color active-color - 2
move-to one-of patches with [pcolor != active-color]
]
end
to go
; this only "runs" the active floor, but you could run all of them
; using the same `foreach (range 0 number-of-floors) ...` code as
; in the `setup` procedure.
set-active-floor active-floor
ask active-workers [
; this code can be about the same as you'd write in a normal model...
move-to one-of patches with [pcolor != active-color]
]
tick
end
to set-active-floor [num]
set active-floor num
set active-color (item num colors)
; after the `pcolor` is set, we can use that to determine if a wall
; exists or not for an `active-worker`, we don't have to check the
; floor number at all while we do our work.
ask walls [ set hidden? true ]
ask patches [
set pcolor ifelse-value no-walls-here [ black ] [ active-color ]
]
ask workers [
set hidden? floor-num != active-floor
]
end
to-report active-workers
report workers with [floor-num = active-floor]
end
to-report floor-walls-here
report walls-here with [floor-num = active-floor]
end
to-report no-walls-here
report (count floor-walls-here = 0)
end
to show-all
foreach (range 0 number-of-floors) [ num ->
ask patches [
set pcolor ifelse-value any? walls-here [ red - 3 ] [ black ]
]
ask turtles with [floor-num = num] [
set color item num colors
set hidden? false
]
]
end
Finally, if things got much more complicated than this, I would probably choose to move to NetLogo 3D instead.

Storing a value

A classroom is simulated where appliances (e.g lights Fans and ACs) turn on when a student sits next to it. Each appliance has its own wattage rating. When an appliance is turned on its color changes to green and the on-time is noted and the duration on-time is stored. But if a student sits next to a appliance (e.g light) that is already on. The duration-on-time should not be stored as it would be a repetition.
globals[
simulation-timer
to appliance-on
ask students [ ask lights in-radius 4
[ifelse not already-on?
[ set color green
set light-on-time ticks
set light-on-duration light-on-duration + (time - ticks)
show (word "light on duration = " light-on-duration)
set already-on? true] [
set light-on-duration light-on-duration]]]
In this code the light-on-duration is not adding for all of the lights. Only individual light-on-duration is shown. How should I fix this? Thank you!
I think you have a logic problem rather than a coding problem. You can't add to duration when the light turns on because it hasn't yet built up any duration. Here is a complete model that turns lights on and off and stores duration. I am using ticks as the time, and each tick it adds 5 students and removes 5 students. But what's important is the logic of turning the lights on and off.
globals [light-radius]
breed [students student]
students-own
[ desk
]
breed [lights light]
lights-own
[ on?
turned-on
duration-on
]
to setup
clear-all
set light-radius 3
ask patches [ set pcolor white ]
ask patches with [pxcor mod 3 = 0 and pycor mod 3 = 0]
[ sprout-lights 1
[ set size 0
set on? false
set pcolor gray
]
]
reset-ticks
ask n-of 30 patches
[ sprout-students 1
[ set color blue
]
ask lights in-radius light-radius [switch-light-on]
]
end
to go
repeat 5 [student-arrive]
repeat 5 [student-leave]
ask lights with [any? students in-radius light-radius]
[ switch-light-on
]
tick
end
to student-arrive
ask one-of patches with [not any? students-here]
[ sprout-students 1
[ set color blue
ask lights in-radius light-radius with [not on?]
[ switch-light-on
]
]
]
end
to switch-light-on
set pcolor yellow
set on? true
set turned-on ticks
end
to student-leave
ask one-of students
[ die
]
ask lights with [ on? and not any? students in-radius light-radius ]
[ switch-light-off
]
end
to switch-light-off
set pcolor gray
set on? false
type "previous duration: " print duration-on
let how-long ticks + 1 - turned-on
set duration-on duration-on + how-long
type "new duration: " print duration-on
end
Note that you can't actually see the light turtles, I am making the patch turn yellow for on and grey for off. Every third patch has a light.

Evolutionary dynamics on a Prisoners Dilemma N-Person Iterated program

So, this is a part of a task I have to get done: ''Create a version of this model that rewards successful strategies by allowing them to reproduce and punishes unsuccessful strategies by allowing them to die off.''
But the thing is I can't get it to work, it displays an error every number of ticks, this is the code I have so far (I removed the commands of scores, dying and reproducing, so this is the pure code you could say) what should I add to make it work?
Help would be much appreciated, thanks!
This is the code
globals [
;;number of turtles with each strategy
num-random
num-cooperate
num-defect
num-tit-for-tat
num-unforgiving
num-unknown
;;number of interactions by each strategy
num-random-games
num-cooperate-games
num-defect-games
num-tit-for-tat-games
num-unforgiving-games
num-unknown-games
;;total score of all turtles playing each strategy
random-score
cooperate-score
defect-score
tit-for-tat-score
unforgiving-score
unknown-score
;;Noise
noise-active ;;turn noise on and off
noise-prob-defect ;;probability to flip a cooperate into a defect
noise-prob-cooperate ;;probability to flig a defect into a cooperate
noise-defect-true ;;Counter of times a defect turned into a cooperate
noise-cooperate-true ;;Counter of times a cooperate turned into a defect
]
turtles-own [
score
strategy
defect-now?
partner-defected? ;;action of the partner
partnered? ;;am I partnered?
partner ;;WHO of my partner (nobody if not partnered)
partner-history ;;a list containing information about past interactions
;;with other turtles (indexed by WHO values)
]
;;;;;;;;;;;;;;;;;;;;;;
;;;Setup Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;
to setup
clear-all
store-initial-turtle-counts ;;record the number of turtles created for each strategy
setup-turtles ;;setup the turtles and distribute them randomly
noise-setup ;; setup of the noise variables
reset-ticks
end
;;record the number of turtles created for each strategy
;;The number of turtles of each strategy is used when calculating average payoffs.
;;Slider values might change over time, so we need to record their settings.
;;Counting the turtles would also work, but slows the model.
to store-initial-turtle-counts
set num-random n-random
set num-cooperate n-cooperate
set num-defect n-defect
set num-tit-for-tat n-tit-for-tat
set num-unforgiving n-unforgiving
set num-unknown n-unknown
end
;;setup the turtles and distribute them randomly
to setup-turtles
make-turtles ;;create the appropriate number of turtles playing each strategy
setup-common-variables ;;sets the variables that all turtles share
end
;;create the appropriate number of turtles playing each strategy
to make-turtles
create-turtles num-random [ set strategy "random" set color gray - 1 ]
create-turtles num-cooperate [ set strategy "cooperate" set color red ]
create-turtles num-defect [ set strategy "defect" set color blue ]
create-turtles num-tit-for-tat [ set strategy "tit-for-tat" set color lime ]
create-turtles num-unforgiving [ set strategy "unforgiving" set color turquoise - 1 ]
create-turtles num-unknown [set strategy "unknown" set color magenta ]
end
;;set the variables that all turtles share
to setup-common-variables
ask turtles [
set score 0
set partnered? false
set partner nobody
setxy random-xcor random-ycor
]
setup-history-lists ;;initialize PARTNER-HISTORY list in all turtles
end
;;initialize PARTNER-HISTORY list in all turtles
to setup-history-lists
let num-turtles count turtles
let default-history [] ;;initialize the DEFAULT-HISTORY variable to be a list
;;create a list with NUM-TURTLE elements for storing partner histories
repeat num-turtles [ set default-history (fput false default-history) ]
;;give each turtle a copy of this list for tracking partner histories
ask turtles [ set partner-history default-history ]
end
;;;;;;;;;;;;;;;;;;;;;;;;
;;;Runtime Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;;;
to go
clear-last-round
ask turtles [ partner-up ] ;;have turtles try to find a partner
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [ select-action ] ;;all partnered turtles select action
ask partnered-turtles [ play-a-round ]
do-scoring
tick
end
to clear-last-round
let partnered-turtles turtles with [ partnered? ]
ifelse (random 100 > prob-of-staying)
[ask partnered-turtles [ release-partners ]]
[]
end
;;release partner and turn around to leave
to release-partners
set partnered? false
set partner nobody
rt 180
set label ""
end
;;have turtles try to find a partner
;;Since other turtles that have already executed partner-up may have
;;caused the turtle executing partner-up to be partnered,
;;a check is needed to make sure the calling turtle isn't partnered.
to partner-up ;;turtle procedure
if (not partnered?) [ ;;make sure still not partnered
rt (random-float 90 - random-float 90) fd 1 ;;move around randomly
set partner one-of (turtles-at -1 0) with [ not partnered? ]
if partner != nobody [ ;;if successful grabbing a partner, partner up
set partnered? true
set heading 270 ;;face partner
ask partner [
set partnered? true
set partner myself
set heading 90
]
]
]
end
;;choose an action based upon the strategy being played
to select-action ;;turtle procedure
if strategy = "random" [ act-randomly ]
if strategy = "cooperate" [ cooperate ]
if strategy = "defect" [ defect ]
if strategy = "tit-for-tat" [ tit-for-tat ]
if strategy = "unforgiving" [ unforgiving ]
if strategy = "unknown" [ unknown ]
add-noise ;;adds noise to the games
end
to play-a-round ;;turtle procedure
get-payoff ;;calculate the payoff for this round
update-history ;;store the results for next time
end
;;calculate the payoff for this round and
;;display a label with that payoff.
to get-payoff
set partner-defected? [defect-now?] of partner
ifelse partner-defected? [
ifelse defect-now? [
set score (score + 1) set label 1
] [
set score (score + 0) set label 0
]
] [
ifelse defect-now? [
set score (score + 5) set label 5
] [
set score (score + 3) set label 3
]
]
end
;;update PARTNER-HISTORY based upon the strategy being played
to update-history
if strategy = "random" [ act-randomly-history-update ]
if strategy = "cooperate" [ cooperate-history-update ]
if strategy = "defect" [ defect-history-update ]
if strategy = "tit-for-tat" [ tit-for-tat-history-update ]
if strategy = "unforgiving" [ unforgiving-history-update ]
if strategy = "unknown" [ unknown-history-update ]
end
;;;;;;;;;;;;;;;;
;;;Strategies;;;
;;;;;;;;;;;;;;;;
;;All the strategies are described in the Info tab.
to act-randomly
set num-random-games num-random-games + 1
ifelse (random-float 1.0 < 0.5) [
set defect-now? false
] [
set defect-now? true
]
end
to act-randomly-history-update
;;uses no history- this is just for similarity with the other strategies
end
to cooperate
set num-cooperate-games num-cooperate-games + 1
set defect-now? false
end
to cooperate-history-update
;;uses no history- this is just for similarity with the other strategies
end
to defect
set num-defect-games num-defect-games + 1
set defect-now? true
end
to defect-history-update
;;uses no history- this is just for similarity with the other strategies
end
to tit-for-tat
set num-tit-for-tat-games num-tit-for-tat-games + 1
set partner-defected? item ([who] of partner) partner-history
ifelse (partner-defected?) [
set defect-now? true
] [
set defect-now? false
]
end
to tit-for-tat-history-update
set partner-history
(replace-item ([who] of partner) partner-history partner-defected?)
end
to unforgiving
set num-unforgiving-games num-unforgiving-games + 1
set partner-defected? item ([who] of partner) partner-history
ifelse (partner-defected?)
[set defect-now? true]
[set defect-now? false]
end
to unforgiving-history-update
if partner-defected? [
set partner-history
(replace-item ([who] of partner) partner-history partner-defected?)
]
end
;;defaults to tit-for-tat
;;can you do better?
;;Generous Tit-for-Tat, cooperates 10% of the time that it would otherwise defect.
to unknown
set num-unknown-games num-unknown-games + 1
set partner-defected? item ([who] of partner) partner-history
ifelse (partner-defected?)
[ifelse (random 100 < 10) ;;be generous in 10% of the cases you would defect
[set defect-now? false]
[set defect-now? true]
]
[set defect-now? false]
end
;;defaults to tit-for-tat-history-update
;;can you do better?
to unknown-history-update
set partner-history
(replace-item ([who] of partner) partner-history partner-defected?)
end
;;;;;;;;;;;
;;;Noise;;;
;;;;;;;;;;;
;;read values from slider and store them
to noise-setup
set noise-active noise ;;Set if noise is active or not depending on switch
set noise-prob-defect prob-def-to-cop ;;Passes the current probability of defect turning into cooperate
set noise-prob-cooperate prob-cop-to-def ;;Passes the current probability of cooperate turning into defect
end
;;changes the decision according to the noise setup
to add-noise
;;check if noise is activated
if (noise-active) [
;;check decision
ifelse (defect-now?)
[
if (random 100 < noise-prob-defect)
[
;;flip defect -> cooperate
set defect-now? false
set noise-defect-true (noise-defect-true + 1) ;;Counter of times a flip has been made
]
]
[
if (random 100 < noise-prob-cooperate)
[
;;flip cooperate -> defect
set defect-now? true
set noise-cooperate-true (noise-cooperate-true + 1) ;;Counter of times a flip has been made
]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;
;;;Plotting Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;;;;
;;calculate the total scores of each strategy
to do-scoring
set random-score (calc-score "random" num-random)
set cooperate-score (calc-score "cooperate" num-cooperate)
set defect-score (calc-score "defect" num-defect)
set tit-for-tat-score (calc-score "tit-for-tat" num-tit-for-tat)
set unforgiving-score (calc-score "unforgiving" num-unforgiving)
set unknown-score (calc-score "unknown" num-unknown)
end
;; returns the total score for a strategy if any turtles exist that are playing it
to-report calc-score [strategy-type num-with-strategy]
ifelse num-with-strategy > 0 [
report (sum [ score ] of (turtles with [ strategy = strategy-type ]))
] [
report 0
]
endenter code here

Simulating the spread of a virus from an infected individual to a susceptible individual in a small world network using netlogo?

I am simulating the spread of a virus across a small world network in netlogo. Using the existing model netlogo offers of a small world network where nodes can connect with other nodes using the 'Rewire One' button on the interface I am trying to get it so that if an initially infected individual gets rewired with a susceptible individual the virus will then transfer to that individual and the turtles color will update to red.
Here is the code im using to rewire one turtle with another:
to rewire-one
if count turtles != num-nodes [
setup
]
set rewire-one? true
set rewire-all? false
let potential-edges links with [ not rewired? ]
ifelse any? potential-edges [
ask one-of potential-edges [
let node1 end1
;; if "a" is not connected to everybody
if [ count link-neighbors ] of end1 < (count turtles - 1)
[
;; find a node distinct from node1 and not already a neighbor of node1
let node2 one-of turtles with [ (self != node1) and (not link-neighbor? node1) ]
;; wire the new edge
ask node1 [ create-link-with node2 [ set color cyan set rewired? true ] ]
ask links
[ reed-frost-children]
ask links
[ increment-children ]
set number-rewired number-rewired + 1 ;; counter for number of rewirings
;; remove the old edge
die
]
]
;;I am then using the reed-frost function as the force of infection which can be seen below
to reed-frost-children
ask children with [ child-sus = 1]
[
set num-infected-neighbours length filter [[ child-infected = 1] of ?]
[other-end] of my-links with [not rewired?] ;; i.e not closed? i.e open
set force-of-infection 1 - (1 - prob-infection-child) ^ num-infected- neighbours
if(random-float 1 <= force-of-infection)
[
set child-sus 0
set child-inncubated 1
set child-infected 0
set child-almost-infected 0
set child-recovered 0
set inncubation-period 100
show-turtle
set color orange
set size 1
ask link-neighbors with [ child-infected = 1]
[
ask my-links
[
set color orange show-link
ask other-end
[
set color orange
]
]
]
ask my-links [set closed? false] ;; closed i.e. not open
]
]
However the virus is not transmitting when an infected rewires with a susceptible. Any ideas?

Q: Need help to disseminate a crisis in a network

I have created a network of agents where each agent has a GDP value and a GDP growth rate. The agents are interconnected with undirected links.
I have made a countdown that every 200 ticks randomly assign a 'shock status' to 10 agents. When an agent is under shock it gets the value 'in-crisis? = true' and consequentially its GDP growth rate changes.
However, I want to add a second trigger. I want agents with the 'in-crisis? = false' to check their link-neighbors and see if any of them have 'in-crisis? = true'. If they are linked to an agent whose in-crisis is true, then they should check if their own GDP is smaller than half of the GDP of the linked state that has 'in-crisis? = true'. If it is smaller, then the agent will set his own 'in-crisis?' as true.
to shock
if ticks mod 200 = 0 [ ;; This is the countdown
spread-crisis
ask n-of 10 turtles [
become-in-crisis]
]
end
to spread-crisis ;;;;;;;;;;;;;;;; This is the second trigger. I need help here!
ask turtles with [in-crisis? = false]
[ if any? link-neighbors with [in-crisis? = true] [
if any? link-neighbors with [gdp > my-gdp] [
become-in-crisis ] ]]
end
to-report my-gdp
report (gdp * 2)
end
to become-in-crisis
set in-crisis? true
let random-years-of-shock 20 + random 100
set shock-tick random-years-of-shock
end
If you have time, please help me to adjust the spread-crisis procedure!
Thank you
Change my-gdp to [my-gdp] of myself.
to spread-crisis
ask turtles with [not in-crisis?] [
if any? link-neighbors with [in-crisis? and gdp > [my-gdp] of myself] [
become-in-crisis
]
]
end