DfaLearning

Struct DfaLearning 

Source
pub struct DfaLearning<A>
where A: BlackBoxAutomaton<Output = bool>,
{ automaton: A, prefixes: Vec<Vec<usize>>, suffixes: Vec<Vec<usize>>, table: Vec<BitSet>, row_map: HashMap<BitSet, usize>, }

Fields§

§automaton: A§prefixes: Vec<Vec<usize>>§suffixes: Vec<Vec<usize>>§table: Vec<BitSet>§row_map: HashMap<BitSet, usize>

Implementations§

Source§

impl<A> DfaLearning<A>
where A: BlackBoxAutomaton<Output = bool>,

Source

pub fn new(automaton: A) -> Self

Source

fn add_prefix(&mut self, prefix: Vec<usize>) -> usize

Examples found in repository?
crates/competitive/src/algorithm/automata_learning.rs (line 320)
311    pub fn new(automaton: A) -> Self {
312        let mut this = Self {
313            automaton,
314            prefixes: vec![],
315            suffixes: vec![],
316            table: vec![],
317            row_map: HashMap::new(),
318        };
319        this.add_suffix(vec![]);
320        this.add_prefix(vec![]);
321        this
322    }
323    fn add_prefix(&mut self, prefix: Vec<usize>) -> usize {
324        let row: BitSet = self
325            .suffixes
326            .iter()
327            .map(|s| {
328                self.automaton
329                    .behavior(prefix.iter().cloned().chain(s.iter().cloned()))
330            })
331            .collect();
332        *self.row_map.entry(row.clone()).or_insert_with(|| {
333            let idx = self.table.len();
334            self.table.push(row);
335            self.prefixes.push(prefix);
336            idx
337        })
338    }
339    fn add_suffix(&mut self, suffix: Vec<usize>) {
340        if self.suffixes.contains(&suffix) {
341            return;
342        }
343        for (prefix, table) in self.prefixes.iter_mut().zip(&mut self.table) {
344            table.push(
345                self.automaton
346                    .behavior(prefix.iter().cloned().chain(suffix.iter().cloned())),
347            );
348        }
349        self.suffixes.push(suffix);
350        self.row_map.clear();
351        for (i_prefix, row) in self.table.iter().enumerate() {
352            self.row_map.insert(row.clone(), i_prefix);
353        }
354    }
355    pub fn construct_dfa(&mut self) -> DeterministicFiniteAutomaton {
356        let sigma = self.automaton.sigma();
357        let mut dfa = DeterministicFiniteAutomaton {
358            states: vec![],
359            initial_state: 0,
360        };
361        let mut i_prefix = 0;
362        while i_prefix < self.prefixes.len() {
363            let mut delta = vec![];
364            for x in 0..sigma {
365                let prefix: Vec<usize> =
366                    self.prefixes[i_prefix].iter().cloned().chain([x]).collect();
367                let index = self.add_prefix(prefix);
368                delta.push(index);
369            }
370            dfa.states.push(DfaState {
371                delta,
372                accept: self.table[i_prefix].get(0),
373            });
374            i_prefix += 1;
375        }
376        dfa
377    }
378    pub fn train_sample(&mut self, dfa: &DeterministicFiniteAutomaton, sample: &[usize]) -> bool {
379        let expected = self.automaton.behavior(sample.iter().cloned());
380        if expected == dfa.behavior(sample.iter().cloned()) {
381            return false;
382        }
383        let n = sample.len();
384        let mut states: Vec<(usize, usize)> = Vec::with_capacity(n + 1);
385        let mut s = 0usize;
386        states.push((s, 0));
387        for (k, &x) in sample.iter().enumerate() {
388            s = dfa.states[s].delta[x];
389            states.push((s, k + 1));
390        }
391        let split = states.partition_point(|&(state, k)| {
392            self.automaton.behavior(
393                self.prefixes[state]
394                    .iter()
395                    .cloned()
396                    .chain(sample[k..].iter().cloned()),
397            ) == expected
398        });
399        let new_prefix = sample[..split].to_vec();
400        let new_suffix = sample[split..].to_vec();
401        self.add_suffix(new_suffix);
402        self.add_prefix(new_prefix);
403        true
404    }
Source

fn add_suffix(&mut self, suffix: Vec<usize>)

