How can I count dead turtles in Netlogo - netlogo

I would like to know the numbers of all turtles died in my pseudo model. How can I do that? I would appreciate very much a simply and fast running solution for that problem, like count dead turtles.
I was thinking of a routine like this (but do not know how to implement it):
if turtle is dead % checking all turtles if dead or alive
set death_count death_count + 1 % set counter
tick % go one step ahead in model
This is my code sample (without any check so far):
breed [ humans human ]
humans-own [ age ]
to setup
ca
create-humans(random 100)
[
setxy random-xcor random-ycor
set age (random 51)
]
reset-ticks
end
to death
ask humans [
if floor (ticks mod 1) = 0 [
set age age + 1 ]
if age > 85 [ die ]
]
end
to go
death
tick
if ticks > 20 [ stop ]
end

I'm afraid you have to keep track of it yourself in a global variable. So, add
globals [ number-dead ]
to the top of your model. Then, change death like so:
to death
ask humans [
if floor (ticks mod 1) = 0 [
set age age + 1 ]
if age > 85 [
set number-dead number-dead + 1
die
]
]
end
Then number-dead will always be equal to the number of turtles that have died.

This is really simple:
to setup
let total-population count turtles
end
to go
let current-population count turtles
let dead-people total-population - current-population
ticks
end

Related

Netlogo: How to make a turtle move towards an unique patch target?

