Skip to content

Fix complex compiled constraints#3811

Merged
krzywon merged 1 commit intomainfrom
fix-multi-constraint-fitting
Dec 9, 2025
Merged

Fix complex compiled constraints#3811
krzywon merged 1 commit intomainfrom
fix-multi-constraint-fitting

Conversation

@bmaranville
Copy link
Copy Markdown
Contributor

@bmaranville bmaranville commented Dec 8, 2025

Description

The compiled constraints are supposed to be a function that takes the parameters as input, and updates the values of the parameters based on the constraint expressions supplied, after each call to problem.setp(pvec), through the setp_hook() function.

The existing code (introduced in #3375) was incorrectly replacing the parameter names with f"{name}.slot", which is wrong for 2 reasons:

  1. In the case of simple equality constraints, e.g. P1 = P2, it was instead executing P1.slot = P2.slot, which replaces the Variable in the slot of P1 with the actual Variable instance in P2, so that the P1 is essentially an alias for P2 after that. This will "work" and the fit will complete, but the parameter structure of the model is changed in an unintended way.
  2. In the case of (even slightly) more complex constraints, the constraint expressions fail to execute properly, e.g. for this constraint expression:
    M1.scale = M2.scale * 1.2 you end up with this expression to evaluate: M1.scale.slot = M2.scale.slot * 1.2, which doesn't work because M2.scale.slot is an instance of Variable, on which we can't directly do arithmetic.

To achieve the goal of the compiled constraint functions, we should instead use the .value attribute; on the LHS of the equality, it will trigger the .value setter which pushes the new value down into the slot, and on the RHS it will use the getter to pull the value back out of the slot for (as a float) for doing expression math on it.

This fixes a sub-issue in #3793 (comment)

How Has This Been Tested?

Ran the constrained fit in this project:
6-1-2-constrained-fit-expr.json

Review Checklist:

[if using the editor, use [x] in place of [ ] to check a box]

Documentation (check at least one)

  • There is nothing that needs documenting (fixes a bug)
  • Documentation changes are in this PR
  • There is an issue open for the documentation (link?)

Installers

  • There is a chance this will affect the installers, if so
    • Windows installer (GH artifact) has been tested (installed and worked)
    • MacOSX installer (GH artifact) has been tested (installed and worked)
    • Wheels installer (GH artifact) has been tested (installed and worked)

Licensing (untick if necessary)

  • The introduced changes comply with SasView license (BSD 3-Clause)

@bmaranville bmaranville requested a review from pkienzle December 8, 2025 19:42
@bmaranville bmaranville changed the title Fix compiled constraints again Fix complex compiled constraints Dec 8, 2025
@bmaranville
Copy link
Copy Markdown
Contributor Author

NOTE this should not negatively affect the uncertainties calculations done at the end of the fit. In that case, for each fitter bumps.Parameter object, at the end of the fit the p.slot attribute is replaced by an uncertainties.ufloat object with the same value, and then the correlated uncertainties are calculated from those objects pushed into the covariance matrix.

If the covariance matrix is being distorted by the error introduced in #3375 (I'm not sure if it is), then the current PR should fix that as well.

@krzywon krzywon merged commit acd6ea6 into main Dec 9, 2025
40 checks passed
@krzywon krzywon deleted the fix-multi-constraint-fitting branch December 9, 2025 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants