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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
use mujoco_rs_sys::no_render;
use mujoco_rust::{model::ObjType, Model, Simulation, State};
use nalgebra as na;

use crate::env::environment::Error;

pub struct MjEnv {
    frame_skip: u32,
    state: State,
    simulation: Simulation,
    init_qpos: Vec<f64>,
    init_qvel: Vec<f64>,
}

impl MjEnv {
    pub fn new(xml: impl AsRef<str>, frame_skip: u32) -> Result<Self, Error> {
        let model = Model::from_xml_str(xml).map_err(Error::MjInitError)?;
        let simulation = Simulation::new(model);
        let state = State::new(&simulation.model);

        unsafe {
            no_render::mj_resetData(simulation.model.ptr(), state.ptr());
            no_render::mj_forward(simulation.model.ptr(), state.ptr());
        }

        let init_qpos = {
            let ptr = state.ptr();
            let nq = simulation.model.nq();
            unsafe { std::slice::from_raw_parts((*ptr).qpos, nq).to_vec() }
        };

        let init_qvel = {
            let ptr = state.ptr();
            let nv = simulation.model.nv();
            unsafe { std::slice::from_raw_parts((*ptr).qvel, nv).to_vec() }
        };

        Ok(Self {
            state,
            simulation,
            frame_skip,
            init_qpos,
            init_qvel,
        })
    }

    pub fn model(&self) -> &Model {
        &self.simulation.model
    }

    pub fn state(&self) -> &State {
        &self.state
    }

    pub fn state_mut(&mut self) -> &mut State {
        &mut self.state
    }

    pub fn data(&self) -> &State {
        &self.state
    }

    pub fn simulation(&self) -> &Simulation {
        &self.simulation
    }

    pub fn timestep(&self) -> f64 {
        let ptr = self.model().ptr();
        unsafe { (*ptr).opt.timestep }
    }

    pub fn dt(&self) -> f64 {
        self.timestep() * self.frame_skip as f64
    }

    pub fn time(&self) -> f64 {
        let ptr = self.state.ptr();
        unsafe { (*ptr).time }
    }

    pub fn extent(&self) -> f64 {
        let ptr = self.model().ptr();
        unsafe { (*ptr).stat.extent }
    }

    pub fn body_mass(&self) -> &[f64] {
        let ptr = self.model().ptr();
        let n = self.model().nbody();
        unsafe { std::slice::from_raw_parts((*ptr).body_mass, n) }
    }

    pub fn actuator_ctrlrange(&self) -> &[[f64; 2]] {
        let ptr = self.model().ptr();
        let nu = self.model().nu();
        unsafe { std::slice::from_raw_parts((*ptr).actuator_ctrlrange as *const [f64; 2], nu) }
    }

    pub fn na(&self) -> usize {
        self.model().na()
    }

    pub fn nq(&self) -> usize {
        self.model().nq()
    }

    pub fn nu(&self) -> usize {
        self.model().nu()
    }

    pub fn nv(&self) -> usize {
        self.model().nv()
    }

    pub fn nbody(&self) -> usize {
        self.model().nbody()
    }

    pub fn ngeom(&self) -> usize {
        self.model().ngeom()
    }

    pub fn nsite(&self) -> usize {
        unsafe { (*self.model().ptr()).nsite as usize }
    }

    pub fn ntendon(&self) -> usize {
        unsafe { (*self.model().ptr()).ntendon as usize }
    }

    pub fn init_qpos(&self) -> &[f64] {
        &self.init_qpos
    }

    pub fn init_qvel(&self) -> &[f64] {
        &self.init_qvel
    }

