masoud askari
masoud askari

Reputation: 11

how to access another agents variable value through links in netlogo

im trying to create a model with two agent sets. a retailer and a customer set. customer agents need to access to the price of the closest member of the retailer set and compare its price with their own preference price and make a purchase if the offered price by the retailer is lower than the preferred price. i tried to define the variable price-here for customers so i can transfer the value of price of the closest retailer to the customer and make the comparison.i wonder what syntax i should used to make this transfer? i have created a simple model here so i can get the hang of the process.

breed [ retailers retailer]
breed [ customers customer]

directed-link-breed [info-links info-link]
directed-link-breed [purchase-links purchase-link]

retailers-own [
       price
       inventory
]


customers-own [
         price-here
         prefered-price
]


to setup
  
  create-retailers 10 [ setxy random-pxcor random-pycor
                        set color blue
                        set price random 10
                        set inventory 10
  ]

create-customers 20 [ setxy  random-pxcor random-pycor 
                      set color red
                      set prefered-price random 7 ]
end


to go
  
  foreach sort customers [x -> ask x[
    create-info-link-from  min-one-of retailers [ distance myself ]
  ]]
end

Upvotes: 1

Views: 239

Answers (1)

JenB
JenB

Reputation: 17678

This is not tested so may have syntax errors. I think you want to check the price of only the closest retailer and then buy if the price is low enough. You don't need to create a link just to get information, you just need to specify the agent with the information you want and use of. So you can do something like this:

to go
  ask customers
  [ let closest min-one-of retailers [ distance myself ]
    let closest-price [price] of closest
    if closest-price <= preferred-price
    [ ...

I constructed it as separate lines so you can see what is going on. (1) Identify the closest retailer and assign that turtle to the variable named 'closest'. (2) Get the price from that retailer. (3) Do the comparison and then take action.

You can do it in one line. That would look like:

to go
  ask customers
  [ if closest-price <= [price] of min-one-of retailers [ distance myself ]
    [ ...

Upvotes: 2

Related Questions