From eceb860fca17bee8f705fdb923839b50d71a2489 Mon Sep 17 00:00:00 2001 From: Tobias Jacob Date: Wed, 29 May 2024 20:02:40 +0200 Subject: [PATCH] Added Benchmark --- src/examples/benchmarks/mod.rs | 4 ++- .../benchmarks/stairs_with_lines_benchmark.rs | 30 ++++++++++++++----- src/sketch/mod.rs | 8 +++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/examples/benchmarks/mod.rs b/src/examples/benchmarks/mod.rs index 13ab820..407e9e6 100644 --- a/src/examples/benchmarks/mod.rs +++ b/src/examples/benchmarks/mod.rs @@ -47,8 +47,10 @@ mod tests { let solved = benchmark.check(1e-6); let error = sketch.borrow_mut().get_loss(); println!( - "n: {},\tsolver: {},\tsolved: {},\terror: {:.2},\tduration: {}ms", + "n: {:4}, \tprimitives: {:4}, \tconstraints:{:4}, \tsolver: {},\tsolved: {},\terror: {:.2},\tduration: {}ms", n, + sketch.borrow().get_num_primitives(), + sketch.borrow().get_num_constraints(), solver_name, solved, error, diff --git a/src/examples/benchmarks/stairs_with_lines_benchmark.rs b/src/examples/benchmarks/stairs_with_lines_benchmark.rs index 362ab8e..14f4bb2 100644 --- a/src/examples/benchmarks/stairs_with_lines_benchmark.rs +++ b/src/examples/benchmarks/stairs_with_lines_benchmark.rs @@ -1,9 +1,16 @@ use std::{cell::RefCell, rc::Rc}; +use nalgebra::Vector2; + use crate::{ constraints::{ - distance::horizontal_distance_between_points::HorizontalDistanceBetweenPoints, - lines::horizontal_line::HorizontalLine, ConstraintCell, + distance::{ + horizontal_distance_between_points::HorizontalDistanceBetweenPoints, + vertical_distance_between_points::VerticalDistanceBetweenPoints, + }, + fix_point::FixPoint, + lines::{horizontal_line::HorizontalLine, vertical_line::VerticalLine}, + ConstraintCell, }, primitives::{line::Line, point2::Point2, PrimitiveCell}, sketch::Sketch, @@ -36,6 +43,13 @@ impl BenchmarkFactory for StairsWithLinesBenchmarkFactory { point_references.push(point); } + sketch + .borrow_mut() + .add_constraint(ConstraintCell::FixPoint(Rc::new(RefCell::new( + FixPoint::new(point_references[0].clone(), Vector2::new(0.0, 0.0)), + )))) + .unwrap(); + for i in 0..n - 1 { let line = Rc::new(RefCell::new(Line::new( point_references[i].clone(), @@ -67,8 +81,8 @@ impl BenchmarkFactory for StairsWithLinesBenchmarkFactory { } else { sketch .borrow_mut() - .add_constraint(ConstraintCell::HorizontalDistance(Rc::new(RefCell::new( - HorizontalDistanceBetweenPoints::new( + .add_constraint(ConstraintCell::VerticalDistance(Rc::new(RefCell::new( + VerticalDistanceBetweenPoints::new( point_references[i].clone(), point_references[i + 1].clone(), 0.8, @@ -78,8 +92,8 @@ impl BenchmarkFactory for StairsWithLinesBenchmarkFactory { sketch .borrow_mut() - .add_constraint(ConstraintCell::HorizontalLine(Rc::new(RefCell::new( - HorizontalLine::new(line.clone()), + .add_constraint(ConstraintCell::VerticalLine(Rc::new(RefCell::new( + VerticalLine::new(line.clone()), )))) .unwrap(); } @@ -101,8 +115,8 @@ impl Benchmark for StairsWithLinesBenchmark { fn check(&self, eps: f64) -> bool { for i in 0..self.point_references.len() - 1 { let point = self.point_references[i].as_ref().borrow(); - let true_x = (i / 2) as f64 * 0.8; - let true_y = ((i + 1) / 2) as f64 * 0.8; + let true_x = ((i + 1) / 2) as f64 * 0.8; + let true_y = ((i + 0) / 2) as f64 * 0.8; if (point.x() - true_x).abs() > eps || (point.y() - true_y).abs() > eps { return false; } diff --git a/src/sketch/mod.rs b/src/sketch/mod.rs index c9a0616..e4c7773 100644 --- a/src/sketch/mod.rs +++ b/src/sketch/mod.rs @@ -43,6 +43,10 @@ impl Sketch { Ok(self.primitives_next_id - 1) } + pub fn get_num_primitives(&self) -> usize { + self.primitives.len() + } + pub fn add_constraint(&mut self, constraint: ConstraintCell) -> Result<(), ISOTopeError> { // Make sure all referenced primitives are added to the sketch before the constraint for reference in constraint.borrow().references().iter() { @@ -60,6 +64,10 @@ impl Sketch { Ok(()) } + pub fn get_num_constraints(&self) -> usize { + self.constraints.len() + } + pub fn delete_primitive(&mut self, id: u64) -> Result<(), ISOTopeError> { if self.primitives.remove(&id).is_none() { return Err(ISOTopeError::PrimitiveNotFound(id));