예제 #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)"
예제 #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
예제 #3
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}
예제 #4
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
예제 #5
0
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}
예제 #6
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]
예제 #7
0
def test_check_when_satisfiable_returns_true():
    a = Bool("a")
    lit = Literal(a, negated = False)
    clause = Clause([lit])
    runner = Algorithm3Runner([clause], [a])

    value = runner.check()
    assert value == True
예제 #8
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
예제 #9
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()
예제 #10
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"
예제 #11
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}
예제 #12
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()
예제 #13
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}
예제 #14
0
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 = Algorithm1Runner([clause, clause_bar], [a])

    value = runner.check()
    assert value == False
예제 #15
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}
예제 #16
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
예제 #17
0
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()
예제 #18
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"
예제 #19
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()
예제 #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")
예제 #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")
예제 #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")
예제 #23
0
def test_choose_when_given_same_depth_returns_same_atom():
    atoms = [Bool("a"), Bool("b"), Bool("c")]
    chooser = AtomChooser(atoms)
    chosen = chooser.choose(0)
    next_chosen = chooser.choose(0)
    assert next_chosen == chosen
예제 #24
0
def test_choose_when_no_atoms_left_returns_none():
    atoms = [Bool("a")]
    chooser = AtomChooser(atoms)
    chooser.choose(0)
    chosen = chooser.choose(1)
    assert chosen is None
예제 #25
0
def test_bool_name_properly_assigned():
    a = Bool("a")

    assert a.name == "a"
예제 #26
0
def test_choose_returns_atom():
    atoms = [Bool("a"), Bool("b"), Bool("c")]
    chooser = AtomChooser(atoms)
    chosen = chooser.choose(0)
    assert chosen in atoms
예제 #27
0
def test_str_when_negated():
    a = Bool("a")
    lit = Literal(a, negated = True)
    assert str(lit) == "a_bar"
예제 #28
0
def test_str_when_not_negated():
    a = Bool("a")
    lit = Literal(a, negated = False)
    assert str(lit) == "a"
예제 #29
0
def test_value_when_negated_sets_value_to_negation():
    a = Bool("a")
    lit = Literal(a, negated = True)
    assert lit.value(False) == True
예제 #30
0
def test_value_when_not_negated_sets_value():
    a = Bool("a")
    lit = Literal(a, negated = False)
    assert lit.value(False) == False