QDpy
Loading...
Searching...
No Matches
trace_elites.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2
5
6"""MAP-Elites variation with multi-cell admission."""
7
8from dataclasses import dataclass
9
10import numpy as np
11
12from qdpy.containers import TraceArchive
13from qdpy.tools import iso_line_dd
14
15from .modern import _positive_int, _variation_settings
16
17__all__ = ["TraceElites", "TraceElitesStats"]
18
19
20
21@dataclass(frozen=True)
23 """Count each evaluation once even if it wins many cells."""
24
25 evaluations: int
26 failed_evaluations: int
27 cell_updates: int
28
29
30
32 """Evaluate once per proposal, then offer its measured descriptor trace.
33
34 By default sample from occupied cells. A parent that wins more cells
35 is drawn more often. Set parent_selection to "records" for uniform
36 choice from the retained measurements.
37
38 :param archive: TraceArchive to update, optionally with compatible elites.
39 :param dimension: Genome length.
40 :param ind_domain: Shared inclusive genome bounds.
41 :param iso_sigma: Independent mutation scale for Iso+LineDD.
42 :param line_sigma: Shared direction scale for Iso+LineDD.
43 :param initial_samples: Number of initial uniform samples on an empty archive.
44 :param parent_selection: Sample from "cells" or "records".
45 :param seed: Private seed for proposals and parent selection.
46 """
47
48 def __init__(self, archive, dimension, *, ind_domain=(0., 1.), iso_sigma=.05, line_sigma=.2,
49 initial_samples=16, parent_selection="cells", seed=0):
50 if not isinstance(archive, TraceArchive):
51 raise TypeError("Supply a TraceArchive.")
52 self.dimension = _positive_int(dimension, "dimension")
53 if parent_selection not in ("cells", "records"):
54 raise ValueError("Choose cells or records for parent_selection.")
55 self._by_cell = parent_selection == "cells"
56 initial_samples = _positive_int(initial_samples, "initial_samples")
57 self._bounds = _variation_settings(ind_domain, iso_sigma, line_sigma)
58 if not np.isfinite(self._bounds[1] - self._bounds[0]):
59 raise ValueError("Use a finite genome domain width.")
60 for elite in archive:
61 if (len(elite.genome) != self.dimension
62 or any(not self._bounds[0] <= gene <= self._bounds[1] for gene in elite.genome)):
63 raise ValueError("Existing trace elites must match the dimension and bounds.")
64 self.archive = archive
65 self._warmup = 0 if len(archive) else initial_samples
66 self._iso_sigma, self._line_sigma = iso_sigma, line_sigma
67 self._rng = np.random.default_rng(seed)
68 self._calls = 0
69
70
71 def run(self, evaluate, budget):
72 """Evaluate tuples of reals and expect (scalar fitness, points) or None."""
73 budget = _positive_int(budget, "budget")
74 if not callable(evaluate):
75 raise TypeError("evaluate must be callable.")
76 failed = updates = 0
77 for _ in range(budget):
78 if self._calls < self._warmup or not len(self.archive):
79 point = self._rng.uniform(*self._bounds, size=self.dimension)
80 else:
81 first = self.archive.sample(self._rng, by_cell=self._by_cell)
82 second = self.archive.sample(self._rng, by_cell=self._by_cell)
83 point = iso_line_dd(first.genome, second.genome, bounds=self._bounds,
84 iso_sigma=self._iso_sigma, line_sigma=self._line_sigma, rng=self._rng)
85 self._calls += 1
86 rows = evaluate(tuple(map(float, point)))
87 if rows is None:
88 failed += 1
89 else:
90 fitness, trace = rows
91 updates += len(self.archive.add(point, fitness, trace))
92 return TraceElitesStats(budget, failed, updates)
Attempted calls, failures and cell changes over one Trace-Elites run.
Select trace parents and produce bounded numeric offspring.
run(self, evaluate, budget)
Spend an additional objective budget.