【问题标题】:Netlogo list updated in timeNetlogo列表及时更新
【发布时间】:2018-05-23 01:48:51
【问题描述】:

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

  • 在定向链接之间进行交互并找出它们的合作行为 (coop_b)。
  • 将 coop_b 与交互时间 (reputation_now) 一起存储在列表变量中
  • 每次互动,将reputation_now 添加到更大的列表reportation_h(声誉历史)
    • 现在,为声誉添加时间权重,以便最近进行的交互在总声誉中的权重更大。我通过将交互的遭遇时间除以当前时间滴答来做到这一点,然后将其与 coop_b 相乘以检索每个交互的加权声誉。这存储在列表reputation_h_w(历史声誉加权)中。问题是,每次成员交互时都应该更新此列表,以便之前添加到列表中的内容现在更新为新的时间刻度。我的预感是我的代码在这方面陷入了迷雾(问题描述在代码部分下方)。

我的代码:

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 变量和遭遇的滴答声

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变量,遭遇时间和遭遇时间除以滴答声):

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 项)和遭遇时间/滴答声(第 2 项)是关闭的。

我在这里做错了什么?

【问题讨论】:

    标签: list time foreach netlogo


    【解决方案1】:

    不确定您在代码中的何处更新 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 块运行之前 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
    

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

    (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 运行它,这可能会整理出您的预期输出。

    【讨论】:

    • 非常感谢,我认为这与不可变的 netlogo 列表有关,但我会尝试一下!蜱虫绝对是个好建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    相关资源
    最近更新 更多