Constraints
log_barrier(lhs, comparison, rhs, *, penalty_sign, sharpness=1.0, inf=None)
¶
Return a penalty based on how close the constraint is to being violated.
If the left-hand-side is equal to the right-hand-side, or if the constraint
is violated, the returned penalty will be infinite (+inf
or -inf
,
depending on penalty_sign
). Such inf
values can result in numerical
instabilities. To overcome such instabilities, you might want to set the
keyword argument inf
as a large-enough finite positive quantity M
, so
that very large (or infinite) penalties will be clipped down to M
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lhs
|
Union[float, Tensor]
|
The left-hand-side of the constraint. In the non-batched case, this is expected as a scalar. If it is given as an n-dimensional tensor where n is at least 1, this is considered as a batch of left-hand-side values. |
required |
comparison
|
str
|
The operator used for comparing the left-hand-side and the right-hand-side. Expected as a string. Acceptable values are: '<=', '>='. |
required |
rhs
|
Union[float, Tensor]
|
The right-hand-side of the constraint. In the non-batched case, this is expected as a scalar. If it is given as an n-dimensional tensor where n is at least 1, this is considered as a batch of right-hand-side values. |
required |
penalty_sign
|
str
|
Expected as string, either as '+' or '-', which
determines the sign of the penalty (i.e. determines if the penalty
will be positive or negative). One should consider the objective
sense of the fitness function at hand for deciding |
required |
sharpness
|
Union[float, Tensor]
|
The logarithmic penalty will be divided by this number. By default, this value is 1. A sharper log-penalization allows the constraint to get closer to its boundary, and then makes a more sudden jump towards infinity. |
1.0
|
inf
|
Optional[Union[float, Tensor]]
|
When concerned about the possible numerical instabilities caused
by infinite penalties, one can specify a finite large-enough
positive quantity |
None
|
Source code in evotorch/tools/constraints.py
penalty(lhs, comparison, rhs, *, penalty_sign, linear=None, step=None, exp=None, exp_inf=None)
¶
Return a penalty based on the amount of violation of the constraint.
Depending on the provided arguments, the penalty can be linear, or exponential, or based on step function, or a combination of these.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lhs
|
Union[float, Tensor]
|
The left-hand-side of the constraint. In the non-batched case, this is expected as a scalar. If it is given as an n-dimensional tensor where n is at least 1, this is considered as a batch of left-hand-side values. |
required |
comparison
|
str
|
The operator used for comparing the left-hand-side and the right-hand-side. Expected as a string. Acceptable values are: '<=', '==', '>='. |
required |
rhs
|
Union[float, Tensor]
|
The right-hand-side of the constraint. In the non-batched case, this is expected as a scalar. If it is given as an n-dimensional tensor where n is at least 1, this is considered as a batch of right-hand-side values. |
required |
penalty_sign
|
str
|
Expected as string, either as '+' or '-', which
determines the sign of the penalty (i.e. determines if the penalty
will be positive or negative). One should consider the objective
sense of the fitness function at hand for deciding |
required |
linear
|
Optional[Union[float, Tensor]]
|
Multiplier for the linear component of the penalization. If omitted (i.e. left as None), the value of this multiplier will be 0 (meaning that there will not be any linear penalization). In the non-batched case, this argument is expected as a scalar. If this is provided as a tensor 1 or more dimensions, those dimensions will be considered as batch dimensions. |
None
|
step
|
Optional[Union[float, Tensor]]
|
The constant amount that will be added onto the penalty if there is a violation. If omitted (i.e. left as None), this value is 0. In the non-batched case, this argument is expected as a scalar. If this is provided as a tensor 1 or more dimensions, those dimensions will be considered as batch dimensions. |
None
|
exp
|
Optional[Union[float, Tensor]]
|
A constant |
None
|
exp_inf
|
Optional[Union[float, Tensor]]
|
Upper bound for exponential penalty values. If exponential
penalty is enabled but |
None
|
Source code in evotorch/tools/constraints.py
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 277 278 279 280 281 |
|
violation(lhs, comparison, rhs)
¶
Get the amount of constraint violation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lhs
|
Union[float, Tensor]
|
The left-hand-side of the constraint. In the non-batched case, this is expected as a scalar. If it is given as an n-dimensional tensor where n is at least 1, this is considered as a batch of left-hand-side values. |
required |
comparison
|
str
|
The operator used for comparing the left-hand-side and the right-hand-side. Expected as a string. Acceptable values are: '<=', '==', '>='. |
required |
rhs
|
Union[float, Tensor]
|
The right-hand-side of the constraint. In the non-batched case, this is expected as a scalar. If it is given as an n-dimensional tensor where n is at least 1, this is considered as a batch of right-hand-side values. |
required |