tinymind 0.3.2

Neural networks for embedded Rust: a no_std, allocation-free port of the tinymind (C++) library, bit-exact with it
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
//! Tabular Q-learning, ported from tinymind (C++)'s `qlearn.hpp`: [`QLearner`]
//! (`QLearner` with `DefaultLearningPolicy` and `ArgMaxPolicy`), [`QTable`]
//! (`QValueTablePolicy`) and the [`Environment`] trait (what the C++ example
//! environments derive from `QLearningEnvironment` and supply as policies).
//!
//! The learner keeps a table of Q values, one per (state, action) pair, in
//! the value type of your choice (`Q16_16`, `Q8_8`, `f32`, `f64`): a 6-state,
//! 6-action Q16.16 table is 144 bytes. You describe the world through
//! [`Environment`]: which actions are valid in a state, and a source of random
//! integers for exploration. The loop that moves through the world is yours:
//! ask the learner for an action, apply it, and report the reward and the new
//! state.
//!
//! The update is tinymind (C++)'s, operation for operation:
//!
//! ```text
//! Q(s, a) = (1 - lr) * Q(s, a) + lr * (reward + discount * max_a' Q(s', a'))
//! ```
//!
//! for the experience's own state `s`, where the maximum runs over the actions
//! valid in the next state `s'` (0 if there are none): the greedy value,
//! whatever the learner explores, so the table converges to the optimal Q
//! values. Action selection explores with probability
//! `random_action_decision_point` percent; otherwise it takes the action with
//! the largest Q value, as tinymind (C++)'s `ArgMaxPolicy` does (the first of
//! equal maxima), unless every valid action has the same Q value (an unvisited
//! state), in which case a random valid action is taken.
//!
//! ```
//! use tinymind::qlearn::{Environment, Experience, QLearner};
//! use tinymind::rand::GlibcRand;
//! use tinymind::Q16_16;
//!
//! // A corridor of 4 cells; action 0 moves left, 1 moves right; cell 3 is the goal.
//! struct Corridor(GlibcRand);
//!
//! impl Environment<2> for Corridor {
//!     fn valid_actions(&self, _state: usize, actions: &mut [usize; 2]) -> usize {
//!         *actions = [0, 1];
//!         2
//!     }
//!     fn random_int(&mut self, min: usize, max: usize) -> usize {
//!         self.0.next_i32() as usize % (max + 1 - min) + min
//!     }
//! }
//!
//! let rate = Q16_16::from_int(1) / Q16_16::from_int(5);
//! let discount = Q16_16::from_int(8) / Q16_16::from_int(10);
//! // Explore 100% of the time while learning.
//! let mut learner =
//!     QLearner::<Q16_16, Corridor, 4, 2>::new(Corridor(GlibcRand::new(7)), rate, discount, 100);
//! for _ in 0..200 {
//!     learner.start_new_episode();
//!     let mut state = 0;
//!     while state != 3 {
//!         let action = learner.take_action(state).unwrap();
//!         let next = if action == 0 { state.saturating_sub(1) } else { state + 1 };
//!         let reward = if next == 3 { Q16_16::ONE } else { Q16_16::ZERO };
//!         learner.update_from_experience(&Experience { state, action, reward, new_state: next });
//!         state = next;
//!     }
//! }
//! // Now exploit: from every cell the learner heads right.
//! learner.set_random_action_decision_point(0);
//! for cell in 0..3 {
//!     assert_eq!(learner.take_action(cell), Some(1));
//! }
//! ```
//!
//! tinymind (C++)'s deep Q-network policy (`QValueNeuralNetworkPolicy`) is not
//! ported yet.

use crate::qformat::Scalar;

/// The world a [`QLearner`] acts in: tinymind (C++)'s environment (the
/// `getValidActionsForState` an example environment defines, and its random
/// number policy's `randInt`).
pub trait Environment<const ACTIONS: usize> {
    /// Writes the actions valid in `state` to the front of `actions` and
    /// returns how many there are (0 if none; values above `ACTIONS` count as
    /// `ACTIONS`).
    fn valid_actions(&self, state: usize, actions: &mut [usize; ACTIONS]) -> usize;

