GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/dual_simplex.cpp Lines: 105 120 87.5 %
Date: 2021-09-18 Branches: 195 538 36.2 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Tim King, Aina Niemetz, Morgan Deters
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
 * This is an implementation of the Simplex Module for the Simplex for
14
 * DPLL(T) decision procedure.
15
 */
16
#include "theory/arith/dual_simplex.h"
17
18
#include "base/output.h"
19
#include "options/arith_options.h"
20
#include "smt/smt_statistics_registry.h"
21
#include "theory/arith/constraint.h"
22
#include "theory/arith/error_set.h"
23
#include "theory/arith/linear_equality.h"
24
25
26
using namespace std;
27
28
namespace cvc5 {
29
namespace theory {
30
namespace arith {
31
32
9940
DualSimplexDecisionProcedure::DualSimplexDecisionProcedure(LinearEqualityModule& linEq, ErrorSet& errors, RaiseConflict conflictChannel, TempVarMalloc tvmalloc)
33
  : SimplexDecisionProcedure(linEq, errors, conflictChannel, tvmalloc)
34
  , d_pivotsInRound()
35
9940
  , d_statistics(d_pivots)
36
9940
{ }
37
38
9940
DualSimplexDecisionProcedure::Statistics::Statistics(uint32_t& pivots)
39
9940
    : d_statUpdateConflicts(smtStatisticsRegistry().registerInt(
40
19880
        "theory::arith::dual::UpdateConflicts")),
41
9940
      d_processSignalsTime(smtStatisticsRegistry().registerTimer(
42
19880
          "theory::arith::dual::findConflictOnTheQueueTime")),
43
9940
      d_simplexConflicts(smtStatisticsRegistry().registerInt(
44
19880
          "theory::arith::dual::simplexConflicts")),
45
9940
      d_recentViolationCatches(smtStatisticsRegistry().registerInt(
46
19880
          "theory::arith::dual::recentViolationCatches")),
47
9940
      d_searchTime(smtStatisticsRegistry().registerTimer(
48
19880
          "theory::arith::dual::searchTime")),
49
      d_finalCheckPivotCounter(
50
9940
          smtStatisticsRegistry().registerReference<uint32_t>(
51
59640
              "theory::arith::dual::lastPivots", pivots))
52
{
53
9940
}
54
55
1730336
Result::Sat DualSimplexDecisionProcedure::dualFindModel(bool exactResult){
56
1730336
  Assert(d_conflictVariables.empty());
57
58
  static thread_local unsigned int instance = 0;
59
1730336
  instance = instance + 1;
60
1730336
  d_pivots = 0;
61
62
1730336
  if(d_errorSet.errorEmpty() && !d_errorSet.moreSignals()){
63
927818
    Debug("arith::findModel") << "dualFindModel("<< instance <<") trivial" << endl;
64
927818
    return Result::SAT;
65
  }
66
67
  // We need to reduce this because of
68
802518
  d_errorSet.reduceToSignals();
69
802518
  d_errorSet.setSelectionRule(options::ErrorSelectionRule::VAR_ORDER);
70
71
802518
  if(processSignals()){
72
26696
    d_conflictVariables.purge();
73
74
26696
    Debug("arith::findModel") << "dualFindModel("<< instance <<") early conflict" << endl;
75
26696
    return Result::UNSAT;
76
775822
  }else if(d_errorSet.errorEmpty()){
77
680169
    Debug("arith::findModel") << "dualFindModel("<< instance <<") fixed itself" << endl;
78
680169
    Assert(!d_errorSet.moreSignals());
79
680169
    return Result::SAT;
80
  }
81
82
95653
  Debug("arith::findModel") << "dualFindModel(" << instance <<") start non-trivial" << endl;
83
84
95653
  Result::Sat result = Result::SAT_UNKNOWN;
85
86
  static const bool verbose = false;
87
95653
  exactResult |= d_varOrderPivotLimit < 0;
88
89
95653
  uint32_t checkPeriod = options::arithSimplexCheckPeriod();
90
95653
  if(result == Result::SAT_UNKNOWN){
91
95653
    uint32_t numDifferencePivots = options::arithHeuristicPivots() < 0 ?
92
95653
      d_numVariables + 1 : options::arithHeuristicPivots();
93
    // The signed to unsigned conversion is safe.
94
95653
    if(numDifferencePivots > 0){
95
96
82676
      d_errorSet.setSelectionRule(d_heuristicRule);
97
82676
      if(searchForFeasibleSolution(numDifferencePivots)){
98
11973
        result = Result::UNSAT;
99
      }
100
    }
101
102
    if (verbose && numDifferencePivots > 0)
103
    {
104
      if (result == Result::UNSAT)
105
      {
106
        CVC5Message() << "diff order found unsat" << endl;
107
      }
108
      else if (d_errorSet.errorEmpty())
109
      {
110
        CVC5Message() << "diff order found model" << endl;
111
      }
112
      else
113
      {
114
        CVC5Message() << "diff order missed" << endl;
115
      }
116
    }
117
  }
118
95653
  Assert(!d_errorSet.moreSignals());
119
120
95653
  if(!d_errorSet.errorEmpty() && result != Result::UNSAT){
121
14758
    if(exactResult){
122
14758
      d_errorSet.setSelectionRule(options::ErrorSelectionRule::VAR_ORDER);
123
44286
      while(!d_errorSet.errorEmpty() && result != Result::UNSAT){
124
14764
        Assert(checkPeriod > 0);
125
14764
        if(searchForFeasibleSolution(checkPeriod)){
126
3832
          result = Result::UNSAT;
127
        }
128
      }
129
    }
130
    else if (d_varOrderPivotLimit > 0)
131
    {
132
      d_errorSet.setSelectionRule(options::ErrorSelectionRule::VAR_ORDER);
133
      if (searchForFeasibleSolution(d_varOrderPivotLimit))
134
      {
135
        result = Result::UNSAT;
136
      }
137
      if (verbose)
138
      {
139
        if (result == Result::UNSAT)
140
        {
141
          CVC5Message() << "restricted var order found unsat" << endl;
142
        }
143
        else if (d_errorSet.errorEmpty())
144
        {
145
          CVC5Message() << "restricted var order found model" << endl;
146
        }
147
        else
148
        {
149
          CVC5Message() << "restricted var order missed" << endl;
150
        }
151
      }
152
    }
153
  }
154
155
95653
  Assert(!d_errorSet.moreSignals());
156
95653
  if(result == Result::SAT_UNKNOWN && d_errorSet.errorEmpty()){
157
79848
    result = Result::SAT;
158
  }
159
160
95653
  d_pivotsInRound.purge();
161
  // ensure that the conflict variable is still in the queue.
162
95653
  d_conflictVariables.purge();
163
164
95653
  Debug("arith::findModel") << "end findModel() " << instance << " " << result <<  endl;
165
166
95653
  return result;
167
}
168
169
//corresponds to Check() in dM06
170
//template <SimplexDecisionProcedure::PreferenceFunction pf>
171
97440
bool DualSimplexDecisionProcedure::searchForFeasibleSolution(uint32_t remainingIterations){
172
194880
  TimerStat::CodeTimer codeTimer(d_statistics.d_searchTime);
173
174
97440
  Debug("arith") << "searchForFeasibleSolution" << endl;
175
97440
  Assert(remainingIterations > 0);
176
177
486018
  while(remainingIterations > 0 && !d_errorSet.focusEmpty()){
178
210094
    if(Debug.isOn("paranoid:check_tableau")){ d_linEq.debugCheckTableau(); }
179
210094
    Assert(d_conflictVariables.empty());
180
210094
    ArithVar x_i = d_errorSet.topFocusVariable();
181
182
210094
    Debug("arith::update::select") << "selectSmallestInconsistentVar()=" << x_i << endl;
183
210094
    if(x_i == ARITHVAR_SENTINEL){
184
      Debug("arith::update") << "No inconsistent variables" << endl;
185
15805
      return false; //sat
186
    }
187
188
210094
    --remainingIterations;
189
190
210094
    bool useVarOrderPivot = d_pivotsInRound.count(x_i) >=  options::arithPivotThreshold();
191
210094
    if(!useVarOrderPivot){
192
209978
      d_pivotsInRound.add(x_i);
193
    }
194
195
196
420188
    Debug("arith::update")
197
420188
      << "pivots in rounds: " <<  d_pivotsInRound.count(x_i)
198
210094
      << " use " << useVarOrderPivot
199
420188
      << " threshold " << options::arithPivotThreshold()
200
210094
      << endl;
201
202
210094
    LinearEqualityModule::VarPreferenceFunction pf = useVarOrderPivot ?
203
      &LinearEqualityModule::minVarOrder : &LinearEqualityModule::minBoundAndColLength;
204
205
    //DeltaRational beta_i = d_variables.getAssignment(x_i);
206
210094
    ArithVar x_j = ARITHVAR_SENTINEL;
207
208
210094
    int32_t prevErrorSize CVC5_UNUSED = d_errorSet.errorSize();
209
210
210094
    if(d_variables.cmpAssignmentLowerBound(x_i) < 0 ){
211
111965
      x_j = d_linEq.selectSlackUpperBound(x_i, pf);
212
111965
      if(x_j == ARITHVAR_SENTINEL ){
213
        Unreachable();
214
        // ++(d_statistics.d_statUpdateConflicts);
215
        // reportConflict(x_i);
216
        // ++(d_statistics.d_simplexConflicts);
217
        // Node conflict = d_linEq.generateConflictBelowLowerBound(x_i); //unsat
218
        // d_conflictVariable = x_i;
219
        // reportConflict(conflict);
220
        // return true;
221
      }else{
222
111965
        const DeltaRational& l_i = d_variables.getLowerBound(x_i);
223
111965
        d_linEq.pivotAndUpdate(x_i, x_j, l_i);
224
      }
225
98129
    }else if(d_variables.cmpAssignmentUpperBound(x_i) > 0){
226
98129
      x_j = d_linEq.selectSlackLowerBound(x_i, pf);
227
98129
      if(x_j == ARITHVAR_SENTINEL ){
228
        Unreachable();
229
        // ++(d_statistics.d_statUpdateConflicts);
230
        // reportConflict(x_i);
231
        // ++(d_statistics.d_simplexConflicts);
232
        // Node conflict = d_linEq.generateConflictAboveUpperBound(x_i); //unsat
233
        // d_conflictVariable = x_i;
234
        // reportConflict(conflict);
235
        // return true;
236
      }else{
237
98129
        const DeltaRational& u_i = d_variables.getUpperBound(x_i);
238
98129
        d_linEq.pivotAndUpdate(x_i, x_j, u_i);
239
      }
240
    }
241
210094
    Assert(x_j != ARITHVAR_SENTINEL);
242
243
210094
    bool conflict = processSignals();
244
210094
    int32_t currErrorSize CVC5_UNUSED = d_errorSet.errorSize();
245
210094
    d_pivots++;
246
247
210094
    if(Debug.isOn("arith::dual")){
248
      Debug("arith::dual")
249
        << "#" << d_pivots
250
        << " c" << conflict
251
        << " d" << (prevErrorSize - currErrorSize)
252
        << " f"  << d_errorSet.inError(x_j)
253
        << " h" << d_conflictVariables.isMember(x_j)
254
        << " " << x_i << "->" << x_j
255
        << endl;
256
    }
257
258
210094
    if(conflict){
259
15805
      return true;
260
    }
261
  }
262
81635
  Assert(!d_errorSet.focusEmpty() || d_errorSet.errorEmpty());
263
81635
  Assert(remainingIterations == 0 || d_errorSet.focusEmpty());
264
81635
  Assert(d_errorSet.noSignals());
265
266
81635
  return false;
267
}
268
269
}  // namespace arith
270
}  // namespace theory
271
29574
}  // namespace cvc5