LCOV - code coverage report
Current view: top level - buildbot/coverage/build/src/theory/arith/linear - soi_simplex.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 13 15 86.7 %
Date: 2026-03-21 10:41:10 Functions: 3 3 100.0 %
Branches: 5 8 62.5 %

           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 "theory/arith/linear/linear_equality.h"
      56                 :            : #include "theory/arith/linear/simplex.h"
      57                 :            : #include "theory/arith/linear/simplex_update.h"
      58                 :            : #include "util/dense_map.h"
      59                 :            : #include "util/statistics_stats.h"
      60                 :            : 
      61                 :            : namespace cvc5::internal {
      62                 :            : namespace theory {
      63                 :            : namespace arith::linear {
      64                 :            : 
      65                 :            : class SumOfInfeasibilitiesSPD : public SimplexDecisionProcedure {
      66                 :            : public:
      67                 :            :  SumOfInfeasibilitiesSPD(Env& env,
      68                 :            :                          LinearEqualityModule& linEq,
      69                 :            :                          ErrorSet& errors,
      70                 :            :                          RaiseConflict conflictChannel,
      71                 :            :                          TempVarMalloc tvmalloc);
      72                 :            : 
      73                 :            :  Result::Status findModel(bool exactResult) override;
      74                 :            : 
      75                 :            :  // other error variables are dropping
      76                 :            :  WitnessImprovement dualLikeImproveError(ArithVar evar);
      77                 :            :  WitnessImprovement primalImproveError(ArithVar evar);
      78                 :            : 
      79                 :            : private:
      80                 :            :   /** The current sum of infeasibilities variable. */
      81                 :            :   ArithVar d_soiVar;
      82                 :            : 
      83                 :            :   // dual like
      84                 :            :   // - found conflict
      85                 :            :   // - satisfied error set
      86                 :            :   Result::Status sumOfInfeasibilities();
      87                 :            : 
      88                 :            :   int32_t d_pivotBudget;
      89                 :            : 
      90                 :            :   WitnessImprovement d_prevWitnessImprovement;
      91                 :            :   uint32_t d_witnessImprovementInARow;
      92                 :            : 
      93                 :            :   uint32_t degeneratePivotsInARow() const;
      94                 :            : 
      95                 :            :   static constexpr uint32_t s_focusThreshold = 6;
      96                 :            :   static constexpr uint32_t s_maxDegeneratePivotsBeforeBlandsOnLeaving = 100;
      97                 :            :   static constexpr uint32_t s_maxDegeneratePivotsBeforeBlandsOnEntering = 10;
      98                 :            : 
      99                 :            :   DenseMap<uint32_t> d_leavingCountSinceImprovement;
     100                 :       7410 :   void increaseLeavingCount(ArithVar x){
     101         [ +  - ]:       7410 :     if(!d_leavingCountSinceImprovement.isKey(x)){
     102                 :       7410 :       d_leavingCountSinceImprovement.set(x,1);
     103                 :            :     }else{
     104                 :          0 :       (d_leavingCountSinceImprovement.get(x))++;
     105                 :            :     }
     106                 :       7410 :   }
     107                 :      25935 :   LinearEqualityModule::UpdatePreferenceFunction selectLeavingFunction(ArithVar x){
     108         [ +  + ]:      25940 :     bool useBlands = d_leavingCountSinceImprovement.isKey(x) &&
     109         [ -  + ]:          5 :       d_leavingCountSinceImprovement[x] >= s_maxDegeneratePivotsBeforeBlandsOnEntering;
     110         [ -  + ]:      25935 :     if(useBlands) {
     111                 :          0 :       return &LinearEqualityModule::preferWitness<false>;
     112                 :            :     } else {
     113                 :      25935 :       return &LinearEqualityModule::preferWitness<true>;
     114                 :            :     }
     115                 :            :   }
     116                 :            : 
     117                 :            :   void debugPrintSignal(ArithVar updated) const;
     118                 :            : 
     119                 :            :   ArithVarVec d_sgnDisagreements;
     120                 :            : 
     121                 :            :   void logPivot(WitnessImprovement w);
     122                 :            : 
     123                 :            :   void updateAndSignal(const UpdateInfo& selected);
     124                 :            : 
     125                 :            :   UpdateInfo selectUpdate(LinearEqualityModule::UpdatePreferenceFunction upf);
     126                 :            : 
     127                 :            :   // UpdateInfo selectUpdateForDualLike(ArithVar basic){
     128                 :            :   //   TimerStat::CodeTimer codeTimer(d_statistics.d_selectUpdateForDualLike);
     129                 :            : 
     130                 :            :   //   LinearEqualityModule::UpdatePreferenceFunction upf =
     131                 :            :   //     &LinearEqualityModule::preferWitness<true>;
     132                 :            :   //   LinearEqualityModule::VarPreferenceFunction bpf =
     133                 :            :   //     &LinearEqualityModule::minVarOrder;
     134                 :            :   //   return selectPrimalUpdate(basic, upf, bpf);
     135                 :            :   // }
     136                 :            : 
     137                 :            :   // UpdateInfo selectUpdateForPrimal(ArithVar basic, bool useBlands){
     138                 :            :   //   TimerStat::CodeTimer codeTimer(d_statistics.d_selectUpdateForPrimal);
     139                 :            : 
     140                 :            :   //   LinearEqualityModule::UpdatePreferenceFunction upf = useBlands ?
     141                 :            :   //     &LinearEqualityModule::preferWitness<false>:
     142                 :            :   //     &LinearEqualityModule::preferWitness<true>;
     143                 :            : 
     144                 :            :   //   LinearEqualityModule::VarPreferenceFunction bpf = useBlands ?
     145                 :            :   //     &LinearEqualityModule::minVarOrder :
     146                 :            :   //     &LinearEqualityModule::minRowLength;
     147                 :            :   //   bpf = &LinearEqualityModule::minVarOrder;
     148                 :            : 
     149                 :            :   //   return selectPrimalUpdate(basic, upf, bpf);
     150                 :            :   // }
     151                 :            :   // WitnessImprovement selectFocusImproving() ;
     152                 :            :   WitnessImprovement soiRound();
     153                 :            :   WitnessImprovement SOIConflict();
     154                 :            :   std::vector< ArithVarVec > greedyConflictSubsets();
     155                 :            :   bool generateSOIConflict(const ArithVarVec& subset);
     156                 :            : 
     157                 :            :   // WitnessImprovement focusUsingSignDisagreements(ArithVar basic);
     158                 :            :   // WitnessImprovement focusDownToLastHalf();
     159                 :            :   // WitnessImprovement adjustFocusShrank(const ArithVarVec& drop);
     160                 :            :   // WitnessImprovement focusDownToJust(ArithVar v);
     161                 :            : 
     162                 :            :   void adjustFocusAndError(const AVIntPairVec& focusChanges);
     163                 :            : 
     164                 :            :   /**
     165                 :            :    * This is the main simplex for DPLL(T) loop.
     166                 :            :    * It runs for at most maxIterations.
     167                 :            :    *
     168                 :            :    * Returns true iff it has found a conflict.
     169                 :            :    * d_conflictVariable will be set and the conflict for this row is reported.
     170                 :            :    */
     171                 :            :   bool searchForFeasibleSolution(uint32_t maxIterations);
     172                 :            : 
     173                 :      12826 :   bool initialProcessSignals(){
     174                 :      12826 :     TimerStat &timer = d_statistics.d_initialSignalsTime;
     175                 :      12826 :     IntStat& conflictStat  = d_statistics.d_initialConflicts;
     176                 :      12826 :     return standardProcessSignals(timer, conflictStat);
     177                 :            :   }
     178                 :            : 
     179                 :            :   void quickExplain();
     180                 :            :   DenseSet d_qeInSoi;
     181                 :            :   DenseSet d_qeInUAndNotInSoi;
     182                 :            :   ArithVarVec d_qeConflict;
     183                 :            :   ArithVarVec d_qeGreedyOrder;
     184                 :            :   sgn_table d_qeSgns;
     185                 :            : 
     186                 :            :   uint32_t quickExplainRec(uint32_t cEnd, uint32_t uEnd);
     187                 :            :   void qeAddRange(uint32_t begin, uint32_t end);
     188                 :            :   void qeRemoveRange(uint32_t begin, uint32_t end);
     189                 :            :   void qeSwapRange(uint32_t N, uint32_t r, uint32_t s);
     190                 :            : 
     191                 :            :   unsigned trySet(const ArithVarVec& set);
     192                 :            :   unsigned tryAllSubsets(const ArithVarVec& set, unsigned depth, ArithVarVec& tmp);
     193                 :            : 
     194                 :            :   /** These fields are designed to be accessible to TheoryArith methods. */
     195                 :            :   class Statistics {
     196                 :            :   public:
     197                 :            :     TimerStat d_initialSignalsTime;
     198                 :            :     IntStat d_initialConflicts;
     199                 :            : 
     200                 :            :     IntStat d_soiFoundUnsat;
     201                 :            :     IntStat d_soiFoundSat;
     202                 :            :     IntStat d_soiMissed;
     203                 :            : 
     204                 :            :     IntStat d_soiConflicts;
     205                 :            :     IntStat d_hasToBeMinimal;
     206                 :            :     IntStat d_maybeNotMinimal;
     207                 :            : 
     208                 :            :     TimerStat d_soiTimer;
     209                 :            :     TimerStat d_soiFocusConstructionTimer;
     210                 :            :     TimerStat d_soiConflictMinimization;
     211                 :            :     TimerStat d_selectUpdateForSOI;
     212                 :            : 
     213                 :            :     ReferenceStat<uint32_t> d_finalCheckPivotCounter;
     214                 :            : 
     215                 :            :     Statistics(StatisticsRegistry& sr,
     216                 :            :                const std::string& name,
     217                 :            :                uint32_t& pivots);
     218                 :            :   } d_statistics;
     219                 :            : };/* class FCSimplexDecisionProcedure */
     220                 :            : 
     221                 :            : }  // namespace arith
     222                 :            : }  // namespace theory
     223                 :            : }  // namespace cvc5::internal

Generated by: LCOV version 1.14