Ejemplo n.º 1
0
def test_cyclic_assignment():
    chi = And(GE(x, Real(0)), LE(x, Real(1)), Equals(x, y), Equals(y, x))
    wmi = WMI(chi)
    
    with pytest.raises(WMIParsingException) as ex:
        result, _ = wmi.computeMI(phi)
    assert ex.value.code == WMIParsingException.CYCLIC_ASSIGNMENT_IN_ALIASES
Ejemplo n.º 2
0
def test_invalid_mode():
    chi = Bool(True)
    wmi = WMI(chi)
    
    with pytest.raises(WMIRuntimeException) as ex:
        result_allsmt, _ = wmi.computeMI(phi, mode="mode")
    assert ex.value.code == WMIRuntimeException.INVALID_MODE
Ejemplo n.º 3
0
def test_conversion_pysmt_to_sympy():
    chi = LE(Min(Real(5), Real(2)), x)
    wmi = WMI(chi)
    
    with pytest.raises(WMIParsingException) as ex:
        result, _ = wmi.computeMI(phi)
    assert ex.value.code == WMIParsingException.CANNOT_CONVERT_PYSMT_FORMULA_TO_SYMPY
Ejemplo n.º 4
0
def test_invalid_weight_function():
    chi = And(GE(x, Real(0)), LE(x, Real(1)))
    w = GE(x, Real(2))
    
    with pytest.raises(WMIParsingException) as ex:
        wmi = WMI(chi, w)
    assert ex.value.code == WMIParsingException.INVALID_WEIGHT_FUNCTION
Ejemplo n.º 5
0
def test_not_correct_alias():
    chi = And(GE(x, Real(0)), LE(x, Real(1)), Equals(Plus(x, Real(3)), Plus(y, Real(2))))
    wmi = WMI(chi)
    
    with pytest.raises(WMIParsingException) as ex:
        result, _ = wmi.computeMI(phi)
    assert ex.value.code == WMIParsingException.MALFORMED_ALIAS_EXPRESSION
Ejemplo n.º 6
0
def test_double_assignments_same_variable():
    chi = And(GE(x, Real(0)), LE(x, Real(1)), GE(y, Real(0)), LE(y, Real(1)), Equals(z, Plus(x, Real(3))), Equals(z, Plus(y, Real(2))))
    wmi = WMI(chi)
    
    with pytest.raises(WMIParsingException) as ex:
        result_allsmt, _ = wmi.computeMI(phi, mode=WMI.MODE_ALLSMT)
    assert ex.value.code == WMIParsingException.MULTIPLE_ASSIGNMENT_SAME_ALIAS
Ejemplo n.º 7
0
def test_wrong_domX():
    chi = And(GE(x, Real(0)), LE(x, Real(1)), a)
    wmi = WMI(chi)
    domX = set()
    
    with pytest.raises(WMIRuntimeException) as ex:
        result_allsmt, _ = wmi.computeMI(phi, domX=domX)
    assert ex.value.code == WMIRuntimeException.DOMAIN_OF_INTEGRATION_MISMATCH
Ejemplo n.º 8
0
 def compute_volume_pa(domain, support, weight, convex_backend=None):
     # noinspection PyCallingNonCallable
     solver = WMI(support, weight, convex_backend=convex_backend)
     return solver.computeWMI(
         Bool(True),
         mode=WMI.MODE_PA,
         domA=set(domain.get_bool_symbols()),
         domX=set(domain.get_real_symbols()),
     )[0]
