border_tch_agent/sac/critic/
config.rs1use crate::opt::OptimizerConfig;
2use anyhow::Result;
3use serde::{de::DeserializeOwned, Deserialize, Serialize};
4use std::{
5 fs::File,
6 io::{BufReader, Write},
7 path::Path,
8};
9
10#[allow(clippy::upper_case_acronyms)]
11#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
12pub struct CriticConfig<Q> {
14 pub q_config: Option<Q>,
15 pub opt_config: OptimizerConfig,
16}
17
18impl<Q> Default for CriticConfig<Q> {
19 fn default() -> Self {
20 Self {
21 q_config: None,
22 opt_config: OptimizerConfig::Adam { lr: 0.0 },
23 }
24 }
25}
26
27impl<Q> CriticConfig<Q>
28where
29 Q: DeserializeOwned + Serialize,
30{
31 pub fn q_config(mut self, v: Q) -> Self {
33 self.q_config = Some(v);
34 self
35 }
36
37 pub fn opt_config(mut self, v: OptimizerConfig) -> Self {
39 self.opt_config = v;
40 self
41 }
42
43 pub fn load(path: impl AsRef<Path>) -> Result<Self> {
45 let file = File::open(path)?;
46 let rdr = BufReader::new(file);
47 let b = serde_yaml::from_reader(rdr)?;
48 Ok(b)
49 }
50
51 pub fn save(&self, path: impl AsRef<Path>) -> Result<()> {
53 let mut file = File::create(path)?;
54 file.write_all(serde_yaml::to_string(&self)?.as_bytes())?;
55 Ok(())
56 }
57}