swoop

Crates.ioswoop
lib.rsswoop
version0.1.0
sourcesrc
created_at2022-05-01 14:03:08.965979
updated_at2022-05-01 14:03:08.965979
descriptionSimple, lightweight optimisation algorithms in pure Rust
homepage
repositoryhttps://github.com/benjaminjellis/swoop
max_upload_size
id578609
size36,494
benjamin (benjaminjellis)

documentation

README

CircleCI MSRV version

swoop

Simple, lightweight optimisation algorithms in pure Rust

Motivation

This crate aims to mimic the scipy.optimize module in pure Rust.

Example

This crate has an asynchronous API and all examples use Tokio. To start your Cargo.toml should at least include

[dependencies]
swoop = { "git" = "https://github.com/benjaminjellis/swoop" }
tokio = { version = "1", features = ["full"] }

To minimise the function f(x) = 3x^2 + 4x + 50 in the bound -10 <= x <= 10 you can use the bounded optimiser

use swoop::minimise_scalar::{bounded, ScalarObjectiveFunction};
use swoop::SwoopErrors;

struct MyObjectiveFunction {
    a: f64,
    b: f64,
    c: f64,
}

impl MyObjectiveFunction {
    fn new(a: f64, b: f64, c: f64) -> Self {
        Self { a, b, c }
    }
}

impl ScalarObjectiveFunction for MyObjectiveFunction {
    fn evaluate(&self, x: f64) -> f64 {
        self.a * x.powf(2f64) + self.b * x + self.c
    }
}

#[tokio::main]
async fn main() -> Result<(), SwoopErrors> {
    let objective_function = MyObjectiveFunction::new(3f64, 4f64, 50f64);
    let result = bounded(objective_function, (-10f64, 10f64), 500usize).await?;
    println!("{:?}", result);
    Ok(())
}
Commit count: 8

cargo fmt