Update.
[picoclvr.git] / evasion.py
diff --git a/evasion.py b/evasion.py
new file mode 100755 (executable)
index 0000000..4efa4b3
--- /dev/null
@@ -0,0 +1,117 @@
+#!/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))