在 NetLogo 中,如果为真,如何在执行操作之前循环遍历代理集并检查条件?

In NetLogo, how to cycle through an agentset and check conditions before performing an action if true?

我正在基于 Sugarscape 模型松散地构建一个模型来研究公司收购。我需要检查相邻乌龟的属性,对其属性进行计算,然后选择代理乌龟是否希望吃掉相邻的乌龟。我在提取相关信息时遇到了一些问题,因为我不熟悉数据结构以及如何循环访问代理集中的每个代理。

我想从代理集中的每个代理中提取品种、视力、新陈代谢和当前糖平衡,使用这些信息和有关代理龟的信息进行计算,然后吃 'best' 一个如果他们满足某些条件。

to financials-kill ;; turtle proceedure
  ;; buy another company
  let candidates []
  ask neighbors4 [
    ask turtles-here [set candidates fput self candidates]
  ]
  foreach candidates[
    let best-candidate max-one-of candidates with 

;; checking some conditions etc here and performing the calculations but unsure how to proceed

    if breed of candidates = financials[
      ; 
    ]
    if breed = healthcare [ ]
  if breed = industrials [ ] 
  print candidates 
end

在 python 中,我将使用计数器循环遍历代理集中的每个代理,并有一个数组存储代理的 'fitness' 和一些识别号,但查看了文档,我不知道该怎么做。

如有指点将不胜感激!

你的例子非常模糊,也很不清楚:鉴于我们不知道你想如何使用海龟的值,我将简单地包含一个通用案例(你应该旨在提供一个 minimal and reproducible example . 这也包括提供工作代码——当然,除了您正在寻求帮助的部分之外)。

首先,一些注意事项:

  • 使用 turtles-on 比构建 candidates 列表的方式更有效、更清晰。
  • 更仔细地检查一下 foreach 的作用:它将一个列表作为输入,并为列表中的每一项运行一些命令。根据我从你的例子中可以猜到的,你没有考虑 candidates 列表中的 every 乌龟将执行 let best-candidate max-one-of candidates with ...
  • 你检查breeds的部分很不清楚而且有错误,这意味着你也很难猜到你想在那里实现什么。同样,提供您的问题的工作示例可以帮助其他人帮助您,并帮助您确定需要解决的问题。

就是说,请参阅下面的一个完整但通用的示例,该示例说明如何实现您所说的内容(例如,根据代理海龟的变量及其目标的变量等执行一些选择)。另请参阅其中的评论:

turtles-own [
  the-value
  targets
  chosen-one
]

to setup
  clear-all
  create-turtles 100 [
    setxy random-xcor random-ycor
    set the-value random 70
  ]
end


to go
  ask turtles [
    evaluate-targets
  ]
end


to evaluate-targets
  ; Just clearing 'targets' and 'chosen-one' here, to make sure there is no weird behaviour across iterations if you try this
  ; multiple times.
  set targets (list)
  set chosen-one NOBODY
  
  ; Using 'turtles-on' is much more convenient than doing the same thing by asking patches to ask turtles to deal with lists.
  let potential-targets turtles-on neighbors4
  
  ; The 'foreach' primitive takes a list as input (this is why I use 'sort': not because I really want to sort the agents, but
  ; because it turns the 'potential-targets' agentset into a list of agents) and runs the command block for each item of the
  ; list. In this case, each agent in 'potential-targets' in turn is identified by 'pt', and the if-statement is executed taking
  ; into account that current 'pt'.
  foreach (sort potential-targets) [
    pt ->
    if ([the-value] of pt > the-value) [
      set targets (lput pt targets)
    ]
  ]
  
  ; Note that, referring to the code above, if we wanted to be brief we could simply do:
  ;   foreach (sort turtles-on neigbors4) [...]
  
  ; Now you can just do whatever you want with 'targets', including choosing one of those agents based on some criterion...
  set chosen-one max-one-of (turtle-set targets) [color]
  
  ; ...and doing something with it:
  if (is-turtle? chosen-one) [
    ask chosen-one [
      set shape "star"
    ]
  ]
end

因此,在处理“agentset”中的海龟组时使用的主要结构。许多 NetLogo 原语获取或报告代理集。一个代理集可以包含零个、一个或多个代理。

TURTLES-ON 报告者将海龟或补丁或其中之一的集合作为输入,并报告包含站在这些补丁上的海龟或与这些海龟站在相同补丁上的代理集。

WITH 运算符报告一个代理集,其中包含左侧代理集的那些成员,这些成员报告 TRUE 作为右侧表达式的结果。示例:turtles with [ color = blue ]

OF 运算符报告左侧表达式的值,由右侧的代理或代理集评估。如果是代理集,则结果是值列表。请注意,表达式实际上由代理评估——如果表达式具有 'side effects',它们将应用于该代理。

所以,通常我们只使用代理集,我们不会费心将它转换为列表。

如果我们需要一个代理列表,有一些原语可以做到这一点:

let t (turtles-on neighbors4) ;; the set of turtles standing on this
                              ;; turtle's 4 neighboring patches
[ self ] of t      ;; list of turtles in random order
sort t             ;; list of turtles sorted by WHO
sort-on [ xcor ] t ;; list sorted by value of that expression
                   ;; as evaluated by the individual turtles
sort-by ...        ;; sort using more complex criteria

因此,一般模式可能是:

ask <agentset>
[
  let candidates <another agentset> with [ <critiera> ]
  if any? candidates 
  [ let selected <some expression that selects one agent>
    ;; this agent can access the selected agent's properties using OF
    set energy energy + [ energy ] of selected
    ;; this agent can command the selected agent to do things:
    ask selected
    [ set energy 0
      ;; this agent can access the "calling" agent using MYSELF
      show (word myself " ate me.")
      die ;;
    ]
    ;; "selected" now refers to a dead turtle. Don't use it again.
 ] 

因此您的示例可能如下所示:

to financials-kill ;; turtle procedure
  ;; buy another company
  let candidates turtles-on neighbors4
  ;; make sure there are actually some candidates
  if any? candidates
  [ let best max-one-of candidates [ cash ]
    ;; take their cash
    ;; add their cash to my cash
    set cash cash + [ cash ] of best
    ;; set their cash to 0
    ask best [ set cash 0 ]
    ;; they have no cash, they are defunct, and vanish
    ask best [ die ]
   ]
end

不过,您的问题不只是想使用 cash-- 您想要执行一些评估,然后选择获得最佳结果的那个。为此,我会安排一名记者进行估值,并报告结果。您可以使用 MYSELF 引用主叫代理。您可能需要两名报告者:一名报告 true/false 给 select 可能的候选人,另一名报告 select 最好的候选人的价值。

to potential-takeover
  if breed = financials and income > [ income ] of myself
  [ report true ]
  if cash > [ debt ] of myself [ report true ]
  ;; otherwise
  report false
 end

 to-report max-value
    report (cash - debt) - [ debt ] of myself        
 end

现在你可以做类似的事情了

let candidates turtles-on neighbors4 with [ potential-takeover ]
if any? candidates
[ let target max-one-of candidates [ takeover-value ]
 ...
 ]

最后,我看到你在尝试使用品种。品种应该像代理集一样对待。所以你可以做这样的事情:

ask financials [ .... do things ]

if any? industrials-here [ .... ]

等等。