Skip to content

Commit b91f4e3

Browse files
authoredMar 10, 2025
User guide for cardinality constraints (#496)
This PR adds explanations for the `ContinuousCardinalityConstraint` and the `DiscreteCardinalityConstraint` to the user guide.
2 parents e38b846 + 2a36dff commit b91f4e3

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
- Utilities `activate_parameter` and `is_cardinality_fulfilled` for enforcing and
3333
validating cardinality constraints
3434
- Utility `is_inactive` for determining if parameters are inactive
35+
- Cardinality constraints sections to the user guide
3536

3637
### Changed
3738
- Acquisition function indicator `is_mc` has been removed in favor of new indicators

‎docs/userguide/constraints.md

+54
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,46 @@ ContinuousLinearConstraint(
7979
A more detailed example can be found
8080
[here](../../examples/Constraints_Continuous/linear_constraints).
8181

82+
### ContinuousCardinalityConstraint
83+
The {class}`~baybe.constraints.continuous.ContinuousCardinalityConstraint` gives you a
84+
tool to control the number of active factors (i.e. parameters that take a non-zero
85+
value) in your designs. This comes handy, for example, when designing mixtures with a
86+
limited number of components.
87+
88+
To create a constraint of this kind, simply specify the set of parameters on which the
89+
constraint is to be imposed, together with the corresponding upper and lower cardinality
90+
limits. For instance, the following constraint would ensure that there is always a
91+
minimum of one and a maximum of two components in each parameter configuration:
92+
93+
```python
94+
from baybe.constraints import ContinuousCardinalityConstraint
95+
96+
ContinuousCardinalityConstraint(
97+
parameters=["x_1", "x_2", "x_3"],
98+
min_cardinality=1, # defaults to 0
99+
max_cardinality=2, # defaults to the number of affected parameters (here: 3)
100+
relative_threshold=0.001, # optional, defines the range of values considered active
101+
)
102+
```
103+
104+
```{admonition} Computational Challenges
105+
:class: warning
106+
107+
Note that, compared to optimization with [discrete cardinality
108+
constraints](#DiscreteCardinalityConstraint), finding optimal cardinality-constrained
109+
solutions in continuous spaces is significantly more challenging due to the
110+
fractured nature of the resulting space. For larger parameter sets or complex constraint
111+
settings, searching an optimal parameter configuration can quickly become infeasible,
112+
creating the need for approximation schemes:
113+
114+
* The
115+
{paramref}`BotorchRecommender.max_n_subspaces <baybe.recommenders.pure.bayesian.botorch.BotorchRecommender.max_n_subspaces>`
116+
attribute can be used to limit the number of subspaces considered during optimization.
117+
* When the ranges of cardinality-constrained parameters cover both positive and negative
118+
values, minimal cardinality requirements cannot always be guaranteed, potentially
119+
resulting in a {class}`~baybe.exceptions.MinimumCardinalityViolatedWarning`.
120+
```
121+
82122
## Conditions
83123
Conditions are elements used within discrete constraints.
84124
While discrete constraints can operate on one or multiple parameters, a condition
@@ -369,6 +409,20 @@ DiscretePermutationInvarianceConstraint(
369409
The usage of `DiscretePermutationInvarianceConstraint` is also part of the
370410
[example on slot-based mixtures](../../examples/Mixtures/slot_based).
371411

412+
### DiscreteCardinalityConstraint
413+
Like its [continuous cousin](#ContinuousCardinalityConstraint), the
414+
{class}`~baybe.constraints.discrete.DiscreteCardinalityConstraint` lets you control the
415+
number of active parameters in your design. The construction works analogously:
416+
```python
417+
from baybe.constraints import DiscreteCardinalityConstraint
418+
419+
DiscreteCardinalityConstraint(
420+
parameters=["Fraction_1", "Fraction_2", "Fraction_3"],
421+
min_cardinality=1, # defaults to 0
422+
max_cardinality=2, # defaults to the number of affected parameters (here: 3)
423+
)
424+
```
425+
372426
### DiscreteCustomConstraint
373427
With a [`DiscreteCustomConstraint`](baybe.constraints.discrete.DiscreteCustomConstraint)
374428
constraint, you can specify a completely custom filter:

0 commit comments

Comments
 (0)