why the error you can not use it as an observer context showed up?

218 Views Asked by At

I need your support as I'm using the (interact) command as a button it it gives me an error ( you can not use it as an observer context), as I need to make interaction in area = 1 among agents.the setup button as I defined 5 areas with agents with different colors, and I want them to move to area = 1 and interact as per their culture features

This is the code

breed [ parents   parent ]
breed [ childrens children ]
patches-own [ area]
turtles-own [ my_area culture   ]


to setup
  ca
   define_areas
  create_parent
  create_children
 ask turtles [ set culture [] ]
  ask turtles [ set my_area [area] of patch-here ] 

  repeat cultural_features
  [ ask turtles [ set culture fput  ( random traits_per_feature + 1 ) culture ] ]


reset-ticks
end


to define_areas

  ask patches with [ (pxcor > -3) and (pxcor < 3) and (pycor > -3) and (pycor < 3) ] [  set pcolor white set area 1  ]
  ask patches with [ (pxcor > 5 ) and (pxcor < 16 ) and (pycor > 4) and (pycor < 16) ] [  set pcolor white set area 2 ]
  ask patches with [ (pxcor < -5 ) and (pxcor > -16 ) and (pycor > 4) and (pycor < 16) ] [  set pcolor white set area 3 ]
   ask patches with [ (pxcor < -5 ) and (pxcor > -16 ) and (pycor < -4) and (pycor > -16) ] [  set pcolor white set area 4 ]
  ask patches with [ (pxcor > 5 ) and (pxcor < 16 ) and (pycor < -4) and (pycor > -16) ] [  set pcolor white set area 5 ]

end


to create_parent

  ask n-of Population  patches with [  ( area = 2 ) ] [ sprout  1  [ set shape "person" set color green set size 1.5   ]  ]
  ask n-of Population  patches with [  ( area = 3 ) ] [ sprout  1 [ set shape "person" set color red set size 1.5 ]  ]
  ask n-of Population  patches with [  ( area = 4 ) ] [ sprout  1 [ set shape "person" set color blue  set size 1.5 ] ]
  ask n-of Population  patches with [  ( area = 5 ) ] [ sprout  1  [ set shape "person" set color grey set size 1.5 ] ]

end

to create_children

  ask n-of ( kids * ( Population / 2 ) ) patches with [  ( area = 2 ) ] [ sprout  1  [ set shape "person" set color green set size .5   ]  ]
  ask n-of ( kids * ( Population / 2 ) )  patches with [  ( area = 3 ) ] [ sprout  1 [ set shape "person" set color red set size .5 ]  ]
  ask n-of ( kids * ( Population / 2 ) ) patches with [  ( area = 4 ) ] [ sprout  1 [ set shape "person" set color blue  set size .5 ] ]
  ask n-of ( kids * ( Population / 2 ) )  patches with [  ( area = 5 ) ] [ sprout  1  [ set shape "person" set color grey set size .5 ] ]

end

to move_in_event
  ask n-of random  ( (count turtles with [ size = .5  ]) / 2 )  turtles with [ size = .5 ] 
  [ move-to one-of patches with [ (not any? other turtles-here) and ( area = 1 ) ] ]
end
to interact

  ; identifing the agent chosen
  let selected_agent one-of  turtles with  [ area = 1  ]
  print ( word "selected agent="" " selected_agent)

  ;; culture of selected_agent
  let my_culture culture
  print ( word "selected agent culture ="" "my_culture)

  ;; the agent chooses one neighbor
  let chosen one-of neighbors with  [ area = 1  ]
  print ( word "chosen agent ="" "chosen)

  ;; identify the culture of her neighbor
  let chosen_culture [culture] of chosen
  print ( word "chosen agent culture ="" "chosen_culture)

  ; Creating a local variable to track the number of similarities between the
  ;agents and calculate the probabilities of interactions between two agents
  let similarities 0

  let position_different_traits []

  ; create a list from 0  to number of cultural features
  let N_traits n-values  cultural_features [ i -> i ]
  print ( word "Number of traits to be compared ="" "N_traits)

  ; For each traits of the cultural features
  foreach N_traits
  [ i -> ifelse ( (item i my_culture) = (item i chosen_culture ))
    [ set similarities similarities + 1 ]
    [ set position_different_traits lput i position_different_traits ]   ]

  ;; print out some information to check how does the procedure performs
  print ( word "position_different_traits ="" "position_different_traits)
  print ( word "number of similarities ="" "similarities)

  ; Calculate probability
  let p ( similarities / cultural_features) * 100

  print ( word "probability ="" "p)

  ; Calculate a random number between 1 & 100
  let dice random 100 + 1

  print ( word "Roll a die ="" "dice)


  if (dice <= p)
  [

    ifelse(similarities != cultural_features)
    [
      let position_  one-of position_different_traits
      print ( word "position_trait_to_be_changed in the agent="" "position_)


      let replacement_item item position_ chosen_culture
      print ( word "element to from chosen agent to be replaced in active agent culture="" "replacement_item)

      set culture replace-item position_  culture  replacement_item
      print ( word "active agent culture updated ="" "culture)
    ]
    [

    ]
  ]

  ; only for visualization purposes
  ask selected_agent [  set color [ color ] of chosen]
  ask chosen [  set color [ color ] of selected_agent ]

end
1

There are 1 best solutions below

3
On BEST ANSWER

You are using the button to run the interact procedure. Here is the beginning of that procedure with comments and printing deleted.

to interact
  let selected_agent one-of  turtles with  [ area = 1  ]
  let my_culture culture

The first thing this procedure does is randomly select a turtle and give it the label 'selected_agent'. Then the next line says let my_culture culture but it doesn't say which agent's value of the variable 'culture' to use. Since 'culture' is an agent variable, the only way this line makes sense if it runs from the context of the agent, but you are running from the context of the observer. That's why you get the error.

The way to fix it is to specify which agent's value of culture you want to assign.

let my_culture [culture] of selected_agent