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

Line Exec Source
1
/*********************                                                        */
2
/*! \file simplex.h
3
 ** \verbatim
4
 ** Top contributors (to current version):
5
 **   Tim King, Mathias Preiner, Gereon Kremer
6
 ** This file is part of the CVC4 project.
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.\endverbatim
11
 **
12
 ** \brief This is an implementation of the Simplex Module for the Simplex for
13
 ** DPLL(T) decision procedure.
14
 **
15
 ** This implements the Simplex module for the Simpelx for DPLL(T) decision
16
 ** procedure.
17
 ** See the Simplex for DPLL(T) technical report for more background.(citation?)
18
 ** This shares with the theory a Tableau, and a PartialModel that:
19
 **  - satisfies the equalities in the Tableau, and
20
 **  - the assignment for the non-basic variables satisfies their bounds.
21
 ** This is required to either produce a conflict or satisifying PartialModel.
22
 ** Further, we require being told when a basic variable updates its value.
23
 **
24
 ** During the Simplex search we maintain a queue of variables.
25
 ** The queue is required to contain all of the basic variables that voilate
26
 ** their bounds.
27
 ** As elimination from the queue is more efficient to be done lazily,
28
 ** we do not maintain that the queue of variables needs to be only basic
29
 *( variables or only variables that satisfy their bounds.
30
 **
31
 ** The simplex procedure roughly follows Alberto's thesis. (citation?)
32
 ** There is one round of selecting using a heuristic pivoting rule.
33
 ** (See PreferenceFunction Documentation for the available options.)
34
 ** The non-basic variable is the one that appears in the fewest pivots.
35
 ** (Bruno says that Leonardo invented this first.)
36
 ** After this, Bland's pivot rule is invoked.
37
 **
38
 ** During this proccess, we periodically inspect the queue of variables to
39
 ** 1) remove now extraneous extries,
40
 ** 2) detect conflicts that are "waiting" on the queue but may not be detected
41
 **    by the current queue heuristics, and
42
 ** 3) detect multiple conflicts.
43
 **
44
 ** Conflicts are greedily slackened to use the weakest bounds that still
45
 ** produce the conflict.
46
 **
47
 ** Extra things tracked atm: (Subject to change at Tim's whims)
48
 ** - A superset of all of the newly pivoted variables.
49
 ** - A queue of additional conflicts that were discovered by Simplex.
50
 **   These are theory valid and are currently turned into lemmas
51
 **/
52
53
#include "cvc4_private.h"
54
55
#pragma once
56
57
#include <unordered_map>
58
59
#include "options/arith_options.h"
60
#include "theory/arith/arithvar.h"
61
#include "theory/arith/partial_model.h"
62
#include "util/dense_map.h"
63
#include "util/result.h"
64
#include "util/statistics_registry.h"
65
#include "util/stats_timer.h"
66
67
namespace CVC4 {
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
146368
  void increaseMax() { d_numVariables++; }
162
163
164
1824550
  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
}/* CVC4::theory::arith namespace */
223
}/* CVC4::theory namespace */
224
}/* CVC4 namespace */