1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Tim King, Morgan Deters, Mathias Preiner |
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 |
|
* Datastructures that track variable by variable information. |
14 |
|
* |
15 |
|
* This is a datastructure that tracks variable specific information. |
16 |
|
* This is partially context dependent to back track upper/lower bounds |
17 |
|
* and information derived from these. |
18 |
|
*/ |
19 |
|
|
20 |
|
#include "cvc5_private.h" |
21 |
|
|
22 |
|
#ifndef CVC5__THEORY__ARITH__PARTIAL_MODEL_H |
23 |
|
#define CVC5__THEORY__ARITH__PARTIAL_MODEL_H |
24 |
|
|
25 |
|
#include <vector> |
26 |
|
|
27 |
|
#include "context/cdlist.h" |
28 |
|
#include "expr/node.h" |
29 |
|
#include "theory/arith/arith_utilities.h" |
30 |
|
#include "theory/arith/arithvar.h" |
31 |
|
#include "theory/arith/bound_counts.h" |
32 |
|
#include "theory/arith/callbacks.h" |
33 |
|
#include "theory/arith/constraint_forward.h" |
34 |
|
#include "theory/arith/delta_rational.h" |
35 |
|
|
36 |
|
namespace cvc5 { |
37 |
|
namespace context { |
38 |
|
class Context; |
39 |
|
} |
40 |
|
namespace theory { |
41 |
|
namespace arith { |
42 |
|
|
43 |
|
/** |
44 |
|
* (For the moment) the type hierarchy goes as: |
45 |
|
* Integer <: Real |
46 |
|
* The type number of a variable is an integer representing the most specific |
47 |
|
* type of the variable. The possible values of type number are: |
48 |
|
*/ |
49 |
|
enum class ArithType { |
50 |
|
Unset, |
51 |
|
Real, |
52 |
|
Integer, |
53 |
|
}; |
54 |
|
|
55 |
15268 |
class ArithVariables { |
56 |
|
private: |
57 |
|
|
58 |
1052407 |
class VarInfo { |
59 |
|
friend class ArithVariables; |
60 |
|
ArithVar d_var; |
61 |
|
|
62 |
|
DeltaRational d_assignment; |
63 |
|
ConstraintP d_lb; |
64 |
|
ConstraintP d_ub; |
65 |
|
int d_cmpAssignmentLB; |
66 |
|
int d_cmpAssignmentUB; |
67 |
|
|
68 |
|
unsigned d_pushCount; |
69 |
|
ArithType d_type; |
70 |
|
Node d_node; |
71 |
|
bool d_auxiliary; |
72 |
|
|
73 |
|
public: |
74 |
|
VarInfo(); |
75 |
|
|
76 |
|
bool setAssignment(const DeltaRational& r, BoundsInfo& prev); |
77 |
|
bool setLowerBound(ConstraintP c, BoundsInfo& prev); |
78 |
|
bool setUpperBound(ConstraintP c, BoundsInfo& prev); |
79 |
|
|
80 |
|
/** Returns true if this VarInfo has been initialized. */ |
81 |
|
bool initialized() const; |
82 |
|
|
83 |
|
/** |
84 |
|
* Initializes the VarInfo with the ArithVar index it is associated with, |
85 |
|
* the node that the variable represents, and whether it is an auxillary |
86 |
|
* variable. |
87 |
|
*/ |
88 |
|
void initialize(ArithVar v, Node n, bool aux); |
89 |
|
|
90 |
|
/** Uninitializes the VarInfo. */ |
91 |
|
void uninitialize(); |
92 |
|
|
93 |
|
bool canBeReclaimed() const; |
94 |
|
|
95 |
|
/** Indicator variables for if the assignment is equal to the upper |
96 |
|
* and lower bounds. */ |
97 |
|
BoundCounts atBoundCounts() const; |
98 |
|
|
99 |
|
/** Combination of indicator variables for whether it has upper and |
100 |
|
* lower bounds. */ |
101 |
|
BoundCounts hasBoundCounts() const; |
102 |
|
|
103 |
|
/** Stores both atBoundCounts() and hasBoundCounts(). */ |
104 |
|
BoundsInfo boundsInfo() const; |
105 |
|
}; |
106 |
|
|
107 |
|
/**Maps from ArithVar -> VarInfo */ |
108 |
|
typedef DenseMap<VarInfo> VarInfoVec; |
109 |
|
|
110 |
|
/** This maps an ArithVar to its Variable information.*/ |
111 |
|
VarInfoVec d_vars; |
112 |
|
|
113 |
|
/** Partial Map from Arithvar -> PreviousAssignment */ |
114 |
|
DenseMap<DeltaRational> d_safeAssignment; |
115 |
|
|
116 |
|
/** if d_vars.isKey(x), then x < d_numberOfVariables */ |
117 |
|
ArithVar d_numberOfVariables; |
118 |
|
|
119 |
|
/** [0, d_numberOfVariables) \intersect d_vars.keys == d_pool */ |
120 |
|
// Everything in the pool is fair game. |
121 |
|
// There must be NO outstanding assertions |
122 |
|
std::vector<ArithVar> d_pool; |
123 |
|
std::vector<ArithVar> d_released; |
124 |
|
//std::list<ArithVar>::iterator d_releasedIterator; |
125 |
|
|
126 |
|
// Reverse Map from Node to ArithVar |
127 |
|
// Inverse of d_vars[x].d_node |
128 |
|
NodeToArithVarMap d_nodeToArithVarMap; |
129 |
|
|
130 |
|
|
131 |
|
/** The queue of constraints where the assignment is at the bound.*/ |
132 |
|
DenseMap<BoundsInfo> d_boundsQueue; |
133 |
|
|
134 |
|
/** |
135 |
|
* If this is true, record the incoming changes to the bound information. |
136 |
|
* If this is false, the responsibility of recording the changes is |
137 |
|
* LinearEqualities's. |
138 |
|
*/ |
139 |
|
bool d_enqueueingBoundCounts; |
140 |
|
|
141 |
|
public: |
142 |
|
|
143 |
|
/** Returns the number of variables. */ |
144 |
|
ArithVar getNumberOfVariables() const; |
145 |
|
|
146 |
|
/** Returns true if the node has an associated variables. */ |
147 |
|
bool hasArithVar(TNode x) const; |
148 |
|
|
149 |
|
/** Returns true if the variable has a defining node. */ |
150 |
|
bool hasNode(ArithVar a) const; |
151 |
|
|
152 |
|
/** Returns the ArithVar associated with a node. */ |
153 |
|
ArithVar asArithVar(TNode x) const; |
154 |
|
|
155 |
|
/** Returns the node associated with an ArithVar. */ |
156 |
|
Node asNode(ArithVar a) const; |
157 |
|
|
158 |
|
/** Allocates a freshly allocated variables. */ |
159 |
|
ArithVar allocateVariable(); |
160 |
|
|
161 |
|
class var_iterator { |
162 |
|
private: |
163 |
|
const VarInfoVec* d_vars; |
164 |
|
VarInfoVec::const_iterator d_wrapped; |
165 |
|
public: |
166 |
|
var_iterator(); |
167 |
|
var_iterator(const VarInfoVec* vars, VarInfoVec::const_iterator ci); |
168 |
|
var_iterator& operator++(); |
169 |
|
|
170 |
|
bool operator==(const var_iterator& other) const; |
171 |
|
bool operator!=(const var_iterator& other) const; |
172 |
|
ArithVar operator*() const; |
173 |
|
|
174 |
|
private: |
175 |
|
void nextInitialized(); |
176 |
|
}; |
177 |
|
|
178 |
|
var_iterator var_begin() const; |
179 |
|
var_iterator var_end() const; |
180 |
|
|
181 |
|
|
182 |
|
bool canBeReleased(ArithVar v) const; |
183 |
|
void releaseArithVar(ArithVar v); |
184 |
|
void attemptToReclaimReleased(); |
185 |
|
|
186 |
|
/** Is this variable guaranteed to have an integer assignment? |
187 |
|
* (Should agree with the type system.) */ |
188 |
|
bool isInteger(ArithVar x) const; |
189 |
|
|
190 |
|
/** Is the assignment to x integral? */ |
191 |
|
bool integralAssignment(ArithVar x) const; |
192 |
|
|
193 |
|
/* Is this variable defined as a linear sum of other variables? */ |
194 |
|
bool isAuxiliary(ArithVar x) const; |
195 |
|
|
196 |
|
/* Is the variable both input and not auxiliary? */ |
197 |
|
bool isIntegerInput(ArithVar x) const; |
198 |
|
|
199 |
|
private: |
200 |
|
|
201 |
|
typedef std::pair<ArithVar, ConstraintP> AVCPair; |
202 |
|
class LowerBoundCleanUp { |
203 |
|
private: |
204 |
|
ArithVariables* d_pm; |
205 |
|
public: |
206 |
|
LowerBoundCleanUp(ArithVariables* pm); |
207 |
|
void operator()(AVCPair* restore); |
208 |
|
}; |
209 |
|
|
210 |
|
class UpperBoundCleanUp { |
211 |
|
private: |
212 |
|
ArithVariables* d_pm; |
213 |
|
public: |
214 |
|
UpperBoundCleanUp(ArithVariables* pm); |
215 |
|
void operator()(AVCPair* restore); |
216 |
|
}; |
217 |
|
|
218 |
|
typedef context::CDList<AVCPair, LowerBoundCleanUp> LBReverts; |
219 |
|
LBReverts d_lbRevertHistory; |
220 |
|
|
221 |
|
typedef context::CDList<AVCPair, UpperBoundCleanUp> UBReverts; |
222 |
|
UBReverts d_ubRevertHistory; |
223 |
|
|
224 |
|
void pushUpperBound(VarInfo&); |
225 |
|
void popUpperBound(AVCPair*); |
226 |
|
void pushLowerBound(VarInfo&); |
227 |
|
void popLowerBound(AVCPair*); |
228 |
|
|
229 |
|
// This is true when setDelta() is called, until invalidateDelta is called |
230 |
|
bool d_deltaIsSafe; |
231 |
|
// Cache of a value of delta to ensure a total order. |
232 |
|
Rational d_delta; |
233 |
|
// Function to call if the value of delta needs to be recomputed. |
234 |
|
DeltaComputeCallback d_deltaComputingFunc; |
235 |
|
|
236 |
|
|
237 |
|
public: |
238 |
|
|
239 |
|
ArithVariables(context::Context* c, DeltaComputeCallback deltaComputation); |
240 |
|
|
241 |
|
/** |
242 |
|
* This sets the lower bound for a variable in the current context. |
243 |
|
* This must be stronger the previous constraint. |
244 |
|
*/ |
245 |
|
void setLowerBoundConstraint(ConstraintP lb); |
246 |
|
|
247 |
|
/** |
248 |
|
* This sets the upper bound for a variable in the current context. |
249 |
|
* This must be stronger the previous constraint. |
250 |
|
*/ |
251 |
|
void setUpperBoundConstraint(ConstraintP ub); |
252 |
|
|
253 |
|
/** Returns the constraint for the upper bound of a variable. */ |
254 |
13123262 |
inline ConstraintP getUpperBoundConstraint(ArithVar x) const{ |
255 |
13123262 |
return d_vars[x].d_ub; |
256 |
|
} |
257 |
|
/** Returns the constraint for the lower bound of a variable. */ |
258 |
14634725 |
inline ConstraintP getLowerBoundConstraint(ArithVar x) const{ |
259 |
14634725 |
return d_vars[x].d_lb; |
260 |
|
} |
261 |
|
|
262 |
|
/* Initializes a variable to a safe value.*/ |
263 |
|
void initialize(ArithVar x, Node n, bool aux); |
264 |
|
|
265 |
|
ArithVar allocate(Node n, bool aux = false); |
266 |
|
|
267 |
|
/* Gets the last assignment to a variable that is known to be consistent. */ |
268 |
|
const DeltaRational& getSafeAssignment(ArithVar x) const; |
269 |
|
const DeltaRational& getAssignment(ArithVar x, bool safe) const; |
270 |
|
|
271 |
|
/* Reverts all variable assignments to their safe values. */ |
272 |
|
void revertAssignmentChanges(); |
273 |
|
|
274 |
|
/* Commits all variables assignments as safe.*/ |
275 |
|
void commitAssignmentChanges(); |
276 |
|
|
277 |
|
|
278 |
|
bool lowerBoundIsZero(ArithVar x); |
279 |
|
bool upperBoundIsZero(ArithVar x); |
280 |
|
|
281 |
|
bool boundsAreEqual(ArithVar x) const; |
282 |
|
|
283 |
|
/* Sets an unsafe variable assignment */ |
284 |
|
void setAssignment(ArithVar x, const DeltaRational& r); |
285 |
|
void setAssignment(ArithVar x, const DeltaRational& safe, const DeltaRational& r); |
286 |
|
|
287 |
|
|
288 |
|
/** Must know that the bound exists before calling this! */ |
289 |
|
const DeltaRational& getUpperBound(ArithVar x) const; |
290 |
|
const DeltaRational& getLowerBound(ArithVar x) const; |
291 |
|
const DeltaRational& getAssignment(ArithVar x) const; |
292 |
|
|
293 |
|
|
294 |
|
bool equalsLowerBound(ArithVar x, const DeltaRational& c); |
295 |
|
bool equalsUpperBound(ArithVar x, const DeltaRational& c); |
296 |
|
|
297 |
|
/** |
298 |
|
* If lowerbound > - \infty: |
299 |
|
* return getAssignment(x).cmp(getLowerBound(x)) |
300 |
|
* If lowerbound = - \infty: |
301 |
|
* return 1 |
302 |
|
*/ |
303 |
|
int cmpToLowerBound(ArithVar x, const DeltaRational& c) const; |
304 |
|
|
305 |
1488873 |
inline bool strictlyLessThanLowerBound(ArithVar x, const DeltaRational& c) const{ |
306 |
1488873 |
return cmpToLowerBound(x, c) < 0; |
307 |
|
} |
308 |
2346292 |
inline bool lessThanLowerBound(ArithVar x, const DeltaRational& c) const{ |
309 |
2346292 |
return cmpToLowerBound(x, c) <= 0; |
310 |
|
} |
311 |
|
|
312 |
454784 |
inline bool strictlyGreaterThanLowerBound(ArithVar x, const DeltaRational& c) const{ |
313 |
454784 |
return cmpToLowerBound(x, c) > 0; |
314 |
|
} |
315 |
|
|
316 |
2583844 |
inline bool greaterThanLowerBound(ArithVar x, const DeltaRational& c) const{ |
317 |
2583844 |
return cmpToLowerBound(x, c) >= 0; |
318 |
|
} |
319 |
|
/** |
320 |
|
* If upperbound < \infty: |
321 |
|
* return getAssignment(x).cmp(getUpperBound(x)) |
322 |
|
* If upperbound = \infty: |
323 |
|
* return -1 |
324 |
|
*/ |
325 |
|
int cmpToUpperBound(ArithVar x, const DeltaRational& c) const; |
326 |
|
|
327 |
243215 |
inline bool strictlyLessThanUpperBound(ArithVar x, const DeltaRational& c) const{ |
328 |
243215 |
return cmpToUpperBound(x, c) < 0; |
329 |
|
} |
330 |
|
|
331 |
2558419 |
inline bool lessThanUpperBound(ArithVar x, const DeltaRational& c) const{ |
332 |
2558419 |
return cmpToUpperBound(x, c) <= 0; |
333 |
|
} |
334 |
|
|
335 |
1083742 |
inline bool strictlyGreaterThanUpperBound(ArithVar x, const DeltaRational& c) const{ |
336 |
1083742 |
return cmpToUpperBound(x, c) > 0; |
337 |
|
} |
338 |
|
|
339 |
2556991 |
inline bool greaterThanUpperBound(ArithVar x, const DeltaRational& c) const{ |
340 |
2556991 |
return cmpToUpperBound(x, c) >= 0; |
341 |
|
} |
342 |
|
|
343 |
13988095 |
inline int cmpAssignmentLowerBound(ArithVar x) const{ |
344 |
13988095 |
return d_vars[x].d_cmpAssignmentLB; |
345 |
|
} |
346 |
11957910 |
inline int cmpAssignmentUpperBound(ArithVar x) const{ |
347 |
11957910 |
return d_vars[x].d_cmpAssignmentUB; |
348 |
|
} |
349 |
|
|
350 |
12008076 |
inline BoundCounts atBoundCounts(ArithVar x) const { |
351 |
12008076 |
return d_vars[x].atBoundCounts(); |
352 |
|
} |
353 |
|
inline BoundCounts hasBoundCounts(ArithVar x) const { |
354 |
|
return d_vars[x].hasBoundCounts(); |
355 |
|
} |
356 |
79688762 |
inline BoundsInfo boundsInfo(ArithVar x) const{ |
357 |
79688762 |
return d_vars[x].boundsInfo(); |
358 |
|
} |
359 |
|
|
360 |
|
bool strictlyBelowUpperBound(ArithVar x) const; |
361 |
|
bool strictlyAboveLowerBound(ArithVar x) const; |
362 |
|
bool assignmentIsConsistent(ArithVar x) const; |
363 |
|
|
364 |
|
void printModel(ArithVar x, std::ostream& out) const; |
365 |
|
void printModel(ArithVar x) const; |
366 |
|
|
367 |
|
/** returns true iff x has both a lower and upper bound. */ |
368 |
|
bool hasEitherBound(ArithVar x) const; |
369 |
41598283 |
inline bool hasLowerBound(ArithVar x) const{ |
370 |
41598283 |
return d_vars[x].d_lb != NullConstraint; |
371 |
|
} |
372 |
38453883 |
inline bool hasUpperBound(ArithVar x) const{ |
373 |
38453883 |
return d_vars[x].d_ub != NullConstraint; |
374 |
|
} |
375 |
|
|
376 |
|
const Rational& getDelta(); |
377 |
|
|
378 |
|
void invalidateDelta(); |
379 |
|
|
380 |
|
void setDelta(const Rational& d); |
381 |
|
|
382 |
|
void startQueueingBoundCounts(); |
383 |
|
void stopQueueingBoundCounts(); |
384 |
|
void addToBoundQueue(ArithVar v, const BoundsInfo& prev); |
385 |
|
|
386 |
|
BoundsInfo selectBoundsInfo(ArithVar v, bool old) const; |
387 |
|
|
388 |
|
bool boundsQueueEmpty() const; |
389 |
|
void processBoundsQueue(BoundUpdateCallback& changed); |
390 |
|
|
391 |
|
void printEntireModel(std::ostream& out) const; |
392 |
|
|
393 |
|
|
394 |
|
/** |
395 |
|
* Precondition: assumes boundsAreEqual(x). |
396 |
|
* If the either the lower/ upper bound is an equality, eq, |
397 |
|
* this returns make_pair(eq, NullConstraint). |
398 |
|
* Otherwise, this returns make_pair(lb, ub). |
399 |
|
*/ |
400 |
|
std::pair<ConstraintP, ConstraintP> explainEqualBounds(ArithVar x) const; |
401 |
|
|
402 |
|
private: |
403 |
|
|
404 |
|
/** |
405 |
|
* This function implements the mostly identical: |
406 |
|
* revertAssignmentChanges() and commitAssignmentChanges(). |
407 |
|
*/ |
408 |
|
void clearSafeAssignments(bool revert); |
409 |
|
|
410 |
|
bool debugEqualSizes(); |
411 |
|
|
412 |
|
bool inMaps(ArithVar x) const; |
413 |
|
|
414 |
|
};/* class ArithVariables */ |
415 |
|
|
416 |
|
} // namespace arith |
417 |
|
} // namespace theory |
418 |
|
} // namespace cvc5 |
419 |
|
|
420 |
|
#endif /* CVC5__THEORY__ARITH__PARTIAL_MODEL_H */ |