Setting Age on Turtles - netlogo

I have my turtles own age as one of their variables and I have age set to ticks so that my turtles age whenever they reach a certain number of ticks. However, this causes all turtles to age at the same time (say when ticks = 5), regardless of when they've been created.
Is there any way to get the age to start when the turtle is created? So if the turtle would be created at tick 5, its age starts at zero, but is still equal to the same length as 1 tick?
Yes! Sorry! Here is the code I've been playing with before I put it into my actual model
breed [kids kid]
breed [adults adult]
breed [elderlies elderly]
turtles-own [age
z]
to setup
clear-all
create-kids 10
[setxy random-xcor random-ycor]
set-default-shape kids "fish"
create-adults 10
[setxy random-xcor random-ycor]
set-default-shape adults "person"
set-default-shape elderlies "cow"
clear-output
reset-ticks
end
to go
ask kids [birthday
move]
ask adults [birthday
reproduce-adults
move]
tick
end
to birthday
set age ticks
if age > 5 [set breed adults]
if age > 10 [set breed elderlies]
if age > 15 [die]
end
to reproduce-adults
set z random 100
if z > 65
[ hatch-kids 1]
end
to move
rt random 360
fd 1
end

In your go procedure, include ask turtles [increment-age], where
to increment-age
set age (1 + age)
end

Alan's solution is good if you want age to be equal to the number of ticks since a turtle was created. You can divide that number by another number if you want a different measure of age. For example, if ticks represent months, you can divide by 12 to get years.
Another method involves adding birth-tick--the tick on which the turtle was "born"--as a turtles-own variable. Suppose that you want to increment the age every 5 ticks. Then you could write increment-age like this:
to increment-age
let age-in-ticks birth-tick - ticks
if (age-in-ticks > 0) and (age-in-ticks mod 5 = 0) [
set age (1 + age)
]
end
The expression containing mod tests whether the number of ticks since birth is evenly divisible by 5.
EDIT: I should clarify that you'll need to set birth-tick to the value of ticks at the time that each turtle is created.

Related

Using link-own variable to transfer resources between specific turtles

Say we have two turtles with a link between them, and that link owns a variable that represents the age of the link, "tenure". For turtle 1 to send resources to turtle 2, there is a conversion cost related to the age of the link between them. How do I refer to the tenure value of the specific link between these two turtles?
In context, I have turtles linked into "households" and as they start each day they assess how much energy they have. If they have more than some threshold of energy, they share the surplus with fellow housemates. To elect how they will share the energy, I have them looping through each housemate to determine who has low health. Surplus energy can be transferred to a housemate, but the conversion rate depends upon the tenure of the link between them. A longer tenure results in a better conversion rate of energy into health. But I don't know how to refer to the specific value of the linked-owned variable between each turtle and each of their housemates.
Right now, I have this approach of the shared-link being something like [link = [link] of myself] but that doesn't work:
turtles-own [energy health age]
links-own [tenure]
to go
tick
ask links
[
set tenure tenure + 1]
ask turtles
[
ifelse energy > 3 [supply] [rest]]
end
to supply
let surplus energy - 3
loop
[ask one-of link-neighbors
[
let shared-link link = [link] of myself
if health < 3 [set health health + 1 * [log [tenure] of shared-link]
set surplus surplus-1]
]]
end
Try running this snippet. it illustrates what I think you need.
links-own [ weight ]
to setup
clear-all
random-seed 12345
create-turtles 4 [setxy random-xcor random-ycor set label who set size 3]
ask turtles [ create-links-with other turtles [ set weight random 100 set label weight ]]
ask turtle 1 [ ask link-with turtle 2 [ set color red set thickness 0.5 ]]
ask turtle 1 [
let myweight [weight ] of link-with turtle 2
print ( word "Turtle 1 says, my link weight with turtle 2 is " myweight )
]
ask turtles [ ask link-neighbors [
let myweight [weight ] of link-with myself
print ( word myself " says, my link weight with " self " is " myweight )
]]
reset-ticks
end

How to combine turtle_owned variables up to a maximum number of turtles?

