Netlogo列表及时更新

Netlogo list updated in time

我正在 Netlogo 中编写代码,基本上应该执行以下操作:

我的代码:

to horizontal_interact
  ask members [
    ;set random example variable for coop_b
    set coop_b random-float 5 ; coop-b stands for cooperation behavior
    if ticks > 0 [
    ask my-out-links [ ;there are directed links between all members
      set reputation_now (list [coop_b] of end2 ticks) ;list of coop_b and encounter time
      set reputation_h lput reputation_now reputation_h ; history of reputations, a list of all reputation_now recorded
      foreach reputation_h [ x ->

        let cooperative_behavior item 0 x
        let encounter_time item 1 x

        let reputation_now_w (list cooperative_behavior encounter_time (encounter_time / ticks ))


       ]
    ]
   ]
  ]

end

如果我用 2 个成员测试 reputation_h 和 reputation_h_w 的内容,我得到:

reputation_h是成员的coop_b变量和遇到的tick

links> show reputation_h
(link 1 0): 
[[4.0900840358972825 1] 
[0.8885953841506328 2] 
[0.47017368072392984 3]]

(link 0 1): [[3.6805257472366164 1] 
[3.6805257472366164 2] 
[3.4201458793705326 3]]

reputation_h_w(包含成员的coop_b变量,遭遇时间和遭遇时间除以ticks):

links> show reputation_h_w

(link 0 1): [[3.6805257472366164 1 1] 
[3.6805257472366164 1 0.5] 
[3.6805257472366164 2 1] 
[3.6805257472366164 1 0.3333333333333333] 
[3.6805257472366164 2 0.6666666666666666] 
[3.4201458793705326 3 1]]

(link 1 0): [[4.0900840358972825 1 1] 
[4.0900840358972825 1 0.5] 
[0.8885953841506328 2 1] 
[4.0900840358972825 1 0.3333333333333333] 
[0.8885953841506328 2 0.6666666666666666] 
[0.47017368072392984 3 1]]

问题是 reputation_h_w 对我来说没有意义 - 首先有六个输入而不是三个,其次,遇到时间(第 1 项)和遇到 time/ticks(项目 2) 已关闭。

我做错了什么?

不确定您在代码中更新 reputation_h_w 的位置,但我猜您不会在 运行 再次 foreach 循环之前将其重置为空白列表。所以,它是 lput-ing 列表末尾的值,它不再是空白了。

示例设置:

breed [ as a ]
as-own [ coop_b ]
links-own [ reputation_now reputation_history reputation_history_w]

to setup
  ca
  create-as 2 [
    set coop_b who + 1
    setxy random-pxcor random-pycor
  ]
  while [ any? as with [ not any? my-in-links ] ] [
    ask one-of as with [ not any? my-out-links ] [
      create-link-to one-of other as with [ not any? my-in-links ] [
        set reputation_now []
        set reputation_history []
      ]
    ]  
  ]
  reset-ticks
end

请注意,我将在 foreach 块 运行s:

之前 set reputation_history []
to interact 
  if ticks > 0 [
    ask links [
      set reputation_now ( list [coop_b] of end2 ticks )
      set reputation_history lput reputation_now reputation_history

      ; reset reputation history to a blank list, as you are
      ; recalculating the weighted value at each tick
      set reputation_history_w []
      foreach reputation_history [ x ->
        let behavior item 0 x
        let encounter_time item 1 x
        let fraction encounter_time / ticks
        set reputation_history_w lput ( 
          list behavior encounter_time fraction ) reputation_history_w
      ]       
      show ( word "Current reputation: " reputation_now )
      show ( word "Reputation history: " reputation_history )
      show ( word "Weighted history rep list: " reputation_history_w )
    ]  
  ]
  tick
end

至于为什么你的滴答声关闭,我猜是因为你在 tick after 你 运行 你的 horizontal_interact 程序。对于上面的示例,我的输出如下所示:

(link 0 1): "Current reputation: [2 2]"
(link 0 1): "Reputation history: [[2 1] [2 2]]"
(link 0 1): "Weighted history rep list: [[2 1 0.5] [2 2 1]]"
(link 1 0): "Current reputation: [1 2]"
(link 1 0): "Reputation history: [[1 1] [1 2]]"
(link 1 0): "Weighted history rep list: [[1 1 0.5] [1 2 1]]"

即使刻度为 3。如果您在程序开始时用 tick 运行 它,可能会得到您预期的输出。