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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
/********************* */
/*! \file partial_model.cpp
** \verbatim
** Top contributors (to current version):
** Tim King
** This file is part of the CVC4 project.
** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
** in the top-level source directory and their institutional affiliations.
** All rights reserved. See the file COPYING in the top-level source
** directory for licensing information.\endverbatim
**
** \brief [[ Add one-line brief description here ]]
**
** [[ Add lengthier description here ]]
** \todo document this file
**/
#include "base/output.h"
#include "theory/arith/constraint.h"
#include "theory/arith/normal_form.h"
#include "theory/arith/partial_model.h"
using namespace std;
namespace cvc5 {
namespace theory {
namespace arith {
ArithVariables::ArithVariables(context::Context* c, DeltaComputeCallback deltaComputingFunc)
: d_vars(),
d_safeAssignment(),
d_numberOfVariables(0),
d_pool(),
d_released(),
d_nodeToArithVarMap(),
d_boundsQueue(),
d_enqueueingBoundCounts(true),
d_lbRevertHistory(c, true, LowerBoundCleanUp(this)),
d_ubRevertHistory(c, true, UpperBoundCleanUp(this)),
d_deltaIsSafe(false),
d_delta(-1,1),
d_deltaComputingFunc(deltaComputingFunc)
{ }
ArithVar ArithVariables::getNumberOfVariables() const {
return d_numberOfVariables;
}
bool ArithVariables::hasArithVar(TNode x) const {
return d_nodeToArithVarMap.find(x) != d_nodeToArithVarMap.end();
}
bool ArithVariables::hasNode(ArithVar a) const {
return d_vars.isKey(a);
}
ArithVar ArithVariables::asArithVar(TNode x) const{
Assert(hasArithVar(x));
Assert((d_nodeToArithVarMap.find(x))->second <= ARITHVAR_SENTINEL);
return (d_nodeToArithVarMap.find(x))->second;
}
Node ArithVariables::asNode(ArithVar a) const{
Assert(hasNode(a));
return d_vars[a].d_node;
}
ArithVariables::var_iterator::var_iterator()
: d_vars(NULL)
, d_wrapped()
{}
ArithVariables::var_iterator::var_iterator(const VarInfoVec* vars, VarInfoVec::const_iterator ci)
: d_vars(vars), d_wrapped(ci)
{
nextInitialized();
}
ArithVariables::var_iterator& ArithVariables::var_iterator::operator++(){
++d_wrapped;
nextInitialized();
return *this;
}
bool ArithVariables::var_iterator::operator==(const ArithVariables::var_iterator& other) const{
return d_wrapped == other.d_wrapped;
}
bool ArithVariables::var_iterator::operator!=(const ArithVariables::var_iterator& other) const{
return d_wrapped != other.d_wrapped;
}
ArithVar ArithVariables::var_iterator::operator*() const{
return *d_wrapped;
}
void ArithVariables::var_iterator::nextInitialized(){
VarInfoVec::const_iterator end = d_vars->end();
while(d_wrapped != end &&
!((*d_vars)[*d_wrapped].initialized())){
++d_wrapped;
}
}
ArithVariables::var_iterator ArithVariables::var_begin() const {
return var_iterator(&d_vars, d_vars.begin());
}
ArithVariables::var_iterator ArithVariables::var_end() const {
return var_iterator(&d_vars, d_vars.end());
}
bool ArithVariables::isInteger(ArithVar x) const {
return d_vars[x].d_type >= ArithType::Integer;
}
/** Is the assignment to x integral? */
bool ArithVariables::integralAssignment(ArithVar x) const {
return getAssignment(x).isIntegral();
}
bool ArithVariables::isAuxiliary(ArithVar x) const {
return d_vars[x].d_auxiliary;
}
bool ArithVariables::isIntegerInput(ArithVar x) const {
return isInteger(x) && !isAuxiliary(x);
}
ArithVariables::VarInfo::VarInfo()
: d_var(ARITHVAR_SENTINEL),
d_assignment(0),
d_lb(NullConstraint),
d_ub(NullConstraint),
d_cmpAssignmentLB(1),
d_cmpAssignmentUB(-1),
d_pushCount(0),
d_type(ArithType::Unset),
d_node(Node::null()),
d_auxiliary(false) {}
bool ArithVariables::VarInfo::initialized() const {
return d_var != ARITHVAR_SENTINEL;
}
void ArithVariables::VarInfo::initialize(ArithVar v, Node n, bool aux){
Assert(!initialized());
Assert(d_lb == NullConstraint);
Assert(d_ub == NullConstraint);
Assert(d_cmpAssignmentLB > 0);
Assert(d_cmpAssignmentUB < 0);
d_var = v;
d_node = n;
d_auxiliary = aux;
if(d_auxiliary){
//The type computation is not quite accurate for Rationals that are
//integral.
//We'll use the isIntegral check from the polynomial package instead.
Polynomial p = Polynomial::parsePolynomial(n);
d_type = p.isIntegral() ? ArithType::Integer : ArithType::Real;
}else{
d_type = n.getType().isInteger() ? ArithType::Integer : ArithType::Real;
}
Assert(initialized());
}
void ArithVariables::VarInfo::uninitialize(){
d_var = ARITHVAR_SENTINEL;
d_node = Node::null();
}
bool ArithVariables::VarInfo::setAssignment(const DeltaRational& a, BoundsInfo& prev){
Assert(initialized());
d_assignment = a;
int cmpUB = (d_ub == NullConstraint) ? -1 :
d_assignment.cmp(d_ub->getValue());
int cmpLB = (d_lb == NullConstraint) ? 1 :
d_assignment.cmp(d_lb->getValue());
bool lbChanged = cmpLB != d_cmpAssignmentLB &&
(cmpLB == 0 || d_cmpAssignmentLB == 0);
bool ubChanged = cmpUB != d_cmpAssignmentUB &&
(cmpUB == 0 || d_cmpAssignmentUB == 0);
if(lbChanged || ubChanged){
prev = boundsInfo();
}
d_cmpAssignmentUB = cmpUB;
d_cmpAssignmentLB = cmpLB;
return lbChanged || ubChanged;
}
void ArithVariables::releaseArithVar(ArithVar v){
VarInfo& vi = d_vars.get(v);
size_t removed CVC5_UNUSED = d_nodeToArithVarMap.erase(vi.d_node);
Assert(removed == 1);
vi.uninitialize();
if(d_safeAssignment.isKey(v)){
d_safeAssignment.remove(v);
}
if(vi.canBeReclaimed()){
d_pool.push_back(v);
}else{
d_released.push_back(v);
}
}
bool ArithVariables::VarInfo::setUpperBound(ConstraintP ub, BoundsInfo& prev){
Assert(initialized());
bool wasNull = d_ub == NullConstraint;
bool isNull = ub == NullConstraint;
int cmpUB = isNull ? -1 : d_assignment.cmp(ub->getValue());
bool ubChanged = (wasNull != isNull) ||
(cmpUB != d_cmpAssignmentUB && (cmpUB == 0 || d_cmpAssignmentUB == 0));
if(ubChanged){
prev = boundsInfo();
}
d_ub = ub;
d_cmpAssignmentUB = cmpUB;
return ubChanged;
}
bool ArithVariables::VarInfo::setLowerBound(ConstraintP lb, BoundsInfo& prev){
Assert(initialized());
bool wasNull = d_lb == NullConstraint;
bool isNull = lb == NullConstraint;
int cmpLB = isNull ? 1 : d_assignment.cmp(lb->getValue());
bool lbChanged = (wasNull != isNull) ||
(cmpLB != d_cmpAssignmentLB && (cmpLB == 0 || d_cmpAssignmentLB == 0));
if(lbChanged){
prev = boundsInfo();
}
d_lb = lb;
d_cmpAssignmentLB = cmpLB;
return lbChanged;
}
BoundCounts ArithVariables::VarInfo::atBoundCounts() const {
uint32_t lbIndc = (d_cmpAssignmentLB == 0) ? 1 : 0;
uint32_t ubIndc = (d_cmpAssignmentUB == 0) ? 1 : 0;
return BoundCounts(lbIndc, ubIndc);
}
BoundCounts ArithVariables::VarInfo::hasBoundCounts() const {
uint32_t lbIndc = (d_lb != NullConstraint) ? 1 : 0;
uint32_t ubIndc = (d_ub != NullConstraint) ? 1 : 0;
return BoundCounts(lbIndc, ubIndc);
}
BoundsInfo ArithVariables::VarInfo::boundsInfo() const{
return BoundsInfo(atBoundCounts(), hasBoundCounts());
}
bool ArithVariables::VarInfo::canBeReclaimed() const{
return d_pushCount == 0;
}
bool ArithVariables::canBeReleased(ArithVar v) const{
return d_vars[v].canBeReclaimed();
}
void ArithVariables::attemptToReclaimReleased(){
size_t readPos = 0, writePos = 0, N = d_released.size();
for(; readPos < N; ++readPos){
ArithVar v = d_released[readPos];
if(canBeReleased(v)){
d_pool.push_back(v);
}else{
d_released[writePos] = v;
writePos++;
}
}
d_released.resize(writePos);
}
ArithVar ArithVariables::allocateVariable(){
if(d_pool.empty()){
attemptToReclaimReleased();
}
bool reclaim = !d_pool.empty();
ArithVar varX;
if(reclaim){
varX = d_pool.back();
d_pool.pop_back();
}else{
varX = d_numberOfVariables;
++d_numberOfVariables;
}
d_vars.set(varX, VarInfo());
return varX;
}
const Rational& ArithVariables::getDelta(){
if(!d_deltaIsSafe){
Rational nextDelta = d_deltaComputingFunc();
setDelta(nextDelta);
}
Assert(d_deltaIsSafe);
return d_delta;
}
bool ArithVariables::boundsAreEqual(ArithVar x) const{
if(hasLowerBound(x) && hasUpperBound(x)){
return getUpperBound(x) == getLowerBound(x);
}else{
return false;
}
}
std::pair<ConstraintP, ConstraintP> ArithVariables::explainEqualBounds(ArithVar x) const{
Assert(boundsAreEqual(x));
ConstraintP lb = getLowerBoundConstraint(x);
ConstraintP ub = getUpperBoundConstraint(x);
if(lb->isEquality()){
return make_pair(lb, NullConstraint);
}else if(ub->isEquality()){
return make_pair(ub, NullConstraint);
}else{
return make_pair(lb, ub);
}
}
void ArithVariables::setAssignment(ArithVar x, const DeltaRational& r){
Debug("partial_model") << "pm: updating the assignment to" << x
<< " now " << r <<endl;
VarInfo& vi = d_vars.get(x);
if(!d_safeAssignment.isKey(x)){
d_safeAssignment.set(x, vi.d_assignment);
}
invalidateDelta();
BoundsInfo prev;
if(vi.setAssignment(r, prev)){
addToBoundQueue(x, prev);
}
}
void ArithVariables::setAssignment(ArithVar x, const DeltaRational& safe, const DeltaRational& r){
Debug("partial_model") << "pm: updating the assignment to" << x
<< " now " << r <<endl;
if(safe == r){
if(d_safeAssignment.isKey(x)){
d_safeAssignment.remove(x);
}
}else{
d_safeAssignment.set(x, safe);
}
invalidateDelta();
VarInfo& vi = d_vars.get(x);
BoundsInfo prev;
if(vi.setAssignment(r, prev)){
addToBoundQueue(x, prev);
}
}
void ArithVariables::initialize(ArithVar x, Node n, bool aux){
VarInfo& vi = d_vars.get(x);
vi.initialize(x, n, aux);
d_nodeToArithVarMap[n] = x;
}
ArithVar ArithVariables::allocate(Node n, bool aux){
ArithVar v = allocateVariable();
initialize(v, n, aux);
return v;
}
// void ArithVariables::initialize(ArithVar x, const DeltaRational& r){
// Assert(x == d_mapSize);
// Assert(equalSizes());
// ++d_mapSize;
// // Is worth mentioning that this is not strictly necessary, but this maintains the internal invariant
// // that when d_assignment is set this gets set.
// invalidateDelta();
// d_assignment.push_back( r );
// d_boundRel.push_back(BetweenBounds);
// d_ubc.push_back(NullConstraint);
// d_lbc.push_back(NullConstraint);
// }
/** Must know that the bound exists both calling this! */
const DeltaRational& ArithVariables::getUpperBound(ArithVar x) const {
Assert(inMaps(x));
Assert(hasUpperBound(x));
return getUpperBoundConstraint(x)->getValue();
}
const DeltaRational& ArithVariables::getLowerBound(ArithVar x) const {
Assert(inMaps(x));
Assert(hasLowerBound(x));
return getLowerBoundConstraint(x)->getValue();
}
const DeltaRational& ArithVariables::getSafeAssignment(ArithVar x) const{
Assert(inMaps(x));
if(d_safeAssignment.isKey(x)){
return d_safeAssignment[x];
}else{
return d_vars[x].d_assignment;
}
}
const DeltaRational& ArithVariables::getAssignment(ArithVar x, bool safe) const{
Assert(inMaps(x));
if(safe && d_safeAssignment.isKey(x)){
return d_safeAssignment[x];
}else{
return d_vars[x].d_assignment;
}
}
const DeltaRational& ArithVariables::getAssignment(ArithVar x) const{
Assert(inMaps(x));
return d_vars[x].d_assignment;
}
void ArithVariables::setLowerBoundConstraint(ConstraintP c){
AssertArgument(c != NullConstraint, "Cannot set a lower bound to NullConstraint.");
AssertArgument(c->isEquality() || c->isLowerBound(),
"Constraint type must be set to an equality or UpperBound.");
ArithVar x = c->getVariable();
Debug("partial_model") << "setLowerBoundConstraint(" << x << ":" << c << ")" << endl;
Assert(inMaps(x));
Assert(greaterThanLowerBound(x, c->getValue()));
invalidateDelta();
VarInfo& vi = d_vars.get(x);
pushLowerBound(vi);
BoundsInfo prev;
if(vi.setLowerBound(c, prev)){
addToBoundQueue(x, prev);
}
}
void ArithVariables::setUpperBoundConstraint(ConstraintP c){
AssertArgument(c != NullConstraint, "Cannot set a upper bound to NullConstraint.");
AssertArgument(c->isEquality() || c->isUpperBound(),
"Constraint type must be set to an equality or UpperBound.");
ArithVar x = c->getVariable();
Debug("partial_model") << "setUpperBoundConstraint(" << x << ":" << c << ")" << endl;
Assert(inMaps(x));
Assert(lessThanUpperBound(x, c->getValue()));
invalidateDelta();
VarInfo& vi = d_vars.get(x);
pushUpperBound(vi);
BoundsInfo prev;
if(vi.setUpperBound(c, prev)){
addToBoundQueue(x, prev);
}
}
int ArithVariables::cmpToLowerBound(ArithVar x, const DeltaRational& c) const{
if(!hasLowerBound(x)){
// l = -\intfy
// ? c < -\infty |- _|_
return 1;
}else{
return c.cmp(getLowerBound(x));
}
}
int ArithVariables::cmpToUpperBound(ArithVar x, const DeltaRational& c) const{
if(!hasUpperBound(x)){
//u = \intfy
// ? c > \infty |- _|_
return -1;
}else{
return c.cmp(getUpperBound(x));
}
}
bool ArithVariables::equalsLowerBound(ArithVar x, const DeltaRational& c){
if(!hasLowerBound(x)){
return false;
}else{
return c == getLowerBound(x);
}
}
bool ArithVariables::equalsUpperBound(ArithVar x, const DeltaRational& c){
if(!hasUpperBound(x)){
return false;
}else{
return c == getUpperBound(x);
}
}
bool ArithVariables::hasEitherBound(ArithVar x) const{
return hasLowerBound(x) || hasUpperBound(x);
}
bool ArithVariables::strictlyBelowUpperBound(ArithVar x) const{
return d_vars[x].d_cmpAssignmentUB < 0;
}
bool ArithVariables::strictlyAboveLowerBound(ArithVar x) const{
return d_vars[x].d_cmpAssignmentLB > 0;
}
bool ArithVariables::assignmentIsConsistent(ArithVar x) const{
return
d_vars[x].d_cmpAssignmentLB >= 0 &&
d_vars[x].d_cmpAssignmentUB <= 0;
}
void ArithVariables::clearSafeAssignments(bool revert){
if(revert && !d_safeAssignment.empty()){
invalidateDelta();
}
while(!d_safeAssignment.empty()){
ArithVar atBack = d_safeAssignment.back();
if(revert){
VarInfo& vi = d_vars.get(atBack);
BoundsInfo prev;
if(vi.setAssignment(d_safeAssignment[atBack], prev)){
addToBoundQueue(atBack, prev);
}
}
d_safeAssignment.pop_back();
}
}
void ArithVariables::revertAssignmentChanges(){
clearSafeAssignments(true);
}
void ArithVariables::commitAssignmentChanges(){
clearSafeAssignments(false);
}
bool ArithVariables::lowerBoundIsZero(ArithVar x){
return hasLowerBound(x) && getLowerBound(x).sgn() == 0;
}
bool ArithVariables::upperBoundIsZero(ArithVar x){
return hasUpperBound(x) && getUpperBound(x).sgn() == 0;
}
void ArithVariables::printEntireModel(std::ostream& out) const{
out << "---Printing Model ---" << std::endl;
for(var_iterator i = var_begin(), iend = var_end(); i != iend; ++i){
printModel(*i, out);
}
out << "---Done Model ---" << std::endl;
}
void ArithVariables::printModel(ArithVar x, std::ostream& out) const{
out << "model" << x << ": "
<< asNode(x) << " "
<< getAssignment(x) << " ";
if(!hasLowerBound(x)){
out << "no lb ";
}else{
out << getLowerBound(x) << " ";
out << getLowerBoundConstraint(x) << " ";
}
if(!hasUpperBound(x)){
out << "no ub ";
}else{
out << getUpperBound(x) << " ";
out << getUpperBoundConstraint(x) << " ";
}
if(isInteger(x) && !integralAssignment(x)){
out << "(not an integer)" << endl;
}
out << endl;
}
void ArithVariables::printModel(ArithVar x) const{
printModel(x, Debug("model"));
}
void ArithVariables::pushUpperBound(VarInfo& vi){
++vi.d_pushCount;
d_ubRevertHistory.push_back(make_pair(vi.d_var, vi.d_ub));
}
void ArithVariables::pushLowerBound(VarInfo& vi){
++vi.d_pushCount;
d_lbRevertHistory.push_back(make_pair(vi.d_var, vi.d_lb));
}
void ArithVariables::popUpperBound(AVCPair* c){
ArithVar x = c->first;
VarInfo& vi = d_vars.get(x);
BoundsInfo prev;
if(vi.setUpperBound(c->second, prev)){
addToBoundQueue(x, prev);
}
--vi.d_pushCount;
}
void ArithVariables::popLowerBound(AVCPair* c){
ArithVar x = c->first;
VarInfo& vi = d_vars.get(x);
BoundsInfo prev;
if(vi.setLowerBound(c->second, prev)){
addToBoundQueue(x, prev);
}
--vi.d_pushCount;
}
void ArithVariables::addToBoundQueue(ArithVar v, const BoundsInfo& prev){
if(d_enqueueingBoundCounts && !d_boundsQueue.isKey(v)){
d_boundsQueue.set(v, prev);
}
}
BoundsInfo ArithVariables::selectBoundsInfo(ArithVar v, bool old) const {
if(old && d_boundsQueue.isKey(v)){
return d_boundsQueue[v];
}else{
return boundsInfo(v);
}
}
bool ArithVariables::boundsQueueEmpty() const {
return d_boundsQueue.empty();
}
void ArithVariables::processBoundsQueue(BoundUpdateCallback& changed){
while(!boundsQueueEmpty()){
ArithVar v = d_boundsQueue.back();
BoundsInfo prev = d_boundsQueue[v];
d_boundsQueue.pop_back();
BoundsInfo curr = boundsInfo(v);
if(prev != curr){
changed(v, prev);
}
}
}
void ArithVariables::invalidateDelta() {
d_deltaIsSafe = false;
}
void ArithVariables::setDelta(const Rational& d){
d_delta = d;
d_deltaIsSafe = true;
}
void ArithVariables::startQueueingBoundCounts(){
d_enqueueingBoundCounts = true;
}
void ArithVariables::stopQueueingBoundCounts(){
d_enqueueingBoundCounts = false;
}
bool ArithVariables::inMaps(ArithVar x) const{
return x < getNumberOfVariables();
}
ArithVariables::LowerBoundCleanUp::LowerBoundCleanUp(ArithVariables* pm)
: d_pm(pm)
{}
void ArithVariables::LowerBoundCleanUp::operator()(AVCPair* p){
d_pm->popLowerBound(p);
}
ArithVariables::UpperBoundCleanUp::UpperBoundCleanUp(ArithVariables* pm)
: d_pm(pm)
{}
void ArithVariables::UpperBoundCleanUp::operator()(AVCPair* p){
d_pm->popUpperBound(p);
}
} // namespace arith
} // namespace theory
} // namespace cvc5
|