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 
27  template<class _GM>
28  struct RebindGm{
30  };
31 
32  template<class _GM,class _ACC>
35  };
36 
37  struct Parameter {
39 
40  }
41  template<class P>
42  Parameter(const P & p){
43 
44  }
45  };
46 
47  Bruteforce(const GraphicalModelType&);
48  Bruteforce(const GraphicalModelType&, const Parameter&);
49  std::string name() const { return "Brute-Force"; }
50  const GraphicalModelType& graphicalModel() const { return gm_; }
51  InferenceTermination infer() { EmptyVisitorType visitor; return infer(visitor);}
52  template<class VISITOR> InferenceTermination infer(VISITOR &);
53  InferenceTermination arg(std::vector<LabelType>&, const size_t = 1) const;
54  virtual ValueType value() const;
55  void reset();
56 
57 private:
58  const GraphicalModelType& gm_;
60  std::vector<LabelType> states_;
61  ValueType energy_;
62 };
63 template<class GM, class AKK>
65 (
66  const GraphicalModelType& gm
67 )
68 : gm_(gm),
69  movemaker_(Movemaker<GM>(gm)),
70  states_(std::vector<typename GM::LabelType>(gm.numberOfVariables() )),
71  energy_(typename Bruteforce<GM, AKK>::ValueType())
72 {
73  AKK::neutral(energy_);
74 }
75 
76 template<class GM, class AKK>
77 void
79 {
80  movemaker_.reset();
81  std::fill(states_.begin(), states_.end(), 0);
82  AKK::neutral(energy_);
83 }
84 
85 template<class GM, class AKK>
87 (
88  const GraphicalModelType& gm,
89  const typename Bruteforce<GM, AKK>::Parameter& param
90 )
91 : gm_(gm),
92  movemaker_(Movemaker<GM>(gm)),
93  states_(std::vector<typename GM::LabelType>(gm.numberOfVariables())),
94  energy_(typename Bruteforce<GM, AKK>::ValueType())
95 {}
96 
97 template<class GM, class AKK>
98 template<class VISITOR>
101 (
102  VISITOR & visitor
103 )
104 {
105  std::vector<LabelType> states(gm_.numberOfVariables());
106  std::vector<IndexType> vi(gm_.numberOfVariables());
107  for(size_t j=0; j<gm_.numberOfVariables(); ++j) {
108  vi[j] = j;
109  }
110 
111  AccumulationType::neutral(energy_);
112  bool exitInf = false;
113  visitor.begin(*this);
114  while(exitInf == false) {
115  ValueType energy = movemaker_.move(vi.begin(), vi.end(), states.begin());
116  if(AccumulationType::bop(energy , energy_)) {
117  states_ = states;
118  }
119  AccumulationType::op(energy, energy_);
120  if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
121  exitInf = true;
122  }
123  bool overflow = true;
124  for(size_t j=0; j<gm_.numberOfVariables(); ++j) {
125  if( size_t(states[j]+1) < size_t(gm_.numberOfLabels(j))) {
126  ++states[j];
127  for(size_t k=0; k<j; ++k) {
128  states[k] = 0;
129  }
130  overflow = false;
131  break;
132  }
133  }
134  if(overflow) {
135  break;
136  }
137  }
138  visitor.end(*this);
139  return NORMAL;
140 }
141 
142 template<class GM, class AKK>
145 (
146  std::vector<LabelType>& states,
147  const size_t j
148 ) const
149 {
150  if(j == 1) {
151  states = states_; // copy
152  return NORMAL;
153  }
154  else {
155  return UNKNOWN;
156  }
157 }
158 
160 template<class GM, class ACC>
161 typename GM::ValueType
163 {
164  return energy_;
165 }
166 
167 } // namespace opengm
168 
169 #endif // #ifndef OPENGM_BRUTEFORCE_HXX
The OpenGM namespace.
Definition: config.hxx:43
virtual ValueType value() const
return the solution (value)
Definition: bruteforce.hxx:162
visitors::VerboseVisitor< Bruteforce< GM, ACC > > VerboseVisitorType
Definition: bruteforce.hxx:23
Bruteforce(const GraphicalModelType &)
Definition: bruteforce.hxx:65
InferenceTermination infer()
Definition: bruteforce.hxx:51
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
Bruteforce< _GM, _ACC > type
Definition: bruteforce.hxx:34
std::string name() const
Definition: bruteforce.hxx:49
Brute force inference algorithm.
Definition: bruteforce.hxx:16
Inference algorithm interface.
Definition: inference.hxx:43
InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
Definition: bruteforce.hxx:145
std::vector< LabelType >::const_iterator LabelIterator
Definition: bruteforce.hxx:22
Bruteforce< _GM, ACC > type
Definition: bruteforce.hxx:29
const GraphicalModelType & graphicalModel() const
Definition: bruteforce.hxx:50
#define OPENGM_GM_TYPE_TYPEDEFS
Definition: inference.hxx:13
InferenceTermination
Definition: inference.hxx:24
GraphicalModelType::ValueType ValueType
Definition: bruteforce.hxx:21