def test_label_conditions(): formula = Ite(a, Ite(b, v1, v2), v3) variables = WMIVariables() w = Weights(formula, variables) labelled_formula, subs = w.label_conditions(formula) assert len(subs) == 2 assert len(get_free_variables(labelled_formula)) == len( get_free_variables(formula))
def test_find_conditions_with_subs(): formula = Ite(a, Ite(LT(x, v1), v2, v3), Ite(b, v1, v2)) variables = WMIVariables() w = Weights(formula, variables) subs = w._find_conditions(formula, { a: Symbol("cond_0"), y: Symbol("cond_1") }) assert len(subs) == 4
def test_evaluate_weight(): formula = Ite(a, v1, v2) variables = WMIVariables() w = Weights(formula, variables) value = w._evaluate_weight(w.weights, [True]) assert value == v1 value = w._evaluate_weight(w.weights, [False]) assert value == v2
def test_evaluate_weight_multiplication(): formula = Ite(a, v1, Times(v2, Ite(b, v1, v3))) variables = WMIVariables() w = Weights(formula, variables) value = w._evaluate_weight(w.weights, [True, True]) assert value == v1 value = w._evaluate_weight(w.weights, [False, True]) assert value == Times(v2, v1) value = w._evaluate_weight(w.weights, [False, False]) assert value == Times(v2, v3)
def test_weight_from_assignment(): formula = Ite(a, v1, Times(v2, Ite(LE(x, v1), v1, v3))) variables = WMIVariables() weight = Weights(formula, variables) var = list(variables.variables.keys()) assignment = {var[0]: True, var[1]: True} result, _ = weight.weight_from_assignment(assignment) assert result == v1 assignment = {var[0]: False, var[1]: True} result, _ = weight.weight_from_assignment(assignment) assert result == Times(v2, v1) assignment = {var[0]: False, var[1]: False} result, _ = weight.weight_from_assignment(assignment) assert result == Times(v2, v3)
def computeMI_batch(self, phis, **options): """Calculates the MI on a batch of queries. Args: phis (list(FNode)): The list of all the queries on which to calculate the MI. **options: - domA: set of pysmt vars encoding the Boolean integration domain (optional) - domX: set of pysmt vars encoding the real integration domain (optional) - mode: The mode to use when calculating MI. Returns: list(real): The list containing the result of each computation. list(int): The list containing the number of integrations for each computation that have been computed. """ # save the old weight old_weights = self.weights old_variables = self.variables # calculate wmi with constant weight new_variables = WMIVariables() new_weights = Weights(Real(1), new_variables) self.weights = new_weights self.variables = new_variables volumes, integrations = self.computeWMI_batch(phis, **options) # restore old weight self.weights = old_weights self.variables = old_variables return volumes, integrations
def computeMI(self, phi, **options): """Calculates the MI on a single query. Args: phi (FNode): The query on which to calculate the MI. **options: - domA: set of pysmt vars encoding the Boolean integration domain (optional) - domX: set of pysmt vars encoding the real integration domain (optional) - mode: The mode to use when calculating MI. Returns: real: The result of the computation. int: The number of integrations that have been computed. """ # save old weight old_weights = self.weights old_variables = self.variables # calculate wmi with constant weight new_variables = WMIVariables() new_weights = Weights(Real(1), new_variables) self.weights = new_weights self.variables = new_variables volume, n_integrations = self.computeWMI(phi, **options) # restore old weight self.weights = old_weights self.variables = old_variables return volume, n_integrations
def test_init(): formula = Ite(GE(x, v1), v1, Times(v2, Ite(b, v1, v3))) variables = WMIVariables() weight = Weights(formula, variables) assert len(get_free_variables(weight.weights)) == len( get_free_variables(formula)) assert len(weight.labels) == 2 assert len(get_free_variables(weight.labelling)) == 2 * 2 assert weight.n_conditions == 2
def __init__(self, chi, weight=Real(1), **options): """Default constructor. Args: chi (FNode): The support of the problem. weight (FNode, optional): The weight of the problem (default: 1). **options: - n_threads: The number of threads to use when computing WMI. """ self.variables = WMIVariables() self.weights = Weights(weight, self.variables) self.chi = And(chi, self.weights.labelling) n_threads = options.get("n_threads") self.integrator = Latte_Integrator(n_threads = n_threads)
def test_find_conditions_count_no_conditions(): formula = Times(v1, Plus(v2, v3)) variables = WMIVariables() w = Weights(formula, variables) subs = w._find_conditions(formula, {}) assert len(subs) == 0
def test_find_conditions_count(): formula = Ite(a, Ite(LT(x, v1), v2, v3), Ite(b, v1, v2)) variables = WMIVariables() w = Weights(formula, variables) subs = w._find_conditions(formula, {}) assert len(subs) == 3
def test_init_not_correct_weight_function(): formula = GE(x, v1) variables = WMIVariables() with pytest.raises(WMIParsingException): weight = Weights(formula, variables)