GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/simplex.h Lines: 2 13 15.4 %
Date: 2021-05-22 Branches: 0 2 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Tim King, Gereon Kremer, 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
 * This is an implementation of the Simplex Module for the Simplex for
14
 * DPLL(T) decision procedure.
15
 *
16
 * This implements the Simplex module for the Simpelx for DPLL(T) decision
17
 * procedure.
18
 * See the Simplex for DPLL(T) technical report for more background.(citation?)
19
 * This shares with the theory a Tableau, and a PartialModel that:
20
 *  - satisfies the equalities in the Tableau, and
21
 *  - the assignment for the non-basic variables satisfies their bounds.
22
 * This is required to either produce a conflict or satisifying PartialModel.
23
 * Further, we require being told when a basic variable updates its value.
24
 *
25
 * During the Simplex search we maintain a queue of variables.
26
 * The queue is required to contain all of the basic variables that voilate
27
 * their bounds.
28
 * As elimination from the queue is more efficient to be done lazily,
29
 * we do not maintain that the queue of variables needs to be only basic
30
 * variables or only variables that satisfy their bounds.
31
 *
32
 * The simplex procedure roughly follows Alberto's thesis. (citation?)
33
 * There is one round of selecting using a heuristic pivoting rule.
34
 * (See PreferenceFunction Documentation for the available options.)
35
 * The non-basic variable is the one that appears in the fewest pivots.
36
 * (Bruno says that Leonardo invented this first.)
37
 * After this, Bland's pivot rule is invoked.
38
 *
39
 * During this proccess, we periodically inspect the queue of variables to
40
 * 1) remove now extraneous extries,
41
 * 2) detect conflicts that are "waiting" on the queue but may not be detected
42
 *    by the current queue heuristics, and
43
 * 3) detect multiple conflicts.
44
 *
45
 * Conflicts are greedily slackened to use the weakest bounds that still
46
 * produce the conflict.
47
 *
