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
use rayon::prelude::*;
pub fn line_search(
position: &[f64],
func_grad: &dyn Fn(&[f64]) -> (f64, Vec<f64>),
direction: &[f64],
initial_step_width: f64,
) -> f64 {
let mut step_width = initial_step_width;
let armijo_param = 0.4;
let curvature_param = 0.6;
let mut i = 1;
loop {
i += 1;
let moved_position = position
.par_iter()
.zip(direction.par_iter())
.map(|(x_e, direction_e)| x_e + step_width * direction_e)
.collect::<Vec<_>>();
let tmp1 = func_grad(position);
let tmp2 = func_grad(&moved_position);
let grad_dot_direction = tmp1
.1
.par_iter()
.zip(direction.par_iter())
.map(|(x_e, direction_e)| x_e * direction_e)
.sum::<f64>();
let armijo_left = tmp2.0;
let armijo_right = tmp1.0 + armijo_param * step_width * grad_dot_direction;
if armijo_left <= armijo_right {
step_width -= initial_step_width / i as f64;
continue;
}
let curvature_left = curvature_param * grad_dot_direction;
let curvature_right = tmp2
.1
.par_iter()
.zip(direction.par_iter())
.map(|(x_e, direction_e)| x_e * direction_e)
.sum();
if curvature_left < curvature_right {
step_width += initial_step_width / i as f64;
continue;
}
break;
}
step_width
}