Painless
A framework to ease parallelization of sequential CDCL SAT solvers
Loading...
Searching...
No Matches
SolverInterface.hpp
1
6#pragma once
7
8#include "containers/ClauseExchange.hpp"
9#include "containers/ClauseUtils.hpp"
10#include "utils/Logger.hpp"
11
12#include <atomic>
13#include <memory>
14#include <random>
15#include <stdio.h>
16#include <stdlib.h>
17#include <typeindex>
18#include <typeinfo>
19#include <unordered_map>
20#include <vector>
21
22
30/* Forward Declaration */
31class SolverInterface;
32
33using SeedGenerator = std::function<int(SolverInterface*)>;
34
38enum class SatResult
39{
40 SAT = 10,
41 UNSAT = 20,
42 TIMEOUT = 30,
43 UNKNOWN = 0
44};
45
50{
51 CDCL = 0,
52 LOCAL_SEARCH = 1,
53 LOOK_AHEAD = 2,
54 OTHER = 3,
55 UNKNOWN = 255
56};
57
62{
63 public:
68 virtual unsigned int getVariablesCount() = 0;
69
74 virtual int getDivisionVariable() = 0;
75
79 virtual void setSolverInterrupt() = 0;
80
84 virtual void unsetSolverInterrupt() = 0;
85
89 virtual void printWinningLog();
90
96 virtual SatResult solve(const std::vector<int>& cube) = 0;
97
102 virtual void addClause(ClauseExchangePtr clause) = 0;
103
108 virtual void addClauses(const std::vector<ClauseExchangePtr>& clauses) = 0;
109
115 virtual void addInitialClauses(const std::vector<simpleClause>& clauses, unsigned int nbVars) = 0;
116
121 virtual void loadFormula(const char* filename) = 0;
122
126 virtual void printStatistics();
127
132 virtual std::vector<int> getModel() = 0;
133
137 virtual void printParameters();
138
142 virtual void diversify(const SeedGenerator& getSeed = [](SolverInterface* s) { return s->getSolverId(); }) = 0;
143
148 bool isInitialized() { return this->m_initialized; }
149
154 void setInitialized(bool value) { this->m_initialized = value; }
155
161
166 unsigned int getSolverTypeId() { return this->m_solverTypeId; }
167
172 void setSolverTypeId(unsigned int typeId) { this->m_solverTypeId = typeId; }
173
178 unsigned int getSolverId() { return this->m_solverId; }
179
184 void setSolverId(unsigned int id) { this->m_solverId = id; }
185
191 unsigned int getSolverTypeCount() const
192 {
193 auto it = s_instanceCounts.find(std::type_index(typeid(*this)));
194 return (it != s_instanceCounts.end()) ? it->second.load() : 0;
195 }
196
202 SolverInterface(SolverAlgorithmType algoType, int solverId);
203
208
209 protected:
216 template<typename Derived>
217 static unsigned int getAndIncrementTypeCount()
218 {
219 auto [it, inserted] = s_instanceCounts.try_emplace(std::type_index(typeid(Derived)), 0);
220 return it->second.fetch_add(1);
221 }
222
231 template<typename Derived>
233 {
235 LOGDEBUG1("I am solver of type %s: id %d, typeId: %u", typeid(Derived).name(), m_solverId, m_solverTypeId);
236 }
237
238 protected:
240 std::atomic<bool> m_initialized;
241 unsigned int m_solverTypeId;
247 static inline std::unordered_map<std::type_index, std::atomic<unsigned int>> s_instanceCounts;
248};
249
Defines logging functions and macros for the SAT solver.
#define LOGDEBUG1(...)
Log a debug message with verbosity level 1 and blue color.
Definition Logger.hpp:213
Interface for a SAT solver with standard features.
Definition SolverInterface.hpp:62
virtual ~SolverInterface()
Virtual destructor for SolverInterface.
SolverInterface(SolverAlgorithmType algoType, int solverId)
Constructor for SolverInterface.
std::atomic< bool > m_initialized
Definition SolverInterface.hpp:240
virtual void addClauses(const std::vector< ClauseExchangePtr > &clauses)=0
Add a list of permanent clauses to the formula.
virtual void loadFormula(const char *filename)=0
Load formula from a given dimacs file.
static std::unordered_map< std::type_index, std::atomic< unsigned int > > s_instanceCounts
Number of existing instances of derived classes.
Definition SolverInterface.hpp:247
virtual void printStatistics()
Print solver statistics.
unsigned int getSolverTypeCount() const
Get the current count of instances of this object's most-derived type.
Definition SolverInterface.hpp:191
virtual void setSolverInterrupt()=0
Interrupt resolution, solving cannot continue until interrupt is unset.
virtual int getDivisionVariable()=0
Get a variable suitable for search splitting.
bool isInitialized()
Check if the solver is initialized.
Definition SolverInterface.hpp:148
virtual void addInitialClauses(const std::vector< simpleClause > &clauses, unsigned int nbVars)=0
Add a list of initial clauses to the formula.
void setSolverTypeId(unsigned int typeId)
Set the solver type ID.
Definition SolverInterface.hpp:172
unsigned int getSolverTypeId()
Get the solver type ID.
Definition SolverInterface.hpp:166
SolverAlgorithmType m_algoType
Definition SolverInterface.hpp:239
void setSolverId(unsigned int id)
Set the solver ID.
Definition SolverInterface.hpp:184
virtual unsigned int getVariablesCount()=0
Get the current number of variables.
void initializeTypeId()
Initialize the type ID for a derived class.
Definition SolverInterface.hpp:232
virtual void printWinningLog()
Print the winning log.
void setInitialized(bool value)
Set the initialization status of the solver.
Definition SolverInterface.hpp:154
unsigned int getSolverId()
Get the solver ID.
Definition SolverInterface.hpp:178
virtual void addClause(ClauseExchangePtr clause)=0
Add a permanent clause to the formula.
virtual void printParameters()
Print the parameters set for a solver.
int m_solverId
Definition SolverInterface.hpp:242
unsigned int m_solverTypeId
Definition SolverInterface.hpp:241
virtual std::vector< int > getModel()=0
Return the model in case of SAT result.
virtual SatResult solve(const std::vector< int > &cube)=0
Solve the formula with a given cube.
virtual void diversify(const SeedGenerator &getSeed=[](SolverInterface *s) { return s->getSolverId();})=0
Perform native diversification. The Default lambda returns the solver ID.
virtual void unsetSolverInterrupt()=0
Remove the SAT solving interrupt request.
SolverAlgorithmType getAlgoType()
Get the algorithm type of the solver.
Definition SolverInterface.hpp:160
static unsigned int getAndIncrementTypeCount()
Get and increment the instance count for a specific derived type.
Definition SolverInterface.hpp:217
SatResult
Enumeration for SAT solver results.
Definition SolverInterface.hpp:39
SolverAlgorithmType
Enumeration for types of solver algorithms.
Definition SolverInterface.hpp:50