Skip to content

Commit 4ff4eb0

Browse files
author
Lars Riegsinger
committed
Remove generic parameter for hyperconfig and add abstract hyperconfig property instead.
1 parent c6c9b87 commit 4ff4eb0

File tree

19 files changed

+149
-78
lines changed

19 files changed

+149
-78
lines changed

bofire/benchmarks/hyperopt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(
1919
show_progress_bar: bool = False,
2020
) -> None:
2121
super().__init__()
22-
if surrogate_data.hyperconfig is None:
22+
if surrogate_data.hyperconfig_access is None:
2323
raise ValueError("No hyperoptimization configuration found.")
2424
self.surrogate_data = surrogate_data
2525
self.training_data = training_data
@@ -30,11 +30,11 @@ def __init__(
3030

3131
@property
3232
def domain(self) -> Domain:
33-
return self.surrogate_data.hyperconfig.domain # type: ignore
33+
return self.surrogate_data.hyperconfig_access.domain # type: ignore
3434

3535
@property
3636
def target_metric(self):
37-
return self.surrogate_data.hyperconfig.target_metric # type: ignore
37+
return self.surrogate_data.hyperconfig_access.target_metric # type: ignore
3838

3939
def _f(self, candidates: pd.DataFrame) -> pd.DataFrame:
4040
for i, candidate in tqdm(
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1-
from typing import Literal
1+
from typing import Literal, Optional, Type
22

3+
from pydantic import Field
4+
5+
from bofire.data_models.features.api import AnyOutput
6+
from bofire.data_models.features.continuous import ContinuousOutput
37
from bofire.data_models.kernels.api import InfiniteWidthBNNKernel
4-
from bofire.data_models.surrogates.single_task_gp import BaseSingleTaskGPSurrogate
8+
from bofire.data_models.priors.api import HVARFNER_NOISE_PRIOR, AnyPrior
9+
from bofire.data_models.surrogates.single_task_gp import TrainableBotorchSurrogate
10+
from bofire.data_models.surrogates.trainable import Hyperconfig
511

612

7-
class SingleTaskIBNNSurrogate(BaseSingleTaskGPSurrogate[InfiniteWidthBNNKernel]):
13+
class SingleTaskIBNNSurrogate(TrainableBotorchSurrogate):
814
type: Literal["SingleTaskIBNNSurrogate"] = "SingleTaskIBNNSurrogate"
15+
kernel: InfiniteWidthBNNKernel = InfiniteWidthBNNKernel()
16+
hyperconfig: Optional[Hyperconfig] = None
17+
noise_prior: AnyPrior = Field(default_factory=lambda: HVARFNER_NOISE_PRIOR())
18+
19+
@classmethod
20+
def is_output_implemented(cls, my_type: Type[AnyOutput]) -> bool:
21+
"""Abstract method to check output type for surrogate models
22+
Args:
23+
my_type: continuous or categorical output
24+
Returns:
25+
bool: True if the output type is valid for the surrogate chosen, False otherwise
26+
"""
27+
return isinstance(my_type, type(ContinuousOutput))
28+
29+
@property
30+
def hyperconfig_access(self) -> Optional[Hyperconfig]:
31+
return self.hyperconfig

bofire/data_models/surrogates/fully_bayesian.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Annotated, List, Literal, Type
1+
from typing import Annotated, List, Literal, Optional, Type
22

33
from pydantic import AfterValidator, Field, field_validator, model_validator
44

@@ -8,7 +8,7 @@
88
from bofire.data_models.types import make_unique_validator
99

1010

11-
class FullyBayesianSingleTaskGPSurrogate(TrainableBotorchSurrogate[Hyperconfig]):
11+
class FullyBayesianSingleTaskGPSurrogate(TrainableBotorchSurrogate):
1212
type: Literal["FullyBayesianSingleTaskGPSurrogate"] = (
1313
"FullyBayesianSingleTaskGPSurrogate"
1414
)
@@ -20,6 +20,12 @@ class FullyBayesianSingleTaskGPSurrogate(TrainableBotorchSurrogate[Hyperconfig])
2020
List[str], AfterValidator(make_unique_validator("Features"))
2121
] = []
2222

23+
hyperconfig: Optional[Hyperconfig] = None
24+
25+
@property
26+
def hyperconfig_access(self) -> Optional[Hyperconfig]:
27+
return self.hyperconfig
28+
2329
@model_validator(mode="after")
2430
def validate_features_to_warp(self):
2531
input_keys = self.inputs.get_keys()

bofire/data_models/surrogates/linear.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, Type
1+
from typing import Literal, Optional, Type
22

33
from pydantic import Field
44

@@ -7,12 +7,19 @@
77
from bofire.data_models.kernels.api import LinearKernel
88
from bofire.data_models.priors.api import THREESIX_NOISE_PRIOR, AnyPrior
99
from bofire.data_models.surrogates.scaler import ScalerEnum
10+
from bofire.data_models.surrogates.trainable import Hyperconfig
1011
from bofire.data_models.surrogates.trainable_botorch import TrainableBotorchSurrogate
1112

1213

1314
class LinearSurrogate(TrainableBotorchSurrogate):
1415
type: Literal["LinearSurrogate"] = "LinearSurrogate"
1516

17+
hyperconfig: Optional[Hyperconfig] = None
18+
19+
@property
20+
def hyperconfig_access(self) -> Optional[Hyperconfig]:
21+
return self.hyperconfig
22+
1623
kernel: LinearKernel = Field(default_factory=lambda: LinearKernel())
1724
noise_prior: AnyPrior = Field(default_factory=lambda: THREESIX_NOISE_PRIOR())
1825
scaler: ScalerEnum = ScalerEnum.NORMALIZE

bofire/data_models/surrogates/map_saas.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import Literal, Type
1+
from typing import Literal, Optional, Type
22

3-
from pydantic import PositiveInt
3+
from pydantic import Field, PositiveInt
44

55
from bofire.data_models.features.api import AnyOutput, ContinuousOutput
66
from bofire.data_models.surrogates.trainable import Hyperconfig
@@ -11,7 +11,7 @@ class TestSurrogate:
1111
pass
1212

1313

14-
class AdditiveMapSaasSingleTaskGPSurrogate(TrainableBotorchSurrogate[Hyperconfig]):
14+
class AdditiveMapSaasSingleTaskGPSurrogate(TrainableBotorchSurrogate):
1515
"""Additive MAP SAAS single-task GP
1616
1717
Maximum-a-posteriori (MAP) version of the sparse axis-aligned subspace
@@ -26,6 +26,12 @@ class AdditiveMapSaasSingleTaskGPSurrogate(TrainableBotorchSurrogate[Hyperconfig
2626
)
2727
n_taus: PositiveInt = 4
2828

29+
hyperconfig: Optional[Hyperconfig] = Field(default=None)
30+
31+
@property
32+
def hyperconfig_access(self) -> Optional[Hyperconfig]:
33+
return self.hyperconfig
34+
2935
@classmethod
3036
def is_output_implemented(cls, my_type: Type[AnyOutput]) -> bool:
3137
"""Abstract method to check output type for surrogate models

bofire/data_models/surrogates/mixed_single_task_gp.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ class MixedSingleTaskGPHyperconfig(Hyperconfig):
5555
] = "FractionalFactorialStrategy"
5656

