ugly.automata 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/automata.rb +1 -0
- data/lib/automata/elementary.rb +41 -0
- data/lib/automata/version.rb +3 -0
- metadata +50 -0
data/lib/automata.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'automata/elementary'
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'enumerator'
|
2
|
+
|
3
|
+
module Automata
|
4
|
+
class Elementary
|
5
|
+
# This elegantly simple algorithm and code is from Jesse Merriman written for
|
6
|
+
# Ruby Quiz 134: Cellular Automata. see: http://www.rubyquiz.com/quiz134.html
|
7
|
+
def self.transformer(rule_num)
|
8
|
+
rule = rule_num.to_s 2
|
9
|
+
rule = ('0' * (2**3 - rule.length) + rule).reverse.split(//)
|
10
|
+
lambda { |hood| rule[hood.join.to_i(2)] }
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.step(state, trans)
|
14
|
+
new_state = []
|
15
|
+
([0,0] + state + [0,0]).each_cons(3) do |hood|
|
16
|
+
new_state << trans[hood]
|
17
|
+
end
|
18
|
+
new_state
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :rule, :steps, :state
|
22
|
+
def initialize(rule, state = ['1'])
|
23
|
+
@rule, @state = rule, state
|
24
|
+
end
|
25
|
+
|
26
|
+
def run(steps)
|
27
|
+
trans = Elementary.transformer(@rule)
|
28
|
+
steps.times do |s|
|
29
|
+
@state = Elementary.step(@state, trans)
|
30
|
+
end
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
@state.join('')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ugly.automata
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Garriss
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-27 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'A collection of algorithms for generating cellular automata. Currently
|
15
|
+
only contains "elementary cellular automata" or aka the cool pictures from the Wolfram
|
16
|
+
book. Provides a Automata::Elementary class that takes rule number, initial state
|
17
|
+
along with a #run(steps) method. Produces an array of strings as a result.'
|
18
|
+
email: mgarriss@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- lib/automata/elementary.rb
|
24
|
+
- lib/automata/version.rb
|
25
|
+
- lib/automata.rb
|
26
|
+
homepage: http://github.com/mgarriss/ugly.automata
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.11
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: A collection of algorithms for generating cellular automata.
|
50
|
+
test_files: []
|