18 """Decode native flat genomes once and apply a different affine map per lane."""
20 def __init__(self, policy, genomes):
22 values = np.asarray(genomes, dtype=np.float32)
23 if not np.isfinite(values).all():
24 raise ValueError(
"Expected finite actor parameters.")
25 parameters = policy._tensor(values)
26 self.
width = len(genomes)
29 for layer
in policy._network:
30 if isinstance(layer, nn.Linear):
31 size = layer.weight.numel()
32 weight = parameters[:, offset:offset + size].reshape(len(genomes), *layer.weight.shape)
34 bias = parameters[:, offset:offset + layer.out_features]
35 offset += layer.out_features
36 self.
layers.append((weight, bias))
41 def actions(self, lanes, observations):
42 indices = (
None if len(lanes) == self.
width
43 else torch.as_tensor(lanes, dtype=torch.long, device=self.
policy.device))
44 outputs = self.
policy._tensor(np.stack(observations))
46 if isinstance(layer, tuple):
48 if indices
is not None:
49 weight, bias = weight[indices], bias[indices]
50 outputs = torch.baddbmm(bias.unsqueeze(-1), weight, outputs.unsqueeze(-1)).squeeze(-1)
52 outputs = layer(outputs)
53 if isinstance(self.
policy, CategoricalPolicy):
54 indices = self.
policy._greedy_indices(outputs).cpu().numpy()
55 return [self.
policy._actions.decode(row)
for row
in indices]
56 return [self.
policy._decode(row)
for row
in outputs.tanh().cpu().numpy()]
60 """Collect the same episode data as the single-lane rollout."""
62 def __init__(self, cursor, seed):
69 def step(self, action, budget, observation_shape):
70 self.
actions.append(action.copy()
if isinstance(action, np.ndarray)
else action)
71 reward, terminated, truncated, info = self.
cursor.step(action, budget,
"evaluation_steps")
77 deepcopy(dict(info)), terminated, truncated)
82def _evaluate_batch(actor, cursors, genomes, budget, seeds, episode_count):
92 assigned = seeds.integers(0, 2**32, size=(len(cursors), episode_count), dtype=np.uint64)