summaryrefslogtreecommitdiff
path: root/src/prop/cadical.cpp
blob: 479ec2414cc8e9a815e27fe5991aae64c0e9f1e8 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*********************                                                        */
/*! \file cadical.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Mathias Preiner, Liana Hadarean
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2019 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 Wrapper for CaDiCaL SAT Solver.
 **
 ** Implementation of the CaDiCaL SAT solver for CVC4 (bitvectors).
 **/

#include "prop/cadical.h"

#ifdef CVC4_USE_CADICAL

#include "proof/sat_proof.h"

namespace CVC4 {
namespace prop {

using CadicalLit = int;
using CadicalVar = int;

// helper functions
namespace {

SatValue toSatValue(int result)
{
  if (result == 10) return SAT_VALUE_TRUE;
  if (result == 20) return SAT_VALUE_FALSE;
  Assert(result == 0);
  return SAT_VALUE_UNKNOWN;
}

/* Note: CaDiCaL returns lit/-lit for true/false. Older versions returned 1/-1.
 */
SatValue toSatValueLit(int value)
{
  if (value > 0) return SAT_VALUE_TRUE;
  Assert(value < 0);
  return SAT_VALUE_FALSE;
}

CadicalLit toCadicalLit(const SatLiteral lit)
{
  return lit.isNegated() ? -lit.getSatVariable() : lit.getSatVariable();
}

CadicalVar toCadicalVar(SatVariable var) { return var; }

}  // namespace helper functions

CadicalSolver::CadicalSolver(StatisticsRegistry* registry,
                             const std::string& name)
    : d_solver(new CaDiCaL::Solver()),
      // Note: CaDiCaL variables start with index 1 rather than 0 since negated
      //       literals are represented as the negation of the index.
      d_nextVarIdx(1),
      d_statistics(registry, name)
{
  d_true = newVar();
  d_false = newVar();

  d_solver->set("quiet", 1);  // CaDiCaL is verbose by default
  d_solver->add(toCadicalVar(d_true));
  d_solver->add(0);
  d_solver->add(-toCadicalVar(d_false));
  d_solver->add(0);
}

CadicalSolver::~CadicalSolver() {}

ClauseId CadicalSolver::addClause(SatClause& clause, bool removable)
{
  for (const SatLiteral& lit : clause)
  {
    d_solver->add(toCadicalLit(lit));
  }
  d_solver->add(0);
  ++d_statistics.d_numClauses;
  return ClauseIdError;
}

ClauseId CadicalSolver::addXorClause(SatClause& clause,
                                     bool rhs,
                                     bool removable)
{
  Unreachable("CaDiCaL does not support adding XOR clauses.");
}

SatVariable CadicalSolver::newVar(bool isTheoryAtom,
                                  bool preRegister,
                                  bool canErase)
{
  ++d_statistics.d_numVariables;
  return d_nextVarIdx++;
}

SatVariable CadicalSolver::trueVar() { return d_true; }

SatVariable CadicalSolver::falseVar() { return d_false; }

SatValue CadicalSolver::solve()
{
  TimerStat::CodeTimer codeTimer(d_statistics.d_solveTime);
  SatValue res = toSatValue(d_solver->solve());
  d_okay = (res == SAT_VALUE_TRUE);
  ++d_statistics.d_numSatCalls;
  return res;
}

SatValue CadicalSolver::solve(long unsigned int&)
{
  Unimplemented("Setting limits for CaDiCaL not supported yet");
};

SatValue CadicalSolver::solve(const std::vector<SatLiteral>& assumptions)
{
  TimerStat::CodeTimer codeTimer(d_statistics.d_solveTime);
  for (const SatLiteral& lit : assumptions)
  {
    d_solver->assume(toCadicalLit(lit));
  }
  SatValue res = toSatValue(d_solver->solve());
  d_okay = (res == SAT_VALUE_TRUE);
  ++d_statistics.d_numSatCalls;
  return res;
}

void CadicalSolver::interrupt() { d_solver->terminate(); }

SatValue CadicalSolver::value(SatLiteral l)
{
  Assert(d_okay);
  return toSatValueLit(d_solver->val(toCadicalLit(l)));
}

SatValue CadicalSolver::modelValue(SatLiteral l)
{
  Assert(d_okay);
  return value(l);
}

unsigned CadicalSolver::getAssertionLevel() const
{
  Unreachable("CaDiCal does not support assertion levels.");
}

bool CadicalSolver::ok() const { return d_okay; }

CadicalSolver::Statistics::Statistics(StatisticsRegistry* registry,
                                      const std::string& prefix)
    : d_registry(registry),
      d_numSatCalls("theory::bv::" + prefix + "::cadical::calls_to_solve", 0),
      d_numVariables("theory::bv::" + prefix + "::cadical::variables", 0),
      d_numClauses("theory::bv::" + prefix + "::cadical::clauses", 0),
      d_solveTime("theory::bv::" + prefix + "::cadical::solve_time")
{
  d_registry->registerStat(&d_numSatCalls);
  d_registry->registerStat(&d_numVariables);
  d_registry->registerStat(&d_numClauses);
  d_registry->registerStat(&d_solveTime);
}

CadicalSolver::Statistics::~Statistics() {
  d_registry->unregisterStat(&d_numSatCalls);
  d_registry->unregisterStat(&d_numVariables);
  d_registry->unregisterStat(&d_numClauses);
  d_registry->unregisterStat(&d_solveTime);
}

}  // namespace prop
}  // namespace CVC4

#endif  // CVC4_USE_CADICAL
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback