Wed, 06 Nov 2024 10:19:59 -0500
test change
13 | 1 | /*! |
2 | Optimisation on non-Riemannian manifolds. | |
3 | */ | |
4 | 4 | |
5 | // We use unicode. We would like to use much more of it than Rust allows. | |
6 | // Live with it. Embrace it. | |
7 | #![allow(uncommon_codepoints)] | |
8 | #![allow(mixed_script_confusables)] | |
9 | #![allow(confusable_idents)] | |
1 | 10 | |
19 | 11 | mod manifold; |
12 | mod fb; | |
13 | mod cube; | |
14 | mod dist; | |
15 | mod zero; | |
16 | mod scaled; | |
17 | ||
12 | 18 | use serde::Serialize; |
16 | 19 | use alg_tools::logger::Logger; |
20 | use alg_tools::tabledump::{TableDump, write_csv}; | |
21 | use alg_tools::error::DynError; | |
22 | use alg_tools::lingrid::LinSpace; | |
23 | use alg_tools::loc::Loc; | |
24 | use alg_tools::types::*; | |
25 | use alg_tools::mapping::{Sum, Apply}; | |
26 | use alg_tools::iterate::{AlgIteratorOptions, AlgIteratorFactory, Verbose}; | |
27 | use image::{ImageFormat, ImageBuffer, Rgb}; | |
28 | ||
19 | 29 | use dist::{DistTo, DistToSquaredDiv2}; |
12 | 30 | use fb::{forward_backward, IterInfo}; |
7 | 31 | use manifold::EmbeddedManifoldPoint; |
11 | 32 | use cube::*; |
16 | 33 | use Face::*; |
19 | 34 | #[allow(unused_imports)] |
16 | 35 | use zero::ZeroFn; |
19 | 36 | use scaled::Scaled; |
1 | 37 | |
13 | 38 | /// Program entry point |
1 | 39 | fn main() { |
12 | 40 | simple_cube_test().unwrap() |
7 | 41 | } |
1 | 42 | |
12 | 43 | /// Helper structure for saving the log into a CSV file |
44 | #[derive(Serialize)] | |
45 | struct CSVLog { | |
46 | iter : usize, | |
47 | value : f64, | |
48 | face : Face, | |
49 | x : f64, | |
50 | y : f64, | |
51 | z : f64 | |
52 | } | |
53 | ||
13 | 54 | /// Location for saving results |
12 | 55 | static PREFIX : &str = "res"; |
56 | ||
57 | /// A simple test on the cube | |
58 | fn simple_cube_test() -> DynError { | |
7 | 59 | |
60 | let points = [ | |
9 | 61 | //OnCube::new(F1, Loc([0.5, 0.5])), |
62 | //OnCube::new(F2, Loc([0.5, 0.5])), | |
63 | //OnCube::new(F4, Loc([0.1, 0.1])), | |
7 | 64 | OnCube::new(F1, Loc([0.5, 0.5])), |
9 | 65 | OnCube::new(F3, Loc([0.5, 0.5])), |
7 | 66 | OnCube::new(F2, Loc([0.5, 0.5])), |
67 | ]; | |
68 | ||
69 | //let x = points[0].clone(); | |
9 | 70 | // OnCube::new(F3, Loc([0.5, 0.5])); goes to opposite side |
71 | let x = OnCube::new(F3, Loc([0.5, 0.4])); | |
7 | 72 | let f = Sum::new(points.into_iter().map(DistToSquaredDiv2)); |
19 | 73 | //let g = ZeroFn::new(); |
74 | let g = Scaled::new(1.0, DistTo(OnCube::new(F4, Loc([0.5, 0.5])))); | |
7 | 75 | let τ = 0.1; |
12 | 76 | |
77 | let mut logger = Logger::new(); | |
78 | let logmap = |iter, IterInfo { value, point } : IterInfo<OnCube>| { | |
79 | let Loc([x,y,z]) = point.embedded_coords(); | |
80 | let face = point.face(); | |
81 | CSVLog { iter, value, face, x, y, z } | |
82 | }; | |
7 | 83 | let iter = AlgIteratorOptions{ |
84 | max_iter : 100, | |
85 | verbose_iter : Verbose::Every(1), | |
86 | .. Default::default() | |
12 | 87 | }.mapped(logmap) |
88 | .into_log(&mut logger); | |
7 | 89 | |
90 | let x̂ = forward_backward(&f, &g, x, τ, iter); | |
91 | println!("result = {}\n{:?}", x̂.embedded_coords(), &x̂); | |
11 | 92 | |
12 | 93 | std::fs::create_dir_all(PREFIX)?; |
94 | ||
95 | logger.write_csv(format!("{PREFIX}/log.txt"))?; | |
96 | ||
11 | 97 | for face in Face::all() { |
16 | 98 | write_face_csv(format!("{PREFIX}/{face}"), face, 64, |x| f.apply(x) + g.apply(x))?; |
99 | write_face_img(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?; | |
11 | 100 | } |
12 | 101 | |
102 | Ok(()) | |
1 | 103 | } |
11 | 104 | |
12 | 105 | /// Writes the values of `f` on `face` of a [`OnCube`] into a PNG file |
106 | /// with resolution `n × n`. | |
16 | 107 | fn write_face_img(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) -> DynError { |
11 | 108 | let mut img = ImageBuffer::new(n as u32, n as u32); |
109 | let grid = LinSpace { | |
110 | start : Loc([0.0, 0.0]), | |
111 | end : Loc([1.0, 1.0]), | |
112 | count : [n, n] | |
113 | }; | |
114 | let rawdata : Vec<_> = grid.into_iter() | |
115 | .map(|x| f(&OnCube::new(face, x))) | |
116 | .collect(); | |
117 | let a = rawdata.iter().copied().reduce(f64::max).unwrap(); | |
118 | img.pixels_mut() | |
119 | .zip(rawdata) | |
120 | .for_each(|(p, v)| { | |
121 | let t = v/a; | |
122 | let rgb = [1.0-t, 1.0-t, 1.0]; | |
123 | *p = Rgb(rgb.map(|v| (v*(u8::RANGE_MAX as f64)) as u8)) | |
124 | }); | |
125 | ||
12 | 126 | img.save_with_format(format!("{filename}.png"), ImageFormat::Png)?; |
127 | ||
128 | Ok(()) | |
11 | 129 | } |
16 | 130 | |
131 | /// Writes the values of `f` on `face` of a [`OnCube`] into a CSV file | |
132 | /// with resolution `n × n`. | |
133 | fn write_face_csv(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) -> DynError { | |
134 | ||
135 | #[derive(Serialize)] | |
136 | struct CSVFace { u : f64, v : f64, value : f64 } | |
137 | ||
138 | let grid = LinSpace { | |
139 | start : Loc([0.0, 0.0]), | |
140 | end : Loc([1.0, 1.0]), | |
141 | count : [n, n] | |
142 | }; | |
143 | ||
144 | let data = grid.into_iter() | |
145 | .map(|p@Loc([u,v])| CSVFace{ u, v, value : f(&OnCube::new(face, p)) }); | |
146 | ||
147 | write_csv(data, format!("{filename}.csv"))?; | |
148 | ||
149 | Ok(()) | |
150 | } |