use derive_new::new;
use burn_core::tensor::Device;
use crate::TransitionBatch;
#[derive(Clone, new)]
pub struct ActionContext<A, C> {
pub context: C,
pub action: A,
}
pub trait PolicyState {
type Record;
fn into_record(self) -> Self::Record;
fn load_record(&self, record: Self::Record) -> Self;
}
pub trait ToObservation<O> {
fn to_observation(&self, device: &Device) -> O;
}
pub trait ToAction<A> {
fn to_action(&self, device: &Device) -> A;
}
pub trait Policy: Clone {
type Observation;
type ActionDistribution;
type Action;
type ActionContext;
type PolicyState: PolicyState;
fn forward(&mut self, obs: Self::Observation) -> Self::ActionDistribution;
fn action(
&mut self,
obs: Self::Observation,
deterministic: bool,
) -> (Self::Action, Vec<Self::ActionContext>);
fn update(&mut self, update: Self::PolicyState);
fn state(&self) -> Self::PolicyState;
fn to_device(self, device: &Device) -> Self;
fn load_record(self, record: <Self::PolicyState as PolicyState>::Record) -> Self;
}
pub trait Batchable: Sized {
fn batch(value: Vec<Self>) -> Self;
fn unbatch(self) -> Vec<Self>;
}
pub struct RLTrainOutput<TO, P> {
pub policy: P,
pub item: TO,
}
pub type LearnerTransitionBatch<P> =
TransitionBatch<<P as Policy>::Observation, <P as Policy>::Action>;
pub trait PolicyLearner
where
<Self::InnerPolicy as Policy>::Observation: Clone + Batchable,
<Self::InnerPolicy as Policy>::ActionDistribution: Clone + Batchable,
<Self::InnerPolicy as Policy>::Action: Clone + Batchable,
{
type TrainContext;
type InnerPolicy: Policy;
type Record;
fn train(
&mut self,
input: LearnerTransitionBatch<Self::InnerPolicy>,
) -> RLTrainOutput<Self::TrainContext, <Self::InnerPolicy as Policy>::PolicyState>;
fn policy(&self) -> Self::InnerPolicy;
fn update_policy(&mut self, update: Self::InnerPolicy);
fn record(&self) -> Self::Record;
fn load_record(self, record: Self::Record) -> Self;
fn device(&self) -> Device;
}