Beispiel #1
0
def simpl(f):
    l = Then(qe, simplify)(f)[0]
    if len(l) == 0:
        return True
    elif len(l) == 1:
        return l[0]
    else:
        return apply(And, l)
Beispiel #2
0
 def solve(self, actions, initial_state, goal_state):
     """Solves the planning problem given the elements of the problem
     """
     # encode the problem
     for length in range(0, self.max_length):
         if self.simplify:
             # s = Then('sat-preprocess', 'psmt').solver()
             # s = With('psmt').solver()
             # s = Then('aig', 'elim-and','psmt').solver()
             # s = Then('aig', 'psmt').solver()
             # s = Then('simplify', 'propagate-values', 'ctx-simplify').solver()
             s = Then('simplify', 'propagate-values', 'psmt').solver()
         else:
             s = Solver()
         self.props.clear()
         self.action_map.clear()
         print("Encoding domain with length {0}".format(length))
         self.encode_formula(s, actions, initial_state, goal_state, length)
         if self.verbose: print(s.to_smt2())
         # print(s)
         if s.check() == sat:
             if self.verbose:
                 print("Model found with length {0}".format(length))
             # print(s.model())
             return self.extract_plan(s.model(), length)
         else:
             if self.verbose:
                 print("No model found with length {0}".format(length))
     return None
Beispiel #3
0
    def test_tactics_z3(self):
        from z3 import Tactic, Then
        from pysmt.shortcuts import Iff

        my_tactic = Then(Tactic('simplify'), Tactic('propagate-values'),
                         Tactic('elim-uncnstr'))

        for (f, validity, satisfiability, logic) in get_example_formulae():
            if not logic.theory.linear: continue
            if not logic.quantifier_free: continue
            if logic.theory.bit_vectors: continue
            s = Solver(name='z3')
            z3_f = s.converter.convert(f)
            simp_z3_f = my_tactic(z3_f)
            simp_f = s.converter.back(simp_z3_f.as_expr())
            v = is_valid(simp_f)
            s = is_sat(simp_f)
            self.assertEqual(v, validity, (f, simp_f))
            self.assertEqual(s, satisfiability, (f, simp_f))
Beispiel #4
0
f = Or(And(-i + x == 0, -i + n - 1 == 0, i - 2 >= 0),
       And(-n + x + 2 == 0, -i + n - 3 >= 0, i >= 0),
       And(x >= 0, n - x - 3 >= 0, i - x - 1 >= 0),
       And(-i + x == 0, -i + n - 2 == 0, i - 1 >= 0),
       And(-n + x + 1 == 0, -i + n - 2 >= 0, n - 3 >= 0, i >= 0),
       And(-i + x - 1 >= 0, n - x - 3 >= 0, i >= 0),
       And(-n + x + 1 == 0, n - 3 >= 0, i - n >= 0),
       And(-i + x == 0, -i + n - 3 >= 0, i >= 0))
g = And(n > 2, ForAll(x, Implies(And(x >= 0, x < n), f)))
h = apply(And, qe(g)[0])

h2 = And(n > 2, i >= 0, i <= n - 2)
#h2neg = Or(n<=2, i<=-1, i>=n-1)

print qe(ForAll(x, Implies(h, h2)))
print qe(ForAll(x, Implies(h2, h)))

from z3 import solve
solve(And(h, Not(h2)))
solve(And(h2, Not(h)))

simplify = Repeat(Then('nnf', 'ctx-solver-simplify'))
h3 = apply(And, simplify(h)[0])

simplify = Then(
    Tactic('simplify'), Tactic('propagate-values'),
    ParThen(Repeat(OrElse(Tactic('split-clause'), Tactic('skip'))),
            Tactic('propagate-ineqs')))

