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
use crate::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct SteepestDescent<L> {
linesearch: L,
}
impl<L> SteepestDescent<L> {
pub fn new(linesearch: L) -> Self {
SteepestDescent { linesearch }
}
}
impl<O, L> Solver<O> for SteepestDescent<L>
where
O: ArgminOp<Output = f64>,
O::Param: Clone
+ Default
+ Serialize
+ ArgminSub<O::Param, O::Param>
+ ArgminDot<O::Param, f64>
+ ArgminScaledAdd<O::Param, f64, O::Param>
+ ArgminMul<f64, O::Param>
+ ArgminSub<O::Param, O::Param>
+ ArgminNorm<f64>,
O::Hessian: Default,
L: Clone + ArgminLineSearch<O::Param> + Solver<OpWrapper<O>>,
{
const NAME: &'static str = "Steepest Descent";
fn next_iter(
&mut self,
op: &mut OpWrapper<O>,
state: &IterState<O>,
) -> Result<ArgminIterData<O>, Error> {
let param_new = state.get_param();
let new_cost = op.apply(¶m_new)?;
let new_grad = op.gradient(¶m_new)?;
self.linesearch.set_search_direction(new_grad.mul(&(-1.0)));
let ArgminResult {
operator: line_op,
state:
IterState {
param: next_param,
cost: next_cost,
..
},
} = Executor::new(
OpWrapper::new_from_op(&op),
self.linesearch.clone(),
param_new,
)
.grad(new_grad)
.cost(new_cost)
.ctrlc(false)
.run()?;
op.consume_op(line_op);
Ok(ArgminIterData::new().param(next_param).cost(next_cost))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::send_sync_test;
use crate::solver::linesearch::MoreThuenteLineSearch;
send_sync_test!(
steepest_descent,
SteepestDescent<MoreThuenteLineSearch<Vec<f64>>>
);
}