Ejemplo n.º 9
0
def test_booleans_constant_weight():
    chi = And(Iff(a, GE(x, Real(0))), GE(x, Real(-2)), LE(x, Real(1)))

    wmi = WMI(chi)

    result_bc, _ = wmi.computeMI(phi, mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeMI(phi, mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeMI(phi, mode=WMI.MODE_PA)
    assert result_bc == 3
    assert result_allsmt == 3
    assert result_pa == 3
Ejemplo n.º 10
0
def test_aliases_leads_to_not_sat():
    chi = And(GE(x, Real(0)), LE(x, Real(2)), Equals(y, x),
              LE(x - y, Real(-2)))

    wmi = WMI(chi)

    result_bc, _ = wmi.computeMI(phi, mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeMI(phi, mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeMI(phi, mode=WMI.MODE_PA)
    assert result_bc == 0
    assert result_allsmt == 0
    assert result_pa == 0
Ejemplo n.º 11
0
def test_setting_domA():
    chi = And(GE(x, Real(0)), LE(x, Real(2)), a)

    wmi = WMI(chi)
    result_bc, _ = wmi.computeMI(phi, mode=WMI.MODE_BC, domA=set([a, b]))
    result_allsmt, _ = wmi.computeMI(phi,
                                     mode=WMI.MODE_ALLSMT,
                                     domA=set([a, b]))
    result_pa, _ = wmi.computeMI(phi, mode=WMI.MODE_PA, domA=set([a, b]))
    assert result_bc == 2 * 2
    assert result_allsmt == 2 * 2
    assert result_pa == 2 * 2
Ejemplo n.º 12
0
def test_double_assignment_same_variable_no_theory_consistent():
    chi = And(GE(x, Real(0)), Equals(y, Plus(x, Real(-2))),
              Equals(y, Plus(x, Real(5))), LE(y, Real(4)))

    wmi = WMI(chi)

    result_bc, _ = wmi.computeMI(phi, mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeMI(phi, mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeMI(phi, mode=WMI.MODE_PA)
    assert result_bc == 0
    assert result_allsmt == 0
    assert result_pa == 0
Ejemplo n.º 13
0
def test_no_booleans_condition_weight():
    chi = And(GE(x, Real(0)), LE(x, Real(1)))

    w = Ite(LE(x, Real(0.5)), x, Times(Real(-1), x))

    wmi = WMI(chi, w)

    result_bc, _ = wmi.computeWMI(phi, mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeWMI(phi, mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeWMI(phi, mode=WMI.MODE_PA)
    assert result_bc == -0.25
    assert result_allsmt == -0.25
    assert result_pa == -0.25
Ejemplo n.º 14
0
def test_aliases():
    chi = And(GE(x, Real(0)), Equals(y, Plus(x, Real(-2))), LE(y, Real(4)))

    w = y

    wmi = WMI(chi, w)

    result_bc, _ = wmi.computeWMI(phi, mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeWMI(phi, mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeWMI(phi, mode=WMI.MODE_PA)
    assert result_bc == 6
    assert result_allsmt == 6
    assert result_pa == 6
Ejemplo n.º 15
0
def test_not_boolean_satisfiable():
    chi = And(Iff(a, GE(x, Real(0))), GE(x, Real(-1)), LE(x, Real(1)), b,
              Not(b))

    w = Ite(b, x, Ite(a, Times(Real(-1), x), Times(Real(2), x)))

    wmi = WMI(chi, w)

    result_bc, _ = wmi.computeWMI(phi, mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeWMI(phi, mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeWMI(phi, mode=WMI.MODE_PA)
    assert result_bc == 0
    assert result_allsmt == 0
    assert result_pa == 0
Ejemplo n.º 16
0
def test_batch_of_query_constant_weight():
    chi = And(GE(x, Real(0)), LE(x, Real(4)))

    phi1 = LE(x, Real(2))
    phi2 = GE(x, Real(2))

    wmi = WMI(chi)

    result_bc, _ = wmi.computeMI_batch([phi1, phi2], mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeMI_batch([phi1, phi2], mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeMI_batch([phi1, phi2], mode=WMI.MODE_PA)
    assert result_bc[0] == 2
    assert result_bc[1] == 2
    assert result_allsmt[0] == 2
    assert result_allsmt[1] == 2
    assert result_pa[0] == 2
    assert result_pa[1] == 2
Ejemplo n.º 17
0
def test_moltiplication_in_weight():
    chi = And(
        Iff(a, GE(x, Real(0))),
        Or(And(GE(x, Real(-3)), LE(x, Real(-2))),
           And(GE(x, Real(-1)), LE(x, Real(1))),
           And(GE(x, Real(2)), LE(x, Real(3)))))

    w = Times(Ite(a, x, Times(x, Real(-1))), x)

    wmi = WMI(chi, w)

    result_bc, _ = wmi.computeWMI(phi, mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeWMI(phi, mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeWMI(phi, mode=WMI.MODE_PA)
    assert result_bc == 0
    assert result_allsmt == 0
    assert result_pa == 0
Ejemplo n.º 18
0
def test_boolean_and_not_simplify():
    chi = And(
        Iff(a, GE(x, Real(0))),
        Or(And(GE(x, Real(-3)), LE(x, Real(-2))),
           And(GE(x, Real(-1)), LE(x, Real(1))),
           And(GE(x, Real(2)), LE(x, Real(3)))))

    w = Ite(LE(x, Real(-0.5)), x, Ite(a, Times(Real(-1), x), Times(Real(2),
                                                                   x)))

    wmi = WMI(chi, w)

    result_bc, _ = wmi.computeWMI(phi, mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeWMI(phi, mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeWMI(phi, mode=WMI.MODE_PA)
    assert result_bc == -6.125
    assert result_allsmt == -6.125
    assert result_pa == -6.125
Ejemplo n.º 19
0
def test_reserved_variables_name():
    a = Symbol("wmi_1_a", BOOL)
    b = Symbol("cond_a", BOOL)
    x = Symbol("query_45", REAL)
    y = FreshSymbol(REAL)

    chi = And(GE(x, Real(0)), LE(x, Real(2)), GE(y, Real(2)), LE(y, Real(4)),
              Iff(a, LE(x, Real(1))), Iff(b, LE(y, Real(3))))

    w = Ite(a, x, y)

    wmi = WMI(chi, w)

    result_bc, abc = wmi.computeWMI(phi, mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeWMI(phi, mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeWMI(phi, mode=WMI.MODE_PA)
    assert result_bc == 7
    assert result_allsmt == 7
    assert result_pa == 7
Ejemplo n.º 20
0
def test_batch_of_query():
    chi = And(GE(x, Real(0)), LE(x, Real(2)))

    phi1 = LE(x, Real(1))
    phi2 = GE(x, Real(1))

    w = x

    wmi = WMI(chi, w)

    result_bc, _ = wmi.computeWMI_batch([phi1, phi2], mode=WMI.MODE_BC)
    result_allsmt, _ = wmi.computeWMI_batch([phi1, phi2], mode=WMI.MODE_ALLSMT)
    result_pa, _ = wmi.computeWMI_batch([phi1, phi2], mode=WMI.MODE_PA)
    assert result_bc[0] == 0.5
    assert result_bc[1] == 1.5
    assert result_allsmt[0] == 0.5
    assert result_allsmt[1] == 1.5
    assert result_pa[0] == 0.5
    assert result_pa[1] == 1.5
Ejemplo n.º 21
0
def run_pa(density, n_bins, log_path):
    formula = density.support
    weight = density.weight
    CACHE = False

    #log_path = join(output_folder, f"{name}-gt-PA.json")
    if not isfile(log_path):
        print("Running WMI-PA")

        wmipa = WMI(density.support, density.weight)
        Z_pa, _ = wmipa.computeWMI(Bool(True), mode=WMI.MODE_PA, cache=CACHE)

        gt_marginals = {}
        for xvar in density.domain.get_real_symbols():
            x = xvar.symbol_name()
            gt_marginals[x] = []
            low, up = density.domain.var_domains[x]
            slices = [(i / n_bins) * (up - low) + low
                      for i in range(0, n_bins + 1)]
            for i in range(len(slices) - 1):
                l, u = slices[i], slices[i + 1]
                gt_q = And(LE(Real(l), xvar), LE(xvar, Real(u)))
                gt_vol, _ = wmipa.computeWMI(gt_q,
                                             mode=WMI.MODE_PA,
                                             cache=CACHE)
                gt_marginals[x].append((l, u, gt_vol / Z_pa))

        with open(log_path, 'w') as log_file:
            json.dump(gt_marginals, log_file, indent=2)

    else:
        print(f"Found {log_path}")
        with open(log_path, 'r') as log_file:
            gt_marginals = json.load(log_file)

    return gt_marginals
Ejemplo n.º 22
0
            logging.info("About to start mpwmi solver")
            mpmi = MP2WMI(density.support,
                          density.weight,
                          smt_solver=args.smt_solver,
                          n_processes=nproc)
            logging.info("Solver inited")
            Z, _ = mpmi.compute_volumes(cache=True)
            logging.info("Volume computed")
            t2 = time.perf_counter()

        elif args.solver == "pa":
            logging.info("using pa")

            # pa
            t1 = time.perf_counter()
            wmipa = WMI(density.support, density.weight)
            Z, _ = wmipa.computeWMI(Bool(True), mode=WMI.MODE_PA)
            t2 = time.perf_counter()

        elif args.solver == "xsdd":
            logging.info("using xsdd")

            # xsdd
            t1 = time.perf_counter()
            xsdd = XsddEngine(density.domain,
                              density.support,
                              density.weight,
                              factorized=False,
                              algebra=PyXaddAlgebra(),
                              ordered=False)
            Z = xsdd.compute_volume(add_bounds=False)
Ejemplo n.º 23
0
from pysmt.shortcuts import *
from pysmt.typing import REAL, BOOL
from wmipa import WMI

# variables definition
a = Symbol("A", BOOL)
x1 = Symbol("x1", REAL)
x2 = Symbol("x2", REAL)

# formula definition
phi = Bool(True)

print("Formula:", serialize(phi))

# weight function definition
w = Plus(Ite(GE(x1, Real(0)), Pow(x1, Real(3)), Times(Real(-2), x1)),
         Ite(a, Times(Real(3), x2), Times(Real(-1), Pow(x2, Real(5)))))

chi = And(LE(Real(-1), x1), LT(x1, Real(1)), LE(Real(-1), x2), LT(x2, Real(1)),
          Iff(a, GE(x2, Real(0))))

print("Weight function:", serialize(w))
print("Support:", serialize(chi))

wmi = WMI(chi, w)
print()
for mode in [WMI.MODE_ALLSMT, WMI.MODE_PA]:
    result, n_integrations = wmi.computeWMI(phi, mode=mode)
    print("WMI with mode {} \t result = {}, \t # integrations = {}".format(
        mode, result, n_integrations))
Ejemplo n.º 24
0
    w = Ite(LE(x, y), Real(2), Real(1))
    queries = [LE(w, Real(0.5)),
               LE(x, Real(0.5)),
               LE(y, Real(0.5)),
               LE(z, Real(0.5)),
               LE(Plus(w, x), Real(0.5)),
               LE(Plus(x, y), Real(0.5)),
               LE(Plus(y, z), Real(0.5))]
    """

    f = And(LE(Real(1), x), LE(x, Real(2)), LE(Real(1), y), LE(y, Real(2)),
            LE(Real(1), z), LE(z, Real(2)))

    w = Ite(LE(x, y), Plus(x, y), Real(1))

    queries = []  #[LE(x, Real(3/2)), LE(y, Real(3/2)), LE(x, y)]

    mpwmi = MP2WMI(f, w, n_processes=3)

    Z_mp, pq_mp = mpwmi.compute_volumes(queries=queries,
                                        cache=bool(int(argv[1])))

    wmipa = WMI(f, w)
    Z_pa, _ = wmipa.computeWMI(Bool(True), mode=WMI.MODE_PA)
    print("==================================================")
    print(f"Z\t\t\t\t{Z_mp}\t{Z_pa}")
    for i, q in enumerate(queries):
        pq_pa, _ = wmipa.computeWMI(q, mode=WMI.MODE_PA)
        print(f"{q}\t\t\t{pq_mp[i]}\t{pq_pa}")