Esempio n. 1
0
def generate_data(mode, num_simulations=30):
    """
    Generate the dual model data from the TEST_GRID_LIST specified above.

    Args:
        - mode (Str): "delay" or "pressure"; whether the data generated has more
            or fewer monte carlo iterations to solve the test grids
        - num_simulations (int): how many data points to generate from the model
    Returns:
        -
    """
    agent = Agent()
    start = time.time()
    print("Starting {mode} data generation".format(mode=mode))
    model_results = [
    ]  # item e.g. {'model':'constrained','grid_num':23,'reward':3,'best_reward':3,'id':10}
    # Generate dual model "time constrained scenario"
    for i in range(num_simulations):
        if mode == "pressure":
            n_iters = random.randrange(
                0, 50
            )  #choose a randome integer between 20 and 30 for MC iterations
        elif mode == "delay":
            n_iters = random.randrange(
                120, 530
            )  #note these ranges were chosen by looking at the dual model performance graph
            # in the dual_model_data_generation.ipynb

        for ind, grid_init in TEST_GRID_LIST:
            testgrid = grid.Grid(5, random=False, init_pos=grid_init)
            Q, policy = agent.mc_first_visit_control(testgrid.copy(),
                                                     iters=n_iters,
                                                     nn_init=True,
                                                     cutoff=0.4)
            _, _, model_reward = agent.run_final_policy(testgrid.copy(),
                                                        Q,
                                                        nn_init=True,
                                                        display=False)
            individual_info = {
            }  #information for this particular model instantiation
            individual_info['id'] = i
            individual_info['model'] = mode
            individual_info['grid_num'] = ind
            individual_info['reward'] = model_reward
            individual_info['best_reward'] = grid_init['best_reward']
            model_results.append(individual_info)
        print("Simulation {num} took {time} seconds".format(num=i,
                                                            time=time.time() -
                                                            start))
        start = time.time()

    return model_results
Esempio n. 2
0
    def test_constructor(self):
        grid1AllBomb = grid.Grid(1, 1, 1)
        self.assertEqual("* \n", grid1AllBomb.to_s())
        self.assertEqual(1, grid1AllBomb.game_state())
        self.assertTrue(grid1AllBomb.reveal_tile(0, 0))
        self.assertEqual("B \n", grid1AllBomb.to_s())
        self.assertEqual(-1, grid1AllBomb.game_state())

        grid4AllBomb = grid.Grid(2, 2, 4)
        self.assertEqual("* * \n* * \n", grid4AllBomb.to_s())
        self.assertEqual(1, grid4AllBomb.game_state())
        self.assertTrue(grid4AllBomb.reveal_tile(1, 1))
        self.assertEqual("* * \n* B \n", grid4AllBomb.to_s())
        self.assertEqual(-1, grid4AllBomb.game_state())

        grid1NoBomb = grid.Grid(1, 1, 0)
        self.assertEqual("* \n", grid1NoBomb.to_s())
        self.assertFalse(grid1NoBomb.reveal_tile(0, 0))
        self.assertEqual("_ \n", grid1NoBomb.to_s())
        self.assertEqual(1, grid1NoBomb.game_state())

        grid4NoBomb = grid.Grid(2, 2, 0)
        self.assertEqual("* * \n* * \n", grid4NoBomb.to_s())
        self.assertFalse(grid4NoBomb.reveal_tile(0, 0))
        self.assertEqual("_ _ \n_ _ \n", grid4NoBomb.to_s())
        self.assertEqual(1, grid4NoBomb.game_state())

        # Testing with bad parameters
        with self.assertRaises(ValueError):
            grid.Grid(0, 1, 1)
        with self.assertRaises(ValueError):
            grid.Grid(1, 0, 1)
        with self.assertRaises(ValueError):
            grid.Grid(1, 1, 2)
        with self.assertRaises(ValueError):
            grid.Grid("adsf", "sda", 1)
