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