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> 36 template<
class _GM,
class _ACC>
49 std::string
name()
const;
52 template<
class VISITOR>
60 const GraphicalModelType& gm_;
61 std::vector<int> component_;
75 for(
size_t i=0; i<gm_.numberOfVariables();++i) {
108 template<
class VISITOR>
114 visitor.begin(*
this);
115 typedef std::pair<int, int> clause;
116 typedef boost::adjacency_list<> Graph;
118 Graph g(gm_.numberOfVariables() * 2);
119 for(
size_t f=0; f<gm_.numberOfFactors(); ++f) {
120 if(gm_[f].numberOfVariables() != 2) {
121 throw RuntimeError(
"This implementation of the 2-SAT solver supports only factors of order 2.");
123 std::vector<size_t> vec(2);
124 for(vec[0]=0; vec[0]<2; ++vec[0]) {
125 for(vec[1]=0; vec[1]<2; ++vec[1]) {
126 if(!gm_[f](vec.begin())) {
127 const int v1=gm_[f].variableIndex(0)+(1-vec[0])*gm_.numberOfVariables();
128 const int nv1=gm_[f].variableIndex(0)+(0+vec[0])*gm_.numberOfVariables();
129 const int v2=gm_[f].variableIndex(1)+(1-vec[1])*gm_.numberOfVariables();
130 const int nv2=gm_[f].variableIndex(1)+(0+vec[1])*gm_.numberOfVariables();
131 boost::add_edge(nv1,v2,g);
132 boost::add_edge(nv2,v1,g);
137 component_.resize(num_vertices(g));
138 strong_components(g, make_iterator_property_map(component_.begin(),
get(boost::vertex_index, g)));
144 inline typename GM::ValueType
147 bool satisfied =
true;
148 for(
IndexType i=0; i<gm_.numberOfVariables(); i++) {
149 if(component_[i] == component_[i+gm_.numberOfVariables()]) {
158 #endif // #ifndef OPENGM_INFERENCE_SAT_HXX
#define OPENGM_ASSERT(expression)
SAT(const GraphicalModelType &, const Parameter &=Parameter())
GraphicalModelType::OperatorType OperatorType
const GraphicalModelType & graphicalModel() const
visitors::VerboseVisitor< SAT< GM > > VerboseVisitorType
GraphicalModelType::IndexType IndexType
visitors::EmptyVisitor< SAT< GM > > EmptyVisitorType
InferenceTermination infer()
GraphicalModelType::ValueType ValueType
Inference algorithm interface.
opengm::Or AccumulationType
ValueType value() const
return the solution (value)
Disjunction as a binary operation.
visitors::TimingVisitor< SAT< GM > > TimingVisitorType
Conjunction as a binary operation.