how to calculate cumulative average of infected turtles in netlogo - netlogo

im trying to calculate cumulative average of infected turtles in netlogo
i dont know what code exactly i should use and where to put it in the netlogo code
i did some tests and try to calculte the mean of the nodes but thats not correct.
i also tried to create a list but i didnt succed either.
where exactly in the code i should put the lines?
here some code:
observer> show mean [num-infected] of turtles
observer: 198
observer> show mean [num-infected] of turtles / length num-infected
observer> show mean [num-infected] of turtles / length [num-infected] of turtles
observer: 0.99
observer> show sum [num-infected] of turtles / length [num-infected] of turtles
observer: 197
observer> show count [num-infected] of turtles / length [num-infected] of turtles
observer> show mean [num-infected] of turtles / length [num-infected] of turtles
observer: 0.955
observer> show sum [num-infected] of turtles / length [num-infected] of turtles
observer: 191
observer> show sum [mean num-infected] of turtles
observer> show mean [ num-infected] of turtles
observer: 191
observer> show mean [ num-infected] of turtles
observer: 1
observer> show mean [ num-infected] of turtles
observer: 198
observer> show mean [ num-infected] of turtles
observer: 198
observer> show mean [ num-infected] of turtles
observer: 198
observer> show [ num-infected] of turtles
observer> show count turtles with [infected? = true]
observer: 198
observer> show count turtles with [infected? = true] / length [num-infected] of turtles
observer: 0.99
observer> show count num-infected / count turtles with [infected? = true]
ERROR: Expected an agentset here, rather than a list or block.
thanks.

See answer on the netlogo-users group at http://groups.yahoo.com/neo/groups/netlogo-users/conversations/topics/16345
Travis Hinkelman writes:
If I understand you correctly, then your reporter procedure should look like this:
to-report cumul-av-sum
;; drop the first member of the list, but not until there are at least 200 items in the list
if (length cumul-av-list > 200) [ set cumul-av-list but-first
cumul-av-list ]
;; add the number of raindrops created in last tick to the end of the list
set cumul-av-list lput num-infected cumul-av-list
report sum cumul-av-list
end
This reporter procedure needs to be called every tick (presumably as part of a plot) to update the list correctly.

Related

Limiting Population Size while Hatching Additional Turtles

