spectator/
message.rs

1// Copyright 2018-2024 argmin developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use std::collections::{HashMap, HashSet};
9
10use anyhow::Error;
11use argmin::core::{TerminationStatus, KV};
12use bytes::{Bytes, BytesMut};
13use serde::{Deserialize, Serialize};
14use time::Duration;
15
16/// Enum used to encode information sent to spectator.
17#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
18pub enum Message {
19    /// Register a new run
20    NewRun {
21        /// Name of the run
22        name: String,
23        /// Name of the solver
24        solver: String,
25        /// Maximum number of iterations
26        max_iter: u64,
27        /// Target cost function value
28        target_cost: f64,
29        /// Initial parameter vector
30        init_param: Option<Vec<f64>>,
31        /// Solver-specific settings (returned by the `init` method of the `Solver` trait)
32        settings: KV,
33        /// Preselected metrics
34        selected: HashSet<String>,
35    },
36    /// A set of metrics samples sent after an iteration
37    Samples {
38        /// Name of the run
39        name: String,
40        /// Current iteration
41        iter: u64,
42        /// Time needed for this iteration
43        time: Duration,
44        /// Current termination_status
45        termination_status: TerminationStatus,
46        /// Solver-specific metrics
47        kv: KV,
48    },
49    /// Function evaluation counts (Cost function, gradient, Hessian, ...)
50    FuncCounts {
51        /// Name of the run
52        name: String,
53        /// Current iteration
54        iter: u64,
55        /// Function counts
56        kv: HashMap<String, u64>,
57    },
58    /// Parameter vector
59    Param {
60        /// Name of the run
61        name: String,
62        /// Current iteration
63        iter: u64,
64        /// Current parameter vector
65        param: Vec<f64>,
66    },
67    /// Current best parameter vector
68    BestParam {
69        /// Name of the run
70        name: String,
71        /// Current iteration
72        iter: u64,
73        /// Current best parameter vector
74        param: Vec<f64>,
75    },
76    /// Termination
77    Termination {
78        /// Name of the run
79        name: String,
80        /// Termination status
81        termination_status: TerminationStatus,
82    },
83}
84
85impl Message {
86    /// Serialize message
87    #[allow(unused)]
88    pub fn pack(&self) -> Result<Bytes, Error> {
89        let buf = rmp_serde::encode::to_vec(&self)?;
90        Ok(Bytes::from(buf))
91    }
92
93    /// Deserialize message
94    #[allow(unused)]
95    pub fn unpack(buf: &BytesMut) -> Result<Self, Error> {
96        Ok(rmp_serde::from_slice::<Message>(buf)?)
97    }
98}