use std::marker::PhantomData;
use burn_rl::{
Batchable, Environment, EnvironmentInit, Policy, PolicyLearner, PolicyState, ToAction,
ToObservation,
};
use crate::checkpoint::Checkpoint;
use crate::{AgentEvaluationEvent, AsyncProcessorTraining, ItemLazy, RLEvent};
pub trait RLComponentsTypes {
type Env: Environment<State = Self::State, Action = Self::Action> + 'static;
type EnvInit: EnvironmentInit<Self::Env> + Send + 'static;
type State: ToObservation<<Self::Policy as Policy>::Observation> + Clone + Send + 'static;
type Action: From<<Self::Policy as Policy>::Action>
+ ToAction<<Self::Policy as Policy>::Action>
+ Clone
+ Send
+ 'static;
type Policy: Policy<
Observation = Self::PolicyObs,
ActionDistribution = Self::PolicyAD,
Action = Self::PolicyAction,
ActionContext = Self::ActionContext,
PolicyState = Self::PolicyState,
> + Send
+ 'static;
type PolicyObs: Clone + Send + Batchable + 'static;
type PolicyAD: Clone + Send + Batchable;
type PolicyAction: Clone + Send + Batchable;
type ActionContext: ItemLazy + Clone + Send + 'static;
type PolicyState: Clone + Send + PolicyState<Record: Checkpoint> + 'static;
type LearningAgent: PolicyLearner<
TrainContext = Self::TrainingOutput,
InnerPolicy = Self::Policy,
Record: Checkpoint,
> + Send
+ 'static;
type TrainingOutput: ItemLazy + Clone + Send;
}
pub struct RLComponentsMarker<E, EI, A> {
_env: PhantomData<E>,
_env_init: PhantomData<EI>,
_agent: PhantomData<A>,
}
impl<E, EI, A> RLComponentsTypes for RLComponentsMarker<E, EI, A>
where
E: Environment + 'static,
EI: EnvironmentInit<E> + Send + 'static,
A: PolicyLearner + Send + 'static,
<A as PolicyLearner>::Record: Checkpoint,
A::TrainContext: ItemLazy + Clone + Send,
A::InnerPolicy: Policy + Send,
<A::InnerPolicy as Policy>::Observation: Batchable + Clone + Send,
<A::InnerPolicy as Policy>::ActionDistribution: Batchable + Clone + Send,
<A::InnerPolicy as Policy>::Action: Batchable + Clone + Send,
<A::InnerPolicy as Policy>::ActionContext: ItemLazy + Clone + Send + 'static,
<A::InnerPolicy as Policy>::PolicyState: Clone + Send,
<<A::InnerPolicy as Policy>::PolicyState as PolicyState>::Record: Checkpoint,
E::State: ToObservation<<A::InnerPolicy as Policy>::Observation> + Clone + Send + 'static,
E::Action: From<<A::InnerPolicy as Policy>::Action>
+ ToAction<<A::InnerPolicy as Policy>::Action>
+ Clone
+ Send
+ 'static,
{
type Env = E;
type EnvInit = EI;
type LearningAgent = A;
type Policy = A::InnerPolicy;
type PolicyObs = <A::InnerPolicy as Policy>::Observation;
type PolicyAD = <A::InnerPolicy as Policy>::ActionDistribution;
type PolicyAction = <A::InnerPolicy as Policy>::Action;
type ActionContext = <A::InnerPolicy as Policy>::ActionContext;
type PolicyState = <A::InnerPolicy as Policy>::PolicyState;
type TrainingOutput = A::TrainContext;
type State = E::State;
type Action = E::Action;
}
pub(crate) type RlPolicy<RLC> =
<<RLC as RLComponentsTypes>::LearningAgent as PolicyLearner>::InnerPolicy;
pub type RLEventProcessorType<RLC> = AsyncProcessorTraining<
RLEvent<<RLC as RLComponentsTypes>::TrainingOutput, <RLC as RLComponentsTypes>::ActionContext>,
AgentEvaluationEvent<<RLC as RLComponentsTypes>::ActionContext>,
>;
pub type RLPolicyRecord<RLC> =
<<<RLC as RLComponentsTypes>::Policy as Policy>::PolicyState as PolicyState>::Record;
pub type RLAgentRecord<RLC> = <<RLC as RLComponentsTypes>::LearningAgent as PolicyLearner>::Record;