Skip to content

Commit

Permalink
mess with plotting plane of best fit with dummy data
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Pratt committed Nov 30, 2023
1 parent bd70a5a commit 56bb9bb
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 140 deletions.
10 changes: 7 additions & 3 deletions benches/dapol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rand::distributions::{Distribution, Uniform};
use dapol::accumulators::NdmSmt;
use dapol::{EntityId, Height, InclusionProof, MaxThreadCount};

mod heuristic_func_examples;
mod heuristic_func;
mod setup;
use crate::setup::{NUM_USERS, TREE_HEIGHTS};

Expand All @@ -20,7 +20,7 @@ fn bench_build_tree(c: &mut Criterion) {
let alloc = stats::allocated::mib().unwrap();

let mut group = c.benchmark_group("dapol");
group.sample_size(10);
group.sample_size(20);
// `SamplingMode::Flat` is used here as that is what Criterion recommends for long-running benches
// https://bheisler.github.io/criterion.rs/book/user_guide/advanced_configuration.html#sampling-mode
group.sampling_mode(SamplingMode::Flat);
Expand Down Expand Up @@ -297,6 +297,10 @@ fn bench_test_jemalloc_readings() {
println!("Memory usage: {} allocated", setup::bytes_as_string(diff),);
}

fn plot_plane() {
heuristic_func::plot();
}

// ================================================================================================÷

criterion_group!(
Expand All @@ -306,4 +310,4 @@ criterion_group!(
bench_verify_proof
);

criterion_main!(benches, bench_test_jemalloc_readings);
criterion_main!(/* benches, bench_test_jemalloc_readings, */ plot_plane);
92 changes: 92 additions & 0 deletions benches/heuristic_func.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
extern crate nalgebra as na;

use std::mem;

use gnuplot::{
Figure,
PlotOption::{self},
};
use na::{ArrayStorage, Const, Matrix};

// Assuming each hash value is 32 bytes (adjust based on your use case)
const HASH_SIZE_BYTES: usize = 32;

// Heuristic function to estimate memory usage for a Merkle Tree
pub fn estimate_memory_usage(height: u8, num_users: u64) -> usize {
// Calculate the number of hash values in the Merkle Tree
let num_hash_values = 2u32.pow(height as u32);

// Calculate the total memory usage
let memory_usage_bytes =
num_users as usize * HASH_SIZE_BYTES + num_hash_values as usize * mem::size_of::<u8>();

memory_usage_bytes
}

pub fn plot() {
// TODO: replace with actual data (already collected)
// Define points
let points = vec![
na::Point3::new(0.0, 0.0, 0.0),
na::Point3::new(1.0, 3.0, 5.0),
na::Point3::new(-5.0, 6.0, 3.0),
na::Point3::new(3.0, 6.0, 7.0),
na::Point3::new(-2.0, 6.0, 7.0),
];

// Calculate best-fit plane
let plane = fit_plane(&points);

// Plot points and plane
plot_3d(&points, plane);
}

fn fit_plane(
points: &Vec<na::Point3<f64>>,
) -> Matrix<f64, Const<3>, Const<1>, ArrayStorage<f64, 3, 1>> {
// Convert points to a matrix
let points_matrix = na::DMatrix::from_iterator(
points.len(),
3,
points.iter().flat_map(|p| p.coords.iter().cloned()),
);

// Use SVD to calculate the best-fit plane
let svd = points_matrix.svd(true, true);

// Extract the normal vector from the right singular vectors
let normal_vector = na::Vector3::new(
svd.v_t.clone().unwrap()[(0, 2)],
svd.v_t.clone().unwrap()[(1, 2)],
svd.v_t.clone().unwrap()[(2, 2)],
);

normal_vector.normalize()
}

fn plot_3d(
points: &Vec<na::Point3<f64>>,
plane: Matrix<f64, Const<3>, Const<1>, ArrayStorage<f64, 3, 1>>,
) {
let mut fg = Figure::new();

let x = points.iter().map(|p| p.x).collect::<Vec<f64>>();
let y = points.iter().map(|p| p.y).collect::<Vec<f64>>();
let z = points.iter().map(|p| p.z).collect::<Vec<f64>>();

// Plot points
fg.axes3d().points(x, y, z, &[PlotOption::Color("black")]);

fg.axes3d().surface(
&plane,
points.len(),
3,
None,
&[PlotOption::Color("blue"), PlotOption::Caption("Plane")],
);

// Show the plot
fg.show().unwrap();

// fg.save_to_png("benches/3d_plot.png", 640, 480)
}
137 changes: 0 additions & 137 deletions benches/heuristic_func_examples.rs

This file was deleted.

0 comments on commit 56bb9bb

Please sign in to comment.