Skip to content
This repository was archived by the owner on Dec 27, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ let args = Array::from_vec(vec![3.0, -8.3]);
let ans = minimizer.minimize(&function, args.view());

// Print the optimized values
println!("Final optimized arguments: {}", ans);
println!("Final optimized arguments: {:?}", ans);
```
5 changes: 3 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate optimize;

use ndarray::prelude::*;

use optimize::minimizer::Minimizer;
use optimize::vector::NelderMeadBuilder;

pub fn main() {
Expand All @@ -11,10 +12,10 @@ pub fn main() {
let minimizer = NelderMeadBuilder::default()
.xtol(1e-6f64)
.ftol(1e-6f64)
.maxiter(50000)
.maxiter(50000)
.build()
.unwrap();
let args = Array::from_vec(vec![3.0, -8.3]);
let ans = minimizer.minimize(&function, args.view());
println!("Final optimized arguments: {}", ans);
println!("Final optimized arguments: {:?}", ans);
}
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! # extern crate optimize;
//! # use ndarray::prelude::*;
//! # use optimize::vector::NelderMeadBuilder;
//! # use optimize::minimizer::Minimizer;
//! // Define a function that we aim to minimize
//! let function = |x: ArrayView1<f64>| (1.0 - x[0]).powi(2) + 100.0 * (x[1] - x[0].powi(2)).powi(2);
//!
Expand All @@ -29,7 +30,7 @@
//! let ans = minimizer.minimize(&function, args.view());
//!
//! // Print the optimized values
//! println!("Final optimized arguments: {}", ans);
//! println!("Final optimized arguments: {}", ans.minimum_value.unwrap());
//! ```

#![deny(missing_docs)]
Expand All @@ -43,7 +44,8 @@ extern crate derive_builder;
extern crate float_cmp;
extern crate num_traits;

pub mod minimizer;
pub mod scalar;
pub mod vector;

mod utils;
mod utils;
41 changes: 39 additions & 2 deletions src/minimizer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@

//! This module provides the base framework for all minimizers present in this crate, such as the
//! base trait and return type.
use ndarray::prelude::*;
use std::time::Duration;

/// Minimizer states at the end of the run
#[derive(Debug, PartialEq)]
pub enum RunStatus {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The meaning of RunStatus is not entirely clear to me. I assume this enum should reflect the reason for termination, but then it should differentiate between e.g. ftol convergence, xtol convergence or maxiter exceeded. See also my comment in the Nelder-Mead code

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, will change this. The PR is only a WIP for now anyway. I was thinking of adding more states, such as warmup, inprogress, one for various convergences. This can also be kept in the minimizer state (and updated via something like is_finished in NM). Maybe at some point it can also be potentially used to save the state of an in-progress minimization to restart or continue.

/// Minimizer finished successfully.
Success,
/// Minimization was not succesful
Failure,
}

/// Final minimization parameters that result in the smallest found value.
#[derive(Debug, PartialEq)]
pub enum MinResult {
/// Scalar parameter value where the minimum is found
Scalar(f64),
/// Vector of parameters where the minimum if found
Vector(Array1<f64>),
}

/// A minimization result, storing various details of the run and the final results.
#[derive(Debug, PartialEq)]
pub struct OptimResult {
/// The runtime of the minimization according to the system clock.
pub runtime: Option<Duration>,
/// The number of function evaluations performed.
pub f_evals: Option<usize>,
/// The number of iterations run.
pub iterations: Option<usize>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear to me why runtime,f_evals and iterations are Options. Are there cases imaginable where these don't have 'default' values? I would expect every algorithm can return appropriate values for these fields.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my point of view, two reasons:

  1. I did not bother considering all usecases for each parameter.
  2. I want a uniform interface for all of them anyway. I think the additional costs don't matter as much, but it simply adds more flexibility.

/// The final parameter values.
pub minimum: Option<MinResult>,
/// The function value at the found minimum.
pub minimum_value: Option<f64>,
/// The minimizer success or failure status.
pub status: RunStatus,
}

/// A general minimizer trait.
pub trait Minimizer {
Expand All @@ -9,5 +46,5 @@ pub trait Minimizer {
&self,
func: F,
args: ArrayView1<f64>,
) -> Array1<f64>;
) -> OptimResult;
}
2 changes: 1 addition & 1 deletion src/scalar/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
//! This module contains algorithms that search for local minima of functions along a single dimension.
//! This module contains algorithms that search for local minima of functions along a single dimension.
1 change: 0 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ where
grad
}


#[cfg(test)]
mod tests {

Expand Down
2 changes: 1 addition & 1 deletion src/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

mod nelder_mead;

pub use self::nelder_mead::NelderMeadBuilder;
pub use self::nelder_mead::NelderMeadBuilder;
Loading