为什么会出现不能将其用作观察者上下文的错误?

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

我需要你的支持,因为我将 (interact) 命令用作按钮,它给我一个错误(你不能将它用作观察者上下文),因为我需要在 area = 1 中进行交互在 agents.the 设置按钮中,我用不同颜色的代理定义了 5 个区域,我希望他们移动到区域 = 1 并根据他们的文化特征进行交互

这是代码

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

您正在使用按钮 运行 交互程序。这是该过程的开头,删除了注释和打印。

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

这个过程做的第一件事是随机 select 一只乌龟并给它标签 'selected_agent'。然后下一行说 let my_culture culture 但它没有说变量 'culture' 使用哪个代理的值。由于 'culture' 是一个代理变量,如果它 运行 来自代理的上下文,但你 运行ning 来自观察者的上下文,那么这条线唯一有意义的方法。这就是您收到错误的原因。

修复它的方法是指定要分配给哪个代理人的文化值。

let my_culture [culture] of selected_agent