- for u in range(itl, result.size(1) - itl + 1, itl):
- print(f"{itl=} {u=} {result.size(1)=}")
- result[:, u - 1] = (-1) + 1 + escape.first_lookahead_rewards_code
- ar_mask = (t >= u).long() * (t < u + self.height * self.width).long()
- ar_mask = ar_mask[None, :]
- ar_mask = ar_mask.expand_as(result)
- result *= 1 - ar_mask
- ar()
- result[:, u - 1] = (1) + 1 + escape.first_lookahead_rewards_code
- ar_mask = (t >= self.height * self.width).long() * (
- t < self.height * self.width + 2
- ).long()
- ar_mask = ar_mask[None, :]
- ar_mask = ar_mask.expand_as(result)
- result *= 1 - ar_mask
- ar()
+ # Generate iteration after iteration
+
+ for u in tqdm.tqdm(
+ range(it_len, result.size(1) - it_len + 1, it_len), desc="thinking"
+ ):
+ # Put the lookahead reward to -1 for the current iteration,
+ # sample the next state
+ s = -1
+ result[:, u - 1] = s + 1 + escape.first_lookahead_rewards_code
+ ar_mask = (t >= u).long() * (t < u + state_len).long()
+ ar(result, ar_mask)
+
+ # Put the lookahead reward to +1 for the current
+ # iteration, sample the action and reward
+ s = 1
+ result[:, u - 1] = s + 1 + escape.first_lookahead_rewards_code
+ ar_mask = (t >= u + state_len).long() * (t < u + state_len + 2).long()
+ ar(result, ar_mask)
+
+ # Fix the previous lookahead rewards in a consistant state
+ for v in range(0, u, it_len):
+ # Extract the rewards
+ r = result[:, range(v + state_len + 1 + it_len, u + it_len - 1, it_len)]
+ r = r - escape.first_lookahead_rewards_code - 1
+ a = r.min(dim=1).values
+ b = r.max(dim=1).values
+ s = (a < 0).long() * a + (a >= 0).long() * b
+ result[:, v + state_len + 2] = (
+ s + 1 + escape.first_lookahead_rewards_code
+ )