links-demo


Information

Created with NetLogo version NetLogo 3.1.1
Running with NetLogoLite.jar version 314.



Procedures

NetLogo Version: NetLogo 3.1.1

;;;; SUMMARY
;; Nodes make and break undirected links
;;;; COPYRIGHT
;; Copyright (C) 2006 James P. Steiner
;;
;;
;;

globals [ center ]

breed [ link2s link2 ]
breed [ nodes node ] ;; last defined breed displays front-most

to startup
   setup
end
  
to setup
   ca
   set-default-shape link2s "link2"
   set-default-shape nodes "circle"
   
   ;; create 15 nodes, cause every node to link
   ;; with some other node that is not already linked
   ;; to this node.
   create-custom-nodes 15
   [ __create-link2-with
        (one-of nodes with
           [ self != myself and not __link2-neighbor? myself ]
        )
        [ ]
   ]
   ;; size nodes based on number of links
   ask nodes [ set-size-by-link-count ]
   select-new-center-node
   layoutx 100
end

to layoutx [ x ]
   repeat x
   [ __layout-spring nodes .1 5 .5 ]
end

to layout
   __layout-spring nodes .05 5 5
end

to go
   no-display
   every (3 + random-float 2) [ remove-a-random-link ]
   every (1 + random-float 2) [ make-a-new-random-connection ]
   every .1 [ update-layout ]
   every (10 + random-float 5) [ select-new-center-node ]
   
   display
end   

         
to set-size-by-link-count
   ;; a node sets its size based on the number 
   ;; of links to/from the node
   set size 1 + .25 * count __my-link2s 
end

to remove-a-random-link
   ;; pick a random link,
   ;; ask that link to resize its nodes as if
   ;; the link wasn't there...
   ;; then kill the link
   if any? link2s
   [ ask one-of link2s
     [ let e2 __end2
       ask __end1 
       [ __remove-link2-with e2 set-size-by-link-count ]
       ask __end2
       [ set-size-by-link-count ]
       die
     ]
   ]
end     

to make-a-new-random-connection
   ;; pick a random node
   ask one-of nodes
   [ ;; pick another node that is not connected to this node and has the fewest links
     let n min-one-of
              nodes with
              [ self != myself and not __link2-neighbor? myself ]
           [ count __my-link2s ]
     ;; if there is any such node, link to it, and change size
     if is-turtle? n
     [ __create-link2-with n [  ]
       set-size-by-link-count
       ask n [ set-size-by-link-count ]
     ]
   ]
end     

to update-layout
   ;; update the layout,
   ;; method:
   ;; move the "center" node toward the center
   ;; remember that position
   ;; use the auto-layout tool
   ;; restore the position of the center node
   ;; (so it is not affected by the auto-layout tool)
   ask center [ facexy 0 0 jump .1 ]
   let x xcor-of center
   let y ycor-of center
   layout 
   ask center [ setxy x y ]
end

to select-new-center-node
   ;; restore the shape of the old center
   if is-turtle? center [ ask center [ set shape "circle" ] ]
   ;; pick a new center (could pick the same one again, that's ok)
   set center one-of nodes
   ;; change the shape of the new center
   ask center [ set shape "open" ]     
end     

                    


Download Link

View or download the complete model file (to download: right-click, save-link-as):
-- Download links-demo --