I'm writing some code to vaccinate individuals in a population where flu is circulating. There are two kinds of vaccines (named HOV and HEV in the code); the HOV is already done. What I tried/wanted to do was convert the agentset of blue turtles into a list, that I could then iterate over one turtle at a time. I need to do this because the HEV condition has to give a slightly different vaccine to each blue turtle (i.e. the flu strain in each vaccine has to be slightly different). However, NetLogo is highlighting "foreach [vax-turtles]" with an error message that says "Expected a literal value."
My code is below, the "if vaccine = "HEV"" piece of code is what I need help with:
extensions [csv]
globals [strain_list_list epidemic-threshold cumulative-infections proportion-of-infection currently-infected peak-prevalence vax-strain]
turtles-own [infection-period infection-number tflu-strain immune-label ant-distance cross-immunity]
patches-own [fomite-age pflu-strain]
to setup
clear-all
setup-turtles
set strain_list_list (list t-sorted-strain-list)
print strain_list_list
reset-ticks
end
to-report t-sorted-strain-list
report map [i -> [tflu-strain] of i] sort turtles
end
to setup-turtles
create-turtles 100
ask turtles [setxy random-xcor random-ycor]
ask turtles [set color white set infection-number 0 set immune-label -999999999999 set tflu-strain -999999999999]
ask one-of turtles [set color red set infection-period 0 set infection-number 1 set immune-label 0 set tflu-strain 0]
if vaccine = "HOV" [
ifelse (one-of [1 2] = 1) [
set vax-strain (random ((2 * drift-size) + 1))]
[set vax-strain (-1 * random ((2 * drift-size) + 1))]
ask n-of prop-vax turtles with [color = white] [set color blue set immune-label vax-strain]
]
if vaccine = "HEV" [
let vax-turtles (list turtles with [color = blue])
ask n-of prop-vax turtles with [color = white] [set color blue]
foreach [vax-turtles] [
ifelse (one-of [1 2] = 1) [
set vax-strain (random ((2 * drift-size) + 1))]
[set vax-strain (-1 * random ((2 * drift-size) + 1))]
]
]
set epidemic-threshold "no"
set cumulative-infections 0
set peak-prevalence 0
end
to go
if ticks = flu-season-length [stop]
move-infecteds
move-uninfecteds
transmit
mutate
update-immunity
track-infecteds
set cumulative-infections (count turtles with [infection-period = 1] + cumulative-infections)
set proportion-of-infection (100 - (count turtles with [immune-label = -999999999999]))
set currently-infected (count turtles with [infection-period = 1])
csv:to-file "strains_each_tick.csv" strain_list_list
set strain_list_list lput t-sorted-strain-list strain_list_list
tick
end
to move-uninfecteds ;; uninfected turtles move faster than infected ones
ask turtles with [color = white or color = blue] [
right random 360
forward 5
]
end
to move-infecteds ;; infected turtles move slower than uninfected ones and always transmit infection to patches before they leave them
ask turtles with [color = red] [
if pcolor = black [
set pcolor red
set fomite-age 0
set pflu-strain tflu-strain]
right random 360
forward 3
]
end
to transmit ;; uninfected turtles are infected by fomites (red patches) with some probability. immune-labelling currently first infection
ask turtles with [color = white or color = blue and pcolor = red] [
if immune-label != pflu-strain [
set ant-distance (abs (immune-label - pflu-strain))
set cross-immunity (natural-immunity * (1 - (ant-distance / antigenic-distance-limit)))
if cross-immunity < 0 [set cross-immunity 0]
if random 100 < (((100 - cross-immunity) / 100) * probability-of-infection)
[set color red set infection-period 0 set infection-number infection-number + 1 set tflu-strain pflu-strain]
]
if immune-label = pflu-strain [
if random 100 < (((100 - natural-immunity) / 100) * probability-of-infection)
[set color red set infection-period 0 set infection-number infection-number + 1 set tflu-strain pflu-strain]
]
]
end
to mutate ;; some probability of mutation (change in strain label) when an individual receives infection from a patch
ifelse in-host [
ask turtles with [color = red] [
if random 100 < probability-of-mutation [
ifelse (one-of [1 2] = 1) [
set tflu-strain (tflu-strain + (random (drift-size + 1)))]
[set tflu-strain (tflu-strain - (random (drift-size + 1)))]
]
]
]
[ask turtles with [color = red and infection-period = 0] [
if random 100 < probability-of-mutation [
ifelse (one-of [1 2] = 1) [
set tflu-strain (tflu-strain + (random (drift-size + 1)))]
[set tflu-strain (tflu-strain - (random (drift-size + 1)))]
]
]
]
end
to update-immunity
ask turtles with [color = red and infection-period = 0] [
ifelse immune-labelling = "first-infection" [
if immune-label = -999999999999 [
set immune-label tflu-strain]]
[set immune-label tflu-strain]
]
end
to track-infecteds ;; turtles with given infection period should become uninfected
ask turtles with [color = red] [
set infection-period infection-period + 1
if infection-period = age-infection-max [
set color white set infection-period 0 set tflu-strain -999999999999
]
]
ask patches with [pcolor = red] [
set fomite-age fomite-age + 1
if fomite-age > max-fomite-persistence [
set pcolor black
]
]
end
Any advice would be greatly appreciated! Thanks
Looking at your code, I can't see why you need to do the iteration rather than ask and it is much cleaner to stick with ask. So, try replacing:
if vaccine = "HEV" [
let vax-turtles (list turtles with [color = blue])
ask n-of prop-vax turtles with [color = white] [set color blue]
foreach [vax-turtles] [
ifelse (one-of [1 2] = 1) [
set vax-strain (random ((2 * drift-size) + 1))]
[set vax-strain (-1 * random ((2 * drift-size) + 1))]
]
]
with
if vaccine = "HEV" [
let vax-turtles turtles with [color = blue]
ask n-of prop-vax turtles with [color = white] [set color blue]
ask [vax-turtles] [
ifelse (one-of [1 2] = 1)
[ set vax-strain (random ((2 * drift-size) + 1)) ]
[ set vax-strain (-1 * random ((2 * drift-size) + 1)) ]
]
]
For that matter, I think the different values are simply different signs of the same value, so you could do:
if vaccine = "HEV" [
let vax-turtles turtles with [color = blue]
ask n-of prop-vax turtles with [color = white] [set color blue]
ask [vax-turtles] [
set vax-strain one-of [-1 1] * (random ((2 * drift-size) + 1))
]
]
Also, I am not sure if you are doing this intentionally, but you are changing the proportion to blue after creating the vax-turtles
agentset. This means that the newly changed turtles will not be given a vax-strain
until the next tick
or whenever the code is next run. Furthermore, the code you have will assign a new vax-strain
to all the blue turtles, even if they were made blue in a previous steps. If you actually want the vax-strain
to be assigned once only and the turtle to change colour to show this, try:
if vaccine = "HEV" [
ask n-of prop-vax turtles with [color = white]
[ set color blue
set vax-strain one-of [-1 1] * (random ((2 * drift-size) + 1))
type "drift-size is " type drift-size type " and vax-strain is " print vax-strain ;; new line here for diagnostics
]
]
UPDATE: ask
gives a new random number to each agent. You can see this by running the complete model:
turtles-own [testnum]
to setup
clear-all
create-turtles 10
[ set testnum random 5 ]
print [testnum] of turtles
end
The problem is with the line
let vax-turtles (list turtles with [color = blue])
the list
primitive creates a list, but not a list of turtles with [color = blue]
. Rather it creates a list of its arguments, which in this case is a single angentset. You instead want a list of the agents in the agentset. The of
reporter is the easiest way to get that as of
always returns a list.
let vax-turtles [self] of turtles with [color = blue]
Alternatively, if you wanted the list to be sorted, you could use sort
, which also always returns a list.
let vax-turtles sort turtles with [color = blue]
As for the problem you describe in the comment below, you need to ask each vax-turtle to set its own vax-strain (which I assume is a turtles-own variable). Assuming that you are using NetLogo 6.x,
if vaccine = "HEV" [
let vax-turtles [self] of turtles with [color = blue]
ask n-of prop-vax turtles with [color = white] [set color blue]
foreach [vax-turtles] [t ->
ifelse (one-of [1 2] = 1) [
ask t [set vax-strain (random ((2 * drift-size) + 1))]]
[ask t [set vax-strain (-1 * random ((2 * drift-size) + 1))]]
]
]
But if drift-size is the same for all vax-turtles, it would be pretty unlikely that, given random
, JenB's solution below would give the same value for vax-strain for any two turtles, would it not?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With