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