简体   繁体   中英

Netlogo - show turtles components of community clusters

I have the following code to detect and color communities:

to community-detection
  nw:set-context turtles links
  color-clusters nw:louvain-communities
end

to color-clusters [ clusters ]
  ; reset all colors
  ask turtles [ set color gray - 3 ]
  ask links [ set color gray - 3 ]
  let n length clusters
  let colors ifelse-value (n <= 12)
    [ n-of n remove gray remove white base-colors ] ;; choose base colors other than white and gray
    [ n-values n [ approximate-hsb (random 255) (255) (100 + random 100) ] ] ; too many colors - pick random ones
    ; loop through the clusters and colors zipped together
    (foreach clusters colors [ [cluster cluster-color] ->
      ask cluster [ ; for each node in the cluster
        ; give the node the color of its cluster
        set color cluster-color
        ; colorize the links from the node to other nodes in the same cluster
        ; link color is slightly darker...
        ask my-links [ set color cluster-color - 1 ]
      ]
    ])
end

I want to mouse-click in a specific cluster and show the number of each one of turtles, if possible, floating to not overlap the numbers. I created a button with the following code:

to identify-turtles
  nw:set-context turtles links
  if mouse-down? and member? nw:louvain-communities ([who] of turtles) [ 
      ask turtles [ set label who ] 
  ]
end

在此处输入图像描述

But nothing happens. Any suggestions on how to improve the code ?

It is also possible I can put the turtles numbers in a monitor, whatever is more doable.

Or even an observer command to get turtles of a specific cluster.

The problem with your code is that you only check whether or not the mouse is down, but you don't check where the mouse is actually pointed. To do that, you use mouse-xcor and mouse-ycor . I wrote this quick sample code for you where i check in descending order:

  • If the mouse is down
  • If there is a turtle close to the mouse
  • Which turtles is closest to the mouse

From there on, you can start giving commands to show labels and hide labels of themselves or all turtles in the network.

to setup

  ca
  crt 5 [setxy random-xcor random-ycor]
  
end

to go ;Set as a forever button
  
  if mouse-down? [inspect-turtle]
  
end

to inspect-turtle
  
  ifelse any? turtles with [distancexy mouse-xcor mouse-ycor < 1] [
    ask min-one-of turtles [distancexy mouse-xcor mouse-ycor] [
      set label who
      ask other turtles [set label ""]
    ]
  ] [
    ask turtles [set label ""]
  ]
  
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM