20def _denoise(particles, references, log_density, alpha, next_alpha, noise_scale, rng):
48 """Run bounded, population-only Diffusion Evolution with a reporting grid.
50 Shape Gaussian denoising estimates using the previous generation's
51 signed objective. The reporting archive fills from measured candidates
52 and stays outside the proposal loop. Progress through a cosine
53 schedule, then start another Gaussian population if more budget
54 remains. Bounds and fitness scaling adapt the paper to the native
55 fixed-generation evaluator API.
57 :param container: Parentless best-only scalar Grid or CVTGrid.
58 :param budget: Number of attempted candidate evaluations.
59 :param dimension: Number of real genome coordinates.
60 :param batch_size: Maximum population size.
61 :param ind_domain: Shared finite lower and upper bounds.
62 :param diffusion_steps: Generations per cosine schedule, at least two.
63 :param temperature: Positive fitness temperature for denoising weights.
64 :param noise_scale: DDIM noise multiplier between zero and one.
65 :param seed: Private seed for Gaussian proposals and restart populations.
66 :param kwargs: Native optimizer options, such as optimisation_task.
69 _requires_proposed_genome =
True
71 def __init__(self, container, budget, dimension, batch_size, *, ind_domain=(-1., 1.),
72 diffusion_steps=20, temperature=.25, noise_scale=1., seed=0, **kwargs):
73 _validate_grid(container, empty=
False)
75 steps = _positive_int(diffusion_steps,
"diffusion_steps")
76 if (steps < 2
or not np.isfinite(temperature)
or temperature <= 0.
77 or not 0. <= noise_scale <= 1.):
78 raise ValueError(
"Use at least two steps, positive finite temperature, and noise_scale in [0, 1].")
79 self.
_schedule = 1e-4 + (1. - 2e-4) * np.square(np.sin(np.linspace(0., np.pi / 2, steps)))
81 kwargs.setdefault(
"optimisation_task", container.fitness_weights
or "maximisation")
82 super().__init__(container, _positive_int(budget,
"budget"),
83 dimension=_positive_int(dimension,
"dimension"),
84 batch_size=_positive_int(batch_size,
"batch_size"), **kwargs)
86 raise ValueError(
"Use a finite nonzero objective weight.")
89 """Reset the diffusion schedule and its previous measured population."""
91 self.
_rng = np.random.default_rng(self.
_seed)
98 """Return the number of candidates produced by completed denoising steps."""
101 def _normalize(self, entries):
103 return 2. * ((np.asarray(entries, dtype=float) - lower) / (upper - lower)) - 1.
105 def _sources(self, size, alpha):
109 parent_ids = np.arange(size)
if len(vectors) == size
else self.
_rng.integers(len(vectors), size=size)
110 return vectors[parent_ids], vectors, quality
119 particles, vectors, quality = sources
120 latent = _denoise(particles, vectors, _log_density(quality, self.
_temperature),
124 proportion = (.5 + .5 * np.clip(latent, -1., 1.))
125 self.
_proposals = np.clip(lower + (upper - lower) * proportion, lower, upper)
131 def _clear_generation(self):
132 super()._clear_generation()
135 def _check_candidate(self, individual):
137 fitness = self.
container.get_ind_fitness(individual)
138 features = self.
container.get_ind_features(individual)
139 if len(fitness.values) != 1
or len(features) != len(self.
container.features_domain):
140 raise ValueError(
"Return one fitness value and the reporting grid's descriptor vector.")
142 def _fitness(self, individual):
143 return self.
container.get_ind_fitness(individual).values[0]
145 def _reference_data(self, entries):
148 fitness = direction * np.array([self.
_fitness(item)
for item
in entries], dtype=float)
149 return genomes, fitness
152 valid = [item
for item
in evaluated
if item
is not None]
156 for slot, item
in enumerate(evaluated):
159 features = archive.get_ind_features(item)
160 if (archive.in_bounds(features, archive.features_domain)
161 and archive.in_bounds((self.
_fitness(item),), archive.fitness_domain)
170 """Use archive elites as references and seed particles across occupied niches.
172 Each reference is a genuinely measured elite. Draw an occupied cell
173 for each particle and add forward noise at the current schedule stage.
174 The same reverse update as DiffusionEvolution proposes offspring.
175 Select a bounded uniform subset if the archive grows too large.
177 :param max_elites: Maximum number of occupied cells in the denoising reference pool.
180 def __init__(self, container, budget, dimension, batch_size, *, max_elites=256, **kwargs):
181 self.
max_elites = _positive_int(max_elites,
"max_elites")
182 super().__init__(container, budget, dimension, batch_size, **kwargs)
184 def _sources(self, size, alpha):
186 return super()._sources(size, alpha)
189 indices = self.
_rng.choice(indices, size=self.
max_elites, replace=
False)
191 if (vectors.shape != (len(indices), self.
dimension)
or not np.isfinite(vectors).all()
192 or (np.abs(vectors) > 1. + 1e-12).any()
or not np.isfinite(fitness).all()):
193 raise ValueError(
"Seeded elites must have finite in-domain genomes with the configured dimension.")
194 parents = vectors[self.
_rng.integers(len(vectors), size=size)]
195 particles = np.sqrt(alpha) * parents + np.sqrt(1. - alpha) * self.
_rng.normal(size=parents.shape)
196 return particles, vectors, fitness