-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
1 deletion.
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
49 changes: 49 additions & 0 deletions
49
bofire/data_models/strategies/samplers/universal_constraint.py
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,49 @@ | ||
from typing import Annotated, Literal, Type | ||
|
||
from pydantic import Field | ||
|
||
from bofire.data_models.constraints.api import ( | ||
LinearEqualityConstraint, | ||
LinearInequalityConstraint, | ||
NChooseKConstraint, | ||
NonlinearEqualityConstraint, | ||
NonlinearInequalityConstraint, | ||
) | ||
from bofire.data_models.features.api import ( | ||
ContinuousInput, | ||
ContinuousOutput, | ||
Feature, | ||
) | ||
from bofire.data_models.strategies.strategy import Strategy | ||
|
||
|
||
class UniversalConstraintSampler(Strategy): | ||
"""Sampler that generates samples by optimization in IPOPT. | ||
Attributes: | ||
domain (Domain): Domain defining the constrained input space | ||
sampling_fraction (float, optional): Fraction of sampled points to total points generated in | ||
the sampling process. Defaults to 0.3. | ||
ipopt_options (dict, optional): Dictionary containing options for the IPOPT solver. Defaults to {"maxiter":200, "disp"=0}. | ||
""" | ||
|
||
type: Literal["UniversalConstraintSampler"] = "UniversalConstraintSampler" | ||
sampling_fraction: Annotated[float, Field(gt=0, lt=1)] = 0.3 | ||
ipopt_options: dict = {"maxiter": 200, "disp": 0} | ||
|
||
@classmethod | ||
def is_constraint_implemented(cls, my_type: Type[Feature]) -> bool: | ||
return my_type in [ | ||
LinearEqualityConstraint, | ||
LinearInequalityConstraint, | ||
NonlinearInequalityConstraint, | ||
NonlinearEqualityConstraint, | ||
NChooseKConstraint, | ||
] | ||
|
||
@classmethod | ||
def is_feature_implemented(cls, my_type: Type[Feature]) -> bool: | ||
return my_type in [ | ||
ContinuousInput, | ||
ContinuousOutput, | ||
] |
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,55 @@ | ||
import pandas as pd | ||
|
||
from bofire.data_models.strategies.api import UniversalConstraintSampler as DataModel | ||
from bofire.strategies.doe.design import find_local_max_ipopt | ||
from bofire.strategies.enum import OptimalityCriterionEnum | ||
from bofire.strategies.strategy import Strategy | ||
|
||
|
||
class UniversalConstraintSampler(Strategy): | ||
"""Sampler that generates samples by optimization in IPOPT. | ||
Attributes: | ||
domain (Domain): Domain defining the constrained input space | ||
sampling_fraction (float, optional): Fraction of sampled points to total points generated in | ||
the sampling process. Defaults to 0.3. | ||
ipopt_options (dict, optional): Dictionary containing options for the IPOPT solver. Defaults to {"maxiter":200, "disp"=0}. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
data_model: DataModel, | ||
**kwargs, | ||
): | ||
super().__init__(data_model=data_model, **kwargs) | ||
assert data_model.sampling_fraction > 0 and data_model.sampling_fraction <= 1 | ||
self.sampling_fraction = data_model.sampling_fraction | ||
self.ipopt_options = data_model.ipopt_options | ||
|
||
def _ask(self, candidate_count: int) -> pd.DataFrame: | ||
samples = find_local_max_ipopt( | ||
domain=self.domain, | ||
model_type="linear", # dummy model | ||
n_experiments=self.num_candidates | ||
+ int(candidate_count / self.sampling_fraction), | ||
ipopt_options=self.ipopt_options, | ||
objective=OptimalityCriterionEnum.SPACE_FILLING, | ||
fixed_experiments=self.candidates, | ||
) | ||
|
||
samples = samples.iloc[ | ||
self.num_candidates :, | ||
] | ||
samples = samples.sample( | ||
n=candidate_count, | ||
replace=False, | ||
ignore_index=True, | ||
random_state=self._get_seed(), | ||
) | ||
|
||
self.domain.validate_experiments(samples) | ||
|
||
return samples | ||
|
||
def has_sufficient_experiments(self) -> bool: | ||
return True |
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