basin 2.0.0

Numerical optimization in pure Rust, with pluggable linear-algebra backends and WASM support.
Documentation
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
406
407
408
409
410
411
412
413
414
415
416
417
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
//! Explicit problem and local-search transformations.

use super::evaluation::{BoxOnly, BoxProblem, invalid};
use super::*;
use crate::Scalar;
use crate::core::inner::{InitialState, WarmStart};
use crate::core::problem::{
    CostFunction, Gradient, Hessian, HessianProduct, Jacobian,
    MiniBatchGradient, Problem, Residual,
};
use crate::core::solver::{Solver, SolverStep};
use crate::core::state::State;
use crate::core::termination::Termination;

/// A sampling domain used by algorithms whose local search can leave the box.
///
/// Sampling bounds do not declare mathematical constraints. MA-LS accepts only
/// an unconstrained problem with this capability.
pub trait SamplingBounds: CostFunction<Constraints = NoConstraints> {
    /// Borrow both sampling endpoints, reporting missing data explicitly.
    fn sampling_bounds(
        &self,
    ) -> Result<(&Self::Param, &Self::Param), ConstraintError>;
    /// Borrow the lower endpoint after validating the sampling box.
    fn sampling_lower(&self) -> &Self::Param {
        self.sampling_bounds().expect("validated sampling bounds").0
    }
    /// Borrow the upper endpoint after validating the sampling box.
    fn sampling_upper(&self) -> &Self::Param {
        self.sampling_bounds().expect("validated sampling bounds").1
    }
}

/// Explicitly reinterpret a box-constrained objective's bounds as a sampling domain.
///
/// The objective and its derivatives are unchanged. Mathematical bounds are
/// removed, so local searches and accepted results may lie outside the box.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SamplingDomain<P> {
    problem: P,
}
impl<P> SamplingDomain<P> {
    /// Own the problem. Evaluation capabilities require a box-only declaration.
    pub fn new(problem: P) -> Self {
        Self { problem }
    }
    /// Borrow the original problem.
    pub fn inner(&self) -> &P {
        &self.problem
    }
    /// Recover the original problem.
    pub fn into_inner(self) -> P {
        self.problem
    }
}
impl<P> ProblemConstraints for SamplingDomain<P> {
    type Constraints = NoConstraints;
    fn constraint_set(&self) -> NoConstraints {
        NoConstraints::EMPTY
    }
}
impl<P, V> CostFunction for SamplingDomain<P>
where
    P: CostFunction<Param = V> + BoxProblem<Param = V>,
{
    type Param = V;
    type Output = P::Output;
    type Error = P::Error;
    fn cost(&self, x: &V) -> Result<Self::Output, Self::Error> {
        self.problem.cost(x)
    }
}
impl<P, V> SamplingBounds for SamplingDomain<P>
where
    P: CostFunction<Param = V> + BoxProblem<Param = V>,
{
    fn sampling_bounds(&self) -> Result<(&V, &V), ConstraintError> {
        self.problem.box_bounds()
    }
}
/// Explicitly run an unconstrained local solver on a box-constrained problem's objective.
///
/// Use this inside bounded CMA or DE injection when the outer algorithm repairs
/// the local result. This adapter permits exploratory and returned points outside
/// the box. It preserves callback errors and every evaluation category.
/// Robust least-squares solvers retain the loss function and residual scale.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RelaxedInner<I> {
    inner: I,
}
impl<I> RelaxedInner<I> {
    /// Select an unconstrained local relaxation explicitly.
    pub fn new(inner: I) -> Self {
        Self { inner }
    }
    /// Borrow the local solver.
    pub fn inner(&self) -> &I {
        &self.inner
    }
    /// Recover the local solver.
    pub fn into_inner(self) -> I {
        self.inner
    }
}

