-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add linear deterministic surrogate
- Loading branch information
Showing
8 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Annotated, Dict, Literal, Type | ||
|
||
from pydantic import Field, model_validator | ||
|
||
from bofire.data_models.features.api import ( | ||
AnyOutput, | ||
ContinuousInput, | ||
ContinuousOutput, | ||
DiscreteInput, | ||
) | ||
from bofire.data_models.surrogates.botorch import BotorchSurrogate | ||
|
||
|
||
class LinearDeterministicSurrogate(BotorchSurrogate): | ||
type: Literal["LinearDeterministicSurrogate"] = "LinearDeterministicSurrogate" | ||
coefficients: Annotated[Dict[str, float], Field(min_length=1)] | ||
intercept: float | ||
|
||
@classmethod | ||
def is_output_implemented(cls, my_type: Type[AnyOutput]) -> bool: | ||
"""Abstract method to check output type for surrogate models | ||
Args: | ||
my_type: continuous or categorical output | ||
Returns: | ||
bool: True if the output type is valid for the surrogate chosen, False otherwise | ||
""" | ||
return isinstance(my_type, type(ContinuousOutput)) | ||
|
||
@model_validator(mode="after") | ||
def validate_input_types(self): | ||
if len(self.inputs.get([ContinuousInput, DiscreteInput])) != len(self.inputs): | ||
raise ValueError( | ||
"Only numerical inputs are suppoerted for the `LinearDeterministicSurrogate`" | ||
) | ||
return self | ||
|
||
@model_validator(mode="after") | ||
def validate_coefficients(self): | ||
if sorted(self.inputs.get_keys()) != sorted(self.coefficients.keys()): | ||
raise ValueError("coefficient keys do not match input feature keys.") | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import torch | ||
from botorch.models.deterministic import AffineDeterministicModel | ||
|
||
from bofire.data_models.surrogates.api import LinearDeterministicSurrogate as DataModel | ||
from bofire.surrogates.botorch import BotorchSurrogate | ||
from bofire.utils.torch_tools import tkwargs | ||
|
||
|
||
class LinearDeterministicSurrogate(BotorchSurrogate): | ||
def __init__( | ||
self, | ||
data_model: DataModel, | ||
**kwargs, | ||
): | ||
self.intercept = data_model.intercept | ||
self.coefficients = data_model.coefficients | ||
super().__init__(data_model=data_model, **kwargs) | ||
self.model = AffineDeterministicModel( | ||
b=data_model.intercept, | ||
a=torch.tensor( | ||
[data_model.coefficients[key] for key in self.inputs.get_keys()] | ||
) | ||
.to(**tkwargs) | ||
.unsqueeze(-1), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import pandas as pd | ||
from pandas.testing import assert_frame_equal | ||
|
||
import bofire.surrogates.api as surrogates | ||
from bofire.data_models.domain.api import Inputs, Outputs | ||
from bofire.data_models.features.api import ContinuousInput, ContinuousOutput | ||
from bofire.data_models.surrogates.api import LinearDeterministicSurrogate | ||
|
||
|
||
def test_linear_deterministic_surrogate(): | ||
surrogate_data = LinearDeterministicSurrogate( | ||
inputs=Inputs( | ||
features=[ | ||
ContinuousInput(key="a", bounds=(0, 1)), | ||
ContinuousInput(key="b", bounds=(0, 1)), | ||
] | ||
), | ||
outputs=Outputs(features=[ContinuousOutput(key="y")]), | ||
intercept=2.0, | ||
coefficients={"b": 3.0, "a": -2.0}, | ||
) | ||
surrogate = surrogates.map(surrogate_data) | ||
assert surrogate.input_preprocessing_specs == {} | ||
experiments = pd.DataFrame(data={"a": [1.0, 2.0], "b": [0.5, 4.0]}) | ||
preds = surrogate.predict(experiments) | ||
assert_frame_equal(preds, pd.DataFrame(data={"y_pred": [1.5, 10.0], "y_sd": 0.0})) |