*EDIT - The issue has been resolved by updating my Netlogo version from 6.0.3 to 6.1.0 to use primitive "up-to-n-of" *
In my Netlogo program, turtles (namely carbs) own a variable called mass. The setup procedure creates 10 turtles with variable mass from 1 to 3. The to-go procedure asks BIG turtles to find other turtle (target) which mass is less than 2, obtain the mass of the "target", and kill the target. At first, using primitive "up-to-n-of" gave me error in the older Netlogo version. Now, I am using the primitive "up-to-n-of" and "other" to avoid 2 turtles killing the same "target" in a newer Netlogo version.
For example, my model set ups random:
My previous model used primitive "one-of" and in one tick obtained,
Now, my model identifies 2 or more carbs with mass < 2 per tick.
For example, if carbs can identify 2 other carbs with the same set up the results would be:
Turtles
Carbs_mass
Carb_0 kills carb_9 and carb_8
gets carbs_mass 5
Carb_1 kills carb_7 and carb_6
gets carbs_mass 5
Carb_2
keeps carbs_mass 1
Carb_3
keeps carbs_mass 1
Carb_4
keeps carbs_mass 1
Carb_5
keeps carbs_mass 1
Total mass
14
Any comments or suggestions on this issue?
Thanks a lot in advance.
Breed [carbs carb]
carbs-own [carbs_mass]
Globals
[time]
to setup
clear-all
set time 0
;;;CARBS;;;
set-default-shape carbs "circle"
Create-carbs (10)
[
set color white
set size 1.5
set carbs_mass (1 + random-float 2)
setxy random-xcor random-ycor
]
reset-ticks
end
to go
if not any? turtles [stop]
ask carbs
[combine_carbs]
set time time + 1
tick
if (time = 1) [stop]
end
to combine_carbs
if carbs_mass > 2
[
let target up-to-n-of 2 other carbs with
[carbs_mass < 2]
if target != nobody
[set carbs_mass carbs_mass + sum ([carbs_mass] of target)
ask target [die]
]
]
end
To use up-to-n-of, you'll need to be using NetLogo 6.1 or higher. It doesn't exist in 6.0.

How to find the turtle with the most neighbors in netlogo

i am struggling with a way to find the turtle with the most neigbors in a radius of 3 and change its color. At the moment i tried something with a while loop that increases an the id of the turtles and tests if the number of neighbors is higher than the last one. But It's causing anf infinite loop and travels only between the id 0 and 1.
I can't seem to find where the error comes from, Here's the code i wrote:
to election
while [var1 < 9][
ask turtle var1 [
set voisin count turtles in-radius 3
if voisin > maxi [
set maxi voisin
set idmax var1
]
show voisin
show idmax
set var1 var1 + 1
set color pink
]
]
ask turtle idmax[
set color green
]
end
You can do this with a single statement,
ask max-one-of turtles [count other turtles in-radius 3] [set color green]
max-one-of finds the turtle with the maximum value of the expression in brackets. (If there are multiple turtles with that maximum value, then a random one of those with the maximum value is chosen.) In the brackets, each turtle evaluates the number of other turtles in radius 3. The other is not strictly necessary. Without it, every turtle will count itself, adding one to the count.

How to create a certain number of regions with a given patch-size in NetLogo?

