How can I rank turtle, according to their attribute? in Netlogo - netlogo

I built a model that each turtle has their own strategy, and they will earn money according to their strategy.
Therefore, Turtles will own money, and strategy.
I want to rank top10 turtles who has the most money, and want to see which strategy that they have.
Is there any way that I can show it?

You can use max-n-of to get the top 10 agents and ask them about their strategy.
Something like
ask max-n-of 10 turtles [money] [show strategy]
To sort the result use sort-on:
foreach sort-on [(- money)] max-n-of 10 turtles [money]
[ ask ? [ show strategy ] ]

Related

Speed up an age and position check for turtles in R?

I'm writing a program that is testing the ability of turtles to navigate in an environment. This line of code has two checks. I want it to kill off the oldest turtle that has made the least amount of progress. It does what it is supposed to, but the program slows down dramatically, creating little hiccups every time it makes this check. I was wondering if someone knew a better way to go about this or make this line more efficient. Thanks!
ask one-of turtles with [XCOR = min [XCOR] of turtles with [age = max [age] of turtles]] [
die]
First of all, there are primitives that are specifically finding the turtles with maximum or minimum values of some variable using min-one-of or with-min. So your code would look something like this I think:
ask min-one-of (turtles with-max [age]) [xcor] [...]
I suspect that would solve your efficiency problem, since it may well be because of implicit brackets not being where you think they are so it's trying to loop everything a couple of times. But a much cleaner to read version that would solve the efficiency problem is to specifically limit the position loop to those who are oldest and then choose the lowest position from that set.
let old-turtles turtles with-max [age]
ask min-one-of old-turtles [xcor] [...]

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

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?

Ask turtles to visit random turtles in different ticks

I have these agentsets called collectors and bins, collectors visit bins once a day (each tick is equal to one hour). But they will keep visiting the same bin on the other days.
I would like to make them on the next day to visit a random different bin, but I do not know how to ask that. Could someone help me, please.
Here is the command where I ask this command.
to search-bins-to-collect
ask one-of collectors [move-to one-of [ bins in-radius 10] of myself]
collect-waste
end
The number of collectors will vary during the year and I can not tell them the exact number of the turtle.
Thank you in advance.

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.

Comparing two agent variables

I am currently making a simulation (for homework) using genetic algorithms. What I want to do is compare the fitness of agents on a specific patch and the one with the lowest fitness will die.
I have scoured the net and found this code: if any? breed1-here with [fitness > fitness-of myself] [die]]
But this doesn't seem to work and now I'm completely out of ideas.
let goner min-one-of breed1-here [fitness]
if is-turtle? goner [ ask goner [ die ] ]`
the is-turtle? check is necessary because the patch might be empty.
Yes, that code is from an old version of the NetLogo language. That line of code should be re-written as:
if any? breed1-here with [fitness > [fitness] of myself] [die]]
Of course, that code will kill all turtles in a patch except for the one(s) with maximum fitness, which is not exactly what you want.