Inequality Constraints
Cross-parameter inequalities such as t_head < t_tail or
t1 + t2 <= total are not dependencies: no parameter is removed from
the fit. They are declared as InequalitySpec objects on the project
and enforced by the BUMPS engines (the Bumps* minimizers and the
DREAM sampler) as penalties on the fit problem. LMFit and DFO-LS cannot
enforce them, and fit raises ValueError in that case rather than
silently dropping the physics.
from easyscience.fitting import AvailableMinimizers
from easyreflectometry import InequalitySpec
project.minimizer = AvailableMinimizers.Bumps
t_a = project.parameter_path(layer_a.thickness) # 'models/0/sample/1/layers/0/thickness'
t_b = project.parameter_path(layer_b.thickness)
project.add_inequality_constraint(InequalitySpec('a', '<', 'b', {'a': t_a}, {'b': t_b}, name='order'))
project.add_inequality_constraint(InequalitySpec('a + b', '<', '90', {'a': t_a, 'b': t_b}, {}))
project.violated_inequality_constraints() # check the start point first
project.fitter.fit_single_data_set_1d(dataset) # penalties applied automatically
Parameters are referenced by structural path (see
Project.parameter_path) so the constraints are saved with the project.
While a constraint is violated BUMPS skips the model evaluation and adds
a penalty growing with the violation, steering the optimizer back into
the feasible region; the Bumps_lm method spreads the penalty over the
residuals instead and enforces inequalities more weakly.
Both sides of a spec are unit-checked when it is registered; a mismatch
raises UnitError, a subclass of ValueError.
inequality_constraints
Cross-parameter inequality constraints enforced by the fit.
An :class:InequalitySpec is a purely declarative statement such as
t_head < t_tail or t1 + t2 <= total: two expressions over parameter
aliases and a relation. Unlike the equality constraints in
:mod:easyreflectometry.constraints it is not a parameter dependency —
no parameter is removed from the fit. Instead the specs are translated,
at the start of every fit, into penalty terms attached to the BUMPS
FitProblem (FitProblem(constraints=[...])): while a constraint is
violated BUMPS skips the model, adds a large penalty and a term growing
with the violation, which steers the optimizer back into the feasible
region. Only the BUMPS engine family (Bumps* minimizers and the DREAM
sampler) supports this; Fitter.fit rejects inequalities for LMFit and
DFO-LS.
Specs reference parameters by structural path (models/0/sample/1/layers/0/thickness,
see :meth:easyreflectometry.Project.parameter_path) rather than by
object or unique name, so they survive project save/load and can be
rebuilt against a reloaded object tree.
Design notes — why the translation happens inside the fit, and reads the BUMPS parameters:
- BUMPS evaluates the constraints before the model and skips the model
while any fails. The EasyScience parameter values are only written inside
the model call, so at constraint-evaluation time they lag the optimizer's
trial vector — and freeze entirely once a constraint fails. The operands
built here therefore read
bumps.Parameter.valueof the live problem, which BUMPS sets for every trial point. - Those BUMPS parameters only exist for the free EasyScience parameters
and are rebuilt per fit, which is why a factory (
constraints_factory) is passed down the fitting chain and invoked as each BUMPS problem is built (see :mod:easyreflectometry._bumps_constraints). Fixed parameters are frozen as constants; dependent (constrained or derived) parameters are expanded recursively into their independent leaves. - Each penalty term returns the linear violation; BUMPS squares it once, giving a quadratic penalty.
__all__ = ['RELATIONS', 'InequalityEvaluation', 'InequalitySpec', 'UnitError', 'build_constraints_factory', 'check_units', 'evaluate_spec']
module-attribute
RELATIONS = ('<', '<=', '>', '>=')
module-attribute
_RELATION_ALIASES = {'≤': '<=', '≥': '>=', '=<': '<=', '=>': '>='}
module-attribute
_BUMPS_PREFIX = 'p'
module-attribute
_SAFE_SYMBOLS = {'pi': np.pi, 'e': np.e, 'sqrt': np.sqrt, 'exp': np.exp, 'log': np.log, 'log10': np.log10, 'sin': np.sin, 'cos': np.cos, 'tan': np.tan, 'abs': abs, 'min': min, 'max': max}
module-attribute
PathResolver = Callable[[str], DescriptorNumber]
module-attribute
UnitError
Bases: ValueError
A unit problem in an inequality constraint.
Raised by :func:check_units when a side cannot be evaluated with its
units or the two sides of a spec carry incompatible units. Subclasses
ValueError so existing except ValueError callers keep working;
new callers can catch this type instead of matching message substrings.
Source code in src/easyreflectometry/inequality_constraints.py
75 76 77 78 79 80 81 82 | |
InequalitySpec
dataclass
Declarative cross-parameter inequality, e.g. t_head < t_tail.
Attributes:
| Name | Type | Description |
|---|---|---|
lhs_expression |
str
|
Expression over the aliases in |
op |
str
|
One of |
rhs_expression |
str
|
Expression over the aliases in |
lhs_paths, rhs_paths |
dict[str, str]
|
Alias to structural parameter path ( |
name |
str
|
Optional user label. |
enabled |
bool
|
Disabled specs are kept but not applied to fits. |
Source code in src/easyreflectometry/inequality_constraints.py
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 | |
paths
property
All alias → path pairs of both sides.
validate_syntax()
Check that both sides parse and every identifier has an alias mapping.
Source code in src/easyreflectometry/inequality_constraints.py
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 | |
InequalityEvaluation
dataclass
Result of evaluating a spec against the current parameter values.
Source code in src/easyreflectometry/inequality_constraints.py
242 243 244 245 246 247 248 249 250 251 252 253 | |
_Constant
Source code in src/easyreflectometry/inequality_constraints.py
259 260 261 262 263 264 265 266 | |
_BumpsValue
Reads a BUMPS parameter — i.e. the optimizer's current trial value.
Source code in src/easyreflectometry/inequality_constraints.py
269 270 271 272 273 274 275 276 277 278 | |
_Expression
Evaluates an expression whose symbols are value sources.
Source code in src/easyreflectometry/inequality_constraints.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
_Violation
The BUMPS constraint object: float() is 0 when satisfied, else the violation.
Source code in src/easyreflectometry/inequality_constraints.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
_normalize_relation(op)
Source code in src/easyreflectometry/inequality_constraints.py
110 111 112 113 114 | |
_new_interpreter()
Source code in src/easyreflectometry/inequality_constraints.py
117 118 119 120 121 | |
_evaluate(interpreter, expression, symbols)
Source code in src/easyreflectometry/inequality_constraints.py
124 125 126 127 | |
_identifiers(expression)
Identifiers used in expression that are not builtin math symbols.
Source code in src/easyreflectometry/inequality_constraints.py
130 131 132 133 | |
_violation(lhs, op, rhs)
Linear violation of lhs op rhs (0 when satisfied).
Strict and non-strict relations are treated alike: a penalty of exactly zero at the boundary is the only continuous choice.
Source code in src/easyreflectometry/inequality_constraints.py
316 317 318 319 320 321 322 323 324 325 326 | |
_source_for(parameter, bumps_pars, trail)
Translate an EasyScience parameter into a source reading the BUMPS trial vector.
- free parameter → the BUMPS parameter of the problem;
- fixed parameter (or plain descriptor) → constant;
- dependent parameter → its dependency expression, expanded recursively.
Source code in src/easyreflectometry/inequality_constraints.py
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 | |
_resolve_all(spec, resolve)
Source code in src/easyreflectometry/inequality_constraints.py
360 361 362 363 364 365 366 367 | |
build_constraints_factory(specs, resolve)
Build the constraints_factory hook for the given specs.
The returned callable is what easyscience's BUMPS engine invokes
with the {prefixed unique name: bumps.Parameter} mapping of a
freshly built problem; it returns one penalty object per enabled spec.
Paths are resolved and dependent parameters expanded at that moment, so
the factory always reflects the model as it is when the fit starts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
specs
|
Iterable[InequalitySpec]
|
Specs to apply; disabled ones are skipped. |
required |
resolve
|
Callable[[str], DescriptorNumber]
|
Structural-path resolver, typically |
required |
Returns:
| Type | Description |
|---|---|
Callable | None
|
The factory, or |
Source code in src/easyreflectometry/inequality_constraints.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
evaluate_spec(spec, resolve)
Evaluate a spec against the current parameter values.
Used for the start-point feasibility check before a fit is launched and for displaying the constraint state; dependent parameters contribute their current (already propagated) value.
Source code in src/easyreflectometry/inequality_constraints.py
414 415 416 417 418 419 420 421 422 423 424 425 426 | |
check_units(spec, resolve)
Raise :class:UnitError when the two sides of spec have incompatible units.
Each side is evaluated with the unit-carrying DescriptorNumber
objects themselves (the same arithmetic the equality constraints use),
so t1 + sld is rejected by EasyScience and t_head < t_tail
passes. A plain numeric side is accepted against any unit: it is read in
the unit of the other side. The same applies to a side mixing literals
with parameters (90 - b): it is checked numerically and its literals
are read in the unit of the other side.
Source code in src/easyreflectometry/inequality_constraints.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | |