Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffolding out has_smoothed_weights #185

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading