ogym 0.1.0

Composable, type-safe reinforcement learning environments for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::f64::consts::{PI, TAU};

use nalgebra::SVector;
use rand::{
    distr::{Distribution, Uniform},
    rngs::StdRng,
    Rng, SeedableRng,
};

use crate::{
    env::environment::{Environment, Error, Experience},
    spaces::{Boxed, EnvSpace, Mixed, MixedItem, Space},
};

use super::{config::DynamicsMode, AcrobotConfig};

const ACTION_SIZE: usize = 1;
const RAW_STATE_SIZE: usize = 4;
const STATE_SIZE: usize = 6;

type RawState = SVector<f64, RAW_STATE_SIZE>;
type State = SVector<f64, STATE_SIZE>;
type StateSpace = Boxed<STATE_SIZE>;

type Action = MixedItem<ACTION_SIZE>;
type ActionSpace = Mixed<ACTION_SIZE>;

#[derive(Debug, Clone)]
pub struct Acrobot {
    pub config: AcrobotConfig,
    t: u32,
    raw_state: Option<RawState>,
    pub space: EnvSpace<StateSpace, ActionSpace>,
    rng: StdRng,
}

impl Acrobot {
    pub fn new(config: AcrobotConfig) -> Result<Self, Error> {
        let ha = SVector::from_element(1.0);
        let hs = SVector::from_vec(vec![1., 1., 1., 1., 4. * PI, 9. * PI]);
        let space = EnvSpace {
            state: Boxed::new(-hs, hs)?,
            action: match config.continuous {
                true => Mixed::continuous(-ha, ha)?,
                false => Mixed::discrete(3)?,
            },
        };
        Ok(Self {
            config,
            space,
            t: 0,
            raw_state: None,
            rng: StdRng::from_os_rng(),
        })
    }

    fn raw(&self) -> Result<RawState, Error> {
        match self.raw_state {
            Some(s) => Ok(s),
            None => Err(Error::NotInitialized),
        }
    }

    fn tau(&mut self, action: &Action) -> Result<f64, Error> {
        let mut tau = match (&self.space.action, action) {
            (Mixed::Discrete(space), MixedItem::Discrete(act)) => {
                space.contains(act).map_err(|_| Error::InvalidAction)?;
                (*act as i32 - 1) as f64 // Cast to signed int to prevent underflow
            }
            (Mixed::Continuous(space), MixedItem::Continuous(act)) => {
                // Clip out-of-range torque instead of rejecting the step,
                // matching Gymnasium's np.clip convention for continuous actions.
                let (low, high) = space.bounds();
                act[0].clamp(low[0], high[0])
            }
            _ => return Err(Error::InvalidAction),
        };

        // FIX: Ensure range is not empty (0.0..0.0) to prevent panic in rand 0.9+
        if self.config.torque_noise_max > 0.0 {
            tau += self
                .rng
                .random_range(-self.config.torque_noise_max..self.config.torque_noise_max);
        }
        Ok(tau)
    }

    fn ds_dt(&self, state: RawState, tau: f64) -> SVector<f64, RAW_STATE_SIZE> {
        let g = self.config.g;
        let m1 = self.config.link_mass_1;
        let m2 = self.config.link_mass_2;
        let l1 = self.config.link_length_1;
        let c1 = self.config.link_com_pos_1;
        let c2 = self.config.link_com_pos_2;
        let moi = self.config.link_moi;
        let th1 = state[0];
        let th2 = state[1];
        let dt1 = state[2];
        let dt2 = state[3];

        let (sin_th2, cos_th2) = th2.sin_cos();

        let d1 = m1 * c1 * c1 + m2 * (l1 * l1 + c2 * c2 + 2.0 * l1 * c2 * cos_th2) + moi * 2.0;
        let d2 = m2 * (c2 * c2 + l1 * c2 * cos_th2) + moi;

        let phi2 = m2 * c2 * g * (th1 + th2 - PI / 2.0).cos();
        let phi1 = -m2 * l1 * c2 * dt2 * dt2 * sin_th2 - 2.0 * m2 * l1 * c2 * dt2 * dt1 * sin_th2
            + (m1 * c1 + m2 * l1) * g * (th1 - PI / 2.0).cos()
            + phi2;

        let alpha2 = match self.config.dynamics_mode {
            DynamicsMode::Nips => {
                // Consistent with the paper
                (tau + d2 / d1 * phi1 - phi2) / (m2 * c2 * c2 + moi - d2 * d2 / d1)
            }
            DynamicsMode::Book => {
                // Consistent with java implementation and book
                (tau + d2 / d1 * phi1 - m2 * l1 * c2 * dt1 * dt1 * sin_th2 - phi2)
                    / (m2 * c2 * c2 + moi - d2 * d2 / d1)
            }
        };
        let alpha1 = -(d2 * alpha2 + phi1) / d1;

        SVector::<f64, RAW_STATE_SIZE>::new(dt1, dt2, alpha1, alpha2)
    }

