mod tiny_solver_backend;
mod tiny_solver_manifolds;
use crate::Error;
use anyhow::Result as AnyhowResult;
use nalgebra::DVector;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::ir::ProblemIR;
pub use tiny_solver_backend::TinySolverBackend;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackendSolveOptions {
pub max_iters: usize,
pub verbosity: usize,
pub linear_solver: Option<LinearSolverKind>,
pub min_abs_decrease: Option<f64>,
pub min_rel_decrease: Option<f64>,
pub min_error: Option<f64>,
}
impl Default for BackendSolveOptions {
fn default() -> Self {
Self {
max_iters: 100,
verbosity: 0,
linear_solver: Some(LinearSolverKind::SparseCholesky),
min_abs_decrease: Some(1e-5),
min_rel_decrease: Some(1e-5),
min_error: Some(1e-10),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LinearSolverKind {
SparseCholesky,
SparseQR,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SolveReport {
pub final_cost: f64,
}
#[derive(Debug, Clone)]
pub struct BackendSolution {
pub params: HashMap<String, DVector<f64>>,
pub solve_report: SolveReport,
}
pub trait OptimBackend {
fn solve(
&self,
ir: &ProblemIR,
initial: &HashMap<String, DVector<f64>>,
opts: &BackendSolveOptions,
) -> AnyhowResult<BackendSolution>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackendKind {
TinySolver,
Ceres,
}
pub fn solve_with_backend(
backend: BackendKind,
ir: &ProblemIR,
initial: &HashMap<String, DVector<f64>>,
opts: &BackendSolveOptions,
) -> Result<BackendSolution, Error> {
match backend {
BackendKind::TinySolver => TinySolverBackend
.solve(ir, initial, opts)
.map_err(Error::from),
BackendKind::Ceres => Err(Error::numerical("Ceres backend not implemented")),
}
}