CA1D Benchmark

No preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 4.1pre9 • Viewed 173 times • Downloaded 26 times • Run 0 times
Download the 'CA1D Benchmark' 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

globals
[
  row           ;; current row
  old-rule      ;; previous rule
  rules-shown?  ;; flag to check if rules have been displayed
  gone?         ;; flag to check if go has already been pressed
  result
]

patches-own
[on?]

to startup  ;; initially, nothing has been displayed
  set rules-shown? false
  set gone? false
  set old-rule rule
end 

to benchmark
  random-seed 4378
  setup-random
  reset-timer
  repeat 10 * world-height [ go ]
  set result timer
end 

;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setup Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;

to setup-general  ;; setup general working environment
  cp ct
  set row max-pycor   ;; reset current row
  refresh-rules
  set gone? false
  set rules-shown? false  ;; rules are no longer shown since the screen has been cleared
end 

to single-cell
  setup-general
  ask patches with [pycor = row] [set on? false set pcolor background]  ;; initialize top row
  ask patch 0 row [ set pcolor foreground
                    set on? true ]
end 

to setup-random
  setup-general
  ask patches with [pycor = row]  ;; randomly place cells across the top of the screen
  [
    set on? ((random 100) < density)
    color-patch
  ]
end 

to setup-continue
  let on?-list []
  if not gone?  ;; make sure go has already been called
    [ stop ]
  set on?-list map [[on?] of ?] sort patches with [pycor = row]  ;; copy cell states from the
                                                                 ;; current row to a list
  setup-general
  ask patches with [ pycor = row ]
  [
    set on? item (pxcor + max-pxcor) on?-list  ;; copy states from list to top row
    color-patch
  ]
  set gone? true
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; GO Procedures      ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;

to go
  if (rules-shown?)  ;; don't do unless we are properly set up
    [ stop ]
  if (row = min-pycor)  ;; if we reach the end, continue from the top or stop
  [
    ifelse auto-continue?
      [ display
        setup-continue ]
      [ stop ]
  ]
  ask patches with [ pycor = row ]  ;; apply rule
    [ do-rule ]
  set row (row - 1)
  ask patches with [ pycor = row ]  ;; color in changed cells
    [ color-patch ]
  set gone? true
  tick
end 

to do-rule  ;; patch procedure
  let left-on? [on?] of patch-at -1 0  ;; set to true if the patch to the left is on
  let right-on? [on?] of patch-at 1 0  ;; set to true if the patch to the right is on

  ;; each of these lines checks the local area and (possibly)
  ;; sets the lower cell according to the corresponding switch
  let new-value
    (iii and left-on?       and on?       and right-on?)          or
    (iio and left-on?       and on?       and (not right-on?))    or
    (ioi and left-on?       and (not on?) and right-on?)          or
    (ioo and left-on?       and (not on?) and (not right-on?))    or
    (oii and (not left-on?) and on?       and right-on?)          or
    (oio and (not left-on?) and on?       and (not right-on?))    or
    (ooi and (not left-on?) and (not on?) and right-on?)          or
    (ooo and (not left-on?) and (not on?) and (not right-on?))
  ask patch-at 0 -1 [ set on? new-value ]
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Utility Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;

to color-patch  ;;patch procedure
  ifelse on?
    [ set pcolor foreground ]
    [ set pcolor background ]
end 

to-report bindigit [number power-of-two]
  ifelse (power-of-two = 0)
    [ report floor number mod 2 ]
    [ report bindigit (floor number / 2) (power-of-two - 1) ]
end 

to refresh-rules  ;; update either switches or slider depending on which has been changed last
  ifelse (rule = old-rule)
  [
    if (rule != calculate-rule)
      [ set rule calculate-rule ]
  ]
  [ extrapolate-switches ]
  set old-rule rule
end 

to extrapolate-switches
  ;; set the switches based on the slider
  set ooo ((bindigit rule 0) = 1)
  set ooi ((bindigit rule 1) = 1)
  set oio ((bindigit rule 2) = 1)
  set oii ((bindigit rule 3) = 1)
  set ioo ((bindigit rule 4) = 1)
  set ioi ((bindigit rule 5) = 1)
  set iio ((bindigit rule 6) = 1)
  set iii ((bindigit rule 7) = 1)
end 

to-report calculate-rule
  ;; set the slider based on the switches
  let rresult 0
  if ooo [ set rresult rresult +   1 ]
  if ooi [ set rresult rresult +   2 ]
  if oio [ set rresult rresult +   4 ]
  if oii [ set rresult rresult +   8 ]
  if ioo [ set rresult rresult +  16 ]
  if ioi [ set rresult rresult +  32 ]
  if iio [ set rresult rresult +  64 ]
  if iii [ set rresult rresult + 128 ]
  report rresult
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SHOW-RULES RELATED PROCEDURES ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to show-rules  ;; preview cell state transitions
  setup-general
  let rules list-rules

  ask patches with [pycor > max-pycor - 5]
    [ set pcolor gray ]

  ;; create 8 turtles evenly spaced across the screen
  ask patches with [ pycor = max-pycor and
                    ((pxcor + 1) mod (floor (world-width / 8))) = 0 ]
  [
    sprout 1
    [
      set heading 270
      fd 18  ;;16px offset + 2px
      print-block (item 0 (item who rules))  ;; right cell
      fd 2
      print-block (item 1 (item who rules))  ;; center cell
      fd 2
      print-block (item 2 (item who rules))  ;; left cell
      bk 2
      set heading 180
      fd 2
      set heading 90
      print-block (item 3 (item who rules))  ;; next cell state
      die
    ]
  ]
  set rules-shown? true
end 

;; turtle procedure

to print-block [ state ]  ;; draw a 2x2 block of with a color determined by the state
  ifelse state
    [ set color foreground ]
    [ set color background ]
  set heading 90
  repeat 4
  [
    set pcolor color
    rt 90
    fd 1
  ]
end 

to-report list-rules  ;; return a list of state-transition 4-tuples corresponding to the switches
  let rules []
  set rules (lput (lput ooo [false false false]) rules)
  set rules (lput (lput ooi [false false true ]) rules)
  set rules (lput (lput oio [false true  false]) rules)
  set rules (lput (lput oii [false true  true ]) rules)
  set rules (lput (lput ioo [true  false false]) rules)
  set rules (lput (lput ioi [true  false true ]) rules)
  set rules (lput (lput iio [true  true  false]) rules)
  set rules (lput (lput iii [true  true  true ]) rules)
  report rules
end 

There are 3 versions of this model.

Uploaded by When Description Download
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago CA1D Benchmark Download this version
Uri Wilensky almost 14 years ago CA1D Benchmark Download this version

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.