1mod cobyla;
11mod derivative_free;
12mod gradient;
13mod newton_cg;
14
15pub use derivative_free::{Bobyqa, Cobyla};
16pub use gradient::{LBfgs, NewtonExact};
17pub use newton_cg::NewtonCg;
18
19use nalgebra::DMatrix;
20use serde::{Deserialize, Serialize};
21
22use crate::error::{Error, Result};
23use crate::time::Instant;
24
25#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
27pub struct Eval {
28 pub cost: f64,
30 pub fidelity: f64,
32 pub penalty: f64,
34}
35
36impl Eval {
37 pub fn score(&self) -> f64 {
39 self.fidelity - self.penalty
40 }
41}
42
43pub trait Objective: Sync {
45 fn dim(&self) -> usize;
47 fn value(&self, x: &[f64]) -> Result<Eval>;
49 fn gradient(&self, x: &[f64]) -> Result<(Eval, Vec<f64>)>;
51 fn hessian(&self, x: &[f64]) -> Result<(Eval, Vec<f64>, DMatrix<f64>)>;
53 fn hvp(&self, x: &[f64], v: &[f64]) -> Result<Vec<f64>> {
56 let (_, _, h) = self.hessian(x)?;
57 Ok((h * nalgebra::DVector::from_column_slice(v)).as_slice().to_vec())
58 }
59}
60
61#[derive(Clone, Copy, Debug, PartialEq, Eq)]
63pub enum Derivatives {
64 None,
66 Gradient,
68 Hessian,
70}
71
72#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
74pub struct IterationReport {
75 pub iteration: usize,
78 pub fidelity: f64,
80 pub penalty: f64,
82 pub cost: f64,
84 pub grad_norm: Option<f64>,
86 pub evaluations: usize,
88 pub elapsed_s: f64,
90}
91
92pub trait ProgressSink {
94 fn on_iteration(&mut self, report: &IterationReport);
96 fn should_cancel(&self) -> bool {
98 false
99 }
100}
101
102#[derive(Clone, Copy, Debug, Default)]
104pub struct NoProgress;
105
106impl ProgressSink for NoProgress {
107 fn on_iteration(&mut self, _: &IterationReport) {}
108}
109
110#[derive(Clone, Debug, Default)]
112pub struct RecordingSink {
113 pub reports: Vec<IterationReport>,
115}
116
117impl ProgressSink for RecordingSink {
118 fn on_iteration(&mut self, report: &IterationReport) {
119 self.reports.push(report.clone());
120 }
121}
122
123#[derive(Clone, Copy, Debug)]
125pub struct ConsoleSink {
126 pub every: usize,
128 header: bool,
129}
130
131impl ConsoleSink {
132 pub fn new(every: usize) -> Self {
134 ConsoleSink {
135 every: every.max(1),
136 header: false,
137 }
138 }
139}
140
141impl ProgressSink for ConsoleSink {
142 fn on_iteration(&mut self, r: &IterationReport) {
143 if !self.header {
144 println!(
145 "{:>9} {:>10} {:>10} {:>10} {:>9}",
146 "iteration", "fidelity", "penalty", "|grad|", "time (s)"
147 );
148 self.header = true;
149 }
150 if r.iteration.is_multiple_of(self.every) {
151 let g = r.grad_norm.map_or("-".to_string(), |g| format!("{g:.3e}"));
152 println!(
153 "{:>9} {:>10.6} {:>10.3e} {:>10} {:>9.2}",
154 r.iteration, r.fidelity, r.penalty, g, r.elapsed_s
155 );
156 }
157 }
158}
159
160#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
162pub enum Exit {
163 TargetReached,
165 Converged,
167 MaxIterations,
169 Cancelled,
171 Failed(String),
173}
174
175impl Exit {
176 pub fn message(&self) -> String {
178 match self {
179 Exit::TargetReached => "target fidelity reached".into(),
180 Exit::Converged => "converged".into(),
181 Exit::MaxIterations => "iteration limit reached".into(),
182 Exit::Cancelled => "cancelled".into(),
183 Exit::Failed(m) => format!("stopped: {m}"),
184 }
185 }
186}
187
188#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
190pub struct OptimResult {
191 pub x: Vec<f64>,
193 pub eval: Eval,
195 pub iterations: usize,
197 pub evaluations: usize,
199 pub exit: Exit,
201}
202
203pub struct RunControl<'a> {
205 pub max_iter: usize,
207 pub target_fidelity: f64,
209 pub sink: &'a mut dyn ProgressSink,
211}
212
213pub trait Optimizer: Send + Sync {
215 fn name(&self) -> &'static str;
217 fn derivatives(&self) -> Derivatives;
219 fn minimize(&self, obj: &dyn Objective, x0: &[f64], ctl: &mut RunControl) -> Result<OptimResult>;
221}
222
223pub fn optimizer(name: &str) -> Result<Box<dyn Optimizer>> {
225 Ok(match name {
226 "l-bfgs" => Box::new(LBfgs),
227 "newton-cg" => Box::new(NewtonCg),
228 "newton-exact" => Box::new(NewtonExact),
229 "cobyla" => Box::new(Cobyla),
230 "bobyqa" => Box::new(Bobyqa),
231 _ => {
232 return Err(Error::NotSupported(format!(
233 "algorithm \"{name}\" is not available; choose one of {}",
234 optimizer_names().join(", ")
235 )));
236 }
237 })
238}
239
240pub fn optimizer_names() -> &'static [&'static str] {
242 &["l-bfgs", "newton-cg", "newton-exact", "cobyla", "bobyqa"]
243}
244
245pub struct Monitor<'s> {
247 sink: &'s mut dyn ProgressSink,
248 target: f64,
249 max_iter: usize,
250 started: Instant,
251 pub evaluations: usize,
253 pub best: Option<(Vec<f64>, Eval)>,
255 pub exit: Option<Exit>,
257}
258
259impl<'s> Monitor<'s> {
260 pub fn new(ctl: &'s mut RunControl) -> Monitor<'s> {
262 Monitor {
263 target: ctl.target_fidelity,
264 max_iter: ctl.max_iter,
265 sink: &mut *ctl.sink,
266 started: Instant::now(),
267 evaluations: 0,
268 best: None,
269 exit: None,
270 }
271 }
272
273 pub fn evaluated(&mut self, x: &[f64], e: Eval) {
275 self.evaluations += 1;
276 if self.best.as_ref().is_none_or(|(_, b)| e.cost < b.cost) {
277 self.best = Some((x.to_vec(), e));
278 }
279 }
280
281 pub fn report(&mut self, iteration: usize, e: Eval, grad_norm: Option<f64>) {
283 self.sink.on_iteration(&IterationReport {
284 iteration,
285 fidelity: e.fidelity,
286 penalty: e.penalty,
287 cost: e.cost,
288 grad_norm,
289 evaluations: self.evaluations,
290 elapsed_s: self.started.elapsed().as_secs_f64(),
291 });
292 if self.exit.is_some() {
293 return;
294 }
295 if e.score() >= self.target {
296 self.exit = Some(Exit::TargetReached);
297 } else if self.sink.should_cancel() {
298 self.exit = Some(Exit::Cancelled);
299 } else if iteration >= self.max_iter {
300 self.exit = Some(Exit::MaxIterations);
301 }
302 }
303
304 pub fn finish(self, iterations: usize, exit: Exit) -> Result<OptimResult> {
306 let exit = self.exit.unwrap_or(exit);
307 match self.best {
308 Some((x, eval)) => Ok(OptimResult {
309 x,
310 eval,
311 iterations,
312 evaluations: self.evaluations,
313 exit,
314 }),
315 None => Err(match exit {
316 Exit::Failed(m) => Error::Numerical(m),
317 other => Error::Numerical(format!(
318 "the optimiser stopped ({}) before any evaluation",
319 other.message()
320 )),
321 }),
322 }
323 }
324}