basin 0.4.0

An optimization library for Rust
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
use rand::{Rng, RngExt};
use rand_distr::{Distribution, StandardNormal};

use super::cl_scaling::{
    cl_scaling_pair, max_feasible_step_component, project_strictly_inside_component,
    BoxAffineScaling,
};
use super::sample::{SampleStandardNormal, SampleUniformBox};
use super::{
    ClampInPlace, ComponentDivAssign, ComponentMaxAssign, ComponentMulAssign, Dot,
    FloorZerosInPlace, NegInPlace, NormInfinity, NormSquared, ScaleInPlace, ScaledAdd, VectorIndex,
    VectorLen,
};

impl ScaledAdd<f64> for Vec<f64> {
    fn scaled_add(&mut self, scalar: f64, other: &Self) {
        assert_eq!(self.len(), other.len(), "scaled_add: length mismatch");
        for (x, y) in self.iter_mut().zip(other.iter()) {
            *x += scalar * y;
        }
    }
}

impl NormSquared for Vec<f64> {
    fn norm_squared(&self) -> f64 {
        self.iter().map(|x| x * x).sum()
    }
}

impl NormInfinity for Vec<f64> {
    fn norm_infinity(&self) -> f64 {
        self.iter().map(|x| x.abs()).fold(0.0, f64::max)
    }
}

impl Dot for Vec<f64> {
    fn dot(&self, other: &Self) -> f64 {
        assert_eq!(self.len(), other.len(), "dot: length mismatch");
        self.iter().zip(other.iter()).map(|(a, b)| a * b).sum()
    }
}

impl NegInPlace for Vec<f64> {
    fn neg_in_place(&mut self) {
        for x in self.iter_mut() {
            *x = -*x;
        }
    }
}

impl ScaleInPlace for Vec<f64> {
    fn scale_in_place(&mut self, scalar: f64) {
        for x in self.iter_mut() {
            *x *= scalar;
        }
    }
}

impl ComponentMulAssign for Vec<f64> {
    fn component_mul_assign(&mut self, other: &Self) {
        assert_eq!(
            self.len(),
            other.len(),
            "component_mul_assign: length mismatch"
        );
        for (x, y) in self.iter_mut().zip(other.iter()) {
            *x *= *y;
        }
    }
}

impl ComponentMaxAssign for Vec<f64> {
    fn component_max_assign(&mut self, other: &Self) {
        assert_eq!(
            self.len(),
            other.len(),
            "component_max_assign: length mismatch"
        );
        for (x, y) in self.iter_mut().zip(other.iter()) {
            *x = x.max(*y);
        }
    }
}

impl FloorZerosInPlace for Vec<f64> {
    fn floor_zeros_in_place(&mut self, value: f64) {
        for x in self.iter_mut() {
            if *x <= 0.0 {
                *x = value;
            }
        }
    }
}

impl ComponentDivAssign for Vec<f64> {
    fn component_div_assign(&mut self, other: &Self) {
        assert_eq!(
            self.len(),
            other.len(),
            "component_div_assign: length mismatch"
        );
        for (x, y) in self.iter_mut().zip(other.iter()) {
            *x /= *y;
        }
    }
}

impl VectorLen for Vec<f64> {
    fn vec_len(&self) -> usize {
        self.len()
    }
}

impl VectorIndex for Vec<f64> {
    fn get_scalar(&self, i: usize) -> f64 {
        self[i]
    }
    fn set_scalar(&mut self, i: usize, value: f64) {
        self[i] = value;
    }
}

impl SampleStandardNormal for Vec<f64> {
    fn sample_standard_normal<R: Rng + ?Sized>(template: &Self, rng: &mut R) -> Self {
        let n = template.len();
        let mut out = Vec::with_capacity(n);
        for _ in 0..n {
            out.push(StandardNormal.sample(rng));
        }
        out
    }
}

impl SampleUniformBox for Vec<f64> {
    fn sample_uniform_box<R: Rng + ?Sized>(lower: &Self, upper: &Self, rng: &mut R) -> Self {
        assert_eq!(
            lower.len(),
            upper.len(),
            "sample_uniform_box: bounds length mismatch"
        );
        let n = lower.len();
        let mut out = Vec::with_capacity(n);
        for i in 0..n {
            out.push(rng.random_range(lower[i]..=upper[i]));
        }
        out
    }
}

impl ClampInPlace for Vec<f64> {
    fn clamp_in_place(&mut self, lower: &Self, upper: &Self) {
        assert_eq!(
            self.len(),
            lower.len(),
            "clamp_in_place: lower length mismatch"
        );
        assert_eq!(
            self.len(),
            upper.len(),
            "clamp_in_place: upper length mismatch"
        );
        for ((x, &lo), &hi) in self.iter_mut().zip(lower.iter()).zip(upper.iter()) {
            *x = x.clamp(lo, hi);
        }
    }
}

