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 
31  template<class _GM>
32  struct RebindGm{
33  typedef SAT<_GM> type;
34  };
35 
36  template<class _GM,class _ACC>
38  typedef SAT<_GM> type;
39  };
40 
41 
42  struct Parameter{
43  Parameter ( ) {};
44  template<class P>
45  Parameter (const P & p) {};
46  };
47 
48  SAT(const GraphicalModelType&, const Parameter& = Parameter());
49  std::string name() const;
50  const GraphicalModelType& graphicalModel() const;
52  template<class VISITOR>
53  InferenceTermination infer(VISITOR &);
54  virtual void reset();
55  ValueType value() const;
59  private:
60  const GraphicalModelType& gm_;
61  std::vector<int> component_;
62  };
63 
64  template<class GM>
65  inline SAT<GM>::SAT
66  (
67  const GraphicalModelType& gm,
68  const Parameter& para
69  )
70  : gm_(gm)
71  {
72  if(!NO_DEBUG) {
73  OPENGM_ASSERT(gm_.factorOrder() <= 2);
74  OPENGM_ASSERT(typeid(OperatorType) == typeid(opengm::And));
75  for(size_t i=0; i<gm_.numberOfVariables();++i) {
76  OPENGM_ASSERT(gm_.numberOfLabels(i) == 2);
77  }
78  }
79  }
80  template<class GM>
81  void
82  inline SAT<GM>::reset()
83  {
84  }
85 
86  template<class GM>
87  inline std::string
88  SAT<GM>::name() const
89  {
90  return "2Sat";
91  }
92 
93  template<class GM>
94  inline const GM&
96  {
97  return gm_;
98  }
99 
100  template<class GM>
104  return infer(v);
105  }
106 
107  template<class GM>
108  template<class VISITOR>
111  (
112  VISITOR & visitor
113  ) {
114  visitor.begin(*this);
115  typedef std::pair<int, int> clause;
116  typedef boost::adjacency_list<> Graph; // properties of our graph. by default: oriented graph
117  // build 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.");
122  }
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);
133  }
134  }
135  }
136  }
137  component_.resize(num_vertices(g));
138  strong_components(g, make_iterator_property_map(component_.begin(), get(boost::vertex_index, g)));
139  visitor.end(*this);
140  return NORMAL;
141  }
142 
143  template<class GM>
144  inline typename GM::ValueType
146  {
147  bool satisfied = true;
148  for(IndexType i=0; i<gm_.numberOfVariables(); i++) {
149  if(component_[i] == component_[i+gm_.numberOfVariables()]) {
150  satisfied = false;
151  }
152  }
153  return satisfied;
154  }
155 
156 } // namespace opengm
157 
158 #endif // #ifndef OPENGM_INFERENCE_SAT_HXX
159 
The OpenGM namespace.
Definition: config.hxx:43
2-SAT solver
Definition: sat.hxx:24
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
SAT(const GraphicalModelType &, const Parameter &=Parameter())
Definition: sat.hxx:66
GraphicalModelType::OperatorType OperatorType
Definition: inference.hxx:51
std::string name() const
Definition: sat.hxx:88
const GraphicalModelType & graphicalModel() const
Definition: sat.hxx:95
visitors::VerboseVisitor< SAT< GM > > VerboseVisitorType
Definition: sat.hxx:56
SAT< _GM > type
Definition: sat.hxx:33
GraphicalModelType::IndexType IndexType
Definition: inference.hxx:49
virtual void reset()
Definition: sat.hxx:82
visitors::EmptyVisitor< SAT< GM > > EmptyVisitorType
Definition: sat.hxx:58
GM GraphicalModelType
Definition: sat.hxx:27
InferenceTermination infer()
Definition: sat.hxx:102
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:50
Inference algorithm interface.
Definition: inference.hxx:43
opengm::Or AccumulationType
Definition: sat.hxx:26
const bool NO_DEBUG
Definition: config.hxx:64
ValueType value() const
return the solution (value)
Definition: sat.hxx:145
Parameter(const P &p)
Definition: sat.hxx:45
Disjunction as a binary operation.
Definition: or.hxx:10
visitors::TimingVisitor< SAT< GM > > TimingVisitorType
Definition: sat.hxx:57
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