NetLogo中解决"that link is dead"错误及max-links列表含nobody问题
nobody from max-links Let's break down what's going wrong with your code and fix both issues step by step.
Why You're Seeing These Problems
"That link is dead" error: Your code is iterating over the
max-linkslist and asking links to perform actions after some of them have already been deleted. Once a link runsdie, any subsequent attempt to access its properties (likeend1ortrust) will throw this error because the link no longer exists.nobodyinmax-links: When a turtle has no outgoing links,max-one-of my-out-links [trust]returnsnobody. Your original code didn't filter these out when building thelink-set, sonobodygets added. The line you tried to fix this (if one-of max-links = nobody [...]) doesn't work becauseone-ofpicks a random element—you're not guaranteed to hit thenobodyentry, and even if you do, regeneratingmax-linkswithout filtering out turtles with no out-links will just re-addnobody.
Corrected Solution
We'll restructure the logic to avoid modifying the list while iterating over it, and explicitly filter out invalid entries from max-links:
; Step 1: Generate a clean initial max-links (filter out turtles with no outgoing links) set max-links sort-on [(- trust)] link-set [max-one-of my-out-links [trust]] of turtles with [any? my-out-links] ; Step 2: Find all reciprocal max-link pairs (A→B and B→A both in max-links) let reciprocal-pairs [] foreach max-links [ current-link -> let reverse-link link [end2] of current-link [end1] of current-link ; Only add the pair if reverse exists, is in max-links, and we haven't already added the reverse pair if reverse-link != nobody and member? reverse-link max-links and not member? (list reverse-link current-link) reciprocal-pairs [ set reciprocal-pairs lput (list current-link reverse-link) reciprocal-pairs ] ] ; Step 3: Process each reciprocal pair—delete the lower-weight link foreach reciprocal-pairs [ pair -> let link1 first pair let link2 last pair ifelse [trust] of link1 < [trust] of link2 [ ask link1 [die] ] [ ask link2 [die] ] ] ; Step 4: Regenerate max-links to remove any entries for deleted links/turtles with no out-links set max-links sort-on [(- trust)] link-set [max-one-of my-out-links [trust]] of turtles with [any? my-out-links] show max-links
Key Improvements
- No more dead link errors: We first identify all reciprocal pairs before deleting any links, so we never try to access a link that's already been removed.
- No
nobodyentries: Usingturtles with [any? my-out-links]ensures we only include turtles that actually have outgoing links, somax-one-ofnever returnsnobody. - Avoid duplicate processing: We check that we haven't already added the reverse pair to
reciprocal-pairs, so each pair is only handled once.
Quick Fix for Existing nobody Entries
If you ever need to clean up nobody from a list without regenerating it, use filter:
set max-links filter [is-link? max-links] max-links
This removes any elements that aren't valid links (like nobody).
内容的提问来源于stack exchange,提问作者mohammad tahsiri

