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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#![allow(clippy::range_plus_one)]
#[macro_use]
pub mod macros;
mod errors;
pub mod executor;
mod iterstate;
mod kv;
mod math;
mod nooperator;
mod observers;
mod opwrapper;
mod result;
mod serialization;
mod termination;
pub use anyhow::Error;
pub use errors::*;
pub use executor::*;
pub use iterstate::*;
pub use kv::ArgminKV;
pub use math::*;
pub use nooperator::*;
use num::traits::{Float, FloatConst, FromPrimitive, ToPrimitive};
pub use observers::*;
pub use opwrapper::*;
pub use result::ArgminResult;
use serde::de::DeserializeOwned;
use serde::Serialize;
pub use serialization::*;
use std::fmt::{Debug, Display};
pub use termination::TerminationReason;
pub trait ArgminFloat:
Float + FloatConst + FromPrimitive + ToPrimitive + Debug + Display + Serialize + DeserializeOwned
{
}
impl<I> ArgminFloat for I where
I: Float
+ FloatConst
+ FromPrimitive
+ ToPrimitive
+ Debug
+ Display
+ Serialize
+ DeserializeOwned
{
}
pub trait ArgminOp {
type Param: Clone + Serialize + DeserializeOwned;
type Output: Clone + Serialize + DeserializeOwned;
type Hessian: Clone + Serialize + DeserializeOwned;
type Jacobian: Clone + Serialize + DeserializeOwned;
type Float: ArgminFloat;
fn apply(&self, _param: &Self::Param) -> Result<Self::Output, Error> {
Err(ArgminError::NotImplemented {
text: "Method `apply` of ArgminOp trait not implemented!".to_string(),
}
.into())
}
fn gradient(&self, _param: &Self::Param) -> Result<Self::Param, Error> {
Err(ArgminError::NotImplemented {
text: "Method `gradient` of ArgminOp trait not implemented!".to_string(),
}
.into())
}
fn hessian(&self, _param: &Self::Param) -> Result<Self::Hessian, Error> {
Err(ArgminError::NotImplemented {
text: "Method `hessian` of ArgminOp trait not implemented!".to_string(),
}
.into())
}
fn jacobian(&self, _param: &Self::Param) -> Result<Self::Jacobian, Error> {
Err(ArgminError::NotImplemented {
text: "Method `jacobian` of ArgminOp trait not implemented!".to_string(),
}
.into())
}
fn modify(&self, _param: &Self::Param, _extent: Self::Float) -> Result<Self::Param, Error> {
Err(ArgminError::NotImplemented {
text: "Method `modify` of ArgminOp trait not implemented!".to_string(),
}
.into())
}
}
pub trait Solver<O: ArgminOp>: Serialize {
const NAME: &'static str = "UNDEFINED";
fn next_iter(
&mut self,
op: &mut OpWrapper<O>,
state: &IterState<O>,
) -> Result<ArgminIterData<O>, Error>;
fn init(
&mut self,
_op: &mut OpWrapper<O>,
_state: &IterState<O>,
) -> Result<Option<ArgminIterData<O>>, Error> {
Ok(None)
}
fn terminate_internal(&mut self, state: &IterState<O>) -> TerminationReason {
let solver_terminate = self.terminate(state);
if solver_terminate.terminated() {
return solver_terminate;
}
if state.get_iter() >= state.get_max_iters() {
return TerminationReason::MaxItersReached;
}
if state.get_cost() <= state.get_target_cost() {
return TerminationReason::TargetCostReached;
}
TerminationReason::NotTerminated
}
fn terminate(&mut self, _state: &IterState<O>) -> TerminationReason {
TerminationReason::NotTerminated
}
}
#[derive(Clone, Debug, Default)]
pub struct ArgminIterData<O: ArgminOp> {
param: Option<O::Param>,
cost: Option<O::Float>,
grad: Option<O::Param>,
hessian: Option<O::Hessian>,
jacobian: Option<O::Jacobian>,
population: Option<Vec<(O::Param, O::Float)>>,
termination_reason: Option<TerminationReason>,
kv: ArgminKV,
}
impl<O: ArgminOp> ArgminIterData<O> {
pub fn new() -> Self {
ArgminIterData {
param: None,
cost: None,
grad: None,
hessian: None,
jacobian: None,
termination_reason: None,
population: None,
kv: make_kv!(),
}
}
pub fn param(mut self, param: O::Param) -> Self {
self.param = Some(param);
self
}
pub fn cost(mut self, cost: O::Float) -> Self {
self.cost = Some(cost);
self
}
pub fn grad(mut self, grad: O::Param) -> Self {
self.grad = Some(grad);
self
}
pub fn hessian(mut self, hessian: O::Hessian) -> Self {
self.hessian = Some(hessian);
self
}
pub fn jacobian(mut self, jacobian: O::Jacobian) -> Self {
self.jacobian = Some(jacobian);
self
}
pub fn population(mut self, population: Vec<(O::Param, O::Float)>) -> Self {
self.population = Some(population);
self
}
pub fn kv(mut self, kv: ArgminKV) -> Self {
self.kv = kv;
self
}
pub fn termination_reason(mut self, reason: TerminationReason) -> Self {
self.termination_reason = Some(reason);
self
}
pub fn get_param(&self) -> Option<O::Param> {
self.param.clone()
}
pub fn get_cost(&self) -> Option<O::Float> {
self.cost
}
pub fn get_grad(&self) -> Option<O::Param> {
self.grad.clone()
}
pub fn get_hessian(&self) -> Option<O::Hessian> {
self.hessian.clone()
}
pub fn get_jacobian(&self) -> Option<O::Jacobian> {
self.jacobian.clone()
}
pub fn get_population(&self) -> Option<&Vec<(O::Param, O::Float)>> {
match &self.population {
Some(population) => Some(&population),
None => None,
}
}
pub fn get_termination_reason(&self) -> Option<TerminationReason> {
self.termination_reason
}
pub fn get_kv(&self) -> ArgminKV {
self.kv.clone()
}
}
pub trait ArgminLineSearch<P, F>: Serialize {
fn set_search_direction(&mut self, direction: P);
fn set_init_alpha(&mut self, step_length: F) -> Result<(), Error>;
}
pub trait ArgminTrustRegion<F>: Clone + Serialize {
fn set_radius(&mut self, radius: F);
}
pub trait ArgminNLCGBetaUpdate<T, F: ArgminFloat>: Serialize {
fn update(&self, nabla_f_k: &T, nabla_f_k_p_1: &T, p_k: &T) -> F;
}