#print tactic(ForAll(x,Or(h2neg,h)))
def writeQDIMACS(filename, constraint, quantifiers, bitmap=None):
    # filename: String
    # constraints: list of BV constraints
    # quantifiers: list of tuples (['a','e','max','count'], list of vars)

    assert_consistent_quantifiers(quantifiers)

    log('Bit blasting')
    bitmap = {}

    for q in quantifiers:
        bitvecs = filter(is_bv, q[1])
        localBitmap, localBitmapConstraints = create_bitmap(bitvecs)
        bitmap.update(localBitmap)
        constraint = And(localBitmapConstraints, constraint)
        newQuantifiedVars = filter(lambda v: not is_bv(v), q[1])
        for (_, boolvar) in localBitmap.iteritems():
            newQuantifiedVars.append(boolvar)
        q[1] = newQuantifiedVars

    g = Goal()
    g.add(constraint)
    matrix = []
    t = Then('simplify', 'bit-blast', 'tseitin-cnf')
    subgoal = t(g)
    # print(subgoal[0][0].children()[1].children()[0] == bitmap())
    assert len(subgoal) == 1

    # print('Printing quantifier')
    # print(quantifiers)
    # print('Printing goal')
    # print(g)
    # exit()

    max_var = 0
    var_mapping = {}  # maps to qdimacs variables

    textFile = open(filename, "w")

    log('Creating and writing symbol table')
    textFile.write('c Symbol table for bitvectors\n')
    symbol_table = []
    for ((bv, i), boolvar) in bitmap.iteritems():
        max_var += 1
        var_mapping[boolvar.get_id()] = max_var
        # symbol_table.append('c ' + str(boolvar) + ' --> ' + str(max_var))
        textFile.write('c ' + str(boolvar) + ' --> ' + str(max_var) + '\n')

    log('Reserving variable names for quantified variables')
    for i, q in enumerate(quantifiers):
        for var in q[1]:
            if var.get_id() not in var_mapping:
                max_var += 1
                var_mapping[var.get_id()] = max_var

    # minTseitin = max_var + 1
    Tseitin_vars = []

    log('Generating clauses ... (this may take a while)')
    clause_num = 0
    for c in subgoal[0]:
        clause_num += 1
        if clause_num % 10000 == 0:
            log('  {} clauses'.format(clause_num))
        if is_or(c):
            clause = ''
            for l in c.children():  # literals
                max_var, lit_str = encode_literal(var_mapping, Tseitin_vars,
                                                  max_var, l)
                clause += lit_str
            matrix.append(clause)
        elif is_const(c) or is_not(c):
            max_var, lit_str = encode_literal(var_mapping, Tseitin_vars,
                                              max_var, c)
            matrix.append(lit_str)
        else:
            log('Error: Unknown element ' + str(c))
            assert false
    matrix.append('')
    log('  Generated ' + str(clause_num) + ' clauses')

    log('Writing header')
    textFile.write('p cnf {} {}\n'.format(max_var, clause_num))

    # Extending quantifiers by innermost existential if necessary
    if quantifiers[-1][0] == 'a' and len(
            Tseitin_vars) > 0:  #  max_var + 1 - minTseitin > 0
        quantifiers.append(['e', []])  # empty existential

    log('Writing quantifiers')
    for i, q in enumerate(quantifiers):
        textFile.write(q[0])
        for v in q[1]:
            # try:
            v_id = v.get_id()
            textFile.write(' ' + str(var_mapping[v_id]))
            # except Exception as ex:
            #     log(' Error when writing var {} to file ({})'.format(str(v), str(ex)))
            #
            #     template = "An exception of type {0} occurred. Arguments:\n{1!r}"
            #     message = template.format(type(ex).__name__, ex.args)
            #     print message
            #
            #     exit()

        if i == len(quantifiers) - 1:
            log('Adding {} Tseitin variables'.format(len(Tseitin_vars)))
            for varID in Tseitin_vars:
                textFile.write(' ' + str(varID))

            # for varID in range(minTseitin,max_var+1):
#                 # log('Adding var {}'.format(varID))
#                 textFile.write(' '+str(varID))
#                 # quantifiers[-1][1].append(varID)
# log('  OK (added {} Tseitin vars)'.format(len(range(minTseitin,max_var+1))))

        textFile.write(' 0\n')

    log('Writing clauses')
    textFile.write('0\n'.join(matrix))
    textFile.close()

    return var_mapping
