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