ask turtles - ordering of agentset - netlogo

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

Related

How to report agent variables in a consistent order in Netlogo's Behaviourspace

Picture of my behaviourspace menu
I'm working on an agent based model where a variable (agentvariable1) owned by all agents changes every tick. I want to report a time series for the values of this variable for every agent using Behaviourspace.
However, when I measure runs using the following reporter
[agentvariable1] of turtles
the values that are reported for agentvariable1 are randomly shuffled, because "turtles" calls all turtles in a random order, which is different every tick. Because of this the data that is exported is not usable to create a time-series.
Is it posstible to create a reporter in Behaviourspace that reports the values of the agentvariable1 in a sequence that remains the same every tick?
Using sort on an agentset creates a list of those agents sorting them by some criteria. In the case of turtles, they are sorted by their who which means that their relative order will always be the same.
However you cannot directly do [agentvariable1] of sort turtles, because of expects an agent/agentset but you are giving it a list.
What you can do is creating a global variable as a list: at each tick the list is emptied, and later all turtles (sorted as per sort) will append their value to the list.
That list is what you will report in your Behavior Space.
globals [
all-values
]
turtles-own [
my-value
]
to setup
clear-all
reset-ticks
create-turtles 5
end
to go
set all-values (list)
ask turtles [
set my-value random 10
]
foreach sort turtles [
t ->
ask t [
set all-values lput my-value all-values
]
]
show all-values
tick
end
As an alternative to Matteo's answer (which is perfectly suitable and directly addresses your intention, I just present another option depending on preference) you could also pair the variable of interest with some turtle identifier and report that as a list of lists. This adds a bit of flexibility in cases where the number of turtles increases or decreases. In this example, I use who and xcor for simplicity, but you may want to create your own unique turtle identifier for more explicit tracking. With this toy model:
to setup
ca
crt 5
reset-ticks
end
to go
ask turtles [
rt random 30 - 15
fd 1
]
tick
end
to-report report-who-x
report list who xcor
end
At any point, you can call the list with [report-who-x] of turtles to get a list of lists. With a behaviorspace setup such as:
you get an output that would look something like:

Percentage in netlogo

I want to write in Netlogo that a certain percentage of the agent's population has this attribute. How do I do that in NetLogo?
So far, in a toy model, I do it manually. i.e: ask n-of 740 households [set composition 1] when in fact I want to say: ask 8% of the households to set composition 1.
There are two ways. I will call them ex-ante and ex-post.
Ex-ante
A frequent approach is to let each agent have a certain chance (expressed as the percentage value) of doing something. In this case you will use the random-float command in combination with your percentage value, which is the standard way to make things happen in NetLogo according to a certain probability (see here, or also see just random if you're working with integers). It can be used directly within the create-turtles block of commands:
globals [
the-percentage
]
turtles-own [
my-attribute
]
to setup
clear-all
set the-percentage 0.083 ; In this example we set the percentage to be 8.3%.
create-turtles 500 [
if (random-float 1 < the-percentage) [
set attribute 1
]
]
end
This means that you will not always have the exact number of turtles having that attribute. If you check count turtles with [attribute = 1] on multiple iterations, you will see that the number varies. This approach is good if you want to reproduce the probability of something happening (over a population of agents or over time), which is the case for many uses of NetLogo models.
Ex-post
The ex-post approach follows the logic that you more or less expressed: first you create a number of turtles, later you assign to them the attribute. In this case, you simply need to treat the percentage as in any other mathematical expression: multiply it by the total number of turtles to get the relevant turtles:
globals [
the-percentage
]
turtles-own [
my-attribute
]
to setup
clear-all
set the-percentage 0.083
create-turtles 500
ask n-of (count turtles * the-percentage) turtles [
set attribute 1
]
end
With this approach, you will always have the exact same number of turtles with that attribute. In fact, if you run count turtles with [attribute = 1] on multiple iterations you'll see that the result is always 41 (500 * 0.083 = 41.5, in fact if the number passed to n-of is fractional, it will be rounded down).

Netlogo: How do I make turtles of a specific breed make a decision based on previous ticks?

I'm trying to build a simulation where commuters can choose to take the car or bike to work. I'm want the turtles to make a decision based on their wait time in traffic from the last 20 ticks e.g. if average wait time is more than 10, they will take the bike to work. I have a plot that shows average wait time of cars and managed to have them make decision on that overall average. However, I have not managed to make each car make a decision based on their own experience. I'm not getting any errors, just not any results.
So far I have turtles owning average-wait-time-list and wait-time.
Create-cars ;;(cars are one of two breeds)
set average-wait-time-list []
to-report average-wait-time-sum
;; drop the first member of the list, but not until there are at least 60 items in the list
if (length average-wait-time-list > 20) [ set average-wait-time-list but-first average-wait-time-
list ]
;; add the number of raindrops created in last tick to the end of the list
set average-wait-time-list lput wait-time average-wait-time-list
report sum average-wait-time-list
end
to record-data ;; turtle procedure
ifelse speed = 0 [
set num-cars-stopped num-cars-stopped + 1
set wait-time wait-time + 1
]
[ set wait-time 0 ]
end

Netlogo: asking "up to a maximum number"

Is there a way to ask up to a certain number of patches? For example, ask up to 100 patches but there are only 50 available, so the action takes on this 50 patches. Thanks.
The way to do this at the moment would be something like:
to-report at-most [n agents]
report ifelse-value (n <= count agents) [ agents ] [ n-of n agents ]
end
You can then say ask at-most 100 patches [ ... ] and you will get what you want.
Note that this doesn't work if there is a chance that your variable contains nobody instead of an agentset. In that case, you can convert nobody to an agentset using patch-set, turtle-set or link-set, depending on the type of agent you expect it to contain. For example:
ask one-of turtle-set other turtles-here [ ... ]
Note that the need to jump through all these hoops might disappear in the near future. There is currently an open proposal to add a primitive to NetLogo for handling these cases: https://github.com/NetLogo/NetLogo/issues/1594.

Get the mean age of all turtles on the time of death

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­.