Skip to content

Rework Exclude Into Filtering Constraint, Add exclude Flag #873

Description

@Scienfitz

Motivation

DiscreteExcludeConstraint is really a general pruning constraint hardwired to removal. We introduce a new DiscreteFilteringConstraint and an exclude flag that standardizes selection semantics across all pruning constraints, i.e. every discrete constraint except DiscreteBatchConstraint (which does not prune the search space). Pruning constraints keep the entries their specification describes by default (inclusion-by-default); exclude=True inverts the selection to keep the complement. DiscreteFilteringConstraint(..., exclude=True) reproduces today's DiscreteExcludeConstraint.

Todo

  • Add abstract DiscretePruningConstraint base in the derivation chain, owning the pruning machinery (get_invalid/get_valid, get_invalid_polars, has_polars_implementation) and the kw-only exclude flag; move these off DiscreteConstraint.
  • Let subclasses express positive "valid rows" logic via a _get_valid hook (and _get_valid_polars); the base derives the removal set and applies the exclude inversion for both pandas and Polars.
  • Implement _can_evaluate per subclass reading self.exclude (partial-evaluation soundness depends on the constraint and, for the condition-based filter, on the combiner; verified empirically). No global rule.
  • Add DiscreteFilteringConstraint (replaces DiscreteExcludeConstraint, inclusion-by-default).
  • Remove the dead _get_invalid placeholder from DiscreteBatchConstraint.
  • Rename DISCRETE_CONSTRAINTS_FILTERING_ORDERDISCRETE_CONSTRAINTS_PRUNING_ORDER, remove DiscreteBatchConstraint from it, and switch filtering selection to isinstance(c, DiscretePruningConstraint).
  • Deprecation (per AGENTS.md §10): keep DiscreteExcludeConstraint as a deprecated wrapper mapping to DiscreteFilteringConstraint(..., exclude=True); add cattrs (de)serialization redirect; cover in tests/test_deprecations.py.
  • Add a focused naive == incremental test parametrized over exclude for the early-filtering constraints.
  • Update fixtures, docs, and examples (rename exclusion_constraints example → filtering_constraints).

Code References

  • DiscreteExcludeConstraint: baybe/constraints/discrete.py:39-80
  • Shared filtering entry point: baybe/constraints/base.py:103-188
  • Polars driver: baybe/searchspace/utils.py:277-294
  • Filtering order tuple / selection: discrete.py:572-585, searchspace/utils.py:195,269

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions