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
//! A collection of reinforcement learning benchmark domains.
#[cfg_attr(test, macro_use)]
extern crate ndarray;
extern crate rand;
extern crate spaces;

use crate::spaces::Space;
use std::iter;

macro_rules! impl_into {
    (Transition < S, $type:ty > => Transition < S,() >) => {
        impl<S> Into<Transition<S, ()>> for Transition<S, $type> {
            fn into(self) -> Transition<S, ()> { self.drop_action() }
        }
    };
}

macro_rules! make_index {
    ($tname:ident [$($name:ident => $idx:literal),+]) => {
        use std::ops::{Index, IndexMut};

        #[derive(Debug, Clone, Copy)]
        enum $tname {
            $($name = $idx),+
        }

        impl Index<$tname> for Vec<f64> {
            type Output = f64;

            fn index(&self, idx: StateIndex) -> &f64 { self.index(idx as usize) }
        }

        impl Index<$tname> for [f64] {
            type Output = f64;

            fn index(&self, idx: StateIndex) -> &f64 { self.index(idx as usize) }
        }

        impl IndexMut<$tname> for Vec<f64> {
            fn index_mut(&mut self, idx: StateIndex) -> &mut f64 { self.index_mut(idx as usize) }
        }

        impl IndexMut<$tname> for [f64] {
            fn index_mut(&mut self, idx: StateIndex) -> &mut f64 { self.index_mut(idx as usize) }
        }
    }
}

pub type Reward = f64;

/// Container class for data associated with a domain observation.
#[derive(Clone, Copy, Debug)]
pub enum Observation<S> {
    /// Fully observed state of the environment.
    Full(S),

    /// Partially observed state of the environment.
    Partial(S),

    /// Terminal state of the environment.
    Terminal(S),
}

impl<S> Observation<S> {
    /// Helper function returning a reference to the state values for the given
    /// observation.
    pub fn state(&self) -> &S {
        use self::Observation::*;

        match self {
            Full(ref state) | Partial(ref state) | Terminal(ref state) => state,
        }
    }

    pub fn map<O>(&self, f: impl Fn(&S) -> O) -> Observation<O> {
        use self::Observation::*;

        match self {
            Full(ref state) => Full(f(state)),
            Partial(ref state) => Partial(f(state)),
            Terminal(ref state) => Terminal(f(state)),
        }
    }

    pub fn map_into<O>(&self, f: impl Fn(&S) -> O) -> O {
        use self::Observation::*;

        match self {
            Full(ref state) | Partial(ref state) | Terminal(ref state) => f(state),
        }
    }

    pub fn borrowed(&self) -> Observation<&S> {
        use self::Observation::*;

        match self {
            Full(ref state) => Full(state),
            Partial(ref state) => Partial(state),
            Terminal(ref state) => Terminal(state),
        }
    }

    /// Returns true if the state was fully observed, otherwise false.
    pub fn is_full(&self) -> bool {
        match self {
            Observation::Full(_) => true,
            _ => false,
        }
    }

    /// Returns true if the state was only partially observed, otherwise false.
    pub fn is_partial(&self) -> bool {
        match self {
            Observation::Partial(_) => true,
            _ => false,
        }
    }

    /// Returns true if the observation is the terminal state, otherwise false.
    pub fn is_terminal(&self) -> bool {
        match self {
            Observation::Terminal(_) => true,
            _ => false,
        }
    }
}

/// Container class for data associated with a domain transition.
#[derive(Clone, Copy, Debug)]
pub struct Transition<S, A> {
    /// State transitioned _from_, `s`.
    pub from: Observation<S>,

    /// Action taken to initiate the transition (control tasks).
    pub action: A,

    /// Reward obtained from the transition.
    pub reward: Reward,

    /// State transitioned _to_, `s'`.
    pub to: Observation<S>,
}

impl<S, A> Transition<S, A> {
    /// Return references to the `from` and `to` states associated with this
    /// transition.
    pub fn states(&self) -> (&S, &S) { (self.from.state(), self.to.state()) }

    pub fn borrowed(&self) -> Transition<&S, &A> {
        Transition {
            from: self.from.borrowed(),
            action: &self.action,
            reward: self.reward,
            to: self.to.borrowed(),
        }
    }

    /// Apply a closure to the `from` and `to` states associated with this
    /// transition.
    pub fn map_states<O>(self, f: impl Fn(&S) -> O) -> Transition<O, A> {
        Transition {
            from: self.from.map(&f),
            action: self.action,
            reward: self.reward,
            to: self.to.map(f),
        }
    }

