Random Boolean Network

No preview image

1 collaborator

Default-person David Weintrop (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0RC3 • Viewed 559 times • Downloaded 35 times • Run 0 times
Download the 'Random Boolean Network' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

breed [nodes node]

nodes-own [
  state ; 0 or 1
  rule ; this number of the rule to follow (between 0 and 255)
  input-nodes ; this is the nodes whose states I take as input
  community ; used for finding components
  visits ; how frequency I am visited during strong component calculation
]

to setup
  __clear-all-and-reset-ticks
  set-default-shape nodes "circle"
  create-nodes 9 [
    set label who + 1
    set state random 2
    set color item state [red blue]

;    to decide a random set of rules
;    set rule random 256
;    set links-to []
;    repeat 3 [
;      set input-nodes lput random 8 input-nodes
;    ]

;   to visualize links
;    foreach links-to [
;      if not (who = ?)
;      [ create-link-with node ? ]
;    ]
  ]
  
;  setup hard coded rules and links for node
;  note - this was initially decided using the above commented out lines
  ask node 0 [ set rule 92 set input-nodes [3 7 0] ]
  ask node 1 [ set rule 229 set input-nodes [0 6 7] ]
  ask node 2 [ set rule 70 set input-nodes [3 6 5] ]
  ask node 3 [ set rule 160 set input-nodes [4 8 1] ]
  ask node 4 [ set rule 75 set input-nodes [7 1 1] ]
  ask node 5 [ set rule 107 set input-nodes [0 3 1] ]
  ask node 6 [ set rule 245 set input-nodes [7 3 2] ]
  ask node 7 [ set rule 105 set input-nodes [3 5 2] ]
  ask node 8 [ set rule 11 set input-nodes [5 0 8] ]          
  
  layout-circle (sort nodes) (max-pxcor - 1)
  reset-ticks
end 

; iterates through every possible state and figures out what state it will transistion to

to map-out-network
  let future-network-state []
  let state-iter 0
  let c 0
  let my-inputs []
  let element-of-rule-to-follow 0
  
  ; for each possible state in the network
  while [ state-iter < 2 ^ count nodes ] [
    
    ; set each nodes state to match state-iter
    ask nodes [ set state item who to-binary state-iter ]
    
    set future-network-state []
    set c 0
    
    ;calculate next state based on current configuration
    ; ask each node in order, what it's next state will be
    while [ c < count nodes ] [
      set my-inputs []
      ask node c [
        ; figure out which element of my rule to follow
        set my-inputs lput [state] of node (item 0 input-nodes) my-inputs
        set my-inputs lput [state] of node (item 1 input-nodes) my-inputs
        set my-inputs lput [state] of node (item 2 input-nodes) my-inputs
        
        ; I know know which element of my rule to follow
        set element-of-rule-to-follow to-decimal my-inputs
        
        ; add future state for this node to array for future network state
        set future-network-state lput item element-of-rule-to-follow to-binary rule future-network-state
      ]
      set c c + 1
    ]
;    show (se "from " state-iter " to: " to-decimal future-network-state)    
    
    ; write out the current state and future state
    ; this file can be read in later
    file-close
    file-open "Final_3_P3_network.txt"
 
    file-write state-iter
    file-write to-decimal future-network-state
    file-print " "
  
    file-close
    set state-iter state-iter + 1
  ]
end 

; helper function, takes in a number and returns an array of the binary version ex: 5 -> [1 0 1]

to-report to-binary [in-number]
  let i 10
  let binary []
  ; set each node to be 1 or 0
  while [i >= 0] [
    ifelse in-number >= 2 ^ i
    [
      set binary fput 1 binary
      set in-number in-number - 2 ^ i
    ]
    [
      set binary fput 0 binary
    ]
    set i i - 1
  ]
  report binary
end 

; helper function, takes in a binary array and returns the number ex: [1 0 1] -> 5

to-report to-decimal [binary]
  let i 0
  let num 0
  while [ i < length binary] [
    if 1 = item i binary 
    [ set num num + 2 ^ i ]
    set i i + 1
  ]
  report num
end 

to load-network
  clear-all
  create-nodes 512 [
    set color red
    set community 0
    set size .5
  ]
  
  layout-circle (sort nodes) (max-pxcor - 1)
  
  file-open "Final_3_P3_network.txt"
  ;; Read in all the data in the file
  while [not file-at-end?]
  [
    ;; this reads a single line into a three-item list
    let items read-from-string (word "[" file-read-line "]")

    ask node item 0 items
    [ create-link-to node item 1 items ]      
  ]
  file-close
end 

; control logic to find strong components

to find-strong-components
;  reset network
  ask nodes [
    set color white
    set community 0
    set visits 0
  ]
  
  let community-id 1
  while [any? nodes with [community = 0] ]
  [
    ask one-of nodes with [community = 0] [
      ;first node is definately in the network, so set visits to 2 to ensure it is included
      set visits 1
      
      find-component community-id
    ]
   
   ; look at every visited node, if visited more than 1, it is part of a strong component
   ask nodes with [community = community-id] [
     ;; if only visited once, you're a weak connection, reset state
     if visits <= 1 [
       set visits 0
       set community 0
     ]
   ]
   if count nodes with [community = community-id] > 1 [
     let com-color random 256
     ask nodes with [community = community-id] [ set color com-color]
   ]
   set community-id community-id + 1
  ]
  display-component-info
end 

; recursively visit neighbors at least once

to find-component [community-id]
  ifelse visits > 2
  [set visits visits + 1 ] ; i've seen this node before, mark the second visit and stop to prevent infinite loops
  [
    set visits visits + 1
    
    ;visit neighbors in your component or in no component
    if  community = 0 or community = community-id [
      ;;set color item community-id base-colors
      set community community-id
      ask out-link-neighbors [find-component community-id]
    ]
  ]
end 

; this outputs info about the components to the console

to display-component-info
  let community-size-count []
  let largest-community 0
  foreach [community] of nodes [
    if count nodes with [community = ?] > largest-community
    [ set largest-community count nodes with [community = ?] ]
  ]
  repeat largest-community + 1 [
    set community-size-count lput 0 community-size-count
  ]

  foreach [community] of nodes [
    set community-size-count replace-item count nodes with [community = ?] community-size-count (item count nodes with [community = ?] community-size-count + 1) 
  ]
  let c 1
  while [c < largest-community + 1] [
    show (se (item c community-size-count / c) " strong component(s) of size: " c)
    set c c + 1
  ]
end 

There is only one version of this model, created over 12 years ago by David Weintrop.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.