Ejemplo n.º 1
0
def play_random(board, save_normalized_matrix=True):

    steps = []
    render_board(board)

    while True:

        #Exit if needed by pressing red cross
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        #Playing randomly
        r = np.random.RandomState()
        action = r.choice(list(range(GameEnv.NB_ACTIONS)))  #Select a random action
        moved = board.move(action)
        matrix = board.normalized_matrix if save_normalized_matrix else board.matrix

        if moved:
            print()
            print(board.matrix)
            print("SCORE:", board.score, "\tSTEP:", board.n_steps_valid, "\tHIGHEST VALUE:", board.highest_value)
            steps.append(Step(matrix=matrix, action=action, action_encoded=encode_action(action)))
            render_board(board)

            if board.is_gameover():
                print("GAME OVER!")
                return Game(steps=steps, score=board.score, random_seed=board.random_seed, is_gameover=True)            

        clock.tick(5)
        pygame.display.flip()
Ejemplo n.º 2
0
 def loop(self):
     center = Point2D(self.width / 2, self.height / 2)
     offset = Point2D(0, 0)
     self.hilbert(center, 1, offset, Step(Step.CONST_DIRECTION_D))
     if self._load_image_flag is True:
         pygame.image.save(self.canvas, self.save_name)
     while self._continue_flag is True:
         # self.canvas.fill(self.BACKGROUND_COLOR)
         pygame.display.flip()
         self.handle_events()
         pass
Ejemplo n.º 3
0
def play(board, save_normalized_matrix=True):
    """
    Parameters
    ----------
    board : numpy.array
    save_normalized_matrix : bool
        Whether to save normalized (log2 transformed) or original matrix.

    Returns
    -------
    collections.namedtuple
        Game with recorded steps.
    """

    steps = []
    render_board(board)

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key in POSSIBLE_ACTIONS:
                    matrix = board.normalized_matrix if save_normalized_matrix else board.matrix
                    action = POSSIBLE_ACTIONS[event.key]
                    moved = board.move(action) #boolean

                    if moved:
                        print()
                        print(board.matrix)
                        print("SCORE:", board.score, "\tSTEP:", board.n_steps_valid, "\tHIGHEST VALUE:", board.highest_value)
                        steps.append(Step(matrix=matrix, action=action, action_encoded=encode_action(action)))
                        render_board(board)

                        if board.is_gameover():
                            print("GAME OVER!")
                            return Game(steps=steps, score=board.score, random_seed=board.random_seed, is_gameover=True)
                    else:
                        print("\nCannot move to this direction!")
                elif event.key == pygame.K_q:
                    screen.fill(BLACK)
                    return Game(steps=steps, random_seed=board.random_seed, is_gameover=False)
                elif event.key == pygame.K_p:
                    screen.fill(BLACK)
                    return "quit"

        clock.tick(60)
        pygame.display.flip()
Ejemplo n.º 4
0
def rollout(env,
            start_state: State,
            policy,
            buffer,
            num_steps=args.subpolicy_duration) -> None:
    s = start_state

    for i in range(num_steps):
        mean, std = policy(np_to_var(s))
        p = distributions.Normal(mean, std)
        a = p.sample()
        succ, r, done, _ = env.step(a.data.numpy())
        env.done = done
        buffer.append(Step(s, a, r, succ, done))
        s = np_to_var(env.reset()) if done else succ
