X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=escape.py;h=f51863b0c42e9c4c0d38fa334296c6ebfca9ed04;hb=19ec7f3e4030ddece2647983dcf1bed5eb0d9544;hp=a2e378d882814c14192b664829ce0081b3583cb8;hpb=49218a3e2adb19d2ec98a454e0076c3461ab1c69;p=picoclvr.git diff --git a/escape.py b/escape.py index a2e378d..f51863b 100755 --- a/escape.py +++ b/escape.py @@ -25,7 +25,7 @@ nb_codes = first_lookahead_rewards_code + nb_lookahead_rewards_codes ###################################################################### -def generate_episodes(nb, height=6, width=6, T=10): +def generate_episodes(nb, height=6, width=6, T=10, nb_walls=3): rnd = torch.rand(nb, height, width) rnd[:, 0, :] = 0 rnd[:, -1, :] = 0 @@ -33,11 +33,12 @@ def generate_episodes(nb, height=6, width=6, T=10): rnd[:, :, -1] = 0 wall = 0 - for k in range(3): + for k in range(nb_walls): 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)) states = wall[:, None, :, :].expand(-1, T, -1, -1).clone() @@ -93,7 +94,7 @@ def generate_episodes(nb, height=6, width=6, T=10): ) hit = (hit > 0).long() - assert hit.min() == 0 and hit.max() <= 1 + # assert hit.min() == 0 and hit.max() <= 1 rewards[:, t + 1] = -hit + (1 - hit) * agent[:, t + 1, -1, -1] @@ -110,40 +111,49 @@ 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 r = rewards[:, :, None] rewards = (r + 1) + first_rewards_code - assert ( - states.min() >= first_state_code - and states.max() < first_state_code + nb_state_codes - ) - assert ( - actions.min() >= first_actions_code - and actions.max() < first_actions_code + nb_actions_codes - ) - assert ( - rewards.min() >= first_rewards_code - and rewards.max() < first_rewards_code + nb_rewards_codes - ) + # assert ( + # states.min() >= first_state_code + # and states.max() < first_state_code + nb_state_codes + # ) + # assert ( + # actions.min() >= first_actions_code + # and actions.max() < first_actions_code + nb_actions_codes + # ) + # assert ( + # rewards.min() >= first_rewards_code + # and rewards.max() < first_rewards_code + nb_rewards_codes + # ) 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 - ) + # 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 ) @@ -191,7 +201,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: @@ -218,7 +228,7 @@ 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() @@ -271,11 +281,11 @@ def episodes2str( ###################################################################### if __name__ == "__main__": - nb, height, width, T = 10, 4, 6, 20 - states, actions, rewards = generate_episodes(nb, height, width, T) + nb, height, width, T, nb_walls = 25, 5, 7, 25, 5 + states, actions, rewards = generate_episodes(nb, height, width, T, nb_walls) 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) + # print() + # for s in seq2str(seq): + # print(s)