I need to create a number of regions which have a given patch-size. The total number of regions is 100.
I start with these data:
A list whose components are the number of regions with the same size (the sum of the components must be 100) and another list whose components are the patch-size of these regions. Therefore, if L1 = (n1, n2, n3) and L2 = (s1, s2, s3), it means that n1 regions have a patch-size of s1, and so on.
So I start creating a list with 100 components, where the first n3 components have a value equal to s3, the following n2 components have a value of s2, and the following n1 components have a value of s1.
to setup-world
let plot-distribution (list 9 20 25 15 11 5 5 5 4) ;sum = 100
let plot-patches (list 1 3 7 15 29 49 77 141 392)
let plot-list (list)
(foreach plot-distribution plot-patches [
let aux ?2
repeat ?1 [set plot-list (fput aux plot-list)]
])
...
end
Then I start a foreach loop, to create each region I need. I start asking one random patch to be (let's say) green. Then, I define its "neighborhood" as the patch-set of neighbor patches with color black. If the number of patches in neighborhood is greater than the patches I need to color, then I color all the neighborhood; if not, I just net to color a smaller number of patches. Afterwards I redefine the new "neighborhood" as black patches of the old "neighborhood", to keep processing the loop.
to setup-world
...
foreach plot-list
[
let counter 1 ;;;;number of patches colored
let max_counter ? ;;;;number of patches of the region
let colorete one-of base-colors ;color
let neighborhood 0 ;;;;start a variable for the patch-set
;;;; Set one random patch with a random color and set a region of
;;;; its neighbours which have black color
ask one-of patches with [pcolor = black]
[
set pcolor colorete
set neighborhood (patch-set neighbors with [pcolor = black])
]
;;;; Start the loop to complete the region
while [counter < max_counter ] ;;;;while the amount of colored patches is smaller than the total size of the region
[
let patch-number (count neighborhood)
;;;; If the number of patches I have to color is greater than the number of patches of "neighbourhood", then color all the patches. Otherwise, ask a smaller group of patches in neighborhood, since it has more patches than those I need to color.
ifelse ( (max_counter - counter) > patch-number )
[ask neighborhood [
set pcolor colorete
set counter (counter + patch-number)
set neighborhood (patch-set (neighbors with [pcolor = black]))
]
][
ask n-of (max_counter - counter) neighborhood [set pcolor colorete]
set counter (max_counter)
]
]
]
...
end
However, it cannot complete the loop; it gets stuck, and I don't know why. I have tried creating instructions more simpler (and somehow, repetitive), just to be more "clear", but it's useless.
Can you help me? Thank you!

NetLogo: Changing one breed's variable depending on other breed's variable in an ego-centric network environment

Dear Stackoverflow users,
I am a newbie to NetLogo and the community here, so I hope I can express myself adequately. If you need more information in order to understand my question, please, let me know. As I am not completely sure, where my problem lies, my title might even be misleading.
Here is what I am trying to do: I want an ego-centric network model, in which 1 ego (a Latino immigrant in the US) starts with a given value (between 1 and 6) for
identification with Latino culture and
identification with US/White culture.
The ego (breed #1) has 8 alters (breed #2). The alters consist of Latinos and Whites (ratio to be determined by slider in the interface: number-Latinos). The alters are randomly connected between themselves (amount of undirected links to be determined by another slider in the interface: number-of-alter-links). Each alter has a value for degree d (which is the number of links within the same ethnicity).
At each tick, ego is supposed to interact randomly with one of the alters. If the alter is Latino, then ego's initial value for Latino identification should increase by 0.1 + d * 0.1. If the alter is White, ego's initial value for US identification should increase by 0.1 + d * 0.1. The maximum value that can be reached for the identification variables is 6.
Here comes the code:
breed [egos ego]
breed [alters alter]
egos-own[identification-US identification-Latino]
alters-own[degree]
to setup
clear-all
setup-alters
setup-egos
reset-ticks
end
to setup-alters
create-alters 8
[layout-circle alters 8
if who < number-Latinos [set color orange] ; Latinos are orange
if who >= number-Latinos [set color yellow] ; Whites are yellow
]
while [count links < number-of-alter-links][
let node1 random 8
let node2 random 8
if (node1 != node2)[
ask alter node1 [create-link-with alter node2]
]
]
ask alters [ ; set degree within same ethnicity
ifelse color = yellow
[set degree (count link-neighbors with [color = yellow])]
[set degree (count link-neighbors with [color = orange])]
]
end
to setup-egos
create-egos 1 [
set identification-US initial-US-identification-ego
set identification-Latino initial-Latino-identification-ego]
end
to go
if ticks >= 50 [stop]
interact
change-identification
tick
end
to interact
ask egos [create-link-with one-of alters [set color green]]
end
to change-identification
ask links with [color = green] [let d [degree] of end1
ask egos [
ifelse link-neighbors = yellow
[ifelse (identification-US < 6)
[set identification-US identification-US + 0.1 + d * 0.1]
[set identification-US 6]
]
[ifelse (identification-Latino < 6)
[set identification-Latino identification-Latino + 0.1 + d * 0.1]
[set identification-Latino 6]
]
]
]
ask egos [ask my-links [die]]
end
This is my problem: When I am running the simulation, only the value for Latino identification changes, but not the one for US identification. This is even true, when there are no Latinos in the network. I am not sure where the problem lies. Is it in the nested ifelse command? I have tried to work my way around the nested ifelse and made several if commands, but the problem remains. Does it have to do with how I defined the two ethnicities with colors? Also, when I ask in the command center something about a particular turtle (e.g., turtle 3), I get the answer 9 times (total number of turtles). Maybe the problem is how I ask the link-neighbor(s) for its color?
Thanks for your attention! Any idea, suggestion or possible solution is highly appreciated.
This will always be false: link-neighbors = yellow.
Btw, if you post an entire model like this, you need to replace the interface globals with code-based declaration and initialization of the variables.