Counting breeds within an agentset - netlogo

If I have an agentset called zoo belonging to turtle 1, how do I count all the breed of camels in the zoo? NetLogo has a breed-on command, but not a breed-in. So this won't work, although it will illustrate what I'd like:
ask turtle 1 [let c count camels-in zoo]
Thanks.

Best guess is you want something like:
let c count ([zoo] of turtle 1) with [breed = camels]
But an example of a zoo turtleset would be very helpful in understanding what you are trying to do. Perhaps also what your turtles are representing?

Related

Error in plotting: OF expected input to be a turtle agentset or turtle

I am studying a model where children play with some toys.
The characteristics of the toys are included in attribute1. I had to create a new variable, attribute1_b, as I wanted to have initially the same original value for the neighbours in order to subtract a small quantity m. I would like to plot the new value of attribute1 to study how it changes through time, respect of fun2 (i.e. 1- (attribute1-m)), but unfortunately I have found difficulties in defining fun2 (global, children-own, and/or toys-own).
After plotting
ask children [plotxy [attribute1] of picked_toy fun2]
I have got the following error message:
Runtime Error: OF expected input to be a turtle agentset or turtle but
good the number 0 instead
Could you please tell me what the message is referring to and how to fix it?
It is important for me to consider the attribute1 related to the picked_toy, as its value can be equal to attribute1 (as in myself) or attribute1 - m (as in ask link-neighbors).
The code that I am using for building the model is the following
globals [
this-toy
attribute1_b
]
breed [children child]
breed [toys toy]
children-own [
bag
fun1
fun2
attribute1
picked_toy
]
toys-own[
fun1
attribute1
m_children
]
One procedure, to proc1, includes the definition of attribute1 and fun1 as follows:
if breed = children[
set selected children
hatch-toys 1 [
set m_children selected
set attribute1 random-float 1
set attribute1_b attribute1
set fun1 (1 - attribute1)
set this-toy self
ask myself [
set bag fput this-toy bag
]
]
ask link-neighbors [
let m random-float 0.01
set attribute1 attribute1_b - m
set bag fput this-toy bag
]
]
Another one,to proc2, should include the definition of fun2:
if breed = children [
set picked_toy max-one-of turtle-set bag [attribute1]
set fun2 (1 - [attribute1] of picked_toy)
set bag fput picked_toy bag
]
UPDATE: I am also getting the following error message (because of max-one-of turtle-set):
List inputs to TURTLE-SET must only contain turtle or turtle agentset
elements. The list [(toy 20) 0] contained 0 which is NOT a turtle or
turtle agentset.
I cannot understand where 0 comes from...
All comments and clarification are more than welcome. Thanks
First, 'picked_toy' should be a turtle attribute so uncomment it there and delete it from the global variables. A global variable is something that is the same for every agent in your model. Clearly, each child has its own toy, so that needs to be an agent variable. This is what agent variables do - each agent has its own copy (which can be different or the same as those of other agents). You need to get this concept clear if you want to make any progress in NetLogo and I suggest redoing some tutorials.
This line set picked_toy max-one-of turtle-set bag [attribute1] doesn't really make sense. The variable 'bag' is a list. The primitive max-one-of applies to an agentset. I think you have tried to use turtle-set to convert the list to an agentset, but that won't work. The correct way to turn a list of turtles into an agentset is:
turtles with [member? self listname]
Does your code work if you have:
set picked_toy max-one-of (children with [member? self bag]) [attribute1]
As I have mentioned in responses to some of your other questions, unless you have a good reason to use lists (like needing to keep the order, or having multiple copies), it is generally better to use agentsets because they are easier to work with, particularly for beginners.

Find the difference between two variables of the agents of two different breeds - Netlogo

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 ] ]
]

How to 'ask' all turtles in a list

