Crates.io | swoop |
lib.rs | swoop |
version | 0.1.0 |
source | src |
created_at | 2022-05-01 14:03:08.965979 |
updated_at | 2022-05-01 14:03:08.965979 |
description | Simple, lightweight optimisation algorithms in pure Rust |
homepage | |
repository | https://github.com/benjaminjellis/swoop |
max_upload_size | |
id | 578609 |
size | 36,494 |
Simple, lightweight optimisation algorithms in pure Rust
This crate aims to mimic the scipy.optimize module in pure Rust.
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(())
}