rlevo-environments 0.2.0

RL benchmark environments and landscapes for rlevo (internal crate — use `rlevo` for the full API)
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
//! [`Reacher`] environment — `Environment` implementation and world builder.
//!
//! This module wires the Rapier3D physics backend to the `rlevo-core`
//! `Environment` trait. The public entry-points are:
//!
//! - [`Reacher::with_config`] — construct from an explicit [`ReacherConfig`].
//! - [`ConstructableEnv::new`] — construct with default config (render flag is
//!   a no-op; there is no render path in this environment).
//! - [`METADATA_KEY_REWARD_DISTANCE`] / [`METADATA_KEY_REWARD_CONTROL`] — keys
//!   for per-step reward components stored in `SnapshotMetadata`.

use std::marker::PhantomData;

use rand::rngs::StdRng;
use rand::{RngExt, SeedableRng};
use rapier3d::math::Vector;
use rapier3d::prelude::*;
use rlevo_core::environment::{ConstructableEnv, Environment, EnvironmentError, EpisodeStatus, SnapshotMetadata};
use rlevo_core::reward::ScalarReward;

use crate::locomotion::backend::{LocomotionBackend, Rapier3DBackend, Rapier3DWorld};
use crate::locomotion::common::{LocomotionSnapshot, ctrl_cost, wrap_to_pi};

use super::action::ReacherAction;
use super::config::ReacherConfig;
use super::observation::ReacherObservation;
use super::state::ReacherState;

/// Reward-component key: `−‖finger − target‖` (≤ 0).
pub const METADATA_KEY_REWARD_DISTANCE: &str = "reward_distance";
/// Reward-component key: `−0.1 · ‖action‖²` (≤ 0).
pub const METADATA_KEY_REWARD_CONTROL: &str = "reward_control";

/// Reacher — a 2-link planar arm whose fingertip must reach a randomly
/// placed target. Generic in the physics backend; v1 only implements
/// `B = Rapier3DBackend`.
#[derive(Debug)]
pub struct Reacher<B: LocomotionBackend = Rapier3DBackend> {
    world: B::World,
    state: ReacherState,
    config: ReacherConfig,
    rng: StdRng,
    steps: usize,
    _marker: PhantomData<B>,
}

/// Default backend alias.
pub type ReacherRapier = Reacher<Rapier3DBackend>;

impl Reacher<Rapier3DBackend> {
    /// Create with an explicit configuration.
    #[must_use]
    pub fn with_config(config: ReacherConfig) -> Self {
        let mut rng = StdRng::seed_from_u64(config.seed);
        let (world, state) = Self::build_world(&config, &mut rng);
        Self {
            world,
            state,
            config,
            rng,
            steps: 0,
            _marker: PhantomData,
        }
    }

