Usage

API Documentation

The API documentation can be found here.

Examples

This is the most basic example of a simulation.

// This is a simple onedimensional random walk
use entromatica::prelude::*;
use std::sync::Arc;

// The initial state. It has to be Hash + Clone + Send + Sync + PartialEq + Eq + Debug
let initial_state: i32 = 0;

// The state transition generator. The simulation panics if the probabilities don't sum to 1.0
let state_transition_generator =
Arc::new(|state: i32| vec![(state + 1, "next", 0.5), (state - 1, "previous", 0.5)]);

let mut simulation = Simulation::new(initial_state, state_transition_generator);

// The Shannon-entropy at the given time
assert_eq!(simulation.entropy(0), 0.0);
simulation.next_step();
assert_eq!(simulation.entropy(1), 1.0);

This is another example using the rules module.

use entromatica::prelude::*;
use entromatica::models::rules::*;
use std::sync::Arc;
use hashbrown::HashMap;

// A random walk where the chance of suddenly returning to the initial state is 0.1
let initial_state = 0;
let return_rule: Rule<i32> = Rule::new(
    "Return".to_string(),
    Arc::new(|state| state != 0),
    0.1,
    Arc::new(|_| 0),
);
let forward_rule: Rule<i32> = Rule::new(
    "Forward".to_string(),
    Arc::new(|_| true),
    1.,
    Arc::new(|state| state + 1),
);

let backward_rule: Rule<i32> = Rule::new(
    "Backward".to_string(),
    Arc::new(|_| true),
    1.,
    Arc::new(|state| state - 1),
);

let rules = vec![forward_rule, backward_rule, return_rule];

let state_transition_generator = get_state_transition_generator(rules);
let mut simulation = Simulation::new(initial_state, state_transition_generator);

// state == 0
assert_eq!(simulation.probability_distribution(0).len(), 1);

// now -1 and 1 are equally likely
simulation.next_step();
assert_eq!(simulation.probability_distribution(1).len(), 2);

// now are -2, 0 and 2 possible
simulation.next_step();
assert_eq!(simulation.probability_distribution(2).len(), 3);

// and last but not least -3, -1, 0, 1 and 3. 0 is only possible because of the return rule
simulation.next_step();
assert_eq!(simulation.probability_distribution(3).len(), 5);
Table of Contents