如何在NetLogo中提取网络邻域并统计其中链接数量?
make_ego_graph Link Counting in NetLogo Great question! I’ve tackled this exact scenario when working with NetLogo’s network tools, and it’s totally doable with a combination of NetLogo’s built-in primitives. Here’s a step-by-step breakdown to get the link count for a turtle’s ego network (just like make_ego_graph in igraph):
Step 1: Define the Ego Neighborhood
First, we need to get the set of turtles that make up the ego graph—this includes the target turtle itself plus all its direct link neighbors. For undirected networks (which matches your example), use link-neighbors:
let ego-neighbors (turtle-set target-turtle [link-neighbors] of target-turtle)
turtle-setcombines the target turtle and its neighbors into a single agent set.[link-neighbors] of target-turtlegrabs all turtles directly connected to the target via undirected links.
Step 2: Count Links Within the Neighborhood
Next, we filter all links in the model to only those where both endpoints are in the ego neighborhood. This gives us all the links in the subgraph we care about:
let ego-links links with [member? end1 ego-neighbors and member? end2 ego-neighbors] let total-links count ego-links
links with [...]filters the global link set to only include links where bothend1andend2are part of our ego neighborhood.count ego-linksgives us the total number of links in that subgraph.
Full Reporter Procedure
To make this reusable, wrap the logic into a reporter procedure that you can call for any turtle:
to-report ego-graph-link-count [target-turtle] ;; Get the ego neighborhood (target + all direct link neighbors) let ego-neighbors (turtle-set target-turtle [link-neighbors] of target-turtle) ;; Filter links to those entirely within the neighborhood let ego-links links with [member? end1 ego-neighbors and member? end2 ego-neighbors] ;; Return the total link count report count ego-links end
Example Usage
Let’s test this against your examples:
- Case 1: Target has two unconnected neighbors
If Turtle A is linked to Turtle B and Turtle C, but B and C have no link between them, callingego-graph-link-count turtle 0(assuming A is turtle 0) will return2(the links A-B and A-C). - Case 2: Target’s neighbors are connected
If B and C also have a link, the same call will return3(A-B, A-C, B-C)—exactly what you described.
You can use this reporter in observer commands or ask turtles to calculate their own ego link counts:
;; Get the count for a random turtle show ego-graph-link-count one-of turtles ;; Have every turtle store their ego link count in a variable ask turtles [ set my-ego-links ego-graph-link-count self ]
Notes for Directed Networks
If you’re working with directed links instead of undirected, adjust the neighborhood to include in-link-neighbors and out-link-neighbors depending on whether you want incoming, outgoing, or all connections. For example:
;; For all directed connections (in and out) let ego-neighbors (turtle-set target-turtle [in-link-neighbors] of target-turtle [out-link-neighbors] of target-turtle)
内容的提问来源于stack exchange,提问作者ThomasC

