+++ /dev/null
-#!/usr/bin/env python
-
-import torch
-
-from torch.nn import functional as F
-
-######################################################################
-
-nb_state_codes = 4
-nb_rewards_codes = 3
-nb_actions_codes = 5
-
-first_state_code = 0
-first_rewards_code = first_state_code + nb_state_codes
-first_actions_code = first_rewards_code + nb_rewards_codes
-nb_codes = first_actions_code + nb_actions_codes
-
-######################################################################
-
-
-def generate_episodes(nb, height=6, width=6, T=10):
- rnd = torch.rand(nb, height, width)
- rnd[:, 0, :] = 0
- rnd[:, -1, :] = 0
- rnd[:, :, 0] = 0
- rnd[:, :, -1] = 0
- wall = 0
-
- for k in range(3):
- wall = wall + (
- rnd.flatten(1).argmax(dim=1)[:, None]
- == torch.arange(rnd.flatten(1).size(1))[None, :]
- ).long().reshape(rnd.size())
- rnd = rnd * (1 - wall.clamp(max=1))
-
- states = wall[:, None, :, :].expand(-1, T, -1, -1).clone()
-
- agent = torch.zeros(states.size(), dtype=torch.int64)
- agent[:, 0, 0, 0] = 1
- agent_actions = torch.randint(5, (nb, T))
- rewards = torch.zeros(nb, T, dtype=torch.int64)
-
- monster = torch.zeros(states.size(), dtype=torch.int64)
- monster[:, 0, -1, -1] = 1
- monster_actions = torch.randint(5, (nb, T))
-
- all_moves = agent.new(nb, 5, height, width)
- for t in range(T - 1):
- all_moves.zero_()
- all_moves[:, 0] = agent[:, t]
- all_moves[:, 1, 1:, :] = agent[:, t, :-1, :]
- all_moves[:, 2, :-1, :] = agent[:, t, 1:, :]
- all_moves[:, 3, :, 1:] = agent[:, t, :, :-1]
- all_moves[:, 4, :, :-1] = agent[:, t, :, 1:]
- a = F.one_hot(agent_actions[:, t], num_classes=5)[:, :, None, None]
- after_move = (all_moves * a).sum(dim=1)
- collision = (
- (after_move * (1 - wall) * (1 - monster[:, t]))
- .flatten(1)
- .sum(dim=1)[:, None, None]
- == 0
- ).long()
- agent[:, t + 1] = collision * agent[:, t] + (1 - collision) * after_move
-
- all_moves.zero_()
- all_moves[:, 0] = monster[:, t]
- all_moves[:, 1, 1:, :] = monster[:, t, :-1, :]
- all_moves[:, 2, :-1, :] = monster[:, t, 1:, :]
- all_moves[:, 3, :, 1:] = monster[:, t, :, :-1]
- all_moves[:, 4, :, :-1] = monster[:, t, :, 1:]
- a = F.one_hot(monster_actions[:, t], num_classes=5)[:, :, None, None]
- after_move = (all_moves * a).sum(dim=1)
- collision = (
- (after_move * (1 - wall) * (1 - agent[:, t + 1]))
- .flatten(1)
- .sum(dim=1)[:, None, None]
- == 0
- ).long()
- monster[:, t + 1] = collision * monster[:, t] + (1 - collision) * after_move
-
- hit = (
- (agent[:, t + 1, 1:, :] * monster[:, t + 1, :-1, :]).flatten(1).sum(dim=1)
- + (agent[:, t + 1, :-1, :] * monster[:, t + 1, 1:, :]).flatten(1).sum(dim=1)
- + (agent[:, t + 1, :, 1:] * monster[:, t + 1, :, :-1]).flatten(1).sum(dim=1)
- + (agent[:, t + 1, :, :-1] * monster[:, t + 1, :, 1:]).flatten(1).sum(dim=1)
- )
- hit = (hit > 0).long()
-
- assert hit.min() == 0 and hit.max() <= 1
-
- rewards[:, t + 1] = -hit + (1 - hit) * agent[:, t + 1, -1, -1]
-
- states += 2 * agent + 3 * monster
-
- return states, agent_actions, rewards
-
-
-######################################################################
-
-
-def episodes2seq(states, actions, rewards):
- states = states.flatten(2) + first_state_code
- actions = actions[:, :, None] + first_actions_code
- rewards = (rewards[:, :, None] + 1) + first_rewards_code
-
- assert (
- states.min() >= first_state_code
- and states.max() < first_state_code + nb_state_codes
- )
- assert (
- actions.min() >= first_actions_code
- and actions.max() < first_actions_code + nb_actions_codes
- )
- assert (
- rewards.min() >= first_rewards_code
- and rewards.max() < first_rewards_code + nb_rewards_codes
- )
-
- return torch.cat([states, actions, rewards], dim=2).flatten(1)
-
-
-def seq2episodes(seq, height, width):
- seq = seq.reshape(seq.size(0), -1, height * width + 2)
- states = seq[:, :, : height * width] - first_state_code
- states = states.reshape(states.size(0), states.size(1), height, width)
- actions = seq[:, :, height * width] - first_actions_code
- rewards = seq[:, :, height * width + 1] - first_rewards_code - 1
- return states, actions, rewards
-
-
-######################################################################
-
-
-def episodes2str(states, actions, rewards, unicode=False, ansi_colors=False):
- if unicode:
- symbols = " █@$"
- # vert, hori, cross, thin_hori = "║", "═", "╬", "─"
- vert, hori, cross, thin_hori = "┃", "━", "╋", "─"
- else:
- symbols = " #@$"
- vert, hori, cross, thin_hori = "|", "-", "+", "-"
-
- hline = (cross + hori * states.size(-1)) * states.size(1) + cross + "\n"
-
- result = hline
-
- for n in range(states.size(0)):
- for i in range(states.size(2)):
- result += (
- vert
- + vert.join(
- [
- "".join([symbols[v.item()] for v in row])
- for row in states[n, :, i]
- ]
- )
- + vert
- + "\n"
- )
-
- result += (vert + thin_hori * states.size(-1)) * states.size(1) + vert + "\n"
-
- def status_bar(a, r):
- a = "ISNEW"[a.item()]
- r = "" if r == 0 else f"{r.item()}"
- return a + " " * (states.size(-1) - len(a) - len(r)) + r
-
- result += (
- vert
- + vert.join([status_bar(a, r) for a, r in zip(actions[n], rewards[n])])
- + vert
- + "\n"
- )
-
- result += hline
-
- if ansi_colors:
- for u, c in [("$", 31), ("@", 32)]:
- result = result.replace(u, f"\u001b[{c}m{u}\u001b[0m")
-
- return result
-
-
-######################################################################
-
-if __name__ == "__main__":
- nb, height, width, T = 8, 4, 6, 20
- states, actions, rewards = generate_episodes(nb, height, width, T)
- seq = episodes2seq(states, actions, rewards)
- s, a, r = seq2episodes(seq, height, width)
- print(episodes2str(s, a, r, unicode=True, ansi_colors=True))