    fn build_world(config: &ReacherConfig, rng: &mut StdRng) -> (Rapier3DWorld, ReacherState) {
        // Zero gravity — the arm is planar and must not droop in z.
        let mut world =
            Rapier3DWorld::new(Vector::new(0.0, 0.0, 0.0), config.dt, config.frame_skip);

        let n = config.reset_noise_scale;
        let theta1_init: f32 = rng.random_range(-n..=n);
        let theta2_init: f32 = rng.random_range(-n..=n);
        let theta1_dot_init: f32 = rng.random_range(-n..=n);
        let theta2_dot_init: f32 = rng.random_range(-n..=n);

        // Rejection-sample target position in a disk of radius `target_disk_radius`.
        let r = config.target_disk_radius;
        let (tx, ty) = loop {
            let x: f32 = rng.random_range(-r..=r);
            let y: f32 = rng.random_range(-r..=r);
            if x * x + y * y <= r * r {
                break (x, y);
            }
        };

        let half1 = config.link1_length * 0.5;
        let half2 = config.link2_length * 0.5;

        // Density from mass / capsule-volume so each body has a valid inertia
        // tensor (using `additional_mass` instead would leave angular inertia zero).
        let capsule_volume = |half_len: f32, radius: f32| {
            std::f32::consts::PI * radius.powi(2) * (2.0 * half_len + (4.0 / 3.0) * radius)
        };
        let link1_density =
            config.link_mass / capsule_volume(half1, config.link_radius).max(f32::EPSILON);
        let link2_density =
            config.link_mass / capsule_volume(half2, config.link_radius).max(f32::EPSILON);

        // Root anchor — fixed body at the world origin serves as the shoulder pivot.
        let root =
            world.add_body(RigidBodyBuilder::fixed().translation(Vector::new(0.0, 0.0, 0.0)));

        // link1 position: body origin sits at the capsule midpoint, i.e.
        // shoulder + link1_rotation · (half1, 0, 0) in world frame.
        let c1 = theta1_init.cos();
        let s1 = theta1_init.sin();
        let link1_pos = Vector::new(half1 * c1, half1 * s1, 0.0);
        let link1_builder = RigidBodyBuilder::dynamic()
            .translation(link1_pos)
            .rotation(Vector::new(0.0, 0.0, theta1_init))
            .angvel(Vector::new(0.0, 0.0, theta1_dot_init))
            .enabled_translations(true, true, false)
            .enabled_rotations(false, false, true);
        let link1 = world.add_body(link1_builder);
        world.add_collider(
            ColliderBuilder::capsule_x(half1, config.link_radius).density(link1_density),
            link1,
        );

        // Shoulder revolute joint (impulse), axis = world-z.
        let z_axis: Vector = Vector::new(0.0, 0.0, 1.0);
        let shoulder_joint = RevoluteJointBuilder::new(z_axis)
            .local_anchor1(Vector::new(0.0, 0.0, 0.0))
            .local_anchor2(Vector::new(-half1, 0.0, 0.0))
            .build();
        let shoulder = world.add_impulse_joint(root, link1, shoulder_joint);

        // link2: absolute orientation is θ1 + θ2 (θ2 is the relative elbow
        // angle). Elbow world position = link1_body + link1_rot·(half1, 0, 0).
        let theta2_abs = theta1_init + theta2_init;
        let c12 = theta2_abs.cos();
        let s12 = theta2_abs.sin();
        let elbow_world = Vector::new(2.0 * half1 * c1, 2.0 * half1 * s1, 0.0);
        let link2_pos = Vector::new(
            elbow_world.x + half2 * c12,
            elbow_world.y + half2 * s12,
            0.0,
        );
        let link2_builder = RigidBodyBuilder::dynamic()
            .translation(link2_pos)
            .rotation(Vector::new(0.0, 0.0, theta2_abs))
            .angvel(Vector::new(0.0, 0.0, theta1_dot_init + theta2_dot_init))
            .enabled_translations(true, true, false)
            .enabled_rotations(false, false, true);
        let link2 = world.add_body(link2_builder);
        world.add_collider(
            ColliderBuilder::capsule_x(half2, config.link_radius).density(link2_density),
            link2,
        );

        // Elbow revolute joint (impulse), axis = world-z.
        let elbow_joint = RevoluteJointBuilder::new(z_axis)
            .local_anchor1(Vector::new(half1, 0.0, 0.0))
            .local_anchor2(Vector::new(-half2, 0.0, 0.0))
            .build();
        let elbow = world.add_impulse_joint(link1, link2, elbow_joint);

        // Target body — fixed, small ball at the sampled position.
        let target =
            world.add_body(RigidBodyBuilder::fixed().translation(Vector::new(tx, ty, 0.0)));
        world.add_collider(ColliderBuilder::ball(0.01), target);

        let state = ReacherState {
            link1,
            link2,
            target,
            shoulder,
            elbow,
            target_xy: [tx, ty],
            last_obs: ReacherObservation::default(),
        };
        (world, state)
    }

    fn extract_observation(&self) -> ReacherObservation {
        let p1 = Rapier3DBackend::get_pose(&self.world, self.state.link1);
        let p2 = Rapier3DBackend::get_pose(&self.world, self.state.link2);
        let v1 = Rapier3DBackend::get_vel(&self.world, self.state.link1);
        let v2 = Rapier3DBackend::get_vel(&self.world, self.state.link2);

        // Pure rotation about world-z ⇒ quaternion = (cos(θ/2), 0, 0, sin(θ/2))
        // in [w, x, y, z] order. θ = 2·atan2(qz, qw).
        let [w1, _, _, z1] = p1.orientation;
        let [w2, _, _, z2] = p2.orientation;
        let theta1 = wrap_to_pi(2.0 * z1.atan2(w1));
        let theta2_world = wrap_to_pi(2.0 * z2.atan2(w2));
        let theta2 = wrap_to_pi(theta2_world - theta1); // relative elbow angle

        // Fingertip in world frame: link2 body origin is the capsule midpoint;
        // the tip sits at body-local (+half2, 0, 0).
        let half2 = self.config.link2_length * 0.5;
        let fx = p2.position[0] + half2 * theta2_world.cos();
        let fy = p2.position[1] + half2 * theta2_world.sin();

        let [tx, ty] = self.state.target_xy;
        ReacherObservation([
            theta1.cos(),
            theta2.cos(),
            theta1.sin(),
            theta2.sin(),
            tx,
            ty,
            v1.angular[2],
            v2.angular[2] - v1.angular[2],
            fx - tx,
            fy - ty,
        ])
    }

