OpenGM  2.3.x
Discrete Graphical Model Library
sat.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_INFERENCE_SAT_HXX
3 #define OPENGM_INFERENCE_SAT_HXX
4 
5 #include <iostream>
6 #include <vector>
7 
8 #include <boost/config.hpp>
9 #include <boost/graph/strong_components.hpp>
10 #include <boost/graph/adjacency_list.hpp>
11 #include <boost/foreach.hpp>
12 
15 #include "opengm/operations/or.hxx"
17 
18 namespace opengm {
19 
23  template<class GM>
24  class SAT : Inference<GM, opengm::Or> {
25  public:
27  typedef GM GraphicalModelType;
29 
30  struct Parameter {};
31 
32  SAT(const GraphicalModelType&, const Parameter& = Parameter());
33  std::string name() const;
34  const GraphicalModelType& graphicalModel() const;
36  template<class VISITOR>
37  InferenceTermination infer(VISITOR &);
38  virtual void reset();
39  ValueType value() const;
43  private:
44  const GraphicalModelType& gm_;
45  std::vector<int> component_;
46  };
47 
48  template<class GM>
49  inline SAT<GM>::SAT
50  (
51  const GraphicalModelType& gm,
52  const Parameter& para
53  )
54  : gm_(gm)
55  {
56  if(!NO_DEBUG) {
57  OPENGM_ASSERT(gm_.factorOrder() <= 2);
58  OPENGM_ASSERT(typeid(OperatorType) == typeid(opengm::And));
59  for(size_t i=0; i<gm_.numberOfVariables();++i) {
60  OPENGM_ASSERT(gm_.numberOfLabels(i) == 2);
61  }
62  }
63  }
64  template<class GM>
65  void
66  inline SAT<GM>::reset()
67  {
68  }
69 
70  template<class GM>
71  inline std::string
72  SAT<GM>::name() const
73  {
74  return "2Sat";
75  }
76 
77  template<class GM>
78  inline const GM&
80  {
81  return gm_;
82  }
83 
84  template<class GM>
88  return infer(v);
89  }
90 
91  template<class GM>
92  template<class VISITOR>
95  (
96  VISITOR & visitor
97  ) {
98  visitor.begin(*this);
99  typedef std::pair<int, int> clause;
100  typedef boost::adjacency_list<> Graph; // properties of our graph. by default: oriented graph
101  // build 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.");
106  }
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);
117  }
118  }
119  }
120  }
121  component_.resize(num_vertices(g));
122  strong_components(g, make_iterator_property_map(component_.begin(), get(boost::vertex_index, g)));
123  visitor.end(*this);
124  return NORMAL;
125  }
126 
127  template<class GM>
128  inline typename GM::ValueType
130  {
131  bool satisfied = true;
132  for(IndexType i=0; i<gm_.numberOfVariables(); i++) {
133  if(component_[i] == component_[i+gm_.numberOfVariables()]) {
134  satisfied = false;
135  }
136  }
137  return satisfied;
138  }
139 
140 } // namespace opengm
141 
142 #endif // #ifndef OPENGM_INFERENCE_SAT_HXX
143 
The OpenGM namespace.
Definition: config.hxx:43
2-SAT solver
Definition: sat.hxx:24
std::string name() const
Definition: sat.hxx:72
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
SAT(const GraphicalModelType &, const Parameter &=Parameter())
Definition: sat.hxx:50
GraphicalModelType::OperatorType OperatorType
Definition: inference.hxx:42
visitors::VerboseVisitor< SAT< GM > > VerboseVisitorType
Definition: sat.hxx:40
GraphicalModelType::IndexType IndexType
Definition: inference.hxx:40
virtual void reset()
Definition: sat.hxx:66
visitors::EmptyVisitor< SAT< GM > > EmptyVisitorType
Definition: sat.hxx:42
GM GraphicalModelType
Definition: sat.hxx:27
InferenceTermination infer()
Definition: sat.hxx:86
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:41
const GraphicalModelType & graphicalModel() const
Definition: sat.hxx:79
Inference algorithm interface.
Definition: inference.hxx:34
ValueType value() const
return the solution (value)
Definition: sat.hxx:129
opengm::Or AccumulationType
Definition: sat.hxx:26
const bool NO_DEBUG
Definition: config.hxx:64
Disjunction as a binary operation.
Definition: or.hxx:10
visitors::TimingVisitor< SAT< GM > > TimingVisitorType
Definition: sat.hxx:41
OPENGM_GM_TYPE_TYPEDEFS
Definition: sat.hxx:28
Conjunction as a binary operation.
Definition: and.hxx:10
OpenGM runtime error.
Definition: opengm.hxx:100
InferenceTermination
Definition: inference.hxx:24