Netlogo列表及时更新 [英] Netlogo list updated in time

查看:191
本文介绍了Netlogo列表及时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Netlogo写了一个代码,基本上应该这样做:


  • 在定向链接中,交互并找出它们合作行为(coop_b)。
  • 将coop_b与交互时间(reputation_now)一起存储在列表变量中。
  • 每次交互都添加reputation_now到一个更大的名单,声誉_h(声誉历史)


    • 现在,添加一个时间重量的声誉,以便最近有更多的交互权重总声望。我通过将交互的相遇时间除以当前时间滴答,然后将其与coop_b相乘来检索每个交互的加权信誉。这是存储在名单reputation_h_w(历史声誉加权)。问题是,每次成员交互时都应该更新这个列表,以便将更早的列表添加到新的时间记录中。我的预感是这里是我的代码在雾中(问题描述下面的代码部分)。



    我的代码:

     到horizo​​ntal_interact 
    问成员[
    ;设置随机的例子变量为coop_b
    set coop_b random-float 5; coop-b代表合作行为
    如果ticks> 0 [
    ask my-out-links [;所有成员之间有定向链接
    set end_ tick(end2 ticks的列表[coop_b]); coop_b和遇到时间列表
    set reputation_h lput reputation_now reputation_h;声誉历史,所有声誉记录的列表记录
    foreach reputation_h [x - >

    让cooperative_behavior项目0 x
    让encounter_time项目1 x

    let reputation_now_w(列表cooperative_behavior encounter_time(encounter_time / ticks))









    $ $
    $ b

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

    reputation_h是成员的coop_b变量,tick相遇

     链接> 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]]



     链接>包含会员的coop_b变量,遭遇时间和遭遇时间除以滴答声。 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 ,但是我猜测在运行的foreach再次循环。所以,它是 lput - 在列表末尾的值,不是空白的。



    设置:

      breed [as a] 
    as-own [coop_b]
    links-own [reputation_now声望历史声望历史_w]

    设置
    ca
    创建为2 [
    设置coop_b谁+ 1
    setxy随机-pxcor随机-pycor
    ]
    而[any?与[不是吗? my-in-links]] [
    问一个跟[不是吗? my-out-links] [
    create-link-to-one- my-in-links] [
    set reputation_now []
    set reputation_history []
    ]
    ]
    ]
    重设记号
    结束

    请注意,在这里我会 set reputation_history [] 正好在 foreach 块运行之前:

     交互
    如果蜱> 0 [
    请求链接[
    set end_ tick(列表[coop_b] end2 ticks)
    set声誉历史输出声望声望历史

    ;重置名誉历史到一个空白列表,因为你是
    ;重新计算每个时间点的加权值
    set reputation_history_w []
    foreach reputation_history [x - >
    让行为项目0 x
    让encounter_time项目1 x
    让分数encounter_time / ticks
    set reputation_history_w输入(
    列表行为encounter_time分数)reputation_history_w
    ]
    show(word当前声望:reputation_now)
    显示(单词声望历史:reputation_history)
    显示(单词Weighted history rep list:reputation_history_w)
    ]
    ]
    tick
    end

    至于为什么你的蜱关闭,我想这是因为你运行 horizo​​ntal_interact 之后调用 tick 程序。用上面的例子,我的输出如下所示:

     (link 0 1):当前信誉:[2 2]
    (link 0 1):声誉记录:[[2 1] [2 2]]
    (link 0 1):加权历史记录列表:[[2 1 0.5] [2 2 1 ]
    (link 1 0):当前声望:[1 2]
    (link 1 0):声望记录:[[1 1] [1 2]]
    (link 1 0):加权历史代表清单:[[1 1 0.5] [1 2 1]]

    ,即使ticks读取为3.如果在过程开始时使用 tick 运行它,可能会整理您的预期输出。


    I'm writing up a code in Netlogo that basically should do the following:

    • Amongst directed links, interact and seek out their cooperative behavior (coop_b).
    • Store coop_b in a list variable together with the time of the interaction (reputation_now)
    • Every interaction, add the reputation_now to a bigger list, reputation_h (reputation history)
      • Now, add a time-weight to the reputation, so that the more recently had interactions weigh more in the total reputation. I do this by dividing the encounter time of an interaction by the current time tick, then multiplying that with the coop_b to retrieve a weighted reputation for each interaction. This is stored in the list reputation_h_w (historic reputations weighted). The thing is, this list should be updated every time the members interact, so that earlier additions to the list are now updated to the new time tick. My hunch is this is where my code goes in the mist (problems depicted below the code section).

    My code:

    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
    

    If I test the content of reputation_h and reputation_h_w with 2 members, I get:

    reputation_h is the coop_b variable of the member and the tick of encounter

    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 (containing the member's coop_b variable, the encounter time and the encounter time divided by the 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]]
    

    The problem is that reputation_h_w doesn't make sense to me - firstly there's six inputs instead of three, and secondly, the encounter time (item 1) and the encounter time/ticks (item 2) is off.

    What am I doing wrong here?

    解决方案

    Not sure where you update reputation_h_w in your code, but I'm guessing that you are not resetting it to a blank list before running your foreach loop again. So, it's lput-ing the values at the end of the list, which is not blank anymore.

    Example setup:

    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
    

    Note that here I will set reputation_history [] right before the foreach chunk runs:

    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
    

    As far as why your ticks are off, I'd guess it's because you are calling tick after you run your horizontal_interact procedure. With the example above, my output looks like:

    (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]]"
    

    even though the ticks read 3. If you run it with tick at the start of the procedure, that might sort out your expected output.

    这篇关于Netlogo列表及时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