Skip to main content

fpm_rs/algorithms/
mod.rs

1mod admm;
2mod alternating_projection;
3mod common;
4mod epry;
5mod fpie;
6mod gradient_descent;
7pub mod objective;
8mod regularization;
9
10pub use admm::Admm;
11pub use alternating_projection::AlternatingProjection;
12pub use epry::Epry;
13pub use fpie::Fpie;
14pub use gradient_descent::GradientDescent;
15
16use crate::{
17    Result,
18    backend::Backend,
19    callbacks::Callback,
20    diagnostics::StepDiagnostics,
21    measurements::MeasurementRead,
22    reconstruction::{
23        Batch, ReconstructionCheckpoint, ReconstructionProblem, ReconstructionResult,
24        ReconstructionState, RunOptions, Runner,
25    },
26};
27use std::sync::Arc;
28
29pub trait ReconstructionAlgorithm {
30    fn validate(&self) -> Result<()> {
31        Ok(())
32    }
33
34    fn validate_problem<M: MeasurementRead>(
35        &self,
36        _problem: &ReconstructionProblem<M>,
37    ) -> Result<()> {
38        Ok(())
39    }
40
41    fn initialize<M: MeasurementRead>(
42        &self,
43        problem: &ReconstructionProblem<M>,
44    ) -> Result<ReconstructionState> {
45        ReconstructionState::initialize(problem)
46    }
47
48    fn initialize_with_backend<M: MeasurementRead>(
49        &self,
50        problem: &ReconstructionProblem<M>,
51        backend: Arc<dyn Backend>,
52    ) -> Result<ReconstructionState> {
53        ReconstructionState::initialize_with_backend(problem, backend)
54    }
55
56    fn step<M: MeasurementRead>(
57        &mut self,
58        problem: &ReconstructionProblem<M>,
59        state: &mut ReconstructionState,
60        batch: &Batch,
61        iteration: usize,
62    ) -> Result<StepDiagnostics>;
63
64    fn iterations(&self) -> usize;
65
66    fn batch_size(&self) -> usize {
67        1
68    }
69
70    fn run<M: MeasurementRead>(
71        self,
72        problem: &ReconstructionProblem<M>,
73    ) -> Result<ReconstructionResult>
74    where
75        Self: Sized,
76    {
77        let options = RunOptions {
78            max_iterations: self.iterations(),
79            batch_size: self.batch_size(),
80            ..RunOptions::default()
81        };
82        Runner::new(self, options).run(problem)
83    }
84
85    fn run_with_callbacks<M: MeasurementRead>(
86        self,
87        problem: &ReconstructionProblem<M>,
88        callbacks: Vec<Box<dyn Callback>>,
89    ) -> Result<ReconstructionResult>
90    where
91        Self: Sized,
92    {
93        let options = RunOptions {
94            max_iterations: self.iterations(),
95            batch_size: self.batch_size(),
96            ..RunOptions::default()
97        };
98        Runner::new(self, options)
99            .with_callbacks(callbacks)
100            .run(problem)
101    }
102
103    fn run_from_checkpoint<M: MeasurementRead>(
104        self,
105        problem: &ReconstructionProblem<M>,
106        checkpoint: ReconstructionCheckpoint,
107    ) -> Result<ReconstructionResult>
108    where
109        Self: Sized,
110    {
111        let options = RunOptions {
112            max_iterations: self.iterations(),
113            batch_size: self.batch_size(),
114            ..RunOptions::default()
115        };
116        Runner::new(self, options)
117            .resume_from(checkpoint)
118            .run(problem)
119    }
120}