use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::{
default::Default,
fs::File,
io::{BufReader, Write},
path::Path,
};
use super::{WeightNormalizer, WeightNormalizer::All};
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct PerConfig {
pub(super) alpha: f32,
pub(super) beta_0: f32,
pub(super) beta_final: f32,
pub(super) n_opts_final: usize,
pub(super) normalize: WeightNormalizer,
}
impl Default for PerConfig {
fn default() -> Self {
Self {
alpha: 0.6,
beta_0: 0.4,
beta_final: 1.0,
n_opts_final: 500_000,
normalize: All,
}
}
}
impl PerConfig {
pub fn alpha(mut self, alpha: f32) -> Self {
self.alpha = alpha;
self
}
pub fn beta_0(mut self, beta_0: f32) -> Self {
self.beta_0 = beta_0;
self
}
pub fn beta_final(mut self, beta_final: f32) -> Self {
self.beta_final = beta_final;
self
}
pub fn n_opts_final(mut self, n_opts_final: usize) -> Self {
self.n_opts_final = n_opts_final;
self
}
pub fn normalize(mut self, normalize: WeightNormalizer) -> Self {
self.normalize = normalize;
self
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct SimpleReplayBufferConfig {
pub(super) capacity: usize,
pub(super) seed: u64,
pub(super) per_config: Option<PerConfig>,
}
impl Default for SimpleReplayBufferConfig {
fn default() -> Self {
Self {
capacity: 10000,
seed: 42,
per_config: None,
}
}
}
impl SimpleReplayBufferConfig {
pub fn capacity(mut self, capacity: usize) -> Self {
self.capacity = capacity;
self
}
pub fn per_config(mut self, per_config: Option<PerConfig>) -> Self {
self.per_config = per_config;
self
}
pub fn load(path: impl AsRef<Path>) -> Result<Self> {
let file = File::open(path)?;
let rdr = BufReader::new(file);
let b = serde_yaml::from_reader(rdr)?;
Ok(b)
}
pub fn save(&self, path: impl AsRef<Path>) -> Result<()> {
let mut file = File::create(path)?;
file.write_all(serde_yaml::to_string(&self)?.as_bytes())?;
Ok(())
}
}