pub(crate) struct RelaxedProblem<'a, P> {
    pub(crate) problem: &'a P,
}
impl<P> ProblemConstraints for RelaxedProblem<'_, P> {
    type Constraints = NoConstraints;
    fn constraint_set(&self) -> NoConstraints {
        NoConstraints::EMPTY
    }
}
impl<P: CostFunction> CostFunction for RelaxedProblem<'_, P> {
    type Param = P::Param;
    type Output = P::Output;
    type Error = P::Error;
    fn cost(&self, x: &Self::Param) -> Result<Self::Output, Self::Error> {
        self.problem.cost(x)
    }
}
macro_rules! forward_derivatives {
    ($wrapper:ty,$($bound:tt)*)=>{
        impl<P> MiniBatchGradient for $wrapper where P:MiniBatchGradient,$($bound)* {
            type Gradient=P::Gradient;
            fn n_samples(&self)->usize{self.problem.n_samples()}
            fn batch_gradient(&self,x:&Self::Param,batch:&[usize])->Result<Self::Gradient,Self::Error>{self.problem.batch_gradient(x,batch)}
        }
        impl<P> Gradient for $wrapper where P:Gradient,$($bound)* {
            type Gradient=P::Gradient;
            fn gradient(&self,x:&Self::Param)->Result<Self::Gradient,Self::Error>{self.problem.gradient(x)}
            fn cost_and_gradient(&self,x:&Self::Param)->Result<(Self::Output,Self::Gradient),Self::Error>{self.problem.cost_and_gradient(x)}
        }
        impl<P> Hessian for $wrapper where P:Hessian,$($bound)* {
            type Hessian=P::Hessian;
            fn hessian(&self,x:&Self::Param)->Result<Self::Hessian,Self::Error>{self.problem.hessian(x)}
            fn cost_and_gradient_and_hessian(&self,x:&Self::Param)->Result<(Self::Output,Self::Gradient,Self::Hessian),Self::Error>{self.problem.cost_and_gradient_and_hessian(x)}
        }
        impl<P> HessianProduct for $wrapper where P:HessianProduct,$($bound)* {
            fn hessian_product(&self,x:&Self::Param,v:&Self::Param)->Result<Self::Gradient,Self::Error>{self.problem.hessian_product(x,v)}
        }
    }
}
forward_derivatives!(RelaxedProblem<'_, P>,);
forward_derivatives!(SamplingDomain<P>,P:BoxProblem<Param=<P as CostFunction>::Param>,);
macro_rules! forward_residuals {
    ($wrapper:ty,$($bound:tt)*)=>{
        impl<P> Residual for $wrapper where P:Residual,$($bound)* {
            type Param=<P as Residual>::Param;type Output=P::Output;type Error=P::Error;
            fn residual(&self,x:&Self::Param)->Result<Self::Output,Self::Error>{self.problem.residual(x)}
        }
        impl<P> Jacobian for $wrapper where P:Jacobian,$($bound)* {
            type Jacobian=P::Jacobian;
            fn jacobian(&self,x:&Self::Param)->Result<Self::Jacobian,Self::Error>{self.problem.jacobian(x)}
            fn residual_and_jacobian(&self,x:&Self::Param)->Result<(Self::Output,Self::Jacobian),Self::Error>{self.problem.residual_and_jacobian(x)}
        }
    }
}
forward_residuals!(RelaxedProblem<'_, P>,);
forward_residuals!(SamplingDomain<P>, P:BoxProblem<Param=<P as Residual>::Param>,);

impl<I, V> InitialState<V> for RelaxedInner<I>
where
    I: InitialState<V>,
{
    type State = I::State;
    fn seed(&self, x: &V) -> Self::State {
        self.inner.seed(x)
    }
}
impl<I, V> WarmStart<V> for RelaxedInner<I> where I: WarmStart<V> {}
impl<I, V, F: Scalar> crate::solver::cma_inject::MemeticInner<V, F>
    for RelaxedInner<I>
where
    I: crate::solver::cma_inject::MemeticInner<V, F>,
{
    fn seed_scaled(&mut self, x: &V, sigma: F) -> Self::State {
        self.inner.seed_scaled(x, sigma)
    }
}

impl<P, S, I> Solver<P, S> for RelaxedInner<I>
where
    P: CostFunction,
    P::Constraints: BoxOnly<P::Param>,
    S: State,
    I: for<'a> Solver<RelaxedProblem<'a, P>, S, Error = P::Error>,
{
    type Error = ConstraintSolveError<P::Error>;
    fn init(&mut self, p: &mut Problem<P>, state: S) -> Result<S, Self::Error> {
        let mut view = Problem::new(RelaxedProblem { problem: p.inner() });
        *view.counts_mut() = *p.counts();
        let result = self.inner.init(&mut view, state);
        let counts = *view.counts();
        *p.counts_mut() = counts;
        result.map_err(ConstraintSolveError::Evaluation)
    }
    fn next_iter(
        &mut self,
        p: &mut Problem<P>,
        state: S,
    ) -> Result<SolverStep<S>, Self::Error> {
        let mut view = Problem::new(RelaxedProblem { problem: p.inner() });
        *view.counts_mut() = *p.counts();
        let result = self.inner.next_iter(&mut view, state);
        let counts = *view.counts();
        *p.counts_mut() = counts;
        result.map_err(ConstraintSolveError::Evaluation)
    }
    fn terminate(&self, state: &S) -> Option<Termination<S::Float>> {
        <I as Solver<RelaxedProblem<'_, P>, S>>::terminate(&self.inner, state)
    }
    fn reset_convergence(&mut self) {
        <I as Solver<RelaxedProblem<'_, P>, S>>::reset_convergence(
            &mut self.inner,
        )
    }
    fn should_check_iterate_change(&self) -> bool {
        <I as Solver<RelaxedProblem<'_, P>, S>>::should_check_iterate_change(
            &self.inner,
        )
    }
    fn check_convergence(
        &mut self,
        p: &Problem<P>,
        state: &S,
    ) -> Option<Termination<S::Float>> {
        let mut view = Problem::new(RelaxedProblem { problem: p.inner() });
        *view.counts_mut() = *p.counts();
        self.inner.check_convergence(&view, state)
    }
}

