示例#1
0
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)
示例#2
0
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 test_conway_dead_rules(self):
     """
     Test for proper adherence to game of life rules for currently dead cells.
     """
     read_grid = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
     game = ConwayGameOfLife(board=read_grid)
     dead_list = [game.get_lookup(0, n) for n in range(9)]
     self.assertListEqual(dead_list, [0, 0, 0, 1, 0, 0, 0, 0, 0])
 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)
示例#5
0
def create_data(n, shape = (5,5)):
    """
    Generates raw data-set for randomly-seeded Game of Life boards of a specified
    shape.

    Parameters
    -------------------
    n : int
        The number of data-sets to generate
    shape : tuple(int, int)
        The shape of the board

    Returns
    -------------------
    X : nparray
        Data-set of boards reshaped for CNN
    Y : nparray
        Corresponding ground-truth for subsequent generation for data-set
    """
    X = np.array([
        np.random.randint(2, size=shape, dtype = np.uint8) 
        for i in range(n)])
    games = np.array([ConwayGameOfLife(board = X_board) for X_board in X])
    for game in games:
        game.run_iterations(1)
    Y = np.array([game.board for game in games])
    X = X.reshape(X.shape[0], X.shape[1], X.shape[2], 1)
    Y = Y.reshape(X.shape)
    return X, Y
 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_invalid_type(self):
     """
     Test for constructor throwing error when given invalid size board-state
     """
     read_grid = 3
     with self.assertRaises(NotImplementedError):
         game = ConwayGameOfLife(board=read_grid)
 def test_conway_invalid_values(self):
     """
     Test for constructor throwing error when given invalid size board-state
     """
     read_grid = np.array([[0, 1, 2], [0, 1, 0], [0, 1, 0]])
     with self.assertRaises(NotImplementedError):
         game = ConwayGameOfLife(board=read_grid)
 def test_conway_too_small(self):
     """
     Test that constructor throws error when given invalid size board-state
     """
     read_grid = np.array([[0, 0]])
     with self.assertRaises(NotImplementedError):
         game = ConwayGameOfLife(board=read_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)
 def test_conway(self):
     """
     Test for proper construction of game object.
     """
     read_grid = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
     game = ConwayGameOfLife(board=read_grid)
     test_grid = game.board
     np.testing.assert_equal(test_grid, read_grid)