Restart from a surrogate¶
This page continues the tutorial's example:
eval_f_g and domain below are the ones built there.
The pieces¶
The package already ships the minimizer this page builds: descent_minimize is
BLRGlobalModel's default minimize, one restart-free n-path run over the
acquisition. Leave minimize out and you get it. Write your own only when you
need an inner knob the default does not expose — e.g. a time_limit instead of
an iteration cap, a different local solver, budget_check, restarts inside — and
the rest of this page is the recipe for doing that: the same function the
default is, with inner_iters opened back up into its parts.
The job is one function: it takes the acquisition BLRGlobalModel builds — its
score for how promising a point looks, here -log EI — and returns the points to
restart from: the local minima of that score. The obvious way to descend it is to
hand the engine back to itself: one restart-free run (that is,
restart_stalled=False) whose width is the number of minimizers you want, read
off flock_x.
BLRGlobalModel fits its posterior and builds its own -log EI acquisition,
but it never descends that acquisition itself: how to descend it is the engine's
business, so the model receives it as an injected minimize — the parameter's
name, typed MinimizeFn, with signature
(acquisition, domain, paths, key) -> {path: Point or None} and defaulting to
descent_minimize. The answer is
keyed by the paths the model was asked about, so an inner run pairs its
minimizers with the paths they belong to and says None for a path it could not
place — that path's restart is simply declined, not filled in with a coordinate.
The domain is the one the outer run's propose call carried, not a box
re-derived from it, so the scale and samplers you declared reach the inner
run's own draws. The acquisition is
(M, d) -> ((M,), (M, d)) (value and gradient, no feasibility column) and key
is a fresh subkey of the model's own key, split for this call alone.
The outer engine is the model's only caller: it hands it every batch it serves
(observe) and asks it for points when a path restarts (propose), or for
iteration 0's flock when no x0 was handed in, and the model only ever answers.
What it answers with, and when, is the rest of this page.
The projection is the other knob — it supplies the prior the model fits.
SphereProjection keeps the posterior variance bounded far from data;
IdentityProjection, also in soar_lite, passes the raw coordinates through,
so its variance grows with distance toward the box corners. Default to the first
unless you have a reason not to. projection_factory takes the class, not an
instance: the model calls it with the domain, once per propose.
import jax
import jax.numpy as jnp
from soar_lite import (
BLRGlobalModel,
Domain,
Point,
SphereProjection,
n_path_schedule,
pointwise,
)
# the tutorial's objective, built here so the block stands alone
def squares(x):
return jnp.sum((x - 3.0) ** 2)
eval_f_g, _ = pointwise(squares)
domain = Domain([(-5.0, 5.0), (-5.0, 5.0)])
INNER_ITERS = 40 # long enough to settle; shorter than the outer's horizon
def custom_minimize(acquisition, domain, paths, key):
"""Descend an acquisition with one restart-free n-path run."""
# the acquisition is two slots; the seam wants three, the last one None
def fused_seam(x_batch):
f, g = acquisition(x_batch)
return f, g, None # finite everywhere: no feasibility to mask
result = n_path_schedule(
fused_seam,
domain=domain,
n_paths=len(paths), # one path per minimizer: one batch, not n runs
max_iters=INNER_ITERS,
restart_stalled=False, # a pure descent, no restarting inside it
key=key,
)
# one answer per named path, and every answer has to be finite: the inner
# run was handed this same `domain`, so the box needs no check of its own
# here, and a path it never placed is declined rather than filled in with a
# coordinate the acquisition was never asked about
placed = result.flock_x
usable = jnp.all(jnp.isfinite(placed), axis=1)
return {
i: (Point(row) if bool(ok) else None)
for i, row, ok in zip(paths, placed, usable, strict=True)
}
# the default: descent_minimize is what you get with no `minimize=` at all
global_model = BLRGlobalModel(
projection_factory=SphereProjection, # the class, not an instance
key=jax.random.PRNGKey(0),
)
# a tuned one, for an inner knob the default does not expose
# global_model = BLRGlobalModel(
# projection_factory=SphereProjection,
# minimize=custom_minimize,
# key=jax.random.PRNGKey(0),
# )
result = n_path_schedule(
eval_f_g,
domain=domain,
n_paths=8,
max_iters=200,
global_model=global_model,
)
Five things to get right¶
It is the engine inside the engine: the outer run asks the model for a restart
when a path stalls, the model asks custom_minimize to descend its acquisition,
and custom_minimize runs the engine again — restart-free this time. It is the
same five things descent_minimize does, so a tuned copy can also be read as the
default with its parts in view:
restart_stalled=Falseinside. The inner run has one job: reach the acquisition's local minimum from each start. Restarts would replace this job with random wandering, so they are off: a path that settles is parked for good and never moves again.n_paths = len(paths), not a loop. The acquisition is batched, so one run of widthlen(paths)serves the whole cohort one row per path. Loopingn_paths=1runs would buy the same points withn_pathstimes the batches — the serial shape of a job the GPU wants wide.- The outer run's first ask draws, not descends. With no
x0, iteration 0 asks for the flock's placement andproposehas nothing to fit, so it answers with the domain's own draw, never callingminimize. Any later ask also draws untilobservehas given the posterior a spread to standardize by. - Every returned answer must be a finite point inside the box. An inner path
can end holding no point at all — the acquisition was not finite at its start,
and every row of a run that does zero iterations (
max_iters=0) is one — and an unplaced path carries an all-nanrow. That is the one answer to translate rather than pass on: answerNonefor it, so the outer run leaves that path where it stands instead of restarting it from a draw the acquisition never ranked. - The acquisition has no feasibility mask (
feas=None). It is finite everywhere, so the fused seam's third slot isNone.
The inner horizon is a cap, not a test¶
inner_iters (the default driver's knob) and INNER_ITERS above are an upper
bound, not a convergence test. A path that settles
stops there, so an inner run whose flock settles early ends on all_stalled
before reaching it — provided every path held a usable point, since one holding
none is still re-drawn even with restart_stalled=False; 40 is a cap with room
to spare, not an iteration count to aim at. Raise it if the outer run keeps
restarting paths that barely moved - and it is a descent_minimize knob, not a
BLRGlobalModel one: hand the model a bound copy via
minimize=functools.partial(descent_minimize, inner_iters=80).
The inner run bills its own Work, and none of it reaches the outer
result.work: the outer budget_check is handed the outer ledger alone, so a
budget that has to cover the surrogate descents must price them itself.
MinimizeFn, descent_minimize and both projections are in the
API Reference, with their exact signatures and defaults.