summaryrefslogtreecommitdiff
path: root/src/proof/dimacs.cpp
blob: d9d9f8c1c3bf782d51f21562124fe12caafff13e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*********************                                                        */
/*! \file dimacs.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Alex Ozdemir
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2020 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.h"

#include "base/check.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::unordered_map<ClauseId, prop::SatClause>& clauses,
                 const std::vector<ClauseId>& usedIndices)
{
  size_t maxVar = 0;
  for (const ClauseId i : usedIndices)
  {
    const prop::SatClause& c = clauses.at(i);
    for (const auto& l : c)
    {
      if (l.getSatVariable() + 1 > maxVar)
      {
        maxVar = l.getSatVariable() + 1;
      }
    }
  }
  o << "p cnf " << maxVar << " " << usedIndices.size() << '\n';
  for (const ClauseId i : usedIndices)
  {
    const prop::SatClause& c = clauses.at(i);
    for (const auto& l : c)
    {
      if (l.isNegated())
      {
        o << '-';
      }
      o << l.getSatVariable() + 1 << " ";
    }
    o << "0\n";
  }
}

std::vector<prop::SatClause> parseDimacs(std::istream& in)
{
  std::string tag;
  uint64_t nVars;
  uint64_t nClauses;

  in >> tag;
  Assert(in.good());
  Assert(tag == "p");

  in >> tag;
  Assert(in.good());
  Assert(tag == "cnf");

  in >> nVars;
  Assert(nVars >= 0);

  in >> nClauses;
  Assert(nClauses >= 0);

  std::vector<prop::SatClause> cnf;
  for (uint64_t i = 0; i < nClauses; ++i)
  {
    cnf.emplace_back();
    int64_t lit;
    in >> lit;
    Assert(in.good());
    while (lit != 0)
    {
      cnf.back().emplace_back(std::abs(lit) - 1, lit < 0);
      in >> lit;
      Assert(static_cast<uint64_t>(std::abs(lit)) <= nVars);
      Assert(in.good());
    }
  }

  return cnf;
}

}  // namespace proof
}  // namespace CVC4
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback