5 from torch.nn import functional as F
7 ######################################################################
10 def generate_sequence(nb, height=6, width=6, T=10):
11 rnd = torch.rand(nb, height, width)
20 rnd.flatten(1).argmax(dim=1)[:, None]
21 == torch.arange(rnd.flatten(1).size(1))[None, :]
22 ).long().reshape(rnd.size())
23 rnd = rnd * (1 - wall.clamp(max=1))
25 seq = wall[:, None, :, :].expand(-1, T, -1, -1).clone()
27 agent = torch.zeros(seq.size(), dtype=torch.int64)
29 agent_actions = torch.randint(5, (nb, T))
30 rewards = torch.zeros(nb, T, dtype=torch.int64)
32 monster = torch.zeros(seq.size(), dtype=torch.int64)
33 monster[:, 0, -1, -1] = 1
34 monster_actions = torch.randint(5, (nb, T))
36 all_moves = agent.new(nb, 5, height, width)
37 for t in range(T - 1):
39 all_moves[:, 0] = agent[:, t]
40 all_moves[:, 1, 1:, :] = agent[:, t, :-1, :]
41 all_moves[:, 2, :-1, :] = agent[:, t, 1:, :]
42 all_moves[:, 3, :, 1:] = agent[:, t, :, :-1]
43 all_moves[:, 4, :, :-1] = agent[:, t, :, 1:]
44 a = F.one_hot(agent_actions[:, t], num_classes=5)[:, :, None, None]
45 after_move = (all_moves * a).sum(dim=1)
47 (after_move * (1 - wall) * (1 - monster[:, t]))
49 .sum(dim=1)[:, None, None]
52 agent[:, t + 1] = collision * agent[:, t] + (1 - collision) * after_move
55 all_moves[:, 0] = monster[:, t]
56 all_moves[:, 1, 1:, :] = monster[:, t, :-1, :]
57 all_moves[:, 2, :-1, :] = monster[:, t, 1:, :]
58 all_moves[:, 3, :, 1:] = monster[:, t, :, :-1]
59 all_moves[:, 4, :, :-1] = monster[:, t, :, 1:]
60 a = F.one_hot(monster_actions[:, t], num_classes=5)[:, :, None, None]
61 after_move = (all_moves * a).sum(dim=1)
63 (after_move * (1 - wall) * (1 - agent[:, t + 1]))
65 .sum(dim=1)[:, None, None]
68 monster[:, t + 1] = collision * monster[:, t] + (1 - collision) * after_move
71 (agent[:, t + 1, 1:, :] * monster[:, t + 1, :-1, :]).flatten(1).sum(dim=1)
72 + (agent[:, t + 1, :-1, :] * monster[:, t + 1, 1:, :]).flatten(1).sum(dim=1)
73 + (agent[:, t + 1, :, 1:] * monster[:, t + 1, :, :-1]).flatten(1).sum(dim=1)
74 + (agent[:, t + 1, :, :-1] * monster[:, t + 1, :, 1:]).flatten(1).sum(dim=1)
76 hit = (hit > 0).long()
78 assert hit.min() == 0 and hit.max() <= 1
80 rewards[:, t] = -hit + (1 - hit) * agent[:, t + 1, -1, -1]
82 seq += 2 * agent + 3 * monster
84 return seq, agent_actions, rewards
87 ######################################################################
90 def seq2str(seq, actions, rewards):
92 # vert, hori, cross, thin_hori = "|", "-", "+", "-"
95 vert, hori, cross, thin_hori = "║", "═", "╬", "─"
96 vert, hori, cross, thin_hori = "┃", "━", "╋", "─"
98 # hline = ("+" + "-" * seq.size(-1)) * seq.size(1) + "+" + "\n"
99 hline = (cross + hori * seq.size(-1)) * seq.size(1) + cross + "\n"
103 for n in range(seq.size(0)):
104 for i in range(seq.size(2)):
108 ["".join([symbols[v.item()] for v in row]) for row in seq[n, :, i]]
115 result += (vert + thin_hori * seq.size(-1)) * seq.size(1) + vert + "\n"
117 def status_bar(a, r):
118 a = "INESW"[a.item()]
120 return a + " " * (seq.size(-1) - len(a) - len(r)) + r
124 + vert.join([status_bar(a, r) for a, r in zip(actions[n], rewards[n])])
134 ######################################################################
136 if __name__ == "__main__":
137 seq, actions, rewards = generate_sequence(10, 4, 6, T=20)
139 print(seq2str(seq, actions, rewards))