Netlogo turtle stops being infectious after 14 days and starts after being infected - netlogo

ask turtles with [ infected? and ticks <= 14 ]
[
infect
]
this is my code, after a turtle becomes infected? he will also infect. but I want it to infect only in a 14 day period. what happens is that it infects only on the first 14 ticks and stop. What I want to do is for example, a turtle becomes infected in tick 5, then it will stop infecting in tick 19. Thank you in advance

ticks is the global time step counter so it increments from 0 (when you start the simulation with reset-ticks). What you are trying to do is have each turtle know when it became infected and then be infectious for the next 14 ticks. So you have to create a turtle variable that tracks when it becomes infected, and use that as the condition. Something like:
turtles-own
[ infected?
when-infected
]
to infect
ask turtles
[ if < whatever code you have that exposes them >
[ set infected? true ; you must have this already
set when-infected ticks
]
...
end
to ???
ask turtles with [ infected? and when-infected >= ticks - 14 ] [ infect ]
...
end
You can see that the replacement line compares the turtle's particular value of the new when-infected variable to the current value of ticks

Related

How to implement a timer for each turtle in netlogo

I am trying to implement a timer for each turtle in Netlogo
so I can take the minimum value, maximum and average time taken for each turtle.
Can anyone help
You haven't said what you actually want timed (or given any code attempt). Any time you need a turtle to remember anything from one tick to the next, you need a variable. The simplest code for a timer is to have a variable that you set to the current value of ticks when the process starts and subtract that start time from the current value of ticks when the process stops. Here is a complete model example:
turtles-own
[ start-time
time-taken
]
to setup
clear-all
create-turtles 20
[ set start-time 2 + random 10
set time-taken -1
]
reset-ticks
end
to go
let movers turtles with [time-taken = -1 and start-time <= ticks]
ask movers
[ set heading random 360
forward 1 + random 3
if random-float 1 < 0.05 [ set time-taken ticks - start-time ]
]
tick
end
And check out the new Time extension if you want your timers to use real time units (seconds, minutes, years...) and have non-integer values.
https://github.com/NetLogo/Time-Extension
Look at primitives such as time:difference-between
For example, you could do this coding that flags the turtle you want to monitor and increments the flag variable when an event occurs. You can then monitor it in the BehaviorSpace and analyses the results output in a csv file. For example, the following codes:
globals [ID]
turtles-own [special-turtle]
to create-turtle
crt 1 [
setxy min-pxcor 0
set heading 90
set special-turtle false
set ID who]
end
to go
;omitted
special-turtles
tick
end
to special-turtles
ask turtles-on patch 0 0 [set ID who]
ask max-one-of turtles [who] [set special-turtle true]
ask turtles with [special-turtle = true][set special-turtle (special-turtle + 1)]
end
I think there is something that none of the previous answers considered: the way you implement your timer depends on how you want to use the measurement.
1 - If you want to read the measurement only after the measurement is completed
The expression "after the measurement is completed" can mean both that you want to read it at some point later during the model, or maybe even just from some model's output.
The approach I'd take in this case is similar to what JenB suggested, but I believe you can put it a bit simpler because here I only use one extra turtle variable (apart from my-variable, that is there only to represent something that you already have in your model):
turtles-own [
my-variable ; This represents something you have in your model, to be used as a condition for the timer
my-timer
]
to setup
clear-all
reset-ticks
create-turtles 10
end
to go
; Here you have your normal code. When you need, just include
; 'start-timer' and 'stop-timer'. For simplicity, let's
; say you want to count how long does each turtle take
; to get from a value of 1 to a value of 5 for my-variable.
; You can just do:
ask turtles [
if (random 10 < 2) [
set my-variable (my-variable + 1)
]
if (my-variable = 1) [
start-timer
]
if (my-variable = 5) [
stop-timer
type "I am turtle " type who type " and it took me " type my-timer print " ticks."
die
]
]
tick
if (not any? turtles) [stop]
end
to start-timer
set my-timer ticks
end
to stop-timer
set my-timer (ticks - my-timer)
end
Note that most of the code is there only to make a full reproducible example, but the actual implementation only consists of my-timer, to start-timer and to stop-timer.
You can take this approach because the hypothesis here is that you will be interested in reading the measurement only after to stop-timer happened.
Otherwise, see point 2.
2 - If you want to be able to read the measurement at any moment during the simulation
In this case, you need to make the timer progress as time progresses (as opposed to point 1, where you could just take the initial and final time, and make the difference between the two).
I guess the easiest way to do this is to conditionally increment the timer every time there is a tick.
Taking the same example as before, it would be something like:
turtles-own [
my-variable ; This represents something you have in your model, to be used as a condition for the timer
timer-active?
my-timer
]
to setup
clear-all
reset-ticks
create-turtles 10 [
set timer-active? FALSE
]
end
to go
ask turtles [
if (random 10 < 2) [
set my-variable (my-variable + 1)
]
if (my-variable = 1) [
set timer-active? TRUE
]
if (my-variable = 5) [
set timer-active? FALSE
]
]
tick
ask turtles with [timer-active?] [
set my-timer (my-timer + 1)
]
if (count turtles with [my-variable < 5] = 0) [stop]
end
This way, you will be able to see at any moment what is the current value of my-timer for each turtle.

Get color of global variables with netlogo

I am trying to get the real color of my global variable.
Here my code:
breed [players player]
globals [
INITIAL-POSSESSION ;
]
to setup
clear-all
reset-ticks
set-initial-possession
end
to go
ticks
ask players [
decision
]
end
to-report initial-possession
report random 2
end
to set-initial-possession
ifelse initial-possession = 1
[
set INITIAL-POSSESSION black]
[
set INITIAL-POSSESSION white]
end
to decision
if ([color] of INITIAL-POSSESSION) = black
[] ;;do something
if ([color] of INITIAL-POSSESSION) = white
[];;do something
end
But I get this error:
OF expected input to be a turtle agentset or link agentset or turtle
or link but got the number 0 instead.
So I change it with (and it works):
to decision
if INITIAL-POSSESSION = 0
[]
if INITIAL-POSSESSION = 9.9
[]
end
But there is any other way to do that? (I am using netlogo 6.0)
I think there may be some code missing so I can't confirm, but it looks like you may not have set BALL-OWNER up as a turtle or patch, and instead assigned a value directly to that variable. of queries a variable from an agent (or a list of variables from an agentset), so if BALL-OWNER is set to a value, NetLogo gets confused. If you do assign an agent to BALL-OWNER, however, your approach should work fine. For example, try running the code below:
to setup
ca
crt 10 [
setxy random-xcor random-ycor
set color one-of [ red blue ]
]
reset-ticks
end
to go
let ball-owner one-of turtles
ifelse [color] of ball-owner = red [
print "red team has possession"
] [
print "blue team has possession"
]
end
Edit: You can definitely use a global to pick a color just as you did in your second code block- I just wanted to point out that of is specifically tied to agents. If you want to store a color in a global variable for comparison, that's possible, it's just that your comparison is simpler than using of:
globals [ initial-possession ]
to setup
ca
crt 3
set-initial-possession
reset-ticks
end
to go
ask turtles [
decision
]
end
to set-initial-possession
set initial-possession ifelse-value ( random 2 = 1 ) [black] [white]
end
to decision
ifelse initial-possession = black [
print "I see that black has possession"
] [
print "I see that white has possession"
]
end
I'm not sure if that helps, it may depend on your purpose of storing a color in the global in the first place!

Netlogo: How to compare the ID and then flag in this case?

I would like to put a flag called "min-id" for the turtle with the smallest ID. And I want to flag other turtles as "not-min-id". However the following sample syntax has errors. The error message is as follows.
" error while turtle 0 running >
called by procedure GO
called by button 'go' "
I probably to need your advice. Thank you.
globals [ min-id not-min-id count-up ID ]
to go
reset-ticks
ask patch 0 0
[
sprout 1 ;;This model needs to use sprout.
]
ask (turtles-on patch 0 0)
[
set ID who
setxy min-pxcor 0
set heading 90
]
if (count turtles > 0)
[
ask min-one-of turtles [who]
[
set min-id TRUE
]
]
if (count turtles > 0)
[
ask (turtles-on patch 0 0)
[
if ID > min-one-of turtles [who] ;;This syntax has errors.
[
set not-min-id TRUE
]
]
]
ask (turtles-on patch 0 0) with [not-min-id]
[
set count-up count-up + 1
]
if (count turtles > 0) [
ask (turtles-on patch 0 0) with [min-id]
[
die
]
tick
end
You have some confusion in your code. From your description, I believe you want each turtle to have a flag for whether or not it has the minimum who number. This means you need a flag for each turtle. However, you have set up min-id as a global variable instead of a turtle variable. Furthermore, you only need the flag variable once (that is, you need min-id but not not-min-id) and you set it to TRUE or FALSE.
Replace
globals [ min-id not-min-id count-up ID ]
with
globals [ count-up ID ]
turtles-own [ min-id ]
and see if that fixes it. Also initialise min-id to FALSE as part of the sprout.
Having said all that, I strongly agree with Alan, if you ever use the who variable for anything except print statements in debug, you probably need to rethink your code. In your case, what is special about the turtle with the lowest who number that makes you want to keep track of it? Do you simply want a random turtle that happens to be at a particular location? Then select a random turtle at that location to do the TRUE/FALSE without going through who.

n-of or one-of turtles is not working as expected when working with turtles individually

I have 200 turtles which represent producers. Each turtle, has 2 variables (sales of 2 prodcuts) and they are compared to let the prodcuer know which product was the most popular, so the turtle's color changes to the color representing that product.
I defined this loop :
while [counter < 201][
ask n-of 1 producers [
if (product1sales > product2sales)
[
set color green
]
if (product2sales > product1sales)
[
set color red
]
;
set counter counter + 1]
]
I assumed each time n-of is called, one turtle is chosen, but this specific turtle will not be chosen again. I also assumed that by running the code, all the turtles must be either green or red, but some are neither green nor red since n-of is not working as I assumed. one-of is not doing what I want either. Any ideas?
Thanks
If you just ask producers it will ask all of the producers in a random order, but without repeating- if you just want all of them to make a choice between the two choices you wouldn't even need the counter.
If either product1sales or product2salesis always higher, you could also trim a little by using ifelse. See if the example below would give you what you need:
breed [ producers producer ]
producers-own [ product1sales product2sales ]
to setup
ca
reset-ticks
create-producers 200 [
setxy random 30 - 15 random 30 - 15
set product1sales random-float 11
set product2sales random-float 11
set color white
]
end
to choose
ask producers [
ifelse product1sales > product2sales [
set color green
]
[
set color red
]
]
end

Netlogo: ask turtle to reproduce after it completes a procedure, and ask new turtle to repeat that procedure for itself

I'm new to NetLogo and am attempting to model home range selection of subsequent colonizers. The model should follow simple steps:
Individual 1 picks a home range (a subset of patches).
When individual 1 is done picking its home range, it hatches new
individual 2.
Individual 2 picks a home range, then hatches individual 3.
Individual 3 picks a home range, and so on.
I'm having trouble figuring out how to get this to work. I can get the first turtle to pick a home range. But the offspring do not. Writing the code numerous ways has only accomplished two unintended outcomes. Either endless new individuals are hatched simultaneously, before the first turtle has a home range, and the new turtles fail to pick a home range. Or, the first turtle picks its home range and hatches a new turtle, but that new turtle doesn't pick a home range. Neither outcome is what I want.
How do I set this up to run as intended, so that hatchlings pick home ranges too? Here is one simplified version of my code:
to setup-turtles
crt 1
[setxy random-xcor random-ycor]
end
to go
ask turtles [pick-homerange]
tick
end
to pick-homerange
while [food-mine < food-required] ;; if not enough food, keep picking patches for home range
[;; code to pick home range until it has enough food; this is working okay
]
[;; when enough food, stop picking home range
hatch 1 fd 20 ;; now hatch 1, move new turtle slightly away
]
end
So it is at this last part, once the home range is built, that I want a new turtle to hatch from its parent. I then want that turtle to repeat the pick-homerange procedure. How could that be coded to happen? I've tried writing this every way I can think of; nothing is working. Thanks in advance for any assistance!
One way to do this is to have each patch equal one "food value", and have turtles grow their home range until their home range supplies them with enough food. I would set this up so that patches "know" to which turtle they belong, and so that turtles know how much food they need, which patches are part of their home range, and the food supplied by their homerange. Example patch and turtle variables would then be:
patches-own [
owned_by
]
turtles-own [
food_required
my_homerange
homerange_food
]
Then, your turtles can add patches into their home range until they hit their "food_required", whatever you set that as. For simplicity, in this example I assume that turtles are territorial and so won't "share" home ranges. Further explanation of steps is commented in the code below. This is intended just to get you started- for example, it will hang if you run pick-homerange too many times.
to setup-turtles
crt 1 [
set size 1.5
setxy random-xcor random-ycor
set food_required 5 + random 5
set homerange_food 0
set my_homerange []
]
end
to pick-homerange
ask turtles [
;; Check if the current patch is owned by anyone other than myself
if ( [owned_by] of patch-here != self ) and ( [owned_by] of patch-here != nobody ) [
;; if it is owned by someone else, move to a new patch that is not owned
let target one-of patches in-radius 10 with [ owned_by = nobody ]
if target != nobody [
move-to target
]
]
;; Now add the current patch into my homerange
ask patch-here [
set owned_by myself
]
set my_homerange patches with [ owned_by = myself ]
;; calculate the number of patches currently in my homerange
set homerange_food count patches with [owned_by = myself]
;; Now grow the homerange until there are enough patches in the homerange
;; to fulfill the "food_required" variable
while [ homerange_food < food_required ] [
let expander one-of my_homerange with [ any? neighbors with [ owned_by = nobody ] ]
if expander != nobody [
ask expander [
let expand_to one-of neighbors4 with [ owned_by = nobody ]
if expand_to != nobody[
ask expand_to [
set owned_by [owned_by] of myself
]
]
]
]
;; Reassess homerange food worth
set my_homerange patches with [ owned_by = myself ]
set homerange_food count patches with [owned_by = myself]
]
ask my_homerange [
set pcolor [color] of myself - 2
]
;; Now that my homerange has been defined, I will hatch a new turtle
hatch 1 [
set color ([color] of myself + random 4 - 2)
]
]
end