libDAI
include/dai/mr.h
Go to the documentation of this file.
00001 /*  This file is part of libDAI - http://www.libdai.org/
00002  *
00003  *  Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
00004  *
00005  *  Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
00006  */
00007 
00008 
00011 
00012 
00013 #ifndef __defined_libdai_mr_h
00014 #define __defined_libdai_mr_h
00015 
00016 
00017 #include <vector>
00018 #include <string>
00019 #include <dai/factorgraph.h>
00020 #include <dai/daialg.h>
00021 #include <dai/enum.h>
00022 #include <dai/properties.h>
00023 #include <dai/exceptions.h>
00024 #include <dai/graph.h>
00025 #include <boost/dynamic_bitset.hpp>
00026 
00027 
00028 namespace dai {
00029 
00030 
00032 
00034 class MR : public DAIAlgFG {
00035     private:
00037         bool supported;
00038 
00040         GraphAL G;
00041 
00043         std::vector<std::vector<Real> >                 tJ;
00045         std::vector<Real>                               theta;
00046 
00048         std::vector<std::vector<Real> >                 M;
00050         std::vector<std::vector<std::vector<Real> > >   cors;
00051 
00053         typedef boost::dynamic_bitset<> sub_nb;
00054         
00056         std::vector<Real> Mag;
00057         
00059         Real _maxdiff;
00060         
00062         size_t _iters;
00063 
00064     public:
00066         struct Properties {
00068 
00072             DAI_ENUM(UpdateType,FULL,LINEAR);
00073 
00075 
00080             DAI_ENUM(InitType,RESPPROP,CLAMPING,EXACT);
00081 
00083             size_t verbose;
00084 
00086             Real tol;
00087 
00089             UpdateType updates;
00090 
00092             InitType inits;
00093         } props;
00094 
00095     public:
00097         MR() : DAIAlgFG(), supported(), G(), tJ(), theta(), M(), cors(), Mag(), _maxdiff(), _iters(), props() {}
00098 
00100 
00105         MR( const FactorGraph &fg, const PropertySet &opts );
00106 
00107 
00109 
00110         virtual MR* clone() const { return new MR(*this); }
00111         virtual MR* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new MR( fg, opts ); }
00112         virtual std::string name() const { return "MR"; }
00113         virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
00114         virtual Factor belief( const VarSet &/*vs*/ ) const;
00115         virtual Factor beliefV( size_t i ) const;
00116         virtual std::vector<Factor> beliefs() const;
00117         virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
00118         virtual void init() {}
00119         virtual void init( const VarSet &/*ns*/ ) { DAI_THROW(NOT_IMPLEMENTED); }
00120         virtual Real run();
00121         virtual Real maxDiff() const { return _maxdiff; }
00122         virtual size_t Iterations() const { return _iters; }
00123         virtual void setProperties( const PropertySet &opts );
00124         virtual PropertySet getProperties() const;
00125         virtual std::string printProperties() const;
00127 
00128     private:
00130         Real calcCavityCorrelations();
00131         
00133         void propagateCavityFields();
00134         
00136         void calcMagnetizations();
00137 
00139 
00142         Real _tJ(size_t i, sub_nb A);
00143         
00145         Real Omega(size_t i, size_t _j, size_t _l);
00146         
00148 
00151         Real T(size_t i, sub_nb A);
00152         
00154         Real T(size_t i, size_t _j);
00155         
00157         Real Gamma(size_t i, size_t _j, size_t _l1, size_t _l2);
00158         
00160         Real Gamma(size_t i, size_t _l1, size_t _l2);
00161         
00163 
00171         Real appM(size_t i, sub_nb A);
00172         
00174 
00179         void sum_subs(size_t j, sub_nb A, Real *sum_even, Real *sum_odd);
00180 };
00181 
00182 
00183 } // end of namespace dai
00184 
00185 
00186 #endif