asep

amazon:渋滞学を読んでいます。まだ序盤ですが、ASEPというセルオートマトンがでてきました。
かなり既出っぽいですが

#! /usr/bin/python
# coding: sjis

import sys, os, random

ACTIVE = True
PASSIVE = False

class Node:
    def __init__(self, state):
	self._state = state
    def flip(self):
	self._state = not self._state
	return self._state
    def state(self):
	return self._state
    def __str__(self):
	return self._state and "@" or "-"

class AsepField(list):
    def __init__(self, length):
	list.__init__(self, [Node(PASSIVE) for _ in range(length)])
    def update(self):
	def pred(idx):
	    return self[idx].state() and \
		   not self[(idx+1)%len(self)].state()
	predlst = [pred(x) for x in range(len(self))]
	for i in range(len(self)):
	    if predlst[i]:
		self[i].flip()
		self[(i+1)%len(self)].flip()
    def __str__(self):
	strs = map(str, self)
	s = "<AsepField at %d [%s]>" % (id(self), ", ".join(strs))
	return s

def main():
    if sys.argv.__len__() <= 3:
        progname = os.path.split(__file__)[-1]
        print "usage: %s field_length nnode turns" % progname
	print "descrption: show command line asep simulation."
	sys.exit()
    arg = [int(x) for x in sys.argv[1:]]
    
    asep = AsepField(arg[0])
    # init 
    for i in range(arg[1]):
	lst = [x for x in range(len(asep)) if not asep[x].state()]
	idx = random.choice(lst)
	asep[idx].flip()
    # mainloop
    import time
    for i in range(arg[2]):
	#print "".join(map(str, asep))+ "\r",
        print "".join(map(str,asep))
	#time.sleep(0.5)
	asep.update()

if __name__ == '__main__':
    main()

実行:流れがスムーズな状態

$ python asep2.py 70 10 20
---@---@@----------@-----------@--@-----------@@---------@------@-----
----@--@-@----------@-----------@--@----------@-@---------@------@----
-----@--@-@----------@-----------@--@----------@-@---------@------@---
------@--@-@----------@-----------@--@----------@-@---------@------@--
-------@--@-@----------@-----------@--@----------@-@---------@------@-
--------@--@-@----------@-----------@--@----------@-@---------@------@
@--------@--@-@----------@-----------@--@----------@-@---------@------
-@--------@--@-@----------@-----------@--@----------@-@---------@-----
--@--------@--@-@----------@-----------@--@----------@-@---------@----
---@--------@--@-@----------@-----------@--@----------@-@---------@---
----@--------@--@-@----------@-----------@--@----------@-@---------@--
-----@--------@--@-@----------@-----------@--@----------@-@---------@-
------@--------@--@-@----------@-----------@--@----------@-@---------@
@------@--------@--@-@----------@-----------@--@----------@-@---------
-@------@--------@--@-@----------@-----------@--@----------@-@--------
--@------@--------@--@-@----------@-----------@--@----------@-@-------
---@------@--------@--@-@----------@-----------@--@----------@-@------
----@------@--------@--@-@----------@-----------@--@----------@-@-----
-----@------@--------@--@-@----------@-----------@--@----------@-@----
------@------@--------@--@-@----------@-----------@--@----------@-@---

実行:臨界

$ python asep2.py 70 35 20
-@@-@@--@@-@@--@@-@-@--@-@--@--@@@@-----@--@@@-@-@@@-@--@@@----@@--@@-
-@-@@-@-@-@@-@-@-@-@-@--@-@--@-@@@-@-----@-@@-@-@@@-@-@-@@-@---@-@-@-@
@-@@-@-@-@@-@-@-@-@-@-@--@-@--@@@-@-@-----@@-@-@@@-@-@-@@-@-@---@-@-@-
-@@-@-@-@@-@-@-@-@-@-@-@--@-@-@@-@-@-@----@-@-@@@-@-@-@@-@-@-@---@-@-@
@@-@-@-@@-@-@-@-@-@-@-@-@--@-@@-@-@-@-@----@-@@@-@-@-@@-@-@-@-@---@-@-
@-@-@-@@-@-@-@-@-@-@-@-@-@--@@-@-@-@-@-@----@@@-@-@-@@-@-@-@-@-@---@-@
-@-@-@@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@---@@-@-@-@@-@-@-@-@-@-@---@@
@-@-@@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@--@-@-@-@@-@-@-@-@-@-@-@--@-
-@-@@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@--@-@-@@-@-@-@-@-@-@-@-@--@
@-@@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@--@-@@-@-@-@-@-@-@-@-@-@--
-@@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@--@@-@-@-@-@-@-@-@-@-@-@-
-@-@-@-@-@-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