I have a slider that controls population, set to the max value of 100, which creates 100 turtles in the nest. However, the number of turtles within a breed is independent of the total population, so instead of having 100 total turtles, I get 100 + #breed1 + #breed2. Additionally, I am hatching new turtles of breed [followers] and [foragers] during the course of the model. How do I get the turtles to die each time a new breed member is hatched?
I know this is not a code problem, but ideally, I would like the new foragers to be the turtles that are in the nest, not just new turtles.
to setup
clear-all
set-default-shape turtles "bug"
create-turtles population
create-foragers 10
[set color yellow]
end
to go ;; forever button
ask leaders
[wiggle
fd 1
return-to-nest]
ask followers
[if any? leaders
[uphill-chemical
fd 3
pickup-food]
uphill-food
fd 1
if distancexy nest-x nest-y < 3 and color = violet
[hatch-foragers 1
[set color yellow
uphill-chemical]]
tick
end
to return-to-nest ;; turtle procedure
ifelse nest?
[if count followers < 5
[hatch-followers 1 [set color brown - 1]]
facexy food-x food-y ;; drop food and head out again
move-to patch food-x food-y]
[else commands]
end
````````
The easiest way to maintain a population is simply to ask a newly born turtle to kill a randomly selected turtle. So instead of:
[ if count followers < 5
[ hatch-followers 1
[ set color brown - 1
]
]
you could have:
[ if count followers < 5
[ hatch-followers 1
[ set color brown - 1
ask one-of turtles [die]
]
]

Turtles hatch when crossing a line

I want my turtles to hatch (= make one more turtle) when they cross a specific line. I have tried the command ifelse?, and I can get it to work on a simple model, when my turtles randomly wanders: If they move to a patch on the left side (xcor < 0) they die, if they make a move to a patch with xcor > 0 they hatch 1.
But I want the proces to be linked to witch patch they come from. If they stand on a patch with xcor < 0 and moves to another patch with xcor < 0 they shall die - but if they change xcor from negative to positive - they should multiply (= hatch 1).
My problem is: Is it possible to write a code-string that "remembers" the turtles position one tick before and use it to either let the turtle die og multiply?
{
to setup
clear-all
create-turtles 20
ask turtles [set size 2
set heading random 45 ;; turtle is now facing northeast
setxy random-xcor random-ycor
set color white
set shape "person"]
end
to go
ask turtles
[ rt random 360 ; turns in a random direction
fd 4 ;; all turtles move forward one step
rt random 180 ;; ...and turn a random amount
fd 4
lt random 180
]
ask turtles
[ifelse pxcor > 0
[hatch random 2]
[die]]
end }
You can use a turtle variable to give each turtle a memory.
turtles-own
[
old-xcor
]
Just before the turtle moves, assign ‘xcor‘ to that variable.
To move-turtle
Set old-xcor xcor
< Your movement code >
if old-xcor < 0
[ Ifelse xcor < 0
[ Die ]
[ Hatch 1 ]
]
End

Problems creating a x% fraction of turtles using n-of command

I want x% of turtles, called pholders, to change their choice from a good 1 to a good 2.
The code is as follows:
ask pholders [ifelse random-float 1 <= probkauf
[ask (n-of (count pholders with [choice-num = 1] * 0.01) pholders with[choice-num = 1]) [set choice-num 2]]
[ifelse random-float 1 < 0.5[imitation set typeofchoice 1][beratung set typeofchoice 4]]
]
Initially 100% of the pholders chose good 1. The Problem is as follows: When i rise the number of pholders above something between 102 and 108 n-of doesn't calculate a 1%-fraction anymore, it calculates 10%. The higher the number of pholders the bigger the fraction: for 200 pholders the code calculates 60%. When i leave the number of pholders constant and below 108 but change the percentage from 0.01 to 0.02 it calculates something like 55% or 58%. Is the problem probably coming from ask n-of in an ask environment?
Thank you very much in advance.
Your problem is that you are running the probabilistic code multiple times. Your code has this structure:
ask pholders
[ ifelse random-float 1 <= probkauf
[ ask (n-of (count pholders with [choice-num = 1] * 0.01) pholders with [choice-num = 1])
[ set choice-num 2]
]
[ <do something else> ]
]
If you have 500 pholders, then there will be 500 times that a pholder selects a random number and, if the number is lower than your value probkauf, it instructs a number of pholders with choice-num of 1 to change it to choice-num 2. 500 potential occasions of 1% conversion is why you have so many being converted.
Based on the description in your comments, I think you want this:
globals [probkauf]
turtles-own [choice-num]
to setup
clear-all
set probkauf 0.5
create-turtles 1000
[ setxy random-xcor random-ycor
set color blue
set choice-num 1
]
reset-ticks
end
to go
update-choices
tick
end
to update-choices
ifelse random-float 1 < probkauf
[ ask turtles with [choice-num = 1]
[ if random-float 1 < 0.01
[ set choice-num 2
set color red
]
]
]
[ ; whatever happens with other part of probability
]
end

Spider Aggression: Creating A Simple Behavioral Model

UPDATE
Thanks Luke C & JenB. Here's my revised code. I just converted the females to "killer" by turning her territory to a different color (green to blue) if a male wanders into her territory. If any male happens to be on a blue colored patch, he dies. It seems that the male who initially triggers the conversion does not get eaten immediately, which is what I want. He can be eaten if he doesn't get out of her blue territory on the next tick. Then, any subsequent males are eaten immediately.
Also, I am exploring some cool things with the BehaviorSpace tool, like how many ticks it takes for all the males to be eaten in a number of runs.
The only loose ends are creating a graph window that displays the number of males over time (ticks) and a report window(s) with the number of nonkiller/killer females.
ORIGINAL POST
I would like to model a behavioral system of wolf spiders with the following rules:
Females are stationary
Males move around
If any male encroaches within a certain radius of a female, that female becomes a “killer” two ticks later
Any males within a “killer” female’s territory is eaten and taken out of the population
The simulation ends when all males are eaten
All females are “non-killers” at setup
End goals of the model:
How many ticks does it take for all of the males to be eaten?
Are there any ‘non-killer’ females in the population at the end of the simulation?
Here is what I have so far. The parts I am struggling with are setting up the female territories, converting non-killer females to killer females when a male enters her territory, accounting for/taking out eaten males, reporting the duration of the simulation in ticks and if any non-killer females remain at the end.
breed [females female]
breed [males male]
to setup
clear-all
reset-ticks
ask patches [set pcolor white]
setup-females
setup-males
end
to setup-females
create-females 15 [
set shape "spider"
set color 15
set size 25
setxy random-xcor random-ycor]
ask females [move-to one-of patches with [not any? females in-radius 40]
; prevents overlapping
ask patches in-radius 20 [set pcolor green]
]
end
to setup-males
create-males 30 [
set shape "spider"
set color black
set size 20
setxy random-xcor random-ycor]
ask males [move-to one-of patches with [not any? males in-radius 10]
; prevents overlapping
]
end
to go
move-males
tick
if count males = 0 [stop]
end
to move-males
ask males [
right random 360
if random 100 > 49 [forward 1 + random (15 - 1)]
if pcolor = blue [die]
;moves male 1 to 15 units per tick if condition satisfied
score-females
]
end
to score-females
ask females in-radius 20 [
if count males > 0 [ask patches in-radius 20 [set pcolor blue]]
]
end

How show few turtles at a time?

I have this basic code for setup and moving turtle.
By this time I want only few turtles to appear during setup and then when they move. Other turtles will show or will become visible.
to setup
crt 100
setxy random 19 random 80
end
to go
fd 1
end
I tried this. But I got error
to setup
clear-all
create-turtles random 10
reset-ticks
end
to go
fd 1
if count turtles < 100 [
create-turtles min list (random 10) (100 - count turtles)
]
tick
end
Your question is not that clear, if you want to be able to set visibility of turtles you should use hidden? Primitive to set visibility of turtles,
Following example shows how turtles appear when their who id is smaller than ticks, in tick 101 all turtles will be visible.
to setup
clear-all
reset-ticks
crt 100 [
set hidden? true
setxy random 19 random 80
]
end
to go
ask turtles
[
if who < ticks
[
set hidden? false
fd 1
]
]
ask patch 0 0 [set plabel ticks] ; just for your info
ask patch 1 1 [set plabel "Ticks"] ; just for your info
tick
end
After 1 tick only one turtle is visible:
And now 40 turtles are visible :
Update:
In this example, you can have a list of numbers that you want to ask turtles to set their visibility to true:
globals [ number-to-set-visible]
to setup
clear-all
reset-ticks
set number-to-set-visible [ 5 5 7 8 2 ]
crt 100 [
set hidden? true
setxy random 19 random 80
]
end
to go
if visibility-condition and length number-to-set-visible > 0
[
ask n-of item 0 number-to-set-visible turtles [
set hidden? false
]
set number-to-set-visible remove-item 0 number-to-set-visible
]
ask turtles with [not hidden? ]
[
fd 1
]
tick
end
to-report visibility-condition
report ticks mod 100 = 0 and ticks > 0
end
Marzy's answer covers how to create invisible turtles during setup, then gradually make them visible during go.
It's not clear to me if you actually need this visible/invisible distinction. Do you actually need all the turtles to exist from the beginning? Might it be OK to just create a few more turtles each tick? If so, try code like this:
to setup
clear-all
create-turtles random 10
reset-ticks
end
to go
if count turtles < 100 [
create-turtles min list (random 10) (100 - count turtles)
]
tick
end