1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Tim King, Dejan Jovanovic, Andres Noetzli |
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 |
|
* [[ Add one-line brief description here ]] |
14 |
|
* |
15 |
|
* [[ Add lengthier description here ]] |
16 |
|
* \todo document this file |
17 |
|
*/ |
18 |
|
|
19 |
|
#include <vector> |
20 |
|
|
21 |
|
#include "base/output.h" |
22 |
|
#include "expr/node_algorithm.h" |
23 |
|
#include "options/arith_options.h" |
24 |
|
#include "smt/smt_statistics_registry.h" |
25 |
|
#include "theory/arith/arith_static_learner.h" |
26 |
|
#include "theory/arith/arith_utilities.h" |
27 |
|
#include "theory/arith/normal_form.h" |
28 |
|
#include "theory/rewriter.h" |
29 |
|
|
30 |
|
using namespace std; |
31 |
|
using namespace cvc5::kind; |
32 |
|
|
33 |
|
namespace cvc5 { |
34 |
|
namespace theory { |
35 |
|
namespace arith { |
36 |
|
|
37 |
|
|
38 |
15273 |
ArithStaticLearner::ArithStaticLearner(context::Context* userContext) : |
39 |
|
d_minMap(userContext), |
40 |
|
d_maxMap(userContext), |
41 |
15273 |
d_statistics() |
42 |
|
{ |
43 |
15273 |
} |
44 |
|
|
45 |
15268 |
ArithStaticLearner::~ArithStaticLearner(){ |
46 |
15268 |
} |
47 |
|
|
48 |
15273 |
ArithStaticLearner::Statistics::Statistics() |
49 |
15273 |
: d_iteMinMaxApplications(smtStatisticsRegistry().registerInt( |
50 |
30546 |
"theory::arith::iteMinMaxApplications")), |
51 |
15273 |
d_iteConstantApplications(smtStatisticsRegistry().registerInt( |
52 |
30546 |
"theory::arith::iteConstantApplications")) |
53 |
|
{ |
54 |
15273 |
} |
55 |
|
|
56 |
250978 |
void ArithStaticLearner::staticLearning(TNode n, NodeBuilder& learned) |
57 |
|
{ |
58 |
501956 |
vector<TNode> workList; |
59 |
250978 |
workList.push_back(n); |
60 |
501956 |
TNodeSet processed; |
61 |
|
|
62 |
|
//Contains an underapproximation of nodes that must hold. |
63 |
501956 |
TNodeSet defTrue; |
64 |
|
|
65 |
250978 |
defTrue.insert(n); |
66 |
|
|
67 |
10418974 |
while(!workList.empty()) { |
68 |
5083998 |
n = workList.back(); |
69 |
|
|
70 |
5083998 |
bool unprocessedChildren = false; |
71 |
13263961 |
for(TNode::iterator i = n.begin(), iend = n.end(); i != iend; ++i) { |
72 |
8179963 |
if(processed.find(*i) == processed.end()) { |
73 |
|
// unprocessed child |
74 |
2977160 |
workList.push_back(*i); |
75 |
2977160 |
unprocessedChildren = true; |
76 |
|
} |
77 |
|
} |
78 |
5083998 |
if(n.getKind() == AND && defTrue.find(n) != defTrue.end() ){ |
79 |
|
for(TNode::iterator i = n.begin(), iend = n.end(); i != iend; ++i) { |
80 |
|
defTrue.insert(*i); |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
5083998 |
if(unprocessedChildren) { |
85 |
1855860 |
continue; |
86 |
|
} |
87 |
|
|
88 |
3228138 |
workList.pop_back(); |
89 |
|
// has node n been processed in the meantime ? |
90 |
3228138 |
if(processed.find(n) != processed.end()) { |
91 |
48791 |
continue; |
92 |
|
} |
93 |
3179347 |
processed.insert(n); |
94 |
|
|
95 |
3179347 |
process(n,learned, defTrue); |
96 |
|
|
97 |
|
} |
98 |
250978 |
} |
99 |
|
|
100 |
3179347 |
void ArithStaticLearner::process(TNode n, |
101 |
|
NodeBuilder& learned, |
102 |
|
const TNodeSet& defTrue) |
103 |
|
{ |
104 |
3179347 |
Debug("arith::static") << "===================== looking at " << n << endl; |
105 |
|
|
106 |
3179347 |
switch(n.getKind()){ |
107 |
171317 |
case ITE: |
108 |
171317 |
if (expr::hasBoundVar(n)) |
109 |
|
{ |
110 |
|
// Unsafe with non-ground ITEs; do nothing |
111 |
9338 |
Debug("arith::static") |
112 |
4669 |
<< "(potentially) non-ground ITE, ignoring..." << endl; |
113 |
4669 |
break; |
114 |
|
} |
115 |
|
|
116 |
579920 |
if(n[0].getKind() != EQUAL && |
117 |
413272 |
isRelationOperator(n[0].getKind()) ){ |
118 |
1768 |
iteMinMax(n, learned); |
119 |
|
} |
120 |
|
|
121 |
499944 |
if((d_minMap.find(n[1]) != d_minMap.end() && d_minMap.find(n[2]) != d_minMap.end()) || |
122 |
331893 |
(d_maxMap.find(n[1]) != d_maxMap.end() && d_maxMap.find(n[2]) != d_maxMap.end())) { |
123 |
2974 |
iteConstant(n, learned); |
124 |
|
} |
125 |
166648 |
break; |
126 |
|
|
127 |
162789 |
case CONST_RATIONAL: |
128 |
|
// Mark constants as minmax |
129 |
162789 |
d_minMap.insert(n, n.getConst<Rational>()); |
130 |
162789 |
d_maxMap.insert(n, n.getConst<Rational>()); |
131 |
162789 |
break; |
132 |
2845241 |
default: // Do nothing |
133 |
2845241 |
break; |
134 |
|
} |
135 |
3179347 |
} |
136 |
|
|
137 |
1768 |
void ArithStaticLearner::iteMinMax(TNode n, NodeBuilder& learned) |
138 |
|
{ |
139 |
1768 |
Assert(n.getKind() == kind::ITE); |
140 |
1768 |
Assert(n[0].getKind() != EQUAL); |
141 |
1768 |
Assert(isRelationOperator(n[0].getKind())); |
142 |
|
|
143 |
3536 |
TNode c = n[0]; |
144 |
1768 |
Kind k = oldSimplifiedKind(c); |
145 |
3536 |
TNode t = n[1]; |
146 |
3536 |
TNode e = n[2]; |
147 |
3536 |
TNode cleft = (c.getKind() == NOT) ? c[0][0] : c[0]; |
148 |
3536 |
TNode cright = (c.getKind() == NOT) ? c[0][1] : c[1]; |
149 |
|
|
150 |
1768 |
if((t == cright) && (e == cleft)){ |
151 |
40 |
TNode tmp = t; |
152 |
20 |
t = e; |
153 |
20 |
e = tmp; |
154 |
20 |
k = reverseRelationKind(k); |
155 |
|
} |
156 |
|
//(ite (< x y) x y) |
157 |
|
//(ite (x < y) x y) |
158 |
|
//(ite (x - y < 0) x y) |
159 |
|
// ---------------- |
160 |
|
// (ite (x - y < -c) ) |
161 |
|
|
162 |
1768 |
if(t == cleft && e == cright){ |
163 |
|
// t == cleft && e == cright |
164 |
22 |
Assert(t == cleft); |
165 |
22 |
Assert(e == cright); |
166 |
22 |
switch(k){ |
167 |
20 |
case LT: // (ite (< x y) x y) |
168 |
|
case LEQ: { // (ite (<= x y) x y) |
169 |
40 |
Node nLeqX = NodeBuilder(LEQ) << n << t; |
170 |
40 |
Node nLeqY = NodeBuilder(LEQ) << n << e; |
171 |
20 |
Debug("arith::static") << n << "is a min =>" << nLeqX << nLeqY << endl; |
172 |
20 |
learned << nLeqX << nLeqY; |
173 |
20 |
++(d_statistics.d_iteMinMaxApplications); |
174 |
20 |
break; |
175 |
|
} |
176 |
2 |
case GT: // (ite (> x y) x y) |
177 |
|
case GEQ: { // (ite (>= x y) x y) |
178 |
4 |
Node nGeqX = NodeBuilder(GEQ) << n << t; |
179 |
4 |
Node nGeqY = NodeBuilder(GEQ) << n << e; |
180 |
2 |
Debug("arith::static") << n << "is a max =>" << nGeqX << nGeqY << endl; |
181 |
2 |
learned << nGeqX << nGeqY; |
182 |
2 |
++(d_statistics.d_iteMinMaxApplications); |
183 |
2 |
break; |
184 |
|
} |
185 |
|
default: Unreachable(); |
186 |
|
} |
187 |
|
} |
188 |
1768 |
} |
189 |
|
|
190 |
2974 |
void ArithStaticLearner::iteConstant(TNode n, NodeBuilder& learned) |
191 |
|
{ |
192 |
2974 |
Assert(n.getKind() == ITE); |
193 |
|
|
194 |
2974 |
Debug("arith::static") << "iteConstant(" << n << ")" << endl; |
195 |
|
|
196 |
2974 |
if (d_minMap.find(n[1]) != d_minMap.end() && d_minMap.find(n[2]) != d_minMap.end()) { |
197 |
5948 |
const DeltaRational& first = d_minMap[n[1]]; |
198 |
5948 |
const DeltaRational& second = d_minMap[n[2]]; |
199 |
5948 |
DeltaRational min = std::min(first, second); |
200 |
2974 |
CDNodeToMinMaxMap::const_iterator minFind = d_minMap.find(n); |
201 |
2974 |
if (minFind == d_minMap.end() || (*minFind).second < min) { |
202 |
1418 |
d_minMap.insert(n, min); |
203 |
2836 |
Node nGeqMin; |
204 |
1418 |
if (min.getInfinitesimalPart() == 0) { |
205 |
4254 |
nGeqMin = NodeBuilder(kind::GEQ) |
206 |
2836 |
<< n << mkRationalNode(min.getNoninfinitesimalPart()); |
207 |
|
} else { |
208 |
|
nGeqMin = NodeBuilder(kind::GT) |
209 |
|
<< n << mkRationalNode(min.getNoninfinitesimalPart()); |
210 |
|
} |
211 |
1418 |
learned << nGeqMin; |
212 |
1418 |
Debug("arith::static") << n << " iteConstant" << nGeqMin << endl; |
213 |
1418 |
++(d_statistics.d_iteConstantApplications); |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
2974 |
if (d_maxMap.find(n[1]) != d_maxMap.end() && d_maxMap.find(n[2]) != d_maxMap.end()) { |
218 |
5368 |
const DeltaRational& first = d_maxMap[n[1]]; |
219 |
5368 |
const DeltaRational& second = d_maxMap[n[2]]; |
220 |
5368 |
DeltaRational max = std::max(first, second); |
221 |
2684 |
CDNodeToMinMaxMap::const_iterator maxFind = d_maxMap.find(n); |
222 |
2684 |
if (maxFind == d_maxMap.end() || (*maxFind).second > max) { |
223 |
1288 |
d_maxMap.insert(n, max); |
224 |
2576 |
Node nLeqMax; |
225 |
1288 |
if (max.getInfinitesimalPart() == 0) { |
226 |
3864 |
nLeqMax = NodeBuilder(kind::LEQ) |
227 |
2576 |
<< n << mkRationalNode(max.getNoninfinitesimalPart()); |
228 |
|
} else { |
229 |
|
nLeqMax = NodeBuilder(kind::LT) |
230 |
|
<< n << mkRationalNode(max.getNoninfinitesimalPart()); |
231 |
|
} |
232 |
1288 |
learned << nLeqMax; |
233 |
1288 |
Debug("arith::static") << n << " iteConstant" << nLeqMax << endl; |
234 |
1288 |
++(d_statistics.d_iteConstantApplications); |
235 |
|
} |
236 |
|
} |
237 |
2974 |
} |
238 |
|
|
239 |
|
std::set<Node> listToSet(TNode l){ |
240 |
|
std::set<Node> ret; |
241 |
|
while(l.getKind() == OR){ |
242 |
|
Assert(l.getNumChildren() == 2); |
243 |
|
ret.insert(l[0]); |
244 |
|
l = l[1]; |
245 |
|
} |
246 |
|
return ret; |
247 |
|
} |
248 |
|
|
249 |
3902 |
void ArithStaticLearner::addBound(TNode n) { |
250 |
|
|
251 |
3902 |
CDNodeToMinMaxMap::const_iterator minFind = d_minMap.find(n[0]); |
252 |
3902 |
CDNodeToMinMaxMap::const_iterator maxFind = d_maxMap.find(n[0]); |
253 |
|
|
254 |
7804 |
Rational constant = n[1].getConst<Rational>(); |
255 |
7804 |
DeltaRational bound = constant; |
256 |
|
|
257 |
3902 |
switch(Kind k = n.getKind()) { |
258 |
|
case kind::LT: bound = DeltaRational(constant, -1); CVC5_FALLTHROUGH; |
259 |
|
case kind::LEQ: |
260 |
|
if (maxFind == d_maxMap.end() || (*maxFind).second > bound) |
261 |
|
{ |
262 |
|
d_maxMap.insert(n[0], bound); |
263 |
|
Debug("arith::static") << "adding bound " << n << endl; |
264 |
|
} |
265 |
|
break; |
266 |
|
case kind::GT: bound = DeltaRational(constant, 1); CVC5_FALLTHROUGH; |
267 |
3902 |
case kind::GEQ: |
268 |
3902 |
if (minFind == d_minMap.end() || (*minFind).second < bound) |
269 |
|
{ |
270 |
3645 |
d_minMap.insert(n[0], bound); |
271 |
3645 |
Debug("arith::static") << "adding bound " << n << endl; |
272 |
|
} |
273 |
3902 |
break; |
274 |
|
default: Unhandled() << k; break; |
275 |
|
} |
276 |
3902 |
} |
277 |
|
|
278 |
|
} // namespace arith |
279 |
|
} // namespace theory |
280 |
31137 |
} // namespace cvc5 |