Netlogo列表及时更新

时间:2017-12-08 19:10:31

标签: list time foreach netlogo

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

  • 在有向链接中,互动并寻找他们的合作行为(coop_b)。
  • 将coop_b存储在列表变量中以及交互时间(reputation_now)
  • 每次互动,将Reputation_now添加到更大的列表,Reputation_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]]

问题在于,对于我来说,声誉_h_w没有意义 - 首先是六个输入而不是三个,其次,遇到时间(第1项)和遭遇时间/刻度(第2项)是关闭的。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

不确定您在代码中更新/(\d)(?=(\d{3})+(?!\d))/g, "$1,"的位置,但我猜您在再次运行reputation_h_w循环之前没有将其重置为空白列表。所以,它是foreach - 列表末尾的值,不再是空白。

示例设置:

lput

请注意,我会在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 块运行之前set reputation_history []

foreach

至于为什么你的蜱被关闭,我猜是因为你在运行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 。通过上面的示例,我的输出如下:

horizontal_interact

即使刻度读数为3.如果在程序开始时使用(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]]" 运行它,则可能会对您的预期输出进行排序。