Ejemplo n.º 5
0
def create_lateral_pseudo_steps():
    lateral_files = files_folder.joinpath('lateral')
    element_data = pd.read_excel(lateral_files.joinpath('element_data.xlsx'),
                                 header=None)
    inital_gap = pd.read_excel(lateral_files.joinpath('initial_gap.xlsx'),
                               header=None)
    surface_behav = pd.read_excel(lateral_files.joinpath('surface_behav.xlsx'),
                                  header=None)
    surface_behav = list(surface_behav.ffill().groupby(0).apply(
        lambda df: df.iloc[:, 1:].values))
    assert len(surface_behav) == inital_gap.shape[0] == 61
    max_count = len(surface_behav)
    pseudo_steps = list()
    for i in range(max_count):
        pseudo_step = Step(
            get_keywords(lateral_files.joinpath('lateral_template.txt')))

        element_kw = pseudo_step['Element']

        # Update *Element
        new_name = element_kw.params['ELSET'][:-1] + str(i + 1)
        element_kw.params['ELSET'] = new_name

        # Read i'th row from element_data.xlsx and insert it into element_kw.data
        element_datum = element_data.iloc[i].tolist()
        element_kw.data = [element_datum]

        # Update *GAP
        gap_kw = pseudo_step['GAP']
        gap_kw.params['ELSET'] = new_name

        gap_kw.data[0][0] = -inital_gap.iloc[i, 0]
        if i == 0 or i == max_count - 1:  # if first or last, then ignore
            pass
        else:  # else multiply by 2
            gap_kw.data[0][-1] = float(gap_kw.data[0][-1]) * 2

        # Update *SURFACE BEHAVIOR
        surfbeh_kw = pseudo_step['SURFACE BEHAVIOR']
        surfbeh_kw.data = list(surface_behav[i])
        pseudo_steps.append(pseudo_step)

    write_steps(pseudo_steps,
                results_folder.joinpath('lateral_pseudo_steps.txt'))
Ejemplo n.º 6
0
def process_cycle(cycle_path, out_filepath, pressure_data,
                  mode):  # pressure data is added as argument
    cycle_data = pd.read_csv(cycle_path, index_col=0)
    y_data = cycle_data.drop(['X'], axis=1)
    pressure_generator = it.cycle(pressure_data['p'].tolist(
    ))  # cycle through pressure values at each step
    out_steps = list()
    for step_column in y_data.columns:
        step_num = int(step_column.split('-')[-1])
        the_step = Step.from_template('step_template_e.txt')
        the_step['Step'].params['name'] = f'Step-{step_num}'

        # change the last value of the first row in Dsload card to the next pressure value
        the_step['Dsload'].data[0][-1] = next(pressure_generator)

        row_labels = pd.DataFrame(the_step['Temperature'].data).iloc[:, 0]
        y_values = y_data[step_column].tolist()[::2]
        the_step['Temperature'].data = zip(row_labels, y_values)
        out_steps.append(the_step)
    write_steps(out_steps, out_filepath, mode=mode)
Ejemplo n.º 7
0
def create_end_pseudo_steps():
    end_files = files_folder.joinpath('end')
    end_data = pd.read_excel(end_files.joinpath('element_data_end.xlsx'),
                             header=None)
    area_data = pd.read_excel(end_files.joinpath('area_data.xlsx'),
                              header=None)
    qz_curve = pd.read_excel(end_files.joinpath('qz_curve.xlsx'), header=None)

    max_count = 21
    pseudo_steps = list()
    for i in range(max_count):
        pseudo_step = Step(get_keywords(
            end_files.joinpath('end_template.txt')))

        element_kw = pseudo_step['Element']

        # Update *Element
        new_name = element_kw.params['ELSET'][:-1] + str(i + 1)
        element_kw.params['ELSET'] = new_name

        # Read i'th row from element_data_end.xlsx and insert it into element_kw.data
        element_datum = end_data.iloc[i].tolist()
        element_kw.data = [element_datum]

        # Update *GAP
        gap_kw = pseudo_step['GAP']
        gap_kw.params['ELSET'] = new_name

        # Replace only the last value
        gap_kw.data[0][-1] = area_data.iloc[i, 0]

        # Update *SURFACE BEHAVIOR
        surfbeh_kw = pseudo_step['SURFACE BEHAVIOR']
        surfbeh_kw.data = list(qz_curve.values)
        pseudo_steps.append(pseudo_step)

    write_steps(pseudo_steps, results_folder.joinpath('end_pseudo_steps.txt'))

    pass