Esempio n. 3
0
    print("* data filtered")
    epoch = time.mktime(dt.date(1970, 1, 1).timetuple())
    df["date"] = df.apply(lambda row: int((time.mktime(
        dt.date(row["iyear"], row["imonth"], row["iday"]).timetuple()) - epoch)
                                          / (24 * 3600.0)),
                          axis=1)
    print("* date created")
    df2 = df.copy()
    df2 = df2[df2['longitude'] > -200]  #to clean strange data
    min_latitude = floor_int(min(list(df2['latitude'])))
    max_latitude = floor_int(max(list(df2['latitude'])))
    min_longitude = floor_int(min(list(df2['longitude'])))
    max_longitude = floor_int(max(list(df2['longitude'])))
    grid_size = (max_longitude - min_longitude, max_latitude - min_latitude)
    grid_granularity = (75, 75)
    grid_x_len = math.ceil(grid_size[0] / grid_granularity[0])
    grid_y_len = math.ceil(grid_size[1] / grid_granularity[1])
    grid = grid.Grid(grid_x_len, grid_y_len, min_latitude, max_latitude,
                     min_longitude, max_longitude, grid_granularity)
    for index, row in df2.iterrows():
        grid.FillSquarePoint(row['latitude'], row['longitude'], row['date'],
                             row)
    fig, axs = plt.subplots(grid_y_len, grid_x_len, constrained_layout=True)
    for i in range(grid_x_len):
        for j in range(grid_y_len):
            current_data = grid.GetSquareFromMatrix(j, i).Data
            x_axis = list(1970 + (pd.Series(current_data.index) / 365.25))
            y_axis = list(current_data['nkill'])
            axs[i, j].plot(x_axis, y_axis)
    plt.show()
Esempio n. 4
0
    def __init__(self):
        """
        Intializes pygame and the pygame screen, loads the sprite groups that are needed,
        creates a model object belonging to the backgroundreq class, creates a quit button
        object belonging to the backgroundreq class (both model and quit button object are
        both added to backgroundreq sprite class), sets the amount of grid rows and columns,
        sets the grid and cell dimensions, sets the grid, creates slider objects for each
        slider and button name, creates backgroundreq objects for each slider and button name,
        and creates a label object for each slider and button name, adds each instance of the
        class to the all_sprites group, and calls the set_json_config() function to create
        a json file that stores the slider name (key) to the sliders value (value)
        """
        # Initialize environment
        self.grid_rows = 5
        self.grid_cols = 5
        self.grid_dimensions = (self.grid_rows, self.grid_cols)
        os.environ['SDL_VIDEO_CENTERED'] = '1'
        pygame.init()
        self.screen = pygame.display.set_mode(
            (c.SCREEN_WIDTH, c.SCREEN_HEIGHT))
        pygame.display.set_caption('MelodyDesign')

        # Initialize synth
        self.config = {}
        self.get_json_config()
        self.synth = synth.Synth(self.grid_dimensions, self.config)

        # Initialize sprite groups
        self.gridcells = pygame.sprite.Group()
        self.cells = pygame.sprite.Group()
        self.bg_reqs = pygame.sprite.Group()
        self.bg_labels = pygame.sprite.Group()
        self.sliders = pygame.sprite.Group()
        self.modals = pygame.sprite.Group()
        self.texts = pygame.sprite.Group()
        self.modal_parent = backgroundreq.Backgroundreq(x=-1000,
                                                        y=120,
                                                        h=350,
                                                        w=650,
                                                        type=2)
        self.quit_button = backgroundreq.Backgroundreq(x=-1000,
                                                       y=120,
                                                       h=30,
                                                       w=30,
                                                       type=3,
                                                       color=c.RED)
        self.modals.add(self.modal_parent, self.quit_button)
        self.quit_button.image.fill((250, 0, 0))

        # Set values
        self.cell_h = c.SCREEN_HEIGHT / self.grid_rows
        self.cell_w = self.cell_h
        self.cell_dimensions = self.cell_h, self.cell_w
        self.cell_sep = 2 if sum(self.grid_dimensions) < 20 else 1
        self.prev_cell = None
        self.curr_cell = None
        self.done = False

        # Create/populate grid
        self.grid = grid.Grid()
        self.populate_grid()

        # Create sliders
        self.slider_names = ['vol', 'res', 'att', 'dec', 'sus', 'rel', 'dur']
        self.slider_bg_padding = 20
        self.slider_label_w = 30
        self.slider_bg_x = c.SCREEN_HEIGHT + self.slider_bg_padding
        self.slider_bg_y = self.slider_bg_padding + 5
        self.slider_bg_h = 20
        self.slider_bg_w = c.SCREEN_WIDTH - c.SCREEN_HEIGHT - self.slider_bg_padding * 2
        self.sliders_x = self.slider_bg_x + self.slider_label_w
        self.sliders_y = self.slider_bg_padding
        self.sliders_h = self.slider_bg_h * 1.5
        self.sliders_w = 40
        self.slider_bg_total = (self.slider_bg_w - self.sliders_w -
                                self.slider_label_w)
        self.initialize_sliders()

        # Create button(s)
        self.button_x = 620
        self.button_y = c.SCREEN_HEIGHT - self.slider_bg_padding * 2.3
        self.button_sep = 100
        self.button_w = 70
        self.button_h = self.sliders_h
        self.initialize_button("Stop", 695, 400, self.button_h, 100, c.RED)
        self.initialize_button("Help", 800, 550, self.button_h, self.button_w)

        # Add sprites groups to all_sprites
        self.all_sprites = pygame.sprite.Group(
            tuple(self.gridcells) + tuple(self.cells) + tuple(self.bg_reqs) +
            tuple(self.sliders) + tuple(self.bg_labels) + tuple(self.modals))
