From: François Fleuret Date: Sun, 24 Mar 2024 10:24:55 +0000 (+0100) Subject: Update. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=9664af37378218468190741c9ea5c3d7cb231926;p=picoclvr.git Update. --- diff --git a/escape.py b/escape.py index aa87cdd..a8b9536 100755 --- a/escape.py +++ b/escape.py @@ -12,13 +12,15 @@ from torch.nn import functional as F ###################################################################### nb_state_codes = 4 -nb_rewards_codes = 3 nb_actions_codes = 5 +nb_rewards_codes = 3 +nb_lookahead_rewards_codes = 3 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 +first_actions_code = first_state_code + nb_state_codes +first_rewards_code = first_actions_code + nb_actions_codes +first_lookahead_rewards_code = first_rewards_code + nb_rewards_codes +nb_codes = first_lookahead_rewards_code + nb_lookahead_rewards_codes ###################################################################### @@ -103,10 +105,24 @@ def generate_episodes(nb, height=6, width=6, T=10): ###################################################################### -def episodes2seq(states, actions, rewards): +def episodes2seq(states, actions, rewards, lookahead_delta=None): states = states.flatten(2) + first_state_code actions = actions[:, :, None] + first_actions_code - rewards = (rewards[:, :, None] + 1) + first_rewards_code + + if lookahead_delta is not None: + r = rewards + print(f"{r.size()=} {lookahead_delta=}") + 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.min(dim=-1).values + b = u.max(dim=-1).values + s = (a < 0).long() * a + (a >= 0).long() * b + lookahead_rewards = (1 + s[:, :, None]) + first_lookahead_rewards_code + + r = rewards[:, :, None] + rewards = (r + 1) + first_rewards_code assert ( states.min() >= first_state_code @@ -121,29 +137,48 @@ def episodes2seq(states, actions, rewards): and rewards.max() < first_rewards_code + nb_rewards_codes ) - return torch.cat([states, actions, rewards], dim=2).flatten(1) + if lookahead_delta is None: + return torch.cat([states, actions, rewards], dim=2).flatten(1) + else: + assert ( + lookahead_rewards.min() >= first_lookahead_rewards_code + and lookahead_rewards.max() + < first_lookahead_rewards_code + nb_lookahead_rewards_codes + ) + return torch.cat([states, actions, rewards, lookahead_rewards], dim=2).flatten( + 1 + ) -def seq2episodes(seq, height, width): - seq = seq.reshape(seq.size(0), -1, height * width + 2) +def seq2episodes(seq, height, width, lookahead=False): + seq = seq.reshape(seq.size(0), -1, height * width + (3 if lookahead else 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 + + if lookahead: + lookahead_rewards = ( + seq[:, :, height * width + 2] - first_lookahead_rewards_code - 1 + ) + return states, actions, rewards, lookahead_rewards + else: + return states, actions, rewards ###################################################################### -def episodes2str(states, actions, rewards, unicode=False, ansi_colors=False): +def episodes2str( + states, actions, rewards, lookahead_rewards=None, unicode=False, ansi_colors=False +): if unicode: symbols = " █@$" # vert, hori, cross, thin_hori = "║", "═", "╬", "─" - vert, hori, cross, thin_hori = "┃", "━", "╋", "─" + vert, hori, cross, thin_vert, thin_hori = "┃", "━", "╋", "│", "─" else: symbols = " #@$" - vert, hori, cross, thin_hori = "|", "-", "+", "-" + vert, hori, cross, thin_vert, thin_hori = "|", "-", "+", "|", "-" hline = (cross + hori * states.size(-1)) * states.size(1) + cross + "\n" @@ -167,11 +202,14 @@ def episodes2str(states, actions, rewards, unicode=False, ansi_colors=False): result += (vert + thin_hori * states.size(-1)) * states.size(1) + vert + "\n" - def status_bar(a, r): - a = a.item() - a = "ISNEW"[a] if a >= 0 and a < 5 else "?" - r = "?" if r < -1 or r > 2 else ("" if r == 0 else f"{r.item()}") - return a + " " * (states.size(-1) - len(a) - len(r)) + r + def status_bar(a, r, lr=None): + a, r = a.item(), r.item() + sb = "ISNEW"[a] if a >= 0 and a < 5 else "?" + sb = sb + thin_vert + ("- +"[r + 1] if r in {-1, 0, 1} else "?") + if lr is not None: + lr = lr.item() + sb = sb + thin_vert + +("- +"[lr + 1] if lr in {-1, 0, 1} else "?") + return sb + " " * (states.size(-1) - len(sb)) result += ( vert @@ -194,6 +232,6 @@ def episodes2str(states, actions, rewards, unicode=False, ansi_colors=False): 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)) + seq = episodes2seq(states, actions, rewards, lookahead_delta=2) + s, a, r, lr = seq2episodes(seq, height, width, lookahead=True) + print(episodes2str(s, a, r, lookahead_rewards=lr, unicode=True, ansi_colors=True))