impl BoxAffineScaling for Vec<f64> {
    fn compute_cl_scaling(
        &self,
        gradient: &Self,
        lower: &Self,
        upper: &Self,
        d_sq: &mut Self,
        c_diag: &mut Self,
    ) {
        let n = self.len();
        assert_eq!(
            n,
            gradient.len(),
            "compute_cl_scaling: gradient length mismatch"
        );
        assert_eq!(n, lower.len(), "compute_cl_scaling: lower length mismatch");
        assert_eq!(n, upper.len(), "compute_cl_scaling: upper length mismatch");
        assert_eq!(n, d_sq.len(), "compute_cl_scaling: d_sq length mismatch");
        assert_eq!(
            n,
            c_diag.len(),
            "compute_cl_scaling: c_diag length mismatch"
        );
        for i in 0..n {
            let (d_sq_i, c_i) = cl_scaling_pair(self[i], gradient[i], lower[i], upper[i]);
            d_sq[i] = d_sq_i;
            c_diag[i] = c_i;
        }
    }

    fn max_feasible_step(&self, step: &Self, lower: &Self, upper: &Self) -> f64 {
        let n = self.len();
        assert_eq!(n, step.len(), "max_feasible_step: step length mismatch");
        assert_eq!(n, lower.len(), "max_feasible_step: lower length mismatch");
        assert_eq!(n, upper.len(), "max_feasible_step: upper length mismatch");
        let mut tau = f64::INFINITY;
        for i in 0..n {
            let t = max_feasible_step_component(self[i], step[i], lower[i], upper[i]);
            if t < tau {
                tau = t;
            }
        }
        tau
    }

    fn cl_kkt_inf_norm(&self, d_sq: &Self) -> f64 {
        assert_eq!(self.len(), d_sq.len(), "cl_kkt_inf_norm: length mismatch");
        self.iter()
            .zip(d_sq.iter())
            .map(|(&v, &d)| v.abs() / d)
            .fold(0.0, f64::max)
    }

    fn weighted_norm_squared(&self, weights: &Self) -> f64 {
        assert_eq!(
            self.len(),
            weights.len(),
            "weighted_norm_squared: length mismatch"
        );
        self.iter()
            .zip(weights.iter())
            .map(|(&v, &w)| v * v * w)
            .sum()
    }

