mini_ode/
optimizers.rs

1//! Nonlinear optimization algorithms which are required by some ODE solvers
2//!
3//! The user may create objects containing optimizer configuration and pass it to ODE solver.
4
5use anyhow::anyhow;
6use tch::Tensor;
7
8/// Optimizer interface common for any optimizer in the library
9pub trait Optimizer {
10    /// Solves the problem of optimization of function `function` starting from point `x0`
11    fn optimize(&self, function: &dyn Fn(&Tensor) -> Tensor, x0: &Tensor) -> anyhow::Result<Tensor>;
12}
13
14/// Broyden-Fletcher-Goldfarb-Shanno optimization algorithm
15pub struct BFGS {
16    // Maximum number of optimization steps
17    max_steps: usize,
18    // Minimum gradient
19    gtol: Option<f64>,
20    // minimum change in the objective function between iterations
21    ftol: Option<f64>,
22    // Absolute error tolerance for line search
23    linesearch_atol: f64,
24}
25
26/// Conjugate Gradient optimization algorithm
27pub struct CG {
28    // Maximum number of optimization steps
29    max_steps: usize,
30    // Minimum gradient
31    gtol: Option<f64>,
32    // Minimum change in the objective function between iterations
33    ftol: Option<f64>,
34    // Absolute error tolerance for line search
35    linesearch_atol: f64,
36}
37
38fn differentiate(function: &dyn Fn(&Tensor) -> Tensor, x: &Tensor) -> Tensor {
39    let x_with_grad = x.detach().copy().set_requires_grad(true);
40    let y = function(&x_with_grad);
41
42    tch::Tensor::run_backward(&[y], &[x_with_grad], false, false)[0].copy()
43}
44
45const P0: f64 = 0.000001f64;
46
47const PHI2: f64 = 2.618033988749894848207f64;
48const RPHI: f64 = 0.618033988749894848207f64;
49
50fn choose_step(
51    x0: &Tensor,
52    direction: &Tensor,
53    function: &dyn Fn(&Tensor) -> Tensor,
54    atol: f64,
55) -> Tensor {
56    let (mut x1, mut x2, mut x3, mut x4): (Tensor, Tensor, Tensor, Tensor);
57    let (fx1, mut fx3, mut fx4): (Tensor, Tensor, Tensor);
58
59    let kind = x0.kind();
60
61    fx1 = function(&x0);
62
63    x1 = Tensor::from(0.).to_kind(kind);
64    x2 = Tensor::from(P0).to_kind(kind);
65    while function(&(x0 + direction * &x2)).double_value(&[]) <= fx1.double_value(&[]) {
66        x2 = &x1 + (&x2 - &x1) * PHI2;
67    }
68
69    x3 = x2.copy() - (x2.copy() - &x1) * RPHI;
70    x4 = x1.copy() + (x2.copy() - &x1) * RPHI;
71    fx3 = function(&(x0 + direction * &x3));
72    fx4 = function(&(x0 + direction * &x4));
73    while (x1.copy() - &x2).abs().double_value(&[]) > atol {
74        if fx3.double_value(&[]) < fx4.double_value(&[]) {
75            x2 = x4.copy();
76
77            fx4 = fx3.copy();
78            x3 = x2.copy() - (x2.copy() - &x1) * RPHI;
79            x4 = x1.copy() + (x2.copy() - &x1) * RPHI;
80            fx3 = function(&(x0 + direction * &x3));
81        } else {
82            x1 = x3.copy();
83
84            fx3 = fx4.copy();
85            x3 = x2.copy() - (x2.copy() - &x1) * RPHI;
86            x4 = x1.copy() + (x2.copy() - &x1) * RPHI;
87            fx4 = function(&(x0 + direction * &x4));
88        }
89    }
90
91    direction * ((&x1 + &x2) / 2.)
92}
93
94impl CG {
95    pub fn new(
96        max_steps: usize,
97        gtol: Option<f64>,
98        ftol: Option<f64>,
99        linesearch_atol: Option<f64>,
100    ) -> Self {
101        Self {
102            max_steps,
103            gtol,
104            ftol,
105            linesearch_atol: if let Some(linesearch_atol) = linesearch_atol {
106                linesearch_atol
107            } else {
108                P0
109            },
110        }
111    }
112}
113
114impl Optimizer for CG {
115    fn optimize(&self, function: &dyn Fn(&Tensor) -> Tensor, x0: &Tensor) -> anyhow::Result<Tensor> {
116        // Ensure that rank of the initital guess tensor is 1
117        if x0.size().len() != 1 {
118            return Err(anyhow!("`x0` must have rank 1"));
119        }
120
121        let iters_to_reset = x0.size().len();
122        let mut prev_grad = Tensor::zeros_like(&x0);
123        let mut prev_direction = Tensor::zeros_like(&x0);
124        let mut prev_y: Option<Tensor> = None;
125        let mut x = x0.copy();
126
127        for step_num in 0..self.max_steps {
128            let grad = differentiate(function, &x);
129
130            // Stop if gradient is smaller than `gtol`
131            if let Some(gtol) = self.gtol {
132                if grad.norm().double_value(&[]) < gtol {
133                    return Ok(x);
134                }
135            }
136
137            // Calculate direction according to the Polak-Ribiere formula
138            let direction = match step_num % iters_to_reset {
139                0 => -&grad,
140                _ => {
141                    let beta = grad.squeeze().dot(&(&grad - &prev_grad).squeeze())
142                        / prev_grad.squeeze().dot(&prev_grad.squeeze());
143
144                    -&grad + beta * &prev_direction
145                }
146            };
147
148            // Choose step in direction `direction`
149            let step = choose_step(&x, &direction, &function, self.linesearch_atol);
150
151            // Apply step
152            x = x + step;
153
154            // Stop if change in function value is smaller than `ftol`
155            let y = function(&x);
156            if let (Some(prev_y), Some(ftol)) = (prev_y, self.ftol) {
157                if (&prev_y - &y).double_value(&[]) < ftol {
158                    return Ok(x);
159                }
160            }
161            prev_y = Some(y);
162
163            // Update previous gradient value and previous direction value
164            prev_grad = grad;
165            prev_direction = direction;
166        }
167
168        Ok(x)
169    }
170}
171
172impl BFGS {
173    pub fn new(
174        max_steps: usize,
175        gtol: Option<f64>,
176        ftol: Option<f64>,
177        linesearch_atol: Option<f64>,
178    ) -> Self {
179        Self {
180            max_steps,
181            gtol,
182            ftol,
183            linesearch_atol: if let Some(linesearch_atol) = linesearch_atol {
184                linesearch_atol
185            } else {
186                P0
187            },
188        }
189    }
190}
191
192impl Optimizer for BFGS {
193    fn optimize(&self, function: &dyn Fn(&Tensor) -> Tensor, x0: &Tensor) -> anyhow::Result<Tensor> {
194        // Ensure that rank of the initital guess tensor is 1
195        if x0.size().len() != 1 {
196            return Err(anyhow!("`x0` must have rank 1"));
197        }
198
199        // Determine the device and kind for use in the function
200        let kind = x0.kind();
201        let device = x0.device();
202
203        let identity = Tensor::eye(x0.size()[0], (kind, device));
204        let mut x = x0.copy();
205        let mut appr_inv_h = identity.copy();
206        let mut curr_grad = differentiate(function, &x);
207        let mut curr_y = function(&x);
208
209        // Ensure that output of `function` is a scalar
210        if curr_y.size() != Vec::<i64>::new() {
211            return Err(anyhow!("Output of function `function` must be scalar"));
212        }
213
214        for _ in 0..self.max_steps {
215            // Check for stop condition
216            if let Some(gtol) = self.gtol {
217                if curr_grad.norm().double_value(&[]) < gtol {
218                    return Ok(x);
219                }
220            }
221
222            // Calculate step direction base on the gradient and approximate hessian
223            let direction = (-appr_inv_h.mm(&curr_grad.unsqueeze(1))).squeeze();
224
225            // Choose optimal step in given direction using line search
226            let step = choose_step(&x, &direction, function, self.linesearch_atol);
227
228            // Apply step
229            x = x + &step;
230
231            // Check for stop contition
232            let y = function(&x);
233            if let Some(ftol) = self.ftol {
234                if (curr_y.double_value(&[]) - y.double_value(&[])) < ftol {
235                    return Ok(x);
236                }
237            }
238            curr_y = y;
239
240            let grad = differentiate(function, &x);
241            let gdiff = &grad - &curr_grad;
242
243            // Use Powell's dampening for gamma computation
244            // This prevents gamma from blowing up. Normal formula for gamma is 1/step.dot(gdiff)
245            let gamma = {
246                let delta = 0.0001;
247
248                let sty = step.dot(&gdiff).double_value(&[]);
249                let step_norm_sq = step.dot(&step).double_value(&[]);
250
251                let theta = if sty >= delta * step_norm_sq {
252                    1.
253                } else {
254                    let numerator = (1. - delta) * step_norm_sq;
255                    let denominator = step_norm_sq - sty;
256
257                    if denominator.abs() < 1e-10 {
258                        1.
259                    } else {
260                        (numerator / denominator).min(1.)
261                    }
262                };
263
264                let projection_factor = if step_norm_sq < 1e-10 {
265                    0.
266                } else {
267                    sty / step_norm_sq
268                };
269                let gdiff_prime = &gdiff * theta + &step * ((1. - theta) * projection_factor);
270                let sty_prime = step.dot(&gdiff_prime).double_value(&[]);
271
272                if sty_prime.abs() < 1e-10 {
273                    1. / (delta * step_norm_sq + 1e-10)
274                } else {
275                    1. / sty_prime
276                }
277            };
278
279            // Compute approximation of inverse Hessian
280            appr_inv_h = (&identity - gamma * step.reshape([-1, 1]).mm(&gdiff.reshape([1, -1])))
281                .mm(&appr_inv_h)
282                .mm(&(&identity - gamma * gdiff.reshape([-1, 1]).mm(&step.reshape([1, -1]))))
283                + gamma * step.reshape([-1, 1]).mm(&step.reshape([1, -1]));
284
285            curr_grad = grad;
286        }
287
288        Ok(x)
289    }
290}