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
use autd::{
consts::{DataArray, NUM_TRANS_IN_UNIT},
geometry::Geometry,
prelude::Vector3,
Float, PI,
};
use super::macros::*;
use crate::{
macros::{MatrixXf, VectorXf},
Optimizer,
};
const EPS_1: Float = 1e-8;
const EPS_2: Float = 1e-8;
const TAU: Float = 1e-3;
const K_MAX: usize = 5;
pub struct LM {
pub eps_1: Float,
pub eps_2: Float,
pub tau: Float,
pub k_max: usize,
}
impl LM {
pub fn new() -> Self {
Self::default()
}
}
impl Default for LM {
fn default() -> Self {
Self {
eps_1: EPS_1,
eps_2: EPS_2,
tau: TAU,
k_max: K_MAX,
}
}
}
impl Optimizer for LM {
#[allow(non_snake_case, clippy::many_single_char_names)]
fn optimize(
&self,
geometry: &Geometry,
foci: &[Vector3],
amps: &[Float],
atten: Float,
data: &mut [DataArray],
) {
let m = foci.len();
let n = geometry.num_devices() * NUM_TRANS_IN_UNIT;
let n_param = n + m;
let x0 = VectorXf::zeros(n_param);
let I = MatrixXf::identity(n_param, n_param);
let BhB = make_BhB(geometry, atten, amps, foci, m);
let mut x = x0;
let mut nu = 2.0;
let T = make_T(&x, n, m);
let (mut A, mut g) = calc_JtJ_Jtf(&BhB, &T);
let A_max = A.diagonal().max();
let mut mu = self.tau * A_max;
let mut found = g.max() <= self.eps_1;
let mut Fx = calc_Fx(&BhB, &x, n, m);
const THIRD: Float = 1. / 3.;
for _ in 0..self.k_max {
if found {
break;
}
let h_lm = match (&A + &I.scale(mu)).qr().solve(&g) {
Some(v) => -v,
None => {
break;
}
};
if h_lm.norm() <= self.eps_2 * (x.norm() + self.eps_2) {
found = true;
} else {
let x_new = &x + &h_lm;
let Fx_new = calc_Fx(&BhB, &x_new, n, m);
let L0_Lhlm = 0.5 * h_lm.dot(&(mu * &h_lm - &g));
let rho = (Fx - Fx_new) / L0_Lhlm;
Fx = Fx_new;
if rho > 0.0 {
x = x_new;
let T = make_T(&x, n, m);
let (A_new, g_new) = calc_JtJ_Jtf(&BhB, &T);
A = A_new;
g = g_new;
found = g.max() <= self.eps_1;
mu *= THIRD.max(1. - (2. * rho - 1.).powf(3.));
nu = 2.;
} else {
mu *= nu;
nu *= 2.;
}
}
}
let duty = 0xFF00;
for (d, xe) in data.iter_mut().flatten().zip(x.iter()) {
let phase = (xe % (2.0 * PI)) / (2.0 * PI);
let phase = (255.0 * (1.0 - phase)) as u16;
*d = duty | phase;
}
}
}