【问题标题】:In NetLogo, how to cycle through an agentset and check conditions before performing an action if true?在 NetLogo 中,如果为真,如何在执行操作之前循环通过代理集并检查条件?
【发布时间】:2022-01-03 17:12:40
【问题描述】:

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

我想从智能体集中的每个智能体中提取品种、视力、新陈代谢和当前糖平衡,使用这些信息和有关代理海龟的信息进行计算,然后在满足某些条件时吃“最好”的海龟.

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 中,我会使用计数器循环遍历代理集中的每个代理,并有一个数组存储代理的“适应度”和一些识别号,但是查看文档后,我看不出如何这样做。

任何指点将非常感谢!

【问题讨论】:

    标签: loops conditional-statements netlogo agentset


    【解决方案1】:

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

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

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

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

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

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

    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 联系呼叫代理。您可能需要两个报告器:一个报告真/假以选择可能的候选人,另一个报告价值以选择最好的候选人。

    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 [ .... ]
    

    等等。

    【讨论】:

    • 还有:注意max [ cash ] of candidates(候选人的cash属性的最大值)和max-one-of candidates [ cash ](现金属性的最大值)之间的区别。
    【解决方案2】:

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

    首先,一些注意事项:

    • 与构建candidates 列表的方式相比,使用turtles-on 是一种更有效、更清晰的选择。
    • 更仔细地检查foreach 所做的事情:它接受一个列表作为输入,并为列表中的每个项目运行一些命令。根据我从您的示例中猜到的情况,您没有考虑到candidates 列表中的每一个海龟都将执行let best-candidate max-one-of candidates with ... 等。
    • 您检查品种的部分非常不清楚并且包含错​​误,这意味着您也很难猜测您想要在那里实现什么。同样,提供问题的工作示例可以帮助其他人帮助您,并帮助您找出需要解决的问题。

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

    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
    

    【讨论】:

    • 好吧,不用担心,感谢指点
    猜你喜欢
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 2022-01-18
    • 2021-12-12
    • 2021-12-04
    • 2021-04-21
    • 2021-06-06
    相关资源
    最近更新 更多