Skip to content

feat: add Constraint dataclass and feasibility filtering#99

Merged
jc-macdonald merged 1 commit intomainfrom
feat/constraint-handling
Apr 17, 2026
Merged

feat: add Constraint dataclass and feasibility filtering#99
jc-macdonald merged 1 commit intomainfrom
feat/constraint-handling

Conversation

@jc-macdonald
Copy link
Copy Markdown
Contributor

Summary

Adds constraint handling for feasibility filtering of designs (#74).

Changes

  • Constraint dataclass in protocols.py: frozen dataclass with name, observable, op, threshold. Validates operator on construction (>=, <=, >, <, ==, !=). Includes a .check(value) method for scalar tests.
  • ResultsTable.feasible(constraints): returns a boolean NDArray mask — True for rows where all constraints are satisfied. Supports both observable and annotation columns.
  • feasibility_filter(constraints) in study.py: creates a Phase.filter_fn-compatible callable that keeps only feasible designs, enabling constraint-driven phase transitions.
  • Exported Constraint and feasibility_filter from the public API.

Example usage

from trade_study import Constraint, Phase, feasibility_filter

constraints = [
    Constraint("min_conversion", "conversion", ">=", 0.5),
    Constraint("budget", "cost", "<=", 100.0),
]

Phase(
    name="screen",
    grid=grid,
    filter_fn=feasibility_filter(constraints),
)

Tests

  • 17 new tests covering Constraint creation, operator validation, all 6 comparison operators, ResultsTable.feasible with single/multiple/annotation constraints, and feasibility_filter standalone and inside a Study pipeline
  • 258 total tests passing, 99.57% coverage

Closes #74

- Constraint(name, observable, op, threshold) with operator validation
  and a .check() method for scalar values
- ResultsTable.feasible(constraints) returns a boolean mask over rows
- feasibility_filter(constraints) creates a Phase.filter_fn-compatible
  callable that keeps only feasible designs
- Exported Constraint and feasibility_filter from the public API
- 17 new tests (Constraint creation, operator validation, all 6 ops,
  ResultsTable.feasible with single/multiple/annotation constraints,
  feasibility_filter standalone and inside a Study pipeline)
@jc-macdonald jc-macdonald merged commit e04384e into main Apr 17, 2026
4 checks passed
@jc-macdonald jc-macdonald deleted the feat/constraint-handling branch April 17, 2026 14:41
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.

Constraint handling: feasibility filters for designs

1 participant