GithubHelp home page GithubHelp logo

cloudxtreme / 1d-cellular-automata Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nossidge/1d-cellular-automata

0.0 1.0 0.0 232 KB

One dimensional cellular automata. Supports multiple cell states. Output to stdout and .png image.

Ruby 100.00%

1d-cellular-automata's Introduction

One dimensional cellular automata

by Paul Thompson - [email protected] - tilde.town/~nossidge

One dimensional cellular automata. Supports multiple cell states. Output to stdout and .png image.

This is so unfinished it's just like wow.

http://mathworld.wolfram.com/ElementaryCellularAutomaton.html

Tutorial

A tutorial for the console.

Let's create an output using just the -x and -y options to specify the size:

$ automata.rb -x37 -y7
1000000000000000000000000000000000000
0100000000000000000000000000000000000
1010000000000000000000000000000000000
0001000000000000000000000000000000000
0010100000000000000000000000000000000
0100010000000000000000000000000000000
1010101000000000000000000000000000000
0000000100000000000000000000000000000

This writes a single true '1' cell at the leftmost character of the first row, and applies a random rule to generate 6 more generations. The random rule used for this is rule 146. Each time a automaton is generated, the rule number is saved to a file called ~rules.txt so you can see which rule was most recently used.

We can use the -u option to choose a specific rule, so let's use -u146 so we can compare future output using the same rule. We will now use the -c option to centre the initial '1' cell in the middle of the input row.

$ automata.rb -x37 -y7 -u146 -c
0000000000000000001000000000000000000
0000000000000000010100000000000000000
0000000000000000100010000000000000000
0000000000000001010101000000000000000
0000000000000010000000100000000000000
0000000000000101000001010000000000000
0000000000001000100010001000000000000
0000000000010101010101010100000000000

The default output symbols are 01234... for each cell state. But we can overwrite this using the -s option. Let's make all zero states a space, and all one states a lowercase O.

$ automata.rb -x37 -y7 -u146 -c -s' o'
                  o
                 o o
                o   o
               o o o o
              o       o
             o o     o o
            o   o   o   o
           o o o o o o o o

That looks pretty cool. You can also use the -v option to flip the output so that the oldest generations are displayed last:

$ automata.rb -x37 -y7 -u146 -c -s' o' -v
           o o o o o o o o
            o   o   o   o
             o o     o o
              o       o
               o o o o
                o   o
                 o o
                  o

The -t option is used to add Y-axis symmetry to the output:

$ automata.rb -x37 -y7 -u146 -c -s' o' -t
                  o
                 o o
                o   o
               o o o o
              o       o
             o o     o o
            o   o   o   o
           o o o o o o o o
            o   o   o   o
             o o     o o
              o       o
               o o o o
                o   o
                 o o
                  o
$ automata.rb -x37 -y7 -u146 -c -s' o' -tv
           o o o o o o o o
            o   o   o   o
             o o     o o
              o       o
               o o o o
                o   o
                 o o
                  o
                 o o
                o   o
               o o o o
              o       o
             o o     o o
            o   o   o   o
           o o o o o o o o

Instead of using a single one cell as the initial state, we can specify whatever state we want, by using the -i option. The -c option works here as well, so we can use it to write '1000001' to the centre of the initial state:

$ automata.rb -x37 -y7 -u146 -c -s' o' -i'1000001'
               o     o
              o o   o o
             o   o o   o
            o o o   o o o
           o     o o     o
          o o   o   o   o o
         o   o o o o o o   o
        o o o           o o o

Or you could just let it randomly create an initial state, with the -r option:

$ automata.rb -x37 -y7 -u146 -c -s' o' -r
  oooo oo   o o o   ooo o o o o o o
 o oo    o o     o o o             o
o    o  o   o   o     o           o o
 o  o oo o o o o o   o o         o
o oo              o o   o       o o
    o            o   o o o     o   o
   o o          o o o     o   o o o o
  o   o        o     o   o o o

The -N option lets you constrain the randomised initial state to just a few cells, leaving the rest with state zero. So to randomise just the centremost 15 cells:

$ automata.rb -x37 -y7 -u146 -c -s' o' -r -N15
           o   o  oo   o
          o o o oo  o o o
         o        oo     o
        o o      o  o   o o
       o   o    o oo o o   o
      o o o o  o        o o o
     o       oo o      o     o
    o o     o    o    o o   o o

And you can use the -p option to specify the probablility of each option. This chooses state one 90% of the time, and zero the remainder:

$ automata.rb -x37 -y7 -u146 -c -s' o' -r -p'0111111111'
o oooooooooo ooooo oooooooo ooooooooo
   oooooooo   ooo   oooooo   ooooooo
  o oooooo o o o o o oooo o o ooooo o
 o   oooo             oo       ooo
o o o oo o           o  o     o o o
          o         o oo o   o     o
         o o       o      o o o   o o
        o   o     o o    o     o o

I'm going to use the initial state of that last example to show the -w option. This wraps the screen, so that the far-left cell will take into account the value of the far-right cell and vice versa.

$ automata.rb -x37 -y7 -u146 -c -s' o' -r -i'1 1111111111 11111 11111111 111111111' -w
o oooooooooo ooooo oooooooo ooooooooo
   oooooooo   ooo   oooooo   oooooooo
o o oooooo o o o o o oooo o o oooooo
     oooo             oo       oooo
    o oo o           o  o     o oo o
   o      o         o oo o   o      o
o o o    o o       o      o o o    o
     o  o   o     o o    o     o  o

Now you can start to see the pretty, chaotic patterns that can be generated with just these simple rules!

1d-cellular-automata's People

Contributors

nossidge avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.