首页 > 解决方案 > 如何在 Netlogo 中访问以前的海龟交互?

问题描述

我正在编写一个模型,其中代理相互交互,他们要么合作,要么背叛。然后他们根据他们的互动更新他们与合作伙伴的合作水平,我设法实施如下:

to random-matching ; randomly match agents
  while [not matched?] [ ; check that I have not been matched yet
    set partner one-of agents with [not matched?] ; select one agent from those who have not been matched yet
    if (partner != nobody and [myID] of partner != myID) [ ; if there is still someone who has been matched and who is not myself
      set matched? true ; then record that I have been matched
      set color white
      set partner-ID [myID] of partner ; save the ID of my partner for the future
      ask partner [ ; and do the same for my partner
        set matched? true
        set color white
        set partner-ID [myID] of myself
        set partner myself
      ]
    ]
  ]
  
  
end

; access the cooperation level of the agent toward the partner
to-report coop-agent-partner
  let coop-agent table:get cooperation-matrix myID  
  let coop table:get coop-agent partner-ID
  report coop
end

; access the cooperation level of the partner toward the agent
to-report coop-partner-agent
  let coop-partner table:get cooperation-matrix partner-ID
  let coop table:get coop-partner myID
  report coop
end
  


to interact ; define whether the interaction is a cooperation or not
  ; check that I did not interact already with my partner
  if not interacted? [
    
  ; let's check what I do
    let j random-float 1 * 100
    let coop-% cooperation-level + coop-agent-partner
    ifelse (j <= coop-%) [
      set action-self "C"
      ask partner [set action-partner "C" ]
    ][
      set action-self "D"
      ask partner [set action-partner "D" ]
    ]
    
  ; and what my partner does
    let k random-float 1 * 100
    let coop-partner-% [cooperation-level] of partner + coop-partner-agent
    
    ifelse (j <= coop-partner-%) [
      ask partner [set action-self "C" ]
      set action-partner "C"
    ][
      ask partner [set action-self "D" ]
      set action-partner "D"
    ]
    
  ;save the outcome of the interaction
    ifelse (action-self = "C" and action-partner = "C")[
      set interaction-outcome "C"
      set current-goal (current-goal + 1)
    ]
    [
      set interaction-outcome "D"
    ]  
  ]
end

; update the agents cooperation according to the current interaction

to update-cooperation
  
  if not updated-coop? [
    ifelse (action-partner = "C")[ ; if the partner cooperated
      let coop-agent table:get cooperation-matrix myID  
      let coop-a-p table:get coop-agent partner-ID
      set coop-a-p (coop-a-p / 2) + update_cooperation_direct
      
      if debug-int? [print coop-a-p]
    ][
      let coop-agent table:get cooperation-matrix myID  
      let coop-a-p table:get coop-agent partner-ID
      set coop-a-p (coop-a-p / 2) - update_cooperation_direct
      
    ]
    ifelse (action-self = "C")[
      ask partner[ ; if the interaction was a cooperation
        let coop-agent table:get cooperation-matrix myID  
        let coop-a-p table:get coop-agent partner-ID
        set coop-a-p (coop-a-p / 2) + update_cooperation_direct
      
      ]
    ][
      ask partner [
        let coop-agent table:get cooperation-matrix myID  
        let coop-a-p table:get coop-agent partner-ID
        set coop-a-p (coop-a-p / 2) - update_cooperation_direct
        
      ]
    ]
    
    ; record that both the agent and the partner have updated their cooperation score
    
    set updated-coop? true
    ask partner [set updated-coop? true]
  
  ]

end

我现在需要代理将他们的交互和他们的合作伙伴 ID 存储在一个变量中,以跟踪何时发生,所以我需要为每个代理存储交互发生的时间(tick),他们与谁交互(合作伙伴- ID)和合作伙伴做了什么(行动合作伙伴)。有没有简单的方法可以做到这一点?我想我到目前为止写的代码已经过于复杂了,我想寻求一些帮助。我刚开始在 Netlogo 中编码,我正在尝试从 Matlab 翻译我的代码,它被实现为一个宽度等于交互发生的圆的矩阵。

任何帮助将不胜感激,谢谢:)

标签: timenetlogohistoryinteraction

解决方案


推荐阅读