to266 / optimize

Rust crate for numerical optimization
Apache License 2.0
7 stars 3 forks source link

Golden ratio #6

Closed sebasv closed 5 years ago

sebasv commented 5 years ago

Golden ratio search code and a performance improvement for the approx_fprime routine by skipping vector addition in the loop. cargo bench gave a 3-4 time speedup on 10-1000 dimensional testfunctions with random data.

Bench code:

    #[bench]
    fn speed_approx_fprime(bench: &mut Bencher) {
        let n = 1000;
        let mut rng = thread_rng();
        let rand = Normal::new(0.0, 1.0);
        let x = Array1::from_shape_fn(n, |_| rand.sample(&mut rng));
        let eps = Array1::from_shape_fn(n, |_| rand.sample(&mut rng)/1e8);
        let f = |x: ArrayView1<f64>| x.mapv(|xi| xi*xi).scalar_sum();

        bench.iter(|| approx_fprime(x.view(), &f, eps.view()));
    }
to266 commented 5 years ago

I added a checkbox to ease the communication when a PR is ready to be merged / for review :)

sebasv commented 5 years ago

This PR is ready for review. I have added a method to search around an initial guess. If this method is used on a monotone function, it will return nan.

sebasv commented 5 years ago

In addition, feel free to add the bench to the repo as well

I quickly looked into this. cargo bench currently only works on nightly, so we could include a separate benches directory that should be compiled on nightly. Alternatively we could use Criterion, which would add a dev depencency. What are your thoughts?

to266 commented 5 years ago

I'd say include the nightly benches. Currently we'd not run them on CI anyway, and each dev can switch to nightly for the benchmarks. These can be done in a separate PR as well, to not pollute this :)