Search code examples
netlogo

How to ask turtles to average the opinions of other turtles with a matching variable?


I'm trying to ask turtles in my NetLogo model to average other turtles' variables and save that average to a new variable. When I try to run my code setup, I get the following error:

MEAN expected input to be a list but got the number #### instead.

(NOTE: the #### is a random float that varies each time the model setup is run.)

The way the model runs at setup:

  1. I have a breed of districts. This breed creates different colored zones in the map.
  2. I generate turtles. They get some random opinion, and they get randomly assigned 0 or 1 for a "linked" variable. They also get passed the district id of the patch their own which comes from the district that generated the zone (and thus color of the patch).
  3. I try to run the "org-effects-mean" process, which should average the opinions of all turtles in a zone who have "linked = 1" and pass that average to the turtles' "org-influence-opinion" variable.

The org-effects-mean process is what is breaking.


The error notification is signaling I need to pass a list to mean, which I know I'm not doing with the code as presented. To clarify: I don't really know or understand how to pass a list of variable values across a set of turtles to mean in the operation org-effects-mean.

breed [ districts district ]

turtles-own [ linked mydistrict opinion org-influence-opinion ]
districts-own [ district-id district-patches district-turtles ]
patches-own [ pdistrict ]

globals [ num-districts ]

to setup
  ca
  set num-districts 4
  grow-districts
  create-turtles 100 
  ask turtles [
    setxy random-xcor random-ycor
    set linked one-of [ 0 1 ] 
    set mydistrict [pdistrict] of patch-here
    set opinion random-float 1
  ]
  org-effects-mean
end

to grow-districts ;; Barranca addition
  Create-districts num-districts
  [
    ;; set district breed shape
    set shape "arrow"
    __set-line-thickness 1
    set size .4
    ;; Set randomish location...
    setxy random-xcor random-ycor
    ;;; tag this patch
    Set pdistrict self
    ;set district-id district-id + 1
    set color (deep-color-1 who)
    set pcolor color

  ]
  ;; expand districts
  While [ any? patches with [ pdistrict = 0 ] ]
  [
    Ask patches with
    [ pdistrict != 0  and any? Neighbors with [ pdistrict = 0 ] ]
    [ Ask neighbors with
      [ pdistrict = 0 ]
      [ set pdistrict [ pdistrict] of myself
        Set pcolor [ color ] of pdistrict
      ]
    ]
  ]
end

to org-effects-mean
  ask turtles [
    if linked = 1
    [
      set org-influence-opinion ([ mean opinion ] of turtles with [mydistrict = [mydistrict] of myself and linked = 1])
    ]
  ]
end

to-report deep-color-1 [ value ]
  report (
  red + 10 * (value mod 13) ;; base netlogo color
  ;; shift up or down
  +  .5 * ( (int (value / 13 )) mod 2 * 2 - 1) ;; alternate normal -1, +1
  ;; increase difference (gets brighter and darker)
  * (1 + int ( value / ( 2 * 13 )))
  )

Solution

  • I figured out the problem: the mean aggregation needs to come before the bracketed turtles-own variable it is aggregating, so while I originally had:

    to org-effects-mean
      ask turtles [
        if linked = 1
        [
          set org-influence-opinion ([ mean opinion ] of turtles with [mydistrict = [mydistrict] of myself and linked = 1])
        ]
      ]
    end
    

    it should have been,

    to org-effects-mean
      ask turtles [
        if linked = 1
        [
          set org-influence-opinion (mean [ opinion ] of turtles with [mydistrict = [mydistrict] of myself and linked = 1])
        ]
      ]
    end