How do I count the number of unique interactions between turtles in my model? - netlogo

I am writing a NetLogo model to describe the food exchange interactions between pairs of bees until the food is distributed evenly among all. I have two breeds of "fulls" and "hungries" in my model. Right now I am counting the number of interactions until the model stops, like this:
ask turtles [
set neighborhood other hungries-on neighbors
set target one-of neighborhood with-min [distance myself]
ifelse target != nobody
[ exchange-food
set counter counter + 1]
;if there are no hungry neighbors continue moving and searching for one
[continue ]
]
The counter here, counts all the exchange-food interactions but I am also interested in the number of unique interactions. Does it mean that I have to keep a list of tuples as a turtle-own variable for each turtle! But I don't even need the actual ids, I just want to count the number of unique interaction. How can I keep track of this? Any simpler ideas?

Related

keeping variable results after turtle dies netlogo

Looking for a way to store a turtle length of stay in the model after they have left the model. My model runs for several months and a few thousand turtles enter, undergo process then leave the area. It's complicate model (it's a hybrid DES and ABM) so I've tried to reproduce the simple bit below.
Turtles will be created at every tick and given a random length of stay but will only be able to begin process when they move to the right area (area-name) and when their time is up they leave the area. Their time-in-system reflects the wait for the area and the length-of-stay which I want to save once they're complete. If I leave them in the model it starts to break down after a couple of months and I suspect this is because the model has too many turtles still in the system for calculation and is inefficient.
go
create turtles 2
[
set time-in-system 0
set length-of-stay ceiling ((random-normal 48 4) + ticks)]
set shape "person"
if any? area-name with [not any? turtles-here]
[move-to one-of area-name]
]
undergo-process
end
to-undergo-process
ask turtles with [shape = "person"]
[
set time-in-system time-in-system + 1
]
ask turtles-on area-name
[if ticks = length-of-stay
[set shape "dot"
move-to exit-door]
end
I can then plot and see in realtime to make sure it is working
histogram time-in-system of turtles with [shape = "dot"]
but can't seem to figure out how to store them as unique values for plotting after the model has run and I have a dataset of outcomes without keeping them alive in the model. The real-time plot isn't necessary as long as I can store the unique values after they have left
If I ask them to die then I lose the unique values in the histogram. I don't want a tally of all values but each turtle's unique value at the end of the process after they left - at the moment the only solution I have to storing them is as an agent-set that stays alive in the exit-door patch but this takes up a lot of calculation power as the model progresses for months...
There may be a really simple command for this but I've been going round in circles through the programming manual trying to find it. Any tips appreciated
You should create a list storing the values of turtles that left.
Isolating only the code that is relevant for this purpose, it would be something like:
globals [
times
]
to setup
set times (list)
end
to leave-simulation ; This being executed by turtles.
set times lput (time-in-system times)
die
end
If your program is going to run for actual months, I recommend you use the file-write command to store your data. This way the data is preserved if the program halts for any reason; it gives you much more freedom to do the analysis you want without running the full simulation again.
If you write to a .csv (comma separated value) file, you can use almost any program (excel, R, matlab, python, C# or back to netlogo) to plot a histogram.

Netlogo: is nobody still being treated as an agent?

I have a list of parcel agents. but I will kill the parcels periodically. However, the list is still recording someting like this: [nobody nobody nobody nobody nobody nobody nobody nobody], and overtime the running of the model is getting slower and eventually pop up message "your model is too large to run with available memory"
In this case, are the dead agents (i.e. nobody) still treated as an agents that consumes much of the memory? what if it is a pure list of numbers or strings? would it cause the same OOM issue? how big the list can be in Netlogo and any uppper limit?
From, the NetLogo dictionary for die: If you have a list of agents and the agent dies, then the agent is removed from any agentset and:
The agent will disappear from any agentsets it was in, reducing the size of those agentsets by one.
Any variable that was storing the agent will now instead have nobody in it
The dead agents are not consuming resources, but the list is (as you have found by printing out the list). You can see this with the following model:
globals [mylist myagentset]
to setup
clear-all
create-turtles 1
set mylist sort-on [who] turtles
set myagentset turtles
reset-ticks
end
to go
create-turtles 1
[ set myagentset (turtle-set myagentset self)
]
set mylist lput one-of turtles mylist
ask one-of turtles [die]
type "turtles: " print count turtles
type "list: " print length mylist
type "agentset: " print count myagentset
tick
end
If you want the dead turtle to be removed from the list, you need to explicitly do so with remove-item. The same is true of lists of numbers, strings etc.
Alternatively, if the list doesn't need to be maintained over ticks, but can be reconstructed (eg if it is a sorted list of the turtles agentset), you could create it each tick and that list would only contain turtles that are alive.

hatching random specific turtles

I an creating a model with 2 breeds, males and females. When the number of turtles becomes less than 100, I want to randomly hatch either breed (both having an equal chance of being created) to return the turtle count to 100. How would I go about doing this?

Add a turtle's variable value to another turtle's variable value

I am struggling with trying to write this command.
Basically, what I want to do is this:
I have a collectors [own carryingcapacity], bins [own waste in bins] and warehouses [own waste_in_warehouse] as turtles.
The collectors have a maximum carrying capacity value.
I want to make the collectors check if they have carrying capacity when they find a bin, if trash in bin <= collectors max capacity, the collectors will collect, if not they'll ignore the bin.
I came up with something like this:
ask collectors
[
if any? bins with [distance myself <= 1]
[set carryingcapacity (carryingcapacity + (bins_holding_capacity of myself))]
]
Second command:
I want to make the collectors take the trash they collected to the warehouse. But this variable's value might change from one collector to another, and might vary with the quantity of bins they checked.
I want the warehouse to sum the value that it already had it on the previous days with the new daily values.
I came up with something like this as a line of command:
ask warehouses
[
if any? collectors with [distance myself <= 1]
[set waste_in_warehouse ( waste_in_warehouse + (carryingcapacity of myself))]
Thank you in advance.
Best wishes.
If I'm understanding correctly, your code for command #1 is doing the following:
ask collector agents
See if there are any bins close to a collector (but you didn't ask for this group)
increment the collector's carryingcapacity by (bins_holding_capacity) of myself (you can't use myself like this, because you aren't in a nested ask)
Step 3's logic also seems wrong, since this would be changing the collector's capacity each time, as opposed to checking if it is full.
It should be noted that by using any?, you are not also commanding that group of agents. any? only returns true or false, corresponding to whether there were agents that met the criteria or not. You need to use a second ask to command these agents.
Based on what you said, you'll need to do something like this:
ask collectors
[
ask bins with [distance myself <= 1] ;;myself refers to the collector asking
[
ifelse (waste_in_bin + [waste_in_collector] of myself) > [carryingcapacity] of myself
[;;add the waste to the collector]
[;;stop asking bins, collector is full]
]
]
These same steps should help with your second question, too.

Turtle variable representing memory of other turtles

I have a model in which turtles navigate a graph of nodes (also stored as turtles) and links in discrete increments (using the move-to procedure). I would like to create a turtle based variable that allows turtles to remember the number of times they have visited particular nodes in the graph.
e.g.
turtle move-to a node
look up the presence of that node in the desired structure
if it exists set #visits #visits + 1 associated with that node
if not include it in the structure and set #visits 1
I imagine my options are related to lists or tables - however I am not sure which of these will be most efficient. With regards to tables I don't think the key can be a turtle - perhaps a string derived from its who - but this feels like it may be inefficient.
Two points that are likely pertinent:
some of the graphs will have a large number of nodes (~5000) , and often some of the turtles will never visit all of them, so it may be sensible to grow the structure on-the-fly to save memory.
there can also be a relatively large number of agents (~2500)
Any advice, as ever, much appreciated.
Tables are the more efficient way to go as well as easier codewise, though using agents as keys does not work (much to my surprise). That said, you can just use who numbers as keys.
It would look something like:
extensions [ table ]
turtles-own [ node-visits ]
...
to move-to-node [ node ]
move-to node
let key [ who ] of node
let visits ifelse-value (table:has-key? node-visits key) [ table:get node-visits key ] [ 0 ]
table:put node-visits key (visits + 1)
end
tables are efficient in both memory use and lookup speed. You could do this with lists and be efficient in memory use (if you use key value pairs) or lookup speed (if you use who numbers as indices or something) but not both, unless you basically write your own hash table implementation with lists.
Now, all that aside, you generally should worry about performance problems like these until you are:
having performance problems right now
know that these things are the source of your performance problems (by profiling either with the timer or with the profiler extension)
The real reason I suggest the table extension is because it's the simplest to implement.
I realize I am late to the party, and Bryan's answer is perfectly fine, but an alternative solution would be to use a separate breed of links to record the number of visits to a node:
breed [ nodes node ]
undirected-link-breed [ edges edge ] ; node <---> node
breed [ agents an-agent ]
directed-link-breed [ visits visit ] ; agent ----> node
visits-own [ num-visits ]
to setup
clear-all
create-nodes 10 [ create-edges-with n-of (1 + random 2) other nodes ]
create-agents 10 [ move-to one-of nodes ]
end
to go
ask agents [
let destination one-of [ edge-neighbors ] of one-of nodes-here
move-to destination
if out-visit-to destination = nobody [ create-visit-to destination ]
ask out-visit-to destination [ set num-visits num-visits + 1 ]
]
end
It might not be quite as fast as using the table extension, but it would have the advantage of allowing queries from both sides: you ask an agent how many times it visited which nodes, but you can also ask a node how many times it has been visited by which agents.