gridworld-rl 0.1.2

A deep reinforcement learning 2D gridworld environment with single- and multi-agent support.
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
use relayrl_types::prelude::tensor::burn::backend::Backend;
use relayrl_types::prelude::tensor::burn::{Float, Tensor, TensorData};
use relayrl_env_trait::environment_traits::{EnvironmentTrait, TrainingPerformanceReturnFn, EnvironmentError};
use std::any::Any;
use std::cell::RefCell;
use std::sync::atomic::{AtomicBool, Ordering};

#[derive(Clone, Debug)]
pub struct RewardConfig {
    pub collision_reward: f32,
    pub end_state_reward: f32,
    pub step_reward: f32,
}

impl Default for RewardConfig {
    fn default() -> Self {
        Self {
            collision_reward: -1.0,
            end_state_reward: 10.0,
            step_reward: -0.01,
        }
    }
}

enum Move {
    Up,
    Down,
    Left,
    Right,
}

impl Move {
    fn from_action(action: u8) -> Option<Self> {
        match action {
            0 => Some(Move::Up),
            1 => Some(Move::Down),
            2 => Some(Move::Left),
            3 => Some(Move::Right),
            _ => None,
        }
    }

    fn delta(&self) -> (isize, isize) {
        match self {
            Move::Up    => (-1,  0),
            Move::Down  => ( 1,  0),
            Move::Left  => ( 0, -1),
            Move::Right => ( 0,  1),
        }
    }
}

enum MoveResult {
    Valid,
    AtEnd,
    WallCollision,
    AgentCollision,
    OutOfBounds,
}

#[derive(Clone, Debug)]
pub struct Actor {
    pub id: usize,
    pub initial_position: (isize, isize),
    pub current_position: (isize, isize),
    pub done: bool,
    pub last_reward: f32,
    pub cumulative_reward: f32,
}

impl Actor {
    fn reset(&mut self) {
        self.current_position = self.initial_position;
        self.done = false;
        self.last_reward = 0.0;
        self.cumulative_reward = 0.0;
    }
}

pub struct GridWorldEnv<B: Backend>
where
    B::Device: Clone,
{
    pub training: bool,
    pub length: usize,
    pub width: usize,
    pub wall_positions: Vec<(isize, isize)>,
    pub end_position: (isize, isize),
    pub reward_config: RewardConfig,
    pub max_steps: usize,
    pub device: B::Device,
    actors: RefCell<Vec<Actor>>,
    step_count: RefCell<usize>,
    last_observations: RefCell<Vec<Vec<f32>>>,
    episode_returns: RefCell<Vec<f32>>,
    running: AtomicBool,
}

impl<B: Backend> Default for GridWorldEnv<B>
where
    B::Device: Clone + Default,
{
    fn default() -> Self {
        let wall_positions: Vec<(isize, isize)> = vec![
            (2, 1), (2, 2), (2, 3), (2, 4),
            (3, 4), (4, 4), (5, 4), (6, 4), (7, 4),
            (2, 6), (2, 7), (2, 8),
        ];
        let num_actors = 1;
        let cells = 10 * 10;
        Self {
            training: true,
            length: 10,
            width: 10,
            wall_positions,
            end_position: (9, 9),
            reward_config: RewardConfig::default(),
            max_steps: 200,
            device: B::Device::default(),
            actors: RefCell::new(vec![Actor {
                id: 0,
                initial_position: (0, 0),
                current_position: (0, 0),
                done: false,
                last_reward: 0.0,
                cumulative_reward: 0.0,
            }]),
            step_count: RefCell::new(0),
            last_observations: RefCell::new(vec![vec![0.0f32; cells]; num_actors]),
            episode_returns: RefCell::new(vec![0.0f32; num_actors]),
            running: AtomicBool::new(false),
        }
    }
}

