Esempio n. 1
0
def test_str():
    a = Bool("a")
    b = Bool("b")
    lit_a = Literal(a, negated=False)
    lit_b = Literal(b, negated=False)
    clause = Clause([lit_a, lit_b])

    assert str(clause) == "(a or b)"
Esempio n. 2
0
def test_check_when_unit_returns_unit():
    a = Bool("a")
    b = Bool("b")
    lit_a = Literal(a, negated=False)
    lit_b = Literal(b, negated=False)
    clause = Clause([lit_a, lit_b])
    model = {a: False}

    value = clause.check(model)
    assert value == SatisfyStatus.Unit
def test_check_when_unsatisfiable_returns_false():
    a = Bool("a")
    lit = Literal(a, negated = False)
    lit_bar = Literal(a, negated = True)
    clause = Clause([lit])
    clause_bar = Clause([lit_bar])
    runner = Algorithm3Runner([clause, clause_bar], [a])

    value = runner.check()
    assert value == False
def test_model_when_not_satisfiable_raises():
    a = Bool("a")
    lit = Literal(a, negated = False)
    lit_bar = Literal(a, negated = True)
    clause = Clause([lit])
    clause_bar = Clause([lit_bar])

    runner = Algorithm3Runner([clause, clause_bar], [a])
    runner.check()

    with pytest.raises(RuntimeError):
        runner.model()
Esempio n. 5
0
def test_inspect_with_propagations_returns_propgations():
    a = Bool("a")
    b = Bool("b")
    lit_a = Literal(a, negated=False)
    lit_b = Literal(b, negated=False)
    clause = Clause([lit_a, lit_b])

    watcher = LiteralWatcher([clause])
    assignment = {a: False}
    propagations, _ = watcher.inspect(assignment)

    assert propagations == {b: True}
Esempio n. 6
0
def test_check_when_not_fully_defined_returns_pending():
    a = Bool("a")
    b = Bool("b")
    c = Bool("c")
    lit_a = Literal(a, negated=False)
    lit_b = Literal(b, negated=False)
    lit_c = Literal(c, negated=False)
    clause = Clause([lit_a, lit_b, lit_c])
    model = {a: False}

    value = clause.check(model)
    assert value == SatisfyStatus.Pending
Esempio n. 7
0
def test_check_when_unsatisfiable_returns_unsat():
    a = Bool("a")
    lit = Literal(a, negated=False)
    lit_bar = Literal(a, negated=True)
    clause = Clause([lit])
    clause_bar = Clause([lit_bar])

    s = Solver()
    s.add_clause(clause)
    s.add_clause(clause_bar)

    assert s.check() == "unsat"
def test_model_returns_valid_model():
    a = Bool("a")
    b = Bool("b")
    lit_a = Literal(a, negated=False)
    lit_b = Literal(b, negated=True)
    clause_a = Clause([lit_a])
    clause_b = Clause([lit_b])

    runner = Algorithm1Runner([clause_a, clause_b], [a, b])
    runner.check()

    model = runner.model()
    assert model == {a: True, b: False}
Esempio n. 9
0
def test_model_when_unsatisfiable_raises():
    a = Bool("a")
    lit = Literal(a, negated=False)
    lit_bar = Literal(a, negated=True)
    clause = Clause([lit])
    clause_bar = Clause([lit_bar])

    s = Solver()
    s.add_clause(clause)
    s.add_clause(clause_bar)

    s.check()

    with pytest.raises(RuntimeError):
        s.model()
Esempio n. 10
0
def test_literal_returns_associated_literals():
    a = Bool("a")
    lit = Literal(a, negated=False)
    clause = Clause([lit])

    literal = clause.literal(a)
    assert literal == [lit]
Esempio n. 11
0
def test_check_when_satisfiable_returns_true():
    a = Bool("a")
    lit = Literal(a, negated=False)
    clause = Clause([lit])
    runner = Algorithm1Runner([clause], [a])

    value = runner.check()
    assert value == True
Esempio n. 12
0
def test_check_when_not_satisfiable_returns_unsatisfied():
    a = Bool("a")
    lit = Literal(a, negated=False)
    clause = Clause([lit])
    model = {a: False}

    value = clause.check(model)
    assert value == SatisfyStatus.Unsatisfied
Esempio n. 13
0
def test_model_when_not_checked_raises():
    a = Bool("a")
    lit = Literal(a, negated = False)
    clause = Clause([lit])

    runner = Algorithm3Runner([clause], [a])

    with pytest.raises(RuntimeError):
        runner.model()
Esempio n. 14
0
def test_check_when_satisfiable_returns_sat():
    a = Bool("a")
    lit = Literal(a, negated=False)
    clause = Clause([lit])

    s = Solver()
    s.add_clause(clause)

    assert s.check() == "sat"