pub(crate) fn validate_sampling<P, V, F>(
    p: &P,
    n: Option<usize>,
) -> Result<(), ConstraintSolveError<P::Error>>
where
    P: SamplingBounds<Param = V, Output = F>,
    V: crate::VectorLen + std::ops::Index<usize, Output = F>,
    F: Scalar,
{
    let (lower, upper) = p.sampling_bounds().map_err(invalid)?;
    let n = n.unwrap_or_else(|| lower.vec_len());
    super::model::dimension(
        ConstraintBlock::Bounds,
        ConstraintAxis::Length,
        n,
        lower.vec_len(),
    )
    .map_err(invalid)?;
    super::model::dimension(
        ConstraintBlock::Bounds,
        ConstraintAxis::Length,
        n,
        upper.vec_len(),
    )
    .map_err(invalid)?;
    for i in 0..n {
        super::model::validate_bound(lower[i], BoundSide::Lower, i)
            .map_err(invalid)?;
        super::model::validate_bound(upper[i], BoundSide::Upper, i)
            .map_err(invalid)?;
        if lower[i] > upper[i] {
            return Err(invalid(ConstraintError::InvertedBounds {
                coordinate: i,
            }));
        }
        if !lower[i].is_finite() || !upper[i].is_finite() {
            return Err(invalid(ConstraintError::NonFiniteInterval {
                coordinate: i,
            }));
        }
    }
    Ok(())
}

