src/main.rs

changeset 24
8b4b014277fa
parent 20
bd6fd49146ce
child 25
9ac11616a2c5
equal deleted inserted replaced
23:b719a44b0b26 24:8b4b014277fa
22 use alg_tools::lingrid::LinSpace; 22 use alg_tools::lingrid::LinSpace;
23 use alg_tools::loc::Loc; 23 use alg_tools::loc::Loc;
24 use alg_tools::types::*; 24 use alg_tools::types::*;
25 use alg_tools::mapping::{Sum, Apply}; 25 use alg_tools::mapping::{Sum, Apply};
26 use alg_tools::iterate::{AlgIteratorOptions, AlgIteratorFactory, Verbose}; 26 use alg_tools::iterate::{AlgIteratorOptions, AlgIteratorFactory, Verbose};
27 use alg_tools::mapping::Mapping;
27 use image::{ImageFormat, ImageBuffer, Rgb}; 28 use image::{ImageFormat, ImageBuffer, Rgb};
28 29
29 use dist::{DistTo, DistToSquaredDiv2}; 30 use dist::{DistTo, DistToSquaredDiv2};
30 use fb::{forward_backward, IterInfo}; 31 use fb::{forward_backward, IterInfo, Desc, Prox};
31 use manifold::EmbeddedManifoldPoint; 32 use manifold::EmbeddedManifoldPoint;
32 use cube::*; 33 use cube::*;
33 use Face::*; 34 use Face::*;
34 #[allow(unused_imports)] 35 #[allow(unused_imports)]
35 use zero::ZeroFn; 36 use zero::ZeroFn;
95 write_points(format!("{PREFIX}/data"), points.iter())?; 96 write_points(format!("{PREFIX}/data"), points.iter())?;
96 write_points(format!("{PREFIX}/origin"), std::iter::once(&origin))?; 97 write_points(format!("{PREFIX}/origin"), std::iter::once(&origin))?;
97 98
98 //let x = points[0].clone(); 99 //let x = points[0].clone();
99 // OnCube::new(F3, Loc([0.5, 0.5])); goes to opposite side 100 // OnCube::new(F3, Loc([0.5, 0.5])); goes to opposite side
100 let x = OnCube::new(F3, Loc([0.1, 0.7]));
101 let f = Sum::new(points.into_iter().map(DistToSquaredDiv2)); 101 let f = Sum::new(points.into_iter().map(DistToSquaredDiv2));
102 //let g = ZeroFn::new(); 102 //let g = ZeroFn::new();
103 let g = Scaled::new(0.5, DistTo(origin)); 103 let g = Scaled::new(0.5, DistTo(origin));
104 let τ = 0.05; 104 let τ = 0.05;
105
106 std::fs::create_dir_all(PREFIX)?;
107 for face in Face::all() {
108 write_face_csv(format!("{PREFIX}/{face}"), face, 32, |x| f.apply(x) + g.apply(x))?;
109 write_face_img(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?;
110 }
111
112 run_and_save("x1", &f, &g, OnCube::new(F3, Loc([0.1, 0.7])), τ)?;
113 run_and_save("x2", &f, &g, OnCube::new(F2, Loc([0.1, 0.7])), τ)?;
114 run_and_save("x3", &f, &g, OnCube::new(F6, Loc([0.6, 0.2])), τ)
115 }
116
117 pub fn run_and_save<F, G>(
118 name : &str,
119 f : &F,
120 g : &G,
121 x : OnCube,
122 τ : f64,
123 ) -> DynError
124 where F : Desc<OnCube> + Mapping<OnCube, Codomain = f64>,
125 G : Prox<OnCube> + Mapping<OnCube, Codomain = f64> {
105 126
106 let mut logger = Logger::new(); 127 let mut logger = Logger::new();
107 let logmap = |iter, IterInfo { value, point } : IterInfo<OnCube>| { 128 let logmap = |iter, IterInfo { value, point } : IterInfo<OnCube>| {
108 let CSVPoint {x , y, z, face} = CSVPoint::from(&point); 129 let CSVPoint {x , y, z, face} = CSVPoint::from(&point);
109 CSVLog { 130 CSVLog {
116 verbose_iter : Verbose::Every(1), 137 verbose_iter : Verbose::Every(1),
117 .. Default::default() 138 .. Default::default()
118 }.mapped(logmap) 139 }.mapped(logmap)
119 .into_log(&mut logger); 140 .into_log(&mut logger);
120 141
121 let x̂ = forward_backward(&f, &g, x, τ, iter); 142 let x̂ = forward_backward(f, g, x, τ, iter);
122 println!("result = {}\n{:?}", x̂.embedded_coords(), &x̂); 143 println!("result = {}\n{:?}", x̂.embedded_coords(), &x̂);
123 144
124 std::fs::create_dir_all(PREFIX)?; 145 logger.write_csv(format!("{PREFIX}/{name}_log.csv"))?;
125
126 logger.write_csv(format!("{PREFIX}/log.txt"))?;
127
128 for face in Face::all() {
129 write_face_csv(format!("{PREFIX}/{face}"), face, 32, |x| f.apply(x) + g.apply(x))?;
130 write_face_img(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?;
131 }
132 146
133 Ok(()) 147 Ok(())
134 } 148 }
135 149
136 /// Writes the values of `f` on `face` of a [`OnCube`] into a PNG file 150 /// Writes the values of `f` on `face` of a [`OnCube`] into a PNG file

mercurial