Skip to main content

hydr_structures/structures/controlled_gate/
batch.rs

1//! Owning atomic sampled-controller and actual gate-response candidates.
2
3use super::{ControlledGateEvent, ControlledGateTrial};
4use crate::structures::tensor::controlled_gate::TensorControlledGateContext;
5use crate::structures::{
6    ControlEvent, ControlSample, GateActuatorBatch, GateActuatorEvent, OperatingControlBatch,
7    StructureError,
8};
9use numr::dtype::DType;
10use numr::runtime::Runtime;
11use std::sync::Arc;
12
13/// Complete accepted sampled commands and separately modelled actual gate response.
14#[derive(Clone, Debug)]
15pub struct ControlledGateBatch<R: Runtime<DType = DType>> {
16    pub(super) context: Arc<TensorControlledGateContext<R>>,
17    pub(crate) controls: OperatingControlBatch<R>,
18    pub(crate) actuators: GateActuatorBatch<R>,
19}
20
21impl<R: Runtime<DType = DType>> ControlledGateBatch<R> {
22    /// Bind fresh initial children with one sampled controller per supplied actual actuator.
23    pub fn new(
24        controls: OperatingControlBatch<R>,
25        actuators: GateActuatorBatch<R>,
26    ) -> Result<Self, StructureError> {
27        let context = Arc::new(TensorControlledGateContext::new(&controls, &actuators)?);
28        Ok(Self {
29            context,
30            controls,
31            actuators,
32        })
33    }
34
35    /// Accepted command continuation, exposed immutably for explicit projections.
36    pub fn controls(&self) -> &OperatingControlBatch<R> {
37        &self.controls
38    }
39
40    /// Accepted actual response, exposed immutably for operating-source frames and projections.
41    pub fn actuators(&self) -> &GateActuatorBatch<R> {
42        &self.actuators
43    }
44
45    /// Common accepted time in seconds.
46    pub fn time(&self) -> f64 {
47        self.controls.time()
48    }
49
50    /// Common accepted positive-duration interval count.
51    pub fn accepted_steps(&self) -> usize {
52        self.controls.accepted_steps()
53    }
54
55    /// Next decision, actuator arrival boundary, or common terminal boundary.
56    pub fn next_event(&self) -> Result<Option<ControlledGateEvent>, StructureError>
57    where
58        R::Client: numr::ops::BinaryOps<R>
59            + numr::ops::CompareOps<R>
60            + numr::ops::ConditionalOps<R>
61            + numr::ops::ReduceOps<R>,
62    {
63        let control = self.controls.next_event();
64        let actuator = self.actuators.next_event()?;
65        Ok(Self::common(control, actuator))
66    }
67
68    /// Next decision, actuator arrival window, or common terminal boundary of a coordinator.
69    ///
70    /// Each actuator arrival window counts as one event, as in
71    /// `GateActuatorBatch::next_coordinated_event`. Decisions keep their exact times.
72    #[doc(hidden)]
73    pub fn next_coordinated_event(&self) -> Result<Option<ControlledGateEvent>, StructureError>
74    where
75        R::Client: numr::ops::BinaryOps<R>
76            + numr::ops::CompareOps<R>
77            + numr::ops::ConditionalOps<R>
78            + numr::ops::ReduceOps<R>,
79    {
80        let control = self.controls.next_event();
81        let actuator = self.actuators.next_coordinated_event()?;
82        Ok(Self::common(control, actuator))
83    }
84
85    /// The common event exactly at `end_s`, or `None` when `end_s` holds no decision, actuator
86    /// event, or terminal boundary.
87    pub(crate) fn event_at(&self, end_s: f64) -> Result<Option<ControlledGateEvent>, StructureError>
88    where
89        R::Client: numr::ops::BinaryOps<R>
90            + numr::ops::CompareOps<R>
91            + numr::ops::ConditionalOps<R>
92            + numr::ops::ReduceOps<R>,
93    {
94        let control = self
95            .controls
96            .next_event()
97            .filter(|event| event.time_s == end_s);
98        let actuator = self.actuators.event_at(end_s)?;
99        Ok(Self::common(control, actuator))
100    }
101
102    /// The earlier of the `control` and `actuator` events, with the flags of both at that time.
103    fn common(
104        control: Option<ControlEvent>,
105        actuator: Option<GateActuatorEvent>,
106    ) -> Option<ControlledGateEvent> {
107        let time_s = match (control, actuator) {
108            (Some(control), Some(actuator)) => control.time_s.min(actuator.time_s),
109            (Some(control), None) => control.time_s,
110            (None, Some(actuator)) => actuator.time_s,
111            (None, None) => return None,
112        };
113        let control = control.filter(|event| event.time_s == time_s);
114        let actuator = actuator.filter(|event| event.time_s == time_s);
115        Some(ControlledGateEvent {
116            time_s,
117            decision_index: control.and_then(|event| event.decision_index),
118            arrival_lower_boundary: actuator.is_some_and(|event| event.arrival_lower_boundary),
119            arrival_upper_boundary: actuator.is_some_and(|event| event.arrival_upper_boundary),
120            terminal: control.is_some_and(|event| event.terminal)
121                || actuator.is_some_and(|event| event.terminal),
122        })
123    }
124
125    /// Trial both children atomically, applying each endpoint command after old-target response.
126    pub fn trial_to(
127        &self,
128        end_s: f64,
129        decision_samples: &[ControlSample],
130    ) -> Result<ControlledGateTrial<R>, StructureError>
131    where
132        R::Client: numr::ops::BinaryOps<R>
133            + numr::ops::CompareOps<R>
134            + numr::ops::ConditionalOps<R>
135            + numr::ops::ReduceOps<R>
136            + numr::ops::ScalarOps<R>
137            + numr::ops::TwoSumOps<R>
138            + numr::ops::TypeConversionOps<R>
139            + numr::ops::UnaryOps<R>,
140    {
141        self.complete(self.prepare_to(end_s)?, decision_samples)
142    }
143}