    fn project_strictly_inside(&mut self, lower: &Self, upper: &Self, rstep: f64) {
        let n = self.len();
        assert_eq!(
            n,
            lower.len(),
            "project_strictly_inside: lower length mismatch"
        );
        assert_eq!(
            n,
            upper.len(),
            "project_strictly_inside: upper length mismatch"
        );
        for i in 0..n {
            self[i] = project_strictly_inside_component(self[i], lower[i], upper[i], rstep);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::SeedableRng;
    use rand_chacha::ChaCha8Rng;

    #[test]
    fn sample_uniform_box_stays_within_bounds() {
        let lower = vec![-1.0, 0.5, -10.0];
        let upper = vec![1.0, 0.5, 10.0]; // Middle component pinned (lo == hi).
        let mut rng = ChaCha8Rng::seed_from_u64(123);
        for _ in 0..200 {
            let x = Vec::<f64>::sample_uniform_box(&lower, &upper, &mut rng);
            assert_eq!(x.len(), 3);
            for i in 0..3 {
                assert!(
                    x[i] >= lower[i] && x[i] <= upper[i],
                    "x[{i}] = {} not in [{}, {}]",
                    x[i],
                    lower[i],
                    upper[i]
                );
            }
            // Pinned coordinate is exactly its single bound.
            assert_eq!(x[1], 0.5);
        }
    }

    #[test]
    fn sample_uniform_box_is_seed_reproducible() {
        let lower = vec![-1.0, -1.0];
        let upper = vec![1.0, 1.0];
        let mut rng_a = ChaCha8Rng::seed_from_u64(7);
        let mut rng_b = ChaCha8Rng::seed_from_u64(7);
        let a = Vec::<f64>::sample_uniform_box(&lower, &upper, &mut rng_a);
        let b = Vec::<f64>::sample_uniform_box(&lower, &upper, &mut rng_b);
        assert_eq!(a, b);
    }

    #[test]
    fn clamp_inside_box_is_identity() {
        let mut x = vec![0.5, -0.5, 1.5];
        x.clamp_in_place(&vec![-1.0, -1.0, 0.0], &vec![1.0, 1.0, 2.0]);
        assert_eq!(x, vec![0.5, -0.5, 1.5]);
    }

    #[test]
    fn clamp_partially_outside_pins_only_offending_components() {
        let mut x = vec![-2.0, 0.5, 3.0];
        x.clamp_in_place(&vec![-1.0, -1.0, -1.0], &vec![1.0, 1.0, 1.0]);
        assert_eq!(x, vec![-1.0, 0.5, 1.0]);
    }

    #[test]
    fn clamp_entirely_outside_pins_to_nearest_face() {
        let mut x = vec![-10.0, 10.0];
        x.clamp_in_place(&vec![-1.0, -1.0], &vec![1.0, 1.0]);
        assert_eq!(x, vec![-1.0, 1.0]);
    }

    #[test]
    fn clamp_with_equal_bounds_pins_to_value() {
        let mut x = vec![5.0, -5.0];
        x.clamp_in_place(&vec![1.0, 2.0], &vec![1.0, 2.0]);
        assert_eq!(x, vec![1.0, 2.0]);
    }

    #[test]
    fn cl_scaling_finite_bounds_negative_gradient_uses_upper() {
        // x = 0.5, g = -1, bounds [-2, 2].
        // Case (i): v = x - u = -1.5, |v| = 1.5, d_sq = 1/1.5, c = 1/1.5.
        let x = vec![0.5];
        let g = vec![-1.0];
        let lower = vec![-2.0];
        let upper = vec![2.0];
        let mut d_sq = vec![0.0];
        let mut c = vec![0.0];
        x.compute_cl_scaling(&g, &lower, &upper, &mut d_sq, &mut c);
        assert!((d_sq[0] - (1.0 / 1.5)).abs() < 1e-12);
        assert!((c[0] - (1.0 / 1.5)).abs() < 1e-12);
    }

    #[test]
    fn cl_scaling_finite_bounds_positive_gradient_uses_lower() {
        // x = 0.5, g = 2, bounds [-2, 2].
        // Case (ii): v = x - l = 2.5, |v| = 2.5, d_sq = 1/2.5, c = 2/2.5.
        let x = vec![0.5];
        let g = vec![2.0];
        let lower = vec![-2.0];
        let upper = vec![2.0];
        let mut d_sq = vec![0.0];
        let mut c = vec![0.0];
        x.compute_cl_scaling(&g, &lower, &upper, &mut d_sq, &mut c);
        assert!((d_sq[0] - (1.0 / 2.5)).abs() < 1e-12);
        assert!((c[0] - (2.0 / 2.5)).abs() < 1e-12);
    }

    #[test]
    fn cl_scaling_infinite_bounds_yields_unit_d_and_zero_c() {
        // Both bounds infinite: d_sq = 1, c = 0 (cases iii / iv).
        // Effectively reduces to LM (D = I, C = 0).
        let x = vec![0.0, 0.0];
        let g = vec![-1.0, 1.0];
        let lower = vec![f64::NEG_INFINITY, f64::NEG_INFINITY];
        let upper = vec![f64::INFINITY, f64::INFINITY];
        let mut d_sq = vec![0.0; 2];
        let mut c = vec![0.0; 2];
        x.compute_cl_scaling(&g, &lower, &upper, &mut d_sq, &mut c);
        assert_eq!(d_sq, vec![1.0, 1.0]);
        assert_eq!(c, vec![0.0, 0.0]);
    }

    #[test]
    fn max_feasible_step_finds_first_binding_component() {
        // x = (0, 0), step = (1, 2), bounds [-1, 1]^2.
        // Component 0: τ_0 = (1 - 0) / 1 = 1.
        // Component 1: τ_1 = (1 - 0) / 2 = 0.5.
        // τ_max = 0.5.
        let x = vec![0.0, 0.0];
        let step = vec![1.0, 2.0];
        let lower = vec![-1.0, -1.0];
        let upper = vec![1.0, 1.0];
        let tau = x.max_feasible_step(&step, &lower, &upper);
        assert!((tau - 0.5).abs() < 1e-12);
    }

    #[test]
    fn max_feasible_step_with_no_binding_bound_is_infinite() {
        // step is zero: no component reaches a bound.
        let x = vec![0.0, 0.0];
        let step = vec![0.0, 0.0];
        let lower = vec![-1.0, -1.0];
        let upper = vec![1.0, 1.0];
        assert_eq!(x.max_feasible_step(&step, &lower, &upper), f64::INFINITY);
    }

    #[test]
    fn cl_kkt_inf_norm_matches_max_abs_g_over_d_sq() {
        // g = (3, -8), d_sq = (1, 4) → |g| / d_sq = (3, 2) → ‖·‖_∞ = 3.
        let g = vec![3.0, -8.0];
        let d_sq = vec![1.0, 4.0];
        assert!((g.cl_kkt_inf_norm(&d_sq) - 3.0).abs() < 1e-12);
    }

    #[test]
    fn cl_kkt_inf_norm_vanishes_at_face_active_point() {
        // Face-active emulation: g_i bounded but d_sq_i = 1/|v_i| huge
        // (because v_i ≈ 0 near the boundary). The KKT metric should
        // go to zero, *not* blow up.
        let g = vec![-16.0, -20.0]; // gradients at corner (1, 1)
        let d_sq = vec![1e10, 1e10]; // d_sq → ∞ as iterate → boundary
        assert!(g.cl_kkt_inf_norm(&d_sq) < 1e-8);
    }
}