forked from StudioInfinity/dyna3
Encapsulate realization results
In the process, spruce up our realization diagnostics logging and factor out some of the repetitive code in the examples, because we're already changing those parts of the code to adapt them to the new encapsulation. This commit changes the example output format. I've checked by hand that the output is rearranged but not meaningfully changed.
This commit is contained in:
parent
4cb3262555
commit
679c421d04
8 changed files with 176 additions and 103 deletions
36
app-proto/examples/common/mod.rs
Normal file
36
app-proto/examples/common/mod.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
use nalgebra::DMatrix;
|
||||
|
||||
use dyna3::engine::{Q, DescentHistory, RealizationResult};
|
||||
|
||||
pub fn print_title(title: &str) {
|
||||
println!("─── {title} ───");
|
||||
}
|
||||
|
||||
pub fn print_realization_diagnostics(realization_result: &RealizationResult) {
|
||||
let RealizationResult { result, history } = realization_result;
|
||||
println!();
|
||||
if let Err(ref msg) = result {
|
||||
println!("❌️ {msg}");
|
||||
} else {
|
||||
println!("✅️ Target accuracy achieved!");
|
||||
}
|
||||
println!("Steps: {}", history.scaled_loss.len() - 1);
|
||||
println!("Loss: {}", history.scaled_loss.last().unwrap());
|
||||
}
|
||||
|
||||
pub fn print_gram_matrix(config: &DMatrix<f64>) {
|
||||
println!("\nCompleted Gram matrix:{}", (config.tr_mul(&*Q) * config).to_string().trim_end());
|
||||
}
|
||||
|
||||
pub fn print_config(config: &DMatrix<f64>) {
|
||||
println!("\nConfiguration:{}", config.to_string().trim_end());
|
||||
}
|
||||
|
||||
pub fn print_loss_history(history: &DescentHistory) {
|
||||
println!("\nStep │ Loss\n─────┼────────────────────────────────");
|
||||
for (step, scaled_loss) in history.scaled_loss.iter().enumerate() {
|
||||
println!("{:<4} │ {}", step, scaled_loss);
|
||||
}
|
||||
}
|
|
@ -1,25 +1,28 @@
|
|||
use dyna3::engine::{Q, examples::realize_irisawa_hexlet};
|
||||
mod common;
|
||||
|
||||
use common::{
|
||||
print_gram_matrix,
|
||||
print_loss_history,
|
||||
print_realization_diagnostics,
|
||||
print_title
|
||||
};
|
||||
use dyna3::engine::{Realization, examples::realize_irisawa_hexlet};
|
||||
|
||||
fn main() {
|
||||
const SCALED_TOL: f64 = 1.0e-12;
|
||||
let (config, _, success, history) = realize_irisawa_hexlet(SCALED_TOL);
|
||||
print!("\nCompleted Gram matrix:{}", config.tr_mul(&*Q) * &config);
|
||||
if success {
|
||||
println!("Target accuracy achieved!");
|
||||
} else {
|
||||
println!("Failed to reach target accuracy");
|
||||
}
|
||||
println!("Steps: {}", history.scaled_loss.len() - 1);
|
||||
println!("Loss: {}", history.scaled_loss.last().unwrap());
|
||||
if success {
|
||||
let realization_result = realize_irisawa_hexlet(SCALED_TOL);
|
||||
print_title("Irisawa hexlet");
|
||||
print_realization_diagnostics(&realization_result);
|
||||
if let Ok(Realization { config, .. }) = realization_result.result {
|
||||
// print the diameters of the chain spheres
|
||||
println!("\nChain diameters:");
|
||||
println!(" {} sun (given)", 1.0 / config[(3, 3)]);
|
||||
for k in 4..9 {
|
||||
println!(" {} sun", 1.0 / config[(3, k)]);
|
||||
}
|
||||
|
||||
// print the completed Gram matrix
|
||||
print_gram_matrix(&config);
|
||||
}
|
||||
println!("\nStep │ Loss\n─────┼────────────────────────────────");
|
||||
for (step, scaled_loss) in history.scaled_loss.into_iter().enumerate() {
|
||||
println!("{:<4} │ {}", step, scaled_loss);
|
||||
}
|
||||
print_loss_history(&realization_result.history);
|
||||
}
|
|
@ -1,30 +1,37 @@
|
|||
mod common;
|
||||
|
||||
use nalgebra::{DMatrix, DVector};
|
||||
|
||||
use dyna3::engine::{Q, examples::realize_kaleidocycle};
|
||||
use common::{
|
||||
print_config,
|
||||
print_gram_matrix,
|
||||
print_realization_diagnostics,
|
||||
print_title
|
||||
};
|
||||
use dyna3::engine::{Realization, examples::realize_kaleidocycle};
|
||||
|
||||
fn main() {
|
||||
const SCALED_TOL: f64 = 1.0e-12;
|
||||
let (config, tangent, success, history) = realize_kaleidocycle(SCALED_TOL);
|
||||
print!("Completed Gram matrix:{}", config.tr_mul(&*Q) * &config);
|
||||
print!("Configuration:{}", config);
|
||||
if success {
|
||||
println!("Target accuracy achieved!");
|
||||
} else {
|
||||
println!("Failed to reach target accuracy");
|
||||
let realization_result = realize_kaleidocycle(SCALED_TOL);
|
||||
print_title("Kaleidocycle");
|
||||
print_realization_diagnostics(&realization_result);
|
||||
if let Ok(Realization { config, tangent }) = realization_result.result {
|
||||
// print the completed Gram matrix and the realized configuration
|
||||
print_gram_matrix(&config);
|
||||
print_config(&config);
|
||||
|
||||
// find the kaleidocycle's twist motion by projecting onto the tangent
|
||||
// space
|
||||
const N_POINTS: usize = 12;
|
||||
let up = DVector::from_column_slice(&[0.0, 0.0, 1.0, 0.0]);
|
||||
let down = -&up;
|
||||
let twist_motion: DMatrix<_> = (0..N_POINTS).step_by(4).flat_map(
|
||||
|n| [
|
||||
tangent.proj(&up.as_view(), n),
|
||||
tangent.proj(&down.as_view(), n+1)
|
||||
]
|
||||
).sum();
|
||||
let normalization = 5.0 / twist_motion[(2, 0)];
|
||||
println!("\nTwist motion:{}", (normalization * twist_motion).to_string().trim_end());
|
||||
}
|
||||
println!("Steps: {}", history.scaled_loss.len() - 1);
|
||||
println!("Loss: {}\n", history.scaled_loss.last().unwrap());
|
||||
|
||||
// find the kaleidocycle's twist motion by projecting onto the tangent space
|
||||
const N_POINTS: usize = 12;
|
||||
let up = DVector::from_column_slice(&[0.0, 0.0, 1.0, 0.0]);
|
||||
let down = -&up;
|
||||
let twist_motion: DMatrix<_> = (0..N_POINTS).step_by(4).flat_map(
|
||||
|n| [
|
||||
tangent.proj(&up.as_view(), n),
|
||||
tangent.proj(&down.as_view(), n+1)
|
||||
]
|
||||
).sum();
|
||||
let normalization = 5.0 / twist_motion[(2, 0)];
|
||||
print!("Twist motion:{}", normalization * twist_motion);
|
||||
}
|
|
@ -1,4 +1,19 @@
|
|||
use dyna3::engine::{Q, point, realize_gram, sphere, ConstraintProblem};
|
||||
mod common;
|
||||
|
||||
use common::{
|
||||
print_config,
|
||||
print_gram_matrix,
|
||||
print_loss_history,
|
||||
print_realization_diagnostics,
|
||||
print_title
|
||||
};
|
||||
use dyna3::engine::{
|
||||
point,
|
||||
realize_gram,
|
||||
sphere,
|
||||
ConstraintProblem,
|
||||
Realization
|
||||
};
|
||||
|
||||
fn main() {
|
||||
let mut problem = ConstraintProblem::from_guess(&[
|
||||
|
@ -11,21 +26,14 @@ fn main() {
|
|||
}
|
||||
}
|
||||
problem.frozen.push(3, 0, problem.guess[(3, 0)]);
|
||||
println!();
|
||||
let (config, _, success, history) = realize_gram(
|
||||
let realization_result = realize_gram(
|
||||
&problem, 1.0e-12, 0.5, 0.9, 1.1, 200, 110
|
||||
);
|
||||
print!("\nCompleted Gram matrix:{}", config.tr_mul(&*Q) * &config);
|
||||
print!("Configuration:{}", config);
|
||||
if success {
|
||||
println!("Target accuracy achieved!");
|
||||
} else {
|
||||
println!("Failed to reach target accuracy");
|
||||
}
|
||||
println!("Steps: {}", history.scaled_loss.len() - 1);
|
||||
println!("Loss: {}", history.scaled_loss.last().unwrap());
|
||||
println!("\nStep │ Loss\n─────┼────────────────────────────────");
|
||||
for (step, scaled_loss) in history.scaled_loss.into_iter().enumerate() {
|
||||
println!("{:<4} │ {}", step, scaled_loss);
|
||||
print_title("Point on a sphere");
|
||||
print_realization_diagnostics(&realization_result);
|
||||
if let Ok(Realization{ config, .. }) = realization_result.result {
|
||||
print_gram_matrix(&config);
|
||||
print_config(&config);
|
||||
}
|
||||
print_loss_history(&realization_result.history);
|
||||
}
|
|
@ -1,4 +1,12 @@
|
|||
use dyna3::engine::{Q, realize_gram, sphere, ConstraintProblem};
|
||||
mod common;
|
||||
|
||||
use common::{
|
||||
print_gram_matrix,
|
||||
print_loss_history,
|
||||
print_realization_diagnostics,
|
||||
print_title
|
||||
};
|
||||
use dyna3::engine::{realize_gram, sphere, ConstraintProblem, Realization};
|
||||
|
||||
fn main() {
|
||||
let mut problem = ConstraintProblem::from_guess({
|
||||
|
@ -14,20 +22,13 @@ fn main() {
|
|||
problem.gram.push_sym(j, k, if j == k { 1.0 } else { -1.0 });
|
||||
}
|
||||
}
|
||||
println!();
|
||||
let (config, _, success, history) = realize_gram(
|
||||
let realization_result = realize_gram(
|
||||
&problem, 1.0e-12, 0.5, 0.9, 1.1, 200, 110
|
||||
);
|
||||
print!("\nCompleted Gram matrix:{}", config.tr_mul(&*Q) * &config);
|
||||
if success {
|
||||
println!("Target accuracy achieved!");
|
||||
} else {
|
||||
println!("Failed to reach target accuracy");
|
||||
}
|
||||
println!("Steps: {}", history.scaled_loss.len() - 1);
|
||||
println!("Loss: {}", history.scaled_loss.last().unwrap());
|
||||
println!("\nStep │ Loss\n─────┼────────────────────────────────");
|
||||
for (step, scaled_loss) in history.scaled_loss.into_iter().enumerate() {
|
||||
println!("{:<4} │ {}", step, scaled_loss);
|
||||
print_title("Three spheres");
|
||||
print_realization_diagnostics(&realization_result);
|
||||
if let Ok(Realization{ config, .. }) = realization_result.result {
|
||||
print_gram_matrix(&config);
|
||||
}
|
||||
print_loss_history(&realization_result.history);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue