mirror of
https://github.com/Z3Prover/z3
synced 2025-04-27 02:45:51 +00:00
add assert_and_track to optimize for #2116
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
d1877f58a5
commit
6c464f8aec
5 changed files with 69 additions and 35 deletions
|
@ -7336,6 +7336,36 @@ class Optimize(Z3PPObject):
|
|||
self.add(fml)
|
||||
return self
|
||||
|
||||
def assert_and_track(self, a, p):
|
||||
"""Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
|
||||
|
||||
If `p` is a string, it will be automatically converted into a Boolean constant.
|
||||
|
||||
>>> x = Int('x')
|
||||
>>> p3 = Bool('p3')
|
||||
>>> s = Optimize()
|
||||
>>> s.set(unsat_core=True)
|
||||
>>> s.assert_and_track(x > 0, 'p1')
|
||||
>>> s.assert_and_track(x != 1, 'p2')
|
||||
>>> s.assert_and_track(x < 0, p3)
|
||||
>>> print(s.check())
|
||||
unsat
|
||||
>>> c = s.unsat_core()
|
||||
>>> len(c)
|
||||
2
|
||||
>>> Bool('p1') in c
|
||||
True
|
||||
>>> Bool('p2') in c
|
||||
False
|
||||
>>> p3 in c
|
||||
True
|
||||
"""
|
||||
if isinstance(p, str):
|
||||
p = Bool(p, self.ctx)
|
||||
_z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
|
||||
_z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
|
||||
Z3_optimize_assert_and_track(self.ctx.ref(), self.optimize, a.as_ast(), p.as_ast())
|
||||
|
||||
def add_soft(self, arg, weight = "1", id = None):
|
||||
"""Add soft constraint with optional weight and optional identifier.
|
||||
If no weight is supplied, then the penalty for violating the soft constraint
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue