How to link agentbreed1 to agentbreed2 one-on-one? - netlogo

I got 2 large sets of agentbreeds1 (villagers) and an equal amount of agentbreeds2 (houses). I am attempting to link these two together on a one-to-one ratio. So far I tried to do this by the following line, but I am not sure whether it does so correctly:
ask villagers [
create-link-with one-of other houses
]
Just for clarity, if I have villager1 it may only connect to one house that does not have any link to another villager already.
Hopefully one of the bright folks here can help me further!
Thanks in advance.

That's pretty close, but since you have no constraint saying something like "you may only create a link with a house that has no villager already" you will end up with some houses that have multiple villagers linked and some houses that have no villagers linked. All you have to do to correct that is to include that constraint, such that villagers may only link to a house that is not already linked:
ask villagers [
create-link-with one-of houses with [ not any? link-neighbors ]
]
To check that it worked, you can either visually inspect the links or run some variation of the lines below:
ask turtles [
if count link-neighbors != 1 [
show ("I am not linked to just one turtle.")
]
]
If any turtle is not linked to another, or is linked to more than one, it will print the line in quotes.

Related

Creating attribute of a breed and managing

I have two breeds, let's say sellersA and sellersB, and an item that I want to create for one seller chosen either from a sellersA or sellersB.
Right now, therefore, I have
globals[
chosen?
]
breed [sellersA sellerA]
breed [sellersB sellerB]
sellersA[
catalogue
]
sellersB[
catalogue
]
and the item which has two attributes, attr1 and attr2. I initialised the catalogue in the setup: set catalogue [].
The following part of code should check if the catalogue is empty or not: if it is empty, a selected seller create a new item with some specific attributes (1 and 2):
let selected_one nobody
let customers nobody
set selected_one one-of turtles with [breed = sellersA or breed = sellersB]
ifelse empty? [catalogue] of selected_one [
create-items 1[ hide-turtle set new_item self set chosen? false]
ifelse [breed = sellersA] of selected_one
[ ask selected_one [
set attr1 random-float 1
set attr2 random-float
set function1 (1 + attr2)
]
]
[ ask selected_one [
set attr1 random-float 1
set attr2 random-float
set function2 (1 - attr2)
]
]
Then the seller add the item to its catalogue and to the catalogue of the customers connected with.
ask selected_one [
set customers (turtle-set self in-link-neighbors with [breed = sellersA])
ask customers [set catalogue fput new_item catalogue]
]
]
]
If the catalogue of a selected seller is not empty (i.e. the seller has items to sell), I would like to select one of the previous items created and track it (for example if one of sellers A created this new item and this item was added to its list and to its customers' list (neighbours), I would like to know how many customers from its neighbourhood selected its item).
For your previous answers to my questions, I know that I should use the extension rnd:weighted-one-of and, in order to select the item with highest value of one of the two attributes, I should create a new list with only indices that point to the attributes of the object created. This should require to use who. However, as result, I am getting only empty lists when I run show catalogue in the observer.
I think the problem might be in the definition of the item and of its attribute. I tried to define it as a breed with attr1 and attr2, but it did not work when I put the function as sellersA-own.
I would like to keep the code simple, but I do not know how.
I hope you can help me.
You don't show it, so I will assume you have a breed of items which have attributes:
breed [ items item]
items-own [
attr1
attr2
]
So sellers have catalogs, the catalogs list items, and the items have attributes.
It's not clear to me who "customers" are, or what attributes they have. They seem to be simply other sellers who have links to a given seller. Is that right?
Anyway, you asked
I would like to know how many customers from its neighbourhood
selected its item).
The phrase "its item" implies to me that items must have some additional attribute such as "creator". In any case, you don't describe what it means for a customer to have "selected" an item, or where you store such information, or where you store a history of who ever selected an item in the past -- so it's impossible to give advice on this question.
You also ask why this happens:
I am getting only empty lists when I run show catalogue in the
observer.
Well, "catalog" is not a global variable, it is an attribute of the sellers. You need to inspect the sellers to see if they have catalogs with the correct item or items in them, or ask something like
ask sellersA [ show catalog ]
but more probably, you want to open an inspector window and leave it open so you can watch what's going on.
inspect one-of sellersA
But, however you do it, you may still find that no sellers have catalogs with any items in them. You should look to see if any items have been created, as they are global and you should be able to see them in the observer, or inspect them, or simply count them.
Actually I don't understand how the code you listed above could possibly work. Your code has
[ ask selected_one [
set attr1 random-float 1
set attr2 random-float
set function1 (1 + attr2)
]
where selected_one is a seller, who does not own attributes or functions. You're not in the context of an item so I would expect the command "set attr" to fail. Maybe that code is failing silently which is why you have no items.
After you run your code, are there any items that have been created?

ASK expected input to be an agent or agentset but got NOBODY instead

In my study, I'm trying to build a simulation representing of a real work setting, with the aim of creating knowledge on how make people feel better at work.
In particular, I'm modelling a scenario where people work for more than one team (do you work on only one project at a time? Many don't...). To do this, I'm working with NetLogo.
I'm having a problem with an ASK to a specific agent of a custom link-turtle-set. The point is that sometimes it reports an error, saying that "ASK expected input to be an agent or agentset but got NOBODY instead", but it should not arrive to that "ASK" without the agent to exist! What am I doing wrong?
The function is the following:
to TeamRecruiting
;; I ask non-complete teams to...
ask teams with [ teamsize != (count membership-neighbors) ]
[
;; ... count how many individuals they have...
let actualteammembers count membership-neighbors
;; ... to save locally how many individuals they can share...
let teamoverlap overlap
;; ... their target size...
let neededsize teamsize
;; ... and their identity.
let teamwho who
;; then I ask those individuals that have not yet more things than they can handle...
ask individuals with [ indMTM != (count membership-neighbors) ]
[
;; ... to save locally who they are...
let indwho who
let createdalink 0
;; ... then if some conditions have been met ...
if(some conditions)
[
;; I create a link (of my specific type) with the team...
create-membership-with team teamwho
;; I do the following because more than one individual could join the team teamwho in the same run of the function
ask team teamwho [ set actualteammembers (actualteammembers + 1) ]
set createdalink 1
]
;; if the association occurred, ...
if(createdalink = 1)
[
;; we ask all other teams to evaluate if the new connection violates their maximum overlap constraint between the team I am considering from the beginning of the function, and all other teams, in other words...
ask teams with [ who != teamwho ]
[
let numpaths 0
let team2who who
;; I count how many individuals say that they are shared by the two teams
ask individuals
[
if((membership-neighbor? team teamwho) and (membership-neighbor? team team2who)) [ set numpaths (numpaths + 1) ]
]
;; ... and if the number of paths is more than the maximum allowed overlap...
if(numpaths > teamoverlap)
[
;; I take the connection away...
ask membership teamwho indwho [ die ]
;; and I reduce the actual number of team members
set actualteammembers (actualteammembers - 1)
]
]
]
]
]
end
Thank you for your precious help!
I think that the problem is likely to be in the way you refer to the link you are asking to die. membership is an undirected link and so which agent is end1 and which is end2 depends upon the order in which the agents were created. The one with the lower who number is end1 and the other is end2. So, if the team agent was created after the individual agent, that particular link would be membership indwho teamwho. In general, using who numbers at all is bad practice, but the same problem would arise if you used the team and individual agents themselves rather than their who numbers.
Someone with more experience in the use of undirected links than I might have better advice on how to easily refer to an undirected link when you don't know offhand which agent is the older, but something like
ifelse (indwho < teamwho) [
ask membership indwho teamwho [ die ]
]
[
ask membership teamwho indwho [ die ]
]
should work. Using directed links obviates this problem as the creator agent is always end1 and the target agent end2. So, if membership links always are created by the individual, you'll always know that the individual is at end1.
Hope this helps.
Charles

How to hide all link that connect to a particular breed in netlogo

Greeting,
This is the scenario:
I have 2 breed namely: "dog" and "cat"
Links are established within dog breed only and cat breed only, and temporarily there is no cross linkages.
I am looking for a way to hide dog breed and links that is associated with dog only. I would like to add that the links are not randomly generated, they are known links given to the breed. The purpose of doing it this way is that I am doing a network calculation on each breed individually using nw:mean-path-length
In netlogo documentation, I will be using
ask dog[ hide-turtle ] to hide the dog breed. However, I do not know how to hide link associated with dog only. And similarly, I will be using ask dog[ show-turtle ] to make dog breed appear again(with the links associated with dog too) and use ask cat[ hide-turtle ] for my calculation on the other breed.
Thanks for helping out!
ask dogs [ ask my-links [ hide-link ] ]
...will hide the dogs' links.
That being said, if you want to perform calculations on a separate part of a network, you will need to use nw:set-context: just hiding the turtles and links won't affect the results of primitives like nw:mean-path-length.

Confused about how "other" works in NetLogo

The netlogo docs give the following example
show count turtles-here
=> 10
show count other turtles-here
=> 9
and the documentation says the "other" command excludes "this" agent. My question is ... WHICH agent? Seems like this command can be run in the observer context and so no agent. Or at least in this example, the context could be a patch context in which case the "other" would exclude ALL the turtles? Is there some mechanism to set the context for a particular agent? Maybe:
ask agent [
show count other turtles-here
]
in which case why didn't the NetLogo code snippet include that?
The agent excluded is the agent being asked. ask, ask-concurrent, and of set the context. For example,
ask turtle 0 [ show count other turtles ]
counts all turtles except for turtle 0.
ask turtles [ show count other turtles ]
iterates over each turtle individually. In each iteration, other excludes the current turtle.
other never excludes agents of a different type. That is,
ask patch 0 0 [ show count other turtles ]
will just count all the turtles since none of the turtles are patch 0 0.
The agent of the current context can be referred to with self. The agent that other excludes will always be self. Thus,
ask agents [ show count other agents ]
is exactly equivalent to
ask agents [
let this-agent self
show count agents with [ self != this-agent ]
]
(note that this can be expressed more succinctly using myself, but since myself is way more confusing, and way worse named, than other I'm avoiding it here)
Seems like this command can be run in the observer context and so no agent.
This is in fact a bug! I've created an issue for it here: https://github.com/NetLogo/NetLogo/issues/757

Netlogo Turtles - sharing the difference between two turtles

I am fairly new to Netlogo and have been working through Railsback & Grimms excellent book while trying my own stuff on the side
Currently, I am trying to code a kind of money transfer whereby two turtles meet (e.g. Turtle 1 and Turtle 2), and the two turtles then share money (e.g. T1 has $4 and T2 has $6, they both leave with $5)
The code I have tried using is as below
to currency-share
let neighbor one-of bystanders-here ; identify neighbor around
if neighbor != nobody ;; is there a neighbor? if there is
[ set currency round ((currency + neighbor currency) / 2)] ; share money
end
Unfortunately the code isn't working and I can't find any examples through the forums which use a similar idea. Perhaps I'm searching incorrectly (using key words like turtle exchange, share etc. normally comes up with patch sharing). If anyone has any models they could recommend where such exchanges happen, or if anyone knows how I may improve my code, please let me know. Thank you.
At first glance, this seems like a simple syntax problem. To get the currency of the neighbor, you should be using of:
set currency round ((currency + [ currency ] of neighbor) / 2)
But since you also said that you want both turtles to get the new amount, you also need to add:
ask neighbor [ set currency [ currency ] of myself ]
Or, perhaps less confusingly you could do something like:
set new-amount round ((currency + [ currency ] of neighbor) / 2)
set currency new-amount
ask neighbor [ set currency new-amount ]
One last variant, slightly better in my opinion because more general (and perhaps even clearer) is:
let sharers (turtle-set self one-of bystanders-here)
let new-amount mean [ currency ] of sharers
ask sharers [ set currency new-amount ]
In this last one, you don't even need the if neighbor != nobody check because if there are no bystanders, turtle-set will build an agentset containing only self and the mean currency of that set will just be the present currency value.