use crate::{ArgminOp, Error};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct OpWrapper<O: ArgminOp> {
op: O,
pub cost_func_count: u64,
pub grad_func_count: u64,
pub hessian_func_count: u64,
pub jacobian_func_count: u64,
pub modify_func_count: u64,
}
impl<O: ArgminOp> OpWrapper<O> {
pub fn new(op: &O) -> Self {
OpWrapper {
op: op.clone(),
cost_func_count: 0,
grad_func_count: 0,
hessian_func_count: 0,
jacobian_func_count: 0,
modify_func_count: 0,
}
}
pub fn new_move(op: O) -> Self {
OpWrapper {
op,
cost_func_count: 0,
grad_func_count: 0,
hessian_func_count: 0,
jacobian_func_count: 0,
modify_func_count: 0,
}
}
pub fn apply(&mut self, param: &O::Param) -> Result<O::Output, Error> {
self.cost_func_count += 1;
self.op.apply(param)
}
pub fn gradient(&mut self, param: &O::Param) -> Result<O::Param, Error> {
self.grad_func_count += 1;
self.op.gradient(param)
}
pub fn hessian(&mut self, param: &O::Param) -> Result<O::Hessian, Error> {
self.hessian_func_count += 1;
self.op.hessian(param)
}
pub fn jacobian(&mut self, param: &O::Param) -> Result<O::Jacobian, Error> {
self.jacobian_func_count += 1;
self.op.jacobian(param)
}
pub fn modify(&mut self, param: &O::Param, extent: f64) -> Result<O::Param, Error> {
self.modify_func_count += 1;
self.op.modify(param, extent)
}
pub fn consume_op<O2: ArgminOp>(&mut self, other: OpWrapper<O2>) {
self.cost_func_count += other.cost_func_count;
self.grad_func_count += other.grad_func_count;
self.hessian_func_count += other.hessian_func_count;
self.jacobian_func_count += other.jacobian_func_count;
self.modify_func_count += other.modify_func_count;
}
pub fn reset(mut self) -> Self {
self.cost_func_count = 0;
self.grad_func_count = 0;
self.hessian_func_count = 0;
self.jacobian_func_count = 0;
self.modify_func_count = 0;
self
}
pub fn get_op(self) -> O {
self.op
}
pub fn clone_op(&self) -> O {
self.op.clone()
}
pub fn new_from_op(op: &OpWrapper<O>) -> Self {
Self::new(&op.clone_op())
}
}
impl<O: ArgminOp> ArgminOp for OpWrapper<O> {
type Param = O::Param;
type Output = O::Output;
type Hessian = O::Hessian;
type Jacobian = O::Jacobian;
fn apply(&self, param: &Self::Param) -> Result<Self::Output, Error> {
self.op.apply(param)
}
fn gradient(&self, param: &Self::Param) -> Result<Self::Param, Error> {
self.op.gradient(param)
}
fn hessian(&self, param: &Self::Param) -> Result<Self::Hessian, Error> {
self.op.hessian(param)
}
fn jacobian(&self, param: &Self::Param) -> Result<Self::Jacobian, Error> {
self.op.jacobian(param)
}
fn modify(&self, param: &Self::Param, extent: f64) -> Result<Self::Param, Error> {
self.op.modify(param, extent)
}
}