    /// A uniformly distributed integer in `min..=max`, for exploration. The
    /// learner asks for `random_int(0, 99)` to decide whether to explore and
    /// `random_int(0, n - 1)` to pick one of `n` valid actions.
    fn random_int(&mut self, min: usize, max: usize) -> usize;
}

/// Where a [`QLearner`] keeps its Q values: tinymind (C++)'s Q value policy.
/// [`QTable`] is the tabular one.
pub trait QValues<V> {
    /// The Q value of taking `action` in `state`.
    fn q_value(&self, state: usize, action: usize) -> V;
    /// The Q value of the best action in the next state, used for the
    /// update's target. A table returns [`q_value`](QValues::q_value).
    fn future_q_value(&mut self, state: usize, action: usize) -> V;
    /// Stores a new Q value for `action` in `state`.
    fn set_q_value(&mut self, state: usize, action: usize, value: V);
}

/// A Q table, `values[state][action]` (tinymind (C++)'s `QValueTablePolicy`).
/// Reading a state or action out of range gives 0 and writing one does nothing,
/// as in the C++.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct QTable<V, const STATES: usize, const ACTIONS: usize> {
    /// The Q values, by state then action.
    pub values: [[V; ACTIONS]; STATES],
}

impl<V: Scalar, const STATES: usize, const ACTIONS: usize> QTable<V, STATES, ACTIONS> {
    /// A table of zeros, the C++ initial Q value.
    pub fn new() -> Self {
        Self::from_values([[V::ZERO; ACTIONS]; STATES])
    }
}

impl<V, const STATES: usize, const ACTIONS: usize> QTable<V, STATES, ACTIONS> {
    /// A table holding `values`, for example ones learned earlier.
    pub const fn from_values(values: [[V; ACTIONS]; STATES]) -> Self {
        Self { values }
    }
}

impl<V: Scalar, const STATES: usize, const ACTIONS: usize> Default for QTable<V, STATES, ACTIONS> {
    fn default() -> Self {
        Self::new()
    }
}

impl<V: Scalar, const STATES: usize, const ACTIONS: usize> QValues<V>
    for QTable<V, STATES, ACTIONS>
{
    fn q_value(&self, state: usize, action: usize) -> V {
        self.values
            .get(state)
            .and_then(|row| row.get(action))
            .copied()
            .unwrap_or(V::ZERO)
    }

    fn future_q_value(&mut self, state: usize, action: usize) -> V {
        self.q_value(state, action)
    }

    fn set_q_value(&mut self, state: usize, action: usize, value: V) {
        if let Some(q) = self
            .values
            .get_mut(state)
            .and_then(|row| row.get_mut(action))
        {
            *q = value;
        }
    }
}

/// One step's outcome, reported to [`QLearner::update_from_experience`]
/// (tinymind (C++)'s `experience_t`).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Experience<V> {
    /// The state the step started in; the update changes its Q value. A state
    /// of `STATES` or more changes nothing (tinymind (C++)'s invalid state).
    pub state: usize,
    /// The action taken.
    pub action: usize,
    /// The reward it earned.
    pub reward: V,
    /// The state it led to.
    pub new_state: usize,
}

/// A Q-learning agent over `STATES` states and `ACTIONS` actions
/// (tinymind (C++)'s `QLearner`). It owns its [`Environment`] and its Q values (a
/// [`QTable`] unless built with [`with_q_values`](Self::with_q_values)).
#[derive(Clone, Debug)]
pub struct QLearner<V, E, const STATES: usize, const ACTIONS: usize, Q = QTable<V, STATES, ACTIONS>>
{
    environment: E,
    q_values: Q,
    state: Option<usize>,
    learning_rate: V,
    discount_factor: V,
    random_action_decision_point: usize,
}

impl<V: Scalar, E: Environment<ACTIONS>, const STATES: usize, const ACTIONS: usize>
    QLearner<V, E, STATES, ACTIONS>
{
    /// A learner with a zeroed [`QTable`]. `learning_rate` and
    /// `discount_factor` must be in [0, 1] and `random_action_decision_point`
    /// (the exploration percentage) at most 100; a value outside that range
    /// is ignored, leaving 0, as in the C++.
    pub fn new(
        environment: E,
        learning_rate: V,
        discount_factor: V,
        random_action_decision_point: usize,
    ) -> Self {
        Self::with_q_values(
            environment,
            QTable::new(),
            learning_rate,
            discount_factor,
            random_action_decision_point,
        )
    }
}

