2 #ifndef OPENGM_INFERENCE_SAT_HXX
3 #define OPENGM_INFERENCE_SAT_HXX
8 #include <boost/config.hpp>
9 #include <boost/graph/strong_components.hpp>
10 #include <boost/graph/adjacency_list.hpp>
11 #include <boost/foreach.hpp>
33 std::string
name()
const;
36 template<
class VISITOR>
44 const GraphicalModelType& gm_;
45 std::vector<int> component_;
51 const GraphicalModelType& gm,
59 for(
size_t i=0; i<gm_.numberOfVariables();++i) {
92 template<
class VISITOR>
99 typedef std::pair<int, int> clause;
100 typedef boost::adjacency_list<> Graph;
102 Graph g(gm_.numberOfVariables() * 2);
103 for(
size_t f=0; f<gm_.numberOfFactors(); ++f) {
104 if(gm_[f].numberOfVariables() != 2) {
105 throw RuntimeError(
"This implementation of the 2-SAT solver supports only factors of order 2.");
107 std::vector<size_t> vec(2);
108 for(vec[0]=0; vec[0]<2; ++vec[0]) {
109 for(vec[1]=0; vec[1]<2; ++vec[1]) {
110 if(!gm_[f](vec.begin())) {
111 const int v1=gm_[f].variableIndex(0)+(1-vec[0])*gm_.numberOfVariables();
112 const int nv1=gm_[f].variableIndex(0)+(0+vec[0])*gm_.numberOfVariables();
113 const int v2=gm_[f].variableIndex(1)+(1-vec[1])*gm_.numberOfVariables();
114 const int nv2=gm_[f].variableIndex(1)+(0+vec[1])*gm_.numberOfVariables();
115 boost::add_edge(nv1,v2,g);
116 boost::add_edge(nv2,v1,g);
121 component_.resize(num_vertices(g));
122 strong_components(g, make_iterator_property_map(component_.begin(),
get(boost::vertex_index, g)));
128 inline typename GM::ValueType
131 bool satisfied =
true;
132 for(
IndexType i=0; i<gm_.numberOfVariables(); i++) {
133 if(component_[i] == component_[i+gm_.numberOfVariables()]) {
142 #endif // #ifndef OPENGM_INFERENCE_SAT_HXX
#define OPENGM_ASSERT(expression)
SAT(const GraphicalModelType &, const Parameter &=Parameter())
GraphicalModelType::OperatorType OperatorType
visitors::VerboseVisitor< SAT< GM > > VerboseVisitorType
GraphicalModelType::IndexType IndexType
visitors::EmptyVisitor< SAT< GM > > EmptyVisitorType
InferenceTermination infer()
GraphicalModelType::ValueType ValueType
const GraphicalModelType & graphicalModel() const
Inference algorithm interface.
ValueType value() const
return the solution (value)
opengm::Or AccumulationType
Disjunction as a binary operation.
visitors::TimingVisitor< SAT< GM > > TimingVisitorType
Conjunction as a binary operation.