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
use crate::prelude::*;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::fmt::Debug;
#[derive(Clone, Serialize, Deserialize)]
pub struct LBFGS<L, P, F> {
linesearch: L,
m: usize,
s: VecDeque<P>,
y: VecDeque<P>,
tol_grad: F,
tol_cost: F,
}
impl<L, P, F: ArgminFloat> LBFGS<L, P, F> {
pub fn new(linesearch: L, m: usize) -> Self {
LBFGS {
linesearch,
m,
s: VecDeque::with_capacity(m),
y: VecDeque::with_capacity(m),
tol_grad: F::epsilon().sqrt(),
tol_cost: F::epsilon(),
}
}
pub fn with_tol_grad(mut self, tol_grad: F) -> Self {
self.tol_grad = tol_grad;
self
}
pub fn with_tol_cost(mut self, tol_cost: F) -> Self {
self.tol_cost = tol_cost;
self
}
}
impl<O, L, P, F> Solver<O> for LBFGS<L, P, F>
where
O: ArgminOp<Param = P, Output = F, Float = F>,
O::Param: Clone
+ Serialize
+ DeserializeOwned
+ Debug
+ Default
+ ArgminSub<O::Param, O::Param>
+ ArgminAdd<O::Param, O::Param>
+ ArgminDot<O::Param, O::Float>
+ ArgminScaledAdd<O::Param, O::Float, O::Param>
+ ArgminNorm<O::Float>
+ ArgminMul<O::Float, O::Param>,
O::Hessian: Clone + Default + Serialize + DeserializeOwned,
L: Clone + ArgminLineSearch<O::Param, O::Float> + Solver<OpWrapper<O>>,
F: ArgminFloat,
{
const NAME: &'static str = "L-BFGS";
fn init(
&mut self,
op: &mut OpWrapper<O>,
state: &IterState<O>,
) -> Result<Option<ArgminIterData<O>>, Error> {
let param = state.get_param();
let cost = op.apply(¶m)?;
let grad = op.gradient(¶m)?;
Ok(Some(
ArgminIterData::new().param(param).cost(cost).grad(grad),
))
}
fn next_iter(
&mut self,
op: &mut OpWrapper<O>,
state: &IterState<O>,
) -> Result<ArgminIterData<O>, Error> {
let param = state.get_param();
let cur_cost = state.get_cost();
let prev_grad = state.get_grad().unwrap();
let gamma: F = if let (Some(ref sk), Some(ref yk)) = (self.s.back(), self.y.back()) {
sk.dot(*yk) / yk.dot(*yk)
} else {
F::from_f64(1.0).unwrap()
};
let mut q = prev_grad.clone();
let cur_m = self.s.len();
let mut alpha: Vec<F> = vec![F::from_f64(0.0).unwrap(); cur_m];
let mut rho: Vec<F> = vec![F::from_f64(0.0).unwrap(); cur_m];
for (i, (ref sk, ref yk)) in self.s.iter().rev().zip(self.y.iter().rev()).enumerate() {
let sk = *sk;
let yk = *yk;
let yksk: F = yk.dot(sk);
let rho_t = F::from_f64(1.0).unwrap() / yksk;
let skq: F = sk.dot(&q);
let alpha_t = skq.mul(rho_t);
q = q.sub(&yk.mul(&alpha_t));
rho[cur_m - i - 1] = rho_t;
alpha[cur_m - i - 1] = alpha_t;
}
let mut r = q.mul(&gamma);
for (i, (ref sk, ref yk)) in self.s.iter().zip(self.y.iter()).enumerate() {
let sk = *sk;
let yk = *yk;
let beta = yk.dot(&r).mul(rho[i]);
r = r.add(&sk.mul(&(alpha[i] - beta)));
}
self.linesearch
.set_search_direction(r.mul(&F::from_f64(-1.0).unwrap()));
let ArgminResult {
operator: line_op,
state:
IterState {
param: xk1,
cost: next_cost,
..
},
} = Executor::new(
OpWrapper::new_from_wrapper(op),
self.linesearch.clone(),
param.clone(),
)
.grad(prev_grad.clone())
.cost(cur_cost)
.ctrlc(false)
.run()?;
op.consume_op(line_op);
if state.get_iter() >= self.m as u64 {
self.s.pop_front();
self.y.pop_front();
}
let grad = op.gradient(&xk1)?;
self.s.push_back(xk1.sub(¶m));
self.y.push_back(grad.sub(&prev_grad));
Ok(ArgminIterData::new()
.param(xk1)
.cost(next_cost)
.grad(grad)
.kv(make_kv!("gamma" => gamma;)))
}
fn terminate(&mut self, state: &IterState<O>) -> TerminationReason {
if state.get_grad().unwrap().norm() < self.tol_grad {
return TerminationReason::TargetPrecisionReached;
}
if (state.get_prev_cost() - state.get_cost()).abs() < self.tol_cost {
return TerminationReason::NoChangeInCost;
}
TerminationReason::NotTerminated
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::solver::linesearch::MoreThuenteLineSearch;
use crate::test_trait_impl;
type Operator = MinimalNoOperator;
test_trait_impl!(lbfgs, LBFGS<Operator, MoreThuenteLineSearch<Operator, f64>, f64>);
#[test]
fn test_tolerances() {
let linesearch: MoreThuenteLineSearch<f64, f64> =
MoreThuenteLineSearch::new().c(1e-4, 0.9).unwrap();
let tol1 = 1e-4;
let tol2 = 1e-2;
let LBFGS {
tol_grad: t1,
tol_cost: t2,
..
}: LBFGS<_, f64, f64> = LBFGS::new(linesearch, 7)
.with_tol_grad(tol1)
.with_tol_cost(tol2);
assert!((t1 - tol1).abs() < std::f64::EPSILON);
assert!((t2 - tol2).abs() < std::f64::EPSILON);
}
}