impl<B: Backend> GridWorldEnv<B>
where
    B::Device: Clone,
{
    pub fn new(
        training: bool,
        length: usize,
        width: usize,
        wall_positions: Vec<(isize, isize)>,
        end_position: (isize, isize),
        initial_actor_positions: Vec<(isize, isize)>,
        reward_config: Option<RewardConfig>,
        max_steps: Option<usize>,
        device: B::Device,
    ) -> Result<Self, EnvironmentError> {
        let (er, ec) = end_position;
        if er < 0 || er >= length as isize || ec < 0 || ec >= width as isize {
            return Err(EnvironmentError::EnvironmentError(format!(
                "End position ({},{}) is out of bounds for {}x{} grid",
                er, ec, length, width
            )));
        }

        for &(wr, wc) in &wall_positions {
            if wr < 0 || wr >= length as isize || wc < 0 || wc >= width as isize {
                return Err(EnvironmentError::EnvironmentError(format!(
                    "Wall at ({},{}) is out of bounds for {}x{} grid",
                    wr, wc, length, width
                )));
            }
            if (wr, wc) == end_position {
                return Err(EnvironmentError::EnvironmentError(format!(
                    "Wall at ({},{}) conflicts with end position ({},{})",
                    wr, wc, er, ec
                )));
            }
        }

        for (i, &(ar, ac)) in initial_actor_positions.iter().enumerate() {
            if ar < 0 || ar >= length as isize || ac < 0 || ac >= width as isize {
                return Err(EnvironmentError::EnvironmentError(format!(
                    "Actor {} at ({},{}) is out of bounds for {}x{} grid",
                    i, ar, ac, length, width
                )));
            }
            if (ar, ac) == end_position {
                return Err(EnvironmentError::EnvironmentError(format!(
                    "Actor {} at ({},{}) conflicts with end position ({},{})",
                    i, ar, ac, er, ec
                )));
            }
            for &(wr, wc) in &wall_positions {
                if (ar, ac) == (wr, wc) {
                    return Err(EnvironmentError::EnvironmentError(format!(
                        "Actor {} at ({},{}) conflicts with wall at ({},{})",
                        i, ar, ac, wr, wc
                    )));
                }
            }
            for (j, &(br, bc)) in initial_actor_positions.iter().enumerate() {
                if i != j && (ar, ac) == (br, bc) {
                    return Err(EnvironmentError::EnvironmentError(format!(
                        "Actors {} and {} both start at ({},{})",
                        i, j, ar, ac
                    )));
                }
            }
        }

        let num_actors = initial_actor_positions.len();
        let cells = length * width;
        let actors: Vec<Actor> = initial_actor_positions
            .into_iter()
            .enumerate()
            .map(|(id, pos)| Actor {
                id,
                initial_position: pos,
                current_position: pos,
                done: false,
                last_reward: 0.0,
                cumulative_reward: 0.0,
            })
            .collect();

        Ok(Self {
            training,
            length,
            width,
            wall_positions,
            end_position,
            reward_config: reward_config.unwrap_or_default(),
            max_steps: max_steps.unwrap_or(200),
            device,
            actors: RefCell::new(actors),
            step_count: RefCell::new(0),
            last_observations: RefCell::new(vec![vec![0.0f32; cells]; num_actors]),
            episode_returns: RefCell::new(vec![0.0f32; num_actors]),
            running: AtomicBool::new(false),
        })
    }

    pub fn reset(&self) {
        let num_actors = {
            let mut actors = self.actors.borrow_mut();
            for actor in actors.iter_mut() {
                actor.reset();
            }
            actors.len()
        };
        *self.step_count.borrow_mut() = 0;
        let cells = self.length * self.width;
        *self.last_observations.borrow_mut() = vec![vec![0.0f32; cells]; num_actors];
        *self.episode_returns.borrow_mut() = vec![0.0f32; num_actors];
        self.update_observations();
    }

    pub fn step(&self, actor_idx: usize, action: u8) -> Result<(f32, bool), EnvironmentError> {
        let mv = Move::from_action(action).ok_or_else(|| {
            EnvironmentError::EnvironmentError(format!("Invalid action: {}", action))
        })?;

        let (is_done, last_r) = {
            let actors = self.actors.borrow();
            if actor_idx >= actors.len() {
                return Err(EnvironmentError::EnvironmentError(format!(
                    "Actor index {} out of range ({})",
                    actor_idx,
                    actors.len()
                )));
            }
            (actors[actor_idx].done, actors[actor_idx].last_reward)
        };

        if is_done {
            *self.step_count.borrow_mut() += 1;
            let episode_done = self.all_done() || self.is_max_steps_reached();
            return Ok((last_r, episode_done));
        }

        let (cr, cc) = self.actors.borrow()[actor_idx].current_position;
        let (dr, dc) = mv.delta();
        let new_pos = (cr + dr, cc + dc);
        let move_result = self.validate_move_result(actor_idx, new_pos);

        let reward = {
            let mut actors = self.actors.borrow_mut();
            let r = match &move_result {
                MoveResult::Valid => {
                    actors[actor_idx].current_position = new_pos;
                    self.reward_config.step_reward
                }
                MoveResult::AtEnd => {
                    actors[actor_idx].current_position = new_pos;
                    actors[actor_idx].done = true;
                    self.reward_config.end_state_reward
                }
                MoveResult::WallCollision
                | MoveResult::AgentCollision
                | MoveResult::OutOfBounds => self.reward_config.collision_reward,
            };
            actors[actor_idx].last_reward = r;
            actors[actor_idx].cumulative_reward += r;
            r
        };

        *self.step_count.borrow_mut() += 1;
        self.update_observations();

        let episode_done = self.all_done() || self.is_max_steps_reached();
        Ok((reward, episode_done))
    }

    pub fn get_observation(&self, actor_idx: usize) -> Vec<f32> {
        self.last_observations.borrow()[actor_idx].clone()
    }

    pub fn get_last_reward(&self, actor_idx: usize) -> f32 {
        self.actors.borrow()[actor_idx].last_reward
    }

    pub fn get_episode_return(&self, actor_idx: usize) -> f32 {
        self.actors.borrow()[actor_idx].cumulative_reward
    }

    pub fn all_done(&self) -> bool {
        self.actors.borrow().iter().all(|a| a.done)
    }

    pub fn is_max_steps_reached(&self) -> bool {
        *self.step_count.borrow() >= self.max_steps
    }

    pub fn actor_count(&self) -> usize {
        self.actors.borrow().len()
    }

    /// Returns the current grid position of every actor, in actor-index order.
    #[cfg(feature = "render")]
    pub fn get_actor_positions(&self) -> Vec<(isize, isize)> {
        self.actors.borrow().iter().map(|a| a.current_position).collect()
    }

    fn is_in_bounds(&self, pos: (isize, isize)) -> bool {
        pos.0 >= 0
            && pos.0 < self.length as isize
            && pos.1 >= 0
            && pos.1 < self.width as isize
    }

    fn is_wall(&self, pos: (isize, isize)) -> bool {
        self.wall_positions.contains(&pos)
    }

    fn is_actor_at(&self, pos: (isize, isize), exclude_idx: usize) -> bool {
        self.actors
            .borrow()
            .iter()
            .enumerate()
            .any(|(i, a)| i != exclude_idx && !a.done && a.current_position == pos)
    }

    fn validate_move_result(&self, actor_idx: usize, new_pos: (isize, isize)) -> MoveResult {
        if !self.is_in_bounds(new_pos) {
            MoveResult::OutOfBounds
        } else if self.is_wall(new_pos) {
            MoveResult::WallCollision
        } else if new_pos == self.end_position {
            MoveResult::AtEnd
        } else if self.is_actor_at(new_pos, actor_idx) {
            MoveResult::AgentCollision
        } else {
            MoveResult::Valid
        }
    }

    fn build_actor_observation(&self, actor_idx: usize) -> Vec<f32> {
        let cells = self.length * self.width;
        let mut obs = vec![0.0f32; cells];

        for &(wr, wc) in &self.wall_positions {
            obs[wr as usize * self.width + wc as usize] = 1.0;
        }

        let (er, ec) = self.end_position;
        obs[er as usize * self.width + ec as usize] = 2.0;

        let actors = self.actors.borrow();
        for (i, actor) in actors.iter().enumerate() {
            if i != actor_idx && !actor.done {
                let (r, c) = actor.current_position;
                obs[r as usize * self.width + c as usize] = 3.0;
            }
        }
        let (ar, ac) = actors[actor_idx].current_position;
        obs[ar as usize * self.width + ac as usize] = 4.0;

        obs
    }

    fn update_observations(&self) {
        let num_actors = self.actors.borrow().len();
        let mut obs = self.last_observations.borrow_mut();
        for i in 0..num_actors {
            obs[i] = self.build_actor_observation(i);
        }
    }
}

