######################################################################
+def action2code(r):
+ return first_actions_code + r
+
+
+def code2action(r):
+ return r - first_actions_code
+
+
+def reward2code(r):
+ return first_rewards_code + r + 1
+
+
+def code2reward(r):
+ return r - first_rewards_code - 1
+
+
+def lookahead_reward2code(r):
+ return first_lookahead_rewards_code + r + 1
+
+
+def code2lookahead_reward(r):
+ return r - first_lookahead_rewards_code - 1
+
+
+######################################################################
+
+
def generate_episodes(nb, height=6, width=6, T=10, nb_walls=3):
rnd = torch.rand(nb, height, width)
rnd[:, 0, :] = 0
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
- # 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):
# Generate iteration after iteration
optimistic_bias = result.new_zeros(self.nb_codes, device=result.device)
- optimistic_bias[(-1) + escape.first_lookahead_rewards_code + 1] = math.log(1e-1)
- optimistic_bias[(1) + escape.first_lookahead_rewards_code + 1] = math.log(1e1)
+ optimistic_bias[escape.lookahead_reward2code(-1)] = -math.log(1e1)
+ optimistic_bias[escape.lookahead_reward2code(1)] = math.log(1e1)
for u in tqdm.tqdm(
range(it_len, result.size(1) - it_len + 1, it_len), desc="thinking"
):
- # Generate the lookahead_reward pessimistically
+ # Re-generate the lookahead_reward pessimistically in the
+ # previous iterations
ar_mask = (t < u).long() * (t % it_len == index_lookahead_reward).long()
ar(result, ar_mask, logit_biases=-optimistic_bias)
ar_mask = (t >= u).long() * (t < u + state_len).long()
ar(result, ar_mask)
- # Generate the lookahead_reward optimistically
+ # 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)