Mon, 06 Jan 2025 21:37:03 -0500
Attempt to do more Serialize / Deserialize but run into csv problems
| 
37
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
1 | /*! | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
2 | Basic proximal penalty based on convolution operators $𝒟$. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
3 | */ | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
4 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
5 | use numeric_literals::replace_float_literals; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
6 | use nalgebra::DVector; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
7 | use colored::Colorize; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
8 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
9 | use alg_tools::types::*; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
10 | use alg_tools::loc::Loc; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
11 | use alg_tools::mapping::{Mapping, RealMapping}; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
12 | use alg_tools::nalgebra_support::ToNalgebraRealField; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
13 | use alg_tools::norms::Linfinity; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
14 | use alg_tools::iterate::{ | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
15 | AlgIteratorIteration, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
16 | AlgIterator, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
17 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
18 | use alg_tools::bisection_tree::{ | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
19 | BTFN, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
20 | PreBTFN, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
21 | Bounds, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
22 | BTSearch, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
23 | SupportGenerator, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
24 | LocalAnalysis, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
25 | BothGenerators, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
26 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
27 | use crate::measures::{ | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
28 | RNDM, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
29 | DeltaMeasure, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
30 | Radon, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
31 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
32 | use crate::measures::merging::{ | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
33 | SpikeMerging, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
34 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
35 | use crate::seminorms::DiscreteMeasureOp; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
36 | use crate::types::{ | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
37 | IterInfo, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
38 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
39 | use crate::measures::merging::SpikeMergingMethod; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
40 | use crate::regularisation::RegTerm; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
41 | use super::{ProxPenalty, FBGenericConfig}; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
42 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
43 | #[replace_float_literals(F::cast_from(literal))] | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
44 | impl<F, GA, BTA, S, Reg, 𝒟, G𝒟, K, const N : usize> | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
45 | ProxPenalty<F, BTFN<F, GA, BTA, N>, Reg, N> for 𝒟 | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
46 | where | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
47 | F : Float + ToNalgebraRealField, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
48 | GA : SupportGenerator<F, N, SupportType = S, Id = usize> + Clone, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
49 | BTA : BTSearch<F, N, Data=usize, Agg=Bounds<F>>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
50 | S: RealMapping<F, N> + LocalAnalysis<F, Bounds<F>, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
51 | G𝒟 : SupportGenerator<F, N, SupportType = K, Id = usize> + Clone, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
52 | 𝒟 : DiscreteMeasureOp<Loc<F, N>, F, PreCodomain = PreBTFN<F, G𝒟, N>>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
53 | 𝒟::Codomain : RealMapping<F, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
54 | K : RealMapping<F, N> + LocalAnalysis<F, Bounds<F>, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
55 | Reg : RegTerm<F, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
56 | RNDM<F, N> : SpikeMerging<F>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
57 | { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
58 | type ReturnMapping = BTFN<F, BothGenerators<GA, G𝒟>, BTA, N>; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
59 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
60 | fn insert_and_reweigh<I>( | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
61 | &self, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
62 | μ : &mut RNDM<F, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
63 | τv : &mut BTFN<F, GA, BTA, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
64 | μ_base : &RNDM<F, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
65 | ν_delta: Option<&RNDM<F, N>>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
66 | τ : F, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
67 | ε : F, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
68 | config : &FBGenericConfig<F>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
69 | reg : &Reg, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
70 | state : &AlgIteratorIteration<I>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
71 | stats : &mut IterInfo<F, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
72 | ) -> (Option<BTFN<F, BothGenerators<GA, G𝒟>, BTA, N>>, bool) | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
73 | where | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
74 | I : AlgIterator | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
75 | { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
76 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
77 | // TODO: is this inefficient to do in every iteration? | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
78 | let op𝒟norm = self.opnorm_bound(Radon, Linfinity); | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
79 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
80 | // Maximum insertion count and measure difference calculation depend on insertion style. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
81 | let (max_insertions, warn_insertions) = match (state.iteration(), config.bootstrap_insertions) { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
82 | (i, Some((l, k))) if i <= l => (k, false), | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
83 | _ => (config.max_insertions, !state.is_quiet()), | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
84 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
85 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
86 | let ω0 = match ν_delta { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
87 | None => self.apply(μ_base), | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
88 | Some(ν) => self.apply(μ_base + ν), | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
89 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
90 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
91 | // Add points to support until within error tolerance or maximum insertion count reached. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
92 | let mut count = 0; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
93 | let (within_tolerances, d) = 'insertion: loop { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
94 | if μ.len() > 0 { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
95 | // Form finite-dimensional subproblem. The subproblem references to the original μ^k | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
96 | // from the beginning of the iteration are all contained in the immutable c and g. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
97 | // TODO: observe negation of -τv after switch from minus_τv: finite-dimensional | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
98 | // problems have not yet been updated to sign change. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
99 | let à = self.findim_matrix(μ.iter_locations()); | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
100 | let g̃ = DVector::from_iterator(μ.len(), | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
101 | μ.iter_locations() | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
102 | .map(|ζ| ω0.apply(ζ) - τv.apply(ζ)) | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
103 | .map(F::to_nalgebra_mixed)); | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
104 | let mut x = μ.masses_dvector(); | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
105 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
106 | // The gradient of the forward component of the inner objective is C^*𝒟Cx - g̃. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
107 | // We have |C^*𝒟Cx|_2 = sup_{|z|_2 ≤ 1} ⟨z, C^*𝒟Cx⟩ = sup_{|z|_2 ≤ 1} ⟨Cz|𝒟Cx⟩ | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
108 | // ≤ sup_{|z|_2 ≤ 1} |Cz|_ℳ |𝒟Cx|_∞ ≤ sup_{|z|_2 ≤ 1} |Cz|_ℳ |𝒟| |Cx|_ℳ | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
109 | // ≤ sup_{|z|_2 ≤ 1} |z|_1 |𝒟| |x|_1 ≤ sup_{|z|_2 ≤ 1} n |z|_2 |𝒟| |x|_2 | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
110 | // = n |𝒟| |x|_2, where n is the number of points. Therefore | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
111 | let Ã_normest = op𝒟norm * F::cast_from(μ.len()); | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
112 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
113 | // Solve finite-dimensional subproblem. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
114 | stats.inner_iters += reg.solve_findim(&Ã, &g̃, τ, &mut x, Ã_normest, ε, config); | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
115 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
116 | // Update masses of μ based on solution of finite-dimensional subproblem. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
117 | μ.set_masses_dvector(&x); | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
118 | } | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
119 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
120 | // Form d = τv + 𝒟μ - ω0 = τv + 𝒟(μ - μ^k) for checking the proximate optimality | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
121 | // conditions in the predual space, and finding new points for insertion, if necessary. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
122 | let mut d = &*τv + match ν_delta { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
123 | None => self.preapply(μ.sub_matching(μ_base)), | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
124 | Some(ν) => self.preapply(μ.sub_matching(μ_base) - ν) | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
125 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
126 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
127 | // If no merging heuristic is used, let's be more conservative about spike insertion, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
128 | // and skip it after first round. If merging is done, being more greedy about spike | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
129 | // insertion also seems to improve performance. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
130 | let skip_by_rough_check = if let SpikeMergingMethod::None = config.merging { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
131 | false | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
132 | } else { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
133 | count > 0 | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
134 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
135 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
136 | // Find a spike to insert, if needed | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
137 | let (ξ, _v_ξ, in_bounds) = match reg.find_tolerance_violation( | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
138 | &mut d, τ, ε, skip_by_rough_check, config | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
139 | ) { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
140 | None => break 'insertion (true, d), | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
141 | Some(res) => res, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
142 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
143 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
144 | // Break if maximum insertion count reached | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
145 | if count >= max_insertions { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
146 | break 'insertion (in_bounds, d) | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
147 | } | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
148 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
149 | // No point in optimising the weight here; the finite-dimensional algorithm is fast. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
150 | *μ += DeltaMeasure { x : ξ, α : 0.0 }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
151 | count += 1; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
152 | stats.inserted += 1; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
153 | }; | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
154 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
155 | if !within_tolerances && warn_insertions { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
156 | // Complain (but continue) if we failed to get within tolerances | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
157 | // by inserting more points. | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
158 | let err = format!("Maximum insertions reached without achieving \ | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
159 | subproblem solution tolerance"); | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
160 | println!("{}", err.red()); | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
161 | } | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
162 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
163 | (Some(d), within_tolerances) | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
164 | } | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
165 | |
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
166 | fn merge_spikes( | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
167 | &self, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
168 | μ : &mut RNDM<F, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
169 | τv : &mut BTFN<F, GA, BTA, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
170 | μ_base : &RNDM<F, N>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
171 | τ : F, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
172 | ε : F, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
173 | config : &FBGenericConfig<F>, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
174 | reg : &Reg, | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
175 | ) -> usize | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
176 | { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
177 | μ.merge_spikes(config.merging, |μ_candidate| { | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
178 | let mut d = &*τv + self.preapply(μ_candidate.sub_matching(μ_base)); | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
179 | reg.verify_merge_candidate(&mut d, μ_candidate, τ, ε, config) | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
180 | }) | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
181 | } | 
| 
 
c5d8bd1a7728
Generic proximal penalty support
 
Tuomo Valkonen <tuomov@iki.fi> 
parents:  
diff
changeset
 | 
182 | } |