2015-05-31 03:36:35 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
2015-06-01 00:58:09 +00:00
|
|
|
import copy
|
|
|
|
import numpy as np
|
|
|
|
import ruleset as rs
|
|
|
|
import cell_plane as cp
|
|
|
|
import neighborhood as nh
|
2015-05-31 03:36:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CAM:
|
|
|
|
"""
|
|
|
|
Represents a Cellular Automata Machine (CAM).
|
|
|
|
|
2015-05-31 22:27:47 +00:00
|
|
|
The CAM consists of any number of cell planes that allow for increasingly complex cellular automata.
|
2015-05-31 03:36:35 +00:00
|
|
|
This is the top-level module that should be used by anyone wanting to work with fifth, and provides
|
|
|
|
all methods needed (i.e. supported) to interact/configure the cellular automata as desired.
|
|
|
|
"""
|
|
|
|
|
2015-05-31 22:27:47 +00:00
|
|
|
def __init__(self, cps=1, dimen=(100,100)):
|
2015-05-31 20:09:33 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
2015-06-01 00:58:09 +00:00
|
|
|
cps = max(cps, 1)
|
2015-05-31 20:09:33 +00:00
|
|
|
self._dimen = dimen
|
2015-06-01 00:58:09 +00:00
|
|
|
self._planes = [cp.CellPlane(dimen) for i in range(cps)]
|
|
|
|
self.master = self._planes[0].grid
|
2015-05-31 20:09:33 +00:00
|
|
|
|
|
|
|
|
2015-05-31 22:27:47 +00:00
|
|
|
def tick(self, ruleset, neighborhood, *args):
|
2015-05-31 03:36:35 +00:00
|
|
|
"""
|
2015-05-31 20:09:33 +00:00
|
|
|
The tick function should be called whenever we want to change the current status of the grid.
|
2015-05-31 03:36:35 +00:00
|
|
|
|
2015-05-31 20:09:33 +00:00
|
|
|
Every time the tick is called, the ruleset is applied to each cell and the next configuration
|
|
|
|
is placed into the master grid. Depending on the timing specifications set by the user, this
|
2015-05-31 22:27:47 +00:00
|
|
|
may also change secondary cell planes (the master is always updated on each tick).
|
2015-05-31 03:36:35 +00:00
|
|
|
"""
|
2015-06-01 00:58:09 +00:00
|
|
|
self.master[:] = rs.Ruleset.update(self.master, ruleset, neighborhood, *args)
|
|
|
|
|
|
|
|
|
|
|
|
def randomize(self):
|
|
|
|
"""
|
|
|
|
Set the master grid to a random configuration.
|
|
|
|
"""
|
|
|
|
@np.vectorize
|
|
|
|
def v_random(cell):
|
|
|
|
cell.value = np.random.random_integers(0, 1)
|
|
|
|
return cell
|
|
|
|
|
|
|
|
v_random(self.master)
|
|
|
|
|
|
|
|
|
|
|
|
def console_display(self):
|
|
|
|
"""
|
|
|
|
A convenience method used to display the grid onto the console.
|
|
|
|
|
|
|
|
This should not be called frequently; I haven't benchmarked it but I do not anticipate this
|
|
|
|
running very well for larger grids.
|
|
|
|
"""
|
|
|
|
vfunc = np.vectorize(lambda x: int(x.value))
|
|
|
|
print(vfunc(self.master))
|
2015-05-31 20:09:33 +00:00
|
|
|
|