6 个版本 (3 个重大更改)

0.4.1 2020 年 5 月 14 日
0.4.0 2020 年 5 月 13 日
0.3.0 2020 年 5 月 12 日
0.2.0 2020 年 5 月 12 日
0.1.1 2020 年 5 月 12 日

机器学习 中排名第 443

GPL-3.0 许可证

25KB
414

markovr crates.io

高阶马尔可夫链 比您典型的马尔可夫链(仅回顾 1 个元素)具有更长的记忆。

酷特性

  • 任意维度的马尔可夫链。可能存在第 N 阶链。
  • 部分视图元素生成。生成过程中缺少输入?没问题。
  • 快速生成。从训练模型生成值的时间复杂度为 O(lg N),其中 N 是该位置可能输出的数量。
  • 可选确定性。需要更多控制?提供确定性生成函数。

用法

将此添加到您的 Cargo.toml

[dependencies]
markovr = {version = "0.4"}

或者,如果您不想将 rand 包引入到依赖树中

[dependencies]
markovr = {version = "0.4", features = []}

然后,在您的程序中

extern crate markovr;

pub fn main() {
    // Create a new, first-order Markov Chain.
    let mut m = markovr::MarkovChain::new(1, &[]);

    // alpha will be our training data.
    let alpha: Vec<char> = "abcdefghijklmnopqrstuvwxyz".chars().collect();

    // Train the model.
    for i in 1..alpha.len() {
        m.train(&[alpha[i - 1]], alpha[i], 1);
    }

    // Generate values from the model.
    let mut last: Option<char> = Some('a');
    while last.is_some() {
        print!("{} ", last.unwrap());
        last = m.generate(&[last.unwrap()]);
    }
    // Prints: a b c d e f g h i j k l m n o p q r s t u v w x y z

    // What's the probability that 'z' follows 'y'?
    print!("\n{}", m.probability(&[Some('y')], 'z'));
    // Prints: 1
    // What's the probability that 'z' follows 'a'?
    print!("\n{}\n", m.probability(&[Some('a')], 'z'));
    // Prints: 0
}

如果您在寻找一个更复杂的示例,该示例使用未知元素(类似于 WaveFunctionCollapse

extern crate markovr;

pub fn main() {
    // Create a new, fourth-order Markov Chain.
    // We'll keep track of each orthogonal neighbor,
    // and allow for any one of them to be unknown.
    let mut m = markovr::MarkovChain::<char>::new(4, &[0, 1, 2, 3]);

    let train: Vec<Vec<char>> = "           
 ┏━━━┓     
 ┃   ┃     
 ┃   ┣━━━┓ 
 ┃   ┃   ┃ 
 ┃   ┣━━━┛ 
 ┃   ┃     
 ┗━━━┛     
           
"
    .lines()
    .map(|c| c.chars().take(12).collect())
    .collect();

    // Train the model.
    for r in 1..(train.len() - 1) {
        let ref row = train[r];
        for c in 1..(row.len() - 1) {
            // Build up a view of the neighbors.
            let neighbors = &[
                train[r - 1][c],
                train[r][c - 1],
                train[r][c + 1],
                train[r + 1][c],
            ];
            m.train(neighbors, train[r][c], 1);
        }
    }

    // Generate values from the model.
    const DIM: usize = 16;
    let mut map: [[Option<char>; DIM]; DIM];
    'gen: loop {
        map = [[None; DIM]; DIM];
        // Fill in spaces around the border. This isn't necessary,
        // but should prevent dangling lines in the output.
        for i in 0..DIM {
            map[i][0] = Some(' ');
            map[i][DIM - 1] = Some(' ');
            map[0][i] = Some(' ');
            map[DIM - 1][i] = Some(' ');
        }
        // Iterate on all non-None spaces and fill them in.
        for r in 1..(DIM - 1) {
            for c in 1..(DIM - 1) {
                let neighbors = &[map[r - 1][c], map[r][c - 1], map[r][c + 1], map[r + 1][c]];

                map[r][c] = m.generate_from_partial(neighbors);
                match map[r][c] {
                    Some(_) => {}
                    // We saw a case that wasn't in our training data,
                    // so throw it away and try again.
                    None => {
                        continue 'gen;
                    }
                }
            }
        }
        break 'gen;
    }

    for r in 1..(DIM - 1) {
        for c in 1..(DIM - 1) {
            match map[r][c] {
                Some(v) => print!("{}", v),
                None => print!("?"),
            }
        }
        print!("\n");
    }
    // Prints:
    /*
           ┏━━━━┓
           ┃    ┃
           ┃    ┃
      ┏━━━━┛    ┃
      ┃         ┃
    ┏━┛         ┃
    ┃           ┃
    ┃           ┃
    ┃           ┃
    ┣━━━━━━━━━━━┛
    ┃
    ┣━━━┓
    ┃   ┃
    ┗━━━┛
       */
}

依赖项

~530KB