    fn apply_action(&mut self, action: &ReacherAction) {
        let (lo, hi) = self.config.action_clip;
        let clipped = [action.0[0].clamp(lo, hi), action.0[1].clamp(lo, hi)];
        let torques = self.config.gear.apply(&clipped);
        // Shoulder torque τ[0] on link1 (root is fixed → no reaction needed).
        // Elbow torque ±τ[1] between link1 and link2 (Newton's third law).
        if let Some(body) = self.world.bodies_mut().get_mut(self.state.link1) {
            body.add_torque(Vector::new(0.0, 0.0, torques[0] - torques[1]), true);
        }
        if let Some(body) = self.world.bodies_mut().get_mut(self.state.link2) {
            body.add_torque(Vector::new(0.0, 0.0, torques[1]), true);
        }
    }
}

impl ConstructableEnv for Reacher<Rapier3DBackend> {
    fn new(_render: bool) -> Self {
        Self::with_config(ReacherConfig::default())
    }
}

impl Environment<1, 1, 1> for Reacher<Rapier3DBackend> {
    type StateType = ReacherState;
    type ObservationType = ReacherObservation;
    type ActionType = ReacherAction;
    type RewardType = ScalarReward;
    type SnapshotType = LocomotionSnapshot<ReacherObservation>;

    /// Reset the environment to a freshly sampled initial state.
    ///
    /// Re-seeds the internal RNG from `config.seed` so that every call to
    /// `reset` on the same config produces an identical initial state, making
    /// rollouts reproducible. The returned snapshot has reward `0.0` and
    /// `EpisodeStatus::Running`; both reward-component metadata keys are set
    /// to `0.0` to satisfy the invariant that Σ components = total reward.
    ///
    /// # Errors
    ///
    /// This implementation never returns `Err`; the signature is required by
    /// the `Environment` trait.
    fn reset(&mut self) -> Result<Self::SnapshotType, EnvironmentError> {
        self.rng = StdRng::seed_from_u64(self.config.seed);
        let (world, mut state) = Self::build_world(&self.config, &mut self.rng);
        self.world = world;
        state.last_obs = ReacherObservation::default();
        self.state = state;
        self.steps = 0;

        let obs = self.extract_observation();
        self.state.last_obs = obs;
        // Zero-reward initial snapshot; emit both reward components at 0.0 so
        // the Σ-components = reward invariant holds at step 0 (no action taken).
        let meta = SnapshotMetadata::new()
            .with(METADATA_KEY_REWARD_DISTANCE, 0.0)
            .with(METADATA_KEY_REWARD_CONTROL, 0.0)
            .with_position("target", [obs.target_xy()[0], obs.target_xy()[1], 0.0]);
        Ok(LocomotionSnapshot::running(obs, ScalarReward(0.0), meta))
    }

