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
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
394
395
396
397
398
399
400
401
402
403
404
405
// Index-based loops mirror Powell's exposition (LINCOA, Powell 2015) and the
// dense index arithmetic of the active-set QR / H-factorization algebra;
// blanket-allowed here.
//! LINCOA (Powell 2015): linearly-constrained model-based derivative-free
//! optimization.
//!
//! [`Lincoa`] reuses the shared Powell quadratic-model core (the crate-internal
//! `powell` module, shared with NEWUOA and BOBYQA) and swaps the trust-region
//! subproblem for the linearly-constrained `trstep` (projected truncated-CG) with
//! the active-set step `getact`/`ActiveSetQr`. It binds
//! [`CostFunction`](crate::core::problem::CostFunction) +
//! [`ConstraintSet`](crate::ConstraintSet) (the
//! general linear form: box bounds, equalities, and inequalities).
//!
//! # Architecture
//!
//! Like its siblings, the model is maintained through a factored inverse-KKT
//! matrix `H`; the driver is the Figure-1 loop with the ρ/Δ schedule, the LINCOA
//! geometry step (`geostep`), origin shifts, and the `tryqalt` alternative-model
//! swap. The linear feasible region enters through the active-set QR
//! (`ActiveSetQr`) threaded across iterations and the residual vector `rescon`.
//! Box bounds and linear equalities fold into the inequality form `A x ≤ b`
//! exactly as PRIMA's `get_lincon` does (`fold_constraints`); the public solver
//! binds the general-form `ConstraintSet` aggregator, which carries all
//! three kinds at once (see [`Lincoa`]).
//!
//! # Backends
//!
//! Backend-generic over the parameter vector; the constraint matrix `A` is read
//! through [`crate::MatTransposeVecInto`](crate::core::math::MatTransposeVec) only (each
//! constraint normal is `Aᵀ eⱼ`), which every backend's matrix type provides
//! (`DenseMatrix` for `Vec<F>`, `Array2`, `DMatrix`, `Mat`), with `F = f64`
//! or `f32`. Model algebra uses pure-Rust `Vec<F>` storage; wasm-clean.
//!
//! # References
//!
//! M. J. D. Powell, *On fast trust region methods for quadratic models with
//! linear constraints*, Math. Program. Comput. 7:237–267 (2015). Ported from
//! [PRIMA](https://github.com/libprima/prima) v0.7.2.
pub
pub
pub
pub
pub
use crateConstraintSolveError;
use crate;
use crateInitialState;
use crate;
use crate;
use crateSolver;
use crate;
use crateTermination;
use ;
use fold_constraints;
/// LINCOA (Powell 2015): linearly-constrained model-based derivative-free
/// trust-region optimization.
///
/// LINCOA minimizes a smooth objective subject to linear constraints (box
/// bounds, linear equalities, and linear inequalities, all reduced internally to
/// `A x ≤ b`) using only objective values, no derivatives. It maintains the same
/// least-Frobenius-norm quadratic surrogate as [`Newuoa`](crate::Newuoa) /
/// [`Bobyqa`](crate::Bobyqa), and keeps every trust-region iterate feasible via a
/// projected truncated-CG subproblem with an active-set QR.
///
/// Drive it with an [`Executor`](crate::Executor) over a [`PointState`] on a
/// problem implementing [`CostFunction`] and
/// declaring its bounds and linear blocks in [`ConstraintSet`](crate::ConstraintSet)
/// (here only the inequality block is present):
///
/// ```
///
/// use basin::{CostFunction, DenseMatrix, Executor, Lincoa, PointState};
///
/// // min ‖x − (2, 2)‖² s.t. x0 + x1 ≤ 2 (optimum: the projection (1, 1)).
/// // The pure-Rust `DenseMatrix`/`Vec<f64>` carriers need no backend feature.
/// struct Proj {
/// a: DenseMatrix<f64>,
/// b: Vec<f64>,
/// }
/// impl basin::Constraints for Proj {
/// type Set = basin::ConstraintSet<basin::NoBounds, basin::LinearBlocks<basin::NoLinearBlock, basin::LinearBlock<DenseMatrix<f64>, Vec<f64>>>>;
/// fn constraints(&self) -> <Self::Set as basin::ConstraintDescription>::Borrowed<'_> { basin::ConstraintSet::new().with_linear_inequalities(basin::LinearBlock::optional({ Some((&self.a, &self.b)) })) }
/// }
/// impl CostFunction for Proj {
/// 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] - 2.0).powi(2) + (x[1] - 2.0).powi(2))
/// }
/// }
///
///
/// let problem = Proj {
/// a: DenseMatrix::from_row_slice(1, 2, &[1.0, 1.0]),
/// b: vec![2.0],
/// };
/// let solver = Lincoa::new()
/// .with_initial_radius(0.5)
/// .with_final_radius(1e-7);
/// let state = PointState::new(vec![0.0, 0.0]);
/// let result = Executor::new(problem, solver, state)
/// .max_cost_evals(500)
/// .run()
/// .unwrap();
/// assert!((result.best_param()[0] - 1.0).abs() < 1e-3);
/// assert!((result.best_param()[1] - 1.0).abs() < 1e-3);
/// ```
///
/// # Configuration
///
/// - [`with_initial_radius`](Self::with_initial_radius): initial trust-region radius `ρ_beg`
/// (a reasonable initial change to the variables; default `1.0`).
/// - [`with_final_radius`](Self::with_final_radius): final radius `ρ_end`, the required
/// accuracy (default `1e-6`); must satisfy `ρ_beg > ρ_end > 0`.
/// - [`with_npt`](Self::with_npt): interpolation-set size `npt`, in
/// `[n+2, ½(n+1)(n+2)]` (default `2n+1`).
///
/// # ConstraintSet
///
/// LINCOA binds the general-form
/// [`ConstraintSet`](crate::ConstraintSet): a problem
/// exposes any combination of box bounds, linear equalities, and linear
/// inequalities (each accessor is optional). All present blocks are folded into
/// one unit-normalized `A x ≤ b` system: bounds as `±eᵢ` rows, an equality
/// `aᵀx = β` as the pair `aᵀx ≤ β`, `−aᵀx ≤ −β`, and every iterate is kept
/// feasible by the active-set projection. The start point should be feasible;
/// if not, LINCOA relaxes `b` to make it feasible (Powell's behavior).
///
/// # Termination
///
/// Natural convergence is `ρ` reaching `ρ_end`, signalled as
/// [`crate::TerminationCode::SolverConverged`]. Add
/// [`max_cost_evals`](crate::Executor::max_cost_evals) to cap the budget or
/// [`with_absolute_radius_tolerance`](crate::Lincoa::with_absolute_radius_tolerance) to stop at a coarser `ρ`.
///
/// # Progress and continuation
///
/// Use [`PointState::new`] or [`crate::Executor::from_start`] with an unevaluated
/// starting point. Fresh initialization clears progress and rebuilds the
/// interpolation model, even when reusing a populated state or solver. The
/// shared state retains the current evaluated point, its historical objective
/// incumbent, and all six raw evaluation categories. It supplies no gradient
/// or radius capability. The solver owns every model buffer and the radius
/// schedule; [`rho`](Self::rho) returns `None` before initialization. Inspect
/// it through [`crate::Executor::run_with_solver`] or [`crate::Stepper::solver`].
///
/// Preserve [`crate::ExactCheckpoint`] for continuation without initialization
/// or reevaluation. With `serde`, the solver and shared state support
/// serialization, including the complete numerical model. A progress-only
/// snapshot is a fresh point warm start. Legacy algorithm-specific state
/// payloads cannot be read as `PointState` or as a new exact checkpoint.
/// Radius-based convergence remains solver-owned; objective targets and stalls
/// use the shared state's historical incumbent.
///
/// # Backends
///
/// `Vec<F>`/`DenseMatrix<F>`, nalgebra `DVector<F>`/`DMatrix<F>`, ndarray
/// `Array1<F>`/`Array2<F>`, and faer `Col<F>`/`Mat<F>`, for every supported
/// backend release and `F = f64` or `f32`. Vectors need [`Clone`], [`VectorLen`],
/// and indexing; constraint matrices need [`crate::MatTransposeVecInto`]. Model algebra
/// uses pure-Rust `Vec<F>` storage, with no BLAS/LAPACK dependency. Choose radii
/// appropriate to the scalar precision.
///
/// # References
///
/// M. J. D. Powell, *On fast trust region methods for quadratic models with
/// linear constraints*, Math. Program. Comput. 7:237–267 (2015). Cross-validated
/// against [PRIMA](https://github.com/libprima/prima) v0.7.2, the authoritative
/// source for the exact formulas. PRIMA is BSD 3-Clause licensed; its required
/// notice is retained in the crate's `COPYRIGHT` file.
///
/// [`CostFunction`]: crate::core::problem::CostFunction
/// [`crate::TerminationCode::SolverConverged`]: crate::TerminationCode::SolverConverged
/// Build a `V` from a flat `&[F]`, reusing `template` for type and length.