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
// Author: Daniel van Niekerk <dvn.demitasse@gmail.com>
//
// Copyright 2016 The Department of Arts and Culture of the Government
// of South Africa
//
// See the "LICENCE" file for information on usage and redistribution
// of this file.
////////////////////////////////////////////////////////////////////////////////

//! This module implements the generation of random weights for
//! testing purposes. See the source file `test_semiring.rs` for
//! simple examples of intended use.

extern crate rand;
use self::rand::{Rng, StdRng};

use super::*;
use super::float::Float;
use super::floatweight::{TropicalWeight, LogWeight, MinmaxWeight};

//////////////////////////DEFINE HOW DIFFERENT WEIGHTS CAN BE CREATED FROM U32
pub trait RandomWeight: Weight {
    fn from_u32(u32) -> Self;
}

impl<T: Float<T>> RandomWeight for TropicalWeight<T> {
    fn from_u32(n: u32) -> Self {
        Self::new(Some(T::from_u32(n)))
    }
}

impl<T: Float<T>> RandomWeight for LogWeight<T> {
    fn from_u32(n: u32) -> Self {
        Self::new(Some(T::from_u32(n)))
    }
}

impl<T: Float<T>> RandomWeight for MinmaxWeight<T> {
    fn from_u32(n: u32) -> Self {
        Self::new(Some(T::from_u32(n)))
    }
}


//////////////////////////////DEFINE HOW RANDOM WEIGHTS CAN BE CREATED USING RNG

//This bounds the random integers generated: [0, K)
const K: u32 = 5;

pub trait RandomWeightGenerator {
    fn genweight<T: RandomWeight>(&mut self, allow_zero: bool) -> T;
}

impl RandomWeightGenerator for StdRng {
    fn genweight<T: RandomWeight>(&mut self, allow_zero: bool) -> T {
        let n = self.gen_range(0, K + allow_zero as u32);
        if allow_zero && n == K {
            T::zero()
        } else {
            T::from_u32(n)
        }
    }
}