--- /dev/null
+#!/usr/bin/env python
+
+import torch
+
+from torch.nn import functional as F
+
+######################################################################
+
+
+def generate_sequence(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))
+
+ seq = wall[:, None, :, :].expand(-1, T, -1, -1).clone()
+
+ agent = torch.zeros(seq.size(), dtype=torch.int64)
+ agent[:, 0, 0, 0] = 1
+ agent_actions = torch.randint(5, (nb, T))
+ monster = torch.zeros(seq.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
+
+ seq += 2 * agent + 3 * monster
+
+ return seq, agent_actions
+
+
+######################################################################
+
+
+def seq2str(seq, actions=None):
+ # symbols=" #@$"
+ symbols = " █@$"
+
+ hline = ("+" + "-" * seq.size(-1)) * seq.size(1) + "+" + "\n"
+
+ result = hline
+
+ for n in range(seq.size(0)):
+ for i in range(seq.size(2)):
+ result += (
+ "|"
+ + "|".join(
+ ["".join([symbols[v.item()] for v in row]) for row in seq[n, :, i]]
+ )
+ + "|"
+ + "\n"
+ )
+
+ result += hline
+
+ if actions is not None:
+ result += (
+ "|"
+ + "|".join(
+ ["INESW"[a.item()] + " " * (seq.size(-1) - 1) for a in actions[n]]
+ )
+ + "|"
+ + "\n"
+ )
+
+ result += hline
+
+ return result
+
+
+######################################################################
+
+if __name__ == "__main__":
+ seq, actions = generate_sequence(40, 4, 6, T=20)
+
+ print(seq2str(seq, actions))
def create_maze(h=11, w=17, nb_walls=8):
assert h % 2 == 1 and w % 2 == 1
- a, k = 0, 0
+ nb_attempts, nb_added_walls = 0, 0
- while k < nb_walls:
+ while nb_added_walls < nb_walls:
while True:
- if a == 0:
+ if nb_attempts == 0:
m = torch.zeros(h, w, dtype=torch.int64)
m[0, :] = 1
m[-1, :] = 1
r = torch.rand(4)
if r[0] <= 0.5:
+ # Add a vertical wall
i1, i2, j = (
int((r[1] * h).item()),
int((r[2] * h).item()),
)
i1, i2, j = i1 - i1 % 2, i2 - i2 % 2, j - j % 2
i1, i2 = min(i1, i2), max(i1, i2)
+
+ # If this wall does not hit another one, add it
if i2 - i1 > 1 and i2 - i1 <= h / 2 and m[i1 : i2 + 1, j].sum() <= 1:
m[i1 : i2 + 1, j] = 1
break
+
else:
+ # Add an horizontal wall
i, j1, j2 = (
int((r[1] * h).item()),
int((r[2] * w).item()),
)
i, j1, j2 = i - i % 2, j1 - j1 % 2, j2 - j2 % 2
j1, j2 = min(j1, j2), max(j1, j2)
+
+ # If this wall does not hit another one, add it
if j2 - j1 > 1 and j2 - j1 <= w / 2 and m[i, j1 : j2 + 1].sum() <= 1:
m[i, j1 : j2 + 1] = 1
break
- a += 1
- if a > 10 * nb_walls:
- a, k = 0, 0
+ nb_attempts += 1
+
+ if nb_attempts > 10 * nb_walls:
+ nb_attempts, nb_added_walls = 0, 0
- k += 1
+ nb_added_walls += 1
return m