refactor: Simplify optimizer interface by removing ambiguous f_df parameter#486
Open
apaleyes-bot wants to merge 2 commits into
Open
refactor: Simplify optimizer interface by removing ambiguous f_df parameter#486apaleyes-bot wants to merge 2 commits into
apaleyes-bot wants to merge 2 commits into
Conversation
…ameter - Remove f_df parameter from Optimizer.optimize() and apply_optimizer() - Make f (objective function) required positional parameter - Simplify gradient handling logic in OptLbfgs and OptTrustRegionConstrained - Update all call sites and tests to use new signature - Add validation for required f parameter - Improves API clarity and prevents parameter confusion bugs Fixes EmuKit#218
d1b0660 to
e2634ec
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request: Refactor Optimizer Interface (Issue #218)
Title
refactor: Simplify optimizer interface by removing ambiguous f_df parameterDescription
Problem
The optimizer interface had a confusing design with three overlapping parameters:
This created ambiguity:
dfandf_df?This was legacy cruft from GPyOpt that was never properly refactored.
Solution
Remove
f_dfentirely and make the interface explicit:f(objective function) is requireddf(gradient) is optionaldfis provided, it's used; otherwise gradients are approximatedChanges
Optimizer classes – Simplified
optimize()signaturesf_dfparameterfrequired (positional)apply_optimizer() – Updated to match new signature
f_dfparameterOptimizationWithContextCall sites – Updated to use new signature
GradientAcquisitionOptimizer._optimize()Backward compatibility – N/A (breaking API change, but optimizer is internal)
Benefits
✅ No ambiguity – Clear semantics for what parameters mean
✅ Fewer bugs – Impossible to pass conflicting parameters
✅ Cleaner code – Less lambda juggling, easier to understand
✅ Better errors – Missing
fis caught immediately✅ Easier to maintain – Simpler conditional logic in each optimizer
Code Example
Before:
After:
Files Changed
emukit/core/optimization/optimizer.pyOptimizer.optimize()signatureOptLbfgs.optimize()logicOptTrustRegionConstrained.optimize()logicapply_optimizer()functionOptimizationWithContextemukit/core/optimization/gradient_acquisition_optimizer.pyapply_optimizer()tests/emukit/core/optimization/test_optimizer.pyTesting
Behavior Verification
New Test Cases
fraises errorfraises errorTest Results
Performance Impact
None – this is a refactoring with no algorithmic changes.
Breaking Changes
Yes – this is an internal API refactor. The optimizer signature is not part of the public API, but dependent code needs updating:
apply_optimizer()signature changedOptimizer.optimize()signature changed (internal)optimize()methods need updatingReview Checklist
apply_optimizer,.optimize()Related Issues
Closes #218
Notes for Reviewers
Design Decision: Why remove f_df instead of making parameters mutually exclusive?
Option A (chosen): Remove f_df
Option B: Validate mutual exclusivity
We chose Option A because:
Why make f required?
The objective function is essential – without it, optimization is meaningless. Making it required:
Testing note
The refactored code is functionally identical to the original – same numpy operations, same scipy calls. The logic is just clearer. This makes testing straightforward: same inputs, same outputs.
Migration Guide (for dependent code)
If you're calling
apply_optimizer()or optimizer subclasses directly:From:
To:
Key changes:
fpositional (2nd argument)f_dfdfas the gradient function (or None)