Skip to content

Commit

Permalink
Debug shifting experimenter due to OOB values (and revert to previous…
Browse files Browse the repository at this point in the history
… code).

PiperOrigin-RevId: 596415985
  • Loading branch information
qiuyiz authored and copybara-github committed Jan 7, 2024
1 parent 0bc5ee7 commit 9ff98b6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def __call__(self) -> experimenter.Experimenter:
exptr = self.base_factory()
if self.shift is not None:
exptr = shifting_experimenter.ShiftingExperimenter(
exptr, shift=self.shift
exptr, shift=self.shift, should_restrict=self.should_restrict
)
if self.num_normalization_samples:
exptr = normalizing_experimenter.NormalizingExperimenter(
Expand Down
7 changes: 5 additions & 2 deletions vizier/_src/benchmarks/experimenters/shifting_experimenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,16 @@ def problem_statement(self) -> pyvizier.ProblemStatement:

def evaluate(self, suggestions: Sequence[pyvizier.Trial]) -> None:
"""Evaluate the trials after shifting their parameters by +shift."""
previous_parameters = [suggestion.parameters for suggestion in suggestions]
self._offset(suggestions, self._shift)
self._exptr.evaluate(suggestions)
self._offset(suggestions, -self._shift)
# Must replace stored previous parameters since offsetting may clip.
for parameters, suggestion in zip(previous_parameters, suggestions):
suggestion.parameters = parameters

def _offset(self, suggestions: Sequence[pyvizier.Trial],
shift: np.ndarray) -> None:
"""Offsets the suggestions parameter values in place."""
"""Offsets parameter values (OOB values are clipped)."""
for suggestion in suggestions:
features = self._converter.to_features([suggestion])
new_parameters = self._converter.to_parameters(features + shift)[0]
Expand Down
21 changes: 21 additions & 0 deletions vizier/_src/benchmarks/experimenters/shifting_experimenter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,27 @@ def test_shift_forward(self):
shifted_exptr._offset([trial], shift=shift)
self.assertEqual(trial.parameters.as_dict(), {'x0': 5.0, 'x1': -5.0})

@parameterized.parameters((True,), (False,))
def test_shift_forward_oob(self, should_restrict):
dim = 2
shift = np.array([2.2, -2.3])
func = bbob.Sphere
exptr = numpy_experimenter.NumpyExperimenter(
func, bbob.DefaultBBOBProblemStatement(dim)
)
shifted_exptr = shifting_experimenter.ShiftingExperimenter(
exptr=exptr, shift=np.asarray(shift), should_restrict=should_restrict
)
# Test OOB shifts does not change parameter values.
trial = pyvizier.Trial(parameters={'x0': 3.0, 'x1': 1.0})
shifted_exptr.evaluate([trial])
self.assertEqual(trial.parameters.as_dict(), {'x0': 3.0, 'x1': 1.0})

# Test OOB shifts stay within bounds.
shifted_exptr._offset([trial], shift=shift)
# x0 is shifted OOB, so clip at 5.0.
self.assertEqual(trial.parameters.as_dict(), {'x0': 5.0, 'x1': 1.0 - 2.3})

def test_large_shift(self):
dim = 2
shift = np.array([10.2, 20.3])
Expand Down

0 comments on commit 9ff98b6

Please sign in to comment.