1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::core::{ArgminOp, Error};
use serde::{Deserialize, Serialize};
use std::default::Default;
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct OpWrapper<O: ArgminOp> {
pub op: Option<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: Some(op),
cost_func_count: 0,
grad_func_count: 0,
hessian_func_count: 0,
jacobian_func_count: 0,
modify_func_count: 0,
}
}
pub fn new_from_wrapper(op: &mut OpWrapper<O>) -> Self {
OpWrapper {
op: op.take_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.as_ref().unwrap().apply(param)
}
pub fn gradient(&mut self, param: &O::Param) -> Result<O::Param, Error> {
self.grad_func_count += 1;
self.op.as_ref().unwrap().gradient(param)
}
pub fn hessian(&mut self, param: &O::Param) -> Result<O::Hessian, Error> {
self.hessian_func_count += 1;
self.op.as_ref().unwrap().hessian(param)
}
pub fn jacobian(&mut self, param: &O::Param) -> Result<O::Jacobian, Error> {
self.jacobian_func_count += 1;
self.op.as_ref().unwrap().jacobian(param)
}
pub fn modify(&mut self, param: &O::Param, extent: O::Float) -> Result<O::Param, Error> {
self.modify_func_count += 1;
self.op.as_ref().unwrap().modify(param, extent)
}
pub fn take_op(&mut self) -> Option<O> {
self.op.take()
}
pub fn consume_op(&mut self, other: OpWrapper<O>) {
self.op = other.op;
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 consume_func_counts<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.unwrap()
}
}
impl<O: ArgminOp> ArgminOp for OpWrapper<O> {
type Param = O::Param;
type Output = O::Output;
type Hessian = O::Hessian;
type Jacobian = O::Jacobian;
type Float = O::Float;
fn apply(&self, param: &Self::Param) -> Result<Self::Output, Error> {
self.op.as_ref().unwrap().apply(param)
}
fn gradient(&self, param: &Self::Param) -> Result<Self::Param, Error> {
self.op.as_ref().unwrap().gradient(param)
}
fn hessian(&self, param: &Self::Param) -> Result<Self::Hessian, Error> {
self.op.as_ref().unwrap().hessian(param)
}
fn jacobian(&self, param: &Self::Param) -> Result<Self::Jacobian, Error> {
self.op.as_ref().unwrap().jacobian(param)
}
fn modify(&self, param: &Self::Param, extent: Self::Float) -> Result<Self::Param, Error> {
self.op.as_ref().unwrap().modify(param, extent)
}
}