cycling
cycling
¶
Parameter cycling and sensitivity-based selection for Q2MM optimization.
Implements the upstream Q2MM grad-simp optimization loop, adapted for our scipy-based optimizer architecture. The key insight from Norrby & Liljefors (1998) and Quinn et al. (2022) is that the Nelder-Mead simplex converges well on ≤40 parameters but fails on larger sets; the upstream code therefore selected only the 2-4 most sensitive parameters for each simplex pass.
Sensitivity is measured by central differentiation of all parameters:
- d1 = (f(x+h) - f(x-h)) / 2 (1st derivative, unnormalised)
- d2 = f(x+h) + f(x-h) - 2·f(x) (2nd derivative, unnormalised)
- simp_var = d2 / d1² (upstream selection metric)
Low simp_var identifies parameters where the objective is steep but
shallow-bottomed — gradient methods struggle here, but simplex can make
progress. The upstream authors noted this criterion is imperfect
(simplex.py:414: "Sorting based upon the 2nd derivative isn't such
a good criterion"); we therefore also support |d1| (absolute first
derivative) as an alternative.
References¶
- Norrby, P.-O.; Liljefors, T. J. Comput. Chem. 1998, 19, 1146-1166.
- Hansen, E.C. "Development and Applications of Q2MM" (PhD dissertation, University of Notre Dame, 2016).
- Quinn, T.R. et al. PLOS ONE 2022, 17, e0264960.
- Upstream code: github.com/nsf-c-cas/q2mm-2 (
simplex.py,opt.py).
SensitivityResult
dataclass
¶
SensitivityResult(d1: ndarray, d2: ndarray, simp_var: ndarray, ranking: ndarray, metric: str, n_evals: int)
Result of parameter sensitivity analysis via central differentiation.
Attributes:
| Name | Type | Description |
|---|---|---|
d1 |
ndarray
|
First derivative (unnormalised) for each parameter. |
d2 |
ndarray
|
Second derivative (unnormalised) for each parameter. |
simp_var |
ndarray
|
Upstream "simplex variable":
|
ranking |
ndarray
|
Parameter indices sorted by sensitivity (most sensitive first). |
metric |
str
|
Which metric was used for ranking. |
n_evals |
int
|
Number of objective function evaluations performed. |
LoopResult
dataclass
¶
LoopResult(success: bool, initial_score: float, final_score: float, n_cycles: int, n_eval: int = 0, cycle_scores: list[float] = list(), selected_indices: list[list[int]] = list(), sensitivity_results: list[SensitivityResult] = list(), message: str = '')
Result of an :class:OptimizationLoop run.
Attributes:
| Name | Type | Description |
|---|---|---|
success |
bool
|
|
initial_score |
float
|
Objective value before any optimisation. |
final_score |
float
|
Objective value after the last cycle. |
n_cycles |
int
|
Number of grad-simp cycles completed. |
n_eval |
int
|
Total objective function evaluations across all cycles (grad + sensitivity + simp). |
cycle_scores |
list[float]
|
Objective value at the end of each cycle. |
selected_indices |
list[list[int]]
|
Parameter indices selected for each simplex pass. |
sensitivity_results |
list[SensitivityResult]
|
Full sensitivity analysis for each cycle. |
message |
str
|
Human-readable summary. |
improvement
property
¶
Fractional improvement: (initial - final) / initial.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Fractional improvement, or 0.0 if |
summary
¶
Human-readable summary string.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Multi-line summary of the loop result. |
Source code in q2mm/optimizers/cycling.py
SubspaceObjective
¶
SubspaceObjective(objective: ObjectiveFunction, active_indices: list[int] | ndarray, full_vector: ndarray)
Wraps an :class:ObjectiveFunction to optimise a parameter subset.
The wrapper accepts a sub-vector of length len(active_indices)
and maps it into the full parameter vector before delegating to the
underlying objective. This lets :func:scipy.optimize.minimize run
Nelder-Mead (or any other method) on just the selected parameters
while the rest stay fixed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objective
|
ObjectiveFunction
|
The full objective function. |
required |
active_indices
|
list[int] | ndarray
|
Indices into the full parameter vector that are active. |
required |
full_vector
|
ndarray
|
The current full parameter vector (inactive params are taken from this snapshot). |
required |
Initialize the subspace objective wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objective
|
ObjectiveFunction
|
The full objective function. |
required |
active_indices
|
list[int] | ndarray
|
Indices into the full parameter vector that are active. |
required |
full_vector
|
ndarray
|
The current full parameter vector. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in q2mm/optimizers/cycling.py
build_full_vector
¶
Map a sub-vector back into the full parameter vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sub_vector
|
ndarray
|
Values for the active parameters. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Full parameter vector with active slots replaced. |
Source code in q2mm/optimizers/cycling.py
__call__
¶
Evaluate the objective on sub_vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sub_vector
|
ndarray
|
Values for the active parameters. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Objective function score. |
Source code in q2mm/optimizers/cycling.py
residuals
¶
Return the residual vector (for least_squares).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sub_vector
|
ndarray
|
Values for the active parameters. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Weighted residual vector. |
Source code in q2mm/optimizers/cycling.py
get_initial_vector
¶
Return the sub-vector corresponding to current active parameters.
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Copy of active parameter values from the base vector. |
Source code in q2mm/optimizers/cycling.py
get_bounds
¶
Bounds for the active parameters only.
Returns:
| Type | Description |
|---|---|
list[tuple[float, float]]
|
list[tuple[float, float]]: Lower/upper bound pairs for each active parameter. |
Source code in q2mm/optimizers/cycling.py
OptimizationLoop
¶
OptimizationLoop(objective: ObjectiveFunction, *, max_params: int = 3, convergence: float = 0.01, max_cycles: int = 10, full_method: str = 'L-BFGS-B', simp_method: str = 'Nelder-Mead', full_maxiter: int = 200, simp_maxiter: int = 200, sensitivity_metric: Literal['simp_var', 'abs_d1'] = 'simp_var', eps: float = 0.001, full_jac: str | None = None, verbose: bool = True)
grad-simp cycling loop inspired by the upstream Q2MM workflow.
Each cycle
- Full-space pass — run
full_method(default L-BFGS-B) on all parameters. - Sensitivity analysis — central differentiation to rank params.
- Subspace simplex — run
simp_method(default Nelder-Mead) on only the topmax_paramsmost sensitive parameters. - Convergence check — stop if the fractional improvement in the
objective falls below
convergence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objective
|
ObjectiveFunction
|
The objective function to minimise. |
required |
max_params
|
int
|
Number of parameters per simplex pass (upstream default: 3). |
3
|
convergence
|
float
|
Stop when |
0.01
|
max_cycles
|
int
|
Maximum number of grad-simp cycles. |
10
|
full_method
|
str
|
Scipy method for the full-space pass. |
'L-BFGS-B'
|
simp_method
|
str
|
Scipy method for the subspace pass. |
'Nelder-Mead'
|
full_maxiter
|
int
|
Max iterations for the full-space pass. |
200
|
simp_maxiter
|
int
|
Max iterations for the subspace pass. |
200
|
sensitivity_metric
|
str
|
How to rank parameters for selection
— |
'simp_var'
|
eps
|
float
|
Finite-difference step size for the full-space optimizer. |
0.001
|
verbose
|
bool
|
Whether to log progress. |
True
|
References
Upstream loop.py:Loop.opt_loop() and
simplex.py:Simplex.run().
Initialize the optimization loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objective
|
ObjectiveFunction
|
The objective function to minimise. |
required |
max_params
|
int
|
Number of parameters per simplex pass. |
3
|
convergence
|
float
|
Fractional improvement threshold. |
0.01
|
max_cycles
|
int
|
Maximum number of grad-simp cycles. |
10
|
full_method
|
str
|
Scipy method for the full-space pass. |
'L-BFGS-B'
|
simp_method
|
str
|
Scipy method for the subspace pass. |
'Nelder-Mead'
|
full_maxiter
|
int
|
Max iterations for the full-space pass. |
200
|
simp_maxiter
|
int
|
Max iterations for the subspace pass. |
200
|
sensitivity_metric
|
str
|
Parameter ranking criterion. |
'simp_var'
|
eps
|
float
|
Finite-difference step size. |
0.001
|
full_jac
|
str | None
|
Jacobian strategy for the full-space
pass. |
None
|
verbose
|
bool
|
Whether to log progress. |
True
|
Source code in q2mm/optimizers/cycling.py
run
¶
run() -> LoopResult
Execute the grad-simp cycling loop.
Returns:
| Name | Type | Description |
|---|---|---|
LoopResult |
LoopResult
|
Contains convergence status, per-cycle scores, and selected parameter indices. |
Source code in q2mm/optimizers/cycling.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 | |
compute_sensitivity
¶
compute_sensitivity(objective: ObjectiveFunction, step_sizes: ndarray | None = None, metric: Literal['simp_var', 'abs_d1'] = 'simp_var', bounds: list[tuple[float, float]] | None = None) -> SensitivityResult
Central differentiation to rank parameter sensitivity.
For each parameter i, evaluates f(x + h_i e_i) and
f(x - h_i e_i) and computes:
d1_i = (f_fwd - f_bwd) / 2d2_i = f_fwd + f_bwd - 2·f_0simp_var_i = d2_i / d1_i²(upstream criterion)
The ranking is by ascending simp_var (lowest = most suitable
for simplex) or descending |d1| (largest gradient = most
sensitive), depending on metric.
When bounds are provided, step sizes are shrunk so that
x0 ± h_eff stays within [lower, upper] for each parameter.
Parameters where no symmetric step is possible (i.e. x0 is at a
bound) are skipped and assigned inf simp_var / zero d1.
When the engine supports :meth:~MMEngine.batched_energy and all
references are energy-only, all required 2K + 1 evaluations
(for the K parameters with nonzero step sizes, K ≤ N) are
vectorised into a single call where possible (e.g. jax.vmap on GPU).
Cost: 2K + 1 objective evaluations (1 baseline + 2 per active
parameter for central differentiation; in the worst case K = N,
giving 2N + 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objective
|
ObjectiveFunction
|
Must already be evaluable (engine and molecules configured). |
required |
step_sizes
|
ndarray | None
|
Per-parameter step sizes.
Defaults to :meth: |
None
|
metric
|
str
|
Ranking criterion — |
'simp_var'
|
bounds
|
list[tuple[float, float]] | None
|
Per-parameter
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
SensitivityResult |
SensitivityResult
|
Derivatives, rankings, and evaluation count. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in q2mm/optimizers/cycling.py
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 286 287 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 322 323 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 389 390 391 392 393 | |