|
| 1 | +"""Measure the LearnerND speedup from `simplices_containing` and `default_loss`. |
| 2 | +
|
| 3 | +This monkeypatches adaptive (>= 1.5.0, with the Rust backend active) to use |
| 4 | +the two batched APIs, the way the adaptive-side integration would: |
| 5 | +
|
| 6 | +- ``tell_pending`` distributes a pending point over the simplices containing |
| 7 | + it with one ``tri.simplices_containing(point, simplex=hint)`` call instead |
| 8 | + of a Python loop of per-neighbour ``tri.point_in_simplex`` checks. |
| 9 | +- The default loss is the Rust ``default_loss`` instead of the Python |
| 10 | + wrapper around ``simplex_volume_in_embedding``. |
| 11 | +
|
| 12 | +Requirements: |
| 13 | + pip install "adaptive[rust]>=1.5" |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import time |
| 19 | +from typing import TYPE_CHECKING |
| 20 | + |
| 21 | +import adaptive_triangulation as at |
| 22 | +import numpy as np |
| 23 | +from adaptive.learner import learnerND as lnd_mod |
| 24 | +from adaptive.learner.learnerND import LearnerND |
| 25 | + |
| 26 | +if TYPE_CHECKING: |
| 27 | + from collections.abc import Callable, Sequence |
| 28 | + |
| 29 | +reference_default_loss = lnd_mod.default_loss |
| 30 | + |
| 31 | + |
| 32 | +def ring_of_fire(xy: Sequence[float]) -> float: |
| 33 | + x, y = xy |
| 34 | + a, d = 0.2, 0.5 |
| 35 | + return x + np.exp(-((x**2 + y**2 - d**2) ** 2) / a**4) |
| 36 | + |
| 37 | + |
| 38 | +def sphere_of_fire(xyz: Sequence[float]) -> float: |
| 39 | + x, y, z = xyz |
| 40 | + a, d = 0.2, 0.5 |
| 41 | + return x + np.exp(-((x**2 + y**2 + z**2 - d**2) ** 2) / a**4) |
| 42 | + |
| 43 | + |
| 44 | +def _add_pending_to_simplex(self: LearnerND, point: tuple, simplex: tuple) -> tuple: |
| 45 | + """`_try_adding_pending_point_to_simplex` without the containment check |
| 46 | + (the caller already knows `simplex` contains `point`).""" |
| 47 | + if simplex not in self._subtriangulations: |
| 48 | + vertices = self.tri.get_vertices(simplex) |
| 49 | + self._subtriangulations[simplex] = self._triangulation_class(vertices) |
| 50 | + self._pending_to_simplex[point] = simplex |
| 51 | + return self._subtriangulations[simplex].add_point(point) |
| 52 | + |
| 53 | + |
| 54 | +def tell_pending_batched(self: LearnerND, point: tuple, *, simplex: tuple | None = None) -> None: |
| 55 | + """tell_pending with the point_in_simplex loop replaced by one |
| 56 | + `simplices_containing` call (the `simplex` argument becomes the hint).""" |
| 57 | + point = tuple(point) |
| 58 | + if not self.inside_bounds(point): |
| 59 | + return |
| 60 | + self.pending_points.add(point) |
| 61 | + if self.tri is None: |
| 62 | + return |
| 63 | + containing = self.tri.simplices_containing(point, simplex=simplex) |
| 64 | + for simpl in containing: |
| 65 | + _, to_add = _add_pending_to_simplex(self, point, simpl) |
| 66 | + if to_add is None: |
| 67 | + continue |
| 68 | + self._update_subsimplex_losses(simpl, to_add) |
| 69 | + |
| 70 | + |
| 71 | +def run( |
| 72 | + func: Callable[[Sequence[float]], float], |
| 73 | + bounds: list[tuple[float, float]], |
| 74 | + n_points: int, |
| 75 | + *, |
| 76 | + rust_loss: bool, |
| 77 | + batched_tell: bool, |
| 78 | +) -> tuple[float, LearnerND]: |
| 79 | + lnd_mod.default_loss = at.default_loss if rust_loss else reference_default_loss |
| 80 | + learner = LearnerND(func, bounds=bounds) |
| 81 | + if batched_tell: |
| 82 | + learner.tell_pending = tell_pending_batched.__get__(learner) |
| 83 | + t0 = time.perf_counter() |
| 84 | + for _ in range(n_points): |
| 85 | + points, _ = learner.ask(1) |
| 86 | + for p in points: |
| 87 | + learner.tell(p, func(p)) |
| 88 | + elapsed = time.perf_counter() - t0 |
| 89 | + return elapsed, learner |
| 90 | + |
| 91 | + |
| 92 | +CONFIGS = [ |
| 93 | + ("baseline (Rust backend, adaptive >= 1.5)", False, False), |
| 94 | + ("+ rust default_loss", True, False), |
| 95 | + ("+ simplices_containing tell_pending", False, True), |
| 96 | + ("+ both", True, True), |
| 97 | +] |
| 98 | + |
| 99 | +for name, func, bounds, n in [ |
| 100 | + ("2D ring_of_fire", ring_of_fire, [(-1, 1), (-1, 1)], 3000), |
| 101 | + ("3D sphere_of_fire", sphere_of_fire, [(-1, 1), (-1, 1), (-1, 1)], 1500), |
| 102 | +]: |
| 103 | + print(f"\n{name}, {n} points:") |
| 104 | + baseline = None |
| 105 | + base_points = None |
| 106 | + for label, rust_loss, batched_tell in CONFIGS: |
| 107 | + best = min( |
| 108 | + run(func, bounds, n, rust_loss=rust_loss, batched_tell=batched_tell)[0] |
| 109 | + for _ in range(3) |
| 110 | + ) |
| 111 | + _, learner = run(func, bounds, n, rust_loss=rust_loss, batched_tell=batched_tell) |
| 112 | + pts = sorted(learner.data) |
| 113 | + if baseline is None: |
| 114 | + baseline, base_points = best, pts |
| 115 | + same = "identical points" if pts == base_points else "DIFFERENT POINTS" |
| 116 | + print(f" {label:40s} {best:6.2f}s ({baseline / best:4.2f}x, {same})") |
0 commit comments