I would like to use ask to loop over all of my turtles, but I do not want the order to be random. My solution is to add all of the turtles to a list, sorted by their ID, and then loop over this list:
let sorted-turtles sort-on [who] turtles
foreach sorted-turtles [
x -> ask x [ ; x is the turtle
; do something
]
]
That works correctly, but vastly slows down my model.
My question is: is this the best way to ask all the turtles to do something in a pre-defined order?
I have tried using ask on the list directly (e.g. ask sorted-turtles [ print who ] but NetLogo says that ask needs an agentset rather than a list.
Thanks in advance,
Nick
First, you can use sort turtles instead of sort-on [who] turtles. It does the same thing, but it's shorter and more idiomatic. Not sure if it's faster, though (it might help a little bit).
The foreach sorted-turtles [ x -> ask x [ ... ] ] pattern should be fast enough. It's the list creation that is costly.
The main thing you can do to improve the speed of your model is thus to store the sorted list of turtles in a global variable and reuse that list instead of re-sorting every time.
That would only work, however, if you're not constantly creating and/or killing turtles, it which case you would have to re-create the sorted list every time.

NetLogo: Finding out all turtles linked to each other and assigning them to a group id

I have searched all topics for the last week and haven't be able to yet find a solution to my problem. The scenario is as follows:
I have various groups of turtles that are linked to each other
I would like to assign a group name or id to each group
In order to do the above, I am thinking that I need a procedure that will find out the id of a turtle and then tell me to which other turtles this particular turtle is linked to. So for example I have a group of 4 turtles, all linked to each other. Their ids are 1, 2, 3, 4. Now I would like to get a list of each turtle, which also tells me to whom it is linked to. Here an example list:
Turtle 1 -> Linked to Turtle 2, 3 and 4
Turtle 2 -> Linked to Turtle 1, 3 and 4
...
Turtle 4 -> Linked to Turtle 1, 2 and 3
Once I now which this (although maybe there is a much easier way). I could assign a group id to these 4 turtles.
Any feedback / help very much appreciated.
Here is part of the code:
to go
ask turtles [
set neighbours turtle-set turtles in-radius neighbourhood
set turtle-dim-similar min-one-of other neighbours [myfunc myself self]
create-link-with turtle-dim-similar
]
set beasts sort-by [[who] of ?1 < [who] of ?2] turtles
foreach beasts [
ask ? [ ;; each agent undertakes the list of commands
set closest sort [link-neighbors] of turtle who
print (word "id:" who " ; val1:" value1 " ; val2:" value2 " ; closest:" closest)
]
]
end
If you are using directed links i.e. links created with create-link-to, create-link-from, create-links-to or create-links-from than out-link-neighbors is what you are looking for. It reports a set of the turtles linked to.
If you are using un-directed links, ones created with create-link-with or create-links-with , use link-neighbors.
This solves the first part of the problem for the second half use recursion.
Turtles-own[group-id]
To id [tag]
Set group-id tag
Ask link-neighbors with[Group-id != tag]
[Id tag]
End

NetLogo AgentSet substraction

I got the error message:
MEMBER? expected input to be a string or list or agentset but got the number 0 instead.
during running following NetLogo Code:
to find-flockmates ;; turtle reporter
;; latch on to the nearby birds
set flockmates-infront other birds in-cone vision cone-infront-degree
set flockmates-sidewise other birds in-cone vision cone-sidewise-degree
;; agentset substraction
if (count flockmates-infront > 0)[
set flockmates-sidewise (flockmates-sidewise with [not member? self flockmates-infront])
]
end
Can someone tell me what I am doing wrong or another possible solution for a substraction of two agentsets?
I see, now! Tough it could possibly have been guessed from the initial code sample you posted, I did not realize at first that flockmates-sidewise and flockmates-infront were breed variables.
As a consequence, in this line:
set flockmates-sidewise (flockmates-sidewise with [not member? self flockmates-infront])
...flockmates-infront refers to the breed variable of the agent executing the with block, not the variable of the agent running the find-flockmates reporter. (And that one may very well be 0 if it has not been initialized yet.) What you want is:
set flockmates-sidewise (flockmates-sidewise with [
not member? self [flockmates-infront] of myself
])
myself means "the turtle or patch who asked me to do what I'm doing right now."
I guess you were not getting the error when creating less than 10 birds because your if prevented the line from being executed at all in that case.