ask one agent set to get a value from another agent set - netlogo

My sellers set trade_Price with this code:
ask buyers [ ask sellers [if any? buyers-here [ if seller_Price <= [buyer_Price] of myself
[ set trade_Price seller_Price + random ([buyer_Price] of myself - seller_Price) ]]]]
I want that my buyers also took the same trade_Price if they have a seller in the same patch.(if any? sellers-here). and I code it so:
ask sellers [ ask buyers [if any? sellers-here [set trade_Price ( [trade_Price] of myself )]]]
I think its wrong code because I got different trade_Price s from my agent couples.
Do you have any idea how can I set it?
Best reagrds

As far as I can tell, you're trying for something like this:
ask buyers [
let candidate one-of sellers-here
if candidate != nobody [
set trade_Price [trade_Price] of candidate
]
]
Note that there is no ask sellers around this. You only want each buyer to run this once each time through go.
Note that if there are multiple sellers on the patch, one-of sellers-here picks one randomly.

Related

How to create a friendship network between different Breeds

Dear NetLogo Specialists,
Please can you give me some advice? This is the first NetLogo Model that I have put together and the first time I have posted a query on this forum.
I am currently adapting the Wilensky Voting model from the NetLogo library.
I am interesting in creating two breeds. The first are agents who are part of an “Echo Chamber”, the second Breed are “not in Echo Chamber”.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
breed [ Echos Echo ]
breed [ NotEchos NotEcho ]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
As with the Wilensky model, I would like the agents to base their voting decision on the voting choices of their eight friends. I can set up the a network where “Echo Chamber” agents are friends with “Echo Chamber” and “not in Echo Chamber” agents are friends with “not in Echo Chamber” agents . I achieved this adapting code from JenB. The current code is below.
The limitation of this method is that friendships are not across Breeds.
Ideally, I would like a piece of code that allows me to select of the eight friends a breed has and how many are from their own breed and how many other breed. For example, I would like agents in the “Echo Chamber” to have six “Echo Chamber” agent friends and two “not in Echo Chamber” agent friends.
Any help would be much appreciated.
Many thanks for your time,
Paul
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to make-network
;; THE PURPOSE OF THIS IS TO CREATE A NETWORK WHERE EACH AGENT IN THE ECHO CHAMBER IS FRIENDS WITH
;; 8 OTHER AGENTS IN THE ECHO CHAMBER
ask NotEchos
[ let needed 8 - count my-links
if needed > 0
[ let NotEchoscandidates other NotEchos with [ count my-links < 8]
create-links-with n-of min (list needed count NotEchoscandidates) NotEchoscandidates
[ hide-link]
]
]
;; THE PURPOSE OF THIS IS TO CREATE A NETWORK WHERE EACH AGENT NOT IN THE ECHO CHAMBER IS FRIENDS WITH
;; 8 OTHER AGENTS NOT IN THE ECHO CHAMBER
ask Echos
[ let needed 8 - count my-links
if needed > 0
[ let Echoscandidates other Echos with [ count my-links < 8]
create-links-with n-of min (list needed count Echoscandidates) Echoscandidates
[ hide-link]
]
]
;; THE PURPOSE OF THESE IS TO CREATE MEASURES TO VALIDATE AND QUALITY ASSURE THE ABM
ask NotEchos [ set num_NotEchos_friends count my-links ]
ask NotEchos [ set total_NotEchos_vote (sum [vote] of link-neighbors) ]
ask Echos [ set num_Echos_friends count my-links ]
ask Echos [ set total_Echos_Vote (sum [vote] of link-neighbors) ]
ask Echos [show total_Echos_vote]
ask NotEchos [show total_NotEchos_vote]
End
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
You have NotEcho to NotEcho, Echo to Echo and Echo to NotEcho links and will need separate constructions for each. So far you are limiting your candidates to the right breed, but you haven't adjusted your counts of whether they still have slots available. You need something like (not tested):
ask NotEchos
[ let needed 6 - count my-link-neighbors with [breed = NotEchos]
if needed > 0
[ let NotEchoscandidates other NotEchos with [ count my-neighbors with [breed = NotEchos] < 6]
create-links-with n-of min (list needed count NotEchoscandidates) NotEchoscandidates
[ hide-link]
]
]
Note that I switched to my-link-neighbors instead of my-links because it's the turtles at the end that have a breed, not the links. I also changed 8 to 6 because you want 6 of the same breed.
Also, you will need to think about how many of each breed you have, so that the numbers all come out and there are sufficient to make links.
Finally, are Echo and NotEcho really different turtles? Think about two different opinion groups A and B. A talking to A is likely to be echoing similar sentiments and B talking to B as well, but A and B talking to each other are not echoing. In your model, what do you mean when a NotEcho is linked to a NotEcho. It might be that you really want two opinion groups, with preferential attachment to the same group, and then whether they are same or different at each end is a function of the link (relationship) rather than the turtle.

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?

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.

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

How to code a transaction between sellers and buyers on the same patch?

I have to code a SELLERS-BUYERS model in Netlogo. And some parts of it I have already done.
But there some troubles with calculating of "deal price" and some other processes.
The problem is that I dont know the programm language of Netlogo so well that to code all of the model.
I have to settle a deal_Price between seller and buyer and they have to be in one patch. Every seller has its ownCosts and every buyer has its ownUtility. When the seller_Price = buyer_Price they have deal_Price. And this deal_Price has to be below ownUtility of the buyer and above of ownCosts of the seller (which are in the same patch). And all of this happens in "interaction" process.
i code it so:
to interaction
ask sellers [
set seller_Price 0]
ask buyers [
set buyer_Price 0]
ask buyers [
if buyer_Price > ownUtility [
set buyer_Price buyer_Price < ownUtility ]]
ask sellers [
if seller_Price < ownCosts [
set seller_Price seller_Price > ownCosts ]]
end
For the result buyer and seller ( who are in the same patch) have to deal. And I tried to code "deal" process so:
to deal
if thera buyer and seller in one patch (its probably false)
ask buyers [
set ownUtility ownUtility + (buyer_Price - deal_Price)]
ask sellers [
set ownCosts ownCosts + (seller_Price - deal_Price) ]
end
Here have I problems, because with this codes my model has no result and it doesnt run.
Could anyone help me?
This isn't really a single question, but a bunch of questions in one. I'll try to say a few helpful things that address at least some of the issues you're facing, but in the future, it's really better if you ask specific questions and ask them separately. You'll know it's a good question if you can summarize it in a single sentence. If your summary ends up being "help me code this", it's probably not really a question.
set buyer_Price buyer_Price < ownUtility will set buyer_price to either true or false, depending on the result of the comparison. I don't think that's what you intended; you probably intended to set it to a number. I can't tell from your question what number you want, though. You write, “And this deal_Price has to be below ownUtility of the buyer and above of ownCosts of the seller”. Do you want a random number in that range, or what?
As for your deal procedure, dr_stein covered this in his or her comment, but, perhaps you want something like this:
to deal
ask patches with [any? buyers-here and any? sellers-here] [
ask buyers-here [
set ownUtility ownUtility + (buyer_Price - deal_Price)
]
ask sellers-here [
set ownCosts ownCosts + (seller_Price - deal_Price)
]
]
end
that isn't complete because it doesn't contain any code which computes deal_Price, but hopefully you can supply that part yourself.
Finally, some more general advice. You may be trying to learn too much all at once by writing a big program all at once. Write a really small program; get it working; attempt to make a very small improvement to it, and get that working; and so on. If at any point you get stuck, come here, show your code, and ask a specific question about it.