Update.
[picoclvr.git] / escape.py
index f945172..1c1bc20 100755 (executable)
--- a/escape.py
+++ b/escape.py
@@ -110,13 +110,22 @@ def episodes2seq(states, actions, rewards, lookahead_delta=None):
     actions = actions[:, :, None] + first_actions_code
 
     if lookahead_delta is not None:
-        r = rewards
-        u = F.pad(r, (0, lookahead_delta - 1)).as_strided(
-            (r.size(0), r.size(1), lookahead_delta),
-            (r.size(1) + lookahead_delta - 1, 1, 1),
-        )
-        a = u[:, :, 1:].min(dim=-1).values
-        b = u[:, :, 1:].max(dim=-1).values
+        # r = rewards
+        # u = F.pad(r, (0, lookahead_delta - 1)).as_strided(
+        # (r.size(0), r.size(1), lookahead_delta),
+        # (r.size(1) + lookahead_delta - 1, 1, 1),
+        # )
+        # a = u[:, :, 1:].min(dim=-1).values
+        # b = u[:, :, 1:].max(dim=-1).values
+        # s = (a < 0).long() * a + (a >= 0).long() * b
+        # lookahead_rewards = (1 + s[:, :, None]) + first_lookahead_rewards_code
+
+        # a[n,t]=min_s>t r[n,s]
+        a = rewards.new_zeros(rewards.size())
+        b = rewards.new_zeros(rewards.size())
+        for t in range(a.size(1) - 1):
+            a[:, t] = rewards[:, t + 1 :].min(dim=-1).values
+            b[:, t] = rewards[:, t + 1 :].max(dim=-1).values
         s = (a < 0).long() * a + (a >= 0).long() * b
         lookahead_rewards = (1 + s[:, :, None]) + first_lookahead_rewards_code
 
@@ -165,6 +174,25 @@ def seq2episodes(seq, height, width, lookahead=False):
         return states, actions, rewards
 
 
+def seq2str(seq):
+    def token2str(t):
+        if t >= first_state_code and t < first_state_code + nb_state_codes:
+            return " #@$"[t - first_state_code]
+        elif t >= first_actions_code and t < first_actions_code + nb_actions_codes:
+            return "ISNEW"[t - first_actions_code]
+        elif t >= first_rewards_code and t < first_rewards_code + nb_rewards_codes:
+            return "-0+"[t - first_rewards_code]
+        elif (
+            t >= first_lookahead_rewards_code
+            and t < first_lookahead_rewards_code + nb_lookahead_rewards_codes
+        ):
+            return "n.p"[t - first_lookahead_rewards_code]
+        else:
+            return "?"
+
+    return ["".join([token2str(x.item()) for x in row]) for row in seq]
+
+
 ######################################################################
 
 
@@ -172,7 +200,7 @@ def episodes2str(
     states, actions, rewards, lookahead_rewards=None, unicode=False, ansi_colors=False
 ):
     if unicode:
-        symbols = " █@$"
+        symbols = "·█@$"
         # vert, hori, cross, thin_hori = "║", "═", "╬", "─"
         vert, hori, cross, thin_vert, thin_hori = "┃", "━", "╋", "│", "─"
     else:
@@ -199,16 +227,24 @@ def episodes2str(
                 + "\n"
             )
 
-        result += (vert + thin_hori * states.size(-1)) * states.size(1) + vert + "\n"
+        result += (vert + thin_hori * states.size(-1)) * states.size(1) + vert + "\n"
 
         def status_bar(a, r, lr=None):
             a, r = a.item(), r.item()
             sb_a = "ISNEW"[a] if a >= 0 and a < 5 else "?"
-            sb_r = " " + ("- +"[r + 1] if r in {-1, 0, 1} else "?")
-            if lr is not None:
+            sb_r = "- +"[r + 1] if r in {-1, 0, 1} else "?"
+            if lr is None:
+                sb_lr = ""
+            else:
                 lr = lr.item()
-                sb_r = sb_r + "/" + ("- +"[lr + 1] if lr in {-1, 0, 1} else "?")
-            return sb_a + " " * (states.size(-1) - len(sb_a) - len(sb_r)) + sb_r
+                sb_lr = "n p"[lr + 1] if lr in {-1, 0, 1} else "?"
+            return (
+                sb_a
+                + "/"
+                + sb_r
+                + " " * (states.size(-1) - 1 - len(sb_a + sb_r + sb_lr))
+                + sb_lr
+            )
 
         if lookahead_rewards is None:
             result += (
@@ -244,8 +280,11 @@ def episodes2str(
 ######################################################################
 
 if __name__ == "__main__":
-    nb, height, width, T = 10, 4, 6, 20
+    nb, height, width, T = 25, 5, 7, 25
     states, actions, rewards = generate_episodes(nb, height, width, T)
     seq = episodes2seq(states, actions, rewards, lookahead_delta=T)
     s, a, r, lr = seq2episodes(seq, height, width, lookahead=True)
     print(episodes2str(s, a, r, lookahead_rewards=lr, unicode=True, ansi_colors=True))
+    # print()
+    # for s in seq2str(seq):
+    # print(s)