Giter Site home page Giter Site logo

rustamath_mnmz's Introduction

Rustamath. Library of minimization functions.

MIT licensed CI crates.io version docs.rs

Task of minimization: for given function f that depends on one or more independent variables, find the value of those variables where f takes on a minimum value.

Supported methods:

  • One Dimension
    • Bracket a Minimum
    • Golden Section Search
    • Brent’s Method
    • Brent’s Method using First Derivative
  • Multidimensions
    • Downhill Simplex Method

Example of Downhill Simplex search

fn test_paraboloid() {
    //  Paraboloid center at (1,2), scale factors (10, 20), minimum value 30
    let p = vec![1.0, 2.0, 10.0, 20.0, 30.0];

    let paraboloid = |x: &[f64]|  {
        // Paraboloid centered on (p[0],p[1]), with scale factors (p[2],p[3]) and minimum p[4]
        p[2] * (x[0] - p[0]) * (x[0] - p[0]) + p[3] * (x[1] - p[1]) * (x[1] - p[1]) + p[4]
    };

    let (min, fmin, nr_iterations) = amoeba(paraboloid, &[100.0, -100.0], 1.1, 1.0e-9, 100);

    println!("min: {}, {} fmin: {fmin} iterations: {nr_iterations}", min[0], min[1]);

    assert_float_absolute_eq!(min[0], 1.0, 1.0e-4);
    assert_float_absolute_eq!(min[1], 2.0, 1.0e-4);
    assert_float_absolute_eq!(fmin,  30.0, 1.0e-4);
}

Output:

min: 0.999933263302534, 1.9999850642280714 fmin: 30.000000072002226 iterations: 78

Example of Brent’s Method Using First Derivative

fn test_cosine() {
    use super::{golden_section_search, brent_search};

    // Minimum at Pi when x ∈ [0, 2*Pi].
    let cosine = |x: f64| (x.cos(), -(x.sin()));

    let ranges = vec![(0.01, 1.0)];

    for range in ranges {
        let (xmin, f, nr_iterations) =
            brent_df_search(cosine, range.0, range.1, 0.0, 0);

        let (xmin_golden, _, nr_iterations_golden) =
            golden_section_search(|x| cosine(x).0, range.0, range.1, 0.0, 0);

        let (xmin_brent, _, nr_iterations_brent) =
            brent_search(|x| cosine(x).0, range.0, range.1, 0.0, 0);

        println!("xmin: {:.8} f(xmin): {:6.2} iterations: {} vs brent {} vs golden {}",
            xmin, f, nr_iterations, nr_iterations_brent, nr_iterations_golden
        );

        assert_float_relative_eq!(xmin, std::f64::consts::PI, 1.0e-8);
        assert_float_relative_eq!(xmin_brent, std::f64::consts::PI, 1.0e-8);
        assert_float_relative_eq!(xmin_golden, std::f64::consts::PI, 1.0e-8);
    }
}

Output:

xmin: 3.14159265 f(xmin):  -1.00 iterations: 4 vs brent 8 vs golden 36

rustamath_mnmz's People

Contributors

igorlesik avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.