File size: 3,756 Bytes
7248ace
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from dataclasses import dataclass

import numpy as np


HORIZON = 400
COLUMN = 5
PLAYER_ROW = 9
PHASE_LENGTH = 6


@dataclass
class Wall:
  row: int
  phase: int
  width: int
  period: int
  direction: int

  def blocks(self, column: int) -> bool:
    return (column - self.phase) % self.period < self.width


class BWallsEnvironment:
  score_cap = HORIZON * 2 ** (PLAYER_ROW - 1)

  def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
    self.sticky_action_prob = sticky_action_prob
    self.horizon = min(horizon, HORIZON)
    self.seed(0)
    self.reset()

  def seed(self, seed: int) -> None:
    self.random = np.random.default_rng(seed)

  def reset(self) -> None:
    self.steps = self.wall_clock = self.last_action = self.travel = 0
    self.multiplier = 1
    self.shot: int | None = None
    self.terminal = False
    self.walls: list[Wall] = []
    self._spawn(3)
    self._spawn(0)

  def _spawn(self, row: int) -> None:
    width = int(self.random.integers(1, 3))
    period = width + int(self.random.integers(2, 5))
    self.walls.append(Wall(row, int(self.random.integers(period)), width, period,
                           int(self.random.choice((-1, 1)))))

  @classmethod
  def normalize_score(cls, score: float) -> float:
    return min(max(score, 0), cls.score_cap) / cls.score_cap

  @property
  def stopped(self) -> bool:
    return self.wall_clock // PHASE_LENGTH % 2 == 1

  def act(self, action: int) -> tuple[float, bool]:
    if self.terminal:
      return 0.0, True
    if self.random.random() < self.sticky_action_prob:
      action = self.last_action
    if action == 5 and self.last_action != 5 and self.shot is None:
      self.shot = PLAYER_ROW
      self.multiplier = 1
    self.last_action = action
    self.steps += 1
    moving = not self.stopped
    if moving:
      for wall in self.walls:
        wall.phase = (wall.phase + wall.direction) % wall.period

    near_launch = self.shot is not None and self.shot >= 7
    if near_launch or self.steps % (5 if self.steps < 200 else 4) == 0:
      self.travel += 1
      for wall in self.walls:
        wall.row += 1
      if self.travel % 3 == 0:
        self._spawn(0)

    reward = 0
    if self.shot is not None:
      self.shot -= 1
      if self.shot < 0 or any(wall.row == self.shot and wall.blocks(COLUMN) for wall in self.walls):
        self.shot = None
      else:
        for wall in self.walls:
          if wall.row - 1 == self.shot:
            reward = self.multiplier
            self.multiplier *= 2
            self.walls.remove(wall)
            break
        if moving and self.shot < max((wall.row for wall in self.walls), default=-2) + 1:
          self.shot = None
    if self.shot is None:
      self.wall_clock += 1
    self.terminal = any(wall.row == PLAYER_ROW and wall.blocks(COLUMN) for wall in self.walls)
    self.walls = [wall for wall in self.walls if wall.row <= PLAYER_ROW]
    self.terminal |= self.steps >= self.horizon
    return float(reward), self.terminal

  def state(self) -> np.ndarray:
    state = np.zeros((10, 10, 6), dtype=bool)
    state[PLAYER_ROW, COLUMN, 0] = True
    if self.shot is not None:
      state[self.shot, COLUMN, 3] = True
    for wall in self.walls:
      for column in range(10):
        state[wall.row, column, 1] = wall.blocks(column)
        state[wall.row, column, 4] = (column - wall.phase - wall.direction) % wall.period < wall.width
      if wall.row > 0:
        state[wall.row - 1, COLUMN, 2] = True
    for row, value in enumerate((self.steps, self.wall_clock % (2 * PHASE_LENGTH),
                                 self.travel % 3, self.multiplier, self.last_action)):
      for bit in range(10):
        state[row, bit, 5] = bool(value & (1 << bit))
    return state