Skip to content

Commit

Permalink
Merge c43c8de into 97dacca
Browse files Browse the repository at this point in the history
  • Loading branch information
denehoffman authored Oct 21, 2024
2 parents 97dacca + c43c8de commit 786b699
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@

use std::{
fmt::{Debug, Display, UpperExp},
marker::PhantomData,
sync::{
atomic::{AtomicBool, Ordering},
Once,
Expand Down Expand Up @@ -699,47 +698,55 @@ pub trait Observer<T: Scalar, U> {
}

/// The main struct used for running [`Algorithm`]s on [`Function`]s.
pub struct Minimizer<T, U, E, A>
pub struct Minimizer<T, U, E>
where
A: Algorithm<T, U, E>,
T: Scalar,
{
/// The [`Status`] of the [`Minimizer`], usually read after minimization.
pub status: Status<T>,
algorithm: A,
algorithm: Box<dyn Algorithm<T, U, E>>,
bounds: Option<Vec<Bound<T>>>,
max_steps: usize,
observers: Vec<Box<dyn Observer<T, U>>>,
dimension: usize,
_phantom: PhantomData<E>,
}

impl<T, U, E, A> Display for Minimizer<T, U, E, A>
impl<T, U, E> Display for Minimizer<T, U, E>
where
A: Algorithm<T, U, E>,
T: Scalar + Display + Float + UpperExp,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.status)
}
}

impl<T, U, E, A: Algorithm<T, U, E>> Minimizer<T, U, E, A>
impl<T, U, E> Minimizer<T, U, E>
where
T: Float + Scalar + Default + Display,
{
const DEFAULT_MAX_STEPS: usize = 4000;
/// Creates a new [`Minimizer`] with the given [`Algorithm`] and `dimension` set to the number
/// of free parameters in the minimization problem.
pub fn new(algorithm: &A, dimension: usize) -> Self {
pub fn new<A: Algorithm<T, U, E> + 'static>(algorithm: &A, dimension: usize) -> Self {
Self {
status: Status::default(),
algorithm: dyn_clone::clone(algorithm),
algorithm: Box::new(dyn_clone::clone(algorithm)),
bounds: None,
max_steps: Self::DEFAULT_MAX_STEPS,
observers: Vec::default(),
dimension,
}
}
/// Creates a new [`Minimizer`] with the given (boxed) [`Algorithm`] and `dimension` set to the number
/// of free parameters in the minimization problem.
pub fn new_from_box(algorithm: Box<dyn Algorithm<T, U, E>>, dimension: usize) -> Self {
Self {
status: Status::default(),
algorithm,
bounds: None,
max_steps: Self::DEFAULT_MAX_STEPS,
observers: Vec::default(),
dimension,
_phantom: PhantomData,
}
}
fn reset_status(&mut self) {
Expand All @@ -750,8 +757,8 @@ where
self.status = new_status;
}
/// Set the [`Algorithm`] used by the [`Minimizer`].
pub fn with_algorithm(mut self, algorithm: &A) -> Self {
self.algorithm = dyn_clone::clone(algorithm);
pub fn with_algorithm<A: Algorithm<T, U, E> + 'static>(mut self, algorithm: &A) -> Self {
self.algorithm = Box::new(dyn_clone::clone(algorithm));
self
}
/// Set the maximum number of steps to perform before failure (default: 4000).
Expand Down Expand Up @@ -894,3 +901,19 @@ where
Ok(())
}
}

#[cfg(test)]
mod tests {
use std::convert::Infallible;

use crate::{algorithms::LBFGSB, Algorithm, Minimizer};

#[test]
#[allow(unused_variables)]
fn test_minimizer_constructors() {
let algo: LBFGSB<f64, (), Infallible> = LBFGSB::default();
let minimizer = Minimizer::new(&algo, 5);
let algo_boxed: Box<dyn Algorithm<f64, (), Infallible>> = Box::new(algo);
let minimizer_from_box = Minimizer::new_from_box(algo_boxed, 5);
}
}

0 comments on commit 786b699

Please sign in to comment.