Plot in NetLogo of different breeds with hatch - netlogo

I have some troubles in plotting breeds.
Specifically, I have three breeds, sellers, buyers and cars.
The relationship between breeds is something like the following:
if breed = buyers [
hatch-cars 1
[ set attribute_1 random-float 1
...]
]
if breed = sellers [
hatch-cars 1
[set attribute_2 random-float 1
...]
What I would like to plot is cars depending on the breeds, in order to have two different lines, one for buyers and one for sellers.
I tried with
ask cars with [breed = buyers ][plotxy attribute_1 ticks]
ask cars with [breed = sellers ] [plotxy attribute_2 ticks]
but it does not plot anything and I have not received any error message.
Then, I tried with
ask cars [plotxy attribute_1 ticks]
ask cars [plotxy attribute_2 ticks]
and it is almost ok. However, it plots also other points: it seems that it considers all the breeds, buyers, sellers, and cars.
If I write
if breed = buyers [ask cars [plotxy attribute_1 ticks]]
if breed = sellers [ask cars [plotxy attribute_2 ticks]]
I receive the following error message: You can't use BREED in an observer context, because BREED is turtle/link-only.
How can I fix this issue?
Thank you for your help.

Let's start by looking at why your first attempt does not plot anything:
ask cars with [breed = buyers ][plotxy attribute_1 ticks]
ask cars with [breed = sellers ] [plotxy attribute_2 ticks]
When you declare a breed in NetLogo, it creates a special agentset that has the same name as the breed. If you write:
breed [ buyers buyer ]
breed [ sellers seller ]
breed [ cars car ]
NetLogo creates three special agentsets: buyers, sellers and cars. Each of these agentsets contains only the turtles from the corresponding breed. There also exists the special turtles agentset, which contains all the turtles in your model, regardless of their breed.
In addition to that, all turtles have a breed variable that refer to the breed agentset that they belong to. Each turtle can be either "unbreeded" and have turtles as the value of their breed variable or belong to one (but no more than one) breed and have that breed's agentset as the value of their breed variable.
One thing to know about the breed variable is that you usually don't need to use it. Writing:
ask turtles with [ breed = cars ] [ do-something ]
Has the same effect as writing:
ask cars [ do-something ]
Except that the ask cars version is much faster (and more readable) than the ask turtles with ... version.
Can we see, now, what's wrong with ask cars with [breed = buyers ]? All cars, by definition, have cars as the value of their breed variable, so the breed = buyers condition will always be false. This is why the statement does nothing. (And there is also no reason for it to give you an error message. It's just like asking turtles with [ color = red ] to do something when there are no red turtles: nothing happens, but there is nothing wrong with asking.)
Now let's look at your second attempt:
ask cars [plotxy attribute_1 ticks]
ask cars [plotxy attribute_2 ticks]
You say that "it seems that it considers all the breeds", but given our understanding of how breeds work, you should be able to see that whatever is wrong with it, it can't be that. As far as I can tell, it should work.
My guess would be that you've been creating extra cars somewhere without realizing it. Or maybe it's something else. We can't really tell with the information you have given.
(One small side note: it's customary to put ticks on the x axis, but you're plotting them on the y axis.)
Finally, your last attempt is:
if breed = buyers [ask cars [plotxy attribute_1 ticks]]
if breed = sellers [ask cars [plotxy attribute_2 ticks]]
We've seen that breed is a turtle variable, so it can only be accessed in the context of a particular turtle, and this is why you are getting the warning about trying to use it in the observer context (i.e., by trying to use it outside of a turtle context).
In conclusion, I can't offer a direct solution to your problem, but I'm hoping that gaining a better understanding of how breeds work will help you figure out what's wrong.
Maybe one last thing to clarify is that saying something like:
ask one-of sellers [ hatch-cars 1 ]
does not create any kind of relationship between the newly hatched car and the sellers breed. The car doesn't know it's been hatched by a seller. And there is no way to query NetLogo for "all the cars that have been hatched by sellers" unless you keep this information in another variable somewhere. But how to do this is another question (that you're welcome to ask on this site if needed).

Related

How to extract the who number of agents in an agentset? (NetLogo)

I have a patch variable set as visitantBees [].
The intention is that each patch has a record of which agents were in it.
The method for registering the "visiting agents" is as follows:
to computing Frequency
ask patches
[
if any? turtles-here
[
set visitantBees lput [who] of turtles-here visitantBees
]
]
end
However, this way a list of list is returned ([[3 1 0 2 4]], for example).
Would anyone know how I can add only the who number to the visitantBees list?
Maybe a way to extract all the items from the turtles-here.
The reason why you get a list of lists is that both visitantBees and [who] of turtles-here are lists.
While visitantBees is a list because you set it as a list, why is [who] of turtles-here a list? Because turtles-here is an agentset - and the only way to report a variable of an agentset is to create a list. For example, if you wanted to know [color] of turtles, the only way NetLogo has to give you this information is to put all the turtles' colors in a list.
So, why is turtles-here an agentset? Because, even if sometimes turtles-here can contain 0 or 1 turtle, it can also contain multiple turtles. And anything that is fit to contain multiple agents has to be an agentset.
On the other hand, when you ask a single agent to report one of its variables, you get the value as such, i.e. not a list (unless that value is a list in itself, of course). For example, [color] of turtle 0 is just its color, not a list containing a color.
Therefore, you can achieve your goal by individually asking every turtle on the patch to append their who to visitantBees:
to computingFrequency
ask patches [
ask turtles-here [
set visitantBees lput who visitantBees
]
]
end
Or, given that turtles can automatically read and change the patches-own variables of the patch they are standing on, you can make it even simpler:
to computingFrequency
ask turtles [
set visitantBees lput who visitantBees
]
end
Which is also faster because it will only engage with turtles (who, by definition, are standing on a patch) rather than engaging with every patch even if there are no turtles on it.

Netlogo: "foreach" command with two lists

I have a complete directed graph with each link a weight of it's own. I've managed to select the max-out-link of every turtle. But, sometimes the max-out-link of two turtles are opposite of each other resulting in both links opposite of one another being selected. if this happens i want the link with the lower value to die.
i have created the two lists with this:
set max-end1 [[end1] of max-one-of my-out-links [trust]] of turtles
set max-end2 [[end2] of max-one-of my-out-links [trust]] of turtles
and by setting an x and y parameter like so:
ask turtles
[
set x max-one-of my-out-links [label]
set y my-in-links
]
i was hoping to compare each item of the two lists like so:
if [x] of max-end2 = any? [y] of max-end1
[
ifelse x X y
[ask x [die]]
[ask y [die]]
]
but i have no idea how to combine the foreach command with the if command
can someone help me?
I can't actually figure out how your code is supposed to work. Lists seems like an awkward way to approach this and the way you are using any? is not going to work. So, I have instead started again and written a standalone model (put it in an empty NetLogo session) to do what I think you are trying to do.
links-own [ weight ]
to testme
clear-all
create-turtles 15
ask turtles
[ create-links-to other turtles
[ set weight random 100
]
]
layout-circle turtles 10
kill-lowers
end
to kill-lowers
; first remove any that's not a max weight
ask turtles
[ let big-link max-one-of my-out-links [weight]
let dying my-out-links with [not member? self (link-set big-link)]
ask dying [die]
]
; find pairs and make lower turn red
ask links
[ let opp-links links with [end1 = [end2] of myself and end2 = [end1] of myself ]
if any? opp-links
[ ask opp-links [set color red]
]
]
end
The testme procedure just creates a complete network. It then calls the kill-lowers procedure to do the link removal. The first section has each turtle identify its out-link with the largest weight (or randomly selects one if two equally high) and then creates the link-set of all the other links and gets them to die. I think you already have that happening in your code.
So the interesting bit is the section that starts ask links. It randomly runs through all the links (think of it as a foreach except operating on a set of links rather than a list). In turn, for the particular link, the code checks if there is a link in the opposite direction and sets that to the variable named opp-links. The way it checks is to simply see if there is a link that has end2 to be its own end1 and also the other way. end1 is the source of a directed link and end2 is the target.
If there is such a link, it becomes red. That's so you can check the code is doing what you want. After you have tested it, then you have it die instead.

Netlogo: Store agentset in patch property

I am simulating a neighborhood. Patches represent households, turtles people living there.
I want to track "households" and thought it would be convenient to store an agentset of each household at the patch. That would allow me to do easy "household behavior", like ensuring regular groceries.
However, ask homePatch [ set houseHold (turtle-set partner myself) ] just stores 0 in the patch variable.
Is it possible to save agentsets in the patch variable? It is defined in patches-own.
It is possible for a patch variable to hold an agent set, as shown in the following example.
patches-own [ household ]
to test
clear-all
ask patches [set household nobody]
create-turtles 100 [
fd random 10
if any? other turtles-here [
let partner one-of other turtles-here
ask patch-here [set household (turtle-set partner myself)]
]
]
ask patches with [household != nobody] [show household]
end
To know why is seems not to be working for you, we'd need to see more of your code, since the line you provide does work. (Note that if the turtle who is "myself" is sitting on the patch homePatch, it can set the homePatch variable directly with set household (turtle-set partner self)).

NetLogo - Getting the 'who' of another turtle in the same patch

I'm looking to get the 'who'/turtle ID of a turtle that occupies the same patch as another, and then add this as an item into a list for both turtles.
For example, say turtle A and turtle B are on the same patch, I'd like to store the who of turtle A in the list for turtle B and the who for turtle B in the list for turtle A.
I realise this may be quite a trivial thing to do, so I attempted to do this with the following code:
if not any? turtles-on neighbors[
if who != who[
set collision-list fput (list (who)) collision-list
]
]
Here, I'm checking the patch to see if it contains another turtle, if it does then I'm trying to store the who (using a condition for if the who is not the same as the current who) and if it isn't, then store this in the collision-list for each agent.
Ordinarily it is a mistake to work with who numbers instead of the turtles themselves. So I'll illustrate how you might augment a "collision list" of turtles.
turtles-own [clist]
to setup
ca
crt 100 [
setxy random-xcor random-ycor
set clist []
]
ask turtles [adjust-clist]
end
to adjust-clist ;turtle proc
let _ts [self] of (other turtles-here)
set clist (sentence _ts clist)
end

How to have each turtle share the same links as their link-neighbors

I've been trying to write a model where turtles create links with a certain number of other turtles, and for those turtles to create links with the link-neighbors of the turtle that linked to it. Right now, I can get turtles to create links with other turtles, but they don't share the same link-neighbors. I want to end up with little sub-groups of turtles of a certain group size. However, at the moment I can only tell turtles to create a certain number of links, but they don't end up in sub-groups because their link-neighbors don't necessarily share the same link-neighbors. I thought I could ask each turtle to ask their link-neighbors to create-link-with [link-neighbors] of myself. I think this would work, except I get an error saying a turtle can't link with itself. I've tried to change the code so it says to a turtle ask link-neighbors [create-link-with [link-neighbors] of myself with [who != self]], but this doesn't work either.
Here is some of my code:
;group size is 1 + (count link-neighbors)
;preferred-size is a slider, used to alter what group size I want turtles to be in
ask turtles
[if (preferred-size > group-size) and (any? other turtles in-radius 1 with [preferred-size > group-size])
[create-link-with one-of other turtles in-radius 1 with [preferred-size > group-size]
ask link-neighbors
[create-links-with [link-neighbors] of myself]
]
Also, is there a term like link-neighbors but referring to all the turtles on a string of connections?
Any help would be greatly appreciated!
You were very close! All you need is other:
create-links-with other [ link-neighbors ] of myself
You cannot compare who to self: who is a number and self is a turtle. And in the context of with [ who != self ]], they would always be variables of the same turtle. In any case, it's usually better to avoid dealing with who numbers anyway: there's almost always a better way to do things.
is there a term like link-neighbors but referring to all the turtles on a string of connections?
I'm not entirely sure that's what you mean, but maybe nw:turtles-on-path-to? Or perhaps you will find some other useful thing in the nw extension.