at.latticetools.optimizers#
Optimizers for observable matching.
least_squares_match(): matching withleast_squares(),jacobian_match(): Jacobian (damped Gauss-Newton) matching.
The Jacobian solver is adapted from the JacobianSolver of Xsuite’s
xdeps optimizer
(xsuite/xdeps,
Apache License 2.0).
_JacobianSolver is generic. _MeritFunction
adapts it to AT’s VariableList / ObservableList,
using ResponseMatrix to build the Jacobian and solve each
Newton step.
Functions
|
Observable matching using a damped Gauss-Newton (Jacobian) solver. |
|
Observable matching using |
- jacobian_match(variables, constraints, *, verbose=0, ftol=1e-08, xtol=1e-08, gtol=1e-08, tol=None, max_nfev=None, n_bisections=3, n_steps_max=20, rcond=1e-06, sing_val_cutoff=None, broyden=False, max_step=None, max_rel_penalty_increase=10.0, err=1000000.0, use_mp=False, pool_size=None, start_method=None, one_sided=True, **eval_kw)[source]#
Observable matching using a damped Gauss-Newton (Jacobian) solver.
An alternative to the scipy-backed methods of
match(), better suited to problems where Jacobian evaluation dominates the cost, or where explicit control over the singular-value cutoff or Jacobian reuse (broyden=True) is wanted.- Parameters:
variables (VariableList) – Variable parameters. The delta attribute of each variable is the step used to compute the Jacobian by finite differences.
constraints (ObservableList) – Constraints to fulfill.
verbose (int) – 0 for silent, 1 for a summary, >=2 for per-step detail.
ftol (float | None) – Tolerance on the relative decrease of the cost
0.5 * sum(weighted_deviations**2), as inscipy.optimize.least_squares().Nonedisables it.xtol (float | None) – Tolerance on the relative change of the variables, as in
scipy.optimize.least_squares().Nonedisables it.gtol (float | None) – Tolerance on the infinity norm of the cost gradient, as in
scipy.optimize.least_squares().Nonedisables it.tol (float | None) – Stop when the penalty (norm of the weighted deviations) drops below this value.
Nonedisables it.max_nfev (int | None) – Maximum number of function evaluations, checked after each Newton step.
Nonemeans no limit.n_bisections (int) – Maximum number of step-halvings in the line search.
n_steps_max (int) – Maximum number of outer (Newton) steps.
rcond (float) – Relative singular-value cutoff for the Newton step: singular values smaller than rcond times the largest one are discarded. Used only when
sing_val_cutoffis not given.sing_val_cutoff (int | None) – Number of (largest) singular values to keep for the Newton step; takes precedence over
rcond.broyden (bool) – If True, rank-1 (Broyden) update the Jacobian across outer steps instead of recomputing it by finite differences.
max_step (float | ndarray | None) – Maximum change of each variable in a single step. The whole step is scaled down if needed, keeping its direction.
max_rel_penalty_increase (float | None) – Once
n_bisectionsstep-halvings have been tried, keep halving the step while the trial penalty is at least this factor times the current one (for instance when the evaluation fails).Nonedisables these extra halvings.err (float) – Value substituted for a constraint’s deviation when it cannot be evaluated.
use_mp (bool) – Compute the Jacobian with multiprocessing, as in
ResponseMatrix.build().pool_size (int | None) – Compute the Jacobian with multiprocessing, as in
ResponseMatrix.build().start_method (str | None) – Compute the Jacobian with multiprocessing, as in
ResponseMatrix.build().one_sided (bool) – Compute the Jacobian using positive delta only (default) instead of +/-delta.
- Keyword Arguments:
**eval_kw – Evaluation keywords passed to
VariableList.set()/get()andObservableList.evaluate()(e.g. ring, dp, dct, df…). ring is needed if the variables or the constraints require a lattice.- Returns:
result –
scipy.optimize.OptimizeResultwithx,success,status,message,nfevandfun(the raw, unweighted deviations atx).statusfollowsscipy.optimize.least_squares()(0: maximum number of steps/evaluations, 1:gtol, 2:ftol, 3:xtol, 4: bothftolandxtol), with in addition 5:toland 6: no decrease found in the line search.successisstatus > 0.
Note
use_mp only parallelises the finite-difference Jacobian; the line search and the SVD step remain serial. Each match also pays a fixed cost to start the worker pool. use_mp pays off only when building a Jacobian is expensive; on small problems, it can be slower than serial.
With
broyden=Truethe Jacobian is computed by finite differences only at the first step, then updated from each accepted step. This saves most of the Jacobian evaluations, however on strongly nonlinear problems the solver may take poorer steps or stop early.With
broyden=Trueonly one Jacobian is computed, souse_mp=Truerarely helps.The variable bounds are respected at every step: a step that would cross a bound is scaled down to stop on it, and a variable on a bound is held fixed as long as the gradient pushes it outwards.
The delta must be small enough that observables are not strongly affected or the lattice becomes unstable: the default delta (1.0) is usually far too large. A step that is too large gives an inaccurate Jacobian and a poor or slow convergence; if it makes the Jacobian computation fail, the error message lists the variables whose step exceeds 10% of their value.
- least_squares_match(variables, constraints, *, verbose=0, lstsq_method=None, jac='2-point', ftol=1e-08, xtol=1e-08, gtol=1e-08, x_scale=None, loss='linear', f_scale=1.0, diff_step=None, tr_solver=None, tr_options=None, jac_sparsity=None, max_nfev=None, err=1000000.0, **eval_kw)[source]#
Observable matching using
least_squares().- Parameters:
variables (VariableList) – Variable parameters.
constraints (ObservableList) – Constraints to fulfill.
verbose (int) – Level of verbosity.
lstsq_method (str | None) –
least_squares()algorithm, ‘trf’, ‘dogbox’ or ‘lm’. IfNone, use ‘lm’ for unbounded problems with at least as many constraints as variables, ‘trf’ otherwise. ‘lm’ supports neither bounds nor fewer constraints than variables.jac (str) – see
least_squares().ftol (float | None) – see
least_squares().xtol (float | None) – see
least_squares().gtol (float | None) – see
least_squares().loss (str) – see
least_squares().f_scale (float) – see
least_squares().diff_step (float | ndarray | None) – see
least_squares().tr_solver (str | None) – see
least_squares().tr_options (dict | None) – see
least_squares().jac_sparsity (ndarray | None) – see
least_squares().max_nfev (int | None) – see
least_squares().x_scale (float | ndarray | str | None) – Characteristic scale of each variable. If
None, use the variable steps.err (float) – Value substituted for a constraint’s deviation when it cannot be evaluated.
- Keyword Arguments:
**eval_kw – Evaluation keywords provided to
VariableList.set()andObservableList.evaluate()(e.g. ring, dp, dct, orbit, twiss_in…).- Returns:
result –
scipy.optimize.OptimizeResultreturned byleast_squares().