-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,74 @@ | ||
use log::warn; | ||
|
||
use super::error::MoyoError; | ||
|
||
pub const EPS: f64 = 1e-8; | ||
|
||
const INITIAL_SYMMETRY_SEARCH_STRIDE: f64 = 2.0; | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub enum AngleTolerance { | ||
Radian(f64), | ||
Default, | ||
} | ||
|
||
pub const EPS: f64 = 1e-8; | ||
pub struct ToleranceHandler { | ||
pub symprec: f64, | ||
pub angle_tolerance: AngleTolerance, | ||
stride: f64, | ||
prev_error: Option<MoyoError>, | ||
} | ||
|
||
impl ToleranceHandler { | ||
pub fn new(symprec: f64, angle_tolerance: AngleTolerance) -> Self { | ||
Self { | ||
symprec, | ||
angle_tolerance, | ||
stride: INITIAL_SYMMETRY_SEARCH_STRIDE, | ||
prev_error: None, | ||
} | ||
} | ||
|
||
pub fn update(&mut self, err: MoyoError) { | ||
// Update stride | ||
if !self.prev_error.is_none() && self.prev_error != Some(err) { | ||
self.stride = self.stride.sqrt() | ||
} | ||
self.prev_error = Some(err); | ||
|
||
// Update tolerances | ||
(self.symprec, self.angle_tolerance) = match err { | ||
MoyoError::TooSmallToleranceError => self.increase_tolerance(), | ||
MoyoError::TooLargeToleranceError => self.reduce_tolerance(), | ||
_ => (self.symprec, self.angle_tolerance), | ||
} | ||
} | ||
|
||
fn increase_tolerance(&self) -> (f64, AngleTolerance) { | ||
let symprec = self.symprec * self.stride; | ||
let angle_tolerance = if let AngleTolerance::Radian(angle) = self.angle_tolerance { | ||
AngleTolerance::Radian(angle * self.stride) | ||
} else { | ||
AngleTolerance::Default | ||
}; | ||
warn!( | ||
"Increase tolerance to symprec={}, angle_tolerance={:?}", | ||
symprec, angle_tolerance | ||
); | ||
(symprec, angle_tolerance) | ||
} | ||
|
||
fn reduce_tolerance(&self) -> (f64, AngleTolerance) { | ||
let symprec = self.symprec / self.stride; | ||
let angle_tolerance = if let AngleTolerance::Radian(angle) = self.angle_tolerance { | ||
AngleTolerance::Radian(angle / self.stride) | ||
} else { | ||
AngleTolerance::Default | ||
}; | ||
warn!( | ||
"Reduce tolerance to symprec={}, angle_tolerance={:?}", | ||
symprec, angle_tolerance | ||
); | ||
(symprec, angle_tolerance) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters