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 std;
use errors::*;
use ndarray::{Array1, arr1};
use ArgminSolver;
use result::ArgminResult;
use termination::TerminationReason;
pub struct BacktrackingLineSearch<'a> {
cost_function: &'a Fn(&Array1<f64>) -> f64,
gradient: &'a Fn(&Array1<f64>) -> Array1<f64>,
alpha: f64,
max_iters: u64,
tau: f64,
c: f64,
state: Option<BacktrackingLineSearchState>,
}
pub struct BacktrackingLineSearchState {
p: Array1<f64>,
x: Array1<f64>,
cost: f64,
t: f64,
fx: f64,
iter: u64,
alpha: f64,
}
impl<'a> BacktrackingLineSearch<'a> {
pub fn new(
cost_function: &'a Fn(&Array1<f64>) -> f64,
gradient: &'a Fn(&Array1<f64>) -> Array1<f64>,
) -> Self {
BacktrackingLineSearch {
cost_function: cost_function,
gradient: gradient,
alpha: 1.0,
max_iters: 100,
tau: 0.5,
c: 0.5,
state: None,
}
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha;
self
}
pub fn max_iters(&mut self, max_iters: u64) -> &mut Self {
self.max_iters = max_iters;
self
}
pub fn c(&mut self, c: f64) -> Result<&mut Self> {
if c >= 1.0 || c <= 0.0 {
return Err(ErrorKind::InvalidParameter(
"BacktrackingLineSearch: Parameter `c` must satisfy 0 < c < 1.".into(),
).into());
}
self.c = c;
Ok(self)
}
pub fn tau(&mut self, tau: f64) -> Result<&mut Self> {
if tau >= 1.0 || tau <= 0.0 {
return Err(ErrorKind::InvalidParameter(
"BacktrackingLineSearch: Parameter `tau` must satisfy 0 < tau < 1.".into(),
).into());
}
self.tau = tau;
Ok(self)
}
}
impl<'a> ArgminSolver<'a> for BacktrackingLineSearch<'a> {
type Parameter = Array1<f64>;
type CostValue = f64;
type Hessian = Array1<f64>;
type StartingPoints = Array1<f64>;
type ProblemDefinition = Array1<f64>;
fn init(&mut self, p: Self::ProblemDefinition, x: &Self::StartingPoints) -> Result<()> {
let m: f64 = p.t().dot(&((self.gradient)(x)));
self.state = Some(BacktrackingLineSearchState {
cost: std::f64::NAN,
p: p,
x: x.to_owned(),
t: -self.c * m,
fx: (self.cost_function)(x),
iter: 0,
alpha: self.alpha,
});
Ok(())
}
fn next_iter(&mut self) -> Result<ArgminResult<Self::Parameter, Self::CostValue>> {
let mut state = self.state.take().unwrap();
let param = &state.x + &(state.alpha * &state.p);
state.cost = (self.cost_function)(¶m);
state.iter += 1;
state.alpha *= self.tau;
let mut out = ArgminResult::new(arr1(&[state.alpha]), std::f64::NAN, state.iter);
self.state = Some(state);
out.set_termination_reason(self.terminate());
Ok(out)
}
make_terminate!(self,
self.state.as_ref().unwrap().iter >= self.max_iters, TerminationReason::MaxItersReached;
self.state.as_ref().unwrap().fx - self.state.as_ref().unwrap().cost >= self.state.as_ref().unwrap().alpha * self.state.as_ref().unwrap().t, TerminationReason::TargetCostReached;
);
make_run!(
Self::ProblemDefinition,
Self::StartingPoints,
Self::Parameter,
Self::CostValue
);
}