Storing / recalling the value of a variable in Netlogo - netlogo

Is there a way to store / recall values of a variable in Netlogo from previous ticks?
I need a way to recall what a variable was from previous ticks. If my turtle variable R was equal to 0 the last 3 ticks, I need it to set another variable back to zero.
Here is what that I was thinking.
to regression
ask turtles [if (R = 0 from last 3 ticks [Set Oin 0 ]]
end

How about making a list of variables, then limiting the length of that list to how far back you'd like the agent to "remember"
set memory []
then add to the list each tick and get rid of old memories with
set memory lput value memory
if length memory >= 4 [set memory but-first memory]
and if zero is on the list, have that alter behavior in some way
if member? 0 memory [blah]

Related

How to add values of a variable for specific turtles?

I want to add the values of a variable ( let's say transportation cost) for specific turtles which are in a group. The transportation cost depends on the distance between a specific patch and the location assigned to the turtle. As the value of the said cost is different for each turtle, I want to sum the total cost for turtle which are in a group. To clarify, let's suppose there are 7 turtles in total and only 4 are in a group.
The values of transportation cost of each turtle is assigned as tcost.
to calculate-ttcost
set ttcost 0
let cnt 0
ask turtles [
if in-group? [
set ttcost (tcost + tcost)
set cnt cnt + 1
]
]
end
With the correction of one typo, the code you have should work, assuming that tcost is declared as a turtles-own variable with that turtle's transportation cost (or a reporter that gives the transportation cost for the turtle that calls it), and assuming that those turtles that are in the group in which you are interested have their turtles-own variable in-group? set to true. The typo is in the line
set ttcost (tcost + tcost)
which should be
set ttcost (ttcost + tcost)
But there is a more straight-forward coding that will accomplish the same task.
let cnt count turtles with [in-group?]
let ttcost sum [tcost] of turtles with [in-group?]
with limits the set of turtles to those for which in-group is true. of creates a list of the values of tcost for each of those turtles, which can then be summed up.

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

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

How to change a turtle's attribute if one of its links disappear?

In NetLogo: suppose the model has
a turtle (0) of breed A with undirected links with 3 turtles (1, 2 and 3) of breed B;
the turtle 0 has an attribute named "number-of-links" that equals 3.
Now, let one of the 3 neighbors of 0 dies..
How can I program turtle 0 to change its number-of-links automatically to 2?
If all you want is a way of keeping track of the number links, use count my-links instead of a custom variable.
In general, the least bug prone way of having a value update when the number of links changes is to compute that value when you need it. For number of links, this is simply count my-links. For more complicated things, wrap them in a reporter:
to-report energy-of-neighbors
report sum [ energy ] of link-neighbors
end
If this doesn't work for whatever reason (agents need to react to a link disappearing or you're seeing a serious, measurable performance hit from calculating on the fly), you'll have to make the updates yourself when the number of links change. The best way to do this is to encapsulate the behavior in a command:
to update-on-link-change [ link-being-removed ] ;; turtle procedure
; update stuff
end
and then encapsulate the things that can cause the number of links to change (such as turtle death) in commands as well:
to linked-agent-death ;; turtle procedure
ask links [
ask other-end [ update-on-link-change myself ]
]
die
end

Why does netlogo think that this input is 0?

I have the code:
to calculate-road-length
if [pcolor] of patch mouse-xcor mouse-ycor = 7[
set this-thing road-number
set plabel road-number
set number lput road-number number]
Where road-number is an input. Why do I get the error "LPUT expected input to be a list but got the number 0 instead" even when I enter a number? Any help would be good.
The problem is not with road-number, it is with number: lput expects the second parameter to be a list. Have you initialized number to a list?
You probably want to put set number [] in your setup procedure.
(number is a strange name for a list, by the way. Should it be plural, i.e., numbers?)