Examples found in repository?
crates/competitive/src/algorithm/automata_learning.rs (line 319)
311    pub fn new(automaton: A) -> Self {
312        let mut this = Self {
313            automaton,
314            prefixes: vec![],
315            suffixes: vec![],
316            table: vec![],
317            row_map: HashMap::new(),
318        };
319        this.add_suffix(vec![]);
320        this.add_prefix(vec![]);
321        this
322    }
323    fn add_prefix(&mut self, prefix: Vec<usize>) -> usize {
324        let row: BitSet = self
325            .suffixes
326            .iter()
327            .map(|s| {
328                self.automaton
329                    .behavior(prefix.iter().cloned().chain(s.iter().cloned()))
330            })
331            .collect();
332        *self.row_map.entry(row.clone()).or_insert_with(|| {
333            let idx = self.table.len();
334            self.table.push(row);
335            self.prefixes.push(prefix);
336            idx
337        })
338    }
339    fn add_suffix(&mut self, suffix: Vec<usize>) {
340        if self.suffixes.contains(&suffix) {
341            return;
342        }
343        for (prefix, table) in self.prefixes.iter_mut().zip(&mut self.table) {
344            table.push(
345                self.automaton
346                    .behavior(prefix.iter().cloned().chain(suffix.iter().cloned())),
347            );
348        }
349        self.suffixes.push(suffix);
350        self.row_map.clear();
351        for (i_prefix, row) in self.table.iter().enumerate() {
352            self.row_map.insert(row.clone(), i_prefix);
353        }
354    }
355    pub fn construct_dfa(&mut self) -> DeterministicFiniteAutomaton {
356        let sigma = self.automaton.sigma();
357        let mut dfa = DeterministicFiniteAutomaton {
358            states: vec![],
359            initial_state: 0,
360        };
361        let mut i_prefix = 0;
362        while i_prefix < self.prefixes.len() {
363            let mut delta = vec![];
364            for x in 0..sigma {
365                let prefix: Vec<usize> =
366                    self.prefixes[i_prefix].iter().cloned().chain([x]).collect();
367                let index = self.add_prefix(prefix);
368                delta.push(index);
369            }
370            dfa.states.push(DfaState {
371                delta,
372                accept: self.table[i_prefix].get(0),
373            });
374            i_prefix += 1;
375        }
376        dfa
377    }
378    pub fn train_sample(&mut self, dfa: &DeterministicFiniteAutomaton, sample: &[usize]) -> bool {
379        let expected = self.automaton.behavior(sample.iter().cloned());
380        if expected == dfa.behavior(sample.iter().cloned()) {
381            return false;
382        }
383        let n = sample.len();
384        let mut states: Vec<(usize, usize)> = Vec::with_capacity(n + 1);
385        let mut s = 0usize;
386        states.push((s, 0));
387        for (k, &x) in sample.iter().enumerate() {
388            s = dfa.states[s].delta[x];
389            states.push((s, k + 1));
390        }
391        let split = states.partition_point(|&(state, k)| {
392            self.automaton.behavior(
393                self.prefixes[state]
394                    .iter()
395                    .cloned()
396                    .chain(sample[k..].iter().cloned()),
397            ) == expected
398        });
399        let new_prefix = sample[..split].to_vec();
400        let new_suffix = sample[split..].to_vec();
401        self.add_suffix(new_suffix);
402        self.add_prefix(new_prefix);
403        true
404    }
Source

pub fn construct_dfa(&mut self) -> DeterministicFiniteAutomaton

Examples found in repository?
crates/competitive/src/algorithm/automata_learning.rs (line 409)
405    pub fn train(
406        &mut self,
407        samples: impl IntoIterator<Item = Vec<usize>>,
408    ) -> DeterministicFiniteAutomaton {
409        let mut dfa = self.construct_dfa();
410        for sample in samples {
411            if self.train_sample(&dfa, &sample) {
412                dfa = self.construct_dfa();
413            }
414        }
415        dfa
416    }
Source

pub fn train_sample( &mut self, dfa: &DeterministicFiniteAutomaton, sample: &[usize], ) -> bool

Examples found in repository?
crates/competitive/src/algorithm/automata_learning.rs (line 411)
405    pub fn train(
406        &mut self,
407        samples: impl IntoIterator<Item = Vec<usize>>,
408    ) -> DeterministicFiniteAutomaton {
409        let mut dfa = self.construct_dfa();
410        for sample in samples {
411            if self.train_sample(&dfa, &sample) {
412                dfa = self.construct_dfa();
413            }
414        }
415        dfa
416    }
Source

pub fn train( &mut self, samples: impl IntoIterator<Item = Vec<usize>>, ) -> DeterministicFiniteAutomaton

Trait Implementations§

Source§

impl<A> Clone for DfaLearning<A>
where A: BlackBoxAutomaton<Output = bool> + Clone,

Source§

fn clone(&self) -> DfaLearning<A>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A> Debug for DfaLearning<A>
where A: BlackBoxAutomaton<Output = bool> + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A> Freeze for DfaLearning<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for DfaLearning<A>
where A: RefUnwindSafe,

§

impl<A> Send for DfaLearning<A>
where A: Send,

§

impl<A> Sync for DfaLearning<A>
where A: Sync,

§

impl<A> Unpin for DfaLearning<A>
where A: Unpin,

§

impl<A> UnwindSafe for DfaLearning<A>
where A: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.