metawalker 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
metawalker/__init__.py ADDED
@@ -0,0 +1,19 @@
1
+ """metawalker: learned black-box optimizers for big, complex search spaces.
2
+
3
+ from metawalker import minimize, minimize_auto, recommend, certify, importance, compare, train
4
+ from metawalker.sklearn import MetaWalkerCV # needs scikit-learn
5
+ """
6
+ from .api import METHODS, Result, minimize
7
+ from .asktell import AskTell
8
+ from .certify import Certified, certify
9
+ from .compare import Comparison, compare
10
+ from .importance import Importance, importance
11
+ from .problems import FAMILIES, TestProblem, make_problem
12
+ from .recommend import Recommendation, minimize_auto, recommend
13
+ from .space import Categorical, Float, Int, Space
14
+ from .train import TrainedPolicy, train
15
+
16
+ __all__ = ['minimize', 'minimize_auto', 'recommend', 'Recommendation', 'certify', 'Certified', 'importance',
17
+ 'Importance', 'compare', 'Comparison', 'train', 'TrainedPolicy', 'AskTell', 'Result', 'METHODS',
18
+ 'make_problem', 'TestProblem', 'FAMILIES', 'Space', 'Float', 'Int', 'Categorical']
19
+ __version__ = '0.2.0'
metawalker/api.py ADDED
@@ -0,0 +1,192 @@
1
+ """minimize(): one entry point for every optimizer in the package."""
2
+ import math
3
+ from dataclasses import dataclass, field
4
+
5
+ import numpy as np
6
+
7
+ from .baselines import optuna_search, random_search
8
+ from .cma import cmaes
9
+ from .policy import POLICIES, load_policy
10
+ from .space import Space
11
+ from .walker import WALKER_RULE, navigate
12
+
13
+ METHODS = ('auto', 'rlcma', 'walker', 'walker_hp', 'walker_hp_warm', 'walker_rule', 'cmaes', 'cmaes_rule', 'gp', 'tpe',
14
+ 'random', 'panel')
15
+ WALKERS = ('walker', 'walker_hp', 'walker_rule')
16
+ CMAS = ('rlcma', 'cmaes', 'cmaes_rule')
17
+ PANEL = ('rlcma', 'walker_hp', 'cmaes')
18
+ AUTO_HIGH_DIM = 20 # benchmark: CMA-ES wins below ~20 dims, RL-controlled CMA-ES wins from ~50 up
19
+ AUTO_GP_MAX_DIM = 200 # benchmark: Optuna GP is strongest at 50-200 dims when its ~1 h/run overhead is affordable
20
+
21
+
22
+ @dataclass
23
+ class Result:
24
+ best_params: dict
25
+ best_value: float
26
+ n_evals: int
27
+ method: str
28
+ history: list = field(repr=False, default_factory=list) # [(params, value)] in evaluation order
29
+ n_failed: int = 0
30
+ recheck_values: list = field(default_factory=list) # fresh re-evaluations of best_params (noisy objectives)
31
+ maximize: bool = False # direction of the search; certify() and importance() read it
32
+
33
+
34
+ class _Objective:
35
+ """Budgeted, sign-normalized view of the user's function on the unit box."""
36
+
37
+ def __init__(self, fn, space, budget, maximize, cache=True):
38
+ self.fn, self.space, self.budget, self.sign = fn, space, budget, (-1.0 if maximize else 1.0)
39
+ self.n, self.evals = space.n, 0
40
+ self.history, self.n_failed = [], 0
41
+ self.best_y, self.best_params = math.inf, None
42
+ self.cache, self.n_cached = ({} if cache else None), 0
43
+
44
+ def __call__(self, x):
45
+ if self.evals >= self.budget:
46
+ return math.inf # over budget: ignored (lets CMA-ES finish a generation)
47
+ params = self.space.decode(x)
48
+ key = repr(sorted(params.items())) if self.cache is not None else None
49
+ if key is not None and key in self.cache and self.n_cached < 50 * self.budget: # cap: no endless loop
50
+ self.n_cached += 1
51
+ return self.cache[key]
52
+ self.evals += 1
53
+ try:
54
+ v = float(self.fn(params))
55
+ except Exception: # an invalid configuration is a failed trial, not a crashed search
56
+ v = math.nan
57
+ if not math.isfinite(v):
58
+ self.n_failed += 1
59
+ self.history.append((params, v))
60
+ if key:
61
+ self.cache[key] = math.inf
62
+ return math.inf
63
+ self.history.append((params, v))
64
+ y = self.sign * v
65
+ if y < self.best_y:
66
+ self.best_y, self.best_params = y, params
67
+ if key:
68
+ self.cache[key] = y
69
+ return y
70
+
71
+
72
+ def search_rng(seed):
73
+ """The optimizer's random stream. Salted so that seed=k never replays default_rng(k), which a test problem
74
+ built with the same k uses to place its optimum (unsalted, the first random point lands near it)."""
75
+ return np.random.default_rng(None if seed is None else [int(seed), 0x6E6176])
76
+
77
+
78
+ def _has_optuna():
79
+ try:
80
+ import optuna # noqa: F401
81
+ return True
82
+ except ImportError:
83
+ return False
84
+
85
+
86
+ class _PadTo2:
87
+ """pycma needs >= 2 dimensions: add a dummy coordinate the objective ignores."""
88
+
89
+ def __init__(self, obj):
90
+ self.obj, self.n = obj, 2
91
+
92
+ budget = property(lambda self: self.obj.budget)
93
+ evals = property(lambda self: self.obj.evals)
94
+
95
+ def __call__(self, x):
96
+ return self.obj(x[:1])
97
+
98
+
99
+ def _warmup(obj, rng, k):
100
+ """Evaluate k Latin-hypercube points; return them sorted best first, with their values."""
101
+ X = (rng.permuted(np.tile(np.arange(k), (obj.n, 1)), axis=1).T + rng.uniform(size=(k, obj.n))) / k
102
+ F = np.array([obj(x) for x in X])
103
+ order = np.argsort(F, kind='stable')
104
+ return X[order], F[order]
105
+
106
+
107
+ def _run(method, obj, rng, warmup=0.0):
108
+ if method == 'walker_hp_warm':
109
+ method, warmup = 'walker_hp', warmup or 0.2
110
+ if method not in METHODS[1:-1]:
111
+ raise ValueError(f'unknown method {method!r}; choose from {METHODS}')
112
+ if not 0 <= warmup < 1:
113
+ raise ValueError('warmup must be in [0, 1)')
114
+ if obj.n == 1 and method in CMAS:
115
+ obj = _PadTo2(obj)
116
+ kw = {}
117
+ if warmup > 0:
118
+ k = max(1, round(warmup * obj.budget))
119
+ if method in WALKERS:
120
+ k = max(k, 4) # every walker needs a start point; M = 4 for all shipped walkers
121
+ X, F = _warmup(obj, rng, min(k, obj.budget))
122
+ if method in WALKERS and len(X) >= 4:
123
+ kw = dict(x0=X[:4], f0=F[:4])
124
+ elif method in CMAS:
125
+ kw = dict(x0=X[0])
126
+ if method in POLICIES:
127
+ theta, cfg = load_policy(method)
128
+ (navigate if cfg['kind'] == 'walker' else cmaes)(obj, rng, theta, cfg, **kw)
129
+ elif method == 'walker_rule':
130
+ navigate(obj, rng, rule=WALKER_RULE, **kw)
131
+ elif method in ('cmaes', 'cmaes_rule'):
132
+ cmaes(obj, rng, rule=method == 'cmaes_rule', **kw)
133
+ elif method in ('tpe', 'gp'):
134
+ optuna_search(obj, rng, method)
135
+ else:
136
+ random_search(obj, rng)
137
+
138
+
139
+ def minimize(fn, space, budget=200, method='auto', maximize=False, seed=None, recheck=0, expensive=False,
140
+ warmup=0.0, cache=True):
141
+ """Optimize fn(params: dict) -> float over `space` with `budget` evaluations.
142
+
143
+ method: 'rlcma', 'walker', 'walker_hp' (walker trained with plateau-shaped problems too), 'cmaes',
144
+ 'walker_rule' / 'cmaes_rule' (hand rules distilled from the learned walker / rlcma),
145
+ 'walker_hp_warm' (walker_hp with warmup=0.2: for rare-signal objectives),
146
+ 'gp' / 'tpe' (Optuna; needs metawalker[optuna]), 'random';
147
+ 'auto': CMA-ES below 20 parameters, RL-controlled CMA-ES from 20 up, or Optuna GP up to 200
148
+ parameters when expensive=True (each evaluation costs far more than GP's own overhead);
149
+ a list of methods, e.g. ['rlcma', 'gp', 'cmaes']: your own panel; the budget is split across them
150
+ and the best result is kept; 'panel' = ['rlcma', 'walker_hp', 'cmaes'].
151
+ fn may raise or return nan/inf for invalid configurations; those count as failed trials.
152
+ warmup: fraction of the budget spent first on Latin-hypercube points (counted in the budget and history);
153
+ walkers then start at the best 4 of them, CMA-based methods at the best one, other methods just use
154
+ the rest of the budget. Helps when the signal is rare (a small live region on a flat objective).
155
+ recheck: re-evaluate the best configuration this many extra times (outside the budget) and report the
156
+ values, because on noisy objectives the best observed value is biased upward (winner's curse).
157
+ cache: a configuration that decodes to one already tried (common with Int / Categorical parameters) reuses its
158
+ value instead of calling fn again, and costs no budget. Pass cache=False to re-sample a noisy fn.
159
+ """
160
+ if budget < 1:
161
+ raise ValueError('budget must be >= 1')
162
+ sp = space if isinstance(space, Space) else Space(space)
163
+ rng = search_rng(seed)
164
+ if method == 'auto':
165
+ if expensive and sp.n <= AUTO_GP_MAX_DIM and _has_optuna():
166
+ method = 'gp'
167
+ else:
168
+ method = 'rlcma' if sp.n >= AUTO_HIGH_DIM else 'cmaes'
169
+ panel = PANEL if method == 'panel' else method if isinstance(method, (list, tuple)) else None
170
+ if panel is not None:
171
+ bad = [m for m in panel if m not in METHODS or m in ('auto', 'panel')]
172
+ if not panel or bad:
173
+ raise ValueError(f'panel methods must be from {METHODS[1:-1]}; got {list(panel)}')
174
+ # shortcut: equal split, no racing; upgrade to successive halving across methods if budgets get large
175
+ results = []
176
+ for i, m in enumerate(panel):
177
+ share = budget // len(panel) + (1 if i < budget % len(panel) else 0)
178
+ results.append(minimize(fn, sp, max(share, 1), m, maximize, int(rng.integers(2 ** 31)), warmup=warmup,
179
+ cache=cache))
180
+ best = min(results, key=lambda r: (-r.best_value if maximize else r.best_value)
181
+ if r.best_params is not None else math.inf)
182
+ out = Result(best.best_params, best.best_value, sum(r.n_evals for r in results), f'panel:{best.method}',
183
+ [h for r in results for h in r.history], sum(r.n_failed for r in results))
184
+ else:
185
+ obj = _Objective(fn, sp, budget, maximize, cache)
186
+ _run(method, obj, rng, warmup)
187
+ out = Result(obj.best_params, obj.sign * obj.best_y if obj.best_params is not None else math.nan,
188
+ obj.evals, method, obj.history, obj.n_failed)
189
+ out.maximize = maximize
190
+ if recheck and out.best_params is not None:
191
+ out.recheck_values = [float(fn(out.best_params)) for _ in range(recheck)]
192
+ return out
metawalker/asktell.py ADDED
@@ -0,0 +1,189 @@
1
+ """Ask/tell interface: you run the evaluations (your own workers, a cluster queue), metawalker proposes the points.
2
+
3
+ from metawalker.asktell import AskTell
4
+ with AskTell(space, budget=200, method='rlcma', seed=0) as opt:
5
+ while not opt.done:
6
+ tid, params = opt.ask()
7
+ opt.tell(tid, run_somewhere(params)) # None or nan = failed trial
8
+ opt.result()
9
+
10
+ The optimizer is the same code minimize() runs, driven from a background thread, so for the same seed and
11
+ the same values it asks exactly the points minimize() would evaluate.
12
+
13
+ shortcut: one trial in flight at a time (the shipped optimizers are sequential, and CMA-ES generations are
14
+ fed one point at a time); upgrade to batch asks only if an optimizer gets a true batch mode. For parallel
15
+ evaluation, run several AskTell / minimize instances (different seeds or methods) side by side, e.g. with
16
+ compare(..., n_jobs=k).
17
+ """
18
+ import math
19
+ import queue
20
+ import threading
21
+
22
+ from .api import minimize
23
+
24
+
25
+ class _Closed(BaseException):
26
+ """Unwinds the optimizer thread on close(); a BaseException so _Objective doesn't count it as a failed trial."""
27
+
28
+
29
+ _FINISHED = object()
30
+
31
+
32
+ class AskTell:
33
+ def __init__(self, space, budget, method='auto', maximize=False, seed=None):
34
+ self.budget = budget
35
+ self._asks, self._tells = queue.Queue(), queue.Queue()
36
+ self._closed = False
37
+ self._pending = None # trial id handed out by ask() and not yet told
38
+ self._next = None # (trial_id, params) waiting for ask(), or _FINISHED
39
+ self._result = self._error = None
40
+ self._n_asked = 0
41
+ self._thread = threading.Thread(target=self._run, args=(space, budget, method, maximize, seed),
42
+ daemon=True)
43
+ self._thread.start()
44
+ self._advance()
45
+
46
+ def _fn(self, params):
47
+ if self._closed:
48
+ raise _Closed
49
+ self._asks.put(params)
50
+ v = self._tells.get()
51
+ if v is _Closed:
52
+ raise _Closed
53
+ return math.nan if v is None else v
54
+
55
+ def _run(self, *args):
56
+ try:
57
+ self._result = minimize(self._fn, *args)
58
+ except _Closed:
59
+ pass
60
+ except BaseException as e: # surfaced in the caller's thread by _advance()
61
+ self._error = e
62
+ finally:
63
+ self._asks.put(_FINISHED)
64
+
65
+ def _advance(self):
66
+ """Wait until the optimizer proposes its next point or finishes, so `done` is exact."""
67
+ item = self._asks.get()
68
+ if item is _FINISHED:
69
+ self._thread.join()
70
+ self._next = _FINISHED
71
+ if self._error is not None:
72
+ raise self._error
73
+ else:
74
+ self._next = (self._n_asked, item)
75
+
76
+ @property
77
+ def done(self):
78
+ return self._next is _FINISHED or self._closed
79
+
80
+ def ask(self):
81
+ """-> (trial_id, params). Call tell() for it before the next ask()."""
82
+ if self._closed:
83
+ raise RuntimeError('AskTell is closed')
84
+ if self._next is _FINISHED:
85
+ raise RuntimeError(f'budget of {self.budget} evaluations is used up; call result()')
86
+ if self._pending is not None:
87
+ raise RuntimeError(f'trial {self._pending} is still in flight; tell() its value before asking again '
88
+ '(one trial at a time; run several AskTell instances for parallel evaluation)')
89
+ tid, params = self._next
90
+ self._pending, self._n_asked = tid, self._n_asked + 1
91
+ return tid, dict(params)
92
+
93
+ def tell(self, trial_id, value):
94
+ """value: the objective value; None or nan (or inf) = failed trial."""
95
+ if self._closed:
96
+ raise RuntimeError('AskTell is closed')
97
+ if self._pending is None or trial_id != self._pending:
98
+ raise ValueError(f'unknown trial id {trial_id!r}: the only trial in flight is {self._pending!r}')
99
+ self._pending = None
100
+ self._tells.put(None if value is None else float(value))
101
+ self._advance()
102
+
103
+ def result(self):
104
+ """The metawalker Result, once the budget is used up."""
105
+ if self._next is not _FINISHED:
106
+ raise RuntimeError('result() is available once done is True (the whole budget has been told)')
107
+ return self._result
108
+
109
+ def close(self):
110
+ """Stop the optimizer thread. Safe to call twice; the instance can't be used afterwards."""
111
+ if self._closed:
112
+ return
113
+ self._closed = True
114
+ if self._thread.is_alive():
115
+ self._tells.put(_Closed) # unblocks the optimizer if it is waiting for a value
116
+ self._thread.join(timeout=10)
117
+
118
+ def __enter__(self):
119
+ return self
120
+
121
+ def __exit__(self, *exc):
122
+ self.close()
123
+
124
+ def __del__(self):
125
+ try:
126
+ self.close()
127
+ except Exception:
128
+ pass
129
+
130
+
131
+ try:
132
+ import optuna
133
+ from optuna.distributions import CategoricalDistribution, FloatDistribution, IntDistribution
134
+ from optuna.trial import TrialState
135
+ except ImportError: # MetaWalkerSampler needs optuna; the rest of this module doesn't
136
+ optuna = None
137
+
138
+
139
+ if optuna is not None:
140
+ from .space import Categorical, Float, Int
141
+
142
+ class MetaWalkerSampler(optuna.samplers.BaseSampler):
143
+ """Optuna sampler that proposes trials with a metawalker method (via AskTell).
144
+
145
+ sampler = MetaWalkerSampler({'lr': FloatDistribution(1e-5, 1e-1, log=True), 'act': CategoricalDistribution(['a', 'b'])},
146
+ budget=100, method='cmaes', seed=0)
147
+ optuna.create_study(sampler=sampler).optimize(objective, n_trials=100)
148
+
149
+ Limits: a fixed search space given up front (float without step, int with step 1, categorical);
150
+ single objective; study.optimize(n_jobs=1); at most `budget` trials. Parameters outside the space
151
+ are sampled at random. Failed or pruned trials count as failed metawalker trials.
152
+ """
153
+
154
+ def __init__(self, search_space, budget, method='auto', seed=None):
155
+ spec = {}
156
+ for k, d in search_space.items():
157
+ if isinstance(d, FloatDistribution) and d.step is None:
158
+ spec[k] = Float(d.low, d.high, log=d.log)
159
+ elif isinstance(d, IntDistribution) and d.step == 1:
160
+ spec[k] = Int(d.low, d.high, log=d.log)
161
+ elif isinstance(d, CategoricalDistribution):
162
+ spec[k] = Categorical(d.choices)
163
+ else:
164
+ raise ValueError(f'parameter {k!r}: unsupported distribution {d!r}')
165
+ self._space, self._spec, self._budget, self._method, self._seed = search_space, spec, budget, method, seed
166
+ self._at, self._trials = None, {} # optuna trial number -> AskTell trial id
167
+ self._fallback = optuna.samplers.RandomSampler(seed=seed)
168
+
169
+ def infer_relative_search_space(self, study, trial):
170
+ return dict(self._space)
171
+
172
+ def sample_relative(self, study, trial, search_space):
173
+ if self._at is None:
174
+ self._at = AskTell(self._spec, self._budget, self._method,
175
+ maximize=study.direction == optuna.study.StudyDirection.MAXIMIZE, seed=self._seed)
176
+ tid, params = self._at.ask()
177
+ self._trials[trial.number] = tid
178
+ for k, d in self._space.items(): # guard float round-off at the bounds
179
+ if isinstance(d, FloatDistribution):
180
+ params[k] = min(max(params[k], d.low), d.high)
181
+ return params
182
+
183
+ def sample_independent(self, study, trial, param_name, param_distribution):
184
+ return self._fallback.sample_independent(study, trial, param_name, param_distribution)
185
+
186
+ def after_trial(self, study, trial, state, values):
187
+ tid = self._trials.pop(trial.number, None)
188
+ if tid is not None:
189
+ self._at.tell(tid, values[0] if state == TrialState.COMPLETE and values else None)
@@ -0,0 +1,67 @@
1
+ """Standard optimizers behind the same interface: random search, Optuna (TPE / GP), SMAC3, Nevergrad NGOpt.
2
+ Each takes prob (n, budget, evals, __call__) and rng, and stops when prob.evals reaches prob.budget."""
3
+ from pathlib import Path
4
+
5
+ import numpy as np
6
+
7
+
8
+ def random_search(prob, rng):
9
+ while prob.evals < prob.budget:
10
+ prob(rng.uniform(0, 1, prob.n))
11
+
12
+
13
+ def until_budget(prob):
14
+ """Optuna callback: stop once prob's budget is spent (a problem may not count some evaluations)."""
15
+ return lambda study, trial: study.stop() if prob.evals >= prob.budget else None
16
+
17
+
18
+ def optuna_search(prob, rng, sampler='tpe', **kw):
19
+ """sampler: 'tpe' or 'gp' (Optuna's Gaussian-process BO); kw go to the sampler (e.g. multivariate=True)."""
20
+ try:
21
+ import optuna
22
+ except ImportError as e:
23
+ raise ImportError(f"method={sampler!r} needs optuna: pip install metawalker[optuna]") from e
24
+ optuna.logging.set_verbosity(optuna.logging.WARNING)
25
+ cls = {'tpe': optuna.samplers.TPESampler, 'gp': optuna.samplers.GPSampler}[sampler]
26
+ seen = []
27
+
28
+ def objective(tr):
29
+ y = prob(np.array([tr.suggest_float(f'x{i}', 0, 1) for i in range(prob.n)]))
30
+ if np.isfinite(y):
31
+ seen.append(y)
32
+ return y
33
+ # failed trial: report it as worse than anything seen, since an infinite value breaks the GP model
34
+ return max(seen) + (max(seen) - min(seen)) + 1.0 if seen else 1e6
35
+
36
+ study = optuna.create_study(sampler=cls(seed=int(rng.integers(2 ** 31)), **kw))
37
+ if prob.evals < prob.budget: # after a warm-up, only what is left
38
+ study.optimize(objective, callbacks=[until_budget(prob)]) # an exception (e.g. a time limit) ends the study
39
+
40
+
41
+ def smac_hpo(prob, rng):
42
+ import shutil, tempfile
43
+ from ConfigSpace import ConfigurationSpace, Float
44
+ from smac import HyperparameterOptimizationFacade, Scenario
45
+ from smac.runhistory.dataclasses import TrialValue
46
+ seed = int(rng.integers(2 ** 31))
47
+ cs = ConfigurationSpace(seed=seed)
48
+ cs.add([Float(f'x{i}', (0.0, 1.0)) for i in range(prob.n)])
49
+ out = tempfile.mkdtemp(prefix='smac_')
50
+ try:
51
+ sc = Scenario(cs, deterministic=True, n_trials=prob.budget, seed=seed, output_directory=Path(out))
52
+ smac = HyperparameterOptimizationFacade(sc, lambda config, seed=0: 0.0, overwrite=True, logging_level=40)
53
+ while prob.evals < prob.budget: # ask/tell, so TimeLimit propagates instead of being logged as a crash
54
+ info = smac.ask()
55
+ smac.tell(info, TrialValue(cost=float(prob(np.array([info.config[f'x{i}'] for i in range(prob.n)])))))
56
+ finally:
57
+ shutil.rmtree(out, ignore_errors=True)
58
+
59
+
60
+ def ngopt(prob, rng):
61
+ import nevergrad as ng
62
+ param = ng.p.Array(shape=(prob.n,)).set_bounds(0.0, 1.0)
63
+ param.random_state = np.random.RandomState(int(rng.integers(2 ** 31)))
64
+ opt = ng.optimizers.NGOpt(parametrization=param, budget=prob.budget, num_workers=1)
65
+ while prob.evals < prob.budget:
66
+ c = opt.ask()
67
+ opt.tell(c, float(prob(c.value)))
metawalker/certify.py ADDED
@@ -0,0 +1,116 @@
1
+ """certify(): re-evaluate the top candidates of a search and pick by mean, with a bootstrap CI.
2
+
3
+ On noisy objectives the single best observed value is biased (the winner's curse): the search keeps whichever
4
+ reading was luckiest. Re-measuring the top few candidates and choosing by mean undoes most of that bias.
5
+ """
6
+ import math
7
+ from dataclasses import dataclass, field
8
+
9
+ import numpy as np
10
+
11
+ N_BOOT = 2000
12
+
13
+
14
+ @dataclass
15
+ class Certified:
16
+ best_params: dict
17
+ mean: float
18
+ ci_low: float
19
+ ci_high: float
20
+ n_extra_evals: int
21
+ shrinkage: float # original best observed value - certified mean (> 0 when maximizing means it was inflated)
22
+ maximize: bool
23
+ confidence: float
24
+ n_failed: int = 0
25
+ table: list = field(repr=False, default_factory=list) # one dict per candidate, best observed first
26
+
27
+ def summary(self):
28
+ lines = [f'certified best ({"max" if self.maximize else "min"}imize): mean {self.mean:.4g}, '
29
+ f'{self.confidence:.0%} CI [{self.ci_low:.4g}, {self.ci_high:.4g}], '
30
+ f'shrinkage {self.shrinkage:+.4g}, {self.n_extra_evals} extra evals ({self.n_failed} failed)',
31
+ f' {"observed":>10} {"mean":>10} {"std":>10} {"ci":>23} {"n":>4} {"fail":>4} params']
32
+ for row in self.table:
33
+ mark = '*' if row['params'] == self.best_params else ' '
34
+ ci = f'[{row["ci"][0]:.4g}, {row["ci"][1]:.4g}]'
35
+ lines.append(f'{mark} {row["observed"]:>10.4g} {row["mean"]:>10.4g} {row["std"]:>10.4g} {ci:>23} '
36
+ f'{row["n"]:>4} {row["n_failed"]:>4} {row["params"]}')
37
+ return '\n'.join(lines)
38
+
39
+
40
+ def _eval(fn, params):
41
+ try:
42
+ v = float(fn(dict(params)))
43
+ except Exception: # a crash on re-evaluation is a failed repeat, as in minimize()
44
+ return math.nan
45
+ return v if math.isfinite(v) else math.nan
46
+
47
+
48
+ def _stats(vals, rng, confidence):
49
+ v = np.array([x for x in vals if not math.isnan(x)])
50
+ if v.size == 0:
51
+ return 0, math.nan, math.nan, (math.nan, math.nan)
52
+ std = float(v.std(ddof=1)) if v.size > 1 else 0.0
53
+ boot = rng.choice(v, (N_BOOT, v.size)).mean(axis=1)
54
+ lo, hi = np.percentile(boot, [50 * (1 - confidence), 50 * (1 + confidence)])
55
+ return int(v.size), float(v.mean()), std, (float(lo), float(hi))
56
+
57
+
58
+ def certify(fn, result, top_k=5, repeats=5, maximize=None, seed=None, confidence=0.95, adaptive=False):
59
+ """Re-evaluate the top_k distinct params of result.history `repeats` times each; pick the best mean.
60
+
61
+ maximize: None reads result.maximize if the result has that attribute, otherwise assumes minimization,
62
+ so pass maximize=True for a search run with minimize(..., maximize=True).
63
+ adaptive: successive halving with the same budget of top_k * repeats extra evaluations: every candidate
64
+ starts at 2 repeats, the worse half is dropped, and survivors are topped up to double the
65
+ repeats each round until the budget is spent. The winner is picked among the final survivors.
66
+ Failed repeats (an exception or nan/inf) are excluded from the mean and counted in n_failed.
67
+ seed seeds only the bootstrap; fn's own noise is up to fn.
68
+ """
69
+ if top_k < 1 or repeats < 1:
70
+ raise ValueError('top_k and repeats must be >= 1')
71
+ if not 0 < confidence < 1:
72
+ raise ValueError('confidence must be in (0, 1)')
73
+ if maximize is None:
74
+ maximize = bool(getattr(result, 'maximize', False))
75
+ sign = -1.0 if maximize else 1.0
76
+ seen, cands = set(), []
77
+ for params, v in sorted((h for h in result.history if math.isfinite(h[1])), key=lambda h: sign * h[1]):
78
+ key = repr(sorted(params.items())) # repr: categorical values need not be hashable
79
+ if key not in seen:
80
+ seen.add(key)
81
+ cands.append((params, v))
82
+ if len(cands) == top_k:
83
+ break
84
+ if not cands:
85
+ raise ValueError('result.history has no finite values to certify')
86
+ rng = np.random.default_rng(seed)
87
+ vals = [[] for _ in cands]
88
+ budget = top_k * repeats
89
+ if not adaptive:
90
+ for i, (p, _) in enumerate(cands):
91
+ vals[i] = [_eval(fn, p) for _ in range(repeats)]
92
+ alive = list(range(len(cands)))
93
+ else:
94
+ alive, target, spent = list(range(len(cands))), 2, 0
95
+ while spent < budget:
96
+ for i in alive:
97
+ while len(vals[i]) < target and spent < budget:
98
+ vals[i].append(_eval(fn, cands[i][0]))
99
+ spent += 1
100
+ if len(alive) > 1:
101
+ alive = sorted(alive, key=lambda i: _rank(vals[i], sign))[:(len(alive) + 1) // 2]
102
+ target *= 2
103
+ table = []
104
+ for (p, obs), vs in zip(cands, vals):
105
+ n, mean, std, ci = _stats(vs, rng, confidence)
106
+ table.append({'params': p, 'observed': obs, 'n': n, 'n_failed': len(vs) - n, 'mean': mean, 'std': std,
107
+ 'ci': ci, 'values': vs})
108
+ best = table[min(alive, key=lambda i: _rank(vals[i], sign))]
109
+ return Certified(best['params'], best['mean'], best['ci'][0], best['ci'][1], sum(len(v) for v in vals),
110
+ cands[0][1] - best['mean'], maximize, confidence, sum(r['n_failed'] for r in table), table)
111
+
112
+
113
+ def _rank(vs, sign):
114
+ # best mean first, ties broken by lower std; candidates with no valid repeat go last
115
+ v = [x for x in vs if not math.isnan(x)]
116
+ return (sign * float(np.mean(v)), float(np.std(v, ddof=1)) if len(v) > 1 else 0.0) if v else (math.inf, math.inf)
metawalker/cma.py ADDED
@@ -0,0 +1,67 @@
1
+ """CMA-ES (IPOP restarts, pycma), optionally steered by a learned policy or the distilled rule, and the two
2
+ walker / CMA-ES combinations (handoff: walker first; cma_then_walk: CMA-ES first)."""
3
+ import numpy as np
4
+
5
+ from .policy import STEP_MULT, pick, unpack
6
+ from .walker import navigate
7
+
8
+
9
+ def cmaes(prob, rng, theta=None, cfg=None, rule=False, x0=None, sigma0=0.25, inject=None):
10
+ """CMA-ES with IPOP restarts; with theta, a learned policy also sets the step size and restarts.
11
+ rule=True: the distilled rlcma rule, sigma *= 0.8 after a generation that did not improve on this restart's
12
+ best, unless stalled >= 5 generations or the population is flat; never grows, never restarts early.
13
+ x0 / sigma0 / inject (points tried in the first generations) set the first restart only."""
14
+ import cma
15
+ P = unpack(theta, cfg) if theta is not None else None
16
+ popsize, default_pop = None, 4 + int(3 * np.log(prob.n))
17
+ while prob.evals < prob.budget: # IPOP restarts
18
+ opts = {'bounds': [0, 1], 'seed': int(rng.integers(1, 2 ** 31)), 'verbose': -9,
19
+ 'maxfevals': prob.budget - prob.evals}
20
+ if popsize: opts['popsize'] = popsize
21
+ es = cma.CMAEvolutionStrategy(rng.uniform(0, 1, prob.n) if x0 is None else x0, sigma0, opts)
22
+ if inject is not None:
23
+ es.inject(list(inject), force=True)
24
+ x0, inject, sigma0 = None, None, 0.25 # later IPOP restarts start fresh
25
+ best, stall = np.inf, 0
26
+ while not es.stop() and prob.evals < prob.budget:
27
+ X = es.ask()
28
+ F = [prob(xi) for xi in X]
29
+ es.tell(X, F)
30
+ if P is None and not rule:
31
+ continue
32
+ Ff = np.array([f for f in F if np.isfinite(f)]) if np.isfinite(F).any() else np.array([best])
33
+ improved = Ff.min() < best
34
+ best = min(best, Ff.min()); stall = 0 if improved else stall + 1
35
+ spread = np.log10((np.median(Ff) - Ff.min()) / (abs(Ff.min()) + 1e-12) + 1e-12)
36
+ g = np.array([1.0, np.log(es.sigma / es.sigma0) / np.log(1e-6), prob.evals / prob.budget,
37
+ float(improved), min(stall / 10, 3), np.clip(spread / 6, -2, 2),
38
+ np.log(es.popsize / default_pop)])
39
+ if rule:
40
+ es.sigma = float(np.clip(es.sigma * (0.8 if not improved and stall < 5 and g[5] > -1.2 else 1.0),
41
+ 1e-12, 1.0))
42
+ continue
43
+ h = np.tanh(P['W1'] @ g + P['b1']) if 'W1' in P else g
44
+ es.sigma = float(np.clip(es.sigma * STEP_MULT[pick(rng, P['Vs'] @ h + P['cs'])], 1e-12, 1.0))
45
+ if pick(rng, P['Vr'] @ h + P['cr']) == 1:
46
+ break # learned restart
47
+ popsize = 2 * es.popsize
48
+
49
+
50
+ def handoff(prob, rng, theta, cfg, frac=0.3, k=10):
51
+ """The walker explores the first `frac` of the budget, then CMA-ES starts from its experience: mean at the
52
+ best point (mean over repeats), step size from the spread of the k best points, and those points injected."""
53
+ full, prob.budget = prob.budget, round(frac * prob.budget)
54
+ navigate(prob, rng, theta, cfg)
55
+ prob.budget = full
56
+ top = sorted(prob.obs.items(), key=lambda kv: kv[1][0] / kv[1][1])[:k]
57
+ X = np.array([prob.reps[q] for q, _ in top])
58
+ cmaes(prob, rng, x0=prob.best_x, sigma0=float(np.clip(X.std(0).mean(), 0.02, 0.3)), inject=X)
59
+
60
+
61
+ def cma_then_walk(prob, rng, theta, cfg, frac=0.5):
62
+ """CMA-ES explores the first `frac` of the budget, then the walkers start at its M best points (mean over repeats)."""
63
+ full, prob.budget = prob.budget, round(frac * prob.budget)
64
+ cmaes(prob, rng)
65
+ prob.budget = full
66
+ top = sorted(prob.obs.items(), key=lambda kv: kv[1][0] / kv[1][1])[:cfg.get('M', 4)]
67
+ navigate(prob, rng, theta, cfg, x0=[prob.reps[q] for q, _ in top], f0=[t / c for _, (t, c) in top])