So, the main structure to use when dealing with groups of turtles in the "agentset". Many NetLogo primitives take or report agentsets. An agentset can contain zero, one, or many agents.
The TURTLES-ON
reporter takes a turtle or patch or set of either, as input, and reports an agentset containing the turtles standing on those patches, or standing on the same patches as those turtles.
The WITH
operator reports an agentset containing those members of the agentset on the left side that report TRUE
as the result of the expression on the right side. Example: turtles with [ color = blue ]
The OF
operator reports the value of the expression on the left, as evaluated by the agent or agentset on the right. If an agentset, the result is a list of the values. Note that the expression is actually evaluated by the agent--if the expression has 'side effects' they will apply to that agent.
So, usually, we just use the agentset, we don't bother converting it to a list.
If we need a list of agents, there a a handful of primitives do to that:
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
So, a general pattern might be:
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.
]
So your example might look like this:
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
Your question, though, doesn't just want to use cash
-- you want to perform some evaluation, and pick the one that gets the best result. For that, I'd make a reporter that does the valuation, and reports the result. You can refer to the calling agent using MYSELF
. You may need two reporters: one that reports true/false to select possible candidates, and another that reports a value to select the best of those candidates.
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
now you can do something like
let candidates turtles-on neighbors4 with [ potential-takeover ]
if any? candidates
[ let target max-one-of candidates [ takeover-value ]
...
]
Last, I see you are trying to use breeds. Breeds should be treated like agentsets. So you can do things like:
ask financials [ .... do things ]
or
if any? industrials-here [ .... ]
and so on.