frequency and modified RSset to use BTreeSet

This commit is contained in:
elvis
2025-06-24 18:56:04 +02:00
parent aa33e25771
commit 65d6c93424
7 changed files with 225 additions and 48 deletions

View File

@ -1,7 +1,7 @@
#![allow(unused_imports)] #![allow(unused_imports)]
mod rsprocess; mod rsprocess;
use lalrpop_util::lalrpop_mod; use lalrpop_util::lalrpop_mod;
use std::rc::Rc; use std::{hash::Hash, rc::Rc};
use rsprocess::translator::WithTranslator; use rsprocess::translator::WithTranslator;
// use std::io; // use std::io;
@ -195,5 +195,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
println!(); println!();
} }
// -------------------------------------------------------------------------
Ok(()) Ok(())
} }

View File

@ -77,6 +77,7 @@ pub fn confluent_named(
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// see invariant
pub fn invariant_named( pub fn invariant_named(
delta: &RSenvironment, delta: &RSenvironment,
reaction_rules: &[RSreaction], reaction_rules: &[RSreaction],
@ -116,6 +117,7 @@ pub fn invariant_named(
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// see loop_confluent
pub fn loop_confluent_named( pub fn loop_confluent_named(
deltas: &[RSenvironment], deltas: &[RSenvironment],
reaction_rules: &[RSreaction], reaction_rules: &[RSreaction],
@ -127,6 +129,7 @@ pub fn loop_confluent_named(
.collect::<Option<Vec<_>>>() .collect::<Option<Vec<_>>>()
} }
// see strong_confluent
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub fn strong_confluent_named( pub fn strong_confluent_named(
deltas: &[RSenvironment], deltas: &[RSenvironment],

137
src/rsprocess/frequency.rs Normal file
View File

@ -0,0 +1,137 @@
#![allow(dead_code)]
use std::collections::HashMap;
use crate::rsprocess::perpetual::lollipops_only_loop_decomposed_q;
use super::perpetual::{lollipops_decomposed_named, lollipops_only_loop_decomposed_named, lollipops_only_loop_named};
use super::structure::{RSenvironment, RSreaction, RSset, RSsystem};
use super::transitions::run_separated;
use super::translator::IdType;
#[derive(Debug, Clone)]
pub struct Frequency {
pub frequency_map: HashMap<IdType, u32>,
pub total_runs: usize,
pub weight: u32
}
impl Frequency {
pub fn new(weight: u32) -> Self {
Frequency { frequency_map: HashMap::new(), total_runs: 0, weight }
}
pub fn add(&mut self, e: &RSset) {
for &el in e.iter() {
*self.frequency_map.entry(el).or_default() += 1
}
self.total_runs += 1
}
pub fn merge(&mut self, other: Frequency) {
let mut count = 0;
other.frequency_map.iter().for_each(
|(&k, &v)| {
*self.frequency_map.entry(k)
.or_insert_with(|| {count+=1; 0}) += v;
}
);
self.total_runs += count;
// FIXME: ???
self.weight += other.weight;
}
}
// -----------------------------------------------------------------------------
// see naiveFreq
pub fn naive_frequency(system: &RSsystem) -> Result<Frequency, String> {
let ect = run_separated(system)?;
let es = ect.iter().map(|(e, _, _)| e).collect::<Vec<_>>();
let mut freq = Frequency::new(1);
es.iter().for_each(|e| freq.add(e));
Ok(freq)
}
// see naiveFreq
pub fn naive_frequency_weighted(system: &RSsystem, weight: u32) -> Result<Frequency, String> {
let ect = run_separated(system)?;
let es = ect.iter().map(|(e, _, _)| e).collect::<Vec<_>>();
let mut freq = Frequency::new(weight);
es.iter().for_each(|e| freq.add(e));
Ok(freq)
}
// see loopFreq
pub fn loop_frequency(system: &RSsystem, symb: IdType, weight: u32) -> Frequency {
// FIXME: we return the empty frequency or do we not return anything?
let mut freq = Frequency::new(weight);
if let Some(hoop) = lollipops_only_loop_named(system.clone(), symb) {
hoop.iter().for_each(|e| freq.add(e));
}
freq
}
// see limitFreq
pub fn limit_frequency(
q: &[RSset],
reaction_rules: &[RSreaction],
available_entities: &RSset,
weight: u32
) -> Option<Frequency> {
let mut available_entities = available_entities.clone();
for q in q.iter().rev().skip(1).rev() {
let res = lollipops_only_loop_decomposed_q(q,
reaction_rules,
&available_entities);
available_entities = res.into_iter().next()?;
}
let mut freq = Frequency::new(weight);
lollipops_only_loop_decomposed_q(q.last().unwrap(),
reaction_rules,
&available_entities)
.iter().for_each(|e| freq.add(e));
Some(freq)
}
// see fastFreq
pub fn fast_frequency(
q: &[&[RSset]],
reaction_rules: &[RSreaction],
available_entities: &RSset,
weights: &[u32]
) -> Option<Frequency> {
// FIXME: we return the empty frequency or do we not return anything?
let mut available_entities = available_entities.clone();
let mut freq = Frequency::new(1);
for (&q, &w) in q.iter().zip(weights) {
let mut new_freq = Frequency::new(w);
for q in q.iter().rev().skip(1).rev() {
let hoop = lollipops_only_loop_decomposed_q(q,
reaction_rules,
&available_entities);
hoop.iter().for_each(|e| freq.add(e));
available_entities = hoop.into_iter().next()?;
}
lollipops_only_loop_decomposed_q(q.last().unwrap(),
reaction_rules,
&available_entities)
.iter().for_each(|e| new_freq.add(e));
freq.merge(new_freq);
}
Some(freq)
}

View File

@ -5,3 +5,4 @@ pub mod classical;
pub mod transitions; pub mod transitions;
pub mod perpetual; pub mod perpetual;
pub mod confluence; pub mod confluence;
pub mod frequency;

View File

@ -80,17 +80,15 @@ fn find_prefix_len_loop(
fn filter_delta<'a>(x: (&IdType, &'a RSprocess)) -> Option<&'a RSset> { fn filter_delta<'a>(x: (&IdType, &'a RSprocess)) -> Option<&'a RSset> {
use super::structure::RSprocess::*; use super::structure::RSprocess::*;
let (id, rest) = x; let (id, rest) = x;
match rest {
EntitySet{ entities, next_process} => { if let EntitySet{ entities, next_process} = rest {
match &**next_process { if let RecursiveIdentifier{ identifier } = &**next_process {
RecursiveIdentifier{ identifier } if identifier == id => { if identifier == id {
Some(entities) return Some(entities);
},
_ => None
} }
},
_ => None
} }
}
None
} }
@ -180,17 +178,15 @@ fn filter_delta_named<'a>(
if id != symb { if id != symb {
return None; return None;
} }
match rest {
EntitySet{ entities, next_process} => { if let EntitySet{ entities, next_process} = rest {
match &**next_process { if let RecursiveIdentifier{ identifier } = &**next_process {
RecursiveIdentifier{ identifier } if identifier == id => { if identifier == id {
Some(entities) return Some(entities);
},
_ => None
} }
},
_ => None
} }
}
None
} }
// see lollipop // see lollipop
@ -200,8 +196,6 @@ pub fn lollipops_decomposed_named(
available_entities: &RSset, available_entities: &RSset,
symb: IdType symb: IdType
) -> Option<(Vec<RSset>, Vec<RSset>)> { ) -> Option<(Vec<RSset>, Vec<RSset>)> {
// FIXME: i think we are only interested in "x", not all symbols that
// satisfy X = pre(Q, rec(X))
let filtered = delta.iter().filter_map(|x| filter_delta_named(x, &symb)).next(); let filtered = delta.iter().filter_map(|x| filter_delta_named(x, &symb)).next();
let find_loop_fn = |q| find_loop(reaction_rules, let find_loop_fn = |q| find_loop(reaction_rules,
@ -227,8 +221,6 @@ pub fn lollipops_only_loop_named(
system: RSsystem, system: RSsystem,
symb: IdType symb: IdType
) -> Option<Vec<RSset>> { ) -> Option<Vec<RSset>> {
// FIXME: i think we are only interested in "x", not all symbols that
// satisfy X = pre(Q, rec(X))
let filtered = system.get_delta().iter() let filtered = system.get_delta().iter()
.filter_map(|x| filter_delta_named(x, &symb)).next(); .filter_map(|x| filter_delta_named(x, &symb)).next();
@ -246,8 +238,6 @@ pub fn lollipops_prefix_len_loop_decomposed_named(
available_entities: &RSset, available_entities: &RSset,
symb: IdType symb: IdType
) -> Option<(usize, Vec<RSset>)> { ) -> Option<(usize, Vec<RSset>)> {
// FIXME: i think we are only interested in "x", not all symbols that
// satisfy X = pre(Q, rec(X))
let filtered = delta.iter() let filtered = delta.iter()
.filter_map(|x| filter_delta_named(x, &symb)).next(); .filter_map(|x| filter_delta_named(x, &symb)).next();
@ -265,8 +255,6 @@ pub fn lollipops_only_loop_decomposed_named(
available_entities: &RSset, available_entities: &RSset,
symb: IdType symb: IdType
) -> Option<Vec<RSset>> { ) -> Option<Vec<RSset>> {
// FIXME: i think we are only interested in "x", not all symbols that
// satisfy X = pre(Q, rec(X))
let filtered = delta.iter() let filtered = delta.iter()
.filter_map(|x| filter_delta_named(x, &symb)).next(); .filter_map(|x| filter_delta_named(x, &symb)).next();
@ -276,3 +264,16 @@ pub fn lollipops_only_loop_decomposed_named(
filtered.map(find_loop_fn) filtered.map(find_loop_fn)
} }
// see loop/5
pub fn lollipops_only_loop_decomposed_q(
q: &RSset,
reaction_rules: &[RSreaction],
available_entities: &RSset,
) -> Vec<RSset> {
let find_loop_fn = |q| find_only_loop(reaction_rules,
available_entities.clone(),
q);
find_loop_fn(q)
}

View File

@ -1,6 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::collections::{HashMap, HashSet}; use std::collections::{BTreeSet, HashMap, HashSet};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::rc::Rc; use std::rc::Rc;
use super::translator::{IdType}; use super::translator::{IdType};
@ -8,32 +8,32 @@ use super::translator::{IdType};
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// RSset // RSset
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RSset { pub struct RSset {
identifiers: HashSet<IdType> identifiers: BTreeSet<IdType>
} }
impl<const N: usize> From<[IdType; N]> for RSset { impl<const N: usize> From<[IdType; N]> for RSset {
fn from(arr: [IdType; N]) -> Self { fn from(arr: [IdType; N]) -> Self {
RSset{identifiers: HashSet::from(arr)} RSset{identifiers: BTreeSet::from(arr)}
} }
} }
impl From<&[IdType]> for RSset { impl From<&[IdType]> for RSset {
fn from(arr: &[IdType]) -> Self { fn from(arr: &[IdType]) -> Self {
RSset{identifiers: HashSet::from_iter(arr.to_vec())} RSset{identifiers: BTreeSet::from_iter(arr.to_vec())}
} }
} }
impl From<Vec<IdType>> for RSset { impl From<Vec<IdType>> for RSset {
fn from(arr: Vec<IdType>) -> Self { fn from(arr: Vec<IdType>) -> Self {
RSset{identifiers: HashSet::from_iter(arr)} RSset{identifiers: BTreeSet::from_iter(arr)}
} }
} }
impl RSset { impl RSset {
pub fn new() -> Self { pub fn new() -> Self {
RSset{identifiers: HashSet::new()} RSset{identifiers: BTreeSet::new()}
} }
pub fn is_subset(&self, b: &RSset) -> bool { pub fn is_subset(&self, b: &RSset) -> bool {
@ -61,7 +61,7 @@ impl RSset {
pub fn intersection(&self, b: &RSset) -> RSset { pub fn intersection(&self, b: &RSset) -> RSset {
// TODO maybe find more efficient way without copy/clone // TODO maybe find more efficient way without copy/clone
let res: HashSet<_> = b.identifiers.intersection(&self.identifiers) let res: BTreeSet<_> = b.identifiers.intersection(&self.identifiers)
.copied() .copied()
.collect(); .collect();
RSset { identifiers: res } RSset { identifiers: res }
@ -69,28 +69,31 @@ impl RSset {
pub fn subtraction(&self, b: &RSset) -> RSset { pub fn subtraction(&self, b: &RSset) -> RSset {
// TODO maybe find more efficient way without copy/clone // TODO maybe find more efficient way without copy/clone
let res: HashSet<_> = self.identifiers.difference(&b.identifiers) let res: BTreeSet<_> = self.identifiers.difference(&b.identifiers)
.copied() .copied()
.collect(); .collect();
RSset { identifiers: res } RSset { identifiers: res }
} }
pub fn hashset(&self) -> &HashSet<IdType> { pub fn hashset(&self) -> &BTreeSet<IdType> {
&self.identifiers &self.identifiers
} }
pub fn iter(&self) -> std::collections::btree_set::Iter<'_, IdType> {
self.identifiers.iter()
}
} }
impl Hash for RSset { impl IntoIterator for RSset {
fn hash<H: Hasher>(&self, state: &mut H) { type Item = IdType;
let mut a: Vec<&_> = self.identifiers.iter().collect(); type IntoIter = std::collections::btree_set::IntoIter<Self::Item>;
a.sort();
for s in a.iter() {
s.hash(state);
}
}
fn into_iter(self) -> Self::IntoIter {
self.identifiers.into_iter()
}
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// RSreaction // RSreaction
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@ -41,10 +41,10 @@ impl Translator {
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
use super::structure::{ use super::{frequency::{self, Frequency}, structure::{
RSassert, RSassertOp, RSchoices, RSenvironment, RSlabel, RSprocess, RSassert, RSassertOp, RSchoices, RSenvironment, RSlabel, RSprocess,
RSreaction, RSset, RSsystem, RSBHML, RSreaction, RSset, RSsystem, RSBHML,
}; }};
use std::fmt; use std::fmt;
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
@ -91,6 +91,10 @@ pub enum WithTranslator<'a> {
translator: &'a Translator, translator: &'a Translator,
bhml: &'a RSBHML, bhml: &'a RSBHML,
}, },
Frequency {
translator: &'a Translator,
frequency: &'a Frequency,
}
} }
macro_rules! from_RS { macro_rules! from_RS {
@ -126,6 +130,8 @@ impl<'a> WithTranslator<'a> {
from_RS!(from_RSassert, RSassert, assert, RSassert); from_RS!(from_RSassert, RSassert, assert, RSassert);
from_RS!(from_RSBHML, RSBHML, bhml, RSBHML); from_RS!(from_RSBHML, RSBHML, bhml, RSBHML);
from_RS!(from_Frequency, Frequency, frequency, Frequency);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -367,6 +373,28 @@ fn print_bhml(
todo!() todo!()
} }
fn print_frequency(
f: &mut fmt::Formatter,
translator: &Translator,
frequency: &Frequency,
) -> fmt::Result {
write!(f, "[")?;
let mut it = frequency.frequency_map.iter().peekable();
while let Some((e, freq)) = it.next() {
if it.peek().is_none() {
write!(f, "{} -> {:.2}",
translator.decode(*e),
(*freq as f32 * frequency.weight as f32 * 100.)/(frequency.total_runs as f32))?;
} else {
write!(f, "{} -> {:.2}, ",
translator.decode(*e),
(*freq as f32 * frequency.weight as f32 * 100.)/(frequency.total_runs as f32))?;
}
}
write!(f, "]")
}
impl<'a> fmt::Display for WithTranslator<'a> { impl<'a> fmt::Display for WithTranslator<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
@ -390,6 +418,8 @@ impl<'a> fmt::Display for WithTranslator<'a> {
print_assert(f, translator, assert), print_assert(f, translator, assert),
WithTranslator::RSBHML { translator, bhml, } => WithTranslator::RSBHML { translator, bhml, } =>
print_bhml(f, translator, bhml), print_bhml(f, translator, bhml),
WithTranslator::Frequency { translator, frequency } =>
print_frequency(f, translator, frequency),
} }
} }
} }