class GameRunner(object): def __init__(self, width, height, pure_python=False, should_unroll=False): self.width = width self.height = height self.kernel = IteratedConwayKernel(77) self.kernel.pure_python = pure_python self.kernel.should_unroll = should_unroll # kernel.pure_python = True # create a stencil grid for t+1 self.current_grid = StencilGrid([height, width]) all_neighbors = [(x, y) for x in range(-1, 2) for y in range(-1, 2)] all_neighbors.remove((0, 0)) self.current_grid.neighbor_definition.append(all_neighbors) self.future_grid = copy.deepcopy(self.current_grid) # this will be swapped to current after each iteration # Randomly initialize a quarter of the cells to 1 for x in self.current_grid.interior_points(): if np.random.random() > 0.75: self.current_grid[x] = 1 self.new_state_map = StencilGrid([18]) for index, new_state in enumerate([0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0]): self.new_state_map[index] = new_state def set_pure_python(self, new_mode): self.kernel.pure_python = new_mode def run_game(self, generations=1): for generation in range(generations): self.kernel.kernel(self.current_grid, self.new_state_map, self.future_grid) self.current_grid, self.future_grid = self.future_grid, self.current_grid print("gen %s" % generation) def __call__(self, *args, **kwargs): self.run_game() def run(self): self.run_game() def render(self): GameRunner.render_grid(self.current_grid) @staticmethod def render_grid(sk, msg="---"): """ Simplistic render of a life board """ print(msg) for h in range(sk.shape[0]): for w in range(sk.shape[1]): if sk[h][w] > 0: print('*', end='') else: print(' ', end='') print('')
alpha = 0.5 beta = 1.0 class LaplacianKernel(StencilKernel): def __init__(self, alpha, beta): super(LaplacianKernel, self).__init__() self.constants = {'alpha': alpha, 'beta': beta} def kernel(self, in_grid, out_grid): for x in in_grid.interior_points(): out_grid[x] = alpha * in_grid[x] for y in in_grid.neighbors(x, 1): out_grid[x] += beta * in_grid[y] nx = int(sys.argv[1]) ny = int(sys.argv[2]) nz = int(sys.argv[3]) input_grid = StencilGrid([nx, ny, nz]) output_grid = StencilGrid([nx, ny, nz]) for x in input_grid.interior_points(): input_grid[x] = random.randint(nx * ny * nz) laplacian = LaplacianKernel(alpha, beta) for i in range(50): for x in input_grid.interior_points(): input_grid[x] = random.randint(nx * ny * nz) laplacian.kernel(input_grid, output_grid)
class GameRunner(object): def __init__(self, width, height, pure_python=False, should_unroll=False): self.width = width self.height = height self.kernel = IteratedConwayKernel(77) self.kernel.pure_python = pure_python self.kernel.should_unroll = should_unroll # kernel.pure_python = True # create a stencil grid for t+1 self.current_grid = StencilGrid([height, width]) all_neighbors = [(x, y) for x in range(-1, 2) for y in range(-1, 2)] all_neighbors.remove((0, 0)) self.current_grid.neighbor_definition.append(all_neighbors) self.future_grid = copy.deepcopy( self.current_grid ) # this will be swapped to current after each iteration # Randomly initialize a quarter of the cells to 1 for x in self.current_grid.interior_points(): if np.random.random() > 0.75: self.current_grid[x] = 1 self.new_state_map = StencilGrid([18]) for index, new_state in enumerate( [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0]): self.new_state_map[index] = new_state def set_pure_python(self, new_mode): self.kernel.pure_python = new_mode def run_game(self, generations=1): for generation in range(generations): self.kernel.kernel(self.current_grid, self.new_state_map, self.future_grid) self.current_grid, self.future_grid = self.future_grid, self.current_grid print("gen %s" % generation) def __call__(self, *args, **kwargs): self.run_game() def run(self): self.run_game() def render(self): GameRunner.render_grid(self.current_grid) @staticmethod def render_grid(sk, msg="---"): """ Simplistic render of a life board """ print(msg) for h in range(sk.shape[0]): for w in range(sk.shape[1]): if sk[h][w] > 0: print('*', end='') else: print(' ', end='') print('')