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>,
impl<A> DfaLearning<A>where
A: BlackBoxAutomaton<Output = bool>,
pub fn new(automaton: A) -> Self
Sourcefn add_prefix(&mut self, prefix: Vec<usize>) -> usize
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 }Sourcefn add_suffix(&mut self, suffix: Vec<usize>)
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 }Sourcepub fn construct_dfa(&mut self) -> DeterministicFiniteAutomaton
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 }Sourcepub fn train_sample(
&mut self,
dfa: &DeterministicFiniteAutomaton,
sample: &[usize],
) -> bool
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 }pub fn train( &mut self, samples: impl IntoIterator<Item = Vec<usize>>, ) -> DeterministicFiniteAutomaton
Trait Implementations§
Source§impl<A> Clone for DfaLearning<A>
impl<A> Clone for DfaLearning<A>
Source§fn clone(&self) -> DfaLearning<A>
fn clone(&self) -> DfaLearning<A>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more