thrust-rl 0.4.0

High-performance reinforcement learning in Rust with the Burn tensor backend
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
//! WebAssembly bindings for browser-based visualization
//!
//! This module provides JavaScript bindings for our Rust environments
//! so they can be rendered in the browser.

#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "wasm")]
use crate::env::{
    Environment, cartpole::CartPole, pong::Pong, simple_bandit::SimpleBandit, snake::SnakeEnv,
};

/// WASM bindings for CartPole environment
#[cfg(feature = "wasm")]
#[wasm_bindgen]
pub struct WasmCartPole {
    env: CartPole,
    episode: u32,
    episode_steps: u32,
    best_score: u32,
    policy: Option<crate::policy::inference::InferenceModel>,
}

#[cfg(feature = "wasm")]
#[wasm_bindgen]
impl WasmCartPole {
    /// Construct a fresh CartPole environment with no loaded policy.
    ///
    /// Exposed to JS as `new WasmCartPole()`. Calls
    /// `console_error_panic_hook::set_once()` so any subsequent Rust
    /// panic surfaces as a readable stack trace in the browser console
    /// instead of an opaque WASM trap.
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        console_error_panic_hook::set_once();
        Self { env: CartPole::new(), episode: 0, episode_steps: 0, best_score: 0, policy: None }
    }

    /// Reset the environment and start a new episode
    #[wasm_bindgen]
    pub fn reset(&mut self) -> Vec<f32> {
        // Update best score before resetting
        if self.episode_steps > self.best_score {
            self.best_score = self.episode_steps;
        }

        self.env.reset();
        self.episode += 1;
        self.episode_steps = 0;
        self.env.get_state().to_vec()
    }

    /// Take a step in the environment
    /// Returns: [obs0, obs1, obs2, obs3, reward, terminated, truncated]
    #[wasm_bindgen]
    pub fn step(&mut self, action: i32) -> Vec<f32> {
        let result = self.env.step(action as i64);
        self.episode_steps += 1;

        let mut output = result.observation.clone();
        output.push(result.reward);
        output.push(if result.terminated { 1.0 } else { 0.0 });
        output.push(if result.truncated { 1.0 } else { 0.0 });

        // Update best score if episode ended
        if result.terminated || result.truncated {
            if self.episode_steps > self.best_score {
                self.best_score = self.episode_steps;
            }
        }

        output
    }

    /// Get current episode number
    #[wasm_bindgen]
    pub fn get_episode(&self) -> u32 {
        self.episode
    }

    /// Get total steps in current episode
    #[wasm_bindgen]
    pub fn get_steps(&self) -> u32 {
        self.episode_steps
    }

    /// Get best score
    #[wasm_bindgen]
    pub fn get_best_score(&self) -> u32 {
        self.best_score
    }

    /// Get the current state for rendering
    /// Returns: [x, x_dot, theta, theta_dot]
    #[wasm_bindgen]
    pub fn get_state(&self) -> Vec<f32> {
        self.env.get_state().to_vec()
    }

    /// Load policy from JSON string
    /// The JSON should contain the InferenceModel structure
    #[wasm_bindgen]
    pub fn load_policy_json(&mut self, json: &str) -> Result<(), JsValue> {
        let policy: crate::policy::inference::InferenceModel = serde_json::from_str(json)
            .map_err(|e| JsValue::from_str(&format!("Failed to parse policy JSON: {}", e)))?;

        #[cfg(feature = "wasm")]
        {
            use web_sys::console;
            console::log_1(
                &format!(
                    "Model loaded: obs_dim={}, action_dim={}, hidden_dim={}, activation={:?}",
                    policy.obs_dim, policy.action_dim, policy.hidden_dim, policy.activation
                )
                .into(),
            );
        }

        self.policy = Some(policy);
        Ok(())
    }

    /// Get policy action using the loaded model
    /// Returns action index (0 or 1) or -1 if no policy loaded
    #[wasm_bindgen]
    pub fn get_policy_action(&self) -> i32 {
        if let Some(ref policy) = self.policy {
            let state = self.env.get_state();

            #[cfg(feature = "wasm")]
            {
                use web_sys::console;
                console::log_1(&format!("State: {:?}", state).into());
            }

            let action = policy.get_action(&state) as i32;

            #[cfg(feature = "wasm")]
            {
                use web_sys::console;
                console::log_1(&format!("Action: {}", action).into());
            }

            action
        } else {
            -1 // No policy loaded
        }
    }

    /// Check if policy is loaded
    #[wasm_bindgen]
    pub fn has_policy(&self) -> bool {
        self.policy.is_some()
    }
}

/// WASM bindings for Snake environment
#[cfg(feature = "wasm")]
#[wasm_bindgen]
pub struct WasmSnake {
    env: SnakeEnv,
    episode: u32,
    policy: Option<crate::inference::snake::SnakeCNNInference>,
}

