GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/error_set.cpp Lines: 197 308 64.0 %
Date: 2021-08-01 Branches: 167 627 26.6 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Tim King, 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
 * [[ Add one-line brief description here ]]
14
 *
15
 * [[ Add lengthier description here ]]
16
 * \todo document this file
17
 */
18
19
#include "theory/arith/error_set.h"
20
21
#include "smt/smt_statistics_registry.h"
22
#include "theory/arith/constraint.h"
23
24
using namespace std;
25
26
namespace cvc5 {
27
namespace theory {
28
namespace arith {
29
30
31
530313
ErrorInformation::ErrorInformation()
32
  : d_variable(ARITHVAR_SENTINEL)
33
  , d_violated(NullConstraint)
34
  , d_sgn(0)
35
  , d_relaxed(false)
36
  , d_inFocus(false)
37
  , d_handle()
38
  , d_amount(NULL)
39
530313
  , d_metric(0)
40
{
41
530313
  Debug("arith::error::mem") << "def constructor " << d_variable << " "  << d_amount << endl;
42
530313
}
43
44
390538
ErrorInformation::ErrorInformation(ArithVar var, ConstraintP vio, int sgn)
45
  : d_variable(var)
46
  , d_violated(vio)
47
  , d_sgn(sgn)
48
  , d_relaxed(false)
49
  , d_inFocus(false)
50
  , d_handle()
51
  , d_amount(NULL)
52
390538
  , d_metric(0)
53
{
54
390538
  Assert(debugInitialized());
55
390538
  Debug("arith::error::mem") << "constructor " << d_variable << " "  << d_amount << endl;
56
390538
}
57
58
59
2128250
ErrorInformation::~ErrorInformation() {
60
1064125
  Assert(d_relaxed != true);
61
1064125
  if(d_amount != NULL){
62
674
    Debug("arith::error::mem") << d_amount << endl;
63
674
    Debug("arith::error::mem") << "destroy " << d_variable << " "  << d_amount << endl;
64
674
    delete d_amount;
65
674
    d_amount = NULL;
66
  }
67
1064125
}
68
69
143274
ErrorInformation::ErrorInformation(const ErrorInformation& ei)
70
143274
  : d_variable(ei.d_variable)
71
143274
  , d_violated(ei.d_violated)
72
143274
  , d_sgn(ei.d_sgn)
73
143274
  , d_relaxed(ei.d_relaxed)
74
143274
  , d_inFocus(ei.d_inFocus)
75
  , d_handle(ei.d_handle)
76
716370
  , d_metric(0)
77
{
78
143274
  if(ei.d_amount == NULL){
79
143097
    d_amount = NULL;
80
  }else{
81
177
    d_amount = new DeltaRational(*ei.d_amount);
82
  }
83
143274
  Debug("arith::error::mem") << "copy const " << d_variable << " "  << d_amount << endl;
84
143274
}
85
86
780284
ErrorInformation& ErrorInformation::operator=(const ErrorInformation& ei){
87
780284
  d_variable = ei.d_variable;
88
780284
  d_violated = ei.d_violated;
89
780284
  d_sgn = ei.d_sgn;
90
780284
  d_relaxed = (ei.d_relaxed);
91
780284
  d_inFocus = (ei.d_inFocus);
92
780284
  d_handle = (ei.d_handle);
93
780284
  d_metric = ei.d_metric;
94
780284
  if(d_amount != NULL && ei.d_amount != NULL){
95
    Debug("arith::error::mem") << "assignment assign " << d_variable << " "  << d_amount << endl;
96
    *d_amount = *ei.d_amount;
97
780284
  }else if(ei.d_amount != NULL){
98
    d_amount = new DeltaRational(*ei.d_amount);
99
    Debug("arith::error::mem") << "assignment alloc " << d_variable << " "  << d_amount << endl;
100
780284
  }else if(d_amount != NULL){
101
226307
    Debug("arith::error::mem") << "assignment release " << d_variable << " "  << d_amount << endl;
102
226307
    delete d_amount;
103
226307
    d_amount = NULL;
104
  }else{
105
553977
    d_amount = NULL;
106
  }
107
780284
  return *this;
108
}
109
110
1092
void ErrorInformation::reset(ConstraintP c, int sgn){
111
1092
  Assert(!isRelaxed());
112
1092
  Assert(c != NullConstraint);
113
1092
  d_violated = c;
114
1092
  d_sgn = sgn;
115
116
1092
  if(d_amount != NULL){
117
155
    delete d_amount;
118
155
    Debug("arith::error::mem") << "reset " << d_variable << " "  << d_amount << endl;
119
155
    d_amount = NULL;
120
  }
121
1092
}
122
123
283964
void ErrorInformation::setAmount(const DeltaRational& am){
124
283964
  if(d_amount == NULL){
125
226959
    d_amount = new DeltaRational;
126
226959
    Debug("arith::error::mem") << "setAmount " << d_variable << " "  << d_amount << endl;
127
  }
128
283964
  (*d_amount) = am;
129
283964
}
130
131
9838
ErrorSet::Statistics::Statistics()
132
    : d_enqueues(
133
19676
        smtStatisticsRegistry().registerInt("theory::arith::pqueue::enqueues")),
134
9838
      d_enqueuesCollection(smtStatisticsRegistry().registerInt(
135
19676
          "theory::arith::pqueue::enqueuesCollection")),
136
9838
      d_enqueuesDiffMode(smtStatisticsRegistry().registerInt(
137
19676
          "theory::arith::pqueue::enqueuesDiffMode")),
138
9838
      d_enqueuesVarOrderMode(smtStatisticsRegistry().registerInt(
139
19676
          "theory::arith::pqueue::enqueuesVarOrderMode")),
140
9838
      d_enqueuesCollectionDuplicates(smtStatisticsRegistry().registerInt(
141
19676
          "theory::arith::pqueue::enqueuesCollectionDuplicates")),
142
9838
      d_enqueuesVarOrderModeDuplicates(smtStatisticsRegistry().registerInt(
143
59028
          "theory::arith::pqueue::enqueuesVarOrderModeDuplicates"))
144
{
145
9838
}
146
147
9838
ErrorSet::ErrorSet(ArithVariables& vars,
148
                   TableauSizes tabSizes,
149
9838
                   BoundCountingLookup lookups)
150
    : d_variables(vars),
151
      d_errInfo(),
152
      d_selectionRule(options::ErrorSelectionRule::VAR_ORDER),
153
19676
      d_focus(ComparatorPivotRule(this, d_selectionRule)),
154
      d_outOfFocus(),
155
      d_signals(),
156
      d_tableauSizes(tabSizes),
157
29514
      d_boundLookup(lookups)
158
9838
{}
159
160
2268201
options::ErrorSelectionRule ErrorSet::getSelectionRule() const
161
{
162
2268201
  return d_selectionRule;
163
}
164
165
164356
void ErrorSet::recomputeAmount(ErrorInformation& ei,
166
                               options::ErrorSelectionRule rule)
167
{
168
164356
  switch(rule){
169
153710
    case options::ErrorSelectionRule::MINIMUM_AMOUNT:
170
    case options::ErrorSelectionRule::MAXIMUM_AMOUNT:
171
153710
      ei.setAmount(computeDiff(ei.getVariable()));
172
153710
      break;
173
    case options::ErrorSelectionRule::SUM_METRIC:
174
      ei.setMetric(sumMetric(ei.getVariable()));
175
      break;
176
10646
    case options::ErrorSelectionRule::VAR_ORDER:
177
      // do nothing
178
10646
      break;
179
  }
180
164356
}
181
182
824415
void ErrorSet::setSelectionRule(options::ErrorSelectionRule rule)
183
{
184
824415
  if(rule != getSelectionRule()){
185
323464
    FocusSet into(ComparatorPivotRule(this, rule));
186
161732
    FocusSet::const_iterator iter = d_focus.begin();
187
161732
    FocusSet::const_iterator i_end = d_focus.end();
188
490444
    for(; iter != i_end; ++iter){
189
164356
      ArithVar v = *iter;
190
164356
      ErrorInformation& ei = d_errInfo.get(v);
191
164356
      if(ei.inFocus()){
192
164356
        recomputeAmount(ei, rule);
193
164356
        FocusSetHandle handle = into.push(v);
194
164356
        ei.setHandle(handle);
195
      }
196
    }
197
161732
    d_focus.swap(into);
198
161732
    d_selectionRule = rule;
199
  }
200
824415
  Assert(getSelectionRule() == rule);
201
824415
}
202
203
171570
ComparatorPivotRule::ComparatorPivotRule(const ErrorSet* es,
204
171570
                                         options::ErrorSelectionRule r)
205
171570
    : d_errorSet(es), d_rule(r)
206
171570
{}
207
208
1030287
bool ComparatorPivotRule::operator()(ArithVar v, ArithVar u) const {
209
1030287
  switch(d_rule){
210
551264
    case options::ErrorSelectionRule::VAR_ORDER:
211
      // This needs to be the reverse of the minVariableOrder
212
551264
      return v > u;
213
    case options::ErrorSelectionRule::SUM_METRIC:
214
    {
215
      uint32_t v_metric = d_errorSet->getMetric(v);
216
      uint32_t u_metric = d_errorSet->getMetric(u);
217
      if(v_metric == u_metric){
218
        return v > u;
219
      }else{
220
        return v_metric > u_metric;
221
      }
222
    }
223
479023
    case options::ErrorSelectionRule::MINIMUM_AMOUNT:
224
    {
225
479023
      const DeltaRational& vamt = d_errorSet->getAmount(v);
226
479023
      const DeltaRational& uamt = d_errorSet->getAmount(u);
227
479023
      int cmp = vamt.cmp(uamt);
228
479023
      if(cmp == 0){
229
196030
        return v > u;
230
      }else{
231
282993
        return cmp > 0;
232
      }
233
    }
234
    case options::ErrorSelectionRule::MAXIMUM_AMOUNT:
235
    {
236
      const DeltaRational& vamt = d_errorSet->getAmount(v);
237
      const DeltaRational& uamt = d_errorSet->getAmount(u);
238
      int cmp = vamt.cmp(uamt);
239
      if(cmp == 0){
240
        return v > u;
241
      }else{
242
        return cmp < 0;
243
      }
244
    }
245
  }
246
  Unreachable();
247
}
248
249
228833
void ErrorSet::update(ErrorInformation& ei){
250
228833
  if(ei.inFocus()){
251
252
228833
    switch(getSelectionRule()){
253
57122
      case options::ErrorSelectionRule::MINIMUM_AMOUNT:
254
      case options::ErrorSelectionRule::MAXIMUM_AMOUNT:
255
57122
        ei.setAmount(computeDiff(ei.getVariable()));
256
57122
        d_focus.update(ei.getHandle(), ei.getVariable());
257
57122
        break;
258
      case options::ErrorSelectionRule::SUM_METRIC:
259
        ei.setMetric(sumMetric(ei.getVariable()));
260
        d_focus.update(ei.getHandle(), ei.getVariable());
261
        break;
262
171711
      case options::ErrorSelectionRule::VAR_ORDER:
263
        // do nothing
264
171711
        break;
265
    }
266
  }
267
228833
}
268
269
/** A variable becomes satisfied. */
270
298692
void ErrorSet::transitionVariableOutOfError(ArithVar v) {
271
298692
  Assert(!inconsistent(v));
272
298692
  ErrorInformation& ei = d_errInfo.get(v);
273
298692
  Assert(ei.debugInitialized());
274
298692
  if(ei.isRelaxed()){
275
    ConstraintP viol = ei.getViolated();
276
    if(ei.sgn() > 0){
277
      d_variables.setLowerBoundConstraint(viol);
278
    }else{
279
      d_variables.setUpperBoundConstraint(viol);
280
    }
281
    Assert(!inconsistent(v));
282
    ei.setUnrelaxed();
283
  }
284
298692
  if(ei.inFocus()){
285
298692
    d_focus.erase(ei.getHandle());
286
298692
    ei.setInFocus(false);
287
  }
288
298692
  d_errInfo.remove(v);
289
298692
}
290
291
292
390538
void ErrorSet::transitionVariableIntoError(ArithVar v) {
293
390538
  Assert(inconsistent(v));
294
390538
  bool vilb = d_variables.cmpAssignmentLowerBound(v) < 0;
295
390538
  int sgn = vilb ? 1 : -1;
296
781076
  ConstraintP c = vilb ?
297
781076
    d_variables.getLowerBoundConstraint(v) : d_variables.getUpperBoundConstraint(v);
298
390538
  d_errInfo.set(v, ErrorInformation(v, c, sgn));
299
390538
  ErrorInformation& ei = d_errInfo.get(v);
300
301
390538
  switch(getSelectionRule()){
302
73132
    case options::ErrorSelectionRule::MINIMUM_AMOUNT:
303
    case options::ErrorSelectionRule::MAXIMUM_AMOUNT:
304
73132
      ei.setAmount(computeDiff(v));
305
73132
      break;
306
    case options::ErrorSelectionRule::SUM_METRIC:
307
      ei.setMetric(sumMetric(ei.getVariable()));
308
      break;
309
317406
    case options::ErrorSelectionRule::VAR_ORDER:
310
      // do nothing
311
317406
      break;
312
  }
313
390538
  ei.setInFocus(true);
314
390538
  FocusSetHandle handle = d_focus.push(v);
315
390538
  ei.setHandle(handle);
316
390538
}
317
318
void ErrorSet::dropFromFocus(ArithVar v) {
319
  Assert(inError(v));
320
  ErrorInformation& ei = d_errInfo.get(v);
321
  Assert(ei.inFocus());
322
  d_focus.erase(ei.getHandle());
323
  ei.setInFocus(false);
324
  d_outOfFocus.push_back(v);
325
}
326
327
void ErrorSet::addBackIntoFocus(ArithVar v) {
328
  Assert(inError(v));
329
  ErrorInformation& ei = d_errInfo.get(v);
330
  Assert(!ei.inFocus());
331
  switch(getSelectionRule()){
332
    case options::ErrorSelectionRule::MINIMUM_AMOUNT:
333
    case options::ErrorSelectionRule::MAXIMUM_AMOUNT:
334
      ei.setAmount(computeDiff(v));
335
      break;
336
    case options::ErrorSelectionRule::SUM_METRIC:
337
      ei.setMetric(sumMetric(v));
338
      break;
339
    case options::ErrorSelectionRule::VAR_ORDER:
340
      // do nothing
341
      break;
342
  }
343
344
  ei.setInFocus(true);
345
  FocusSetHandle handle = d_focus.push(v);
346
  ei.setHandle(handle);
347
}
348
349
void ErrorSet::blur(){
350
  while(!d_outOfFocus.empty()){
351
    ArithVar v = d_outOfFocus.back();
352
    d_outOfFocus.pop_back();
353
354
    if(inError(v) && !inFocus(v)){
355
      addBackIntoFocus(v);
356
    }
357
  }
358
}
359
360
361
362
7377078
int ErrorSet::popSignal() {
363
7377078
  ArithVar back = d_signals.back();
364
7377078
  d_signals.pop_back();
365
366
7377078
  if(inError(back)){
367
527525
    ErrorInformation& ei = d_errInfo.get(back);
368
527525
    int prevSgn = ei.sgn();
369
527525
    int focusSgn = ei.focusSgn();
370
527525
    bool vilb = d_variables.cmpAssignmentLowerBound(back) < 0;
371
527525
    bool viub = d_variables.cmpAssignmentUpperBound(back) > 0;
372
527525
    if(vilb || viub){
373
228833
      Assert(!vilb || !viub);
374
228833
      int currSgn = vilb ? 1 : -1;
375
228833
      if(currSgn != prevSgn){
376
1618
        ConstraintP curr = vilb ?  d_variables.getLowerBoundConstraint(back)
377
1618
          : d_variables.getUpperBoundConstraint(back);
378
1092
        ei.reset(curr, currSgn);
379
      }
380
228833
      update(ei);
381
    }else{
382
298692
      transitionVariableOutOfError(back);
383
    }
384
527525
    return focusSgn;
385
6849553
  }else if(inconsistent(back)){
386
390538
    transitionVariableIntoError(back);
387
  }
388
6849553
  return 0;
389
}
390
391
void ErrorSet::clear(){
392
  // Nothing should be relaxed!
393
  d_signals.clear();
394
  d_errInfo.purge();
395
  d_focus.clear();
396
}
397
398
void ErrorSet::clearFocus(){
399
  for(ErrorSet::focus_iterator i =focusBegin(), i_end = focusEnd(); i != i_end; ++i){
400
    ArithVar f = *i;
401
    ErrorInformation& fei = d_errInfo.get(f);
402
    fei.setInFocus(false);
403
    d_outOfFocus.push_back(f);
404
  }
405
  d_focus.clear();
406
}
407
408
696062
void ErrorSet::reduceToSignals(){
409
787116
  for(error_iterator ei=errorBegin(), ei_end=errorEnd(); ei != ei_end; ++ei){
410
91054
    ArithVar curr = *ei;
411
91054
    signalVariable(curr);
412
  }
413
414
696062
  d_errInfo.purge();
415
696062
  d_focus.clear();
416
696062
  d_outOfFocus.clear();
417
696062
}
418
419
283964
DeltaRational ErrorSet::computeDiff(ArithVar v) const{
420
283964
  Assert(inconsistent(v));
421
283964
  const DeltaRational& beta = d_variables.getAssignment(v);
422
283964
  DeltaRational diff = d_variables.cmpAssignmentLowerBound(v) < 0 ?
423
142576
    d_variables.getLowerBound(v) - beta:
424
426540
    beta - d_variables.getUpperBound(v);
425
426
283964
  Assert(diff.sgn() > 0);
427
283964
  return diff;
428
}
429
430
void ErrorSet::debugPrint(std::ostream& out) const {
431
  static int instance = 0;
432
  ++instance;
433
  out << "error set debugprint " << instance << endl;
434
  for(error_iterator i = errorBegin(), i_end = errorEnd();
435
      i != i_end; ++i){
436
    ArithVar e = *i;
437
    const ErrorInformation& ei = d_errInfo[e];
438
    ei.print(out);
439
    out << "  ";
440
    d_variables.printModel(e, out);
441
    out << endl;
442
  }
443
  out << "focus ";
444
  for(focus_iterator i = focusBegin(), i_end = focusEnd();
445
      i != i_end; ++i){
446
    out << *i << " ";
447
  }
448
  out << ";" << endl;
449
}
450
451
void ErrorSet::focusDownToJust(ArithVar v) {
452
  clearFocus();
453
454
  ErrorInformation& vei = d_errInfo.get(v);
455
  vei.setInFocus(true);
456
  FocusSetHandle handle = d_focus.push(v);
457
  vei.setHandle(handle);
458
}
459
460
void ErrorSet::pushErrorInto(ArithVarVec& vec) const{
461
  for(error_iterator i = errorBegin(), e = errorEnd(); i != e; ++i ){
462
    vec.push_back(*i);
463
  }
464
}
465
466
void ErrorSet::pushFocusInto(ArithVarVec& vec) const{
467
  for(focus_iterator i = focusBegin(), e = focusEnd(); i != e; ++i ){
468
    vec.push_back(*i);
469
  }
470
}
471
472
}  // namespace arith
473
}  // namespace theory
474
29280
}  // namespace cvc5