Creating attribute of a breed and managing - netlogo

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?

Related

How to change the own variable of a breed to another breed in netlogo

I was trying to personB's own variable to update in personA's own list type variable. when I tried to run this code i recived an error message "PERSONB breed does not own variable LEDGER
error while personbs 1 running LEDGER, called by procedure UPDATE-LEDGER...."
I cant change the ownership(If I make it global others can alter). how can I solve this?
My code is:
breed [personAs personA]
breed [personBs personB]
personAs-own [
wallet
ledger
]
personBs-own [
packid
batch-num
]
to start
ca
create-personAs 1 [
setxy random-xcor random-ycor
set shape "person"
set ledger []
]
create-personBs 1 [
setxy random-xcor random-ycor
set shape "person"
set packid 10
set batch-num who
]
update-ledger
end
to update-ledger
ask personBs [
set ledger fput (list packid batch-num) ledger
]
end
[Before starting: please note that in NetLogo breeds are defined first in their collective form and later in their individual form, e.g. breed [cats cat] instead of breed [cat cats], so you should change your breeds declarations accordingly. I'll give my answer as if you used the correct declaration, e.g. breed [personAs personA]. Also, note that breeds in NetLogo are not sensible to lower/upper case, so in general you might also want to give more meaningful names to make it easier to read]
--
It is possible to change turtles' breed by just asking them to
set breed newbreed
This will change their breed and, accordingly, all their breed-own variables (i.e. the turtle will lose its previous breed's variables and will gain its new breed's variables).
However your problem seems to be more fundamental: why did you make ledger a personAs-own variable if you want personBs to use set ledger? This is the crucial point.
There are two possibilities here:
You want each turtle to have a ledger to operate on. In that case, ledger must not be a personAs-own variable; it has to be a turtles-own variable (or, if in your full model you have other breeds who do not need ledger, then make it both a personAs-own and personBs-own variable). In any case, no global variables involved (a global variable only has one value for everyone, so that's not what you need).
You want personBs to be able to operate on the ledger of personAs. In that case, personBs will have to ask personAs to modify their ledger; or, alternatively, some personBs can operate on the ledger of some personAs by indeed using the of reporter (see here); for example:
; Solution with 'ask':
ask one-of personBs [
ask one-of personAs [
set ledger 10000
]
]
; Solution with 'of':
ask one-of personBs [
set [ledger] of one-of personAs 10000
]
Important update
Your expression "If I make it global others can alter" suggests me that you're thinking in terms of other programming languages, where you classify variables based on the level of access to that variable that other parts of the programme have.
Note that this is not the case in NetLogo: all parts of the programme (in this case: all agents) have access (even if indirect) to every variable through ask and/or of. No matter whether a variable is turtles-own, links-own, breed-own or patches-own. Every agent can read any other agent's variables. But also: every agent can modify any other agent's variables (see the code examples that I made regarding point 2 above). Therefore, making a variable something-own simply means that those are the agents carrying that variable.

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

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.

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 Agents error

I am implementing an evacuation simulation of a lecture hall.And i have two types of students those who sit on tables and extra students who allocated randomly inside the class.So i created two sliders in order to allocate the students at the desired numbers.The sliders are named extrastudents and standarstudents. When the simulation starts i want all the students (both in tables and extras students) to go to the nearest exit ( i have two exits ) .So i implemented that for only the students that are seating down :
ask standarstudents [
ifelse pycor > 0
[ set target one-of nexits]
[ set target one-of sexits]
face target
]
Nexits is the north exit.
Sexits is the south exit.
The problem is that i get this error and i can move on :
ASK expected input to be an agent or agentset but got the number 3 instead.
error while observer running ASK.(The number 3 derives from the slider that user is pick)
called by procedure SETUP
called by Button 'setup'
org.nlogo.nvm.ArgumentTypeException: ASK expected input to be an agent or agentset but got the number 3 instead.
any ideas?
Well, if you have sliders named extrastudents and standarstudents, those two variables are numeric values representing how many students of each kind you want, not the students themselves. Like the error message says, the ask primitive works with agentsets that represent "who you are asking".
Have you actually created your students? Putting sliders on the interface won't do anything by itself. I would suggest renaming your sliders to something like num-extrastudents and num-standarstudents and then using code like this to initialize your population:
breed [ extrastudents extrastudent ]
breed [ standarstudents standarstudent ]
to setup
create-standarstudents num-standarstudents
create-extrastudents num-extrastudents
end
Then, the code you have posted would work because extrastudents and standarstudents would be breeds of turtles, and thus, agentsets.