Optimizer Workflows
Use this sub-skill when a user wants to run the core bayes_opt.BayesianOptimization
lifecycle for a black-box objective or a small machine-learning hyperparameter
optimization job. This route is self-contained: future agents should not need to
open the original repository docs, notebooks, tests, or examples.
Load This When
- The user has a Python function or external evaluation service and wants to optimize scalar parameters with Bayesian optimization.
- The task mentions
BayesianOptimization,pbounds,maximize,max,res,probe,register,suggest,random_sample,set_bounds,set_gp_params,save_state,load_state, orpredict. - The user asks how to turn a loss into an optimization target, persist an optimizer, resume a run, validate results, or handle duplicate/noisy observations.
- The user wants a tiny scikit-learn HPO pattern using
bayesian-optimization.
Core Rules To Apply First
- The package maximizes target values. Convert losses to scores, commonly by
returning
-lossor using a scikit-learn scorer whose larger value is better. pboundskeys must match the objective function keyword arguments exactly. Dict inputs are safest. Array/list inputs use thepboundsinsertion order.- Keep objective functions deterministic enough for the GP when possible. For
noisy objectives, set
allow_duplicate_points=Trueonly when repeated evaluations at the same coordinates are intentional. - Use
f=Noneonly for manual ask-tell loops: callsuggest(), evaluate outside the optimizer, thenregister(params, target). Do not call eagerprobe(..., lazy=False)ormaximize()withf=None. - After
maximize(), callpredict(..., fit_gp=True)before relying on GP predictions; the final registered point may not be included in the fitted GP until prediction refits it. - Save/load state as JSON with a newly constructed compatible optimizer: same
objective-compatible kwargs,
pbounds, acquisition strategy, constraints, duplicate policy, and any bounds transformer.
Bundled References
- API reference: constructor and method
signatures, lifecycle semantics,
TargetSpaceresult behavior,predictreturn shapes, and state persistence details. - Workflow recipes: basic optimization, manual ask-tell evaluations, tiny HPO patterns, loss sign handling, persistence, duplicate/noisy recipes, bounds updates, and result validation.
- Troubleshooting: symptoms, causes, and fixes
for key mismatches, duplicate points, unfitted GP errors, stale state loads,
target-sign mistakes, bounds/type changes, slow objectives, and
f=Nonemisuse.
Bundled Smoke Scripts
scripts/bo_core_smoke.py: deterministic core API smoke test. It runs a tiny optimizer, validatesmax/res, exercisesprobe,register,suggest,random_sample,set_bounds,set_gp_params,predict, duplicate handling, and optional JSON save/load.scripts/sklearn_hpo_smoke.py: deterministic tiny scikit-learn HPO diagnostic with synthetic data. It demonstrates both maximizing a score and negating a loss, with safe integer casting for model hyperparameters.
Run scripts only in an environment where bayesian-optimization and its normal
Python dependencies are installed. Both scripts support --help, use bounded
synthetic data, do not download from the network, and avoid destructive writes.
Routing Boundaries
Stay in this sub-skill for the optimizer lifecycle, public result validation,
manual ask-tell loops, persistence/resume patterns, predict semantics, noisy
or duplicate objectives, bounds updates, GP parameter tuning through
set_gp_params, and ordinary small HPO recipes.
Route elsewhere for features that have distinct APIs or deeper contracts:
- Acquisition choices, custom acquisition functions,
ConstantLiar,GPHedge, and exploration/exploitation controls:../acquisition-control/SKILL.md. - Constraints, typed/categorical parameters beyond simple wrapper casting,
custom
BayesParameter, and sequential domain reduction:../advanced-domain-features/SKILL.md. - Repository editing, tests, docs, release, packaging, linting, and maintainer
workflows:
../repo-maintenance/SKILL.md.
Minimal Decision Flow
- Identify objective type:
- In-process Python function: create
BayesianOptimization(f=objective, ...). - External/manual evaluation: create
BayesianOptimization(f=None, ...)and use ask-tell withsuggest()/register().
- In-process Python function: create
- Define
pboundswith exact parameter names and realistic finite bounds. Prefer log-transformed parameters for scale-sensitive HPO (log10_C,log_learning_rate) and transform inside the objective wrapper. - Choose a budget. For smoke tests use tiny values such as
init_points=2,n_iter=2; for expensive tasks, increase only after validating objective sign, result shape, and runtime. - Run
maximize(init_points=..., n_iter=...)or the ask-tell loop. - Validate
optimizer.maxis notNone, has keystargetandparams, all returned params are within intended bounds,len(optimizer.res)matches the number of registered observations, and the best target uses the intended metric sign. - Persist long or expensive runs with
save_state(path). To resume, recreate a compatible optimizer and callload_state(path)before more suggestions ormaximize(init_points=0, n_iter=...). - If predictions or uncertainty are needed, call
predict()with explicitfit_gp=Trueafter at least one observation; usereturn_std=Trueorreturn_cov=True, never both.
Handoff Checklist For Future Agents
- Objective wrapper returns a finite scalar target and uses the correct sign.
pboundskeys and objective kwargs match exactly.- Random seeds are set where reproducibility matters.
allow_duplicate_pointsis justified for noise; otherwise duplicate errors are treated as a signal to change the loop.- State files are JSON and loaded into an optimizer with compatible settings.
- Any advanced acquisition, constraint, typed-parameter, or domain-reduction need has been routed to the sibling sub-skill instead of duplicated here.