    /// Advance the simulation by one environment timestep (`dt * frame_skip`).
    ///
    /// The action is clipped to `config.action_clip` before the gear ratio is
    /// applied. The snapshot metadata carries the two reward components under
    /// [`METADATA_KEY_REWARD_DISTANCE`] and [`METADATA_KEY_REWARD_CONTROL`],
    /// and the world-space positions of the fingertip and target under the
    /// keys `"fingertip"` and `"target"`.
    ///
    /// # Errors
    ///
    /// Returns [`EnvironmentError::InvalidAction`] if either action element is
    /// non-finite (`NaN` or ±∞).
    fn step(&mut self, action: ReacherAction) -> Result<Self::SnapshotType, EnvironmentError> {
        if !action.0.iter().all(|v| v.is_finite()) {
            return Err(EnvironmentError::InvalidAction(format!(
                "Reacher action must be finite, got {:?}",
                action.0
            )));
        }

        self.apply_action(&action);
        Rapier3DBackend::step(&mut self.world);
        self.steps += 1;

        let obs = self.extract_observation();
        self.state.last_obs = obs;

        let [dx, dy] = obs.finger_minus_target_xy();
        let reward_distance: f32 = -(dx * dx + dy * dy).sqrt();
        // Clip to the bound-enforced action before computing ctrl cost so that
        // unclipped inputs don't inflate the cost.
        let (lo, hi) = self.config.action_clip;
        let clipped = [action.0[0].clamp(lo, hi), action.0[1].clamp(lo, hi)];
        let reward_control: f32 = -ctrl_cost(self.config.ctrl_cost_weight, &clipped);
        let total = reward_distance + reward_control;

        let status = if self.steps >= self.config.max_steps {
            EpisodeStatus::Truncated
        } else {
            EpisodeStatus::Running
        };

        // Fingertip world-xy = (finger − target) + target.
        let [tx, ty] = self.state.target_xy;
        let fx = dx + tx;
        let fy = dy + ty;

        let meta = SnapshotMetadata::new()
            .with(METADATA_KEY_REWARD_DISTANCE, reward_distance)
            .with(METADATA_KEY_REWARD_CONTROL, reward_control)
            .with_position("fingertip", [fx, fy, 0.0])
            .with_position("target", [tx, ty, 0.0]);
        Ok(LocomotionSnapshot::new(
            obs,
            ScalarReward(total),
            status,
            meta,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rlevo_core::base::Action;
    use rlevo_core::base::Observation;
    use rlevo_core::environment::Snapshot;

    fn cfg(seed: u64) -> ReacherConfig {
        ReacherConfig {
            seed,
            ..Default::default()
        }
    }

    #[test]
    fn action_shape_and_validity() {
        assert_eq!(ReacherAction::shape(), [2]);
        assert!(ReacherAction::new(0.0, 0.0).is_valid());
        assert!(ReacherAction::new(1.0, -1.0).is_valid());
        assert!(!ReacherAction::new(1.5, 0.0).is_valid());
        assert!(!ReacherAction::new(f32::NAN, 0.0).is_valid());
    }

    #[test]
    fn observation_shape() {
        assert_eq!(ReacherObservation::shape(), [10]);
    }

    #[test]
    fn reset_returns_running() {
        let mut env = ReacherRapier::with_config(cfg(7));
        let snap = env.reset().unwrap();
        assert!(!snap.is_done());
        assert!(snap.observation().is_finite());
    }

    #[test]
    fn reward_decomposition_sums_to_total() {
        let mut env = ReacherRapier::with_config(cfg(1));
        env.reset().unwrap();
        for i in 0..20 {
            let a = ReacherAction::new(0.3 * (i as f32).sin(), -0.2);
            let snap = env.step(a).unwrap();
            let meta = snap.metadata().unwrap();
            let sum: f32 = meta.components.values().sum();
            assert!(
                (sum - snap.reward().0).abs() < 1e-5,
                "Σ components ({sum}) must equal reward ({}) at step {i}",
                snap.reward().0
            );
        }
    }

    #[test]
    fn ctrl_cost_scales_quadratically() {
        let a = [0.3f32, -0.5];
        let a2 = [0.6f32, -1.0];
        let c1 = ctrl_cost(0.1, &a);
        let c2 = ctrl_cost(0.1, &a2);
        assert!((c2 - 4.0 * c1).abs() < 1e-5);
    }

    #[test]
    fn determinism_across_reset() {
        let rollout = |actions: &[[f32; 2]]| {
            let mut env = ReacherRapier::with_config(cfg(123));
            env.reset().unwrap();
            let mut last = ReacherObservation::default();
            for a in actions {
                if let Ok(snap) = env.step(ReacherAction(*a)) {
                    last = *snap.observation();
                }
            }
            last
        };
        let actions = [[0.1, -0.2], [0.5, 0.3], [-0.4, 0.2], [0.0, 0.0]];
        assert_eq!(rollout(&actions), rollout(&actions));
    }

    #[test]
    fn init_noise_bounded() {
        for seed in 0..50 {
            let env = ReacherRapier::with_config(cfg(seed));
            let obs = env.state.last_obs;
            assert!(obs.is_finite(), "seed {seed} produced non-finite obs");
            let theta1 = obs.theta1_sin().atan2(obs.theta1_cos());
            let theta2 = obs.theta2_sin().atan2(obs.theta2_cos());
            // Reset noise is ±0.1; allow small float slack.
            assert!(
                theta1.abs() <= 0.1 + 1e-5,
                "seed {seed}: |θ1|={} > 0.1",
                theta1.abs()
            );
            assert!(
                theta2.abs() <= 0.1 + 1e-5,
                "seed {seed}: |θ2|={} > 0.1",
                theta2.abs()
            );
        }
    }

    #[test]
    fn truncates_at_max_steps() {
        let mut env = ReacherRapier::with_config(ReacherConfig {
            max_steps: 5,
            ..Default::default()
        });
        env.reset().unwrap();
        let mut status = EpisodeStatus::Running;
        for _ in 0..5 {
            let snap = env.step(ReacherAction::new(0.0, 0.0)).unwrap();
            status = snap.status();
        }
        assert_eq!(status, EpisodeStatus::Truncated);
    }

    #[test]
    fn invalid_action_is_error() {
        let mut env = ReacherRapier::with_config(ReacherConfig::default());
        env.reset().unwrap();
        let bad = ReacherAction::new(f32::NAN, 0.0);
        assert!(env.step(bad).is_err());
    }

    #[test]
    fn obs_is_finite_after_rollout() {
        let mut env = ReacherRapier::with_config(cfg(42));
        env.reset().unwrap();
        for i in 0..50 {
            let a = ReacherAction::new(0.5 * (i as f32 * 0.3).sin(), 0.5 * (i as f32 * 0.4).cos());
            let snap = env.step(a).unwrap();
            assert!(snap.observation().is_finite(), "non-finite obs at step {i}");
            if snap.is_done() {
                break;
            }
        }
    }

    #[test]
    fn obs_layout_matches_spec() {
        let mut env = ReacherRapier::with_config(cfg(3));
        env.reset().unwrap();
        let snap = env.step(ReacherAction::new(0.2, -0.1)).unwrap();
        let obs = snap.observation().0;
        // cos² + sin² ≈ 1 for each joint
        assert!((obs[0].powi(2) + obs[2].powi(2) - 1.0).abs() < 1e-4);
        assert!((obs[1].powi(2) + obs[3].powi(2) - 1.0).abs() < 1e-4);
        // target_x/y == state cache
        assert!((obs[4] - env.state.target_xy[0]).abs() < 1e-6);
        assert!((obs[5] - env.state.target_xy[1]).abs() < 1e-6);
    }

    #[test]
    fn target_within_disk_on_reset() {
        for seed in 0..200 {
            let env = ReacherRapier::with_config(cfg(seed));
            let [tx, ty] = env.state.target_xy;
            let r2 = tx * tx + ty * ty;
            assert!(
                r2 <= env.config.target_disk_radius.powi(2) + 1e-6,
                "seed {seed}: target ({tx}, {ty}) outside disk radius {}",
                env.config.target_disk_radius
            );
        }
    }

    #[test]
    fn finger_minus_target_matches_positions() {
        let mut env = ReacherRapier::with_config(cfg(5));
        env.reset().unwrap();
        let snap = env.step(ReacherAction::new(0.1, -0.1)).unwrap();
        let obs = snap.observation();

        // Re-derive fingertip from link2's pose and the half-length offset.
        let p2 = Rapier3DBackend::get_pose(&env.world, env.state.link2);
        let theta2_world = wrap_to_pi(2.0 * p2.orientation[3].atan2(p2.orientation[0]));
        let half2 = env.config.link2_length * 0.5;
        let fx = p2.position[0] + half2 * theta2_world.cos();
        let fy = p2.position[1] + half2 * theta2_world.sin();
        let [tx, ty] = env.state.target_xy;

        let [dx, dy] = obs.finger_minus_target_xy();
        assert!(
            (dx - (fx - tx)).abs() < 1e-5,
            "dx mismatch: obs {dx} vs computed {}",
            fx - tx
        );
        assert!(
            (dy - (fy - ty)).abs() < 1e-5,
            "dy mismatch: obs {dy} vs computed {}",
            fy - ty
        );
    }

    #[test]
    fn reward_distance_is_nonpositive() {
        let mut env = ReacherRapier::with_config(cfg(11));
        env.reset().unwrap();
        for i in 0..50 {
            let a = ReacherAction::new(
                0.4 * (i as f32 * 0.17).sin(),
                -0.3 * (i as f32 * 0.23).cos(),
            );
            let snap = env.step(a).unwrap();
            let d = snap.metadata().unwrap().components[METADATA_KEY_REWARD_DISTANCE];
            assert!(d <= 0.0, "reward_distance must be ≤ 0, got {d} at step {i}");
        }
    }

    #[test]
    fn reward_control_is_nonpositive() {
        let mut env = ReacherRapier::with_config(cfg(13));
        env.reset().unwrap();
        for i in 0..50 {
            let a =
                ReacherAction::new(0.6 * (i as f32 * 0.31).cos(), 0.9 * (i as f32 * 0.11).sin());
            let snap = env.step(a).unwrap();
            let c = snap.metadata().unwrap().components[METADATA_KEY_REWARD_CONTROL];
            assert!(c <= 0.0, "reward_control must be ≤ 0, got {c} at step {i}");
        }
    }
}