garzi
garzi

Reputation: 19

Choice based on other turtles' variables

I'm stuck with my ABM. The situation is very simple: there are some turtles, producers, and others, choosers. Producers produce a product with a certain quality, choosers choose based on perceived quality.

Perceived quality is a function "quality-of-product-of-producers - beta-of-chooser", where beta is an individual adaptive variable. All these variables are turtles-own, and they all own them (at each round roles are switched).

I'd like the following to happen: quality-of-product is a producers' variable, beta is a choosers' variable. How to code the operation?

I've tried the following things:

ask turtles with [group = "producers"] [
    set perceived-product-quality ((product-quality - beta))
  ]

However, like beta is producers' one (incorrect, should be choosers).

Then I've tried this:

ask turtles with [group = "choosers"] [
    let quality [product-quality] of turtles with [ group = "producers" ]
    set perceived-product-quality quality - beta
  ]

However, it doesn't work, runtime error: “expected input to be a number but got the list“.

How can I do it?

Upvotes: 0

Views: 83

Answers (1)

Luke C
Luke C

Reputation: 10291

I'm not sure what your exact use case is, if you're trying to query the mean product quality / beta from all producers and choosers, but the general answer I think is that you need to be explicit with which turtles you're actually trying to pull values from. When you use ask, the agent that you are asking to do something will default to using its own variables if it owns them. So, if you are needing to stick with turtles-own variables rather than making breeds and breed-specific variables, you'll need to explicitly state which turtle you're trying to reference. Here is a toy example that has choosers pick the product quality of whatever producer is nearest for use in the formula:

turtles-own [ group product-quality beta perceived-product-quality]

to setup
  ca
  ask n-of 10 patches [
    sprout 1 [
      set group one-of [ "producers" "choosers" ]
    ]
  ]
  ask turtles [
    ifelse group = "producers" [
      set product-quality random 500 + 500
      set beta random 200 + 100
      set color red
    ] [
      set product-quality random 50 + 50
      set beta random 20 + 10
      set color blue
    ] 
  ]
  reset-ticks
end

to set-perceived
  let current-producers turtles with [ group = "producers" ]
  let current-choosers turtles with [ group = "choosers" ]
  ask current-choosers [
    ; Pick the closest producer
    let closest-producer min-one-of current-producers [ distance myself ] 
    
    ; Pull the product quality of that closest producer
    let closest-product-quality [ product-quality ] of closest-producer
   
    ; Set MY perceived product quality to be closest-product-quality - MY beta
    set perceived-product-quality closest-product-quality - beta
    show ( 
      word "I used the turtle " closest-producer 
      " to determine my perceived product quality of: " perceived-product-quality 
    )
  ]
end
  

Edit:

Comment update:

I'd like the following to happen: choosers do the operation “perceived-product-quality - beta“ for all turtles in the Moore neighbour. After doing this, they are going to choose the product with highest value of perceived-product-quality.

In this version, there is a to-report procedure that reports the perceived product quality from the perspective of from-who. This can then be used with max-one-of to return the turtle with the highest perceived value.

turtles-own [ group product-quality beta perceived-product-quality]

to setup
  ca
  resize-world 0 5 0 5
  set-patch-size 40
  ask patches [
    sprout 1 [
      set group one-of [ "producers" "choosers" ]
    ]
  ]
  ask turtles [
    ifelse group = "producers" [
      set product-quality random 500 + 500
      set beta random 200 + 100
      set color red
    ] [
      set product-quality random 50 + 50
      set beta random 20 + 10
      set color blue
    ]
  ]
  reset-ticks
end

to choose-best-moore
  let current-choosers turtles with [ group = "choosers" ]
  ask current-choosers [
    let current-producers ( turtles-on neighbors ) with [ group = "producers" ]
    ifelse any? current-producers [
      let turtle-with-highest-perceived max-one-of current-producers [ perceived-target-value myself ]
      let real-product-quality [product-quality] of turtle-with-highest-perceived
      let false-product-quality [ perceived-target-value myself ] of turtle-with-highest-perceived 
      show ( word "I surveyed " count current-producers " and found that " turtle-with-highest-perceived 
        " had the best product quality (real: " real-product-quality ", perceived: " false-product-quality ")" )
    ] [
      show "I found no producers in my neighboring cells"
    ]
  ]
end

to-report perceived-target-value [ from-who ] 
  let target-product-quality product-quality
  let target-perceived-quality ( target-product-quality - [beta] of from-who )
  report target-perceived-quality
end

Upvotes: 1

Related Questions