I am new to Netlogo and am progressing pretty well with programming. However, I am currently stuck on an issue.
I am using turtles as fish super-individuals (see Railsback and Grimm 2005) which means each turtle has arguments for abundance, sex, age and size. At the end of a year cycle, I would like to find turtles with the same characteristics of sex, age and size and then combine their abundances into one turtle of the same characteristics (then all will be die except the newly combined turtle). Does anyone know how to do this? Any advice would be greatly appreciated.
I didn't test my solution, but this should work. Essentially, for each turtle figure out who are the turtles with the same properties and add it to a running total of abundances, then ask them to die.
ask turtles [
let others-abundance 0 ;; accumulate other's abundances
ask other turtles with [ sex = [sex] of myself and abundance = [abundance] of myself and age = [age] of myself] and size = [size] of myself] ;;determine who are the others with same properties
[
set others-abundance other-abundance + abundance
die
]
set abundance abundance + other-abundance
]
Related
I want place 1 million fish in a lake at random to do so i have this.
ask patches[ if sum [peces] of patches < 1000000 [ask one-of patches with [mallor = 1][set peces peces + 1]]]
This is taking too long, how would you suggest to make it quicker? I know it´s the sum part as it has to always check, but i dont know how else to do it
A first thing I noticed is that you have the construction:
ask patches [ if ... [ ask one-of patches [...]]]
That first ask patches is completely redundant here. It lets each patch direct another random patch to make a fish. Since you use one-of the second time around I don't expect that the speed impact is too big but it should still help
Next, I suggest counting the fish you have already placed with a single local variable, instead of using sum [peces] of patches 1000000 times. Getting variables from an agent (with of or with) takes a lot of time if you have a lot of agents, and I remember from your previous questions that your number of patches is pretty huge.
Then thirdly, you also check for which patches the mallor = 1 condition is true 1000000 times. That one can also be grouped into a local agentset
let fish-placed 0
let mallor_patches patches with [mallor = 1 ]
while [fish-placed < 1000000] [
ask one-of mallor_patches [
set peces peces + 1
set fish-placed fish-placed + 1
]
]
Disclaimer, I haven't tested any of this code in Netlogo since I don't have the bigger model to test the code in. However I expect it will run a many times faster than what you originally wrote.
I would like to apply some process stochastically, but the order matters, or rather it should be done at random, how can I select a set of the same turtles to "treat" but to do it such that each tick, the order is random?
ask turtles [
;; apply a process to turtles, but not all turtles will get something as the pool of energy may have run out by the time they are selected.
]
Elements of agentsets (such as turtles) are always returned in a random order. Each time you use ask with an agentset, it will ask them in a new, random order. From the docs on agentsets:
An agentset is not in any particular order. In fact, it’s always in a random order. And every time you use it, the agentset is in a different random order. This helps you keep your model from treating any particular turtles, patches or links differently from any others (unless you want them to be). Since the order is random every time, no one agent always gets to go first.
And here is a quick example to demonstrate. If you run it in the Command Center you'll see the who numbers will be different each time you ask them to be shown.
to test
clear-all
create-turtles 10
show "Asking once..."
ask turtles [ show who ]
show "Asking a second time..."
ask turtles [ show who ]
end
And here is an example showing an energy pool that will be randomly used until it is gone. Note the turtles that will get to use it are whichever happen to come first out of the agentset for ask:
to test-pool
clear-all
let energy-pool 1000
create-turtles 100
ask turtles [
; can only act if some energy is left...
ifelse energy-pool > 0 [
let energy-use (min (list random 100 energy-pool))
set energy-pool (energy-pool - energy-use)
show (word "I used up " energy-use " energy points! " who " Only " energy-pool " points left")
] [
show (word "No energy left for me! " who)
]
]
end
I want to get the mean age of all dying turtles. I try to achieve that with the following code snipped:
globals [mean-death-age]
turtles-own [age prob-die?]
to-report mean-age
if (prob-die? = true) [
set mean-death-age mean [age] of turtles
]
report mean-death-age
end
Every turtle has the probability of dying (prob-die?) which should be calculated new with every time step (tick). If prob-die? is set to true mean-death-age should be updated. At the moment I have the problem that
I don't know how to initiate the variable mean-death-age. Maybe, working with an if loop would help
if (ticks >= 1) [
if (prob-die? = true) [
set mean-death-age mean [age] of turtles
]
]
I don't get an update of the already calculated mean-death-age but the variable is overwritten and the mean death age of the turtles of this tick is returned
I have problems accesing the value. When I try to call it with e.g.
print mean-age
I get the error message that mean-age is turtle-only even though I tried to save is as a global variable.
The entire code is available here.
I think you're making this more complicated than it needs to be. The easiest approach would be to just keep a list of ages at which turtles die, and then take the mean of that list:
globals [death-ages]
turtles-own [age]
to setup
clear-all
create-turtles 100 [
setxy random-xcor random-ycor
set age random 100
]
set death-ages [] ; start with an empty list
reset-ticks
end
to go
if not any? turtles [ stop ]
ask turtles [
if random 100 < age [
set death-ages lput age death-ages ; add age to our list of ages of death
die
]
]
print mean death-ages
tick
end
The only downside is that your list of death-ages is going to keep growing as your model runs. If that turns out the be a problem (though it probably won't), you will need to keep track of the current average and the current count of observations and update your average with something like set avg ((n * avg) + age) / (n + 1) set n n + 1.
As for why your current code doesn't work, there would be a lot of unpacking to do to explain it all (I'm sorry I can't now). As a general comment, I would suggest trying to take a step back when you run into difficulties like this and think about the minimum amount of information that you need to solve the problem. In this case, you need to mean age of death. What the simplest way to get that? Keep a list of ages of death and take the mean of that list when needed.
I have 2 agent types, boys and girls.
breed [boys boy]
breed [girls girl]
Each turtle has an age from a dataset. Also when an agent is a boy, its boy? is true, and if it is a girl, girl? is true.
turtles-own [
age
boy?
girl?
]
They are connected by some random links. Now I want for each boy, I can access its girl neighbors, and the difference between their ages gets calculated. In other words, the age difference of two different breeds. I wrote this, but it does not work.
ask boys [
ask link-neighbors with [girls? = true]
[
set Gage age]
let H abs(item 0 age - item 0 Gage)
]
Edit When I use ask link-neighbors with [girls? = true]the neighbors are considered all together, while I want them to one by one be considered where I can compare their age difference and base on that do some other stuff.
Any suggestions?
Thanks
This is untested, but I hope it's close enough to get you there if it's not correct.
First, you have some confusion with your breeds and turtles-own sex indicator. It would be much easier to have one or the other. Scrap your turtles-own statement entirely and simply test the breed because then you can't introduce errors where (for example) you have have the flag (girl? or boy?) inconsistent with the breed, or both set to TRUE or whatever. The way you have it set up, it is possible to have a turtle of breed boy but accidentally set its variable boy? to FALSE. There is no need for these variables at all, breed is an automatic variable (like who number or size that is created with the turtle) and you can test on the breed directly.
Getting to your actual error, you are asking the link-neighbors to set their variable Gage rather than setting the value of the original turtle that is doing the asking (that is, the turtle that is the centre of this ego network).
UPDATED from the comments, you want the boy to have a list (called age-diff below) of the difference in age between his own and all the girls he is linked to. The primitive map is used to substract a constant from a list, and asking for the variable values of and agentset constructs the list of those values.
boys-own [age-diff]
ask boys
[ let my-girls link-neighbors with [breed = girls]
if any? my-girls
[ set age-diff map [ x -> abs (x - age) [age] of my-girls ] ]
]
I want to modell the interactions of different tree species in a forest. To do so, I also have to simulate the growth/spread of the forest.
There I face the two following problems:
I want the trees to reach a minimum age from which on they can hatch a new tree each year. But I only know how to make them reproduce every (e.g.) 20 years.
There is also a set age at which the trees are chopped town. The problem is, that when this age is reached, all trees of one breed are chopped down, even if their age should actually be less than their harvest-age.
Here are the relevant parts of my code:
to go
ask oaks [set age ticks]
end
to set-harvest-age
ask oaks [set harvest-age 240]
end
to spread
ask oaks [
if (age mod 30 = 0) and (count oaks > 1) [hatch-oaks 1 [setxy random-xcor random-ycor set age 1]]]
end
to chop-down
ask oaks [if (age >= harvest-age) [die]]
end
The "set age 1" in "spread" does not seem to work. Maybe someone of you has an idea.
Thank you!
I think your main problem is the process order here. Every time go is called, all oaks will set their age to the current ticks. That includes any new saplings that you've hatched, so even if their age was 1 when they hatched, those saplings would instantly be set to the same age as all other oaks (which is just the number of ticks. Instead, you should use your oaks-own (or whatever species you want) variable to track the age of each individual turtle by incrementing it every tick rather than setting it to the ticks.
Additionally, it's probably better to use go or a similarly named procedure to act as the scheduler to call all the other relevant procedures. For example, check out these setup chunks:
breed [ oaks oak ]
oaks-own [ age harvest-age ]
to setup
ca
spawn-oaks
reset-ticks
end
to spawn-oaks ; setup procedure
create-oaks 10 [
set shape "tree"
set color green
setxy random-xcor random-ycor
; Set the harvest age
set harvest-age 100
; Randomly choose the age of the first generation
; to be somewhere between 50 and 75
set age 50 + random 25
]
end
This creates 10 oaks with a an age randomly between 50 and 75. It also sets their harvest age. Now, use a procedure to increase every oak's individual age by one each tick:
to get-older ; Oak procedure
set age age + 1
end
Then, something to have them start creating saplings when they reach maturity. I've included an if any? other oaks-here qualifier so that the population size doesn't immediately explode (as saplings can only survive on a patch without an established oak), but you would limit that growth in whatever way makes sense for your model.
to spread ; Oak procedure
; Get living, mature oaks to spead saplings nearby
; only some years (to avoid population explosion)
if age > 30 and random 50 < 5 [
hatch 1 [
; set sapling age to zero, and have it
; move away from its parent randomly
set age 0
rt random 360
fd random 5
if any? other oaks-here [
die
]
]
]
end
Finally, your chop-down procedure should actually work without changing now that the age issue is sorted out:
to chop-down ; Oak procedure
if age >= harvest-age [
die
]
end
Now all that's needed is to use go to call those procedures in the correct order:
to go
; Use the go procedure to schedule subprocedures
ask oaks [
get-older
spread
chop-down
]
tick
end
Bit of a silly example but hopefully will get you pointed in the right direction!