- # Put a lookahead reward to +1, sample the action and reward
- result[:, u - 1] = (1) + 1 + escape.first_lookahead_rewards_code
- ar_mask = (t >= state_len).long() * (t < state_len + 2).long()
- ar_mask = ar_mask[None, :]
- ar_mask = ar_mask.expand_as(result)
- result *= 1 - ar_mask
- ar()
+ # Re-generate the lookahead_reward optimistically in the
+ # previous iterations
+ ar_mask = (t < u).long() * (t % it_len == index_lookahead_reward).long()
+ ar(result, ar_mask, logit_biases=optimistic_bias)
+ snapshots.append(result[:10].detach().clone())
+
+ # Generate the action and reward
+ ar_mask = (t >= u + index_action).long() * (t <= u + index_reward).long()
+ ar(result, ar_mask)
+ snapshots.append(result[:10].detach().clone())
+
+ filename = os.path.join(result_dir, f"test_thinking_compute_{n_epoch:04d}.txt")
+ with open(filename, "w") as f:
+ for n in range(10):
+ for s in snapshots:
+ s, a, r, lr = escape.seq2episodes(
+ s[n : n + 1], self.height, self.width, lookahead=True
+ )
+ str = escape.episodes2str(
+ s, a, r, lookahead_rewards=lr, unicode=True, ansi_colors=True
+ )
+ f.write(str)
+ f.write("\n\n")