I have turtles (patients), and they can only use only one bed each (white patch). Since patients are randomly generated in a waiting room (green patches), sometimes two or more of them get at the same distance and therefore they find the same patch as its target. I tried to add an attribute to the patch with the purpose of assigning that particular bed to a specific patient. The idea is something like this (please indulge on the ugly code, I'm learning :P):
globals [
waitxmax
waitxmin
waitymax
waitymin
box
]
breed [ patients patient ]
patients-own [ target ]
patches-own [ assigned ]
to setup-wait-room
set waitxmax -15
set waitxmin 15
set waitymax 11
set waitymin 15
ask patches with [
pxcor >= waitxmax and
pxcor <= waitxmin and
pycor >= waitymax and
pycor <= waitymin
] [ set pcolor green ]
end
to setup-beds
let cmy 7
let cmx 15
let dst 3
let nbox 7
ask patch cmx cmy [ set pcolor white ]
let i 1
while [ i < nbox ] [
ask patch (cmx - dst) cmy [ set pcolor white ]
set i i + 1
set cmx cmx - dst
]
ask patches with [ pcolor = white ] [ set assigned false ]
set box patches with [ pcolor = white ]
end
to setup-patients
create-patients start-patients [
set shape "person"
set target nobody
move-to one-of patches with [ pcolor = green ] ]
end
to setup [
clear-all
setup-wait-room
setup-beds
reset-ticks
]
to go
ask patients [ go-to-bed ]
tick
end
to go-to-bed
let _p box with [ self != [ patch-here ] of myself ]
if target = nobody [
set target min-one-of _p [ distance myself ]
ask target [ set assigned myself ]
]
;;; FIXME
if ([ assigned ] of target) != self [ show "not true" ]
if target != nobody [
face target
fd 1
]
end
When I print the two sides of the comparison below FIXME, from the command center I actually get the expected result. For example: both patient 0 and patient 1 have the same target (patch -3 7), but that patch is assigned to (patient 0). I would have expected that comparison to force patient 1 to get a new target since the bed doesn't have his name (I haven't written that code yet), but it always evaluates to true. This is more notorious as more patients I add over available beds (if no beds available, they should wait as soon as one gets free).
When inspecting trough the interface I also see that the patch -3 7 says (patient 0), so I don't know what's happening. Command center example:
observer> show [ assigned ] of patch -3 7
observer: (patient 0)
observer> if ([ assigned ] of patch -3 7) = [self] of patient 0 [ show "true" ]
observer: "true"
observer> if ([ assigned ] of patch -3 7) = [self] of patient 1 [ show "true" ]
;;;; SETUP AND GO
(patient 0): (patch -3 7)
(patient 0): (patient 0)
(patient 0): "true"
(patient 2): (patch 12 7)
(patient 2): (patient 2)
(patient 2): "true"
(patient 1): (patch -3 7)
(patient 1): (patient 1)
(patient 1): "true"
Maybe I'm just overthinking this and there are is a simpler way to assign a bed to a patient and vice versa?
There seems to be a chunk or two missing from your code above (I can't copy-paste and run it), so please have a look at the option below.
This version works by having a single place to store the 'claimed' beds- in the turtle variable. Since the turtle variables can be queried as a list using of, a bed-less turtle can check if there are any beds that are not already present in that list and, if so, claim one.
turtles-own [ owned-bed ]
to setup
ca
ask n-of 5 patches [
set pcolor green
]
crt 10 [
set owned-bed nobody
claim-unclaimed-bed
if owned-bed != nobody [
print word "I own the bed " owned-bed
]
]
reset-ticks
end
to claim-unclaimed-bed
; If I have no bed
if owned-bed = nobody [
; Pull the current owned beds for comparison
let all-owned-beds [owned-bed] of turtles
; Pull those beds that are green AND are not found in 'all-owned-beds'
let available-beds patches with [
pcolor = green and not member? self all-owned-beds
]
; If there are any beds available, claim one
ifelse any? available-beds [
set owned-bed one-of available-beds
] [
; If there are none available, print so
print "There are no available beds."
]
]
end
Edit: Forgot the actual question title- to actually move to their owned-bed (if they have one) in the example above, they simply face it and move how you like- for example:
to go
ask turtles with [ owned-bed != nobody ] [
ifelse distance owned-bed > 1 [
face owned-bed
fd 1
] [
move-to owned-bed
]
]
tick
end
Edit: added complexity
Ok, for an added element of severity, you will likely want to avoid using multiple with [ ... = x statements, and instead to use a primitive called min-one-of which returns the agent with the minimum value of some reporter. Then, you want to tell NetLogo to keep asking the next most severe and the next most severe, etc. One way to do this is with a while loop, which basically says "While this condition is met, continue evaluating the following code." Be careful with while loops- if you forget to write your code such that eventually the condition is no longer true, the while loop will just continue running until you will eventually Tools > Halt your model (or, as has happened to me with a large model, NetLogo crashes).
I've reworked the code (much of what was above is unchanged) to include such a while loop. Note as well that, since the model needs to check which beds are available multiple times, I've made that code into a to-report chunk to condense / simplify.
turtles-own [ owned-bed severity ]
to setup
ca
ask n-of 5 patches [
set pcolor green
]
crt 10 [
set owned-bed nobody
set severity random-float 10
]
let current-available-beds report-available-beds
while [any? current-available-beds] [
; From the turtles with no bed, ask the one with the lowest severity number to
; claim an unclaimed bed. Then, reset the current-available-beds
ask min-one-of ( turtles with [owned-bed = nobody] ) [ severity ] [
claim-unclaimed-bed
if owned-bed != nobody [
show ( word "I have a severity of " severity " so I am claiming the bed " owned-bed )
]
]
set current-available-beds report-available-beds
]
reset-ticks
end
to-report report-available-beds
let all-owned-beds [owned-bed] of turtles
report patches with [
pcolor = green and not member? self all-owned-beds
]
end
to claim-unclaimed-bed
; If I have no bed
if owned-bed = nobody [
let available-beds report-available-beds
; If there are any beds available, claim one
ifelse any? available-beds [
set owned-bed one-of available-beds
] [
; If there are none available, print so
print "There are no available beds."
]
]
end

General questions regarding Netlogo

Im very new to Netlogo and trying to learn the basic. Therefore, I'm trying to extend an example code Netlogo provided. Im trying to make the pollution rate dependent upon the number of people from the Urban Site Pollution example.
Is there also a way to introduce reinforced learning (Q-learning) to improve the simulation?
Sincerly,
Victor
Do I need to create a new function that updates pollution when population increased?
Below is the example code:
breed [ people person ]
breed [ trees tree ]
turtles-own [ health ]
patches-own [
pollution
is-power-plant?
]
to setup
clear-all
set-default-shape people "person"
set-default-shape trees "tree"
ask patches [
set pollution 0
set is-power-plant? false
]
create-power-plants
ask patches [ pollute ]
create-people initial-population [
set color black
setxy random-pxcor random-pycor
set health 5
]
reset-ticks
end
to go
if not any? people [ stop ]
ask people [
wander
reproduce
maybe-plant
eat-pollution
maybe-die
]
diffuse pollution 0.8
ask patches [ pollute ]
ask trees [
cleanup
maybe-die
]
tick
end
to create-power-plants
ask n-of power-plants patches [
set is-power-plant? true
]
end
to pollute ;; patch procedure
if is-power-plant? [
set pcolor red
set pollution polluting-rate
]
set pcolor scale-color red (pollution - .1) 5 0
end
to cleanup ;; tree procedure
set pcolor green + 3
set pollution max (list 0 (pollution - 1))
ask neighbors [
set pollution max (list 0 (pollution - .5))
]
set health health - 0.1
end
to wander ;; person procedure
rt random-float 50
lt random-float 50
fd 1
set health health - 0.1
end
to reproduce ;; person procedure
if health > 4 and random-float 1 < birth-rate [
hatch-people 1 [
set health 5
]
]
end
to maybe-plant ;; person procedure
if random-float 1 < planting-rate [
hatch-trees 1 [
set health 5
set color green
]
]
end
to eat-pollution ;; person procedure
if pollution > 0.5 [
set health (health - (pollution / 10))
]
end
to maybe-die ;; die if you run out of health
if health <= 0 [ die ]
end
; Copyright 2007 Uri Wilensky.
; See Info tab for full copyright and license.

How to set a probability of death for each tick

I want to set my code so that each forager (a breed of turtle) will have a 10% chance of dying for each tick it is vulnerable. I am building off of a code called Ants in the Netlogo models library.
When I use use [if random 100 > 98 [ die ]] (or anything below 98) nearly all of my turtles will die at the beginning and survive more after a hundred or so ticks have passed. However if I use [if random 100 > 98 [ die ]] no turtles will die. It's very weird.
to go ;; forever button
ask foragers
[ if who >= ticks [ stop ]
ifelse color = red
[ look-for-food ]
[ return-to-nest ]
check-death
wiggle
fd 1 ]
to check-death
ask foragers [
if vulnerable?
[if random 100 > 99
[ die ]]]
end
I expected [if random 100 > 98 [ die ]] to make it so that a vulnerable turtle would only have a 2% chance of dying per tick rather than an immediate wipeout.
The issue here is due to nested ask statements. You have an ask foragers [ ... statement that contains check-death, which contains another ask foragers statement. So, every single forager will be asking all foragers (including itself) to check-death. So, if you have 10 foragers, each forager will be running check-death 10 times per tick.
You should just be able to remove the ask foragers block from within your check-death procedure to solve your issue- have a look at this toy model example:
turtles-own [ vulnerable? ]
to setup
ca
crt 100 [ set vulnerable? one-of [ true false ] ]
reset-ticks
end
to go
ask turtles [
rt random 61 - 30
fd 1
check-death
]
if not any? turtles with [ vulnerable? ] [
print count turtles
stop
]
tick
end
to check-death
if vulnerable? and random-float 1 > 0.90 [
die
]
end
That will randomly assign vulnerable? to the turtles, then have vulnerable turtles die if a randomly generated float value is greater than 0.90 (for the 10% chance mentioned in your question). Once there are no more turtles left, the model will stop.

How can I get turtles to breed once during a defined breeding season?

In my model I have males and females. They can breed with each other to produce offspring at a specific tick every 365th day.
How can I get the adults to turn off the ability to breed once they reproduce but regain the ability the following breeding season.
ask females [
if age > 0 and age mod 365 = 0 [
reproduce
]
.
.
.
to reproduce
if count mates > 0 [ ; the number of males in a defined radius
hatch fecundity [
set mother myself
set father one-of [mates] of mother
]
One way to create a variable that counts the number of days since they last bred. Then increment that variable each tick. Then reset it once the female successfully reproduces. Something like (not tested):
females-own [days-since-child]
to go
...
ask females [ set days-since-child days-since-child + 1 ]
ask females with [days-since-child >= 365] [ reproduce ]
tick
end
to reproduce
if any? mates > 0 [ ; the number of males in a defined radius
set days-since-child 0
hatch fecundity [
set mother myself
set father one-of [mates] of mother
]
]
end

Controlling lives of turtles in NetLogo

For a project, I'm developping a simulation in NetLogo dealing with rabies diseases in dogs and humans. I have some turtles-humans with dogs that can be vaccinated or not. At the beginning I create a dog with rabie and, in according to the fase (1 or 2) of the disease, it can spread the disease to other dogs with a probability. At the end the dog can die either for paralysis (if a probability is higher than 75%) or for other complications. Here's the code:
http://pastebin.com/esR75G3T
In the end you can see that a dog not dying for paralysis will die after some days (between 4 or 6). In other words when the days_infected are equal to end-life.
To check if everything is ok at the beginning I tried to set that NONE of the dog is vaccinated so everyone is supposed to get the disease. In fact when the dog is in phase 2 it will bite anyone. The problem is that if I delete the last line of the code, everything works and some dogs die of paralysis and the other remain alive. If I enable also the last line to let the other dogs die too, nothing works...no dog is infected. why?
This is not a problem with your code: this is a problem with the dynamics of your model. What's happening is that your initial sick dog dies before actually infecting another dog. This is why removing the if (days_infected = end-life) [die] "fixes" the problem.
When I tried your model with a huge population (e.g., 5000 people) so that encounters are more frequent, the infection does spread. You could also increase the probability of infection, or increase the duration of the "furious" phase, I guess.
Another unrelated suggestion, if I may: you should have distinct persons and dogs breeds. Trying to cram everything inside regular turtles makes your code much more complicated than it should be. The way I would approach this would be to create a link from the person to her dog, and then use tie so that the dog is automatically moved when you move the person.
Edit:
OK, here is a version of your code slightly modified to use breeds:
globals [
total_dogs_infected
total_dogs
dead_humans
dead_dogs
]
breed [ persons person ]
persons-own [
sick?
]
breed [ dogs dog ]
dogs-own [
sick?
vaccinated?
rabies_phase
days_infected
end-incubator
end-furious
end-life
]
to setup
clear-all
initialize-globals
setup-turtles
reset-ticks
end
to initialize-globals
set dead_humans 0
set dead_dogs 0
set total_dogs_infected 0
end
to setup-turtles
set-default-shape persons "person"
set-default-shape dogs "wolf"
create-persons people [
setxy random-xcor random-ycor
set size 1.5
set sick? false
ifelse random 100 < 43 [
set color green
hatch-dogs 1 [
set color brown
set heading 115 fd 1
create-link-from myself [ tie ]
set days_infected 0
set vaccinated? (random 100 > %_not_vaccinated)
if not vaccinated? [ set color orange ]
]
]
[
set color blue ;umano sano senza cane
]
]
set total_dogs count dogs
ask one-of dogs [ get_sick ]
end
to get_sick
set sick? true
set color white
set rabies_phase 1
set end-incubator 14 + random 57
set end-furious (end-incubator + random 5)
set end-life (end-furious + 4 + random 2)
set total_dogs_infected total_dogs_infected + 1
end
to go
move
infect
get-older-sick-dog
tick
end
to move
ask persons [
rt random 180
lt random 180
fd 1
]
end
to infect
ask dogs with [ sick? ] [
if (rabies_phase = 1 and (random 100) <= 2) or rabies_phase = 2 [
ask other dogs-here with [ not sick? and not vaccinated? ] [ get_sick ]
]
]
end
to get-older-sick-dog
ask dogs with [ sick? ] [
set days_infected days_infected + 1
;the incubator phase ends after at least 14 days + random(57) and then we have phase 2 (furious)
if (days_infected = end-incubator) [ set rabies_phase 2 ]
;when the main furious phase finishes we have 75% of probability that a secondary furious phase continues for other 4 - 6 days until death ;or we have a probability of 25% that the disease end in paralysis with a fast death
if (days_infected = end-furious and (random 100 > 75)) [
set dead_dogs dead_dogs + 1
die
]
if (days_infected = end-life) [
die
]
]
end
; These last reporters are not used,
; they just illustrate how to get the
; dog from the owner or vice-versa:
to-report my-dog ; person reporter
report one-of out-link-neighbors
end
to-report has-dog? ; person reporter
report any? out-link-neighbors
end
to-report my-owner ; dog reporter
report one-of in-link-neighbors
end
Not only does it simplify some expressions (e.g., ask dogs with [ sick? ] instead of ask turtles with [ has_dog? and sick_dog? ]), it opens up all sorts of possibilities: a dog could run away from its owner, the owner could die without the dog dying, an owner could have two dogs, etc.