Class: Cellula::Automaton

Inherits:
Object
  • Object
show all
Defined in:
lib/cellula/automaton.rb

Overview

Public: This class provides all it’s needed to run a cellular automaton.

You rarely wants to initialize an Automaton directly. This is because Automaton’s creation needs a lots of parameters. It is much more simpler to create Automata using AutomatonBuilder class.

Dimensions


Currently only support 1D cellular automata.

Type of automaton


Currently only support elementary cellular automaton. Quoted from Wikipedia:

an elementary cellular automaton is a one-dimensional cellular automaton where there are two possible states (labeled 0 and 1) and the rule to determine the state of a cell in the next generation depends only on the current state of the cell and its two immediate neighbors.

See en.wikipedia.org/wiki/Elementary_cellular_automaton for more details.

Rule


Currently support only wolfram code rules.

I hope to support custom rules and types soon.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, dimensions, type, width, rule) ⇒ Automaton

Public: Initialize a new Automaton.

name - String name of the automaton. dimensions - Integer number of dimensions for the automaton’s

grid. Default is 1.

type - Currently only :elementary. width - The Integer width of the automaton’s grid. rule - Rule of the automaton. Currently rule is a Symbol

following this pattern: :wolfram_code_X, where X is
the rule number, from 0 to 255.


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cellula/automaton.rb', line 45

def initialize(name, dimensions, type, width, rule)
  panic "Bad dimensions: #{dimensions}" if dimensions != 1
  panic "Bad type: #{type}" if type != :elementary
  panic "Bad width: #{width}" if width < 1
  @name = name
  @dimensions = dimensions
  @type = type
  @width = width
  @rule = Rule.new(rule)
  @grid = Array.new(@width)
  @grid.map! {|item| rand(2) }
end

Instance Attribute Details

#dimensionsObject

Public: Set/get the Integer dimensions of the automaton’s grid.



62
63
64
# File 'lib/cellula/automaton.rb', line 62

def dimensions
  @dimensions
end

#nameObject (readonly)

Public: Get the String name of the automaton.



59
60
61
# File 'lib/cellula/automaton.rb', line 59

def name
  @name
end

#ruleObject (readonly)

Public: Get the rule of the automaton.



71
72
73
# File 'lib/cellula/automaton.rb', line 71

def rule
  @rule
end

#typeObject

Public: Set/get the Symbol type of the automaton.



65
66
67
# File 'lib/cellula/automaton.rb', line 65

def type
  @type
end

#widthObject

Public: Set/get the Integer width of the automaton’s grid.



68
69
70
# File 'lib/cellula/automaton.rb', line 68

def width
  @width
end

Instance Method Details

#generate(study, &block) ⇒ Object

Public: Generate successive generations of this automaton. If, for example, ‘student.generations == 4` then #generate will produce 5 generations: The original generation plus the four you want.

study - Study instance. block - What to do with a generation.

Example

automaton.generate(study) do |num, generation|
  printf "Gen %4s: %s\n", num, generation.join()
end
# => will result in something like that:
# => Gen    0: 0101001010001011000001000100000100101011
# => Gen    1: 0000010000010000000010001000001001000000
# => Gen    2: 0000100000100000000100010000010010000000
# => Gen    3: 0001000001000000001000100000100100000000
# => Gen    4: 0010000010000000010001000001001000000000

Returns successive generations as Array.



94
95
96
97
98
99
100
101
# File 'lib/cellula/automaton.rb', line 94

def generate(study, &block)
  adapt_for_single_method if study.method == :single
  block.call(0, @grid)
  1.upto(study.generations) do |cell_index|
    apply_rule(study)
    block.call(cell_index, @grid)
  end
end