Skip to content

Add validity check functions for ContinuousConstraints - #909

Closed
AVHopp wants to merge 3 commits into
mainfrom
feat/check_continuous_constraints
Closed

Add validity check functions for ContinuousConstraints#909
AVHopp wants to merge 3 commits into
mainfrom
feat/check_continuous_constraints

Conversation

@AVHopp

@AVHopp AVHopp commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

This PR adds get_valid and get_invalid functions for ContinuousConstraint objects.

Previously, we never needed those checks and only handed the constraints directly to Botorch. Now, with the current implementation efforts of the LLMRecommender, we need to be able to validate those kind of constraints on our own.

Design note: To keep the same interface, these functions also simply accedpt dataframes. As a consequence, they cannot directly be used for cardinality constraints. Consequently, the corresponding function raises an error and points the user to the corresponding helper.

For linear constraints, we can calculate and evaluate
the constraints. For cardinality constraints, we raise
an error and point the user to the general utility function.
This is necessary as more context is needed for cardinality
constraints and we'd need to change the interface.
Copilot AI lite review requested due to automatic review settings September 1, 2026 09:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a row-level validity API for continuous constraints so BayBE can validate continuous constraints directly against candidate configuration dataframes (needed for the ongoing LLMRecommender work), instead of relying solely on handing constraints off to BoTorch.

Changes:

  • Add ContinuousConstraint.get_invalid(df) (abstract) and ContinuousConstraint.get_valid(df) (derived complement) to provide a common validation interface.
  • Implement ContinuousLinearConstraint.get_invalid(df) for both intra- and interpoint semantics.
  • Add tests covering get_invalid/get_valid behavior and document the new API in the changelog; cardinality constraints explicitly raise with a pointer to the existing helper.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
baybe/constraints/base.py Adds the get_invalid/get_valid API to the ContinuousConstraint base class.
baybe/constraints/continuous.py Implements row-level invalid-index extraction for ContinuousLinearConstraint; adds an explicit “not supported” implementation for cardinality constraints.
tests/constraints/test_constraints_continuous.py Adds unit tests for get_invalid/get_valid on continuous linear constraints and the cardinality “raises” behavior.
CHANGELOG.md Documents the addition of get_invalid/get_valid for continuous constraint validation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +365 to +366
Returns:
None. Always raises NotImplementedError.
Comment on lines +219 to +235
if missing := self._required_parameters - set(df.columns):
raise ValueError(
f"'{self.__class__.__name__}' requires columns {missing} "
f"which are missing from the dataframe."
)
series = pd.Series(
sum(
df[p].to_numpy() * c for p, c in zip(self.parameters, self.coefficients)
),
index=df.index,
)
condition = ThresholdCondition(threshold=self.rhs, operator=self.operator)
if self.is_interpoint:
# Aggregate across all batch rows (mirrors BoTorch interpoint semantics).
satisfied = bool(condition.evaluate(pd.Series([float(series.sum())]))[0])
return pd.Index([]) if satisfied else df.index
return df.index[~condition.evaluate(series)]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me ask @AdrianSosic and @Scienfitz what they'd prefer here: New logic or re-using what we have?

@Scienfitz Scienfitz added this to the 0.16.0 milestone Sep 1, 2026
@AVHopp AVHopp closed this Sep 2, 2026
@AVHopp
AVHopp deleted the feat/check_continuous_constraints branch September 2, 2026 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants