Пример #1
0
def test_minimize():
    # minimize    x1 + 2 x2 + 3 x3 + 4 x4 + 5 x5
    # subject to  x1 + x2 + x3 + x4 + x5  == 1
    C = Clauses(15)
    C.Require(C.ExactlyOne, range(1, 6))
    sol = C.sat()
    C.unsat = True
    # Unsatisfiable constraints
    assert C.minimize([(k, k) for k in range(1, 6)], sol)[1] == 16
    C.unsat = False
    sol, sval = C.minimize([(k, k) for k in range(1, 6)], sol)
    assert sval == 1
    C.Require(C.ExactlyOne, range(6, 11))
    # Supply an initial vector that is too short, forcing recalculation
    sol, sval = C.minimize([(k, k) for k in range(6, 11)], sol)
    assert sval == 6
    C.Require(C.ExactlyOne, range(11, 16))
    # Don't supply an initial vector
    sol, sval = C.minimize([(k, k) for k in range(11, 16)])
    assert sval == 11
Пример #2
0
def test_minimize():
    # minimize    x1 + 2 x2 + 3 x3 + 4 x4 + 5 x5
    # subject to  x1 + x2 + x3 + x4 + x5  == 1
    C = Clauses(15)
    C.Require(C.ExactlyOne, range(1,6))
    sol = C.sat()
    C.unsat = True
    # Unsatisfiable constraints
    assert C.minimize([(k,k) for k in range(1,6)], sol)[1] == 16
    C.unsat = False
    sol, sval = C.minimize([(k,k) for k in range(1,6)], sol)
    assert sval == 1
    C.Require(C.ExactlyOne, range(6,11))
    # Supply an initial vector that is too short, forcing recalculation
    sol, sval = C.minimize([(k,k) for k in range(6,11)], sol)
    assert sval == 6
    C.Require(C.ExactlyOne, range(11,16))
    # Don't supply an initial vector
    sol, sval = C.minimize([(k,k) for k in range(11,16)])
    assert sval == 11
Пример #3
0
def test_sat():
    C = Clauses()
    C.new_var('x1')
    C.new_var('x2')
    assert C.sat() is not None
    assert C.sat([]) is not None
    assert C.sat([()]) is None
    assert C.sat([(False, )]) is None
    assert C.sat([(True, ), ()]) is None
    assert C.sat([(True, False, -1)]) is not None
    assert C.sat([(+1, False), (+2, ), (True, )], names=True) == {'x1', 'x2'}
    assert C.sat([(-1, False), (True, ), (+2, )], names=True) == {'x2'}
    assert C.sat([(True, ), (-1, ), (-2, False)], names=True) == set()
    assert C.sat([(+1, ), (-1, False)], names=True) is None
    C.unsat = True
    assert C.sat() is None
    assert C.sat([]) is None
    assert C.sat([(True, )]) is None
    assert len(Clauses(10).sat([[1]])) == 10
Пример #4
0
def test_sat():
    C = Clauses()
    C.new_var('x1')
    C.new_var('x2')
    assert C.sat() is not None
    assert C.sat([]) is not None
    assert C.sat([()]) is None
    assert C.sat([(False,)]) is None
    assert C.sat([(True,),()]) is None
    assert C.sat([(True,False,-1)]) is not None
    assert C.sat([(+1,False),(+2,),(True,)], names=True) == {'x1','x2'}
    assert C.sat([(-1,False),(True,),(+2,)], names=True) == {'x2'}
    assert C.sat([(True,),(-1,),(-2,False)], names=True) == set()
    assert C.sat([(+1,),(-1,False)], names=True) is None
    C.unsat = True
    assert C.sat() is None
    assert C.sat([]) is None
    assert C.sat([(True,)]) is None
    assert len(Clauses(10).sat([[1]])) == 10