use burn_tensor::backend::Backend;
use relayrl_types::prelude::records::{ArrowTrajectory, CsvTrajectory};
use relayrl_types::prelude::tensor::relayrl::BackendMatcher;
use relayrl_types::prelude::trajectory::RelayRLTrajectory;
use thiserror::Error;
#[derive(Clone, Debug, Error)]
pub enum AlgorithmError {
#[error("Initialization failed: {0}")]
InitializationError(String),
#[error("Insertion of trajectory failed: {0}")]
TrajectoryInsertionError(String),
#[error("Buffer sampling failed: {0}")]
BufferSamplingError(String),
#[error("Kernel registration failed: {0}")]
KernelRegistrationError(String),
#[error("Invalid specification: {0}")]
InvalidSpec(String),
#[error(transparent)]
NeuralNetworkError(#[from] crate::algorithms::NeuralNetworkError),
}
#[allow(clippy::large_enum_variant)]
pub enum TrajectoryType {
RelayRL(RelayRLTrajectory),
Csv(CsvTrajectory),
Arrow(ArrowTrajectory),
}
pub trait TrajectoryData {
fn into_relayrl(self) -> Option<RelayRLTrajectory>;
}
impl TrajectoryData for RelayRLTrajectory {
fn into_relayrl(self) -> Option<RelayRLTrajectory> {
Some(self)
}
}
impl TrajectoryData for CsvTrajectory {
fn into_relayrl(self) -> Option<RelayRLTrajectory> {
self.trajectory
}
}
impl TrajectoryData for ArrowTrajectory {
fn into_relayrl(self) -> Option<RelayRLTrajectory> {
self.trajectory
}
}
pub trait AlgorithmTrait<T: TrajectoryData> {
#[allow(async_fn_in_trait)]
async fn receive_trajectory(&mut self, trajectory: T) -> Result<bool, AlgorithmError>;
fn train_model(&mut self);
fn log_epoch(&mut self);
fn save_model(&self, filename: &str);
fn acquire_model<B: Backend + BackendMatcher<Backend = B>>(
&self,
) -> Option<relayrl_types::model::ModelModule<B>>;
}