    fn constraint(&self, mut state: RawState) -> RawState {
        state[0] = (state[0] + PI).rem_euclid(TAU) - PI;
        state[1] = (state[1] + PI).rem_euclid(TAU) - PI;
        state[2] = state[2].clamp(-self.config.max_vel_1, self.config.max_vel_1);
        state[3] = state[3].clamp(-self.config.max_vel_2, self.config.max_vel_2);

        state
    }

    fn rk4(&self, state: RawState, tau: f64) -> RawState {
        let dt = self.config.dt;
        let dt2 = dt / 2.0;

        let k1 = self.ds_dt(state, tau);
        let k2 = self.ds_dt(state + dt2 * k1, tau);
        let k3 = self.ds_dt(state + dt2 * k2, tau);
        let k4 = self.ds_dt(state + dt * k3, tau);

        self.constraint(state + dt / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4))
    }
}

impl Environment for Acrobot {
    type Action = Action;
    type State = State;
    type Info = ();

    fn reset(&mut self, seed: Option<u64>) -> Result<(Self::State, Self::Info), Error> {
        self.t = 0;
        self.rng = match seed {
            Some(state) => StdRng::seed_from_u64(state),
            None => StdRng::from_rng(&mut rand::rng()),
        };
        let dist = Uniform::new(-0.1, 0.1)?;
        let raw_state: SVector<f64, RAW_STATE_SIZE> =
            SVector::from_fn(|_, _| dist.sample(&mut self.rng));

        self.raw_state = Some(raw_state);
        Ok((self.state()?, ()))
    }

    fn step(
        &mut self,
        action: Self::Action,
    ) -> Result<Experience<Self::State, Self::Info, Self::Action>, Error> {
        if self.is_done()? {
            return Err(Error::EpisodeDone);
        }

        let curr_state = self.state()?;

        let tau = self.tau(&action)?;
        self.raw_state = Some(self.rk4(self.raw()?, tau));
        let next_state = self.state()?;

        self.t += 1;

        Ok(Experience::new(
            curr_state,
            if self.is_terminal()? { 0.0 } else { -1.0 },
            action,
            next_state,
            (),
            self.to_terminal()?,
            self.t,
        ))
    }

    fn state(&self) -> Result<Self::State, Error> {
        let s = self.raw()?;
        let (sin_s0, cos_s0) = s[0].sin_cos();
        let (sin_s1, cos_s1) = s[1].sin_cos();

        Ok(SVector::from_vec(vec![
            cos_s0, sin_s0, cos_s1, sin_s1, s[2], s[3],
        ]))
    }

    #[inline]
    fn is_terminal(&self) -> Result<bool, Error> {
        let s = self.raw()?;
        Ok(s[0].cos() + (s[0] + s[1]).cos() < -1.)
    }