Esempio n. 15
0
def test_model_when_satisifiable_returns_model():
    a = Bool("a")
    lit = Literal(a, negated=True)
    clause = Clause([lit])

    s = Solver()
    s.add_clause(clause)

    s.check()
    assert s.model() == {a: False}
Esempio n. 16
0
def test_model_when_not_checked_raises():
    a = Bool("a")
    lit = Literal(a, negated=False)
    clause = Clause([lit])

    s = Solver()
    s.add_clause(clause)

    with pytest.raises(RuntimeError):
        s.model()
Esempio n. 17
0
def test_inspect_with_conflicts_returns_conflicts():
    a = Bool("a")
    lit = Literal(a, negated=False)
    clause = Clause([lit])

    watcher = LiteralWatcher([clause])
    assignment = {a: False}
    _, conflicts = watcher.inspect(assignment)

    assert conflicts == {a: False}
Esempio n. 18
0
def test_model_returns_valid_model():
    a = Bool("a")
    lit = Literal(a, negated = False)
    clause = Clause([lit])

    runner = Algorithm3Runner([clause], [a])
    runner.check()

    model = runner.model()
    assert model == {a: True}
Esempio n. 19
0
def test_check_when_satisfiable_with_many_inputs_returns_true():
    a = Bool("a")
    b = Bool("b")
    c = Bool("c")
    d = Bool("d")
    e = Bool("e")

    lit_a = Literal(a, negated = True)
    lit_b = Literal(b, negated = False)
    lit_c = Literal(c, negated = False)
    lit_d = Literal(d, negated = True)
    lit_e = Literal(e, negated = True)

    clause1 = Clause([lit_a, lit_b])
    clause3 = Clause([lit_c, lit_d])
    clause3 = Clause([lit_e])
    runner = Algorithm3Runner([clause1, clause3, clause3], [a, b, c, d, e])

    value = runner.check()
    assert value == True
Esempio n. 20
0
def test_inspect_when_watched_literal_assigned_true_returns_none():
    bools = [Bool(name) for name in "abcd"]
    lits = [Literal(b, negated=False) for b in bools]
    clause = Clause(lits)
    propagator = UnitPropagator([clause])

    for b in bools:
        if propagator.is_watched(b):
            atom, value = propagator.inspect(bools, {b: True}, b)

            assert atom is None
            assert value is None

            return

    raise RuntimeError("No watched literals")
Esempio n. 21
0
def test_inspect_when_watched_literal_assigned_false_unit_clause_returns_unit_literal(
):
    bools = [Bool(name) for name in "abcd"]
    lits = [Literal(b, negated=False) for b in bools]
    clause = Clause(lits)
    propagator = UnitPropagator([clause])

    unassigned = bools.copy()
    model = {}

    for b in unassigned:
        if propagator.is_watched(b):
            # This will capture the first watched literal, so it is sure to
            # not be a unit clause
            model[b] = False
            atom, value = propagator.inspect(unassigned, model, b)
            unassigned.remove(b)
            break

    for b in unassigned:
        if propagator.is_watched(b):
            # This is the second watched literal assigned false, so it is sure
            # to not be a unit clause
            model[b] = False
            atom, value = propagator.inspect(unassigned, model, b)
            unassigned.remove(b)
            break

    for b in unassigned:
        if propagator.is_watched(b):
            # This is the third watched literal assigned false, so it finally
            # a unit clause
            model[b] = False
            atom, value = propagator.inspect(unassigned, model, b)

            unassigned.remove(b)
            last_atom = unassigned[0]

            assert atom == last_atom
            assert value == True

            return

    raise RuntimeError("No watched literals")
Esempio n. 22
0
def test_insepct_when_watched_literal_assigned_false_not_unit_clause_unwatches_literal(
):
    bools = [Bool(name) for name in "abcd"]
    lits = [Literal(b, negated=False) for b in bools]
    clause = Clause(lits)
    propagator = UnitPropagator([clause])

    for b in bools:
        if propagator.is_watched(b):
            # This will capture the first watched literal, so it is sure to
            # not be a unit clause
            atom, value = propagator.inspect(bools, {b: False}, b)

            watched = propagator.is_watched(b)
            assert not watched

            return

    raise RuntimeError("No watched literals")
Esempio n. 23
0
def test_value_when_negated_sets_value_to_negation():
    a = Bool("a")
    lit = Literal(a, negated = True)
    assert lit.value(False) == True
Esempio n. 24
0
def test_str_when_negated():
    a = Bool("a")
    lit = Literal(a, negated = True)
    assert str(lit) == "a_bar"
Esempio n. 25
0
def test_value_when_not_negated_sets_value():
    a = Bool("a")
    lit = Literal(a, negated = False)
    assert lit.value(False) == False
Esempio n. 26
0
def test_str_when_not_negated():
    a = Bool("a")
    lit = Literal(a, negated = False)
    assert str(lit) == "a"