def main(): """ Command-line runtime for Conway's game of life. Command-line arguments: -f [string]: Filepath of a single input instance. -o [string]: Filepath of a single output instance. -n [integer]: Number of generations. -v [bool]: Verbosity of functions (optional). """ # Parse command-line arguments. cli_args = dict((sys.argv[1 + i], sys.argv[2 + i]) for i in range(0, len(sys.argv[1:]), 2)) input_arg = cli_args.get("-f", None) output_arg = cli_args.get("-o", None) generation_arg = cli_args.get("-n", None) verbose_arg = cli_args.get("-v", False) == "True" if not input_arg or not output_arg or not generation_arg: print("Invalid arguments.") sys.exit(1) # Generate game and run for specified number of iterations generation_arg = int(generation_arg) game_board = pcw.unpickle_conway_board(input_arg, verbose=verbose_arg) game = ConwayGameOfLife(board=game_board) game.run_iterations(generation_arg, verbose=verbose_arg) pcw.pickle_conway_board(game.board, filename=output_arg, verbose=verbose_arg)
def query_model(n, shape, suffix): """ Loads model and queries it over a specified number of iterations Parameters ------------------- n : int The number of data points to generate for training, validation, and testing sets shape : tuple(int, int) The shape of the board on which to train the model suffix: str The suffix name of the model Returns ------------------- bool True if all iterations match ground-truth. """ shape = (10,10) keras_game = KerasGameOfLife.from_file(suffix, shape) match = [] for i in range(n): X1 = np.random.randint(2, size=shape, dtype = np.uint8) game = ConwayGameOfLife(board = X1) game.run_iterations(1) X2 = game.board match.append(keras_game.query(X1, X2)) print("All {} iterations match: {}".format(n, all(match))) return all(match)
def test_conway_oscillator2(self): """ Test for 3x3 oscillator flipping twice """ read_grid = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]]) game = ConwayGameOfLife(board=read_grid) game.run_iterations(2) test_grid = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]]) np.testing.assert_equal(test_grid, game.board)
def test_conway_boat_still(self): """ Test for boat still pattern: 110 101 010 """ read_grid = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]) game = ConwayGameOfLife(board=read_grid) game.run_iterations(1) test_grid = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]) np.testing.assert_equal(game.board, test_grid)
def test_conway_glider4(self): """ Test for 2-iteration glider pattern. 001 101 011 """ read_grid = np.array([[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [1, 0, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0]]) game = ConwayGameOfLife(board=read_grid) game.run_iterations(4) test_grid = np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 1, 0, 1, 0], [0, 0, 1, 1, 0]]) np.testing.assert_equal(game.board, test_grid)