Tue, 22 Oct 2024 09:34:12 -0500
Fixes to find_crossing and exp
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; |
7 | 12 | use dist::DistToSquaredDiv2; |
12 | 13 | use fb::{forward_backward, IterInfo}; |
7 | 14 | use manifold::EmbeddedManifoldPoint; |
12 | 15 | use alg_tools::logger::Logger; |
16 | use alg_tools::tabledump::TableDump; | |
17 | use alg_tools::error::DynError; | |
11 | 18 | use cube::*; |
19 | use image::{ | |
20 | ImageFormat, | |
21 | ImageBuffer, | |
22 | Rgb | |
23 | }; | |
7 | 24 | |
1 | 25 | mod manifold; |
4 | 26 | mod fb; |
1 | 27 | mod cube; |
5 | 28 | mod dist; |
6
df9628092285
Add a zero function on manifolds
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
29 | mod zero; |
1 | 30 | |
13 | 31 | /// Program entry point |
1 | 32 | fn main() { |
12 | 33 | simple_cube_test().unwrap() |
7 | 34 | } |
1 | 35 | |
12 | 36 | /// Helper structure for saving the log into a CSV file |
37 | #[derive(Serialize)] | |
38 | struct CSVLog { | |
39 | iter : usize, | |
40 | value : f64, | |
41 | face : Face, | |
42 | x : f64, | |
43 | y : f64, | |
44 | z : f64 | |
45 | } | |
46 | ||
13 | 47 | /// Location for saving results |
12 | 48 | static PREFIX : &str = "res"; |
49 | ||
50 | /// A simple test on the cube | |
51 | fn simple_cube_test() -> DynError { | |
7 | 52 | use alg_tools::loc::Loc; |
53 | use Face::*; | |
54 | use zero::ZeroFn; | |
11 | 55 | use alg_tools::mapping::{Sum, Apply}; |
12 | 56 | use alg_tools::iterate::{AlgIteratorOptions, AlgIteratorFactory, Verbose}; |
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() { |
12 | 95 | write_face(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?; |
11 | 96 | } |
12 | 97 | |
98 | Ok(()) | |
1 | 99 | } |
11 | 100 | |
12 | 101 | /// Writes the values of `f` on `face` of a [`OnCube`] into a PNG file |
102 | /// with resolution `n × n`. | |
103 | fn write_face(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) -> DynError { | |
11 | 104 | use alg_tools::lingrid::LinSpace; |
105 | use alg_tools::loc::Loc; | |
106 | use alg_tools::types::*; | |
107 | ||
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 | } |