/// Attach a canonical constraint set to an unconstrained objective or residual problem.
///
/// Evaluation capabilities are forwarded without copying the problem data. The
/// wrapped problem must declare [`NoConstraints`], so this operation cannot
/// silently discard an existing constraint block.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Constrained<P, C> {
    problem: P,
    constraints: C,
}
impl<P, C> Constrained<P, C>
where
    P: ProblemConstraints<Constraints = NoConstraints>,
    C: ConstraintDescription,
{
    /// Own the objective and its complete constraint declaration.
    pub fn new(problem: P, constraints: C) -> Self {
        Self {
            problem,
            constraints,
        }
    }
    /// Borrow the original problem.
    pub fn inner(&self) -> &P {
        &self.problem
    }
    /// Recover the problem and constraint set.
    pub fn into_parts(self) -> (P, C) {
        (self.problem, self.constraints)
    }
}
impl<P, C: ConstraintDescription> ProblemConstraints for Constrained<P, C> {
    type Constraints = C;
    fn constraint_set(&self) -> C::Borrowed<'_> {
        self.constraints.borrowed()
    }
}
impl<P, C> CostFunction for Constrained<P, C>
where
    P: CostFunction<Constraints = NoConstraints>,
    C: ConstraintDescription,
{
    type Param = P::Param;
    type Output = P::Output;
    type Error = P::Error;
    fn cost(&self, x: &Self::Param) -> Result<Self::Output, Self::Error> {
        self.problem.cost(x)
    }
}
impl<P, C> Residual for Constrained<P, C>
where
    P: Residual<Constraints = NoConstraints>,
    C: ConstraintDescription,
{
    type Param = P::Param;
    type Output = P::Output;
    type Error = P::Error;
    fn residual(&self, x: &Self::Param) -> Result<Self::Output, Self::Error> {
        self.problem.residual(x)
    }
}
impl<P, C> Jacobian for Constrained<P, C>
where
    P: Jacobian<Constraints = NoConstraints>,
    C: ConstraintDescription,
{
    type Jacobian = P::Jacobian;
    fn jacobian(&self, x: &Self::Param) -> Result<Self::Jacobian, Self::Error> {
        self.problem.jacobian(x)
    }
    fn residual_and_jacobian(
        &self,
        x: &Self::Param,
    ) -> Result<(Self::Output, Self::Jacobian), Self::Error> {
        self.problem.residual_and_jacobian(x)
    }
}
macro_rules! constrained_values {
    ($cap:ident,$method:ident,$value:ident) => {
        impl<P, C> $cap for Constrained<P, C>
        where
            P: $cap<Constraints = NoConstraints>,
            C: ConstraintDescription,
        {
            type Param = P::Param;
            type $value = P::$value;
            type Error = P::Error;
            fn $method(
                &self,
                x: &Self::Param,
            ) -> Result<Self::$value, ConstraintSolveError<Self::Error>> {
                self.problem.$method(x)
            }
        }
    };
}
constrained_values!(NonlinearEqualityValues, nonlinear_equalities, Values);
constrained_values!(NonlinearInequalityValues, nonlinear_inequalities, Values);
constrained_values!(ConstraintJacobian, constraint_jacobian, Jacobian);

impl<P, C> MiniBatchGradient for Constrained<P, C>
where
    P: MiniBatchGradient<Constraints = NoConstraints>,
    C: ConstraintDescription,
{
    type Gradient = P::Gradient;
    fn n_samples(&self) -> usize {
        self.problem.n_samples()
    }
    fn batch_gradient(
        &self,
        x: &Self::Param,
        batch: &[usize],
    ) -> Result<Self::Gradient, Self::Error> {
        self.problem.batch_gradient(x, batch)
    }
}

impl<P, C> Gradient for Constrained<P, C>
where
    P: Gradient<Constraints = NoConstraints>,
    C: ConstraintDescription,
{
    type Gradient = P::Gradient;
    fn gradient(&self, x: &Self::Param) -> Result<Self::Gradient, Self::Error> {
        self.problem.gradient(x)
    }
    fn cost_and_gradient(
        &self,
        x: &Self::Param,
    ) -> Result<(Self::Output, Self::Gradient), Self::Error> {
        self.problem.cost_and_gradient(x)
    }
}
impl<P, C> Hessian for Constrained<P, C>
where
    P: Hessian<Constraints = NoConstraints>,
    C: ConstraintDescription,
{
    type Hessian = P::Hessian;
    fn hessian(&self, x: &Self::Param) -> Result<Self::Hessian, Self::Error> {
        self.problem.hessian(x)
    }
    fn cost_and_gradient_and_hessian(
        &self,
        x: &Self::Param,
    ) -> Result<(Self::Output, Self::Gradient, Self::Hessian), Self::Error>
    {
        self.problem.cost_and_gradient_and_hessian(x)
    }
}
impl<P, C> HessianProduct for Constrained<P, C>
where
    P: HessianProduct<Constraints = NoConstraints>,
    C: ConstraintDescription,
{
    fn hessian_product(
        &self,
        x: &Self::Param,
        v: &Self::Param,
    ) -> Result<Self::Gradient, Self::Error> {
        self.problem.hessian_product(x, v)
    }
}
impl<I: crate::NativeConvergenceDiagnostics> crate::NativeConvergenceDiagnostics
    for RelaxedInner<I>
{
    fn native_convergence_tests(&self) -> &[crate::NativeConvergenceTest] {
        self.inner.native_convergence_tests()
    }
}