Tue, 22 Oct 2024 08:27:45 -0500
Save log to a CSV file
4 | 1 | |
7 | 2 | use alg_tools::iterate::{AlgIteratorFactory, LogRepr}; |
4 | 3 | use alg_tools::mapping::{Mapping, Sum}; |
4 | use serde::Serialize; | |
5 | 5 | use std::iter::Sum as SumTrait; |
7 | 6 | use colored::ColoredString; |
7 | use crate::manifold::{EmbeddedManifoldPoint, ManifoldPoint}; | |
4 | 8 | |
5 | 9 | /// Trait for function objects that implement gradients |
10 | pub trait Grad<M : ManifoldPoint> { | |
11 | fn grad(&self, x : &M) -> M::Tangent; | |
12 | } | |
13 | ||
4 | 14 | /// Trait for function objects that implement gradient steps |
15 | pub trait Desc<M : ManifoldPoint> { | |
5 | 16 | fn desc(&self, τ : f64, x : M) -> M; |
17 | } | |
18 | ||
19 | /*impl<M : ManifoldPoint, T : Grad<M>> Desc<M> for T { | |
20 | fn desc(&self, τ : f64, x : M) -> M { | |
21 | x.exp(self.grad(x) * τ) | |
22 | } | |
23 | }*/ | |
24 | ||
25 | impl<M, T > Desc<M> for Sum<M, T> | |
26 | where M : ManifoldPoint, | |
27 | T : Grad<M> + Mapping<M, Codomain=f64>, | |
28 | M::Tangent : SumTrait { | |
29 | fn desc(&self, τ : f64, x : M) -> M { | |
30 | let t : M::Tangent = self.iter() | |
31 | .map(|f| f.grad(&x)) | |
32 | .sum(); | |
33 | x.exp(&(t * τ)) | |
34 | } | |
4 | 35 | } |
36 | ||
37 | /// Trait for function objects that implement proximal steps | |
38 | pub trait Prox<M : ManifoldPoint> { | |
5 | 39 | fn prox(&self, τ : f64, x : M) -> M; |
4 | 40 | } |
41 | ||
12 | 42 | /// This structure is used to store information from algorithm iterations |
4 | 43 | #[derive(Clone,Debug,Serialize)] |
44 | pub struct IterInfo<M> { | |
12 | 45 | /// Function value |
46 | pub value : f64, | |
47 | /// Current iterate | |
48 | pub point : M, | |
4 | 49 | } |
50 | ||
7 | 51 | impl<M : ManifoldPoint + EmbeddedManifoldPoint> LogRepr for IterInfo<M> { |
52 | fn logrepr(&self) -> ColoredString { | |
53 | format!("{}\t {}", | |
54 | self.value, | |
55 | self.point.embedded_coords() | |
56 | ).as_str().into() | |
57 | } | |
58 | } | |
59 | ||
12 | 60 | /// The forward-backward method on manifolds. |
61 | /// | |
62 | /// `f` is the smooth, `g` the nonsmooth function, `x` the initial iterate, | |
63 | /// `τ` the step length parameter, and `iterator` controls the iteration count | |
64 | /// and verbosity. Return the final iterate. | |
4 | 65 | pub fn forward_backward<M, F, G, I>( |
66 | f : &F, | |
67 | g : &G, | |
68 | mut x : M, | |
69 | τ : f64, | |
70 | iterator : I | |
71 | ) -> M | |
7 | 72 | where M : ManifoldPoint + EmbeddedManifoldPoint, |
4 | 73 | F : Desc<M> + Mapping<M, Codomain = f64>, |
74 | G : Prox<M> + Mapping<M, Codomain = f64>, | |
75 | I : AlgIteratorFactory<IterInfo<M>> { | |
12 | 76 | |
77 | // Perform as many iterations as requested by `iterator`. | |
4 | 78 | for i in iterator.iter() { |
12 | 79 | // Forward-backward step |
4 | 80 | x = g.prox(τ, f.desc(τ, x)); |
81 | ||
12 | 82 | // If requested by `iterator`, calculate function value and store iterate. |
4 | 83 | i.if_verbose(|| { |
84 | IterInfo { | |
85 | value : f.apply(&x) + g.apply(&x), | |
86 | point : x.clone(), | |
87 | } | |
88 | }) | |
89 | } | |
90 | ||
12 | 91 | // Return final iterate. |
4 | 92 | x |
93 | } |