    /// Returns true if the transition ends in a terminal state.
    pub fn terminated(&self) -> bool { self.to.is_terminal() }

    /// Replace the action associated with this transition and return a new
    /// instance.
    pub fn replace_action<T>(self, action: T) -> Transition<S, T> {
        Transition {
            from: self.from,
            action: action,
            reward: self.reward,
            to: self.to,
        }
    }

    /// Drop the action associated with this transition and return a new
    /// instance.
    pub fn drop_action(self) -> Transition<S, ()> { self.replace_action(()) }

    pub fn negate_reward(self) -> Transition<S, A> {
        Transition {
            from: self.from,
            action: self.action,
            reward: -self.reward,
            to: self.to,
        }
    }
}

impl_into!(Transition<S, u8> => Transition<S, ()>);
impl_into!(Transition<S, u16> => Transition<S, ()>);
impl_into!(Transition<S, u32> => Transition<S, ()>);
impl_into!(Transition<S, u64> => Transition<S, ()>);
impl_into!(Transition<S, usize> => Transition<S, ()>);
impl_into!(Transition<S, i8> => Transition<S, ()>);
impl_into!(Transition<S, i16> => Transition<S, ()>);
impl_into!(Transition<S, i32> => Transition<S, ()>);
impl_into!(Transition<S, i64> => Transition<S, ()>);
impl_into!(Transition<S, isize> => Transition<S, ()>);
impl_into!(Transition<S, f32> => Transition<S, ()>);
impl_into!(Transition<S, f64> => Transition<S, ()>);

pub type Batch<S, A> = Vec<Transition<S, A>>;

pub struct TrajectoryIter<'a, S, A> {
    init: &'a Observation<S>,
    tail: &'a [(Observation<S>, A, Reward)],
}

impl<'a, S, A> TrajectoryIter<'a, S, A> {
    #[inline]
    fn next_transition(&self) -> Option<Transition<&'a S, &'a A>> {
        Some(Transition {
            from: self.init.borrowed(),
            action: &self.tail[0].1,
            reward: self.tail[0].2,
            to: self.tail[0].0.borrowed(),
        })
    }
}

impl<'a, S, A> Iterator for TrajectoryIter<'a, S, A> {
    type Item = Transition<&'a S, &'a A>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.tail.is_empty() {
            None
        } else {
            let ret = self.next_transition();

            self.init = &self.tail[0].0;
            self.tail = &self.tail[1..];

            ret
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = self.tail.len();

        (n, Some(n))
    }

    #[inline]
    fn count(self) -> usize { self.tail.len() }

    #[inline]
    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        if n > self.tail.len() - 1 {
            self.tail = &[];

            None
        } else {
            self.init = &self.tail[n - 1].0;
            self.tail = &self.tail[n..];

            self.next_transition()
        }
    }

    #[inline]
    fn last(self) -> Option<Self::Item> {
        let n = self.tail.len();

        if n == 0 {
            None
        } else if n == 1 {
            self.next_transition()
        } else {
            Some(Transition {
                from: self.tail[n - 2].0.borrowed(),
                action: &self.tail[n - 1].1,
                reward: self.tail[n - 1].2,
                to: self.tail[n - 1].0.borrowed(),
            })
        }
    }
}

impl<'a, S, A> DoubleEndedIterator for TrajectoryIter<'a, S, A> {
    fn next_back(&mut self) -> Option<Transition<&'a S, &'a A>> {
        let n = self.tail.len();

        if n == 0 {
            None
        } else if n == 1 {
            let ret = Some(Transition {
                from: self.init.borrowed(),
                action: &self.tail[0].1,
                reward: self.tail[0].2,
                to: self.tail[0].0.borrowed(),
            });

            self.tail = &self.tail[..(n - 1)];

            ret
        } else {
            let ret = Some(Transition {
                from: self.tail[n - 2].0.borrowed(),
                action: &self.tail[n - 1].1,
                reward: self.tail[n - 1].2,
                to: self.tail[n - 1].0.borrowed(),
            });

            self.tail = &self.tail[..(n - 1)];

            ret
        }
    }
}

impl<'s, 'a, S, A> std::iter::FromIterator<Transition<&'s S, &'a A>> for Vec<Transition<S, A>> {
    fn from_iter<I: IntoIterator<Item = Transition<&'s S, &'a A>>>(iter: I) -> Self {
        iter.into_iter()
            .map(|t| Transition {
                from: t.from.clone(),
                action: t.action.clone(),
                reward: t.reward,
                to: t.to.clone(),
            })
            .collect()
    }
}

pub struct Trajectory<S, A> {
    pub start: Observation<S>,
    pub steps: Vec<(Observation<S>, A, Reward)>,
}

impl<S, A> Trajectory<S, A> {
    pub fn n_states(&self) -> usize { self.steps.len() + 1 }

    pub fn n_transitions(&self) -> usize { self.steps.len() }

    pub fn first(&self) -> Transition<&S, &A> {
        Transition {
            from: self.start.borrowed(),
            action: &self.steps[0].1,
            reward: self.steps[0].2,
            to: self.steps[0].0.borrowed(),
        }
    }

    pub fn get(&self, index: usize) -> Transition<&S, &A> {
        if index == 0 {
            self.first()
        } else {
            Transition {
                from: self.steps[index].0.borrowed(),
                action: &self.steps[index + 1].1,
                reward: self.steps[index + 1].2,
                to: self.steps[index + 1].0.borrowed(),
            }
        }
    }

    pub fn iter<'a>(&'a self) -> TrajectoryIter<'a, S, A> {
        TrajectoryIter {
            init: &self.start,
            tail: &self.steps,
        }
    }

    pub fn total_reward(&self) -> Reward { self.steps.iter().map(|oar| oar.2).sum() }

    pub fn to_batch(&self) -> Batch<S, A> { self.iter().collect() }

    pub fn into_batch(mut self) -> Batch<S, A>
    where
        S: Clone,
        A: Clone,
    {
        if self.n_transitions() == 0 {
            panic!()
        }

        let mut steps = self.steps.drain(..);
        let step_to_first = steps.next().unwrap();

        let mut batch = vec![Transition {
            from: self.start,
            action: step_to_first.1,
            reward: step_to_first.2,
            to: step_to_first.0,
        }];

        for (i, (ns, a, r)) in steps.enumerate() {
            let from = batch[i].from.clone();

            batch.push(Transition {
                from,
                action: a,
                reward: r,
                to: ns,
            });
        }

        batch
    }
}

pub type Trajectories<S, A> = Vec<Trajectory<S, A>>;

pub type State<D> = <<D as Domain>::StateSpace as Space>::Value;
pub type Action<D> = <<D as Domain>::ActionSpace as Space>::Value;

/// An interface for constructing reinforcement learning problem domains.
pub trait Domain {
    /// State space representation type class.
    type StateSpace: Space;

    /// Action space representation type class.
    type ActionSpace: Space;

    /// Returns an instance of the state space type class.
    fn state_space(&self) -> Self::StateSpace;

    /// Returns an instance of the action space type class.
    fn action_space(&self) -> Self::ActionSpace;

    /// Emit an observation of the current state of the environment.
    fn emit(&self) -> Observation<State<Self>>;

    /// Transition the environment forward a single step given an action, `a`.
    fn step(&mut self, a: &Action<Self>) -> (Observation<State<Self>>, Reward);

    fn transition(&mut self, a: Action<Self>) -> Transition<State<Self>, Action<Self>> {
        let s = self.emit();
        let (ns, r) = self.step(&a);

        Transition {
            from: s,
            action: a,
            reward: r,
            to: ns,
        }
    }

    fn rollout<F>(
        mut self,
        mut pi: F,
        step_limit: Option<usize>,
    ) -> Trajectory<State<Self>, Action<Self>>
    where
        F: FnMut(&State<Self>) -> Action<Self>,
        Self: Sized,
    {
        let start = self.emit();
        let action = pi(start.state());
        let step = self.step(&action);

        let iter = iter::successors(Some((step.0, action, step.1)), |(obs, _, _)| match obs {
            Observation::Terminal(_) => None,
            Observation::Full(ref s) | Observation::Partial(ref s) => {
                let a = pi(s);
                let (ns, r) = self.step(&a);

                Some((ns, a, r))
            },
        });

        Trajectory {
            start,
            steps: if let Some(sl) = step_limit {
                iter.take(sl.saturating_sub(1)).collect()
            } else {
                iter.collect()
            },
        }
    }
}

mod consts;
mod grid_world;
mod macros;

mod ode;
use self::ode::*;

mod mountain_car;
pub use self::mountain_car::*;

mod cart_pole;
pub use self::cart_pole::*;

mod acrobot;
pub use self::acrobot::*;

mod hiv;
pub use self::hiv::*;

mod cliff_walk;
pub use self::cliff_walk::*;

mod roulette;
pub use self::roulette::*;

#[cfg(feature = "openai")]
mod openai;
#[cfg(feature = "openai")]
pub use self::openai::*;