Skip to content

Commit

Permalink
Revert "[WIP] Change suggestion building in pytest example"
Browse files Browse the repository at this point in the history
This reverts commit 6c3a482.
  • Loading branch information
ephoris committed Feb 5, 2024
1 parent 9f129a5 commit 55bf6b3
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions mlos_core/mlos_core/tests/optimizers/optimizer_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
"""
Expand Down Expand Up @@ -299,11 +301,14 @@ def test_optimizer_type_defs(optimizer_class: Type[BaseOptimizer]) -> None:


@pytest.mark.parametrize(('optimizer_type', 'kwargs'), [
# Default optimizer
(None, {}),
# Enumerate all supported Optimizers
*[(member, {}) for member in OptimizerType],
# Optimizer with non-empty kwargs argument
(OptimizerType.SMAC, {
# Test with default config.
'use_default_config': True,
# 'n_random_init': 10,
}),
])
def test_mixed_input_space_types(optimizer_type: OptimizerType, kwargs: Optional[dict]) -> None:
"""
Expand All @@ -323,17 +328,11 @@ def objective(point: pd.DataFrame) -> pd.Series:
input_space.add_hyperparameter(CS.UniformIntegerHyperparameter(name='x', lower=0, upper=5))
input_space.add_hyperparameter(CS.UniformFloatHyperparameter(name='y', lower=0.0, upper=5.0))

if optimizer_type is None:
optimizer: BaseOptimizer = OptimizerFactory.create(
parameter_space=input_space,
optimizer_kwargs=kwargs,
)
else:
optimizer: BaseOptimizer = OptimizerFactory.create(
parameter_space=input_space,
optimizer_type=optimizer_type,
optimizer_kwargs=kwargs,
)
optimizer: BaseOptimizer = OptimizerFactory.create(
parameter_space=input_space,
optimizer_type=optimizer_type,
optimizer_kwargs=kwargs,
)

with pytest.raises(ValueError, match="No observations"):
optimizer.get_best_observation()
Expand All @@ -345,12 +344,8 @@ def objective(point: pd.DataFrame) -> pd.Series:
suggestion = optimizer.suggest()
assert isinstance(suggestion, pd.DataFrame)
assert (suggestion.columns == ['x', 'y']).all()
# Build suggestion mapping to cooperate with Configuration, note that
# doing a .iloc[0].to_dict() will cause pandas convert all numeric types
# to float64
tmp_suggest = {'x': suggestion['x'].values[0], 'y': suggestion['y'].values[0]}
# check that suggestion is in the space
configuration = CS.Configuration(optimizer.parameter_space, tmp_suggest)
configuration = CS.Configuration(optimizer.parameter_space, suggestion.iloc[0].to_dict())
# Raises an error if outside of configuration space
configuration.is_valid_configuration()
observation = objective(suggestion)
Expand Down

0 comments on commit 55bf6b3

Please sign in to comment.