28 """Train one optimizer step per operator feedback once replay is ready.
30 :param hidden_sizes: Widths of the operator-value network.
31 :param replay_capacity: Maximum retained archive feedback transitions.
32 :param batch_size: Number of sampled feedback transitions per update.
33 :param learning_rate: Adam learning rate.
34 :param discount: Future archive reward discount, in [0, 1).
35 :param epsilon: Probability of uniform operator exploration.
36 :param target_period: Optimizer steps between target-network copies.
39 hidden_sizes: tuple[int, ...] = (32, 32)
40 replay_capacity: int = 2048
42 learning_rate: float = 5e-4
45 target_period: int = 24
47 def __post_init__(self):
49 for name
in (
"replay_capacity",
"batch_size",
"target_period"):
50 object.__setattr__(self, name, _integer(getattr(self, name), name))
53 raise ValueError(
"Use valid replay, learning-rate, discount, and exploration settings.")
58 """Use a DQN over a bounded summary of the current parent and archive.
60 State includes global and local occupancy, recent admission reward,
61 oriented parent fitness, and the parent's descriptor coordinates.
62 The next state describes the next sampled parent. Replay and a lagged
63 target network allow learning across multiple search calls.
65 :param archive: An empty two-objective hypervolume ParetoGrid.
66 :param initializer: Callback taking a private generator and returning a genome.
67 :param operators: Mapping of names to callbacks taking (parent, donor, rng).
68 :param config: Optional DMOMEConfig.
69 :param initial_samples: Number of initial random candidates.
70 :param seed: Seed for proposal and controller streams.
71 :param device: Torch CPU or CUDA device.
74 def __init__(self, archive, *, initializer, operators, config=None, initial_samples=16, seed=0, device="cpu"):
75 super().__init__(archive, initializer=initializer, operators=operators,
76 initial_samples=initial_samples, seed=seed)
78 if not isinstance(self.
config, DMOMEConfig):
79 raise TypeError(
"config must be a DMOMEConfig.")
80 self.
_device = torch.device(device)
81 streams = np.random.SeedSequence(seed, spawn_key=(4,)).spawn(2)
82 initialization, self.
_sampling = map(np.random.default_rng, streams)
83 self.
_online = _mlp(5 + len(archive.features_domain), len(operators),
89 def _choose_operator(self, state):
91 return super()._choose_operator(state)
93 return int(self.
_online(torch.as_tensor(state, device=self.
_device)).argmax().item())
95 def _observe(self, state, action, reward, successor):
96 self.
_replay.append((state, action, reward, successor))
100 states, actions, rewards, next_states = zip(*(self.
_replay[i]
for i
in indices))
101 inputs = torch.as_tensor(np.stack(states), device=self.
_device)
102 selections = torch.as_tensor(actions, dtype=torch.int64, device=self.
_device)
103 predicted = self.
_online(inputs).gather(1, selections[:,
None]).squeeze(1)
104 with torch.no_grad():
105 future = self.
_target(torch.as_tensor(np.stack(next_states), device=self.
_device)).amax(dim=1)
106 target = torch.as_tensor(rewards, dtype=torch.float32, device=self.
_device) + self.
config.discount * future
107 loss = F.smooth_l1_loss(predicted, target)
108 if not torch.isfinite(loss):
109 raise ValueError(
"The operator DQN produced a nonfinite loss.")
113 self.
_counts[
"optimizer_steps"] += 1
114 if self.
_counts[
"optimizer_steps"] % self.
config.target_period == 0:
116 self.
_counts[
"target_syncs"] += 1