QOpt
A simple optimization package.
Optimization Paradigms
The latest version of QOpt supports the following paradigms.
- Gradient Descent
- Newton's Method
- Genetic Optimization
- Simulated Annealing
Getting Started
Importing maria-linalg
You must import the latest version of the Rust crate (maria-linalg)[https://crates.io/maria-linalg] in order to use this package.
Creating a Function
First, you must define a function struct that satisfies trait Function. This represents a function that accepts an N-dimensional vector and outputs a scalar.
Function has three methods.
Function::objective(required). Evaluates to the function output (f64).Function::gradient(optional). Evaluates to the function gradient (Vector<N>).Function::hessian(optional). Evaluates to the function Hessian (Matrix<N>).
See the example below. Note that you must also import maria_linalg::Vector and (only if you implement the Hessian Function::hessian) maria_linalg::Matrix.
use qopt::Function;
use maria_linalg::{Matrix, Veector};
/// Number of dimensions of input vector.
const N: usize = 6;
pub struct MyFunction { }
impl MyFunction {
pub fn new() -> Self {
Self { }
}
}
impl Function<N> for MyFunction {
fn objective(&self, input: Vector<N>) -> f64 {
// Required
}
fn gradient(&self, input: Vector<N>) -> Vector<N> {
// Optional
}
fn hessian(&self, input: Vector<N>) -> Matrix<N> {
// Optional
}
}
Creating an Optimizer
Once you have a struct that satisfies Function, you can create your Optimizer.
use qopt::Optimizer;
/// Number of individuals per optimization iteration.
///
/// For deterministic methods (gradient descent or Newton's method), this should be 1.
/// For stochastic methods (genetic optimization or simulated annealing), this should be about 100.
const POPULATION: usize = 100;
fn main() {
let f = MyFunction::new();
let optimizer: Optimizer<N, POPULATION> = Optimizer::new(Box::new(f));
// An initial guess
let input = Vector::zero();
let output = optimizer.optimize(input);
println!("{}", output);
}