Multiple trajectories

Wed, 06 Nov 2024 21:40:54 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 06 Nov 2024 21:40:54 -0500
changeset 24
8b4b014277fa
parent 23
b719a44b0b26
child 25
9ac11616a2c5

Multiple trajectories

src/main.rs file | annotate | diff | comparison | revisions
visualisation/cube.tex file | annotate | diff | comparison | revisions
--- a/src/main.rs	Wed Nov 06 21:12:14 2024 -0500
+++ b/src/main.rs	Wed Nov 06 21:40:54 2024 -0500
@@ -24,10 +24,11 @@
 use alg_tools::types::*;
 use alg_tools::mapping::{Sum, Apply};
 use alg_tools::iterate::{AlgIteratorOptions, AlgIteratorFactory, Verbose};
+use alg_tools::mapping::Mapping;
 use image::{ImageFormat, ImageBuffer, Rgb};
 
 use dist::{DistTo, DistToSquaredDiv2};
-use fb::{forward_backward, IterInfo};
+use fb::{forward_backward, IterInfo, Desc, Prox};
 use manifold::EmbeddedManifoldPoint;
 use cube::*;
 use Face::*;
@@ -97,12 +98,32 @@
 
     //let x = points[0].clone();
     // OnCube::new(F3, Loc([0.5, 0.5])); goes to opposite side
-    let x = OnCube::new(F3, Loc([0.1, 0.7]));
     let f = Sum::new(points.into_iter().map(DistToSquaredDiv2));
     //let g = ZeroFn::new();
     let g = Scaled::new(0.5, DistTo(origin));
     let τ = 0.05;
     
+    std::fs::create_dir_all(PREFIX)?;
+    for face in Face::all() {
+        write_face_csv(format!("{PREFIX}/{face}"), face, 32, |x| f.apply(x) + g.apply(x))?;
+        write_face_img(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?;
+    }
+
+    run_and_save("x1", &f, &g, OnCube::new(F3, Loc([0.1, 0.7])), τ)?;
+    run_and_save("x2", &f, &g, OnCube::new(F2, Loc([0.1, 0.7])), τ)?;
+    run_and_save("x3", &f, &g, OnCube::new(F6, Loc([0.6, 0.2])), τ)
+}
+
+pub fn run_and_save<F, G>(
+    name : &str,
+    f : &F,
+    g : &G,
+    x : OnCube,
+    τ : f64,
+) -> DynError
+where F : Desc<OnCube> +  Mapping<OnCube, Codomain = f64>,
+      G : Prox<OnCube> +  Mapping<OnCube, Codomain = f64> {
+    
     let mut logger = Logger::new();
     let logmap = |iter, IterInfo { value, point } : IterInfo<OnCube>| {
         let CSVPoint {x , y, z, face} = CSVPoint::from(&point);
@@ -118,17 +139,10 @@
     }.mapped(logmap)
      .into_log(&mut logger);
 
-    let x̂ = forward_backward(&f, &g, x, τ, iter);
+    let x̂ = forward_backward(f, g, x, τ, iter);
     println!("result = {}\n{:?}", x̂.embedded_coords(), &x̂);
 
-    std::fs::create_dir_all(PREFIX)?;
-
-    logger.write_csv(format!("{PREFIX}/log.txt"))?;
-
-    for face in Face::all() {
-        write_face_csv(format!("{PREFIX}/{face}"), face, 32, |x| f.apply(x) + g.apply(x))?;
-        write_face_img(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?;
-    }
+    logger.write_csv(format!("{PREFIX}/{name}_log.csv"))?;
 
     Ok(())
 }
--- a/visualisation/cube.tex	Wed Nov 06 21:12:14 2024 -0500
+++ b/visualisation/cube.tex	Wed Nov 06 21:40:54 2024 -0500
@@ -1,6 +1,7 @@
 \documentclass[tikz]{standalone}
 \usepackage{pgfplots}
 \usepackage{tikz-3dplot}
+\usepackage[svgnames]{xcolor}
 \usepgfplotslibrary{colorbrewer}
 
 \def\datapath{../res/}
@@ -22,14 +23,18 @@
 \pgfplotsset{
     % forget plot = no legend
     cube/.style = {thick, gray!50!black,forget plot},
-    iter/.style = {mark=x, red},
+    onlyfront/.code = \pgfplotsset{x filter/.code={\onlyfront{\thisrow{face}}}},
+    onlyback/.code = \pgfplotsset{x filter/.code={\onlyback{\thisrow{face}}}},
     data/.style = {mark=*, blue, only marks},
     backdata/.style = {mark=*, blue!30!white, only marks, onlyback, forget plot},
-    backiter/.style = {mark=x, red!30!white, onlyback, forget plot},
+    iter1/.style = {mark=x, red},
+    iter2/.style = {mark=star, BlueViolet},
+    iter3/.style = {mark=asterisk, purple},
+    backiter1/.style = {mark=x, red!30!white, onlyback, forget plot},
+    backiter2/.style = {mark=star, BlueViolet!30!white, onlyback, forget plot},
+    backiter3/.style = {mark=asterisk, purple!30!white, onlyback, forget plot},
     origin/.style = {mark=o, green, only marks, thick},
     surfstyle/.style = {very nearly opaque, forget plot},
-    onlyfront/.code = \pgfplotsset{x filter/.code={\onlyfront{\thisrow{face}}}},
-    onlyback/.code = \pgfplotsset{x filter/.code={\onlyback{\thisrow{face}}}},
     legend style = {
         inner sep = 0pt,
         outer xsep = 5pt,
@@ -61,10 +66,11 @@
         %axis z line = none,
         legend columns = 3,
         legend style = {
-            at = {(0.5, 1.05)},
+            at = {(0.5, 1.1)},
             anchor = north,
             column sep = 1ex,
         },
+        mark size=1.5pt,
     }
 }
 
@@ -73,7 +79,9 @@
         \addplot3[cube] coordinates {(0, 0, 0) (0, 1, 0) (0, 1, 1) };
         \addplot3[cube] coordinates {(0, 1, 0) (1, 1, 0) };
         \addplot3[backdata] table[x=x,y=y,z=z] {\datapath/data.csv};
-        \addplot3[backiter] table[x=x,y=y,z=z] {\datapath/log.txt};
+        \addplot3[backiter1] table[x=x,y=y,z=z] {\datapath/x1_log.csv};
+        \addplot3[backiter2] table[x=x,y=y,z=z] {\datapath/x2_log.csv};
+        \addplot3[backiter3] table[x=x,y=y,z=z] {\datapath/x3_log.csv};
 
         \addplot3[
             surf,
@@ -123,8 +131,12 @@
         \addplot3[origin,onlyfront] table[x=x,y=y,z=z] {\datapath/origin.csv};
         \addlegendentry{Origin}
 
-        \addplot3[iter,onlyfront] table[x=x,y=y,z=z] {\datapath/log.txt};
-        \addlegendentry{Iterates}
+        \addplot3[iter1,onlyfront] table[x=x,y=y,z=z] {\datapath/x1_log.csv};
+        \addlegendentry{Iterates 1}
+        \addplot3[iter2,onlyfront] table[x=x,y=y,z=z] {\datapath/x2_log.csv};
+        \addlegendentry{Iterates 2}
+        \addplot3[iter3,onlyfront] table[x=x,y=y,z=z] {\datapath/x3_log.csv};
+        \addlegendentry{Iterates 3}
     \end{axis}
 \end{tikzpicture}
 

mercurial