从 Netlogo 中的列表中提取值

问题描述 投票:0回答:1

我有一个模型,其中斑块具有“栖息地”值,并且我有一个动物选择比率的 csv 文件。我的目标是让每个特工在下一步中调查栖息地,以决定搬到哪里。

代码如下

to move_females_test
  let this_step (one-of step_length_lsf / patch-length)
  let available [habitat] of patches in-radius this_step
  let unique-habitats remove-duplicates available
  print available
  print unique-habitats

  ifelse length unique-habitats = 1 [
    ; If there is only one unique habitat, move as per the existing code
    set heading (one-of turning_angles_lsf * 100)
    fd this_step
  ] []
  
end 

因此,海龟每次蜱虫都会生成这样的东西

[Pastures Pastures Pastures Pastures Pastures]

在这种情况下,无需做出决定 - 因此它们会随机移动。或者类似的东西

[Pastures Pastures Pastures Scrub Arable]

在这种情况下,我希望他们从名为

selection
的列表中绘制,我已经初始化并填充了栖息地选择模型中的值。作为示例,该列表如下所示。

[[Pastures 1] [Pastures 1] [Arable 2.485988192] [Scrub 0.684336963] [Arable 0.994063271] [Pastures 1.009595509] 

我想做的 - 是将

unique-habitats
列表中出现的栖息地与
selection
列表中随机选择的匹配栖息地进行匹配,然后选择最高值来决定去哪里。

即伪代码中。

to move_females_test
      let this_step (one-of step_length_lsf / patch-length)
      let available [habitat] of patches in-radius this_step
      let unique-habitats remove-duplicates available
      print available
      print unique-habitats
    
      ifelse length unique-habitats = 1 [
        ; If there is only one unique habitat, move as per the existing code
        set heading (one-of turning_angles_lsf * 100)
        fd this_step
      ] 
[let ratios [foreach habitat in unique-habitats [select one-of habitat in select]
 ;ratio will look like this 
 ;[[Pastures 1] [Arable 0.91]]
 let sorted-ratios sort-by [[- item 1 ?] of ?] ratios
 let highest-ratio first sorted-ratios
 let target-patch one-of patches with [habitat = item 0 highest-ratio] 
 set heading target-patch
 fd this_step
]
      
    end 

如有任何帮助,我们将不胜感激

list netlogo agent-based-modeling
1个回答
0
投票

我总是发现在 NetLogo 中处理列表中的列表有点令人讨厌。特别是在它们包含两种不同变量类型的情况下。但我想我找到了解决你问题的方法。不确定这是否是最有效/最简洁的,但是......

to move_females_test
  let this_step (one-of step_length_lsf / patch-length)
  let available [habitat] of patches in-radius this_step
  let unique-habitats remove-duplicates available
  print available
  print unique-habitats
  
  ifelse length unique-habitats = 1 [
    ; If there is only one unique habitat, move as per the existing code
    set heading (one-of turning_angles_lsf * 100)
    fd this_step
  ][
    let best-habitat best-habitat-REP selection unique-habitats
    let target-patch one-of patches in-radius this_step with [habitat = best-habitat]
    set heading target-patch
    fd this_step   
end

to-report best-habitat-REP [selection unique-habitats]
  ; initallize sperate list for selection habitats and their values
  let selection-habitats []  
  let selection-values []

  ; extract habitats and their values from selection into these variables
  foreach selection [
    hab -> 
    set selection-habitats lput item 0 hab selection-habitats
    set selection-values lput item 1 hab selection-values    
  ]
  
  ; initalize a list for the randomly selected habitats from the selection
  let selected-habitats []
  
  ; for each unique habitat
  foreach unique-habitats [
   uhab -> 
    ; we determine the index of all items of the selection that matches (see all-items-REP function below)
    let idx-uhab-in-selection all-items-REP selection-habitats uhab
    ; if at least one entry of the unique habitat exists in the selection
    if not empty? idx-uhab-in-selection [
      ; we randomly pick one from all the matching entries
      set selected-habitats lput one-of idx-uhab-in-selection selected-habitats
    ]    
  ]
  
  ; initialize a list for the values of the selected habitats
  let selected-values []
  
  ; extract the values of the selected habitats from the selection
  foreach selected-habitats [
    current-habitat ->
     set selected-values lput item current-habitat selection-values selected-values
  ]
  
  ; from all these selected values we find the maximum
  let idx-max-value position max selected-values selected-values
  ; and find the according index of this value in the selection
  let final-idx-in-selection item idx-max-value selected-habitats
  
  report item 0 item final-idx-in-selection selection
end

;; Helper function to find all elements that match (the item reporter only reports the
;;  first matching value)
to-report all-items-REP [list-to-search item-to-find]
  ; initialize counter
  let i 0
  ; initialize list with indices
  let idx-item []
  ; loop through list
  foreach list-to-search [
    current-list-item -> 
    ; check if value matches, and append its index if it matches
     if current-list-item = item-to-find [set idx-item lput i idx-item]
     set i i + 1
  ]
  report idx-item
end  
© www.soinside.com 2019 - 2024. All rights reserved.