5757

58-
class MixedSingleTaskGPSurrogate(
59-
TrainableBotorchSurrogate[MixedSingleTaskGPHyperconfig]
60-
):
58+
class MixedSingleTaskGPSurrogate(TrainableBotorchSurrogate):
6159
type: Literal["MixedSingleTaskGPSurrogate"] = "MixedSingleTaskGPSurrogate"
6260
continuous_kernel: AnyContinuousKernel = Field(
6361
default_factory=lambda: RBFKernel(
@@ -76,6 +74,10 @@ class MixedSingleTaskGPSurrogate(
7674
default_factory=lambda: MixedSingleTaskGPHyperconfig(),
7775
)
7876

77+
@property
78+
def hyperconfig_access(self) -> Optional[Hyperconfig]:
79+
return self.hyperconfig
80+
7981
@classmethod
8082
def _default_categorical_encodings(
8183
cls,
@@ -145,8 +147,7 @@ def is_output_implemented(cls, my_type: Type[AnyOutput]) -> bool:
145147
bool: True if the output type is valid for the surrogate chosen, False otherwise
146148
"""
147149
return isinstance(my_type, type(ContinuousOutput))
148-
149-
150+
150151
def update_hyperparameters(
151152
self,
152153
hyperparameters: pd.Series,
@@ -194,4 +195,3 @@ def update_hyperparameters(
194195

195196
else:
196197
raise ValueError(f"Kernel {hyperparameters.kernel} not known.")
197-

bofire/data_models/surrogates/mlp.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Sequence
2-
from typing import Annotated, Literal, Type
2+
from typing import Annotated, Literal, Optional, Type
33

44
from pydantic import Field
55

@@ -9,11 +9,18 @@
99
ContinuousOutput,
1010
)
1111
from bofire.data_models.surrogates.scaler import ScalerEnum
12+
from bofire.data_models.surrogates.trainable import Hyperconfig
1213
from bofire.data_models.surrogates.trainable_botorch import TrainableBotorchSurrogate
1314

1415

1516
class MLPEnsemble(TrainableBotorchSurrogate):
1617
type: Literal["MLPEnsemble"] = "MLPEnsemble"
18+
hyperconfig: Optional[Hyperconfig] = None
19+
20+
@property
21+
def hyperconfig_access(self) -> Optional[Hyperconfig]:
22+
return self.hyperconfig
23+
1724
n_estimators: Annotated[int, Field(ge=1)] = 5
1825
hidden_layer_sizes: Sequence = (100,)
1926
activation: Literal["relu", "logistic", "tanh"] = "relu"

bofire/data_models/surrogates/multi_task_gp.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class MultiTaskGPHyperconfig(Hyperconfig):
4545
] = "FractionalFactorialStrategy"
4646

4747

48-
class MultiTaskGPSurrogate(TrainableBotorchSurrogate[MultiTaskGPHyperconfig]):
48+
class MultiTaskGPSurrogate(TrainableBotorchSurrogate):
4949
type: Literal["MultiTaskGPSurrogate"] = "MultiTaskGPSurrogate"
5050
kernel: AnyKernel = Field(
5151
default_factory=lambda: MaternKernel(
@@ -60,6 +60,10 @@ class MultiTaskGPSurrogate(TrainableBotorchSurrogate[MultiTaskGPHyperconfig]):
6060
default_factory=lambda: MultiTaskGPHyperconfig(),
6161
)
6262

63+
@property
64+
def hyperconfig_access(self) -> Optional[Hyperconfig]:
65+
return self.hyperconfig
66+
6367
@classmethod
6468
def _default_categorical_encodings(
6569
cls,
@@ -118,7 +122,7 @@ def validate_encoding(cls, v, info):
118122
def update_hyperparameters(
119123
self,
120124
hyperparameters: pd.Series,
121-
):
125+
):
122126
super().update_hyperparameters(hyperparameters)
123127

124128
def matern_25(ard: bool, lengthscale_prior: AnyPrior) -> MaternKernel:

bofire/data_models/surrogates/polynomial.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
from typing import Literal, Type
1+
from typing import Literal, Optional, Type
22

33
from pydantic import Field
44

55
from bofire.data_models.domain.api import Inputs, Outputs
66
from bofire.data_models.features.api import AnyOutput, ContinuousOutput
77
from bofire.data_models.kernels.api import PolynomialKernel
88
from bofire.data_models.priors.api import THREESIX_NOISE_PRIOR, AnyPrior
9+
from bofire.data_models.surrogates.trainable import Hyperconfig
910
from bofire.data_models.surrogates.trainable_botorch import TrainableBotorchSurrogate
1011

1112

1213
class PolynomialSurrogate(TrainableBotorchSurrogate):
1314
type: Literal["PolynomialSurrogate"] = "PolynomialSurrogate"
15+
hyperconfig: Optional[Hyperconfig] = None
16+
17+
@property
18+
def hyperconfig_access(self) -> Optional[Hyperconfig]:
19+
return self.hyperconfig
1420

1521
kernel: PolynomialKernel = Field(default_factory=lambda: PolynomialKernel(power=2))
1622
noise_prior: AnyPrior = Field(default_factory=lambda: THREESIX_NOISE_PRIOR())

bofire/data_models/surrogates/random_forest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
from bofire.data_models.surrogates.trainable_botorch import TrainableBotorchSurrogate
88

99

10-
class RandomForestSurrogate(TrainableBotorchSurrogate[Hyperconfig]):
10+
class RandomForestSurrogate(TrainableBotorchSurrogate):
1111
type: Literal["RandomForestSurrogate"] = "RandomForestSurrogate"
1212

13+
hyperconfig: Optional[Hyperconfig] = None
14+
15+
@property
16+
def hyperconfig_access(self) -> Optional[Hyperconfig]:
17+
return self.hyperconfig
18+
1319
# hyperparams passed down to `RandomForestRegressor`
1420
n_estimators: int = 100
1521
criterion: Literal[

0 commit comments

Comments
 (0)