Skip to content

Commit

Permalink
Refactor Random Strategy II (#352)
Browse files Browse the repository at this point in the history
* integrate polytope sampling into random strategy

* rename universal to space filling
  • Loading branch information
jduerholt authored Feb 13, 2024
1 parent 67f3320 commit 7d6af93
Show file tree
Hide file tree
Showing 33 changed files with 534 additions and 696 deletions.
2 changes: 1 addition & 1 deletion bofire/data_models/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
AnyCondition,
AnyLocalSearchConfig,
AnyPredictive,
AnySampler,
AnyStrategy,
PredictiveStrategy,
Strategy,
Expand All @@ -39,6 +38,7 @@

data_model_list = [
AnyAcquisitionFunction,
AnyCondition,
AnyConstraint,
AnyFeature,
AnyKernel,
Expand Down
2 changes: 2 additions & 0 deletions bofire/data_models/domain/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ def sample(
Returns:
pd.DataFrame: Dataframe containing the samples.
"""
if len(self) == 0:
return pd.DataFrame()
if method == SamplingMethodEnum.UNIFORM:
# we cannot just propagate the provided seed to
# the sample methods as they would then sample
Expand Down
10 changes: 2 additions & 8 deletions bofire/data_models/strategies/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from bofire.data_models.strategies.doe import DoEStrategy
from bofire.data_models.strategies.factorial import FactorialStrategy
from bofire.data_models.strategies.polytope import PolytopeSampler
from bofire.data_models.strategies.predictives.botorch import LSRBO, BotorchStrategy
from bofire.data_models.strategies.predictives.mobo import MoboStrategy
from bofire.data_models.strategies.predictives.multiobjective import (
Expand All @@ -20,6 +19,7 @@
)
from bofire.data_models.strategies.random import RandomStrategy
from bofire.data_models.strategies.shortest_path import ShortestPathStrategy
from bofire.data_models.strategies.space_filling import SpaceFillingStrategy
from bofire.data_models.strategies.stepwise.conditions import ( # noqa: F401
AlwaysTrueCondition,
CombiCondition,
Expand All @@ -30,9 +30,6 @@
StepwiseStrategy,
)
from bofire.data_models.strategies.strategy import Strategy
from bofire.data_models.strategies.universal_constraint import (
UniversalConstraintSampler,
)

AbstractStrategy = Union[
Strategy,
Expand All @@ -49,8 +46,7 @@
QehviStrategy,
QnehviStrategy,
QparegoStrategy,
PolytopeSampler,
UniversalConstraintSampler,
SpaceFillingStrategy,
RandomStrategy,
DoEStrategy,
StepwiseStrategy,
Expand All @@ -70,8 +66,6 @@
MoboStrategy,
]

AnySampler = Union[PolytopeSampler, UniversalConstraintSampler]


AnyCondition = Union[NumberOfExperimentsCondition, CombiCondition, AlwaysTrueCondition]

Expand Down
57 changes: 0 additions & 57 deletions bofire/data_models/strategies/polytope.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
NonlinearEqualityConstraint,
NonlinearInequalityConstraint,
)
from bofire.data_models.features.api import ContinuousInput, ContinuousOutput, Feature
from bofire.data_models.features.api import (
CategoricalOutput,
ContinuousInput,
ContinuousOutput,
Feature,
)
from bofire.data_models.strategies.strategy import Strategy


class UniversalConstraintSampler(Strategy):
"""Sampler that generates samples by optimization in IPOPT.
class SpaceFillingStrategy(Strategy):
"""Stratey that generates space filling samples by optimization in IPOPT.
Attributes:
domain (Domain): Domain defining the constrained input space
Expand All @@ -24,7 +29,7 @@ class UniversalConstraintSampler(Strategy):
ipopt_options (dict, optional): Dictionary containing options for the IPOPT solver. Defaults to {"maxiter":200, "disp"=0}.
"""

type: Literal["UniversalConstraintSampler"] = "UniversalConstraintSampler"
type: Literal["SpaceFillingStrategy"] = "SpaceFillingStrategy"
sampling_fraction: Annotated[float, Field(gt=0, lt=1)] = 0.3
ipopt_options: dict = {"maxiter": 200, "disp": 0}

Expand All @@ -40,7 +45,4 @@ def is_constraint_implemented(cls, my_type: Type[Constraint]) -> bool:

@classmethod
def is_feature_implemented(cls, my_type: Type[Feature]) -> bool:
return my_type in [
ContinuousInput,
ContinuousOutput,
]
return my_type in [ContinuousInput, ContinuousOutput, CategoricalOutput]
12 changes: 10 additions & 2 deletions bofire/data_models/strategies/stepwise/stepwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
from bofire.data_models.constraints.api import Constraint
from bofire.data_models.features.api import Feature
from bofire.data_models.strategies.doe import DoEStrategy
from bofire.data_models.strategies.polytope import PolytopeSampler
from bofire.data_models.strategies.factorial import FactorialStrategy
from bofire.data_models.strategies.predictives.mobo import MoboStrategy
from bofire.data_models.strategies.predictives.qehvi import QehviStrategy
from bofire.data_models.strategies.predictives.qnehvi import QnehviStrategy
from bofire.data_models.strategies.predictives.qparego import QparegoStrategy
from bofire.data_models.strategies.predictives.sobo import (
AdditiveSoboStrategy,
CustomSoboStrategy,
MultiplicativeSoboStrategy,
SoboStrategy,
)
from bofire.data_models.strategies.random import RandomStrategy
from bofire.data_models.strategies.shortest_path import ShortestPathStrategy
from bofire.data_models.strategies.space_filling import SpaceFillingStrategy
from bofire.data_models.strategies.stepwise.conditions import (
AlwaysTrueCondition,
CombiCondition,
Expand All @@ -28,12 +32,16 @@
SoboStrategy,
AdditiveSoboStrategy,
MultiplicativeSoboStrategy,
CustomSoboStrategy,
QehviStrategy,
QnehviStrategy,
QparegoStrategy,
PolytopeSampler,
SpaceFillingStrategy,
RandomStrategy,
DoEStrategy,
FactorialStrategy,
MoboStrategy,
ShortestPathStrategy,
]

AnyCondition = Union[NumberOfExperimentsCondition, CombiCondition, AlwaysTrueCondition]
Expand Down
3 changes: 1 addition & 2 deletions bofire/strategies/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from bofire.strategies.doe_strategy import DoEStrategy
from bofire.strategies.mapper import map
from bofire.strategies.polytope import PolytopeSampler
from bofire.strategies.predictives.botorch import BotorchStrategy
from bofire.strategies.predictives.predictive import PredictiveStrategy
from bofire.strategies.predictives.qehvi import QehviStrategy
Expand All @@ -14,6 +13,6 @@
)
from bofire.strategies.random import RandomStrategy
from bofire.strategies.shortest_path import ShortestPathStrategy
from bofire.strategies.space_filling import SpaceFillingStrategy
from bofire.strategies.stepwise.stepwise import StepwiseStrategy
from bofire.strategies.strategy import Strategy
from bofire.strategies.universal_constraint import UniversalConstraintSampler
10 changes: 3 additions & 7 deletions bofire/strategies/doe/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
from bofire.data_models.domain.api import Domain
from bofire.data_models.enum import SamplingMethodEnum
from bofire.data_models.features.api import ContinuousInput, Input
from bofire.data_models.strategies.api import (
PolytopeSampler as PolytopeSamplerDataModel,
)
from bofire.data_models.strategies.api import RandomStrategy as RandomStrategyDataModel
from bofire.strategies.doe.objective import get_objective_class
from bofire.strategies.doe.utils import (
constraints_as_scipy_constraints,
Expand All @@ -28,7 +26,7 @@
nchoosek_constraints_as_bounds,
)
from bofire.strategies.enum import OptimalityCriterionEnum
from bofire.strategies.polytope import PolytopeSampler
from bofire.strategies.random import RandomStrategy


def find_local_max_ipopt_BaB(
Expand Down Expand Up @@ -454,9 +452,7 @@ def find_local_max_ipopt(
x0 = sampling.values.flatten()
else:
if len(domain.constraints.get(NonlinearConstraint)) == 0:
sampler = PolytopeSampler(
data_model=PolytopeSamplerDataModel(domain=domain)
)
sampler = RandomStrategy(data_model=RandomStrategyDataModel(domain=domain))
x0 = sampler.ask(n_experiments).to_numpy().flatten()
else:
warnings.warn(
Expand Down
8 changes: 3 additions & 5 deletions bofire/strategies/doe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
)
from bofire.data_models.domain.domain import Domain
from bofire.data_models.features.continuous import ContinuousInput
from bofire.data_models.strategies.api import (
PolytopeSampler as PolytopeSamplerDataModel,
)
from bofire.strategies.polytope import PolytopeSampler
from bofire.data_models.strategies.api import RandomStrategy as RandomStrategyDataModel
from bofire.strategies.random import RandomStrategy


def get_formula_from_string(
Expand Down Expand Up @@ -175,7 +173,7 @@ def n_zero_eigvals(
)
N = len(model_formula) + 3

sampler = PolytopeSampler(data_model=PolytopeSamplerDataModel(domain=domain))
sampler = RandomStrategy(data_model=RandomStrategyDataModel(domain=domain))
X = sampler.ask(N)
# compute eigenvalues of information matrix
A = model_formula.get_model_matrix(X)
Expand Down
6 changes: 2 additions & 4 deletions bofire/strategies/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import bofire.data_models.strategies.api as data_models
from bofire.strategies.doe_strategy import DoEStrategy
from bofire.strategies.factorial import FactorialStrategy
from bofire.strategies.polytope import PolytopeSampler
from bofire.strategies.predictives.botorch import BotorchStrategy
from bofire.strategies.predictives.mobo import MoboStrategy
from bofire.strategies.predictives.predictive import PredictiveStrategy
Expand All @@ -18,9 +17,9 @@
)
from bofire.strategies.random import RandomStrategy
from bofire.strategies.shortest_path import ShortestPathStrategy
from bofire.strategies.space_filling import SpaceFillingStrategy
from bofire.strategies.stepwise.stepwise import StepwiseStrategy
from bofire.strategies.strategy import Strategy
from bofire.strategies.universal_constraint import UniversalConstraintSampler

STRATEGY_MAP: Dict[Type[data_models.Strategy], Type[Strategy]] = {
data_models.RandomStrategy: RandomStrategy,
Expand All @@ -31,8 +30,7 @@
data_models.QehviStrategy: QehviStrategy,
data_models.QnehviStrategy: QnehviStrategy,
data_models.QparegoStrategy: QparegoStrategy,
data_models.PolytopeSampler: PolytopeSampler,
data_models.UniversalConstraintSampler: UniversalConstraintSampler,
data_models.SpaceFillingStrategy: SpaceFillingStrategy,
data_models.DoEStrategy: DoEStrategy,
data_models.StepwiseStrategy: StepwiseStrategy,
data_models.FactorialStrategy: FactorialStrategy,
Expand Down
Loading

0 comments on commit 7d6af93

Please sign in to comment.