Ejemplo n.º 8
0
    def trace_path_by_direction(self, center: Point2D, offset: Point2D,
                                direction: Step):
        if not isinstance(direction, Step):
            raise ValueError('Expected \'direction\' to be a instance of Step')
        if not isinstance(center, Point2D):
            raise ValueError('Expected \'center\' to be a instance of Point2D')
        if not isinstance(offset, Point2D):
            raise ValueError('Expected \'offset\' to be a instance of Point2D')

        this_offset = offset / 2
        if direction.step is Step.CONST_DIRECTION_A:
            first = Point2D(center.x - this_offset.x, center.y + this_offset.y)
            second = Point2D(center.x - this_offset.x,
                             center.y - this_offset.y)
            third = Point2D(center.x + this_offset.x, center.y - this_offset.y)
            fourth = Point2D(center.x + this_offset.x,
                             center.y + this_offset.y)
            path = Path(
                Step(Step.CONST_DIRECTION_D),
                Step(Step.CONST_DIRECTION_A),
                Step(Step.CONST_DIRECTION_A),
                Step(Step.CONST_DIRECTION_B),
            )
        elif direction.step is Step.CONST_DIRECTION_B:
            first = Point2D(center.x + this_offset.x, center.y - this_offset.y)
            second = Point2D(center.x - this_offset.x,
                             center.y - this_offset.y)
            third = Point2D(center.x - this_offset.x, center.y + this_offset.y)
            fourth = Point2D(center.x + this_offset.x,
                             center.y + this_offset.y)
            path = Path(
                Step(Step.CONST_DIRECTION_C),
                Step(Step.CONST_DIRECTION_B),
                Step(Step.CONST_DIRECTION_B),
                Step(Step.CONST_DIRECTION_A),
            )
        elif direction.step is Step.CONST_DIRECTION_C:
            first = Point2D(center.x + this_offset.x, center.y - this_offset.y)
            second = Point2D(center.x + this_offset.x,
                             center.y + this_offset.y)
            third = Point2D(center.x - this_offset.x, center.y + this_offset.y)
            fourth = Point2D(center.x - this_offset.x,
                             center.y - this_offset.y)
            path = Path(
                Step(Step.CONST_DIRECTION_B),
                Step(Step.CONST_DIRECTION_C),
                Step(Step.CONST_DIRECTION_C),
                Step(Step.CONST_DIRECTION_D),
            )
        elif direction.step is Step.CONST_DIRECTION_D:
            first = Point2D(center.x - this_offset.x, center.y + this_offset.y)
            second = Point2D(center.x + this_offset.x,
                             center.y + this_offset.y)
            third = Point2D(center.x + this_offset.x, center.y - this_offset.y)
            fourth = Point2D(center.x - this_offset.x,
                             center.y - this_offset.y)
            path = Path(
                Step(Step.CONST_DIRECTION_A),
                Step(Step.CONST_DIRECTION_D),
                Step(Step.CONST_DIRECTION_D),
                Step(Step.CONST_DIRECTION_C),
            )
        else:
            raise ValueError(
                'Expected the attribute \'step\' in the parameter \'direction\' to be valid'
            )

        return Trace(first, second, third, fourth, path)
Ejemplo n.º 9
0
for hparam in hparams.trials(5):
    exp.add_argparse_meta(hparam)
    for timestep in range(hparam.num_steps):
        noise = Normal(
            mean=Variable(torch.zeros(A)),
            std=hparam.noise_factor * Variable(torch.ones(A)),
        )

        if timestep % 1000 == 0:
            hparam.noise_factor /= 2

        a = actor(s) + noise.sample()
        succ, r, done, _ = env.step(a.data.numpy())
        succ = np_to_var(succ)
        buffer.append(Step(s, a, r, succ, done))
        rews.append(r)
        s = np_to_var(env.reset()) if done else succ
        if done:

            exp.add_metric_row({"Timestep": timestep + 1, "Loss": -sum(rews)})

            rews = []

        if len(buffer) >= hparam.batch_size:
            states, actions, rewards, succ_states, dones = format_batch(
                buffer.sample(hparam.batch_size)
            )

            td_estims = get_critic_train_data(succ_states, rewards, dones)