    #[inline]
    fn is_truncated(&self) -> bool {
        self.t >= self.config.max_t
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::env::environment::Environment;
    use crate::spaces::{Mixed, MixedItem};
    use nalgebra::SVector;
    use std::f64::consts::PI;

    #[test]
    fn test_initialization_discrete() {
        let config = AcrobotConfig::default().with_discrete_action();
        let acrobot = Acrobot::new(config).unwrap();

        if let Mixed::Discrete(d) = &acrobot.space.action {
            assert_eq!(d.size(), 3);
        } else {
            panic!("Expected discrete action space");
        }
    }

    #[test]
    fn test_initialization_continuous() {
        let config = AcrobotConfig::default().with_continuous_action();
        let acrobot = Acrobot::new(config).unwrap();

        if let Mixed::Continuous(c) = &acrobot.space.action {
            assert_eq!(c.low[0], -1.0);
            assert_eq!(c.high[0], 1.0);
        } else {
            panic!("Expected continuous action space");
        }
    }

    #[test]
    fn test_reset() {
        let config = AcrobotConfig::default();
        let mut acrobot = Acrobot::new(config).unwrap();

        let (state, _) = acrobot.reset(Some(42)).unwrap();

        assert_eq!(state.len(), 6);

        assert!((state[0] - 1.0).abs() < 0.1); // cos(theta1)
        assert!(state[1].abs() < 0.2); // sin(theta1)
        assert!((state[2] - 1.0).abs() < 0.1); // cos(theta2)
        assert!(state[3].abs() < 0.2); // sin(theta2)
        assert!(state[4].abs() < 0.2); // vel1
        assert!(state[5].abs() < 0.2); // vel2

        // Check internal raw state
        let raw = acrobot.raw().unwrap();
        assert!(raw[0].abs() <= 0.1);
        assert!(raw[1].abs() <= 0.1);
        assert!(raw[2].abs() <= 0.1);
        assert!(raw[3].abs() <= 0.1);
    }

    #[test]
    fn test_step_discrete_actions() {
        let config = AcrobotConfig::default().with_discrete_action();
        let mut acrobot = Acrobot::new(config).unwrap();
        acrobot.reset(None).unwrap();

        // Action 0: Torque -1
        let action_0 = MixedItem::Discrete(0);
        let exp_0 = acrobot.step(action_0).unwrap();
        assert_eq!(exp_0.reward, -1.0);
        assert!(!exp_0.terminal.is_done());

        // Action 2: Torque +1
        let action_2 = MixedItem::Discrete(2);
        let _exp_2 = acrobot.step(action_2).unwrap();
    }

    #[test]
    fn test_step_continuous_actions() {
        let config = AcrobotConfig::default().with_continuous_action();
        let mut acrobot = Acrobot::new(config).unwrap();
        acrobot.reset(None).unwrap();

        // Torque 0.5
        let action = MixedItem::Continuous(SVector::from_element(0.5));
        let exp = acrobot.step(action).unwrap();

        assert_eq!(exp.reward, -1.0);
    }

    #[test]
    fn test_step_invalid_action_mismatch() {
        let config_disc = AcrobotConfig::default().with_discrete_action();
        let mut env_disc = Acrobot::new(config_disc).unwrap();
        env_disc.reset(None).unwrap();

        // Sending continuous action to discrete env
        let bad_action = MixedItem::Continuous(SVector::from_element(0.5));
        let res = env_disc.step(bad_action);
        assert!(res.is_err());
    }

    #[test]
    fn test_step_invalid_action_bounds() {
        let config = AcrobotConfig::default().with_discrete_action();
        let mut env = Acrobot::new(config).unwrap();
        env.reset(None).unwrap();

        // Action index 3 is out of bounds (0, 1, 2 allowed)
        let bad_action = MixedItem::Discrete(3);
        let res = env.step(bad_action);
        assert!(res.is_err());
    }

    #[test]
    fn test_terminal_condition() {
        let config = AcrobotConfig::default();
        let mut acrobot = Acrobot::new(config).unwrap();
        acrobot.reset(None).unwrap();

        acrobot.raw_state = Some(SVector::from_vec(vec![PI, 0.0, 0.0, 0.0]));

        assert!(acrobot.is_terminal().unwrap());

        let action = MixedItem::Discrete(1);
        let res = acrobot.step(action);
        assert!(matches!(res, Err(Error::EpisodeDone)));
    }

    #[test]
    fn test_truncation() {
        let max_t = 5;
        let config = AcrobotConfig::default().with_max_steps(max_t);
        let mut acrobot = Acrobot::new(config).unwrap();
        acrobot.reset(None).unwrap();

        for i in 0..max_t {
            assert!(!acrobot.is_truncated());
            let _ = acrobot.step(MixedItem::Discrete(1)).unwrap();
            assert_eq!(acrobot.t, i + 1);
        }

        assert!(acrobot.is_truncated());

        let res = acrobot.step(MixedItem::Discrete(1));
        assert!(matches!(res, Err(Error::EpisodeDone)));
    }

    #[test]
    fn test_constraint_wrapping() {
        let config = AcrobotConfig::default();
        let mut acrobot = Acrobot::new(config).unwrap();
        acrobot.reset(None).unwrap();

        let large_angle = PI + 0.5;
        acrobot.raw_state = Some(SVector::from_vec(vec![large_angle, large_angle, 0.0, 0.0]));

        let _ = acrobot.step(MixedItem::Discrete(1)).unwrap();

        let raw_after = acrobot.raw().unwrap();
        assert!(raw_after[0] >= -PI && raw_after[0] <= PI);
        assert!(raw_after[1] >= -PI && raw_after[1] <= PI);
    }

    #[test]
    fn test_constraint_velocity_clamping() {
        let config = AcrobotConfig::default().with_max_velocities(1.0, 1.0); // Low limits
        let mut acrobot = Acrobot::new(config).unwrap();
        acrobot.reset(None).unwrap();

        acrobot.raw_state = Some(SVector::from_vec(vec![0.0, 0.0, 10.0, 10.0]));

        let _ = acrobot.step(MixedItem::Discrete(1)).unwrap();

        let raw_after = acrobot.raw().unwrap();
        assert!(raw_after[2] <= 1.5); // allowing some buffer for physics update
        assert!(raw_after[3] <= 1.5);
    }

    #[test]
    fn test_torque_noise() {
        let config = AcrobotConfig::default().with_torque_noise(5.0); // High noise
        let mut acrobot = Acrobot::new(config).unwrap();
        acrobot.reset(Some(123)).unwrap();

        let _ = acrobot.step(MixedItem::Discrete(1)).unwrap();

        let state = acrobot.state().unwrap();
        assert!(state.iter().all(|x| x.is_finite()));
    }
}