Datasets:
File size: 3,681 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 | from dataclasses import dataclass
import numpy as np
HORIZON = 400
SPAWN_INTERVAL = 8
MAX_FISH = (HORIZON // SPAWN_INTERVAL) * 13
BIRD_ROW = 2
NORMAL, BIG, FAKE, EYE = range(4)
FLIGHT = (8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9)
FRAGMENTS = ((0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3),
(1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 5))
@dataclass
class Fish:
column: int
kind: int = NORMAL
age: int = 0
row_offset: int = 0
@property
def row(self) -> int:
return max(0, min(9, FLIGHT[self.age] + self.row_offset))
class BSFishEnvironment:
score_cap = MAX_FISH * (1 + 5 * (MAX_FISH - 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.last_action = self.speed = 0
self.multiplier = 1
self.special_in = 3
self.fish: list[Fish] = []
self.terminal = False
@property
def bird_column(self) -> int:
return 1 + self.speed // 2
@classmethod
def normalize_score(cls, score: float) -> float:
return min(max(score, 0), cls.score_cap) / cls.score_cap
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
self.last_action = action
self.steps += 1
self.speed = min(6, self.speed + 1) if action == 5 else max(0, self.speed - 1)
if self.steps % SPAWN_INTERVAL == 1:
if self.special_in == 0:
kind = int(self.random.choice((BIG, FAKE)))
self.special_in = int(self.random.integers(3, 9))
else:
kind = NORMAL
self.special_in -= 1
self.fish.append(Fish(int(self.random.integers(7, 10)), kind))
reward = 0
remaining = []
for fish in self.fish:
fish.age += 1
if self.speed >= 4 or self.steps % 3 == 0:
fish.column -= 1
if fish.age == len(FLIGHT) or fish.column < -1:
if fish.kind != BIG:
self.multiplier = max(1, self.multiplier - 1)
continue
if fish.kind == FAKE and fish.row <= 5:
remaining.extend(Fish(fish.column + column - 2, EYE if (row, column) == (1, 2) else NORMAL,
fish.age, row - 1) for row, column in FRAGMENTS)
elif fish.kind == BIG:
if abs(fish.row - BIRD_ROW) <= 1 and abs(fish.column - self.bird_column) <= 1:
self.terminal = True
remaining.append(fish)
elif fish.row == BIRD_ROW and fish.column == self.bird_column:
reward += self.multiplier
self.multiplier += 10 if fish.kind == EYE else 1
else:
remaining.append(fish)
self.fish = remaining
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[BIRD_ROW, self.bird_column, 0] = True
for fish in self.fish:
if 0 <= fish.column < 10:
channel = (2, 1, 1, 3)[fish.kind]
state[fish.row, fish.column, channel] = True
next_row = FLIGHT[min(fish.age + 1, len(FLIGHT) - 1)] + fish.row_offset
if 0 <= next_row < 10:
state[next_row, fish.column, 4] = True
for row, value in enumerate((self.steps, self.speed, self.multiplier, self.special_in, self.last_action)):
for bit in range(13):
index = row * 13 + bit
state[index // 10, index % 10, 5] = bool(value & (1 << bit))
return state
|