OpenGM  2.3.x
Discrete Graphical Model Library
bruteforce.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_BRUTEFORCE_HXX
3 #define OPENGM_BRUTEFORCE_HXX
4 
5 #include "inference.hxx"
6 #include "movemaker.hxx"
8 namespace opengm {
9 
10 template<class GM> class Movemaker;
11 
15 template<class GM, class ACC>
16 class Bruteforce : public Inference<GM, ACC>
17 {
18 public:
19  typedef ACC AccumulationType;
20  typedef GM GraphicalModelType;
22  typedef typename std::vector<LabelType>::const_iterator LabelIterator;
26  class Parameter {};
27 
28  Bruteforce(const GraphicalModelType&);
29  Bruteforce(const GraphicalModelType&, const Parameter&);
30  std::string name() const { return "Brute-Force"; }
31  const GraphicalModelType& graphicalModel() const { return gm_; }
32  InferenceTermination infer() { EmptyVisitorType visitor; return infer(visitor);}
33  template<class VISITOR> InferenceTermination infer(VISITOR &);
34  InferenceTermination arg(std::vector<LabelType>&, const size_t = 1) const;
35  virtual ValueType value() const;
36  void reset();
37 
38 private:
39  const GraphicalModelType& gm_;
41  std::vector<LabelType> states_;
42  ValueType energy_;
43 };
44 template<class GM, class AKK>
46 (
47  const GraphicalModelType& gm
48 )
49 : gm_(gm),
50  movemaker_(Movemaker<GM>(gm)),
51  states_(std::vector<typename GM::LabelType>(gm.numberOfVariables() )),
52  energy_(typename Bruteforce<GM, AKK>::ValueType())
53 {
54  AKK::neutral(energy_);
55 }
56 
57 template<class GM, class AKK>
58 void
60 {
61  movemaker_.reset();
62  std::fill(states_.begin(), states_.end(), 0);
63  AKK::neutral(energy_);
64 }
65 
66 template<class GM, class AKK>
68 (
69  const GraphicalModelType& gm,
70  const typename Bruteforce<GM, AKK>::Parameter& param
71 )
72 : gm_(gm),
73  movemaker_(Movemaker<GM>(gm)),
74  states_(std::vector<typename GM::LabelType>(gm.numberOfVariables())),
75  energy_(typename Bruteforce<GM, AKK>::ValueType())
76 {}
77 
78 template<class GM, class AKK>
79 template<class VISITOR>
82 (
83  VISITOR & visitor
84 )
85 {
86  std::vector<LabelType> states(gm_.numberOfVariables());
87  std::vector<IndexType> vi(gm_.numberOfVariables());
88  for(size_t j=0; j<gm_.numberOfVariables(); ++j) {
89  vi[j] = j;
90  }
91 
92  AccumulationType::neutral(energy_);
93  bool exitInf = false;
94  visitor.begin(*this);
95  while(exitInf == false) {
96  ValueType energy = movemaker_.move(vi.begin(), vi.end(), states.begin());
97  if(AccumulationType::bop(energy , energy_)) {
98  states_ = states;
99  }
100  AccumulationType::op(energy, energy_);
101  if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
102  exitInf = true;
103  }
104  bool overflow = true;
105  for(size_t j=0; j<gm_.numberOfVariables(); ++j) {
106  if( size_t(states[j]+1) < size_t(gm_.numberOfLabels(j))) {
107  ++states[j];
108  for(size_t k=0; k<j; ++k) {
109  states[k] = 0;
110  }
111  overflow = false;
112  break;
113  }
114  }
115  if(overflow) {
116  break;
117  }
118  }
119  visitor.end(*this);
120  return NORMAL;
121 }
122 
123 template<class GM, class AKK>
126 (
127  std::vector<LabelType>& states,
128  const size_t j
129 ) const
130 {
131  if(j == 1) {
132  states = states_; // copy
133  return NORMAL;
134  }
135  else {
136  return UNKNOWN;
137  }
138 }
139 
141 template<class GM, class ACC>
142 typename GM::ValueType
144 {
145  return energy_;
146 }
147 
148 } // namespace opengm
149 
150 #endif // #ifndef OPENGM_BRUTEFORCE_HXX
The OpenGM namespace.
Definition: config.hxx:43
visitors::VerboseVisitor< Bruteforce< GM, ACC > > VerboseVisitorType
Definition: bruteforce.hxx:23
const GraphicalModelType & graphicalModel() const
Definition: bruteforce.hxx:31
STL namespace.
Bruteforce(const GraphicalModelType &)
Definition: bruteforce.hxx:46
virtual ValueType value() const
return the solution (value)
Definition: bruteforce.hxx:143
InferenceTermination infer()
Definition: bruteforce.hxx:32
visitors::TimingVisitor< Bruteforce< GM, ACC > > TimingVisitorType
Definition: bruteforce.hxx:25
A fremework for move making algorithms.
Definition: bruteforce.hxx:10
visitors::EmptyVisitor< Bruteforce< GM, ACC > > EmptyVisitorType
Definition: bruteforce.hxx:24
Brute force inference algorithm.
Definition: bruteforce.hxx:16
Inference algorithm interface.
Definition: inference.hxx:34
std::string name() const
Definition: bruteforce.hxx:30
std::vector< LabelType >::const_iterator LabelIterator
Definition: bruteforce.hxx:22
#define OPENGM_GM_TYPE_TYPEDEFS
Definition: inference.hxx:13
InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
Definition: bruteforce.hxx:126
InferenceTermination
Definition: inference.hxx:24
GraphicalModelType::ValueType ValueType
Definition: bruteforce.hxx:21