1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
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
// Index-based loops mirror Powell's exposition (NEWUOA, Powell 2006) and the
// dense index arithmetic of the H-factorization algebra; the lint is
// blanket-allowed for this module.
// The standalone `minimize` driver, `NewuoaConfig` / `NewuoaOutcome`, and the
// model-core read surface have only `#[cfg(test)]` callers (the in-module tests
// and the PRIMA parity fixtures); the public `Newuoa` solver drives the model
// through `NewuoaWork` instead. Blanket-allow so that test-only `pub(crate)`
// surface does not trip dead-code analysis in the non-test build.
//! NEWUOA (Powell 2006) — model-based derivative-free optimization.
//!
//! [`Newuoa`] is Powell's NEWUOA: an unconstrained trust-region method for
//! smooth objectives whose derivatives are unavailable. It maintains a quadratic
//! surrogate `Q` interpolating the objective on a set of `npt` points and updates
//! it by the least-Frobenius-norm rule, so each iteration needs only **one** new
//! objective value. It binds [`CostFunction`] only — no gradient.
//!
//! # Architecture
//!
//! The module is the spine of Powell's model-based family (NEWUOA → BOBYQA →
//! LINCOA): an internal `QuadraticModel`, maintained through a factored
//! inverse-KKT matrix `H = W⁻¹`, plus a swappable trust-region subproblem (here
//! TRSAPP). The two operations that maintain the model are the closed-form
//! initialization (§3) and the least-Frobenius-norm rank-2 update (§4; derivation
//! in Powell 2004a, `references/frobenius-update/`). The Figure-1 driver loop adds
//! the ρ/Δ schedule (§7), the BIGLAG/BIGDEN geometry steps (§6), origin shifts
//! (§7), and the Qint robustness modification (§8). These pieces are
//! crate-internal; only [`Newuoa`] and [`NewuoaState`](crate::NewuoaState) are
//! public.
//!
//! # State / solver split
//!
//! The iterate and the trust-region radius `ρ` live on
//! [`NewuoaState`](crate::NewuoaState); the quadratic model, the factored `H`,
//! and the ρ/Δ schedule are solver-internal scratch the solver carries — the same
//! split Levenberg-Marquardt uses for its μ/ν/diag working state. This is why
//! [`NewuoaState`](crate::NewuoaState) is generic over the parameter vector `V`
//! only, not the backend matrix `M`: NEWUOA's model algebra is internal `Vec<F>`
//! scratch and needs no `linalg`-tier ops from `V`.
//!
//! # Termination
//!
//! NEWUOA's natural convergence is `ρ` reaching `ρ_end` (configured on the
//! solver, where it also drives the eq-7.6 schedule); the solver signals it via
//! [`TerminationReason::SolverConverged`]. Add
//! [`MaxCostEvals`](crate::MaxCostEvals) to cap the evaluation budget, or
//! [`RhoTolerance`](crate::RhoTolerance) to stop early at a coarser `ρ`.
//!
//! # Backends
//!
//! Backend-generic over the parameter vector: `Vec<f64>`, nalgebra, ndarray, and
//! faer all work (the parameter type needs only element access and length —
//! [`Clone`], [`VectorLen`](crate::core::math::VectorLen), and indexing). wasm-
//! clean: the model algebra is pure-Rust `Vec<F>` with no BLAS/LAPACK.
//!
//! [`CostFunction`]: crate::core::problem::CostFunction
//! [`TerminationReason::SolverConverged`]: crate::TerminationReason::SolverConverged
pub
pub
pub
pub
pub
use crate;
use crate;
use crateSolver;
use crateNewuoaState;
use crateTerminationReason;
use ;
/// NEWUOA (Powell 2006): model-based derivative-free trust-region optimization.
///
/// Configure the trust-region radii and interpolation-set size, then drive it
/// with an [`Executor`](crate::Executor) over a [`NewuoaState`]:
///
/// ```
/// use basin::{CostFunction, Executor, Newuoa, NewuoaState, MaxCostEvals};
///
/// struct Quadratic;
/// impl CostFunction for Quadratic {
/// type Param = Vec<f64>;
/// type Output = f64;
/// type Error = std::convert::Infallible;
/// fn cost(&self, x: &Vec<f64>) -> Result<f64, std::convert::Infallible> {
/// Ok((x[0] - 1.0).powi(2) + 2.0 * (x[1] + 2.0).powi(2))
/// }
/// }
///
/// let solver = Newuoa::new().with_rho_beg(0.5).with_rho_end(1e-8);
/// let state = NewuoaState::new(vec![0.0, 0.0]);
/// let result = Executor::new(Quadratic, solver, state)
/// .terminate_on(MaxCostEvals(500))
/// .run()
/// .unwrap();
/// assert!(result.best_cost() < 1e-10);
/// ```
///
/// # Configuration
///
/// - [`with_rho_beg`](Self::with_rho_beg) — initial trust-region radius `ρ_beg`
/// (a reasonable initial change to the variables; default `1.0`). Also the
/// initial `Δ`.
/// - [`with_rho_end`](Self::with_rho_end) — final radius `ρ_end`, the required
/// accuracy in the variables (default `1e-6`). The run converges once `ρ`
/// reaches it; `ρ_end` also drives the eq-7.6 schedule, so it must satisfy
/// `ρ_beg > ρ_end > 0`.
/// - [`with_npt`](Self::with_npt) — interpolation-set size `npt`, in
/// `[n+2, ½(n+1)(n+2)]` (default `2n+1`, Powell's recommendation).
///
/// # Backends
///
/// Backend-generic over the parameter vector: `Vec<f64>`, nalgebra, ndarray, and
/// faer all work. The model algebra is internal pure-Rust `Vec<f64>` scratch, so
/// the parameter type needs only [`Clone`], [`VectorLen`], and `Index`/`IndexMut`
/// element access — never any `linalg`-tier matrix op. wasm-clean (no
/// BLAS/LAPACK).
///
/// # References
///
/// M. J. D. Powell, *The NEWUOA software for unconstrained optimization without
/// derivatives*, in Large-Scale Nonlinear Optimization (2006), pp. 255–297.
/// Cross-validated against [PRIMA](https://github.com/libprima/prima) v0.7.2.
/// Build a `V` from a flat `&[F]`, reusing `template` for type and length.