1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Aina Niemetz |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Implementation of base classes for decision strategies used by theory |
14 |
|
* solvers for use in the DecisionManager of TheoryEngine. |
15 |
|
*/ |
16 |
|
|
17 |
|
#include "theory/decision_strategy.h" |
18 |
|
|
19 |
|
#include "theory/rewriter.h" |
20 |
|
|
21 |
|
using namespace cvc5::kind; |
22 |
|
|
23 |
|
namespace cvc5 { |
24 |
|
namespace theory { |
25 |
|
|
26 |
5122 |
DecisionStrategyFmf::DecisionStrategyFmf(context::Context* satContext, |
27 |
5122 |
Valuation valuation) |
28 |
|
: d_valuation(valuation), |
29 |
|
d_has_curr_literal(false, satContext), |
30 |
5122 |
d_curr_literal(0, satContext) |
31 |
|
{ |
32 |
5122 |
} |
33 |
|
|
34 |
4459 |
void DecisionStrategyFmf::initialize() { d_literals.clear(); } |
35 |
|
|
36 |
2036338 |
Node DecisionStrategyFmf::getNextDecisionRequest() |
37 |
|
{ |
38 |
4072676 |
Trace("dec-strategy-debug") |
39 |
2036338 |
<< "Get next decision request " << identify() << "..." << std::endl; |
40 |
2036338 |
if (d_has_curr_literal.get()) |
41 |
|
{ |
42 |
1943120 |
Trace("dec-strategy-debug") << "...already has decision" << std::endl; |
43 |
1943120 |
return Node::null(); |
44 |
|
} |
45 |
|
bool success; |
46 |
93218 |
unsigned curr_lit = d_curr_literal.get(); |
47 |
65168 |
do |
48 |
|
{ |
49 |
158386 |
success = true; |
50 |
|
// get the current literal |
51 |
270927 |
Node lit = getLiteral(curr_lit); |
52 |
316768 |
Trace("dec-strategy-debug") |
53 |
158384 |
<< "...check literal #" << curr_lit << " : " << lit << std::endl; |
54 |
|
// if out of literals, we are done in the current SAT context |
55 |
158384 |
if (!lit.isNull()) |
56 |
|
{ |
57 |
|
bool value; |
58 |
156066 |
if (!d_valuation.hasSatValue(lit, value)) |
59 |
|
{ |
60 |
45843 |
Trace("dec-strategy-debug") << "...not assigned, return." << std::endl; |
61 |
|
// if it has not been decided, return it |
62 |
45843 |
return lit; |
63 |
|
} |
64 |
110223 |
else if (!value) |
65 |
|
{ |
66 |
130336 |
Trace("dec-strategy-debug") |
67 |
65168 |
<< "...assigned false, increment." << std::endl; |
68 |
|
// asserted false, the current literal is incremented |
69 |
65168 |
curr_lit = d_curr_literal.get() + 1; |
70 |
65168 |
d_curr_literal.set(curr_lit); |
71 |
|
// repeat |
72 |
65168 |
success = false; |
73 |
|
} |
74 |
|
else |
75 |
|
{ |
76 |
45055 |
Trace("dec-strategy-debug") << "...already assigned true." << std::endl; |
77 |
|
} |
78 |
|
} |
79 |
|
else |
80 |
|
{ |
81 |
2318 |
Trace("dec-strategy-debug") << "...exhausted literals." << std::endl; |
82 |
|
} |
83 |
112541 |
} while (!success); |
84 |
|
// the current literal has been decided with the right polarity, we are done |
85 |
47373 |
d_has_curr_literal = true; |
86 |
47373 |
return Node::null(); |
87 |
|
} |
88 |
|
|
89 |
1538 |
bool DecisionStrategyFmf::getAssertedLiteralIndex(unsigned& i) const |
90 |
|
{ |
91 |
1538 |
if (d_has_curr_literal.get()) |
92 |
|
{ |
93 |
1538 |
i = d_curr_literal.get(); |
94 |
1538 |
return true; |
95 |
|
} |
96 |
|
return false; |
97 |
|
} |
98 |
|
|
99 |
585 |
Node DecisionStrategyFmf::getAssertedLiteral() |
100 |
|
{ |
101 |
585 |
if (d_has_curr_literal.get()) |
102 |
|
{ |
103 |
585 |
Assert(d_curr_literal.get() < d_literals.size()); |
104 |
585 |
return getLiteral(d_curr_literal.get()); |
105 |
|
} |
106 |
|
return Node::null(); |
107 |
|
} |
108 |
|
|
109 |
168510 |
Node DecisionStrategyFmf::getLiteral(unsigned n) |
110 |
|
{ |
111 |
|
// allocate until the index is valid |
112 |
175993 |
while (n >= d_literals.size()) |
113 |
|
{ |
114 |
14968 |
Node lit = mkLiteral(d_literals.size()); |
115 |
7483 |
if (!lit.isNull()) |
116 |
|
{ |
117 |
6805 |
lit = Rewriter::rewrite(lit); |
118 |
|
} |
119 |
7483 |
d_literals.push_back(lit); |
120 |
|
} |
121 |
161025 |
Node ret = d_literals[n]; |
122 |
161025 |
if (!ret.isNull()) |
123 |
|
{ |
124 |
|
// always ensure it is in the CNF stream |
125 |
158707 |
ret = d_valuation.ensureLiteral(ret); |
126 |
|
} |
127 |
161025 |
return ret; |
128 |
|
} |
129 |
|
|
130 |
2163 |
DecisionStrategySingleton::DecisionStrategySingleton( |
131 |
|
const char* name, |
132 |
|
Node lit, |
133 |
|
context::Context* satContext, |
134 |
2163 |
Valuation valuation) |
135 |
2163 |
: DecisionStrategyFmf(satContext, valuation), d_name(name), d_literal(lit) |
136 |
|
{ |
137 |
2163 |
} |
138 |
|
|
139 |
2754 |
Node DecisionStrategySingleton::mkLiteral(unsigned n) |
140 |
|
{ |
141 |
2754 |
if (n == 0) |
142 |
|
{ |
143 |
2076 |
return d_literal; |
144 |
|
} |
145 |
678 |
return Node::null(); |
146 |
|
} |
147 |
|
|
148 |
|
Node DecisionStrategySingleton::getSingleLiteral() { return d_literal; } |
149 |
|
|
150 |
|
} // namespace theory |
151 |
29502 |
} // namespace cvc5 |