impl<V: Scalar, E: Environment<ACTIONS>, const STATES: usize, const ACTIONS: usize, Q: QValues<V>>
    QLearner<V, E, STATES, ACTIONS, Q>
{
    /// A learner over the given Q values; otherwise as [`new`](QLearner::new).
    pub fn with_q_values(
        environment: E,
        q_values: Q,
        learning_rate: V,
        discount_factor: V,
        random_action_decision_point: usize,
    ) -> Self {
        // The C++ static_asserts.
        const {
            assert!(STATES > 1, "a QLearner needs at least two states");
            assert!(ACTIONS > 1, "a QLearner needs at least two actions");
        }
        let mut learner = Self {
            environment,
            q_values,
            state: None,
            learning_rate: V::ZERO,
            discount_factor: V::ZERO,
            random_action_decision_point: 0,
        };
        learner.set_learning_rate(learning_rate);
        learner.set_discount_factor(discount_factor);
        learner.set_random_action_decision_point(random_action_decision_point);
        learner
    }

    /// The environment.
    pub fn environment(&self) -> &E {
        &self.environment
    }

    /// The environment, mutably (for example to draw from its generator).
    pub fn environment_mut(&mut self) -> &mut E {
        &mut self.environment
    }

    /// The Q values.
    pub fn q_values(&self) -> &Q {
        &self.q_values
    }

    /// The Q values, mutably.
    pub fn q_values_mut(&mut self) -> &mut Q {
        &mut self.q_values
    }

    /// The Q value of taking `action` in `state`.
    pub fn q_value(&self, state: usize, action: usize) -> V {
        self.q_values.q_value(state, action)
    }

    /// Sets the Q value of taking `action` in `state`.
    pub fn set_q_value(&mut self, state: usize, action: usize, value: V) {
        self.q_values.set_q_value(state, action, value);
    }

    /// The learning rate.
    pub fn learning_rate(&self) -> V {
        self.learning_rate
    }

    /// Sets the learning rate if it is in [0, 1]; otherwise keeps the old one.
    pub fn set_learning_rate(&mut self, rate: V) {
        if is_in_unit_interval(rate) {
            self.learning_rate = rate;
        }
    }

    /// The discount factor.
    pub fn discount_factor(&self) -> V {
        self.discount_factor
    }

    /// Sets the discount factor if it is in [0, 1]; otherwise keeps the old
    /// one.
    pub fn set_discount_factor(&mut self, factor: V) {
        if is_in_unit_interval(factor) {
            self.discount_factor = factor;
        }
    }

    /// How often the learner explores, in percent: 0 never, 100 always.
    pub fn random_action_decision_point(&self) -> usize {
        self.random_action_decision_point
    }

    /// Sets the exploration percentage if it is at most 100; otherwise keeps
    /// the old one.
    pub fn set_random_action_decision_point(&mut self, percent: usize) {
        if percent <= 100 {
            self.random_action_decision_point = percent;
        }
    }

    /// The current state, `None` at the start of an episode.
    pub fn state(&self) -> Option<usize> {
        self.state
    }

    /// Sets the current state if it is below `STATES`.
    pub fn set_state(&mut self, state: usize) {
        if state < STATES {
            self.state = Some(state);
        }
    }

    /// Forgets the current state (the C++ invalid state), as at the start of
    /// an episode.
    pub fn start_new_episode(&mut self) {
        self.state = None;
    }

    /// Chooses an action in `state` (exploring or exploiting), makes `state`
    /// the current state and returns the action, or `None` if `state` has no
    /// valid action.
    pub fn take_action(&mut self, state: usize) -> Option<usize> {
        let action = self.choose_action(state);
        self.set_state(state);
        action
    }

    /// Learns from a step: updates the Q value of `experience.action` in
    /// `experience.state` (unless the learning rate is 0 or the state is out of
    /// range), whatever the learner's current state, and moves to
    /// `experience.new_state`.
    pub fn update_from_experience(&mut self, experience: &Experience<V>) {
        let learning_rate = self.learning_rate;
        if learning_rate > V::ZERO && experience.state < STATES {
            let one_minus_learning_rate = V::ONE - learning_rate;
            let discount_factor = self.discount_factor;
            let old_q_value = self.q_values.q_value(experience.state, experience.action);
            let future_q_value = self.future_q_value(experience.new_state);
            let q_value = (one_minus_learning_rate * old_q_value)
                + (learning_rate * (experience.reward + (discount_factor * future_q_value)));
            self.q_values
                .set_q_value(experience.state, experience.action, q_value);
        }
        self.state = Some(experience.new_state);
    }

    /// max over the actions valid in `state` of their Q value, from the action
    /// [`best_action`](Self::best_action) picks (the first valid one when all
    /// tie, since each is then the maximum); 0 when none is valid. Never draws
    /// from the generator.
    fn future_q_value(&mut self, state: usize) -> V {
        let mut buffer = [0; ACTIONS];
        let count = self.environment.valid_actions(state, &mut buffer);
        let valid = buffer.get(..count).unwrap_or(&buffer);
        let Some(&first) = valid.first() else {
            return V::ZERO;
        };
        let action = self.best_action(state, valid).unwrap_or(first);
        self.q_values.future_q_value(state, action)
    }

    fn choose_action(&mut self, state: usize) -> Option<usize> {
        let mut buffer = [0; ACTIONS];
        let count = self.environment.valid_actions(state, &mut buffer);
        let valid = buffer.get(..count).unwrap_or(&buffer);
        if self.should_choose_random_action() {
            self.random_action(valid)
        } else {
            match self.best_action(state, valid) {
                Some(action) => Some(action),
                None => self.random_action(valid),
            }
        }
    }

    fn should_choose_random_action(&mut self) -> bool {
        match self.random_action_decision_point {
            0 => false,
            100 => true,
            // 100 equally likely values, `point` of them below it: a random
            // action with probability exactly point / 100.
            point => self.environment.random_int(0, 99) < point,
        }
    }

    fn random_action(&mut self, valid: &[usize]) -> Option<usize> {
        match valid {
            [] => None,
            [action] => Some(*action),
            _ => {
                let index = self.environment.random_int(0, valid.len() - 1);
                valid.get(index).copied()
            }
        }
    }

    /// tinymind (C++)'s `ArgMaxPolicy::selectBestActionForState`.
    fn best_action(&self, state: usize, valid: &[usize]) -> Option<usize> {
        match valid {
            [] => None,
            [action] => Some(*action),
            [a0, a1] => {
                let (q0, q1) = (
                    self.q_values.q_value(state, *a0),
                    self.q_values.q_value(state, *a1),
                );
                if q0 > q1 {
                    Some(*a0)
                } else if q1 > q0 {
                    Some(*a1)
                } else {
                    // A tie is no preference, as below.
                    None
                }
            }
            [first, rest @ ..] => {
                // The largest Q value, whatever its sign (the first of equal
                // maxima). Only when every valid action ties, as in an
                // unvisited state of a zeroed table, is there no preference:
                // `None` makes the caller take a random action (and the
                // update's bootstrap any of them, since each is the maximum).
                let mut best_action = *first;
                let mut best_q_value = self.q_values.q_value(state, best_action);
                let mut all_tied = true;
                for &action in rest {
                    let q_value = self.q_values.q_value(state, action);
                    if q_value != best_q_value {
                        all_tied = false;
                    }
                    if q_value > best_q_value {
                        best_q_value = q_value;
                        best_action = action;
                    }
                }
                if all_tied { None } else { Some(best_action) }
            }
        }
    }
}

/// tinymind (C++)'s `isInUnitInterval`: `!(v < 0) && !(1 < v)`, so a float NaN
/// passes, as in the C++.
#[allow(clippy::neg_cmp_op_on_partial_ord)]
fn is_in_unit_interval<V: Scalar>(value: V) -> bool {
    !(value < V::ZERO) && !(V::ONE < value)
}