In NetLogo, suppose there are two kinds (breeds) of turtles: AAA and BBB.
breed [ AAA ]
breed [ BBB ]
AAA-own [ vvv ]
BBB-own [ vvv ]
Suppose I am iterating over AAA, and when an AAA finds a BBB nearby, it steals 10% of vvv from the BBB individual. If there is a global variable called dummy, the following code may work:
to function-name
let QQQ one-of BBB in-radius 1
ask QQQ [
set dummy vvv * 0.1
set vvv vvv - dummy
]
set vvv vvv + dummy
end
Is there any way to do the similar thing without using the global variable, dummy?
Use myself for the turtle who asked.
Related
I have the following doubts:
I have several individuals of 9 turtle codes.
The turtle codes are: R1M1 R2M1 R3M1 R1M2 R2M2 R3M2 R1M3 R2M3 R3M3. And I have 10 individuals of each code.
As the code is structured in a loop sequence and the order that the code exits in the result exported in .csv, I would like to ask the first individual of the first code (R1M1) to print the output header. But, I'm only getting it using one-of, and then the header often doesn't come out in the first line of the output. And I don't know how I can access the first turtle from the R1M1 code. Does anyone have any ideas?
What I thought of is calling turtle 0 which has the code R1M1. But, I still don't know how to do this in NetLogo.
OBS.: I tried to use only turtle 0, but in the other codes, too, there is turtle 0 and then the header appears.
Thanks in advance
let n count turtles with [ profiles-code = "R1M1" ]
if n > 0
[
ask one-of turtles with [ profiles-code = "R1M1" ]
[
prepare-header-output;; CALL A PROCEDURE
]
]
An example of how the header is coming out (has 3 individuals from each of the 9 codes)
I would use a flag / semaphore variable to indicate if a header is needed. You'll need to adapt the below if you have your turtles outputting the data, but hopefully this will get you pointed in the right direction- more details in comments:
globals [ need-headers? ]
turtles-own [ class ]
to setup
ca
random-seed 1
set need-headers? true
if file-exists? "test_output.csv" [ file-delete "test_output.csv" ]
crt 10 [
set class item random 5 "ABCDE"
]
reset-ticks
end
to go
if count turtles < 1 [ stop ]
ask turtles [
if random-float 1 < 0.25 [ die ]
]
; Toy output example
file-open "test_output.csv"
foreach range length "ABCDE" [ i ->
; Output headers if needed
if need-headers? [
file-print "turtle-type, count, ticks"
; Change the need-headers? to false so this step will only happen once
set need-headers? false
]
let ltr item i "ABCDE"
let n-type count turtles with [ class = ltr ]
file-print ( word ltr "," n-type "," ticks )
]
file-close
tick
end
Output here looks something like:
but if I don't change need-headers? to false, it looks like:
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
In netlogo I am simulating a population and I want that individuals between 16 and 50 years old marry randomly with another single individual from the population. Each individual has an household-id and I want that the male individual change its household-id to his "wife" household id, but i don't know how to do it. For now I have this code
ask individuals [
if not married? and sex = "male" and age >= 16 and age <= 50 [
let potential-mate one-of individuals with [
not married? and age >= 16 and age <= 50
and sex = "female" and household-id != household-id
]
if potential-mate != nobody [
; this command do an Bernoulli equation,
; the relation is based on empirical data i have
ifelse random-bernoulli (- 0.765 * ln age + 2.9753) [
stop
] [
set my-mate potential-mate
set married? true
ask my-mate [ set married? true ]
ask my-mate [ set my-mate myself ]
]
]
]
]
Luke C's comment is correct: what you need is myself, as in:
household-id != [ household-id ] of myself
That being said, I would strongly suggest modelling things like marriages as links. Here is a working example:
breed [individuals individual]
individuals-own [age sex household-id]
undirected-link-breed [marriages marriage]
to setup
clear-all
create-individuals 100 [
set age random 100
set sex one-of ["male" "female"]
set household-id random 100
setxy random-xcor random-ycor
]
marry-individuals
reset-ticks
end
to marry-individuals
let bachelors individuals with [ not married? and age >= 16 and age <= 50 ]
ask bachelors with [ sex = "male" ] [
let potential-mates bachelors with [
sex = "female" and household-id != [ household-id ] of myself
]
if any? potential-mates [
if not random-bernoulli (- 0.765 * ln age + 2.9753) [
let mate one-of potential-mates
create-marriage-with mate
set household-id [ household-id ] of mate
]
]
]
end
to-report married? ; individual reporter
report any? my-marriages
end
to-report my-mate ; individual reporter
report [ other-end ] of one-of my-marriages
end
This way, you don't have to manage separate variables for married? and my-mate: a single link tells you all need to know about the relationship between these two individuals. The main advantage is that it's much less error prone: there is no risk for the values of these variables to ever become inconsistent. Notice, also, how the married? and my-mate make these concepts just as easy to access as they were before.
Another couple of comments about your code:
I usually avoid using stop if possible. The behavior of that primitive is not always intuitive, and it sometimes leads to errors.
Notice how I create a temporary bachelors agentset. This avoids checking for the age and married? conditions twice and makes the code more readable.
I don't know what you plan to do with them, but you might want to consider making households agents and representing membership in an household by creating a link to it. Using "id" numbers is not a very netlogoish way of doing things and sometimes lead to inefficient code.
I want to build a model of a city with hospitals. There are people, and people who are employees of specific hospital.
I want the employees to be moving without exceeding a maximum distance from the hospital where they work.
persons-own [
hospital-employees? ; true if work in hospital
hospital-position-cordx ; xcor of the hospital where he works
hospital-position-cordy ; ycor of the hospital where he works
]
to move
; they can move only around the hospital (max distance 5 patch)
ask persons with[hospital-employees?][
...........
]
; other people can move free
ask persons with[not hospital-employees?][
rt random 180
lt random 180
fd 1
]
end
It this possible?
I'm sure there are many ways to approach that problem. Here is a simple one:
breed [hospitals hospital]
breed [employees employee]
employees-own [my-hospital]
to setup
clear-all
set-default-shape hospitals "house"
set-default-shape employees "person"
ask n-of 5 patches [
sprout-hospitals 1 [
hatch-employees 5 [
set my-hospital myself
]
]
]
reset-ticks
end
to go
let max-distance 5
ask employees [
ifelse distance my-hospital > max-distance - 1 [
face my-hospital
] [
rt random 180
lt random 180
]
fd 1
]
end
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.