Skip to content

Commit

Permalink
Scaffolding out has_smoothed_weights
Browse files Browse the repository at this point in the history
  • Loading branch information
mattxwang committed Oct 25, 2023
1 parent 39aadfb commit edfe074
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/repr/ddnnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub trait DDNNFPtr<'a>: Clone + Debug + PartialEq + Eq + Hash + Copy {
where
T: 'static,
{
debug_assert!(params.has_smoothed_weights());
self.fold(|ddnnf| {
use DDNNF::*;
match ddnnf {
Expand Down
33 changes: 33 additions & 0 deletions src/repr/wmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,39 @@ impl<T: Semiring> WmcParams<T> {
pub fn var_weight(&self, label: VarLabel) -> &(T, T) {
return (self.var_to_val[label.value_usize()]).as_ref().unwrap();
}

/// ```
/// use rsdd::repr::VarLabel;
/// use rsdd::repr::WmcParams;
/// use rsdd::util::semirings::{Semiring, RealSemiring};
/// use std::collections::HashMap;
///
/// let weights = HashMap::from([
/// (VarLabel::new(0), (RealSemiring(0.0), RealSemiring(1.0))),
/// (VarLabel::new(1), (RealSemiring(0.3), RealSemiring(0.7)))
/// ]);
///
/// let params = WmcParams::<RealSemiring>::new(weights);
///
/// assert!(params.has_smoothed_weights());
///
/// let weights = HashMap::from([
/// (VarLabel::new(0), (RealSemiring(1.0), RealSemiring(1.0))),
/// (VarLabel::new(1), (RealSemiring(1.0), RealSemiring(1.0)))
/// ]);
///
/// let params = WmcParams::<RealSemiring>::new(weights);
///
/// assert!(!params.has_smoothed_weights());
/// ```
/// returns whether or not the weights within the parameters are normalized,
/// i.e. each true/false weight pair sums to the `One` defined in the Semiring
pub fn has_smoothed_weights(&self) -> bool {
self.var_to_val.iter().all(|v| match v {
Some((low, high)) => *low + *high == T::one(),
None => todo!(),
})
}
}

impl<T: Semiring> Debug for WmcParams<T> {
Expand Down
8 changes: 7 additions & 1 deletion src/util/semirings/semiring_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ use std::fmt::{Debug, Display};
use std::ops;

pub trait Semiring:
Debug + Clone + Copy + Display + ops::Add<Self, Output = Self> + ops::Mul<Self, Output = Self>
Debug
+ Clone
+ Copy
+ Display
+ ops::Add<Self, Output = Self>
+ ops::Mul<Self, Output = Self>
+ PartialEq
{
fn one() -> Self;
fn zero() -> Self;
Expand Down

0 comments on commit edfe074

Please sign in to comment.