Esempio n. 5
0
test14 = {
    "train": (4, 4),
    "trainvel": (-1, 0),
    "cargo1": (1, 3),
    "target1": (3, 2),
    "switch": (4, 1),
    "agent": (0, 2),
    "cargo2": (0, 4),
    "target2": (0, 1),
    'num1': 1,
    'num2': 2
}

test_suite = [
    grid.Grid(5, random=False, init_pos=test3),
    grid.Grid(5, random=False, init_pos=test12),
    grid.Grid(5, random=False, init_pos=test14)
]

swit27 = {
    "train": (1, 0),
    "trainvel": (0, 1),
    "cargo1": (0, 1),
    "target1": (4, 3),
    "switch": (3, 3),
    "agent": (4, 4),
    "cargo2": (1, 2),
    "target2": (0, 3),
    'num1': 1,
    "num2": 2
Esempio n. 6
0
    def test_sample_game(self, random):
        random.sample._mock_side_effect = self.random.sample
        gridCustom1 = grid.Grid(3, 3, 3)
        self.assertEqual("* * * \n* * * \n* * * \n", gridCustom1.to_s())
        self.assertEqual(0, gridCustom1.game_state())
        self.assertFalse(gridCustom1.reveal_tile(0, 0))
        self.assertEqual("_ _ _ \n2 3 2 \n* * * \n", gridCustom1.to_s())
        self.assertEqual(1, gridCustom1.game_state())
        self.assertTrue(gridCustom1.reveal_tile(2, 2))
        self.assertEqual("_ _ _ \n2 3 2 \n* * B \n", gridCustom1.to_s())
        self.assertEqual(-1, gridCustom1.game_state())
        self.assertTrue(gridCustom1.reveal_tile(1, 2))
        self.assertEqual("_ _ _ \n2 3 2 \n* B B \n", gridCustom1.to_s())
        self.assertEqual(-1, gridCustom1.game_state())
        self.assertTrue(gridCustom1.reveal_tile(0, 2))
        self.assertEqual("_ _ _ \n2 3 2 \nB B B \n", gridCustom1.to_s())
        self.assertEqual(-1, gridCustom1.game_state())

        gridCustom2 = grid.Grid(3, 3, 3)
        self.assertEqual("* * * \n* * * \n* * * \n", gridCustom2.to_s())
        self.assertFalse(gridCustom2.reveal_tile(1, 0))
        self.assertEqual("* 1 * \n* * * \n* * * \n", gridCustom2.to_s())
        self.assertFalse(gridCustom2.reveal_tile(0, 0))
        self.assertEqual("1 1 * \n* * * \n* * * \n", gridCustom2.to_s())
        self.assertFalse(gridCustom2.reveal_tile(2, 0))
        self.assertEqual("1 1 _ \n* 3 2 \n* * * \n", gridCustom2.to_s())
        gridCustom2.flag_tile(0, 1)
        self.assertEqual("1 1 _ \n! 3 2 \n* * * \n", gridCustom2.to_s())
        self.assertFalse(gridCustom2.reveal_tile(0, 2))
        self.assertEqual("1 1 _ \n! 3 2 \n2 * * \n", gridCustom2.to_s())
        self.assertEqual(1, gridCustom2.game_state())

        gridCustom3 = grid.Grid(3, 3, 3)
        self.assertTrue(gridCustom3.reveal_tile(1, 1))
        self.assertEqual("* * * \n* B * \n* * * \n", gridCustom3.to_s())

        gridCustom4 = grid.Grid(3, 3, 3)
        self.assertEqual("* * * \n* * * \n* * * \n", gridCustom4.to_s())
        self.assertFalse(gridCustom4.reveal_tile(1, 0))
        self.assertEqual("* 2 * \n* * * \n* * * \n", gridCustom4.to_s())
        self.assertFalse(gridCustom4.reveal_tile(2, 0))
        self.assertEqual("* 2 1 \n* * * \n* * * \n", gridCustom4.to_s())
        self.assertFalse(gridCustom4.reveal_tile(0, 1))
        self.assertEqual("* 2 1 \n3 * * \n* * * \n", gridCustom4.to_s())
        self.assertFalse(gridCustom4.reveal_tile(2, 1))
        self.assertEqual("* 2 1 \n3 * 2 \n* * * \n", gridCustom4.to_s())
        self.assertFalse(gridCustom4.reveal_tile(0, 2))
        self.assertEqual("* 2 1 \n3 * 2 \n2 * * \n", gridCustom4.to_s())
        self.assertFalse(gridCustom4.reveal_tile(2, 2))
        self.assertEqual("* 2 1 \n3 * 2 \n2 * 2 \n", gridCustom4.to_s())

        gridCustom5 = grid.Grid(4, 4, 4, 0, 0)
        self.assertEqual("* * * * \n* * * * \n* * * * \n* * * * \n",
                         gridCustom5.to_s())
        self.assertFalse(gridCustom5.reveal_tile(0, 0))
        self.assertEqual("_ _ 1 * \n_ _ 2 * \n1 1 4 * \n* * * * \n",
                         gridCustom5.to_s())
        self.assertEqual(0, gridCustom5.game_state())
        self.assertFalse(gridCustom5.reveal_tile(3, 0))
        self.assertEqual(0, gridCustom5.game_state())
        self.assertFalse(gridCustom5.reveal_tile(2, 3))
        self.assertEqual(0, gridCustom5.game_state())
        self.assertFalse(gridCustom5.reveal_tile(0, 3))
        self.assertEqual(1, gridCustom5.game_state())
        self.assertEqual("_ _ 1 1 \n_ _ 2 * \n1 1 4 * \n1 * 3 * \n",
                         gridCustom5.to_s())

        game_grid = grid.Grid(8, 8, 25, 3, 3)
        self.assertEqual(
            "* * * * \n* * * * \n* * * * \n* * * * \n* * * * \n* * * * \n* * * * \n* * * * \n",
            game_grid.to_s())
Esempio n. 7
0
#!/usr/bin/python3
#
# Created by Ian Howell on 12/20/17.
# File name: vis.py
from src import grid
import itertools
import os
import time

board = grid.Grid()
os.system('clear')
print(board)
time.sleep(1)
players = itertools.cycle(['X', 'O'])
with open('log.txt', 'r') as f:
    for line in f:
        pos = int(line)
        player = next(players)
        board.set(pos, player)
        os.system('clear')
        print(board)
        time.sleep(1)
Esempio n. 8
0
def main(args):
    # Initialize the board
    board = grid.Grid()

    player_symbols = itertools.cycle('XO')
    if args['is_x']:
        opponent = ai.AI('X')
        players = {'X': opponent.move, 'O': player_turn}
    else:
        opponent = ai.AI('O')
        players = {'O': opponent.move, 'X': player_turn}

    output_file = 'log.txt'
    if os.path.isfile(output_file):
        os.remove(output_file)

    done = False
    turn = 0
    while not done:
        turn += 1
        player = next(player_symbols)

        if (not args['duel']):
            show_board(board)
            print("It is {}'s turn...".format(player))
            if (turn % 2) == args['is_x']:
                print(opponent.random_nonsense())
                time.sleep(1)

        pos = players[player](board)

        if pos is None:
            print("\rGoodbye!")
            return

        board.set(pos, player)

        if (args['duel']) and (turn % 2 == args['is_x']):
            print(pos)

        if args['print']:
            with open(output_file, 'a') as f:
                f.write(str(pos) + '\n')

        winner = board.check_win()
        if winner or turn >= 9:
            done = True

    if (not args['duel']):
        show_board(board)
        if winner:
            print("{} wins!".format(winner))
        else:
            print("No one wins!")
    else:
        print('done')

    if args['print']:
        with open('results.txt', 'w') as f:
            f.write("{} wins!\n".format(winner))
            f.write(str(board) + '\n')