Parameter 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, 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. |
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
¶
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, 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
|
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
418 419 420 421 422 423 424 425 426 427 428 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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 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 | |
compute_sensitivity
¶
compute_sensitivity(objective: ObjectiveFunction, step_sizes: ndarray | None = None, metric: Literal['simp_var', 'abs_d1'] = 'simp_var') -> 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.
Cost: 2N + 1 objective evaluations (1 baseline + 2 per parameter
for central differentiation).
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'
|
Returns:
| Name | Type | Description |
|---|---|---|
SensitivityResult |
SensitivityResult
|
Derivatives, rankings, and evaluation count. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in q2mm/optimizers/cycling.py
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 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 | |