48
 * Extra things tracked atm: (Subject to change at Tim's whims)
49
 * - A superset of all of the newly pivoted variables.
50
 * - A queue of additional conflicts that were discovered by Simplex.
51
 *   These are theory valid and are currently turned into lemmas
52
 */
53
54
#include "cvc5_private.h"
55
56
#pragma once
57
58
#include <unordered_map>
59
60
#include "options/arith_options.h"
61
#include "theory/arith/arithvar.h"
62
#include "theory/arith/partial_model.h"
63
#include "util/dense_map.h"
64
#include "util/result.h"
65
#include "util/statistics_stats.h"
66
67
namespace cvc5 {
68
namespace theory {
69
namespace arith {
70
71
class ErrorSet;
72
class LinearEqualityModule;
73
class Tableau;
74
75
class SimplexDecisionProcedure {
76
protected:
77
  typedef std::vector< std::pair<ArithVar, int> > AVIntPairVec;
78
79
  /** Pivot count of the current round of pivoting. */
80
  uint32_t d_pivots;
81
82
  /** The set of variables that are in conflict in this round. */
83
  DenseSet d_conflictVariables;
84
85
  /** The rule to use for heuristic selection mode. */
86
  options::ErrorSelectionRule d_heuristicRule;
87
88
  /** Linear equality module. */
89
  LinearEqualityModule& d_linEq;
90
91
  /**
92
   * Manages information about the assignment and upper and lower bounds on
93
   * variables.
94
   * Partial model matches that in LinearEqualityModule.
95
   */
96
  ArithVariables& d_variables;
97
98
  /**
99
   * Stores the linear equalities used by Simplex.
100
   * Tableau from the LinearEquality module.
101
   */
102
  Tableau& d_tableau;
103
104
  /** Contains a superset of the basic variables in violation of their bounds. */
105
  ErrorSet& d_errorSet;
106
107
  /** Number of variables in the system. This is used for tuning heuristics. */
108
  ArithVar d_numVariables;
109
110
  /** This is the call back channel for Simplex to report conflicts. */
111
  RaiseConflict d_conflictChannel;
112
113
  /** This is the call back channel for Simplex to report conflicts. */
114
  FarkasConflictBuilder* d_conflictBuilder;
115
116
  /** Used for requesting d_opt, bound and error variables for primal.*/
117
  TempVarMalloc d_arithVarMalloc;
118
119
  /** The size of the error set. */
120
  uint32_t d_errorSize;
121
122
  /** A local copy of 0. */
123
  const Rational d_zero;
124
125
  /** A local copy of 1. */
126
  const Rational d_posOne;
127
128
  /** A local copy of -1. */
129
  const Rational d_negOne;
130
131
  ArithVar constructInfeasiblityFunction(TimerStat& timer);
132
  ArithVar constructInfeasiblityFunction(TimerStat& timer, ArithVar e);
133
  ArithVar constructInfeasiblityFunction(TimerStat& timer, const ArithVarVec& set);
134
135
  void tearDownInfeasiblityFunction(TimerStat& timer, ArithVar inf);
136
  void adjustInfeasFunc(TimerStat& timer, ArithVar inf, const AVIntPairVec& focusChanges);
137
  void addToInfeasFunc(TimerStat& timer, ArithVar inf, ArithVar e);
138
  void removeFromInfeasFunc(TimerStat& timer, ArithVar inf, ArithVar e);
139
  void shrinkInfeasFunc(TimerStat& timer, ArithVar inf, const ArithVarVec& dropped);
140
141
public:
142
  SimplexDecisionProcedure(LinearEqualityModule& linEq, ErrorSet& errors, RaiseConflict conflictChannel, TempVarMalloc tvmalloc);
143
  virtual ~SimplexDecisionProcedure();
144
145
  /**
146
   * Tries to update the assignments of variables such that all of the
147
   * assignments are consistent with their bounds.
148
   * This is done by a simplex search through the possible bases of the tableau.
149
   *
150
   * If all of the variables can be made consistent with their bounds
151
   * SAT is returned. Otherwise UNSAT is returned, and at least 1 conflict
152
   * was reported on the conflictCallback passed to the Module.
153
   *
154
   * Tableau pivoting is performed so variables may switch from being basic to
155
   * nonbasic and vice versa.
156
   *
157
   * Corresponds to the "check()" procedure in [Cav06].
158
   */
159
  virtual Result::Sat findModel(bool exactResult) = 0;
160
161
147913
  void increaseMax() { d_numVariables++; }
162
163
164
1315471
  uint32_t getPivots() const { return d_pivots; }
165
protected:
166
167
  /** Reports a conflict to on the output channel. */
168
  void reportConflict(ArithVar basic);
169
170
  /**
171
   * Checks a basic variable, b, to see if it is in conflict.
172
   * If a conflict is discovered a node summarizing the conflict is returned.
173
   * Otherwise, Node::null() is returned.
174
   */
175
  bool maybeGenerateConflictForBasic(ArithVar basic) const;
176
177
  /** Returns true if a tracked basic variable has a conflict on it. */
178
  bool checkBasicForConflict(ArithVar b) const;
179
180
  /**
181
   * If a basic variable has a conflict on its row,
182
   * this produces a minimized row on the conflict channel.
183
   */
184
  ConstraintCP generateConflictForBasic(ArithVar basic) const;
185
186
187
  /** Gets a fresh variable from TheoryArith. */
188
  ArithVar requestVariable(){
189
    return d_arithVarMalloc.request();
190
  }
191
192
  /** Releases a requested variable from TheoryArith.*/
193
  void releaseVariable(ArithVar v){
194
    d_arithVarMalloc.release(v);
195
  }
196
197
  /** Post condition: !d_queue.moreSignals() */
198
  bool standardProcessSignals(TimerStat &timer, IntStat& conflictStat);
199
200
  struct ArithVarIntPairHashFunc {
201
    size_t operator()(const std::pair<ArithVar, int>& p) const {
202
      size_t h1 = std::hash<ArithVar>()(p.first);
203
      size_t h2 = std::hash<int>()(p.second);
204
      return h1 + 3389 * h2;
205
    }
206
  };
207
208
  typedef std::unordered_map< std::pair<ArithVar, int>, ArithVarVec, ArithVarIntPairHashFunc> sgn_table;
209
210
  static inline int determinizeSgn(int sgn){
211
    return sgn < 0 ? -1 : (sgn == 0 ? 0 : 1);
212
  }
213
214
  void addSgn(sgn_table& sgns, ArithVar col, int sgn, ArithVar basic);
215
  void addRowSgns(sgn_table& sgns, ArithVar basic, int norm);
216
  ArithVar find_basic_in_sgns(const sgn_table& sgns, ArithVar col, int sgn, const DenseSet& m, bool inside);
217
218
  sgn_table::const_iterator find_sgns(const sgn_table& sgns, ArithVar col, int sgn);
219
220
};/* class SimplexDecisionProcedure */
221
222
}  // namespace arith
223
}  // namespace theory
224
}  // namespace cvc5