Skip to main content

RandomWalkMethod

Struct RandomWalkMethod 

Source
pub struct RandomWalkMethod { /* private fields */ }
Expand description

Random-walk based equivalence-test strategy.

Implementations§

Source§

impl RandomWalkMethod

Source

pub fn new( knowledge_base: Arc<Mutex<dyn KnowledgeBaseTrait>>, input_letters: Vec<Letter>, max_steps: usize, restart_probability: f64, ) -> Self

Create a new random-walk equivalence tester.

max_steps bounds the number of transitions explored, and restart_probability controls how often the walk restarts at the initial state.

Examples found in repository?
examples/all_eqtests_custom_kb.rs (line 30)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    println!("=== Custom KB: All Equivalence Tests ===");
16    println!("System under learning: ATM protocol");
17
18    let mut reports = Vec::new();
19
20    let strategies: Vec<(&str, Box<EqBuilder>)> = vec![
21        (
22            "WMethodEQ",
23            Box::new(|kb, input_letters, max_states| {
24                Arc::new(WMethodEQ::new(kb, input_letters, max_states))
25            }),
26        ),
27        (
28            "RandomWalkMethod",
29            Box::new(|kb, input_letters, _| {
30                Arc::new(RandomWalkMethod::new(kb, input_letters, 10_000, 0.75))
31            }),
32        ),
33        (
34            "BDistMethod",
35            Box::new(|kb, input_letters, _| Arc::new(BDistMethod::new(kb, input_letters, 2))),
36        ),
37        (
38            "MultipleEqtests",
39            Box::new(|kb, input_letters, max_states| {
40                let eqtests: Vec<Arc<dyn EquivalenceTest>> = vec![
41                    Arc::new(WMethodEQ::new(
42                        kb.clone(),
43                        input_letters.clone(),
44                        max_states,
45                    )),
46                    Arc::new(RandomWalkMethod::new(
47                        kb.clone(),
48                        input_letters.clone(),
49                        5_000,
50                        0.65,
51                    )),
52                    Arc::new(BDistMethod::new(kb, input_letters, 2)),
53                ];
54                Arc::new(MultipleEqtests::new(eqtests))
55            }),
56        ),
57    ];
58
59    for (name, builder) in strategies {
60        reports.push(run_strategy(name, builder.as_ref()));
61    }
62
63    print_summary(&reports);
64
65    Ok(())
66}
More examples
Hide additional examples
examples/random_walk_eq.rs (lines 44-49)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    println!("=== Random Walk Equivalence Test Example ===\n");
11
12    // Create a simple system
13    let mut kb = DemoKnowledgeBase::new();
14
15    // Define a simple state machine:
16    // S0 -a/0-> S1   (initial state)
17    // S0 -b/1-> S0
18    // S1 -a/0-> S2
19    // S1 -b/1-> S0
20    // S2 -a/0-> S2
21    // S2 -b/1-> S1
22
23    // Make outputs state-dependent so learner must distinguish states
24    kb.add_transition("s0", "a", "0", "s1");
25    kb.add_transition("s0", "b", "1", "s0");
26    kb.add_transition("s1", "a", "1", "s2");
27    kb.add_transition("s1", "b", "0", "s0");
28    kb.add_transition("s2", "a", "1", "s2");
29    kb.add_transition("s2", "b", "0", "s1");
30
31    let knowledge_base = Arc::new(Mutex::new(kb));
32
33    // Create vocabulary
34    let vocabulary = vec!["a".to_string(), "b".to_string()];
35
36    // Create learner
37    let mut lstar = LSTAR::new(vocabulary.clone(), knowledge_base.clone(), 5, None, None);
38
39    // Use random walk equivalence test instead of W-method
40    let input_letters = vocabulary
41        .iter()
42        .map(|s| Letter::new(s))
43        .collect::<Vec<_>>();
44    let random_walk = RandomWalkMethod::new(
45        knowledge_base.clone(),
46        input_letters,
47        10000, // max_steps: 10000 steps
48        0.75,  // restart_probability: 75%
49    );
50
51    lstar = lstar.with_equivalence_test(Arc::new(random_walk));
52
53    // Run learning
54    match lstar.learn() {
55        Ok(automata) => {
56            println!("\n=== Learned Automaton (Random Walk Test) ===\n");
57            println!("{}", automata.build_dot_code());
58            println!("\nLearning completed successfully!");
59
60            // Print statistics
61            println!("\nStatistics:");
62            println!("  Number of states: {}", automata.get_states().len());
63            println!("  Number of transitions: {}", automata.transitions.len());
64        }
65        Err(e) => eprintln!("Error during learning: {}", e),
66    }
67
68    let kb_guard = knowledge_base.lock().unwrap();
69    println!("\nKnowledge Base Statistics:\n{}", kb_guard.stats);
70
71    Ok(())
72}

Trait Implementations§

Source§

impl EquivalenceTest for RandomWalkMethod

Source§

fn find_counterexample( &self, hypothesis: &mut Automata, ) -> Option<Counterexample>

Find a counterexample for the given hypothesis Returns None if the automaton is equivalent to the system under learning

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.