Beispiel #6
0
def simpl(f):
    return apply(And, Then(qe, simplify)(f)[0])
Beispiel #7
0
         z0r, z2l, z2r),
     And(-k + z == 0, -i + x >= 0, -k + n - 1 >= 0, -x + y - 1 >= 0,
         k - y - 1 >= 0, i - 1 >= 0)),
    (And(Not(x0l), x0r, x2l, Not(x2r), Not(y0l), y0r, y2l, Not(y2r), Not(z0l),
         z0r, z2l, Not(z2r)),
     And(-i + x >= 0, -k + n - 1 >= 0, -x + y - 1 >= 0, -y + z - 1 >= 0,
         k - z - 1 >= 0, i - 1 >= 0)),
    (And(Not(x0l), x0r, x2l, Not(x2r), Not(y0l), y0r, y2l, Not(y2r), Not(z0l),
         z0r, Not(z2l), z2r),
     And(-i + x >= 0, -k + z - 1 >= 0, -x + y - 1 >= 0, n - z - 1 >= 0,
         k - y - 1 >= 0, i - 1 >= 0)),
    (And(Not(x0l), x0r, x2l, Not(x2r), Not(y0l), y0r, Not(y2l), y2r, Not(z0l),
         z0r, Not(z2l), z2r),
     And(-i + x >= 0, -k + y - 1 >= 0, -y + z - 1 >= 0, n - z - 1 >= 0,
         k - x - 1 >= 0, i - 1 >= 0)),
    (And(Not(x0l), x0r, Not(x2l), x2r, Not(y0l), y0r, Not(y2l), y2r, Not(z0l),
         z0r, Not(z2l), z2r),
     And(-i + k + 1 >= 0, -k + x - 1 >= 0, -x + y - 1 >= 0, -y + z - 1 >= 0,
         n - z - 1 >= 0, i - 1 >= 0)),
    (And(Not(x0l), Not(x0r), Not(x2l), Not(x2r), Not(y0l), Not(y0r), Not(y2l),
         Not(y2r), Not(z0l), Not(z0r), Not(z2l), Not(z2r)),
     And(i == 0, -x + y - 1 >= 0, -y + z - 1 >= 0, x >= 0, n - z - 1 >= 0))
]

qe = Tactic('qe')
simplify = Repeat(Then('nnf', 'ctx-solver-simplify'))


def simpl(f):
    return apply(And, Then(qe, simplify)(f)[0])
Beispiel #8
0
        n - q >= 0),
    And(-q + x == 0, -p + y - 1 >= 0, q - y - 2 >= 0, p >= 0, n - q >= 0),
    And(-q + y + 1 == 0, -p + q - 2 >= 0, -q + x - 1 >= 0, x - 4 >= 0, p >= 0,
        n - q >= 0, n - 3 >= 0),
    And(-p + y - 1 >= 0, -q + x - 1 >= 0, q - y - 1 >= 0, p >= 0, n - q >= 0,
        n - y - 2 >= 0),
    And(-q + x == 0, p + 1 == 0, y >= 0, q - y - 2 >= 0, n - q - 5 >= 0),
    And(p + 1 == 0, -q + x - 1 >= 0, y >= 0, q - y - 1 >= 0,
        n + q - 2 * x - 4 >= 0))

middle2 = ForAll(x, And(y >= 0, y < n, Implies(And(0 <= x, x <= y), middle)))

qe = Tactic('qe')
# simplify=Repeat(Then('nnf','ctx-solver-simplify'))
simplify = Repeat(
    Then(OrElse('split-clause', 'nnf'), 'propagate-ineqs',
         'ctx-solver-simplify'))


def simpl(f):
    l = Then(qe, simplify)(f)[0]
    if len(l) == 0:
        return True
    elif len(l) == 1:
        return l[0]
    else:
        return apply(And, l)


middle3 = And(middle2, 0 <= y, y < n, n >= 2)
middle4 = And(p >= -1, p < y, y < q, q <= n, n >= 2)
middle5 = simpl(middle3)