因此,在处理“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 [ .... ]
等等。