Skip to content

Commit

Permalink
Added Benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasJacob committed May 29, 2024
1 parent c98ef0e commit eceb860
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/examples/benchmarks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 22 additions & 8 deletions src/examples/benchmarks/stairs_with_lines_benchmark.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand All @@ -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();
}
Expand All @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions src/sketch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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));
Expand Down

0 comments on commit eceb860

Please sign in to comment.