-/********************* */
-/*! \file tableau.cpp
- ** \verbatim
- ** Original author: taking
- ** Major contributors: none
- ** Minor contributors (to current version): none
- ** This file is part of the CVC4 prototype.
- ** Copyright (c) 2009, 2010 The Analysis of Computer Systems Group (ACSys)
- ** Courant Institute of Mathematical Sciences
- ** New York University
- ** See the file COPYING in the top-level source directory for licensing
- ** information.\endverbatim
- **
- ** \brief [[ Add one-line brief description here ]]
- **
- ** [[ Add lengthier description here ]]
- ** \todo document this file
- **/
+/******************************************************************************
+ * Top contributors (to current version):
+ * Tim King
+ *
+ * This file is part of the cvc5 project.
+ *
+ * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
+ * in the top-level source directory and their institutional affiliations.
+ * All rights reserved. See the file COPYING in the top-level source
+ * directory for licensing information.
+ * ****************************************************************************
+ *
+ * [[ Add one-line brief description here ]]
+ *
+ * [[ Add lengthier description here ]]
+ * \todo document this file
+ */
+
+#include "base/output.h"
+#include "theory/arith/tableau.h"
+using namespace std;
+namespace cvc5 {
+namespace theory {
+namespace arith {
-#include "theory/arith/tableau.h"
-using namespace CVC4;
-using namespace CVC4::theory;
-using namespace CVC4::theory::arith;
+void Tableau::pivot(ArithVar oldBasic, ArithVar newBasic, CoefficientChangeCallback& cb){
+ Assert(isBasic(oldBasic));
+ Assert(!isBasic(newBasic));
+ Assert(d_mergeBuffer.empty());
-void Tableau::addRow(ArithVar basicVar,
- const std::vector<Rational>& coeffs,
- const std::vector<ArithVar>& variables){
+ Debug("tableau") << "Tableau::pivot(" << oldBasic <<", " << newBasic <<")" << endl;
- Assert(coeffs.size() == variables.size());
- Assert(d_basicManager.isMember(basicVar));
+ RowIndex ridx = basicToRowIndex(oldBasic);
- //The new basic variable cannot already be a basic variable
- Assert(!d_activeBasicVars.isMember(basicVar));
- d_activeBasicVars.add(basicVar);
- ReducedRowVector* row_current = new ReducedRowVector(basicVar,variables, coeffs,d_rowCount);
- d_rowsTable[basicVar] = row_current;
+ rowPivot(oldBasic, newBasic, cb);
+ Assert(ridx == basicToRowIndex(newBasic));
- //A variable in the row may have been made non-basic already.
- //If this is the case we fake pivoting this variable
- vector<ArithVar>::const_iterator varsIter = variables.begin();
- vector<ArithVar>::const_iterator varsEnd = variables.end();
+ loadRowIntoBuffer(ridx);
- for( ; varsIter != varsEnd; ++varsIter){
- ArithVar var = *varsIter;
+ ColIterator colIter = colIterator(newBasic);
+ while(!colIter.atEnd()){
+ EntryID id = colIter.getID();
+ Entry& entry = d_entries.get(id);
- if(d_basicManager.isMember(var)){
- /*
- if(!isActiveBasicVariable(var)){
- reinjectBasic(var);
- }
- Assert(isActiveBasicVariable(var));
- */
- Assert(d_activeBasicVars.isMember(var));
-
- ReducedRowVector& row_var = lookup(var);
- row_current->substitute(row_var);
+ ++colIter; //needs to be incremented before the variable is removed
+ if(entry.getRowIndex() == ridx){ continue; }
+
+ RowIndex to = entry.getRowIndex();
+ Rational coeff = entry.getCoefficient();
+ if(cb.canUseRow(to)){
+ rowPlusBufferTimesConstant(to, coeff, cb);
+ }else{
+ rowPlusBufferTimesConstant(to, coeff);
}
}
+ clearBuffer();
+
+ //Clear the column for used for this variable
+
+ Assert(d_mergeBuffer.empty());
+ Assert(!isBasic(oldBasic));
+ Assert(isBasic(newBasic));
+ Assert(getColLength(newBasic) == 1);
}
-ReducedRowVector* Tableau::removeRow(ArithVar basic){
- Assert(d_basicManager.isMember(basic));
- Assert(d_activeBasicVars.isMember(basic));
+/**
+ * Changes basic to newbasic (a variable on the row).
+ */
+void Tableau::rowPivot(ArithVar basicOld, ArithVar basicNew, CoefficientChangeCallback& cb){
+ Assert(isBasic(basicOld));
+ Assert(!isBasic(basicNew));
+
+ RowIndex rid = basicToRowIndex(basicOld);
- ReducedRowVector* row = d_rowsTable[basic];
+ EntryID newBasicID = findOnRow(rid, basicNew);
- d_activeBasicVars.remove(basic);
- d_rowsTable[basic] = NULL;
+ Assert(newBasicID != ENTRYID_SENTINEL);
- return row;
+ Tableau::Entry& newBasicEntry = d_entries.get(newBasicID);
+ const Rational& a_rs = newBasicEntry.getCoefficient();
+ int a_rs_sgn = a_rs.sgn();
+ Rational negInverseA_rs = -(a_rs.inverse());
+
+ for(RowIterator i = basicRowIterator(basicOld); !i.atEnd(); ++i){
+ EntryID id = i.getID();
+ Tableau::Entry& entry = d_entries.get(id);
+
+ entry.getCoefficient() *= negInverseA_rs;
+ }
+
+ d_basic2RowIndex.remove(basicOld);
+ d_basic2RowIndex.set(basicNew, rid);
+ d_rowIndex2basic.set(rid, basicNew);
+
+ cb.multiplyRow(rid, -a_rs_sgn);
}
-void Tableau::pivot(ArithVar x_r, ArithVar x_s){
- Assert(d_basicManager.isMember(x_r));
- Assert(!d_basicManager.isMember(x_s));
+void Tableau::addRow(ArithVar basic,
+ const std::vector<Rational>& coefficients,
+ const std::vector<ArithVar>& variables)
+{
+ Assert(basic < getNumColumns());
+ Assert(debugIsASet(variables));
+ Assert(coefficients.size() == variables.size());
+ Assert(!isBasic(basic));
+
+ RowIndex newRow = Matrix<Rational>::addRow(coefficients, variables);
+ addEntry(newRow, basic, Rational(-1));
- Debug("tableau") << "Tableau::pivot(" << x_r <<", " <<x_s <<")" << endl;
+ Assert(!d_basic2RowIndex.isKey(basic));
+ Assert(!d_rowIndex2basic.isKey(newRow));
- ReducedRowVector* row_s = d_rowsTable[x_r];
- Assert(row_s != NULL);
- Assert(row_s->has(x_s));
+ d_basic2RowIndex.set(basic, newRow);
+ d_rowIndex2basic.set(newRow, basic);
- //Swap x_r and x_s in d_activeRows
- d_rowsTable[x_s] = row_s;
- d_rowsTable[x_r] = NULL;
- d_activeBasicVars.remove(x_r);
- d_basicManager.remove(x_r);
+ if(Debug.isOn("matrix")){ printMatrix(); }
- d_activeBasicVars.add(x_s);
- d_basicManager.add(x_s);
+ NoEffectCCCB noeffect;
+ NoEffectCCCB* nep = &noeffect;
+ CoefficientChangeCallback* cccb = static_cast<CoefficientChangeCallback*>(nep);
+
+ vector<Rational>::const_iterator coeffIter = coefficients.begin();
+ vector<ArithVar>::const_iterator varsIter = variables.begin();
+ vector<ArithVar>::const_iterator varsEnd = variables.end();
+ for(; varsIter != varsEnd; ++coeffIter, ++varsIter){
+ ArithVar var = *varsIter;
- row_s->pivot(x_s);
+ if(isBasic(var)){
+ Rational coeff = *coeffIter;
- for(ArithVarSet::iterator basicIter = begin(), endIter = end();
- basicIter != endIter; ++basicIter){
- ArithVar basic = *basicIter;
- if(basic == x_s) continue;
+ RowIndex ri = basicToRowIndex(var);
- ReducedRowVector& row_k = lookup(basic);
- if(row_k.has(x_s)){
- row_k.substitute(*row_s);
+ loadRowIntoBuffer(ri);
+ rowPlusBufferTimesConstant(newRow, coeff, *cccb);
+ clearBuffer();
}
}
+
+ if(Debug.isOn("matrix")) { printMatrix(); }
+
+ Assert(debugNoZeroCoefficients(newRow));
+ Assert(debugMatchingCountsForRow(newRow));
+ Assert(getColLength(basic) == 1);
}
-void Tableau::printTableau(){
- Debug("tableau") << "Tableau::d_activeRows" << endl;
-
- typedef RowsTable::iterator table_iter;
- for(table_iter rowIter = d_rowsTable.begin(), end = d_rowsTable.end();
- rowIter != end; ++rowIter){
- ReducedRowVector* row_k = *rowIter;
- if(row_k != NULL){
- row_k->printRow();
- }
- }
+
+void Tableau::removeBasicRow(ArithVar basic){
+ RowIndex rid = basicToRowIndex(basic);
+
+ removeRow(rid);
+ d_basic2RowIndex.remove(basic);
+ d_rowIndex2basic.remove(rid);
}
+void Tableau::substitutePlusTimesConstant(ArithVar to, ArithVar from, const Rational& mult, CoefficientChangeCallback& cb){
+ if(!mult.isZero()){
+ RowIndex to_idx = basicToRowIndex(to);
+ addEntry(to_idx, from, mult); // Add an entry to be cancelled out
+ RowIndex from_idx = basicToRowIndex(from);
-/*
-void Tableau::updateRow(ReducedRowVector* row){
- ArithVar basic = row->basic();
- for(ReducedRowVector::NonZeroIterator i=row->beginNonZero(), endIter = row->endNonZero(); i != endIter; ){
- ArithVar var = i->first;
- ++i;
- if(var != basic && d_basicManager.isMember(var)){
- ReducedRowVector* row_var = isActiveBasicVariable(var) ? lookup(var) : lookupEjected(var);
+ cb.update(to_idx, from, 0, mult.sgn());
- Assert(row_var != row);
- row->substitute(*row_var);
+ loadRowIntoBuffer(from_idx);
+ rowPlusBufferTimesConstant(to_idx, mult, cb);
+ clearBuffer();
+ }
+}
- i = row->beginNonZero();
- endIter = row->endNonZero();
- }
+uint32_t Tableau::rowComplexity(ArithVar basic) const{
+ uint32_t complexity = 0;
+ for(RowIterator i = basicRowIterator(basic); !i.atEnd(); ++i){
+ const Entry& e = *i;
+ complexity += e.getCoefficient().complexity();
+ }
+ return complexity;
+}
+
+double Tableau::avgRowComplexity() const{
+ double sum = 0;
+ uint32_t rows = 0;
+ for(BasicIterator i = beginBasic(), i_end = endBasic(); i != i_end; ++i){
+ sum += rowComplexity(*i);
+ rows++;
}
+ return (rows == 0) ? 0 : (sum/rows);
+}
+
+void Tableau::printBasicRow(ArithVar basic, std::ostream& out){
+ printRow(basicToRowIndex(basic), out);
}
-*/
+
+} // namespace arith
+} // namespace theory
+} // namespace cvc5