Skip to main content

burn_train/metric/rl/
exploration_rate.rs

1use std::sync::Arc;
2
3use super::super::{
4    MetricAttributes, MetricMetadata, NumericAttributes, NumericEntry,
5    state::{FormatOptions, NumericMetricState},
6};
7use crate::metric::{Metric, MetricName, Numeric, SerializedEntry};
8use burn_core::tensor::TensorReadError;
9
10/// Metric for the length of the last completed episode.
11#[derive(Clone)]
12pub struct ExplorationRateMetric {
13    name: MetricName,
14    state: NumericMetricState,
15}
16
17impl ExplorationRateMetric {
18    /// Creates a new episode length metric.
19    pub fn new() -> Self {
20        Self {
21            name: Arc::new("Exploration rate".to_string()),
22            state: NumericMetricState::new(),
23        }
24    }
25}
26
27impl Default for ExplorationRateMetric {
28    fn default() -> Self {
29        Self::new()
30    }
31}
32
33/// The [ExplorationRateMetric](ExplorationRateMetric) input type.
34#[derive(new)]
35pub struct ExplorationRateInput {
36    exploration_rate: f64,
37}
38
39impl Metric for ExplorationRateMetric {
40    type Input = ExplorationRateInput;
41
42    fn update(
43        &mut self,
44        item: &ExplorationRateInput,
45        _metadata: &MetricMetadata,
46    ) -> Result<SerializedEntry, TensorReadError> {
47        self.state.update(item.exploration_rate, 1);
48        Ok(self
49            .state
50            .compute_update(FormatOptions::new(self.name()).precision(3)))
51    }
52
53    fn compute(&mut self) -> Result<SerializedEntry, TensorReadError> {
54        Ok(self
55            .state
56            .compute_final(FormatOptions::new(self.name()).precision(3)))
57    }
58
59    fn clear(&mut self) {
60        self.state.reset()
61    }
62
63    fn name(&self) -> MetricName {
64        self.name.clone()
65    }
66
67    fn attributes(&self) -> MetricAttributes {
68        NumericAttributes {
69            unit: Some(String::from("%")),
70            higher_is_better: false,
71        }
72        .into()
73    }
74}
75
76impl Numeric for ExplorationRateMetric {
77    fn value(&self) -> Option<NumericEntry> {
78        Some(self.state.current_value())
79    }
80
81    fn running_value(&self) -> Option<NumericEntry> {
82        Some(self.state.running_value())
83    }
84
85    fn final_value(&self) -> NumericEntry {
86        self.state.final_value()
87    }
88}