Skip to content

Commit

Permalink
Move factory code up
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianSosic committed Aug 15, 2024
1 parent c14db95 commit cf7f6b0
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions baybe/searchspace/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,61 @@ def _drop_parameters(self, parameter_names: Collection[str]) -> SubspaceContinuo
],
)

def _ensure_nonzero_parameters(
self,
inactive_parameters: Collection[str],
zero_threshold: float = ZERO_THRESHOLD,
) -> SubspaceContinuous:
"""Create a new subspace with following several actions.
* Ensure active parameter != 0.0.
* Remove cardinality constraint.
Args:
inactive_parameters: A list of inactive parameters.
zero_threshold: Threshold for checking whether a value is zero.
Returns:
A new subspace object.
"""
# Active parameters: parameters involved in cardinality constraints
active_params_sample = set(
self.param_names_in_cardinality_constraint
).difference(set(inactive_parameters))

constraints_lin_ineq = list(self.constraints_lin_ineq)
for active_param in active_params_sample:
index = self.param_names.index(active_param)

# TODO: Ensure x != 0 when x in [..., 0, ...] is not done. Do we need it?
# TODO: To ensure the minimum cardinality constraints, shall we keep the x
# != 0 operations or shall we instead skip the invalid results at the end
# Ensure x != 0 when bounds = [..., 0]. This is needed, otherwise
# the minimum cardinality constraint is easily violated
if self.parameters[index].bounds.upper == 0:
constraints_lin_ineq.append(
ContinuousLinearInequalityConstraint(
parameters=[active_param],
coefficients=[-1.0],
rhs=min(zero_threshold, -self.parameters[index].bounds.lower),
)
)
# Ensure x != 0 when bounds = [0, ...]
elif self.parameters[index].bounds.lower == 0:
constraints_lin_ineq.append(
ContinuousLinearInequalityConstraint(
parameters=[active_param],
coefficients=[1.0],
rhs=min(zero_threshold, self.parameters[index].bounds.upper),
),
)

return SubspaceContinuous(
parameters=tuple(self.parameters),
constraints_lin_eq=self.constraints_lin_eq,
constraints_lin_ineq=tuple(constraints_lin_ineq),
)

def transform(
self,
df: pd.DataFrame | None = None,
Expand Down Expand Up @@ -485,61 +540,6 @@ def _sample_inactive_parameters(self, batch_size: int = 1) -> list[set[str]]:
]
return [set(chain(*x)) for x in zip(*inactives_per_constraint)]

def _ensure_nonzero_parameters(
self,
inactive_parameters: Collection[str],
zero_threshold: float = ZERO_THRESHOLD,
) -> SubspaceContinuous:
"""Create a new subspace with following several actions.
* Ensure active parameter != 0.0.
* Remove cardinality constraint.
Args:
inactive_parameters: A list of inactive parameters.
zero_threshold: Threshold for checking whether a value is zero.
Returns:
A new subspace object.
"""
# Active parameters: parameters involved in cardinality constraints
active_params_sample = set(
self.param_names_in_cardinality_constraint
).difference(set(inactive_parameters))

constraints_lin_ineq = list(self.constraints_lin_ineq)
for active_param in active_params_sample:
index = self.param_names.index(active_param)

# TODO: Ensure x != 0 when x in [..., 0, ...] is not done. Do we need it?
# TODO: To ensure the minimum cardinality constraints, shall we keep the x
# != 0 operations or shall we instead skip the invalid results at the end
# Ensure x != 0 when bounds = [..., 0]. This is needed, otherwise
# the minimum cardinality constraint is easily violated
if self.parameters[index].bounds.upper == 0:
constraints_lin_ineq.append(
ContinuousLinearInequalityConstraint(
parameters=[active_param],
coefficients=[-1.0],
rhs=min(zero_threshold, -self.parameters[index].bounds.lower),
)
)
# Ensure x != 0 when bounds = [0, ...]
elif self.parameters[index].bounds.lower == 0:
constraints_lin_ineq.append(
ContinuousLinearInequalityConstraint(
parameters=[active_param],
coefficients=[1.0],
rhs=min(zero_threshold, self.parameters[index].bounds.upper),
),
)

return SubspaceContinuous(
parameters=tuple(self.parameters),
constraints_lin_eq=self.constraints_lin_eq,
constraints_lin_ineq=tuple(constraints_lin_ineq),
)

def samples_full_factorial(self, n_points: int = 1) -> pd.DataFrame:
"""Deprecated!""" # noqa: D401
warnings.warn(
Expand Down

0 comments on commit cf7f6b0

Please sign in to comment.