forked from pysmt/pysmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportfolio.py
76 lines (65 loc) · 2.45 KB
/
portfolio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Portfolio solving makes it possible to run multiple solvers in
# parallel. As soon as the first solver completes, all other solvers
# are stopped. Current support for portfolio is mostly focused on
# one-shot calls, with only functional incrementality: the interface
# looks incremental, but internally there is no guarantee of re-using
# the solver state.
#
from pysmt.shortcuts import Symbol, Implies, TRUE, FALSE, Not
x, y = Symbol("x"), Symbol("y")
f = Implies(x, y)
# The first example shows how to use multiple solvers in the with the
# is_sat shortcut
#
from pysmt.shortcuts import is_sat
# We enable logging to see what is going on behind the scenes:
import logging
_info = logging.getLogger(__name__).info
logging.basicConfig(level=logging.DEBUG)
# A solver set is an iterable of solver names or pairs of
# solvers+options (See next example)
_info("Example 1: is_sat")
solvers_set = ["z3", "yices", "msat"]
res = is_sat(f, portfolio=solvers_set)
assert res is True
# Behind the scenes, pySMT launched 3 processes and solved the
# expression in parallel.
#
# The is_sat shortcut is useful for prototyping and exploration, but
# we typically need more control over the solver. The Portfolio class
# behaves as a solver and as such implements most of the methods of a
# regular solver.
from pysmt.shortcuts import Portfolio
from pysmt.logics import QF_UFLIRA
# The options given to the Portfolio will be passed to all solvers, in
# particular, we are enabling incrementality and model generation.
_info("Example 2: Portfolio()")
with Portfolio(solvers_set,
logic=QF_UFLIRA,
incremental=True,
generate_models=True) as s:
s.add_assertion(f)
s.push()
s.add_assertion(x)
res = s.solve()
v_y = s.get_value(y)
assert v_y is TRUE()
s.pop()
s.add_assertion(Not(y))
res = s.solve()
v_x = s.get_value(x)
assert v_x is FALSE()
# Portfolio can also be useful to tweak heuristics of the solvers.
# For supported solver options, please refer to the solver
# documentation. This is an area of pySMT that could use additional
# feedback and help!
_info("Example 3: Portfolio w options")
with Portfolio([("msat", {"random_seed": 1}),
("msat", {"random_seed": 17}),
("msat", {"random_seed": 42}),
"cvc4", "yices"],
logic=QF_UFLIRA,
incremental=False,
generate_models=False) as s:
res = s.is_sat(f)
assert res is True