1 unstable release
0.1.0 | May 1, 2022 |
---|
#1657 in Algorithms
24KB
603 lines
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(())
}
Dependencies
~0.4–0.9MB
~20K SLoC