Constraints
Constraint
Base class to define constraints on the input space, g(x) == 0 or g(x) <= 0.
Source code in opti/constraint.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
__call__(data)
Numerically evaluate the constraint g(x).
Source code in opti/constraint.py
11 12 13 |
|
jacobian(data)
Numerically evaluate the jacobian of the constraint J_g(x)
Source code in opti/constraint.py
15 16 17 |
|
satisfied(data)
Check if a constraint is satisfied, i.e. g(x) == 0 for equalities and g(x) <= for inequalities.
Source code in opti/constraint.py
19 20 21 |
|
Constraints
List of input constraints
Source code in opti/constraint.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
|
__call__(data)
Numerically evaluate all constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
Data to evaluate the constraints on. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
Constraint evaluation g(x) for each of the constraints. |
Source code in opti/constraint.py
348 349 350 351 352 353 354 355 356 357 |
|
get(types)
Get all constraints of the given type(s).
Source code in opti/constraint.py
386 387 388 |
|
jacobian(data)
Numerically evaluate all constraint gradients.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
Data to evaluate the constraint gradients on. |
required |
Returns:
Type | Description |
---|---|
List
|
Jacobian evaluation J_g(x) for each of the constraints as a list of dataframes. |
Source code in opti/constraint.py
359 360 361 362 363 364 365 366 367 368 |
|
satisfied(data)
Check if all constraints are satisfied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
Data to evaluate the constraints on. |
required |
Returns:
Type | Description |
---|---|
Series
|
Series of booleans indicating if all constraints are satisfied. |
Source code in opti/constraint.py
370 371 372 373 374 375 376 377 378 379 380 381 |
|
LinearEquality
Bases: Constraint
Source code in opti/constraint.py
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 |
|
__init__(names, lhs=1, rhs=0)
Linear / affine inequality of the form 'lhs * x == rhs'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names
|
List[str]
|
Parameter names that the constraint works on. |
required |
lhs
|
Union[float, List[float], ndarray]
|
Left-hand side / coefficients of the constraint. |
1
|
rhs
|
float
|
Right-hand side of the constraint. |
0
|
Examples:
A mixture constraint where A, B and C need to add up to 100 can be defined as
LinearEquality(["A", "B", "C"], rhs=100)
LinearEquality(["A", "B", "C"], lhs=[10, 2, 5], rhs=100)
Source code in opti/constraint.py
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 |
|
LinearInequality
Bases: Constraint
Source code in opti/constraint.py
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 |
|
__init__(names, lhs=1, rhs=0)
Linear / affine inequality of the form 'lhs * x <= rhs'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names
|
List[str]
|
Parameter names that the constraint works on. |
required |
lhs
|
Union[float, List[float], ndarray]
|
Left-hand side / coefficients of the constraint. |
1
|
rhs
|
float
|
Right-hand side of the constraint. |
0
|
Examples:
A mixture constraint where the values of A, B and C may not exceed 100 can be defined as
LinearInequality(["A", "B", "C"], rhs=100)
LinearInequality(["A", "B", "C"], lhs=[10, 2, 5], rhs=100)
lhs
and rhs
need to be multiplied by -1.
LinearInequality(["A", "B", "C"], lhs=-1, rhs=-100)
LinearInequality(["A", "B", "C"], lhs=[-10, -2, -5], rhs=-100)
Source code in opti/constraint.py
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 |
|
NChooseK
Bases: Constraint
Source code in opti/constraint.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
|
__init__(names, max_active)
Only k out of n values are allowed to take nonzero values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names
|
List[str]
|
Parameter names that the constraint works on. |
required |
max_active
|
int
|
Maximium number of non-zero parameter values. |
required |
Examples:
A choice of 2 or less from A, B, C, D or E can be defined as
NChooseK(["A", "B", "C", "D", "E"], max_active=2)
Source code in opti/constraint.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
|
NonlinearEquality
Bases: Constraint
Source code in opti/constraint.py
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 |
|
__init__(expression, jacobian=None, names=None)
Equality of the form 'expression == 0'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expression
|
str
|
Mathematical expression that can be evaluated by |
required |
jacobian
|
Optional[str]
|
List of mathematical expressions that can be evaluated by |
None
|
names
|
Optional[List[str]]
|
List of variable names present in |
None
|
Examples:
You can pass any expression that can be evaluated by pd.eval
.
To define x12 + x22 = 1, use
NonlinearEquality("x1**2 + x2**2 - 1")
NonlinearEquality("sin(A) / (exp(B) - 1)")
NonlinearEquality("1 - `weight A` / `weight B`")
Source code in opti/constraint.py
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 |
|
NonlinearInequality
Bases: Constraint
Source code in opti/constraint.py
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 282 283 284 285 |
|
__init__(expression, jacobian=None, names=None)
Inequality of the form 'expression <= 0'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expression
|
str
|
Mathematical expression that can be evaluated by |
required |
jacobian
|
Optional[str]
|
List of mathematical expressions that can be evaluated by |
None
|
names
|
Optional[List[str]]
|
List of variable names present in |
None
|
Examples:
You can pass any expression that can be evaluated by pd.eval
.
To define x12 + x22 < 1, use
NonlinearInequality("x1**2 + x2**2 - 1")
NonlinearInequality("sin(A) / (exp(B) - 1)")
NonlinearInequality("1 - `weight A` / `weight B`")
Source code in opti/constraint.py
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 |
|