    pub fn qpos(&self) -> &[f64] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).qpos, self.nq()) }
    }

    pub fn qvel(&self) -> &[f64] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).qvel, self.nv()) }
    }

    pub fn ctrl(&self) -> &[f64] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).ctrl, self.nu()) }
    }

    pub fn act(&self) -> &[f64] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).act, self.na()) }
    }

    pub fn cfrc_ext(&self) -> &[[f64; 6]] {
        let ptr = self.state.ptr();
        let nbody = self.nbody();
        unsafe { std::slice::from_raw_parts((*ptr).cfrc_ext as *const [f64; 6], nbody) }
    }

    pub fn cvel(&self) -> &[[f64; 6]] {
        let ptr = self.state.ptr();
        let nbody = self.nbody();
        unsafe { std::slice::from_raw_parts((*ptr).cvel as *const [f64; 6], nbody) }
    }

    pub fn cinert(&self) -> &[[f64; 10]] {
        let ptr = self.state.ptr();
        let nbody = self.nbody();
        unsafe { std::slice::from_raw_parts((*ptr).cinert as *const [f64; 10], nbody) }
    }

    pub fn qfrc_actuator(&self) -> &[f64] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).qfrc_actuator, self.nv()) }
    }

    pub fn qfrc_constraint(&self) -> &[f64] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).qfrc_constraint, self.nv()) }
    }

    pub fn ten_velocity(&self) -> &[f64] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).ten_velocity, self.ntendon()) }
    }

    pub fn ten_length(&self) -> &[f64] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).ten_length, self.ntendon()) }
    }

    pub fn xipos(&self) -> &[[f64; 3]] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).xipos as *const [f64; 3], self.nbody()) }
    }

    pub fn xpos(&self) -> &[[f64; 3]] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).xpos as *const [f64; 3], self.nbody()) }
    }

    pub fn geom_xpos(&self) -> &[[f64; 3]] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).geom_xpos as *const [f64; 3], self.ngeom()) }
    }

    pub fn site_xpos(&self) -> &[[f64; 3]] {
        let ptr = self.state.ptr();
        unsafe { std::slice::from_raw_parts((*ptr).site_xpos as *const [f64; 3], self.nsite()) }
    }

    pub fn body(&self, name: &str) -> Option<[f64; 3]> {
        let id = self.model().name_to_id(ObjType::BODY, name)?;

        if (id as usize) >= self.nbody() {
            return None;
        }

        let ptr = self.state.ptr();

        unsafe {
            let pos_ptr = (*ptr).xpos.add((id as usize) * 3);
            Some([*pos_ptr, *pos_ptr.add(1), *pos_ptr.add(2)])
        }
    }

    /// Center-of-mass position (MuJoCo's `xipos`) of the body with the given
    /// name, resolved by name rather than a hard-coded body index. This is
    /// what Gymnasium's `get_body_com(name)` reads, as opposed to `body()`
    /// above (`xpos`, the body frame origin) which can differ from the COM
    /// whenever a body's geoms aren't centered on its frame origin.
    pub fn body_com(&self, name: &str) -> Option<[f64; 3]> {
        let id = self.model().name_to_id(ObjType::BODY, name)?;

        if (id as usize) >= self.nbody() {
            return None;
        }

        let ptr = self.state.ptr();

        unsafe {
            let pos_ptr = (*ptr).xipos.add((id as usize) * 3);
            Some([*pos_ptr, *pos_ptr.add(1), *pos_ptr.add(2)])
        }
    }

    pub fn body_com_vector(&self, name: &str) -> Option<na::Vector3<f64>> {
        self.body_com(name).map(na::Vector3::from)
    }

    pub fn qpos_mut(&mut self) -> &mut [f64] {
        let ptr = self.state.ptr();
        let nq = self.nq();
        unsafe { std::slice::from_raw_parts_mut((*ptr).qpos, nq) }
    }

    pub fn qvel_mut(&mut self) -> &mut [f64] {
        let ptr = self.state.ptr();
        let nv = self.nv();
        unsafe { std::slice::from_raw_parts_mut((*ptr).qvel, nv) }
    }

    pub fn ctrl_mut(&mut self) -> &mut [f64] {
        let ptr = self.state.ptr();
        let nu = self.nu();
        unsafe { std::slice::from_raw_parts_mut((*ptr).ctrl, nu) }
    }

    pub fn act_mut(&mut self) -> &mut [f64] {
        let ptr = self.state.ptr();
        let na = self.na();
        unsafe { std::slice::from_raw_parts_mut((*ptr).act, na) }
    }

    pub fn set_state(&mut self, qpos: &[f64], qvel: &[f64]) -> Result<(), Error> {
        if qpos.len() != self.nq() {
            return Err(Error::InvalidStateDimension {
                field: "qpos",
                expected: self.nq(),
                got: qpos.len(),
            });
        }

        if qvel.len() != self.nv() {
            return Err(Error::InvalidStateDimension {
                field: "qvel",
                expected: self.nv(),
                got: qvel.len(),
            });
        }

        self.qpos_mut().copy_from_slice(qpos);
        self.qvel_mut().copy_from_slice(qvel);

        unsafe {
            no_render::mj_forward(self.model().ptr(), self.data().ptr());
        }

        Ok(())
    }

    pub fn do_simulation(&mut self, ctrl: &[f64]) -> Result<(), Error> {
        if ctrl.len() != self.nu() {
            return Err(Error::InvalidActionDimension {
                expected: self.nu(),
                got: ctrl.len(),
            });
        }

        self.ctrl_mut().copy_from_slice(ctrl);

        let model_ptr = self.model().ptr();
        let data_ptr = self.state.ptr();

        unsafe {
            let frame_skip = self.frame_skip;
            for _ in 0..frame_skip {
                no_render::mj_step(model_ptr, data_ptr);
            }
            no_render::mj_rnePostConstraint(model_ptr, data_ptr);
        }

        Ok(())
    }

    pub fn reset(&mut self) {
        unsafe {
            no_render::mj_resetData(self.model().ptr(), self.state.ptr());
            no_render::mj_forward(self.model().ptr(), self.state.ptr());
        }
    }

    pub fn reset_to_initial(&mut self) -> Result<(), Error> {
        let qpos = self.init_qpos.clone();
        let qvel = self.init_qvel.clone();
        self.set_state(&qpos, &qvel)
    }

    pub fn state_vector(&self) -> Vec<f64> {
        let mut state = Vec::with_capacity(self.nq() + self.nv());
        state.extend_from_slice(self.qpos());
        state.extend_from_slice(self.qvel());
        state
    }

    pub fn qpos_vector(&self) -> na::DVector<f64> {
        na::DVector::from_column_slice(self.qpos())
    }

    pub fn qvel_vector(&self) -> na::DVector<f64> {
        na::DVector::from_column_slice(self.qvel())
    }

    pub fn ctrl_vector(&self) -> na::DVector<f64> {
        na::DVector::from_column_slice(self.ctrl())
    }

    pub fn act_vector(&self) -> na::DVector<f64> {
        na::DVector::from_column_slice(self.act())
    }

    pub fn state_vector_dyn(&self) -> na::DVector<f64> {
        let nq = self.nq();
        let nv = self.nv();
        let mut state = na::DVector::zeros(nq + nv);
        state.rows_mut(0, nq).copy_from_slice(self.qpos());
        state.rows_mut(nq, nv).copy_from_slice(self.qvel());
        state
    }

    pub fn body_vector(&self, name: &str) -> Option<na::Vector3<f64>> {
        self.body(name).map(na::Vector3::from)
    }

    pub fn qfrc_actuator_vector(&self) -> na::DVector<f64> {
        na::DVector::from_column_slice(self.qfrc_actuator())
    }

    pub fn qfrc_constraint_vector(&self) -> na::DVector<f64> {
        na::DVector::from_column_slice(self.qfrc_constraint())
    }

    pub fn ten_velocity_vector(&self) -> na::DVector<f64> {
        na::DVector::from_column_slice(self.ten_velocity())
    }

    pub fn ten_length_vector(&self) -> na::DVector<f64> {
        na::DVector::from_column_slice(self.ten_length())
    }

    fn slice_arrays_to_matrix(rows: usize, cols: usize, data: &[f64]) -> na::DMatrix<f64> {
        na::DMatrix::from_row_slice(rows, cols, data)
    }

    pub fn xipos_matrix(&self) -> na::DMatrix<f64> {
        let data = self.xipos();
        let flat_len = data.len() * 3;
        let flat_data =
            unsafe { std::slice::from_raw_parts(data.as_ptr() as *const f64, flat_len) };
        Self::slice_arrays_to_matrix(data.len(), 3, flat_data)
    }

    pub fn xpos_matrix(&self) -> na::DMatrix<f64> {
        let data = self.xpos();
        let flat_len = data.len() * 3;
        let flat_data =
            unsafe { std::slice::from_raw_parts(data.as_ptr() as *const f64, flat_len) };
        Self::slice_arrays_to_matrix(data.len(), 3, flat_data)
    }

    pub fn geom_xpos_matrix(&self) -> na::DMatrix<f64> {
        let data = self.geom_xpos();
        let flat_len = data.len() * 3;
        let flat_data =
            unsafe { std::slice::from_raw_parts(data.as_ptr() as *const f64, flat_len) };
        Self::slice_arrays_to_matrix(data.len(), 3, flat_data)
    }

    pub fn site_xpos_matrix(&self) -> na::DMatrix<f64> {
        let data = self.site_xpos();
        let flat_len = data.len() * 3;
        let flat_data =
            unsafe { std::slice::from_raw_parts(data.as_ptr() as *const f64, flat_len) };
        Self::slice_arrays_to_matrix(data.len(), 3, flat_data)
    }

    pub fn cvel_matrix(&self) -> na::DMatrix<f64> {
        let data = self.cvel();
        let flat_len = data.len() * 6;
        let flat_data =
            unsafe { std::slice::from_raw_parts(data.as_ptr() as *const f64, flat_len) };
        Self::slice_arrays_to_matrix(data.len(), 6, flat_data)
    }

    pub fn cfrc_ext_matrix(&self) -> na::DMatrix<f64> {
        let data = self.cfrc_ext();
        let flat_len = data.len() * 6;
        let flat_data =
            unsafe { std::slice::from_raw_parts(data.as_ptr() as *const f64, flat_len) };
        Self::slice_arrays_to_matrix(data.len(), 6, flat_data)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const TEST_MODEL_XML: &str = r#"
    <mujoco model="simple_test">
        <compiler angle="radian"/>
        <option timestep="0.01" gravity="0 0 -9.81"/>
        <worldbody>
            <light pos="0 0 3" dir="0 0 -1" />
            <geom type="plane" size="10 10 0.1" rgba=".9 .9 .9 1"/>
            <!-- Slider body starting at Z=1 -->
            <body name="slider_body" pos="0 0 1">
                <joint name="slide_x" type="slide" axis="1 0 0" />
                <geom type="box" size="0.1 0.1 0.1" mass="1.0" />
            </body>
        </worldbody>
        <actuator>
            <motor name="motor_x" joint="slide_x" gear="10.0" />
        </actuator>
    </mujoco>
    "#;

    const INVALID_XML: &str = "Not an XML string";

    #[test]
    fn test_initialization_success() {
        let frame_skip = 4;
        let env = MjEnv::new(TEST_MODEL_XML, frame_skip);

        assert!(
            env.is_ok(),
            "MjEnv should initialize successfully with valid XML content"
        );

        let env = env.unwrap();

        assert_eq!(env.nq(), 1, "Expected 1 generalized coordinate (qpos)");
        assert_eq!(env.nv(), 1, "Expected 1 generalized velocity (qvel)");
        assert_eq!(env.nu(), 1, "Expected 1 control input (nu)");

        assert_eq!(env.nbody(), 2, "Expected 2 bodies (world + slider)");

        let expected_dt = 0.01 * frame_skip as f64;
        assert!(
            (env.dt() - expected_dt).abs() < 1e-6,
            "DT calculation incorrect"
        );
    }

    #[test]
    fn test_initialization_failure() {
        let env = MjEnv::new(INVALID_XML, 1);
        assert!(
            env.is_err(),
            "MjEnv should fail when initialized with invalid XML"
        );

        match env {
            Err(Error::MjInitError(_)) => (),
            _ => panic!("Expected MjInitError"),
        }
    }

    #[test]
    fn test_body_lookup_and_position() {
        let env = MjEnv::new(TEST_MODEL_XML, 1).unwrap();

        let pos = env.body_vector("slider_body");
        assert!(pos.is_some(), "Should find 'slider_body'");

        let pos_vec = pos.unwrap();
        assert!((pos_vec.x - 0.0).abs() < 1e-6);
        assert!((pos_vec.y - 0.0).abs() < 1e-6);
        assert!((pos_vec.z - 1.0).abs() < 1e-6);

        // Lookup non-existent body
        let missing = env.body_vector("non_existent_ghost");
        assert!(
            missing.is_none(),
            "Should return None for invalid body name"
        );
    }

    #[test]
    fn test_set_state_and_reset() {
        let mut env = MjEnv::new(TEST_MODEL_XML, 1).unwrap();

        let new_qpos = vec![0.5];
        let new_qvel = vec![0.1];

        let res = env.set_state(&new_qpos, &new_qvel);
        assert!(res.is_ok());

        assert!((env.qpos()[0] - 0.5).abs() < 1e-6, "qpos not updated");
        assert!((env.qvel()[0] - 0.1).abs() < 1e-6, "qvel not updated");

        let bad_qpos = vec![0.5, 0.5];
        let res_bad = env.set_state(&bad_qpos, &new_qvel);
        assert!(res_bad.is_err(), "Should fail with wrong qpos dimensions");

        let reset_res = env.reset_to_initial();
        assert!(reset_res.is_ok());

        assert!((env.qpos()[0] - 0.0).abs() < 1e-6, "qpos not reset to 0");
        assert!((env.qvel()[0] - 0.0).abs() < 1e-6, "qvel not reset to 0");
    }

    #[test]
    fn test_simulation_integration() {
        let frame_skip = 1;
        let mut env = MjEnv::new(TEST_MODEL_XML, frame_skip).unwrap();

        let start_time = env.time();

        let action = vec![1.0];

        let step_res = env.do_simulation(&action);
        assert!(step_res.is_ok());

        let end_time = env.time();
        assert!(
            end_time > start_time,
            "Time should advance after simulation step"
        );
        assert!(
            (end_time - start_time - 0.01).abs() < 1e-6,
            "Time advanced by incorrect amount"
        );

        let qvel_after = env.qvel()[0];
        assert!(
            qvel_after > 0.0,
            "Applying positive motor force should result in positive velocity"
        );

        // Check that control was stored
        assert_eq!(env.ctrl()[0], 1.0);
    }

    #[test]
    fn test_matrix_data_access() {
        let env = MjEnv::new(TEST_MODEL_XML, 1).unwrap();

        let xpos_mat = env.xpos_matrix();
        let (rows, cols) = xpos_mat.shape();

        assert_eq!(cols, 3, "xpos matrix should have 3 columns (x,y,z)");
        assert_eq!(rows, env.nbody(), "xpos matrix rows should match nbody");

        let geom_mat = env.geom_xpos_matrix();
        assert_eq!(geom_mat.shape().0, env.ngeom());
        assert_eq!(geom_mat.shape().1, 3);

        let cvel_mat = env.cvel_matrix();
        assert_eq!(cvel_mat.shape().0, env.nbody());
        assert_eq!(cvel_mat.shape().1, 6);
    }

    #[test]
    fn test_state_vector_concatenation() {
        let mut env = MjEnv::new(TEST_MODEL_XML, 1).unwrap();

        let qpos = vec![2.0];
        let qvel = vec![1.0];
        let _ = env.set_state(&qpos, &qvel);

        let vec_dyn = env.state_vector_dyn();

        assert_eq!(vec_dyn.len(), 2);
        assert!((vec_dyn[0] - 2.0).abs() < 1e-6);
        assert!((vec_dyn[1] - 1.0).abs() < 1e-6);
    }
}