bruteforce.hxx

Go to the documentation of this file.
00001 #pragma once
00002 #ifndef OPENGM_BRUTEFORCE_HXX
00003 #define OPENGM_BRUTEFORCE_HXX
00004 
00005 #include "inference.hxx"
00006 #include "movemaker.hxx"
00007 #include "opengm/inference/visitors/visitor.hxx"
00008 
00009 namespace opengm {
00010 
00011 template<class GM> class Movemaker;
00012 
00016 template<class GM, class ACC>
00017 class Bruteforce : public Inference<GM, ACC>
00018 {
00019 public:
00020    typedef ACC AccumulationType;
00021    typedef GM GraphicalModelType;
00022    OPENGM_GM_TYPE_TYPEDEFS;
00023    typedef typename std::vector<LabelType>::const_iterator LabelIterator;
00024    typedef EmptyVisitor< Bruteforce<GM, ACC> > EmptyVisitorType;
00025    typedef VerboseVisitor< Bruteforce<GM, ACC> > VerboseVisitorType;
00026    typedef TimingVisitor< Bruteforce<GM, ACC> > TimingVisitorType;
00027    class Parameter {};
00028 
00029    Bruteforce(const GraphicalModelType&);
00030    Bruteforce(const GraphicalModelType&, const Parameter&);
00031    std::string name() const { return "Brute-Force"; }
00032    const GraphicalModelType& graphicalModel() const { return gm_; }
00033    InferenceTermination infer()                     { EmptyVisitorType visitor; return infer(visitor);}
00034    template<class VISITOR> InferenceTermination infer(VISITOR &);
00035    InferenceTermination arg(std::vector<LabelType>&, const size_t = 1) const; 
00036    virtual ValueType value() const; 
00037    void reset(); 
00038  
00039 private:
00040     const GraphicalModelType& gm_;
00041     opengm::Movemaker<GraphicalModelType> movemaker_;
00042     std::vector<LabelType> states_;
00043     ValueType energy_;
00044 };
00045 
00046 template<class GM, class AKK>
00047 Bruteforce<GM, AKK>::Bruteforce
00048 (
00049     const GraphicalModelType& gm
00050 )
00051 :  gm_(gm),
00052    movemaker_(Movemaker<GM>(gm)),
00053    states_(std::vector<typename GM::LabelType>(gm.numberOfVariables() )),
00054    energy_(typename Bruteforce<GM, AKK>::ValueType())
00055 {
00056    AKK::neutral(energy_);
00057 }
00058 
00059 template<class GM, class AKK>
00060 void
00061 Bruteforce<GM, AKK>::reset()
00062 {
00063    movemaker_.reset();
00064    std::fill(states_.begin(), states_.end(), 0);
00065    AKK::neutral(energy_);
00066 }
00067 
00068 template<class GM, class AKK>
00069 Bruteforce<GM, AKK>::Bruteforce
00070 (
00071    const GraphicalModelType& gm,
00072    const typename Bruteforce<GM, AKK>::Parameter& param
00073 )
00074 :  gm_(gm),
00075    movemaker_(Movemaker<GM>(gm)),
00076    states_(std::vector<typename GM::LabelType>(gm.numberOfVariables())),
00077    energy_(typename Bruteforce<GM, AKK>::ValueType())
00078 {}
00079 
00080 template<class GM, class AKK>
00081 template<class VISITOR>
00082 InferenceTermination
00083 Bruteforce<GM, AKK>::infer
00084 (
00085    VISITOR & visitor
00086 )
00087 {
00088    std::vector<LabelType> states(gm_.numberOfVariables());
00089    std::vector<IndexType> vi(gm_.numberOfVariables());
00090    for(size_t j=0; j<gm_.numberOfVariables(); ++j) {
00091        vi[j] = j;
00092    }
00093 
00094    AccumulationType::neutral(energy_); 
00095    visitor.begin(*this);
00096    for(;;) {
00097       ValueType energy = movemaker_.move(vi.begin(), vi.end(), states.begin());
00098       if(AccumulationType::bop(energy , energy_)) {
00099          states_ = states;
00100       } 
00101       AccumulationType::op(energy, energy_); 
00102       visitor(*this);
00103       bool overflow = true;
00104       for(size_t j=0; j<gm_.numberOfVariables(); ++j) {
00105          if( size_t(states[j]+1) < size_t(gm_.numberOfLabels(j))) {
00106             ++states[j];
00107             for(size_t k=0; k<j; ++k) {
00108                states[k] = 0;
00109             }
00110             overflow = false;
00111             break;
00112          }
00113       }
00114       if(overflow) {
00115          break;
00116       }
00117    }
00118    visitor.end(*this);
00119    return NORMAL;
00120 }
00121 
00122 template<class GM, class AKK>
00123 inline InferenceTermination
00124 Bruteforce<GM, AKK>::arg
00125 (
00126    std::vector<LabelType>& states,
00127    const size_t j
00128 ) const
00129 {
00130    if(j == 1) {
00131       states = states_; // copy
00132       return NORMAL;
00133    }
00134    else {
00135       return UNKNOWN;
00136    }
00137 }
00138 
00140 template<class GM, class ACC>
00141 typename GM::ValueType
00142 Bruteforce<GM, ACC>::value() const 
00143 {
00144    return energy_;
00145 } 
00146 
00147 } // namespace opengm
00148 
00149 #endif // #ifndef OPENGM_BRUTEFORCE_HXX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Jun 17 16:31:01 2013 for OpenGM by  doxygen 1.6.3