#[cfg(feature = "wasm")]
#[wasm_bindgen]
impl WasmSnake {
    /// Construct a fresh multi-agent Snake environment.
    ///
    /// Exposed to JS as `new WasmSnake(width, height, num_agents)`. Sets
    /// up the panic hook for browser-readable stack traces; no policy is
    /// loaded yet (call `load_policy_json` afterward to enable
    /// inference). `width` / `height` are grid cells; `num_agents` is
    /// the number of snakes that share the grid.
    #[wasm_bindgen(constructor)]
    pub fn new(width: i32, height: i32, num_agents: usize) -> Self {
        console_error_panic_hook::set_once();
        Self { env: SnakeEnv::new_multi(width, height, num_agents), episode: 0, policy: None }
    }

    /// Reset the environment
    #[wasm_bindgen]
    pub fn reset(&mut self) {
        self.env.reset();
        self.episode += 1;
    }

    /// Step the environment with actions for all agents
    /// actions: array of actions where each is 0=up, 1=down, 2=left, 3=right
    #[wasm_bindgen]
    pub fn step(&mut self, actions: &[i32]) {
        let actions_i64: Vec<i64> = actions.iter().map(|&a| a as i64).collect();
        let _ = self.env.step_multi(&actions_i64);
    }

    /// Get grid observation for a specific agent
    /// Returns flattened grid [channels * height * width] where:
    /// - Channel 0: Own snake body
    /// - Channel 1: Own snake head
    /// - Channel 2: Other snakes
    /// - Channel 3: Food
    /// - Channel 4: Walls
    #[wasm_bindgen]
    pub fn get_observation(&self, agent_id: usize) -> Vec<f32> {
        self.env.get_grid_observation(agent_id)
    }

    /// Get number of agents
    #[wasm_bindgen]
    pub fn num_agents(&self) -> usize {
        self.env.num_agents
    }

    /// Get active agents (alive = true, dead = false)
    #[wasm_bindgen]
    pub fn active_agents(&self) -> Vec<u8> {
        self.env.snakes.iter().map(|s| if s.is_alive() { 1 } else { 0 }).collect()
    }

    /// Get grid width in cells.
    #[wasm_bindgen]
    pub fn get_width(&self) -> i32 {
        self.env.width
    }

    /// Get grid height in cells.
    #[wasm_bindgen]
    pub fn get_height(&self) -> i32 {
        self.env.height
    }

    /// Get all snake positions for rendering
    /// Returns flattened array: [num_snakes, len0, x0, y0, x1, y1, ..., len1,
    /// x0, y0, ...]
    #[wasm_bindgen]
    pub fn get_snake_positions(&self) -> Vec<i32> {
        let mut positions = Vec::new();

        // First, add the number of snakes
        positions.push(self.env.snakes.len() as i32);

        // Then add each snake's positions
        for snake in &self.env.snakes {
            positions.push(snake.body.len() as i32);
            for pos in &snake.body {
                positions.push(pos.x);
                positions.push(pos.y);
            }
        }

        positions
    }

    /// Get food position
    /// Returns array: [x, y]
    #[wasm_bindgen]
    pub fn get_food_positions(&self) -> Vec<i32> {
        vec![self.env.food.x, self.env.food.y]
    }

    /// Get current episode
    #[wasm_bindgen]
    pub fn get_episode(&self) -> u32 {
        self.episode
    }

    /// Load policy from JSON string
    /// The JSON should contain the SnakeCNNInference model structure
    #[wasm_bindgen]
    pub fn load_policy_json(&mut self, json: &str) -> Result<(), JsValue> {
        let policy: crate::inference::snake::SnakeCNNInference = serde_json::from_str(json)
            .map_err(|e| JsValue::from_str(&format!("Failed to parse policy JSON: {}", e)))?;

        self.policy = Some(policy);
        Ok(())
    }

    /// Get policy action for a specific agent using the loaded model
    /// Returns action index (0=up, 1=down, 2=left, 3=right) or -1 if no policy
    /// loaded
    #[wasm_bindgen]
    pub fn get_policy_action(&self, agent_id: usize) -> i32 {
        if let Some(ref policy) = self.policy {
            let observation = self.env.get_grid_observation(agent_id);
            policy.get_action(&observation) as i32
        } else {
            -1 // No policy loaded
        }
    }

    /// Check if policy is loaded
    #[wasm_bindgen]
    pub fn has_policy(&self) -> bool {
        self.policy.is_some()
    }
}

/// WASM bindings for Pong environment
#[cfg(feature = "wasm")]
#[wasm_bindgen]
pub struct WasmPong {
    env: Pong,
    episode: u32,
    episode_steps: u32,
    policy: Option<crate::policy::inference::InferenceModel>,
}

