GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/dual_simplex.cpp Lines: 105 120 87.5 %
Date: 2021-11-05 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/env.h"
21
#include "smt/smt_statistics_registry.h"
22
#include "theory/arith/constraint.h"
23
#include "theory/arith/error_set.h"
24
#include "theory/arith/linear_equality.h"
25
26
27
using namespace std;
28
29
namespace cvc5 {
30
namespace theory {
31
namespace arith {
32
33
15271
DualSimplexDecisionProcedure::DualSimplexDecisionProcedure(
34
    Env& env,
35
    LinearEqualityModule& linEq,
36
    ErrorSet& errors,
37
    RaiseConflict conflictChannel,
38
15271
    TempVarMalloc tvmalloc)
39
    : SimplexDecisionProcedure(env, linEq, errors, conflictChannel, tvmalloc),
40
      d_pivotsInRound(),
41
15271
      d_statistics(d_pivots)
42
15271
{ }
43
44
15271
DualSimplexDecisionProcedure::Statistics::Statistics(uint32_t& pivots)
45
15271
    : d_statUpdateConflicts(smtStatisticsRegistry().registerInt(
46
30542
        "theory::arith::dual::UpdateConflicts")),
47
15271
      d_processSignalsTime(smtStatisticsRegistry().registerTimer(
48
30542
          "theory::arith::dual::findConflictOnTheQueueTime")),
49
15271
      d_simplexConflicts(smtStatisticsRegistry().registerInt(
50
30542
          "theory::arith::dual::simplexConflicts")),
51
15271
      d_recentViolationCatches(smtStatisticsRegistry().registerInt(
52
30542
          "theory::arith::dual::recentViolationCatches")),
53
15271
      d_searchTime(smtStatisticsRegistry().registerTimer(
54
30542
          "theory::arith::dual::searchTime")),
55
      d_finalCheckPivotCounter(
56
15271
          smtStatisticsRegistry().registerReference<uint32_t>(
57
91626
              "theory::arith::dual::lastPivots", pivots))
58
{
59
15271
}
60
61
2409389
Result::Sat DualSimplexDecisionProcedure::dualFindModel(bool exactResult){
62
2409389
  Assert(d_conflictVariables.empty());
63
64
  static thread_local unsigned int instance = 0;
65
2409389
  instance = instance + 1;
66
2409389
  d_pivots = 0;
67
68
2409389
  if(d_errorSet.errorEmpty() && !d_errorSet.moreSignals()){
69
1375652
    Debug("arith::findModel") << "dualFindModel("<< instance <<") trivial" << endl;
70
1375652
    return Result::SAT;
71
  }
72
73
  // We need to reduce this because of
74
1033737
  d_errorSet.reduceToSignals();
75
1033737
  d_errorSet.setSelectionRule(options::ErrorSelectionRule::VAR_ORDER);
76
77
1033737
  if(processSignals()){
78
32062
    d_conflictVariables.purge();
79
80
32062
    Debug("arith::findModel") << "dualFindModel("<< instance <<") early conflict" << endl;
81
32062
    return Result::UNSAT;
82
1001675
  }else if(d_errorSet.errorEmpty()){
83
896959
    Debug("arith::findModel") << "dualFindModel("<< instance <<") fixed itself" << endl;
84
896959
    Assert(!d_errorSet.moreSignals());
85
896959
    return Result::SAT;
86
  }
87
88
104716
  Debug("arith::findModel") << "dualFindModel(" << instance <<") start non-trivial" << endl;
89
90
104716
  Result::Sat result = Result::SAT_UNKNOWN;
91
92
104716
  exactResult |= d_varOrderPivotLimit < 0;
93
94
104716
  uint32_t checkPeriod = options().arith.arithSimplexCheckPeriod;
95
104716
  if(result == Result::SAT_UNKNOWN){
96
104716
    uint32_t numDifferencePivots = options().arith.arithHeuristicPivots < 0
97
14168
                                       ? d_numVariables + 1
98
118884
                                       : options().arith.arithHeuristicPivots;
99
    // The signed to unsigned conversion is safe.
100
104716
    if(numDifferencePivots > 0){
101
102
91640
      d_errorSet.setSelectionRule(d_heuristicRule);
103
91640
      if(searchForFeasibleSolution(numDifferencePivots)){
104
12874
        result = Result::UNSAT;
105
      }
106
    }
107
  }
108
104716
  Assert(!d_errorSet.moreSignals());
109
110
104716
  if(!d_errorSet.errorEmpty() && result != Result::UNSAT){
111
14902
    if(exactResult){
112
14902
      d_errorSet.setSelectionRule(options::ErrorSelectionRule::VAR_ORDER);
113
44726
      while(!d_errorSet.errorEmpty() && result != Result::UNSAT){
114
14912
        Assert(checkPeriod > 0);
115
14912
        if(searchForFeasibleSolution(checkPeriod)){
116
3894
          result = Result::UNSAT;
117
        }
118
      }
119
    }
120
    else if (d_varOrderPivotLimit > 0)
121
    {
122
      d_errorSet.setSelectionRule(options::ErrorSelectionRule::VAR_ORDER);
123
      if (searchForFeasibleSolution(d_varOrderPivotLimit))
124
      {
125
        result = Result::UNSAT;
126
      }
127
    }
128
  }
129
130
104716
  Assert(!d_errorSet.moreSignals());
131
104716
  if(result == Result::SAT_UNKNOWN && d_errorSet.errorEmpty()){
132
87948
    result = Result::SAT;
133
  }
134
135
104716
  d_pivotsInRound.purge();
136
  // ensure that the conflict variable is still in the queue.
137
104716
  d_conflictVariables.purge();
138
139
104716
  Debug("arith::findModel") << "end findModel() " << instance << " " << result <<  endl;
140
141
104716
  return result;
142
}
143
144
//corresponds to Check() in dM06
145
//template <SimplexDecisionProcedure::PreferenceFunction pf>
146
106552
bool DualSimplexDecisionProcedure::searchForFeasibleSolution(uint32_t remainingIterations){
147
213104
  TimerStat::CodeTimer codeTimer(d_statistics.d_searchTime);
148
149
106552
  Debug("arith") << "searchForFeasibleSolution" << endl;
150
106552
  Assert(remainingIterations > 0);
151
152
521102
  while(remainingIterations > 0 && !d_errorSet.focusEmpty()){
153
224043
    if(Debug.isOn("paranoid:check_tableau")){ d_linEq.debugCheckTableau(); }
154
224043
    Assert(d_conflictVariables.empty());
155
224043
    ArithVar x_i = d_errorSet.topFocusVariable();
156
157
224043
    Debug("arith::update::select") << "selectSmallestInconsistentVar()=" << x_i << endl;
158
224043
    if(x_i == ARITHVAR_SENTINEL){
159
      Debug("arith::update") << "No inconsistent variables" << endl;
160
16768
      return false; //sat
161
    }
162
163
224043
    --remainingIterations;
164
165
    bool useVarOrderPivot =
166
224043
        d_pivotsInRound.count(x_i) >= options().arith.arithPivotThreshold;
167
224043
    if(!useVarOrderPivot){
168
223859
      d_pivotsInRound.add(x_i);
169
    }
170
171
448086
    Debug("arith::update") << "pivots in rounds: " << d_pivotsInRound.count(x_i)
172
224043
                           << " use " << useVarOrderPivot << " threshold "
173
224043
                           << options().arith.arithPivotThreshold << std::endl;
174
175
224043
    LinearEqualityModule::VarPreferenceFunction pf = useVarOrderPivot ?
176
      &LinearEqualityModule::minVarOrder : &LinearEqualityModule::minBoundAndColLength;
177
178
    //DeltaRational beta_i = d_variables.getAssignment(x_i);
179
224043
    ArithVar x_j = ARITHVAR_SENTINEL;
180
181
224043
    int32_t prevErrorSize CVC5_UNUSED = d_errorSet.errorSize();
182
183
224043
    if(d_variables.cmpAssignmentLowerBound(x_i) < 0 ){
184
118800
      x_j = d_linEq.selectSlackUpperBound(x_i, pf);
185
118800
      if(x_j == ARITHVAR_SENTINEL ){
186
        Unreachable();
187
        // ++(d_statistics.d_statUpdateConflicts);
188
        // reportConflict(x_i);
189
        // ++(d_statistics.d_simplexConflicts);
190
        // Node conflict = d_linEq.generateConflictBelowLowerBound(x_i); //unsat
191
        // d_conflictVariable = x_i;
192
        // reportConflict(conflict);
193
        // return true;
194
      }else{
195
118800
        const DeltaRational& l_i = d_variables.getLowerBound(x_i);
196
118800
        d_linEq.pivotAndUpdate(x_i, x_j, l_i);
197
      }
198
105243
    }else if(d_variables.cmpAssignmentUpperBound(x_i) > 0){
199
105243
      x_j = d_linEq.selectSlackLowerBound(x_i, pf);
200
105243
      if(x_j == ARITHVAR_SENTINEL ){
201
        Unreachable();
202
        // ++(d_statistics.d_statUpdateConflicts);
203
        // reportConflict(x_i);
204
        // ++(d_statistics.d_simplexConflicts);
205
        // Node conflict = d_linEq.generateConflictAboveUpperBound(x_i); //unsat
206
        // d_conflictVariable = x_i;
207
        // reportConflict(conflict);
208
        // return true;
209
      }else{
210
105243
        const DeltaRational& u_i = d_variables.getUpperBound(x_i);
211
105243
        d_linEq.pivotAndUpdate(x_i, x_j, u_i);
212
      }
213
    }
214
224043
    Assert(x_j != ARITHVAR_SENTINEL);
215
216
224043
    bool conflict = processSignals();
217
224043
    int32_t currErrorSize CVC5_UNUSED = d_errorSet.errorSize();
218
224043
    d_pivots++;
219
220
224043
    if(Debug.isOn("arith::dual")){
221
      Debug("arith::dual")
222
        << "#" << d_pivots
223
        << " c" << conflict
224
        << " d" << (prevErrorSize - currErrorSize)
225
        << " f"  << d_errorSet.inError(x_j)
226
        << " h" << d_conflictVariables.isMember(x_j)
227
        << " " << x_i << "->" << x_j
228
        << endl;
229
    }
230
231
224043
    if(conflict){
232
16768
      return true;
233
    }
234
  }
235
89784
  Assert(!d_errorSet.focusEmpty() || d_errorSet.errorEmpty());
236
89784
  Assert(remainingIterations == 0 || d_errorSet.focusEmpty());
237
89784
  Assert(d_errorSet.noSignals());
238
239
89784
  return false;
240
}
241
242
}  // namespace arith
243
}  // namespace theory
244
31125
}  // namespace cvc5