Source code for ax.modelbridge.transforms.search_space_to_choice
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from typing import TYPE_CHECKING, List, Optional
from ax.core.arm import Arm
from ax.core.observation import ObservationData, ObservationFeatures
from ax.core.parameter import ChoiceParameter, FixedParameter, ParameterType
from ax.core.search_space import SearchSpace
from ax.core.types import TConfig
from ax.modelbridge.transforms.base import Transform
from ax.utils.common.typeutils import checked_cast
if TYPE_CHECKING:
# import as module to make sphinx-autodoc-typehints happy
from ax import modelbridge as modelbridge_module # noqa F401 # pragma: no cover
[docs]class SearchSpaceToChoice(Transform):
"""Replaces the search space with a single choice parameter, whose values
are the signatures of the arms observed in the data.
This transform is meant to be used with ThompsonSampler.
Choice parameter will be unordered unless config["use_ordered"] specifies
otherwise.
Transform is done in-place.
"""
def __init__(
self,
search_space: SearchSpace,
observation_features: List[ObservationFeatures],
observation_data: List[ObservationData],
modelbridge: Optional["modelbridge_module.base.ModelBridge"] = None,
config: Optional[TConfig] = None,
) -> None:
super().__init__(
search_space=search_space,
observation_features=observation_features,
observation_data=observation_data,
config=config,
)
if any(p.is_fidelity for p in search_space.parameters.values()):
raise ValueError(
"Cannot perform SearchSpaceToChoice conversion if fidelity "
"parameters are present"
)
self.parameter_name = "arms"
self.signature_to_parameterization = {
Arm(parameters=obsf.parameters).signature: obsf.parameters
for obsf in observation_features
}