"""

Cellular Automata using the GKL rule set for doing density
classification.  If GKL is iterated on an initial configuration in
which the density of 1's less than 1/2 then it will eventually
converge to the all-0 configuration. On the other hand, if the initial
density is greater than 1/2 then GKL will converge to the all-1
configuration. The most interesting behavior occurs when the initial
density is close to 1/2. In this case the convergence time may be
long, as a battle is played out between domains composed largely of
0's and domains composed largely of 1's.

"""

from pyrobot.general.ca import *

rules = Rules()
rules.init(gkl)
print "---------------------------------------------------------------"
print "GKL rule set, with k=2 and radius=3"
print "Each rule will have 7 bits"
print "There should be 2^7 rules"
print "Length of GKL rule set:", len(gkl)
print gkl
print "Experiment with different random starting conditions"
print "---------------------------------------------------------------"
data = Lattice()
for percent in [ x/10.0 for x in range(10)]:
    print "Percent:", percent*100, "--------------------------------------"
    data.randomize(percent)
    rules.applyAll(data)
    data.display()


