Skip to content

Commit

Permalink
Small tidyness updates
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 629804683
  • Loading branch information
xingyousong authored and copybara-github committed May 1, 2024
1 parent 0b91568 commit 4ec4c55
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
7 changes: 4 additions & 3 deletions vizier/_src/benchmarks/experimenters/experimenter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def recover(cls, metadata: vz.Metadata) -> 'BBOBExperimenterFactory':
return cls(**metadata_dict)


@attr.define
@attr.define(kw_only=True)
class SingleObjectiveExperimenterFactory(SerializableExperimenterFactory):
"""Factory for a single objective Experimenter.
Expand All @@ -127,9 +127,10 @@ class SingleObjectiveExperimenterFactory(SerializableExperimenterFactory):
categorical instead of discrete.
"""

base_factory: SerializableExperimenterFactory = attr.field()
base_factory: SerializableExperimenterFactory = attr.field(kw_only=False)

shift: Optional[np.ndarray] = attr.field(default=None)
should_restrict: bool = attr.field(default=True, kw_only=True)
should_restrict: bool = attr.field(default=True)
noise_type: Optional[str] = attr.field(default=None)
noise_seed: int = attr.field(default=0)
num_normalization_samples: int = attr.field(default=0)
Expand Down
54 changes: 29 additions & 25 deletions vizier/_src/benchmarks/experimenters/shifting_experimenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ def __init__(
exptr_problem_statement = exptr.problem_statement()

if exptr_problem_statement.search_space.is_conditional:
raise ValueError('Search space should not have conditional'
f' parameters {exptr_problem_statement}')
raise ValueError(
'Search space should not have conditional'
f' parameters {exptr_problem_statement}'
)
dimension = len(exptr_problem_statement.search_space.parameters)
if dimension <= 0:
raise ValueError(f'Invalid dimension: {dimension}')
Expand All @@ -64,8 +66,8 @@ def __init__(
self._shift = np.broadcast_to(shift, (dimension,))
except ValueError as broadcast_err:
raise ValueError(
f'Shift {shift} is not broadcastable for dim: {dimension}.'
'\n') from broadcast_err
f'Shift {shift} is not broadcastable for dim: {dimension}.\n'
) from broadcast_err

# Converter should be in the underlying extpr space.
self._converter = converters.TrialToArrayConverter.from_study_config(
Expand All @@ -83,26 +85,27 @@ def __init__(
):
if parameter.type != pyvizier.ParameterType.DOUBLE:
raise ValueError(f'Non-double parameters {parameter}')
if (bounds := parameter.bounds) is not None:
if abs(shift) >= bounds[1] - bounds[0]:
raise ValueError(
f'Bounds {bounds} may need to be extended'
f'as shift {shift} is too large '
)
# Shift the bounds to maintain valid bounds.
if shift >= 0:
new_bounds = (bounds[0] + shift, bounds[1])
else:
new_bounds = (bounds[0], bounds[1] + shift)
self._problem_statement.search_space.add(
pyvizier.ParameterConfig.factory(
name=parameter.name,
bounds=new_bounds,
scale_type=parameter.scale_type,
default_value=parameter.default_value,
external_type=parameter.external_type,
)

bounds = parameter.bounds
if abs(shift) >= bounds[1] - bounds[0]:
raise ValueError(
f'Bounds {bounds} may need to be extended'
f'as shift {shift} is too large '
)
# Shift the bounds to maintain valid bounds.
if shift >= 0:
new_bounds = (bounds[0] + shift, bounds[1])
else:
new_bounds = (bounds[0], bounds[1] + shift)
self._problem_statement.search_space.add(
pyvizier.ParameterConfig.factory(
name=parameter.name,
bounds=new_bounds,
scale_type=parameter.scale_type,
default_value=parameter.default_value,
external_type=parameter.external_type,
),
)

def problem_statement(self) -> pyvizier.ProblemStatement:
return copy.deepcopy(self._problem_statement)
Expand All @@ -116,8 +119,9 @@ def evaluate(self, suggestions: Sequence[pyvizier.Trial]) -> None:
for parameters, suggestion in zip(previous_parameters, suggestions):
suggestion.parameters = parameters

def _offset(self, suggestions: Sequence[pyvizier.Trial],
shift: np.ndarray) -> None:
def _offset(
self, suggestions: Sequence[pyvizier.Trial], shift: np.ndarray
) -> None:
"""Offsets parameter values (OOB values are clipped)."""
for suggestion in suggestions:
features = self._converter.to_features([suggestion])
Expand Down

0 comments on commit 4ec4c55

Please sign in to comment.