sybila / biodivine-lib-bdd

A small library for BDD manipulation in Rust. Part of the BioDivine toolset.
MIT License
20 stars 4 forks source link

Add a restrict algorithm #30

Closed ellmau closed 2 years ago

ellmau commented 2 years ago

I am missing a restrict functionality, where one can restrict one variable to a given truth value. The var_select method is not doing that, as it computes B \land (var <=> val).

A couple of easy examples:

a xor b with a -> true, b -> false should result in true a or b with a -> false should result in b a and b with a -> true, b -> true should result in true a and b with a -> false, b-> false should result in false

daemontus commented 2 years ago

Maybe I am missing something, but from your description, it sounds like var_select followed by var_project would do what you are after, right?

I have implemented a quick prototype here - if this corresponds to what you are describing, I can put it into the next release :) (Granted, there is probably a slightly faster algorithm to do this in one "go" - I don't have the time to implement it at the moment though, maybe later this year if everything goes well)

ellmau commented 2 years ago

Thanks for the reply, i will have a look into the var_select + var_project approach and test whether it works out as intended and performs good enough for my application.

In the next release it will be pretty easy to adapt to the "restrict" function then.

I'll let you know when I've done the related testing in the following days.

ellmau commented 2 years ago

Thanks again for the advice. The solution where both operations are chained is working reasonable well on the first tests. (honestly I did not expect that chaining both operations is that well-performing).

Till the feature is implemented in the project, i have implemented a trait for easier use.

/// Implementations of the restrict-operations on BDDs
pub trait BddRestrict {
    /// Provides an implementation of the restrict-operation on BDDs for one variable
    fn var_restrict(&self, variable: biodivine_lib_bdd::BddVariable, value: bool) -> Self;
    /// Provides an implementation of the restrict-operation on a set of variables
    fn restrict(&self, variables: &[(biodivine_lib_bdd::BddVariable, bool)]) -> Self;
}

impl BddRestrict for biodivine_lib_bdd::Bdd {
    fn var_restrict(&self, variable: biodivine_lib_bdd::BddVariable, value: bool) -> Self {
        self.var_select(variable, value).var_project(variable)
    }

    fn restrict(&self, variables: &[(biodivine_lib_bdd::BddVariable, bool)]) -> Self {
        let mut variablelist: Vec<biodivine_lib_bdd::BddVariable> = Vec::new();
        variables
            .iter()
            .for_each(|(var, _val)| variablelist.push(*var));
        self.select(variables).project(&variablelist)
    }
}

PS: I'm not happy about the construction of the helping vector in here, but it is just a quick workaround for now.

daemontus commented 2 years ago

Cool! I'll keep this issue open until it is implemented properly, and I've added this into the 0.4.0 milestone. Thank you for reporting!

daemontus commented 2 years ago

For now, I have merged the "naive" algorithm that combines select and project. This is now released as part of version 0.4.0. Further performance improvements to the method will be tracked as part of #28.