如何在NetLogo中将'priorities'添加到原始'n-of'?

How to add 'priorities' to the primite 'n-of' in NetLogo?

我有 3 种颜色(黄色、绿色和红色)的补丁。绿色的具有 'rank' 值。我想要求给定数量 ('n-cells') 的黄色或绿色补丁 ('candidate-cells') 变成红色。我知道我可以使用原语 'n-of' 随机执行此操作,如下所示:

let candidate-cells ( patches with [ pcolor = yellow or pcolor = green ] )
ask n-of n-cells candidate-cells [ set pcolor red ] 

但我想为将变为红色的补丁添加优先级。首先,我希望黄色补丁(随机)变成红色,但如果在所有黄色补丁之后仍有补丁要变成红色,我希望具有最高 'rank' 值的绿色补丁变成红色,直到转动次数补丁达到 'n-cells' 数量。我认为这段代码应该可以运行到最后一行:

  let candidate-yellow-cells ( pcolor = yellow )
  let candidate-green-cells ( pcolor = green )

  ask n-of n-cells candidate-yellow-cells [ set pcolor red ]

  if n-cells > candidate-yellow-cells [
    let left-cells ( n-cells - candidate-yellow-cells )
    ask n-of left-cells candidate-green-cells [ set pcolor red ]  

但是,我再次使用 'n-of' 作为绿色单元格,我想知道如何用可以选择 'left-cells' 作为具有最高 [=22= 的绿色块的东西来替换它] 价值。提前谢谢你。

这个例子应该可以帮助您:

globals [n-cells]
patches-own [rank]
to setup
  set n-cells 500
  ask patches [
    set pcolor one-of [yellow green red]
    set rank random 3
  ]
end

to recolor
  let _yellows (patches with [pcolor = yellow])
  let _ny count _yellows
  ifelse (n-cells > _ny) [
    ask _yellows [set pcolor red]
    let _greens sort-on [(- rank)] (patches with [pcolor = green])
    let _ng length _greens
    let _n min (list (n-cells - _ny) _ng)
    ask patch-set sublist _greens 0 _n [set pcolor red]
  ][
    ask n-of n-cells _yellows [
      set pcolor red
    ]
  ]
end