diff options
author | Alex Ozdemir <aozdemir@hmc.edu> | 2019-01-18 11:10:26 -0800 |
---|---|---|
committer | Aina Niemetz <aina.niemetz@gmail.com> | 2019-01-18 11:10:26 -0800 |
commit | 8b494ee2e856a0ddb5ea60a1a39340816ba0fc29 (patch) | |
tree | f32dbceda09e06f847451c6f4e537a176e0eca09 /src/proof/dimacs_printer.cpp | |
parent | 7a5e007ea530c97c5f3885958f5d018e350013a7 (diff) |
Extract DIMACS Printing (#2800)
Creating LRAT proofs reuqires writing SAT problems in the DIMACS format.
Before this code was in the LRAT class.
However, since creating ER proofs will also require writing DIMACS, I
decided to extract it.
At the same time I realized that my prior representation of used clauses
was unnecessarily poor. I had chosen it to align with
`CnfProof::collectAtomsForClauses`, but the format is really bad (it
requires extra allocations & manual memory management), and I discovered
that the aforementioned method is super simple, so I'm moving to a
better format.
Diffstat (limited to 'src/proof/dimacs_printer.cpp')
-rw-r--r-- | src/proof/dimacs_printer.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/proof/dimacs_printer.cpp b/src/proof/dimacs_printer.cpp new file mode 100644 index 000000000..48199066e --- /dev/null +++ b/src/proof/dimacs_printer.cpp @@ -0,0 +1,77 @@ +/********************* */ +/*! \file dimacs_printer.cpp + ** \verbatim + ** Top contributors (to current version): + ** Alex Ozdemir + ** This file is part of the CVC4 project. + ** Copyright (c) 2009-2018 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.\endverbatim + ** + ** \brief DIMACS SAT Problem Format + ** + ** Defines serialization for SAT problems as DIMACS + **/ + +#include "proof/dimacs_printer.h" + +#include <iostream> + +namespace CVC4 { +namespace proof { + +// Prints the literal as a (+) or (-) int +// Not operator<< b/c that represents negation as ~ +std::ostream& textOut(std::ostream& o, const prop::SatLiteral& l) +{ + if (l.isNegated()) + { + o << "-"; + } + return o << l.getSatVariable() + 1; +} + +// Prints the clause as a space-separated list of ints +// Not operator<< b/c that represents negation as ~ +std::ostream& textOut(std::ostream& o, const prop::SatClause& c) +{ + for (const auto& l : c) + { + textOut(o, l) << " "; + } + return o << "0"; +} + +void printDimacs( + std::ostream& o, + const std::vector<std::pair<ClauseId, prop::SatClause>>& usedClauses) +{ + size_t maxVar = 0; + for (const auto& c : usedClauses) + { + for (const auto& l : c.second) + { + if (l.getSatVariable() + 1 > maxVar) + { + maxVar = l.getSatVariable() + 1; + } + } + } + o << "p cnf " << maxVar << " " << usedClauses.size() << '\n'; + for (const auto& idAndClause : usedClauses) + { + for (const auto& l : idAndClause.second) + { + if (l.isNegated()) + { + o << '-'; + } + o << l.getSatVariable() + 1 << " "; + } + o << "0\n"; + } +} + +} // namespace proof +} // namespace CVC4 |