I was able to include the recovery or die but now I'm having trouble getting the standard deviation and of the turtles that died. I think I got the standard deviation but can't get the mean
if random-float 1 < recover-or-die [
set epi-state recovered-code
set color green
ifelse random-float 1 < 0.90[
]
]
]
I tried this for the mean and it kept saying "Expected Command"
Your problem is ifelse recovery-prob < 0.1 = true. I don't know what your recovery-prob is set to, but this line is always true or always false. What you probably want to do (and what is in your comment) is:
ask turtles with [epi-state = infectious-code]
[ ifelse random-float 1 < recovery-prob
[ set epi-state recovered-code
set color green
]
[ die
]
]
Note that you don't need to actually have the = true part.
If you have recover-prob set to 0.05 (for example), the condition is true for all turtles and they all recover. If it's set to 0.2 (for example), it is false for all turtles and they all die.
This block still has a logic problem I think. The way you have it written, any infectious turtle will either recover or die immediately. What about the turtles who stay infectious for more than one tick?
Related
I want a little help. I want turtles to cooperate with 50% probability if their land parcels are > 5.
I am writing as
ifelse random 1 = 0 and land > 5
[set cooperate? true]
[set cooperate? false]
But it gives error that; The > operator can only be used on two numbers, two strings, or two agents of the same type, but not on a string and a number.
how to correct it?
Thanks
There's not quite enough information to diagnose the problem. Is this code inside an ask turtles block with the variable 'land' as a turtles-own attribute? Also, you might want to print off some values of 'land' to make sure you actually have numbers in it.
As you can see from the working example below, there is no error in the code you have provided.
turtles-own [ land cooperate? ]
to testme
clear-all
create-turtles 10
[ set land random 10
ifelse random 1 = 0 and land > 5
[ set cooperate? true ]
[ set cooperate? false ]
]
type "Cooperating: " print count turtles with [cooperate?]
type "Not cooperating: " print count turtles with [not cooperate?]
end
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.
I'm trying to have turtles match based on attraction parameters, however I only get either one match or one mismatch and won't count anymore turtles matching, as well as not moving the counter forward.
to new-couple
set countdown2 5
ask turtles [
;; CREATE NEW COUPLE
ifelse countdown2 <= 0
[ die ]
[ set countdown2 countdown2 - 1 ] ]
end
The code that you have provided does not have an error. Try this version of it and you will see that the new-couple code correctly reduces the countdown 5 times and then kills the rest of the turtles. So it's not producing the problem that you describe of things only happening once.
to testme
clear-all
create-turtles 10
type "Start turtles: " print count turtles
new-couple
type "End turtles: " print count turtles
end
to new-couple
let countdown2 5
ask turtles [
;; CREATE NEW COUPLE
ifelse countdown2 <= 0
[ die ]
[ set countdown2 countdown2 - 1 ] ]
end
Could you please provide more of your code. Also, an explanation of what the countdown is supposed to achieve could be useful. At the moment it basically selects the number of turtles that don't die and there are easier ways to achieve that.
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.
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