Constraints
categorical
CategoricalExcludeConstraint
Bases: Constraint
Class for modelling exclusion constraints.
It evaluates conditions on two features and combines them using logical operators. If the logical combination evaluates to true, the constraint is not fulfilled. So far, this kind of constraint is only supported by the RandomStrategy.
Attributes:
Name | Type | Description |
---|---|---|
features |
FeatureKeys
|
List of feature keys to apply the conditions on. |
conditions |
Annotated[List[Union[ThresholdCondition, SelectionCondition]], Field(min_length=2, max_length=2)]
|
List of conditions to evaluate. |
logical_op |
Literal['AND', 'OR', 'XOR']
|
Logical operator to combine the conditions. Can be "AND", "OR", or "XOR". Default is "AND". |
Source code in bofire/data_models/constraints/categorical.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
__call__(experiments)
Numerically evaluates the constraint.
Returns the distance to the constraint fulfillment. Here 1 for not fulfilled and 0 for fulfilled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments
|
DataFrame
|
Dataframe to evaluate the constraint on. |
required |
Returns:
Type | Description |
---|---|
Series
|
pd.Series: Distance to reach constraint fulfillment. |
Source code in bofire/data_models/constraints/categorical.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
is_fulfilled(experiments, tol=1e-06)
Checks if the constraint is fulfilled for the given experiments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tol
|
Optional[float]
|
Tolerance for checking. Not used here. Defaults to 1e-6. |
1e-06
|
Returns:
Type | Description |
---|---|
Series
|
Series indicating whether the constraint is fulfilled for each experiment. |
Source code in bofire/data_models/constraints/categorical.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
validate_inputs(inputs)
Validates that the features stored in Inputs are compatible with the constraint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Inputs
|
Inputs to validate. |
required |
Source code in bofire/data_models/constraints/categorical.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
Condition
Bases: BaseModel
Base class for all conditions.
Conditions evaluate an expression regarding a single feature. Conditions are part of the CategoricalExcludeConstraint.
Source code in bofire/data_models/constraints/categorical.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
__call__(values)
abstractmethod
Evaluate the condition on a given data series.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Series
|
A series containing feature values. |
required |
Returns:
Type | Description |
---|---|
Series
|
A Boolean series indicating which elements satisfy the condition. |
Source code in bofire/data_models/constraints/categorical.py
37 38 39 40 41 42 43 44 45 46 |
|
SelectionCondition
Bases: Condition
Class for modelling selection conditions.
It is checked if the feature value is in the selection of values. If this is the case, the condition is fulfilled. It can be only applied to CategoricalInput and DiscreteInput features.
Attributes:
Name | Type | Description |
---|---|---|
selection |
List[Union[str, float, int]]
|
In case of CategoricalInput, the selection of categories to be included. In case of DiscreteInput, the selection of values to be included. |
Source code in bofire/data_models/constraints/categorical.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
ThresholdCondition
Bases: Condition
Class for modelling threshold conditions.
It can only be applied to ContinuousInput and DiscreteInput features. It is checked if the feature value is above or below a certain threshold depending on the operator. If the expression evaluated to true, the condition is fulfilled.
Attributes:
Name | Type | Description |
---|---|---|
threshold |
float
|
Threshold value. |
operator |
Literal['<', '<=', '>', '>=']
|
Operator to use for comparison. Can be one of "<", "<=", ">", ">=". |
Source code in bofire/data_models/constraints/categorical.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
constraint
Constraint
Bases: BaseModel
Abstract base class to define constraints on the optimization space.
Source code in bofire/data_models/constraints/constraint.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
__call__(experiments)
abstractmethod
Numerically evaluates the constraint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments
|
DataFrame
|
Dataframe to evaluate the constraint on. |
required |
Returns:
Type | Description |
---|---|
Series
|
pd.Series: Distance to reach constraint fulfillment. |
Source code in bofire/data_models/constraints/constraint.py
36 37 38 39 40 41 42 43 44 45 46 |
|
is_fulfilled(experiments, tol=1e-06)
abstractmethod
Abstract method to check if a constraint is fulfilled for all the rows of the provided dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments
|
DataFrame
|
Dataframe to check constraint fulfillment. |
required |
tol
|
float
|
tolerance parameter. A constraint is considered as not fulfilled if the violation is larger than tol. Defaults to 0. |
1e-06
|
Returns:
Name | Type | Description |
---|---|---|
bool |
Series
|
True if fulfilled else False |
Source code in bofire/data_models/constraints/constraint.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
jacobian(experiments)
abstractmethod
Numerically evaluates the jacobian of the constraint Args: experiments (pd.DataFrame): Dataframe to evaluate the constraint on.
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: the i-th row contains the jacobian evaluated at the i-th experiment |
Source code in bofire/data_models/constraints/constraint.py
48 49 50 51 52 53 54 55 56 57 |
|
validate_inputs(inputs)
abstractmethod
Validates that the features stored in Inputs are compatible with the constraint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Inputs
|
Inputs to validate. |
required |
Source code in bofire/data_models/constraints/constraint.py
59 60 61 62 63 64 65 66 |
|
ConstraintError
Bases: Exception
Base Error for Constraints
Source code in bofire/data_models/constraints/constraint.py
94 95 |
|
ConstraintNotFulfilledError
Bases: ConstraintError
Raised when an constraint is not fulfilled.
Source code in bofire/data_models/constraints/constraint.py
98 99 |
|
IntrapointConstraint
Bases: Constraint
An intrapoint constraint describes required relationships within a candidate when asking a strategy to return one or more candidates.
Source code in bofire/data_models/constraints/constraint.py
69 70 71 72 73 74 |
|
interpoint
InterpointConstraint
Bases: Constraint
An interpoint constraint describes required relationships between individual candidates when asking a strategy for returning more than one candidate.
Source code in bofire/data_models/constraints/interpoint.py
13 14 15 16 17 18 |
|
InterpointEqualityConstraint
Bases: InterpointConstraint
Constraint that forces that values of a certain feature of a set/batch of candidates should have the same value. Currently this is only implemented for ContinuousInput features.
Attributes:
Name | Type | Description |
---|---|---|
feature(str) |
The constrained feature. |
|
multiplicity(int) |
The multiplicity of the constraint, stating how many values of the feature in the batch should have always the same value. |
Source code in bofire/data_models/constraints/interpoint.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
feature
property
Feature to be constrained.
__call__(experiments)
Numerically evaluates the constraint. Returns the distance to the constraint fulfillment for each batch of size batch_size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments
|
DataFrame
|
Dataframe to evaluate the constraint on. |
required |
Returns:
Type | Description |
---|---|
Series
|
pd.Series: Distance to reach constraint fulfillment. |
Source code in bofire/data_models/constraints/interpoint.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
linear
LinearConstraint
Bases: IntrapointConstraint
Abstract base class for linear equality and inequality constraints.
Attributes:
Name | Type | Description |
---|---|---|
features |
list
|
list of feature keys (str) on which the constraint works on. |
coefficients |
list
|
list of coefficients (float) of the constraint. |
rhs |
float
|
Right-hand side of the constraint |
Source code in bofire/data_models/constraints/linear.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
validate_list_lengths()
Validate that length of the feature and coefficient lists have the same length.
Source code in bofire/data_models/constraints/linear.py
31 32 33 34 35 36 37 38 |
|
LinearEqualityConstraint
Bases: LinearConstraint
, EqualityConstraint
Linear equality constraint of the form coefficients * x = rhs
.
Attributes:
Name | Type | Description |
---|---|---|
features |
list
|
list of feature keys (str) on which the constraint works on. |
coefficients |
list
|
list of coefficients (float) of the constraint. |
rhs |
float
|
Right-hand side of the constraint |
Source code in bofire/data_models/constraints/linear.py
69 70 71 72 73 74 75 76 77 78 79 |
|
LinearInequalityConstraint
Bases: LinearConstraint
, InequalityConstraint
Linear inequality constraint of the form coefficients * x <= rhs
.
To instantiate a constraint of the form coefficients * x >= rhs
multiply coefficients and rhs by -1, or
use the classmethod from_greater_equal
.
Attributes:
Name | Type | Description |
---|---|---|
features |
list
|
list of feature keys (str) on which the constraint works on. |
coefficients |
list
|
list of coefficients (float) of the constraint. |
rhs |
float
|
Right-hand side of the constraint |
Source code in bofire/data_models/constraints/linear.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
as_greater_equal()
Return attributes in the greater equal convention
Returns:
Type | Description |
---|---|
Tuple[List[str], List[float], float]
|
Tuple[List[str], List[float], float]: features, coefficients, rhs |
Source code in bofire/data_models/constraints/linear.py
106 107 108 109 110 111 112 113 |
|
as_smaller_equal()
Return attributes in the smaller equal convention
Returns:
Type | Description |
---|---|
Tuple[List[str], List[float], float]
|
Tuple[List[str], List[float], float]: features, coefficients, rhs |
Source code in bofire/data_models/constraints/linear.py
97 98 99 100 101 102 103 104 |
|
from_greater_equal(features, coefficients, rhs)
classmethod
Class method to construct linear inequality constraint of the form coefficients * x >= rhs
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features
|
List[str]
|
List of feature keys. |
required |
coefficients
|
List[float]
|
List of coefficients. |
required |
rhs
|
float
|
Right-hand side of the constraint. |
required |
Source code in bofire/data_models/constraints/linear.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
from_smaller_equal(features, coefficients, rhs)
classmethod
Class method to construct linear inequality constraint of the form coefficients * x <= rhs
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features
|
List[str]
|
List of feature keys. |
required |
coefficients
|
List[float]
|
List of coefficients. |
required |
rhs
|
float
|
Right-hand side of the constraint. |
required |
Source code in bofire/data_models/constraints/linear.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
nchoosek
NChooseKConstraint
Bases: IntrapointConstraint
NChooseK constraint that defines how many ingredients are allowed in a formulation.
Attributes:
Name | Type | Description |
---|---|---|
features |
List[str]
|
List of feature keys to which the constraint applies. |
min_count |
int
|
Minimal number of non-zero/active feature values. |
max_count |
int
|
Maximum number of non-zero/active feature values. |
none_also_valid |
bool
|
In case that min_count > 0, this flag decides if zero active features are also allowed. |
Source code in bofire/data_models/constraints/nchoosek.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
__call__(experiments)
Smooth relaxation of NChooseK constraint by countig the number of zeros in a candidate by a sum of narrow gaussians centered at zero.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments
|
DataFrame
|
Data to evaluate the constraint on. |
required |
Returns:
Type | Description |
---|---|
Series
|
pd.Series containing the constraint violation for each experiment (row in experiments argument). |
Source code in bofire/data_models/constraints/nchoosek.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
is_fulfilled(experiments, tol=1e-06)
Check if the concurrency constraint is fulfilled for all the rows of the provided dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments
|
DataFrame
|
Dataframe to evaluate constraint on. |
required |
tol
|
(float, optional)
|
tolerance parameter. A constraint is considered as not fulfilled if the violation is larger than tol. Defaults to 1e-6. |
1e-06
|
Returns:
Name | Type | Description |
---|---|---|
bool |
Series
|
True if fulfilled else False. |
Source code in bofire/data_models/constraints/nchoosek.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
validate_counts()
Validates if the minimum and maximum of allowed features are smaller than the overall number of features.
Source code in bofire/data_models/constraints/nchoosek.py
49 50 51 52 53 54 55 56 57 58 59 |
|
nonlinear
NonlinearConstraint
Bases: IntrapointConstraint
Base class for nonlinear equality and inequality constraints.
Attributes:
Name | Type | Description |
---|---|---|
expression |
str
|
Mathematical expression that can be evaluated by |
jacobian_expression |
str
|
Mathematical expression that that can be evaluated by |
features |
list
|
list of feature keys (str) on which the constraint works on. |
Source code in bofire/data_models/constraints/nonlinear.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
hessian(experiments)
Computes a dict of dataframes where the key dimension is the index of the experiments dataframe and the value is the hessian matrix of the constraint evaluated at the corresponding experiment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments
|
DataFrame
|
Dataframe to evaluate the constraint Hessian on. |
required |
Returns:
Type | Description |
---|---|
Dict[Union[str, int], DataFrame]
|
Dict[pd.DataFrame]: Dictionary of dataframes where the key is the index of the experiments dataframe |
Dict[Union[str, int], DataFrame]
|
and the value is the Hessian matrix of the constraint evaluated at the corresponding experiment. |
Source code in bofire/data_models/constraints/nonlinear.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
NonlinearEqualityConstraint
Bases: NonlinearConstraint
, EqualityConstraint
Nonlinear equality constraint of the form 'expression == 0'.
Attributes:
Name | Type | Description |
---|---|---|
expression |
Union[str, Callable]
|
Mathematical expression that can be evaluated by |
Source code in bofire/data_models/constraints/nonlinear.py
279 280 281 282 283 284 285 286 287 |
|
NonlinearInequalityConstraint
Bases: NonlinearConstraint
, InequalityConstraint
Nonlinear inequality constraint of the form 'expression <= 0'.
Attributes:
Name | Type | Description |
---|---|---|
expression |
Union[str, Callable]
|
Mathematical expression that can be evaluated by |
Source code in bofire/data_models/constraints/nonlinear.py
290 291 292 293 294 295 296 297 298 |
|
product
ProductConstraint
Bases: IntrapointConstraint
Represents a product constraint of the form sign * x1**e1 * x2**e2 * ... * xn**en
.
Attributes:
Name | Type | Description |
---|---|---|
type |
str
|
The type of the constraint. |
features |
FeatureKeys
|
The keys of the features used in the constraint. |
exponents |
List[float]
|
The exponents corresponding to each feature. |
rhs |
float
|
The right-hand side value of the constraint. |
sign |
Literal[1, -1]
|
The sign of the left hand side of the constraint. Defaults to 1. |
Source code in bofire/data_models/constraints/product.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
__call__(experiments)
Evaluates the constraint on the given experiments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments
|
DataFrame
|
The experiments to evaluate the constraint on. |
required |
Returns:
Type | Description |
---|---|
Series
|
pd.Series: The distance to reach constraint fulfillment. |
Source code in bofire/data_models/constraints/product.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
validate_list_lengths()
Validates that the number of features and exponents provided are the same.
Raises:
Type | Description |
---|---|
ValueError
|
If the number of features and exponents are not equal. |
Returns:
Name | Type | Description |
---|---|---|
ProductConstraint |
ProductConstraint
|
The current instance of the class. |
Source code in bofire/data_models/constraints/product.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
ProductEqualityConstraint
Bases: ProductConstraint
, EqualityConstraint
Represents a product constraint of the form sign * x1**e1 * x2**e2 * ... * xn**en == rhs
.
Attributes:
Name | Type | Description |
---|---|---|
type |
str
|
The type of the constraint. |
features |
FeatureKeys
|
The keys of the features used in the constraint. |
exponents |
List[float]
|
The exponents corresponding to each feature. |
rhs |
float
|
The right-hand side value of the constraint. |
sign |
Literal[1, -1]
|
The sign of the left hand side of the constraint. Defaults to 1. |
Source code in bofire/data_models/constraints/product.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
ProductInequalityConstraint
Bases: ProductConstraint
, InequalityConstraint
Represents a product constraint of the form sign * x1**e1 * x2**e2 * ... * xn**en <= rhs
.
Attributes:
Name | Type | Description |
---|---|---|
type |
str
|
The type of the constraint. |
features |
FeatureKeys
|
The keys of the features used in the constraint. |
exponents |
List[float]
|
The exponents corresponding to each feature. |
rhs |
float
|
The right-hand side value of the constraint. |
sign |
Literal[1, -1]
|
The sign of the left hand side of the constraint. Defaults to 1. |
Source code in bofire/data_models/constraints/product.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|