impl<B: Backend> EnvironmentTrait for GridWorldEnv<B>
where
    B::Device: Clone,
{
    fn run_environment(&self) -> Result<(), EnvironmentError> {
        self.running.store(true, Ordering::SeqCst);
        self.reset();
        Ok(())
    }

    fn build_observation(&self) -> Result<Box<dyn Any>, EnvironmentError> {
        self.update_observations();
        let obs = self.last_observations.borrow();
        let n = obs.len();
        let cells = self.length * self.width;
        let flat: Vec<f32> = obs.iter().flat_map(|row| row.iter().copied()).collect();
        let tensor = Tensor::<B, 2, Float>::from_data(
            TensorData::new(flat, [n, cells]),
            &self.device,
        );
        Ok(Box::new(tensor))
    }
}

impl<B: Backend> TrainingPerformanceReturnFn for GridWorldEnv<B> {
	fn calculate_performance_return(&self) -> Result<Box<dyn Any>, EnvironmentError> {
		{
			let actors = self.actors.borrow();
			let mut returns = self.episode_returns.borrow_mut();
			for (i, actor) in actors.iter().enumerate() {
			returns[i] = actor.cumulative_reward;
			}
		}	
		let flat = self.episode_returns.borrow().clone();
		let n = flat.len();
		let tensor = Tensor::<B, 1, Float>::from_data(
			TensorData::new(flat, [n]),
			&self.device,
		);
		Ok(Box::new(tensor))
}

}