#[cfg(feature = "wasm")]
#[wasm_bindgen]
impl WasmPong {
    /// Construct a new WASM Pong wrapper with a freshly reset environment.
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        console_error_panic_hook::set_once();
        let mut env = Pong::new();
        env.reset();
        Self { env, episode: 0, episode_steps: 0, policy: None }
    }

    /// Reset environment, returns initial state [ball_x, ball_y, left_y,
    /// right_y, left_score, right_score]
    #[wasm_bindgen]
    pub fn reset(&mut self) -> Vec<f32> {
        self.env.reset();
        self.episode += 1;
        self.episode_steps = 0;
        self.env.get_state().to_vec()
    }

    /// Step with action (0=up, 1=stay, 2=down).
    /// Returns [ball_x, ball_y, left_y, right_y, left_score, right_score,
    /// reward, terminated, truncated]
    #[wasm_bindgen]
    pub fn step(&mut self, action: i32) -> Vec<f32> {
        let result = self.env.step(action as i64);
        self.episode_steps += 1;

        let mut output = self.env.get_state().to_vec();
        output.push(result.reward);
        output.push(if result.terminated { 1.0 } else { 0.0 });
        output.push(if result.truncated { 1.0 } else { 0.0 });
        output
    }

    /// Current rendering state [ball_x, ball_y, left_y, right_y, left_score,
    /// right_score]
    #[wasm_bindgen]
    pub fn get_state(&self) -> Vec<f32> {
        self.env.get_state().to_vec()
    }

    /// Episode counter (incremented by `reset`).
    #[wasm_bindgen]
    pub fn get_episode(&self) -> u32 {
        self.episode
    }

    /// Step count within the current episode.
    #[wasm_bindgen]
    pub fn get_steps(&self) -> u32 {
        self.episode_steps
    }

    /// Load policy from JSON string (InferenceModel)
    #[wasm_bindgen]
    pub fn load_policy_json(&mut self, json: &str) -> Result<(), JsValue> {
        let policy: crate::policy::inference::InferenceModel = serde_json::from_str(json)
            .map_err(|e| JsValue::from_str(&format!("Failed to parse policy JSON: {}", e)))?;
        self.policy = Some(policy);
        Ok(())
    }

    /// Get policy action (0/1/2) or -1 if no policy loaded
    #[wasm_bindgen]
    pub fn get_policy_action(&self) -> i32 {
        if let Some(ref policy) = self.policy {
            let obs = self.env.get_observation();
            policy.get_action(&obs) as i32
        } else {
            -1
        }
    }

    /// Whether a policy has been loaded via `load_policy_json`.
    #[wasm_bindgen]
    pub fn has_policy(&self) -> bool {
        self.policy.is_some()
    }
}

/// WASM bindings for SimpleBandit environment
#[cfg(feature = "wasm")]
#[wasm_bindgen]
pub struct WasmSimpleBandit {
    env: SimpleBandit,
    episode: u32,
    episode_steps: u32,
    total_reward: f64,
    successes: u32,
}

#[cfg(feature = "wasm")]
#[wasm_bindgen]
impl WasmSimpleBandit {
    /// Construct a fresh SimpleBandit environment.
    ///
    /// Exposed to JS as `new WasmSimpleBandit()`. Sets up the panic
    /// hook so Rust panics surface in the browser console. The bandit
    /// is stateless across episodes (only the cumulative reward and
    /// success counters update); see [`Self::reset`] to bump the
    /// episode counter.
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        console_error_panic_hook::set_once();
        Self {
            env: SimpleBandit::new(),
            episode: 0,
            episode_steps: 0,
            total_reward: 0.0,
            successes: 0,
        }
    }

    /// Reset the environment and start a new episode
    #[wasm_bindgen]
    pub fn reset(&mut self) -> Vec<f32> {
        self.env.reset();
        self.episode += 1;
        self.episode_steps = 0;
        self.successes = 0;
        self.env.get_observation()
    }

    /// Take a step in the environment
    /// Returns: [state, reward, terminated]
    #[wasm_bindgen]
    pub fn step(&mut self, action: i32) -> Vec<f32> {
        let result = self.env.step(action as i64);
        self.episode_steps += 1;
        self.total_reward += result.reward as f64;

        if result.reward > 0.0 {
            self.successes += 1;
        }

        let mut output = result.observation.clone();
        output.push(result.reward);
        output.push(if result.terminated { 1.0 } else { 0.0 });
        output
    }

    /// Get current episode number
    #[wasm_bindgen]
    pub fn get_episode(&self) -> u32 {
        self.episode
    }

    /// Get total steps in current episode
    #[wasm_bindgen]
    pub fn get_steps(&self) -> u32 {
        self.episode_steps
    }

    /// Get current success rate (percentage)
    #[wasm_bindgen]
    pub fn get_success_rate(&self) -> f32 {
        if self.episode_steps == 0 {
            0.0
        } else {
            (self.successes as f32 / self.episode_steps as f32) * 100.0
        }
    }

    /// Get total reward
    #[wasm_bindgen]
    pub fn get_total_reward(&self) -> f32 {
        self.total_reward as f32
    }

    /// Get the current state
    #[wasm_bindgen]
    pub fn get_state(&self) -> Vec<f32> {
        self.env.get_observation()
    }
}

// Add panic hook for better error messages in browser console
#[cfg(feature = "wasm")]
use console_error_panic_hook;